279 lines
9.1 KiB
PHP
279 lines
9.1 KiB
PHP
|
|
<?php
|
||
|
|
if(!defined('sugarEntry') || !sugarEntry) die('-1');
|
||
|
|
if (!$_POST['job'] || $_POST['job']=='') die ('-1');
|
||
|
|
switch ($_POST['job']) {
|
||
|
|
case 'getParentInfo': getParentInfo($_POST['id'], $_POST['type']); break;
|
||
|
|
case 'generateNumber': generateNumber($_POST['stock'],$_POST['date']); break;
|
||
|
|
case 'searchProducts': searchProducts($_POST['searchKey'], $_POST['searchCategory'], $_POST['searchStock'], $_POST['searchSort'], $_POST['searchStockId']); break;
|
||
|
|
case 'getProduct': getProduct($_POST['id'], $_POST['stockId']); break;
|
||
|
|
case 'getItems': getItems($_POST['record']); break;
|
||
|
|
case 'getCategoriesList': getCategoriesList(); break;
|
||
|
|
case 'getTranslation': getTranslation($_POST['product_id'], $_POST['language'], $_REQUEST['unit_id']); break;
|
||
|
|
case 'getPricesInfo': getPricesInfo($_POST['product_id'], $_POST['pricebook_id'], $_POST['account_id']); break;
|
||
|
|
case 'getStockArray' : getStockArray($_POST['product_id']); break;
|
||
|
|
case 'getPurchaseArray' : getPurchaseArray($_POST['product_id']); break;
|
||
|
|
case 'getStockState' : getStockState($_POST['id'], $_POST['stockId']); break;
|
||
|
|
case 'getComponents' : getComponents($_POST['product_id']); break;
|
||
|
|
case 'AutocompliteProductCode' : AutocompliteProductCode($_POST['code']); break;
|
||
|
|
}
|
||
|
|
|
||
|
|
function AutocompliteProductCode($code){
|
||
|
|
global $current_language;
|
||
|
|
$app_list_strings = return_app_list_strings_language($current_language);
|
||
|
|
$db = $GLOBALS ['db'];
|
||
|
|
$result = array();
|
||
|
|
$q = '
|
||
|
|
SELECT
|
||
|
|
product.id AS id,
|
||
|
|
product.name AS name,
|
||
|
|
product.code AS code,
|
||
|
|
product.product_category_id AS product_category_id,
|
||
|
|
product_category.name AS product_category_name,
|
||
|
|
product.unit_id AS dd_unit_id
|
||
|
|
FROM
|
||
|
|
ecmproducts AS product
|
||
|
|
LEFT JOIN
|
||
|
|
ecmproductcategories AS product_category ON product.product_category_id = product_category.id
|
||
|
|
WHERE
|
||
|
|
product.deleted = 0
|
||
|
|
AND product.code LIKE "%' . $code .'%";';
|
||
|
|
$rs = $db->query($q);
|
||
|
|
if ($rs->num_rows > 0) {
|
||
|
|
while ($row = $db->fetchByAssoc($rs)) {
|
||
|
|
$a_json_row ['id'] = $row ['id'];
|
||
|
|
$a_json_row ['name'] = $row ['name'];
|
||
|
|
$a_json_row ['code'] = $row ['code'];
|
||
|
|
$a_json_row ['product_category_id'] = $row ['product_category_id'];
|
||
|
|
$a_json_row ['product_category_name'] = $row ['product_category_name'];
|
||
|
|
$a_json_row ['dd_unit_id'] = $row ['dd_unit_id'];
|
||
|
|
$a_json_row ['label'] = "Kategoria: <span style='color: blue;'>" . $a_json_row ['product_category_name'] . "</span><br>Indeks: <span style='color: blue;'>" . $row ['code'] . "</span><br>Nazwa: <span style='color: blue;'>" . $row ['name'] . '</span>';
|
||
|
|
$a_json_row ['value'] = '';
|
||
|
|
$result [] = $a_json_row;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$a_json_row ['id'] = '';
|
||
|
|
$a_json_row ['name'] = '';
|
||
|
|
$a_json_row ['indeks'] = '';
|
||
|
|
$a_json_row ['time'] = '';
|
||
|
|
$a_json_row ['cost_action'] = '';
|
||
|
|
$a_json_row ['cost_hour'] = '';
|
||
|
|
$a_json_row ['category'] = '';
|
||
|
|
$a_json_row ["label"] = "<span style='color: red;'>Brak czynności o szukanej nazwie</span>";
|
||
|
|
$a_json_row ['value'] = '';
|
||
|
|
$result [] = $a_json_row;
|
||
|
|
}
|
||
|
|
$json = json_encode($result);
|
||
|
|
print $json;
|
||
|
|
}
|
||
|
|
|
||
|
|
function generateNumber($mag,$date) {
|
||
|
|
$data = array();
|
||
|
|
$data['number'] = EcmInventory::generateNumber($date);
|
||
|
|
$data['document_no'] = EcmInventory::generateDocNumberStock($mag,$date);
|
||
|
|
|
||
|
|
echo json_encode($data);
|
||
|
|
unset($data);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function searchProducts($searchKey, $searchCategory, $searchStock, $searchSort, $searchStockId) {
|
||
|
|
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$q = "SELECT id, code, name FROM ecmproducts WHERE
|
||
|
|
(UPPER(code) LIKE '%$searchKey%' OR
|
||
|
|
UPPER(name) LIKE '%$searchKey%')
|
||
|
|
AND deleted='0' ";
|
||
|
|
if ($searchCategory && $searchCategory!="")
|
||
|
|
$q.="AND product_category_id='$searchCategory' ";
|
||
|
|
|
||
|
|
if ($searchSort=='1')
|
||
|
|
$q.="ORDER BY code";
|
||
|
|
else if ($searchSort=='2')
|
||
|
|
$q.="ORDER BY code DESC";
|
||
|
|
else if ($searchSort=='3')
|
||
|
|
$q.="ORDER BY name";
|
||
|
|
else if ($searchSort=='4')
|
||
|
|
$q.="ORDER BY name DESC";
|
||
|
|
|
||
|
|
$q.=" LIMIT 0,50";
|
||
|
|
|
||
|
|
$res = $db->query($q);
|
||
|
|
$result = array();
|
||
|
|
|
||
|
|
$stock_id = $searchStockId;
|
||
|
|
|
||
|
|
while ($row = $db->fetchByAssoc($res)) {
|
||
|
|
$tmp = array();
|
||
|
|
$tmp['id'] = $row['id'];
|
||
|
|
$tmp['name'] = $row['name'];
|
||
|
|
$tmp['code'] = $row['code'];
|
||
|
|
|
||
|
|
//get stock if necessary
|
||
|
|
if ($searchStock!='1') {
|
||
|
|
$tmp['stock_state'] = EcmStockOperation::getStock($row['id'], $stock_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($searchStock=='3' && $tmp['stock_state']==0) continue; //don't show null stock
|
||
|
|
|
||
|
|
$result[] = $tmp;
|
||
|
|
}
|
||
|
|
echo json_encode($result);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getProduct($id, $stockId) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$p = $db->fetchByAssoc($db->query("SELECT p.is_consignment, p.id, p.code, p.name, p.unit_id, p.product_category_id FROM ecmproducts as p INNER JOIN ecmvats as v ON v.id=p.vat_id WHERE p.id='$id'"));
|
||
|
|
global $app_list_strings;
|
||
|
|
$p['unit_name'] = $app_list_strings['ecmproducts_unit_dom'][$p['unit_id']];
|
||
|
|
$p['stock_state'] = $tmp['stock_state'] = EcmStockOperation::getStock($id, $stockId);
|
||
|
|
$p['unit_precision'] = $app_list_strings['ecmproducts_unit_dom_precision'][$p['unit_id']];
|
||
|
|
|
||
|
|
echo json_encode($p);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getItems($record) {
|
||
|
|
$mm = new EcmInventory();
|
||
|
|
$mm->retrieve($record);
|
||
|
|
$pl = $mm->getPositionList(true);
|
||
|
|
unset($mm);
|
||
|
|
echo json_encode($pl);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getCategoriesList() {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$res = $db->query("SELECT id, name FROM ecmproductcategories WHERE deleted='0'");
|
||
|
|
$result = array();
|
||
|
|
while ($row = $db->fetchByAssoc($res)) {
|
||
|
|
$tmp = array();
|
||
|
|
$tmp['id'] = $row['id'];
|
||
|
|
$tmp['name'] = $row['name'];
|
||
|
|
$result[] = $tmp;
|
||
|
|
}
|
||
|
|
echo json_encode($result);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getTranslation($product_id, $language, $unit_id) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
$result = array();
|
||
|
|
if ($language=='en_us') {
|
||
|
|
$r = $db->fetchByAssoc($db->query("SELECT short_description FROM ecmproduct_language WHERE ecmproduct_id='$product_id' AND language='en'"));
|
||
|
|
$result['name'] = htmlspecialchars_decode($r['short_description']);
|
||
|
|
} else if ($language=='pl_pl') {
|
||
|
|
$p = new EcmProduct();
|
||
|
|
$p->retrieve($product_id);
|
||
|
|
$result['name'] = htmlspecialchars_decode($p->name);
|
||
|
|
unset($p);
|
||
|
|
}
|
||
|
|
$lists = return_app_list_strings_language($language);
|
||
|
|
$result['unit_name'] = $lists['ecmproducts_unit_dom'][$unit_id];
|
||
|
|
unset($lists);
|
||
|
|
|
||
|
|
echo json_encode($result);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getPricesInfo($product_id, $pricebook_id, $account_id) {
|
||
|
|
$db = $GLOBALS['db'];
|
||
|
|
|
||
|
|
$result = array();
|
||
|
|
if ($pricebook_id && $pricebook_id!='') {
|
||
|
|
//try get price from pricebook
|
||
|
|
$res = $db->fetchByAssoc($db->query("SELECT price FROM ecmpricebooks_ecmproducts WHERE ecmpricebook_id='$pricebook_id' AND ecmproduct_id='$product_id' AND deleted='0'"));
|
||
|
|
if ($res['price'] && $res['price']!='' && $res['price']!=0) {
|
||
|
|
$tmp = array();
|
||
|
|
$tmp['name'] = 'pricebook';
|
||
|
|
$tmp['price'] = $res['price'];
|
||
|
|
$result[] = $tmp;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//get from ecmprices
|
||
|
|
$res = $db->query("SELECT p.name, pp.price FROM ecmprices_ecmproducts AS pp
|
||
|
|
INNER JOIN ecmprices AS p
|
||
|
|
ON p.id=pp.ecmprice_id
|
||
|
|
WHERE
|
||
|
|
pp.ecmproduct_id='$product_id'
|
||
|
|
AND pp.price!=0");
|
||
|
|
while ($row = $db->fetchByAssoc($res)) {
|
||
|
|
$tmp = array();
|
||
|
|
$tmp['name'] = $row['name'];
|
||
|
|
$tmp['price'] = $row['price'];
|
||
|
|
$tmp['module']='EcmPrices';
|
||
|
|
$tmp['visible']='1';
|
||
|
|
$result[] = $tmp;
|
||
|
|
}
|
||
|
|
|
||
|
|
//get last invoice price
|
||
|
|
if ($account_id && $account_id!='') {
|
||
|
|
$res = $db->fetchByAssoc($db->query("SELECT ii.subprice, i.id, i.document_no FROM ecminvoiceoutitems AS ii
|
||
|
|
INNER JOIN ecminvoiceouts AS i
|
||
|
|
ON ii.ecminvoiceout_id=i.id
|
||
|
|
WHERE ii.ecmproduct_id='$product_id'
|
||
|
|
AND i.parent_id='$account_id'
|
||
|
|
AND ii.deleted='0'
|
||
|
|
AND i.deleted='0'
|
||
|
|
AND i.canceled='0'
|
||
|
|
ORDER BY i.register_date DESC
|
||
|
|
LIMIT 0,1"));
|
||
|
|
if ($res && $res['subprice']!='') {
|
||
|
|
$tmp = array();
|
||
|
|
$tmp['name'] = '<a href="index.php?module=EcmInvoiceOuts&action=DetailView&record='.$res['id'].'">'.$res['document_no'].'</a>';
|
||
|
|
$tmp['price'] = $res['subprice'];
|
||
|
|
$tmp['module']='EcmInvoiceOuts';
|
||
|
|
$tmp['visible']='1';
|
||
|
|
$result[] = $tmp;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//get from ecmstockdocins
|
||
|
|
$res = $db->query("SELECT ii.price, i.id, i.document_no,ii.quantity,i.register_date FROM ecmstockdocinitems AS ii
|
||
|
|
INNER JOIN ecmstockdocins AS i
|
||
|
|
ON ii.ecmstockdocin_id=i.id
|
||
|
|
WHERE ii.ecmproduct_id='$product_id'
|
||
|
|
AND ii.deleted='0'
|
||
|
|
AND i.deleted='0'
|
||
|
|
ORDER BY i.register_date DESC");
|
||
|
|
while ($row = $db->fetchByAssoc($res)) {
|
||
|
|
$tmp = array();
|
||
|
|
$tmp['name'] = '<a href="index.php?module=EcmStockDocIns&action=DetailView&record='.$row['id'].'">'.$row['document_no'].'</a>';
|
||
|
|
$tmp['price'] = $row['price'];
|
||
|
|
$tmp['module']='EcmStockDocIns';
|
||
|
|
$tmp['quantity']=$row['quantity'];
|
||
|
|
$tmp['register_date']=$row['register_date'];
|
||
|
|
$tmp['visible']='0';
|
||
|
|
$result[] = $tmp;
|
||
|
|
}
|
||
|
|
echo json_encode($result);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getStockArray($product_id) {
|
||
|
|
$o = new EcmStockOperation();
|
||
|
|
echo json_encode($o->getStockArray($product_id));
|
||
|
|
unset($o);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getPurchaseArray($product_id) {
|
||
|
|
$o = new EcmStockOperation();
|
||
|
|
echo json_encode($o->getPurchaseArray($product_id));
|
||
|
|
unset($o);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getStockState($id, $stock_id) {
|
||
|
|
echo EcmStockOperation::getStock($id, $stock_id);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getComponents($product_id) {
|
||
|
|
$p = new EcmProduct();
|
||
|
|
$p->retrieve($product_id);
|
||
|
|
echo json_encode($p->getPositionList(true));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|