';
$db=$GLOBALS['db'];
function getConsignmentsDocs($prod_id, $stock_id, $jm) {
$documents_map = array (
'EcmStockDocIns' => 'PZ',
'EcmStockDocCorrects' => 'KS',
'EcmStockDocMoves' => 'MM',
'EcmStockDocInsideIns' => 'PW'
);
global $db;
$query = "
select i.id, i.parent_id, i.parent_type, i.parent_name, i.price, i.quantity as in_qty, i.part_no, 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;";
$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 ();
if($row ['parent_type'] == 'EcmStockDocInsideIns'){
$accountQuery = "SELECT name FROM ecmstockdocinsideins WHERE id='" . $row['parent_id'] . "'";
$accountResult = $db->query($accountQuery);
if($accountResult->num_rows > 0){
$accountRow = $db->fetchByAssoc ( $accountResult );
$tmp ['account_name'] = $accountRow ['name'];
}else{
$tmp ['account_name'] = '';
}
}else{
$accountQuery = "SELECT parent_id, parent_name FROM " . strtolower($row ['parent_type']) ." WHERE id='" . $row['parent_id'] . "'";
$accountResult = $db->query($accountQuery);
if($accountResult->num_rows > 0){
$accountRow = $db->fetchByAssoc ( $accountResult );
$tmp ['account_name'] = '' . $accountRow['parent_name'] . '';
}else{
$tmp ['account_name'] = '';
}
}
$tmp ['operation_id'] = $row ['id'];
$tmp ['parent_name'] = '' . $documents_map [$row ['parent_type']] . ' ' . $row ['parent_name'] . '';
$tmp ['part_no'] = $row['part_no'];
$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 ['currency'] = $sugar_config ['default_currency_iso4217'];
$result [] = $tmp;
}
// number_format($tmp, 2, ",", ".")
$pr = new EcmProduct();
$pr->retrieve($product_id);
$html = '
Struktura zapasu
'.$pr->name.'
| Dokument |
Dostawca/Nazwa dokumentu |
Partia |
Ilość |
Cena |
Wartość |
';
foreach($result as $key => $value){
$tmp = $value['availability']*$value['price'];
$html.='';
$html.='| ' . $value['parent_name'] .' | ';
$html.='' . $value['account_name'] .' | ';
$html.='' . $value['part_no'] .' | ';
$html.='' . number_format($value['availability'], $app_list_strings['ecmproducts_unit_dom_precision'][$jm], ",", ".") .' ' . $value['unit'].' | '; //FormatNumber(c.availability) + ' ' + c.unit
$html.='' . number_format($value['price'], 2, ",", ".") . ' ' . $value['currency'] . ' / ' . $value['unit'] . ' | '; //FormatNumber(c.price) + ' ' + c.currency + ' / ' + c.unit
$html.=''. number_format($tmp, 2, ",", ".") .' zł | ';
$html.='
';
}
$html .= '
';
return $html;
}
function getStockOperations($product_id, $stock_id, $jm){
global $db,$app_list_strings;
$query = "SELECT date_entered, quantity, type, parent_name, parent_type, parent_id, price FROM ecmstockoperations so where so.product_id='" . $product_id . "' AND so.stock_id ='" . $stock_id . "'order by counter";
$res = $db->query ( $query );
$result = array ();
$pr = new EcmProduct();
$pr->retrieve($product_id);
$html = '
Operacje magazynowe
'.$pr->name.'
| Dokument |
Kontrahent |
Data |
Cena |
Ilość |
Stan po |
';
$stanpo =0;
$doc_name=array(
'EcmStockDocInsideIns'=>'PW',
'EcmStockDocMoves'=>'MM',
'EcmStockDocCorrects'=>'KS',
'EcmStockDocIns'=>'PZ',
'EcmStockDocOuts'=>'WZ',
'EcmStockDocInsideOuts'=>'RW',
'EcmInvoiceOuts'=>'FK'
);
while ( $row = $db->fetchByAssoc ( $res ) ) {
$subquery = '';
$subquery = 'SELECT parent_id, parent_name, parent_type FROM ' . strtolower($row['parent_type']) . ' WHERE id = "'.$row['parent_id'].'"';
$result = $db->query ( $subquery );
if(!$result){
$subquery = 'SELECT parent_id, parent_name FROM ' . strtolower($row['parent_type']) . ' WHERE id = "'.$row['parent_id'].'"';
$result = $db->query ( $subquery );
}
$kontrahent = $db->fetchByAssoc ( $result );
if($kontrahent['parent_type']==NULL || $kontrahent['parent_type']==''){
switch($row['parent_type']){
case 'EcmStockDocIns' :{
$kontrahent['parent_type'] = 'Accounts';
$tmp = '' . $kontrahent['parent_name'] . '';
break;
}
default :{
$tmp = $kontrahent['parent_name'];
}
}
}else{
$tmp = '' . $kontrahent['parent_name'] . '';
}
$nazwa = '';
if($row['type']==0){
$nazwa = '' . $doc_name[$row['parent_type']] . ' ' . $row['parent_name'] . '';
$stanpo +=$row['quantity'];
}elseif($row['type']==1){
$nazwa = '' . $doc_name[$row['parent_type']] . ' ' . $row['parent_name'] . '';
$stanpo -=$row['quantity'];
}
$data = substr($row['date_entered'],0,10);
$data = explode('-', $data);
$data = $data[2] . '.' .$data[1] . '.' .$data[0];
$html.='';
$html.='| ' . $nazwa .' | ';
$html.='' . $tmp .' | ';
$html.='' . $data . ' | '; //FormatNumber(c.availability) + ' ' + c.unit
$html.='' . number_format($row['price'], 2, ",", ".") . ' | ';
$html.=''. number_format($row['quantity'], $app_list_strings['ecmproducts_unit_dom_precision'][$jm], ",", ".") .' | ';
$html.=''. number_format($stanpo, $app_list_strings['ecmproducts_unit_dom_precision'][$jm], ",", ".") .' | ';
$html.='
';
}
$html .= '
';
return $html;
}
switch ($_REQUEST['type']) {
case "0":
echo getConsignmentsDocs($_REQUEST['product_id'],$_REQUEST['stock_id']);
break;
case "1":
echo getStockOperations($_REQUEST['product_id'],$_REQUEST['stock_id']);
break;
}
?>