<?php

	if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
	
	if(isset($_REQUEST['as_inputSearch']) && $_REQUEST['as_inputSearch'] != '') {
		$AS_INPUTSEARCH = strtoupper($_REQUEST['as_inputSearch']);

		$language_translate = array(
			'en_us' => 'en',
			'ge_ge' => 'de',
			'pl_pl' => 'pl'
		);
		
		if(isset($_REQUEST['ecmlanguage']) && $_REQUEST['ecmlanguage'] != '') {
			if(isset($language_translate[$_REQUEST['ecmlanguage']]) && $language_translate[$_REQUEST['ecmlanguage']] != '') {
				$use_language = $language_translate[$_REQUEST['ecmlanguage']];
			}
		}
		
		$query = "SELECT DISTINCT";
				$query .= " `pr`.`id`";
				$query .= ", `pr`.`code`";
				$query .= ", `pr`.`unit_id` as unitid";
				$query .= ", `pr`.`name`";
				$query .= ", `pr`.`selling_price`";
				$query .= ", `pr`.`purchase_price`";
				$query .= ", `pr`.`vat_id`";
				$query .= ", `pr`.`vat_name`";
				$query .= ", `pr`.`vat_value`";
				$query .= ", `pr`.`exchange_rate_id` as `currency_id`";
				$query .= ", `pr`.`product_category_id` as `category_id`";
				$query .= ", `pr`.`usage_unit_id` as `unit_id`";
				
				/*if(isset($use_language)) {
					$query .= ", `pr_lang`.`short_description`";
					$query .= ", `pr_lang`.`long_description`";
				}*/
		 
		$query .= " FROM";
				$query .= " `ecmproducts` as `pr`";
				//if(isset($use_language))
					//$query .= " RIGHT JOIN `ecmproduct_language_".$use_language."_view` as `pr_lang` ON `pr`.`id` = `pr_lang`.`ecmproduct_id`";
				 
		$query .= " WHERE";
				$query .= " (";
					$query .= "UPPER(`pr`.`code`) LIKE '%$AS_INPUTSEARCH'";
					$query .= " || UPPER(`pr`.`code`) LIKE '$AS_INPUTSEARCH%'";
					$query .= " || UPPER(`pr`.`code`) LIKE '%$AS_INPUTSEARCH%'";
					$query .= " || UPPER(`pr`.`name`) LIKE '%$AS_INPUTSEARCH'";
					$query .= " || UPPER(`pr`.`name`) LIKE '$AS_INPUTSEARCH%'";
					$query .= " || UPPER(`pr`.`name`) LIKE '%$AS_INPUTSEARCH%'";
					
					/*if(isset($use_language)) {
						$query .= " || UPPER(`pr_lang`.`long_description`) LIKE '%$AS_INPUTSEARCH'";
						$query .= " || UPPER(`pr_lang`.`long_description`) LIKE '%$AS_INPUTSEARCH%'";
						$query .= " || UPPER(`pr_lang`.`long_description`) LIKE '$AS_INPUTSEARCH%'";	
						$query .= " || UPPER(`pr_lang`.`short_description`) LIKE '%$AS_INPUTSEARCH'";
						$query .= " || UPPER(`pr_lang`.`short_description`) LIKE '%$AS_INPUTSEARCH%'";
						$query .= " || UPPER(`pr_lang`.`short_description`) LIKE '$AS_INPUTSEARCH%'";
					}*/
					
				$query .= ")";
				$query .= " AND `pr`.`deleted`='0'";
		$result = $GLOBALS['db']->query($query);
		
		
		global $sugar_config;
		$defaultCurrency = $sugar_config['default_currency_symbol'];
		$currencies = array ( -99 => $defaultCurrency );

		$arr = array();
		if($result)
			while($row = $GLOBALS['db']->fetchByAssoc($result)) {
				$op = new EcmStockOperation();			
				$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select quantity from ecmstockstates where product_id='".$row['id']."' and stock_id='".$_REQUEST['stock_id']."' and deleted='0'"));
				$row['on_stock']=$op->getStock($row['id'], $_REQUEST['stock_id']);
				if ($row['category_id']=='d7f876b0-1a3d-43a1-7c9b-511ba40df3d1') $row['on_stock'] ='-';
				//$row['on_stock']=number_format($r['quantity'],0,"","");
				//$row['on_stock']=5;
				$row['unit_id']=$row['unitid'];
				$row['unit_name']=$app_list_strings['ecmproducts_unit_dom'][$row['unitid']];
				
				$row['price'] = $row['srp_price'];
				
				if (!$row['price']) $row['price'] = 0;
				
				$row['purchase_price'] = format_number($row['purchase_price']);
				
				$row['selling_price'] = format_number($row['selling_price']);
				
				
				if(array_key_exists($row['currency_id'],$currencies))
					$row['currency_symbol']  = $currencies[$row['currency_id']];
				else {
				
					$query = "SELECT symbol FROM currencies WHERE id='".$row['currency_id']."' AND deleted=0;";
					$result2 = $GLOBALS['db']->query($query);
					if($result2) {
						$row2 = $GLOBALS['db']->fetchByAssoc($result2);
						if($row2) {
							$currencies[$id] = $row2['symbol'];
							$row['currency_symbol']  = $row2['symbol'];
						} else $row['currency_symbol']  = '';
					} else $row['currency_symbol']  = '';
					
				}
/*
				if(isset($use_language) && $use_language!="pl") {/*
					if(strpos(strtoupper($row['long_description']), $AS_INPUTSEARCH) !== false)
						$row['name'] = $row['long_description'];
					else
						if(strpos(strtoupper($row['short_description']), $AS_INPUTSEARCH) !== false)
							$row['name'] = $row['short_description'];
						else
							if(strpos(strtoupper($row['name']), $AS_INPUTSEARCH) === false || strpos(strtoupper($row['code']), $AS_INPUTSEARCH) !== false) {
								if(isset($row['long_description']) && $row['long_description'] != '')
									$row['name'] = $row['long_description'];
									else
										if(isset($row['short_description']) && $row['short_description'] != '')
											$row['name'] = $row['short_description'];
							}					
					unset($row['long_description'], $row['short_description']);/
					if($row['long_description'])$row['name']=$row['long_description'];
					elseif(!$row['long_description'] && $row['short_description'])$row['name']=$row['short_description'];
					elseif(!$row['long_description'] && !$row['short_description'])$row['name']=$row['name'];
					unset($row['long_description'],$row['short_desciption']);
					
				}
	*/			
				$arr[] = $row;
		
			}
			
		if(count($arr) > 0) { 
			$json = getJSONobj();
			echo str_replace("&quot;", '\"', $json->encode($arr));
		}
	}


?>