2025-05-12 15:44:39 +00:00
< ? php
2025-05-29 17:43:29 +02:00
if ( ! defined ( 'sugarEntry' ) || ! sugarEntry )
die ( '-1' );
if ( ! $_POST [ 'job' ] || $_POST [ 'job' ] == '' )
die ( '-1' );
switch ( $_POST [ 'job' ]) {
case 'getParentInfo' :
getParentInfo ( $_POST [ 'id' ], $_POST [ 'type' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'generateNumber' :
generateNumber ( $_POST [ 'stock' ], $_POST [ 'date' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'searchProducts' :
searchProducts ( $_POST [ 'searchKey' ], $_POST [ 'searchCategory' ], $_POST [ 'searchStock' ], $_POST [ 'searchSort' ], $_POST [ 'searchStockId' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getProduct' :
getProduct ( $_POST [ 'id' ], $_POST [ 'stockId' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getItems' :
getItems ( $_POST [ 'record' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getCategoriesList' :
getCategoriesList ();
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getTranslation' :
getTranslation ( $_POST [ 'product_id' ], $_POST [ 'language' ], $_REQUEST [ 'unit_id' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getPricesInfo' :
getPricesInfo ( $_POST [ 'product_id' ], $_POST [ 'pricebook_id' ], $_POST [ 'account_id' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getStockArray' :
getStockArray ( $_POST [ 'product_id' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getPurchaseArray' :
getPurchaseArray ( $_POST [ 'product_id' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getStockState' :
getStockState ( $_POST [ 'id' ], $_POST [ 'stockId' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getComponents' :
getComponents ( $_POST [ 'product_id' ]);
2025-05-12 15:44:39 +00:00
break ;
2025-05-29 17:43:29 +02:00
case 'getConsignments' :
getConsignments ( $_POST [ 'prod_id' ], $_REQUEST [ 'stock_id' ]);
break ;
case 'getConsignmentsDocs' :
getConsignmentsDocs ( $_POST [ 'prod_id' ], $_REQUEST [ 'stock_id' ]);
break ;
case 'getLastEcmStockDocCorrectsDate' :
getLastEcmStockDocCorrectsDate ( $_POST [ 'stock' ]);
break ;
case 'getStockStates' :
getStockStates ( $_POST [ 'ids' ], $_POST [ 'stockId' ]);
2025-05-12 15:44:39 +00:00
break ;
}
2025-05-29 17:43:29 +02:00
function getLastEcmStockDocCorrectsDate ( $stock )
{
$db = $GLOBALS [ 'db' ];
$uq = $db -> query ( " SELECT MAX(register_date) AS date FROM ecmstockdoccorrects WHERE stock_id=' " . $stock . " ' and deleted=0 " );
$rul = $db -> fetchByAssoc ( $uq );
echo json_encode ( $rul [ 'date' ]);
return ;
2025-05-12 15:44:39 +00:00
}
2025-05-29 17:43:29 +02:00
function generateNumber ( $mag , $date )
{
$data = array ();
$data [ 'number' ] = EcmStockDocCorrect :: generateNumber ( $date );
$data [ 'document_no' ] = EcmStockDocCorrect :: generateDocNumberStock ( $mag , $date );
echo json_encode ( $data );
unset ( $data );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function searchProducts ( $searchKey , $searchCategory , $searchStock , $searchSort , $searchStockId )
{
$db = $GLOBALS [ 'db' ];
2025-05-12 15:44:39 +00:00
$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 ' " ;
2025-05-29 17:43:29 +02:00
2025-05-12 15:44:39 +00:00
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 " ;
2025-05-29 17:43:29 +02:00
2025-05-12 15:44:39 +00:00
$q .= " LIMIT 0,50 " ;
2025-05-29 17:43:29 +02:00
$res = $db -> query ( $q );
$result = array ();
2025-05-12 15:44:39 +00:00
$stock_id = $searchStockId ;
2025-05-29 17:43:29 +02:00
while ( $row = $db -> fetchByAssoc ( $res )) {
$tmp = array ();
$tmp [ 'id' ] = $row [ 'id' ];
$tmp [ 'name' ] = $row [ 'name' ];
$tmp [ 'code' ] = $row [ 'code' ];
2025-05-12 15:44:39 +00:00
// get stock if necessary
if ( $searchStock != '1' ) {
2025-05-29 17:43:29 +02:00
$tmp [ 'stock_state' ] = EcmStockOperation :: getStock ( $row [ 'id' ], $stock_id );
2025-05-12 15:44:39 +00:00
}
2025-05-29 17:43:29 +02:00
if ( $searchStock == '3' && $tmp [ 'stock_state' ] == 0 )
2025-05-12 15:44:39 +00:00
continue ; // don't show null stock
2025-05-29 17:43:29 +02:00
$result [] = $tmp ;
2025-05-12 15:44:39 +00:00
}
2025-05-29 17:43:29 +02:00
echo json_encode ( $result );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getProduct ( $id , $stockId )
{
$db = $GLOBALS [ 'db' ];
$p = $db -> fetchByAssoc ( $db -> query ( " SELECT p.id, p.code, p.name, p.unit_id,p.is_consignment, p.product_category_id,v.id as ecmvat_id, v.name as ecmvat_name, v.value as ecmvat_value FROM ecmproducts as p INNER JOIN ecmvats as v ON v.id=p.vat_id WHERE p.id=' $id ' " ));
2025-05-12 15:44:39 +00:00
global $app_list_strings ;
2025-05-29 17:43:29 +02:00
$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 );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getItems ( $record )
{
$mm = new EcmStockDocCorrect ();
$mm -> retrieve ( $record );
$pl = $mm -> getPositionList ( true );
unset ( $mm );
echo json_encode ( $pl );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
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 ;
2025-05-12 15:44:39 +00:00
}
2025-05-29 17:43:29 +02:00
echo json_encode ( $result );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getTranslation ( $product_id , $language , $unit_id )
{
$db = $GLOBALS [ 'db' ];
$result = array ();
2025-05-12 15:44:39 +00:00
if ( $language == 'en_us' ) {
2025-05-29 17:43:29 +02:00
$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' ]);
2025-05-12 15:44:39 +00:00
} else if ( $language == 'pl_pl' ) {
2025-05-29 17:43:29 +02:00
$p = new EcmProduct ();
$p -> retrieve ( $product_id );
$result [ 'name' ] = htmlspecialchars_decode ( $p -> name );
unset ( $p );
2025-05-12 15:44:39 +00:00
}
2025-05-29 17:43:29 +02:00
$lists = return_app_list_strings_language ( $language );
$result [ 'unit_name' ] = $lists [ 'ecmproducts_unit_dom' ][ $unit_id ];
unset ( $lists );
echo json_encode ( $result );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getPricesInfo ( $product_id , $pricebook_id , $account_id )
{
$db = $GLOBALS [ 'db' ];
$result = array ();
2025-05-12 15:44:39 +00:00
if ( $pricebook_id && $pricebook_id != '' ) {
// try get price from pricebook
2025-05-29 17:43:29 +02:00
$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 ;
2025-05-12 15:44:39 +00:00
}
}
2025-05-29 17:43:29 +02:00
2025-05-12 15:44:39 +00:00
// get from ecmprices
2025-05-29 17:43:29 +02:00
$res = $db -> query ( " SELECT p.name, pp.price FROM ecmprices_ecmproducts AS pp
2025-05-12 15:44:39 +00:00
INNER JOIN ecmprices AS p
ON p . id = pp . ecmprice_id
WHERE
pp . ecmproduct_id = '$product_id'
2025-05-29 17:43:29 +02:00
AND pp . price != 0 " );
while ( $row = $db -> fetchByAssoc ( $res )) {
$tmp = array ();
$tmp [ 'name' ] = $row [ 'name' ];
$tmp [ 'price' ] = $row [ 'price' ];
$result [] = $tmp ;
2025-05-12 15:44:39 +00:00
}
2025-05-29 17:43:29 +02:00
2025-05-12 15:44:39 +00:00
// get last invoice price
if ( $account_id && $account_id != '' ) {
2025-05-29 17:43:29 +02:00
$res = $db -> fetchByAssoc ( $db -> query ( " SELECT ii.subprice, i.id, i.document_no FROM ecminvoiceoutitems AS ii
2025-05-12 15:44:39 +00:00
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
2025-05-29 17:43:29 +02:00
LIMIT 0 , 1 " ));
if ( $res && $res [ 'subprice' ] != '' ) {
$tmp = array ();
$tmp [ 'name' ] = $res [ 'document_no' ];
$tmp [ 'price' ] = $res [ 'subprice' ];
$result [] = $tmp ;
2025-05-12 15:44:39 +00:00
}
}
2025-05-29 17:43:29 +02:00
echo json_encode ( $result );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getStockArray ( $product_id )
{
$o = new EcmStockOperation ();
echo json_encode ( $o -> getStockArray ( $product_id ));
unset ( $o );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getPurchaseArray ( $product_id )
{
$o = new EcmStockOperation ();
echo json_encode ( $o -> getPurchaseArray ( $product_id ));
unset ( $o );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getStockState ( $id , $stock_id )
{
echo EcmStockOperation :: getStock ( $id , $stock_id );
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getComponents ( $product_id )
{
$p = new EcmProduct ();
$p -> retrieve ( $product_id );
echo json_encode ( $p -> getPositionList ( true ));
2025-05-12 15:44:39 +00:00
return ;
}
2025-05-29 17:43:29 +02:00
function getConsignments ( $prod_id , $stock_id )
{
$documents_map = array (
'EcmStockDocIns' => 'PZ' ,
'EcmStockDocCorrects' => 'KS' ,
'EcmStockDocMoves' => 'MM' ,
'EcmStockDocInsideIns' => 'PW'
2025-05-12 15:44:39 +00:00
);
2025-05-29 17:43:29 +02:00
$db = $GLOBALS [ 'db' ];
2025-05-12 15:44:39 +00:00
$query = "
select i . id , i . parent_id , i . parent_type , i . parent_name , i . price , i . quantity as in_qty , ifnull ( sum ( o . quantity ), 0 ) as out_qty , p . unit_id , i . part_no
from ecmstockoperations as i
left join ecmstockoperations as o
on i . id = o . in_id
inner join ecmproducts as p
on i . product_id = p . id
where
i . product_id = '$prod_id' and
i . stock_id = '$stock_id' and
i . used = '0' and
i . type = '0'
group by i . id
order by i . date_entered ;
" ;
$res = $db -> query ( $query );
$result = array ();
global $app_list_strings , $sugar_config ;
while ( $row = $db -> fetchByAssoc ( $res )) {
$availability = ( floatval ( $row [ 'in_qty' ]) - floatval ( $row [ 'out_qty' ]));
2025-05-29 17:43:29 +02:00
if ( $availability <= 0 ) continue ;
2025-05-12 15:44:39 +00:00
$tmp = array ();
$tmp [ 'operation_id' ] = $row [ 'id' ];
2025-05-29 17:43:29 +02:00
$tmp [ 'document_no' ] = $documents_map [ $row [ 'parent_type' ]] . ' ' . $row [ 'parent_name' ];
2025-05-12 15:44:39 +00:00
$tmp [ 'availability' ] = $availability ;
$tmp [ 'parent_id' ] = $row [ 'parent_id' ];
$tmp [ 'parent_type' ] = $row [ 'parent_type' ];
$tmp [ 'price' ] = $row [ 'price' ];
$tmp [ 'part_no' ] = $row [ 'part_no' ];
$tmp [ 'unit' ] = $app_list_strings [ 'ecmproducts_unit_dom' ][ $row [ 'unit_id' ]];
2025-05-29 17:43:29 +02:00
$tmp [ 'precision' ] = $app_list_strings [ 'ecmproducts_unit_dom_precision' ][ $row [ 'unit_id' ]];
2025-05-12 15:44:39 +00:00
$tmp [ 'currency' ] = $sugar_config [ 'default_currency_iso4217' ];
2025-05-29 17:43:29 +02:00
2025-05-12 15:44:39 +00:00
$result [] = $tmp ;
}
echo json_encode ( $result );
return ;
}
2025-05-29 17:43:29 +02:00
function getConsignmentsDocs ( $prod_id , $stock_id )
{
$documents_map = array (
'EcmStockDocIns' => 'PZ' ,
'EcmStockDocCorrects' => 'KS' ,
'EcmStockDocMoves' => 'MM' ,
'EcmStockDocInsideIns' => 'PW'
);
$db = $GLOBALS [ 'db' ];
$query = "
2025-05-12 15:44:39 +00:00
select i . id , i . parent_id , i . parent_type , i . parent_name , i . price , i . quantity as in_qty , ifnull ( sum ( o . quantity ), 0 ) as out_qty , p . unit_id
from ecmstockoperations as i
left join ecmstockoperations as o
on i . id = o . in_id
inner join ecmproducts as p
on i . product_id = p . id
where
i . product_id = '$prod_id' and
i . stock_id = '$stock_id' and
i . used = '0' and
i . type = '0'
group by i . id
order by i . date_entered ;
" ;
2025-05-29 17:43:29 +02:00
$res = $db -> query ( $query );
$result = array ();
global $app_list_strings , $sugar_config ;
while ( $row = $db -> fetchByAssoc ( $res )) {
$availability = ( floatval ( $row [ 'in_qty' ]) - floatval ( $row [ 'out_qty' ]));
if ( $availability <= 0 )
continue ;
$tmp = array ();
$tmp [ 'operation_id' ] = $row [ 'id' ];
$tmp [ 'part_no' ] = $documents_map [ $row [ 'parent_type' ]] . ' ' . $row [ 'parent_name' ];
$tmp [ 'availability' ] = $availability ;
$tmp [ 'parent_id' ] = $row [ 'parent_id' ];
$tmp [ 'parent_type' ] = $row [ 'parent_type' ];
$tmp [ 'price' ] = $row [ 'price' ];
$tmp [ 'unit' ] = $app_list_strings [ 'ecmproducts_unit_dom' ][ $row [ 'unit_id' ]];
$tmp [ 'precision' ] = $app_list_strings [ 'ecmproducts_unit_dom_precision' ][ $row [ 'unit_id' ]];
$tmp [ 'currency' ] = $sugar_config [ 'default_currency_iso4217' ];
$result [] = $tmp ;
}
echo json_encode ( $result );
return ;
}
function getStockStates ( $ids , $stock_id )
{
$ids = explode ( " | " , $ids );
// foreach ids
$res = array ();
foreach ( $ids as $id ) {
$p = array ();
$p [ 'id' ] = $id ;
$p [ 'state' ] = EcmStockOperation :: getStock ( $id , $stock_id );
$res [] = $p ;
}
echo json_encode ( $res );
2025-05-12 15:44:39 +00:00
}