Files
2025-05-12 15:44:39 +00:00

387 lines
13 KiB
PHP
Executable File

<?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 'getContactInfo': getContactInfo($_POST['id']); break;
case 'generateNumber': generateNumber($_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 'getProductsFromComponents' : getProductsFromComponents($_POST['document_id'],$_POST['position_id']);break;
case 'getUnits':
getUnits();
break;
}
function getUnits(){
global $app_list_strings;
echo json_encode($app_list_strings['ecmproducts_unit_dom']);
return;
}
function getProductsFromComponents($docId,$positionId){
$db=$GLOBALS['db'];
global $app_list_strings;
$ids =$docId;
$components_to_show=$positionId;
$query='select i.ecmproduct_id,i.name,i.code,i.quantity,d.document_no from ecmsaleitems i
inner join ecmsales d on d.id=i.ecmsale_id
where i.ecmsale_id in ("'.implode('","',$ids) .'") and i.deleted=0';
$res = $db->query($query);
$orders=[];
$products=[];
$orderNo='';
$orderNr=[];
while($dane = $db->fetchByAssoc($res)){
$tmp = [];
$tmp['ecmproduct_id']=$dane['ecmproduct_id'];
$tmp['quantity']=(float)$dane['quantity'];
$tmp['name']=$dane['name'];
$tmp['code']=$dane['code'];
$orders[$dane['ecmsale_id']][]=$tmp;
$products[]=$dane['ecmproduct_id'];
if($orderNr[$dane['document_no']]==""){
$orderNr[$dane['document_no']]=1;
$orderNo.=' '.$dane['document_no'];
}
}
$products=array_unique($products);
$query="SELECT c.ecmproduct_id,c.ecmcomponent_id,c.code,c.name,c.quantity,p.product_category_name,p.group_ks,p.unit_id,p.srp_price,p.vat_id,p.vat_name,p.vat_value FROM ecmproductcomponents c
inner join ecmproducts p on p.id=c.ecmcomponent_id
where c.ecmproduct_id in ('".implode("','",$products)."') and c.deleted=0";
$res = $db->query($query);
$lists = return_app_list_strings_language('pl_pl');
$components=[];
$products_has_components=[];
while($dane = $db->fetchByAssoc($res)){
$products_has_components[$dane['ecmproduct_id']][]=$dane;
}
$total_components=0;
$total_in_stock=0;
$components_list=[];
foreach ($orders as $order){
foreach ($order as $product){
if(isset($products_has_components[$product['ecmproduct_id']])){
foreach ($products_has_components[$product['ecmproduct_id']] as $component){
if(count($components_to_show)>0){
if(!in_array($component['ecmcomponent_id'],$components_to_show))continue;
}
if($component['group_ks']=='4')continue;
if($component['product_category_name']=='Usługi')continue;
if($components_list[$component['ecmcomponent_id']]){
$components_list[$component['ecmcomponent_id']]['quantity']=bcadd(bcmul($component['quantity'],$product['quantity'],6),$components_list[$component['ecmcomponent_id']]['quantity'],6);
} else {
$components_list[$component['ecmcomponent_id']]['quantity']=bcmul ($component['quantity'],$product['quantity'],6);
$components_list[$component['ecmcomponent_id']]['product_code']=$component['code'];
$components_list[$component['ecmcomponent_id']]['name']=$component['name'];
$components_list[$component['ecmcomponent_id']]['product_id']=$component['ecmcomponent_id'];
$components_list[$component['ecmcomponent_id']]['product_category_name']=$component['product_category_name'];
$components_list[$component['ecmcomponent_id']]['group_ks_name']=$app_list_strings['ecmproducts_group_ks_dom'][$component['group_ks']];
$components_list[$component['ecmcomponent_id']]['unit_id'] = $component['unit_id'];
$components_list[$component['ecmcomponent_id']]['unit_precision'] = $app_list_strings['ecmproducts_unit_dom_precision'][$component['unit_id']];
$components_list[$component['ecmcomponent_id']]['product_precision'] = $app_list_strings['ecmproducts_unit_dom_precision'][$component['unit_id']];
$components_list[$component['ecmcomponent_id']]['unit_name'] = $lists['ecmproducts_unit_dom'][$component['unit_id']];
$components_list[$component['ecmcomponent_id']]['ecmvat_id'] = $component['vat_id'];
$components_list[$component['ecmcomponent_id']]['ecmvat_name'] = $component['vat_name'];
$components_list[$component['ecmcomponent_id']]['ecmvat_value'] = $component['vat_value'];
$components_list[$component['ecmcomponent_id']]['price_start'] = $component['srp_price'];
$ecmStockOperations = new EcmStockOperation();
$stock=round($ecmStockOperations->getRealStock($component['ecmcomponent_id']),2);
$components_list[$component['ecmcomponent_id']]['stock']=$stock;
}
}
}
}
}
$array=[];
$position=0;
foreach ($components_list as $key=>$component){
$component['position']=$position;
$array[]=$component;
$position++;
}
echo json_encode($array);
return;
}
function getContactInfo($id){
if (!$id || $id == '') die('-1');
$c = new Contact();
$c->retrieve($id);
$data = array();
$data['parent_contact_name']=$c->first_name.' '.$c->last_name;
$data['parent_contact_title']=$c->title;
echo json_encode($data);
unset($data);
unset($c);
return;
}
function getParentInfo($id, $type) {
if (!$id || $id == '') die('-1');
if (!$type || $type == '') die('-1');
if ($type == 'Accounts') {
$a = new Account();
$a->retrieve($id);
$data = array();
$data['name'] = html_entity_decode($a->name);
$data['parent_nip'] = $a->to_vatid;
$data['parent_address_street'] = $a->register_address_street;
$data['parent_address_postalcode'] = $a->register_address_postalcode;
$data['parent_address_city'] = $a->register_address_city;
$data['parent_address_country'] = $a->register_address_country;
$data['invoice_type'] = $a->invoice_type;
$data['currency_id'] = $a->currency_id;
if ($a->ecmpaymentcondition_id && $a->ecmpaymentcondition_id!='') {
$pc = new EcmPaymentCondition();
$pc->retrieve($a->ecmpaymentcondition_id);
$data['ecmpaymentcondition_id'] = $pc->id;
$data['ecmpaymentcondition_name'] = $pc->name;
}
if ($a->ecmdeliverycondition_id && $a->ecmdeliverycondition_id!='') {
$pc = new EcmdeliveryCondition();
$pc->retrieve($a->ecmdeliverycondition_id);
$data['ecmdeliverycondition_id'] = $pc->id;
$data['ecmdeliverycondition_name'] = $pc->name;
}
if ($a->ecmprice_id && $a->ecmprice_id!='') {
$pr = new EcmPrice();
$pr->retrieve($a->ecmprice_id);
$data['ecmprice_id'] = $pr->id;
$data['ecmprice_name'] = $pr->name;
}
//get pricebooks, ownership pricebooks
$pricebooks = array();
$db=$GLOBALS['db'];
$res = $db->query("SELECT id, name FROM ecmpricebooks WHERE account_id IN ('".$a->id."','".$a->parent_id."') AND active='1' AND deleted='0'");
while ($row = $db->fetchByAssoc($res)) {
$tmp = array();
$tmp['id'] = $row['id'];
$tmp['name'] = $row['name'];
$pricebooks[] = $tmp;
unset($tmp);
}
$data['pricebooks'] = $pricebooks;
echo json_encode($data);
unset($data);
unset($a);
unset($res);
return;
}
}
function generateNumber($date) {
$data = array();
$data['number'] = EcmPurchaseOrder::generateNumber($date);
$data['document_no'] = EcmPurchaseOrder::formatNumber($data['number']);
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.id, p.code, p.name, v.id as ecmvat_id, v.name as ecmvat_name, v.value as ecmvat_value, p.ean, p.ean2, p.unit_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 EcmPurchaseOrder();
$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'];
$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'] = $res['document_no'];
$tmp['price'] = $res['subprice'];
$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;
}