<?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']);
    $stock_id = $_REQUEST['stock_id'];

    $language_translate = array(
        'en_us' => 'en',
        'ge_ge' => 'de',
        'pl_pl' => 'pl'
    );

    $use_language = null;

    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`.`index_dbf`";
    $query .= ", `pr`.`unit_name` as unit_name";
    $query .= ", `pr`.`name`";
    $query .= ", `pr`.`selling_price`";
    $query .= ", `pr`.`purchase_price`";
    $query .= ", `pr`.`srp_price`";
    $query .= ", `pr`.`fob_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`";
    $query .= ", `pr`.`type` as `type`";

    if (isset($use_language) && false == is_null($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`.`name`) LIKE '%$AS_INPUTSEARCH%'";

    if (isset($use_language)) {
        $query .= " || UPPER(`pr_lang`.`long_description`) LIKE '%$AS_INPUTSEARCH%'";
        $query .= " || UPPER(`pr_lang`.`short_description`) LIKE '%$AS_INPUTSEARCH%'";
    }

    $query .= ')';

//    $types = array('article', 'income', 'service');
//    $query .= ' AND `type` ' . (in_array($type = $_REQUEST['type'], $types) ? 1 : '\'article\'') . '';

    $query .= ' AND pr.`type` ' . ((@$_REQUEST['type'] == 'service') ? '=' : '!=' ) . ' \'service\'';
    $query .= ' AND pr.`deleted`=\'0\'';

    $result = $GLOBALS['db']->query($query);

    global $sugar_config;
    global $app_list_strings;

    $defaultCurrency = $sugar_config['default_currency_symbol'];
    $currencies = array(-99 => $defaultCurrency);

    $arr = array();
    include_once("modules/EcmStockOperations/EcmStockOperation.php");
    $op = new EcmStockOperation();
    if ($result)
        while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
            //$row['unit_id']=$row['unit_name'];
            $lv = return_app_list_strings_language($_REQUEST['ecmlanguage']);
            $row['unit_name'] = $row['unit_name'];
            $row['stock'] = $op->getStock($row['id'], $stock_id);
            $row['temp_item_id'] = create_guid();
            $row['temp_date'] = date("Y-m-d H:i:s");
            $row['price'] = $row['selling_price'];

            $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)) {
              if(strpos(strtoupper($row['short_description']), $AS_INPUTSEARCH) !== false)
              $row['name'] = $row['short_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['short_description']) && $row['short_description'] != '')
              $row['name'] = $row['short_description'];
              else
              if(isset($row['short_description']) && $row['short_description'] != '')
              $row['name'] = $row['short_description'];
              }
              unset($row['short_description'], $row['short_description']);
              }
             */
            $row['code'] = $row['index_dbf'];

            $arr[] = $row;
        }

    if (count($arr) > 0) {
        $json = getJSONobj();
        echo str_replace("&quot;", '\"', $json->encode($arr));
    }
}