Files
crm.twinpol.com/modules/EcmReports/ProductOperationHistory.php
2025-05-12 15:44:39 +00:00

304 lines
10 KiB
PHP
Executable File

<?php
function getStartValues($product_id,$stock,$date_from){
$db = $GLOBALS['db'];
$r1 = $db->query("select id,name,code,unit_id from ecmproducts where id='".$product_id."' ");
$docs = array();
$i=0;
$total_quantity_old=0;
$total_value_old=0;
while ($tmp2 = $db->fetchByAssoc($r1)) {
$r = $db->query(
"select * from ecmstockoperations where
product_id='" . $tmp2['id'] . "'
and stock_id = '" . $stock . "'
and in_id is null and type=0 and date_entered <='".$date_from."'");
$l = 0;
$i=$tmp2['id'];
if ($r->num_rows > 0) {
$docs[$i]['unit_id']=$app_list_strings['ecmproducts_unit_dom'][$tmp2['unit_id']];
$docs[$i]['unit_precision']=$app_list_strings['ecmproducts_unit_dom_precision'][$tmp2['unit_id']];
$docs[$i]['product_id']=$tmp2['id'];
$docs[$i]['product_name']=$tmp2['name'];
$docs[$i]['product_code']=$tmp2['code'];
$docs[$i]['total_quantity_old']=0;
$docs[$i]['total_price_old']=0;
}
while ($tmp = $db->fetchByAssoc($r)) {
$ii = $db->query(
"select * from ecmstockoperations where in_id='" . $tmp['id'] .
"' and type=1 and date_entered <='".$date_from."'");
if ($ii->num_rows > 0) {
// licz ilość dla użytych
$quantity_tmp = $tmp['quantity'];
while ($tmp2 = $db->fetchByAssoc($ii)) {
$quantity_tmp -= $tmp2['quantity'];
}
if($quantity_tmp==0){
$db->query("update ecmstockoperations set used=1 where id='".$tmp['id']."'");
continue;
}
$doc['quantity'] = $quantity_tmp;
$doc['parent_name'] = $tmp['parent_name'];
$doc['part_no'] = $tmp['part_no'];
$doc['price'] = $tmp['price'];
$doc['parent_type'] = $doc_name[$tmp['parent_type']];
$doc['parent_type2'] = $tmp['parent_type'];
$doc['parent_id'] = $tmp['parent_id'];
$doc['product_id'] = $tmp2['id'];
// $doc['date_entered'] = $n->register_date;
$doc['position_value'] = $doc['quantity']*$tmp['price'];
$total_quantity_old+=$doc['quantity'];
$total_value_old+=round($doc['quantity']*$tmp['price'],2);
//$docs[$i-1]['add'][] = $doc;
$docs[$i]['total_price_old']+=($doc['quantity']*$tmp['price'] );
$docs[$i]['total_quantity_old']+=($doc['quantity']);
} else {
// dla całych
$doc['quantity'] = $tmp['quantity'];
$doc['parent_name'] = $tmp['parent_name'];
$doc['price'] = $tmp['price'];
$doc['part_no'] = $tmp['part_no'];
$doc['parent_type'] = $doc_name[$tmp['parent_type']];
$doc['parent_id'] = $tmp['parent_id'];
// $doc['date_entered'] = $n->register_date;
$doc['product_id'] = $tmp2['id'];
$doc['position_value'] = $doc['quantity']*$tmp['price'];
$total_quantity_old+=$doc['quantity'];
$total_value_old+=round($doc['quantity']*$tmp['price'],2);
// $docs[$i-1]['add'][] = $doc;
$docs[$i]['total_price_old']+=($doc['quantity']*$tmp['price'] );
$docs[$i]['total_quantity_old']+=($doc['quantity']);
}
if ($r->num_rows > 1) {
$docs[$i]['price']=$docs[$i]['total_price_old']/$docs[$i]['total_quantity_old'];
$docs[$i]['parent_name']='';
$docs[$i]['parent_type']='';
} if ($r->num_rows < 2 ){
$docs[$i]['price']=$tmp['price'];
$doc[$i]['part_no'] = $tmp['part_no'];
$docs[$i]['parent_id'] = $tmp['parent_id'];
$docs[$i]['parent_name']=$tmp['parent_name'];
// $docs[$i-1]['date_entered'] = $n->register_date;
$docs[$i]['parent_type'] = $doc_name[$tmp['parent_type']];
}else {
$docs[$i]['price']=$docs[$i]['total_price_old']/$docs[$i]['total_quantity_old'];
$docs[$i]['parent_name']='';
$docs[$i]['parent_type']='';
}
}
//if( $docs[$i]['total_quantity_old']==0 && $_GET['selectShowEmpty']!='')unset($docs[$i]);
}
return array($total_quantity_old,$total_value_old);
}
function getStateAfterOperation($id,$product_id,$stock_id){
$nr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select counter from ecmstockoperations where id='".$id."'"));
$res=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("
select
sum(CASE
WHEN (type=0)
then @t :=+quantity
else @t :=-quantity
end) as tt
,sum(CASE WHEN (type=0) then @t :=+quantity*price else @t :=-quantity*price end) as tt2
from ecmstockoperations where product_id='".$product_id."' and stock_id='".$stock_id."' and counter <= '".$nr['counter']."'"));
return $res;
}
function getOperations($product_id,$stock,$date_from,$date_to,$quantity,$val){
$db = $GLOBALS['db'];
$result=array();
$doc_name=array('EcmStockDocInsideIns'=>'PW','EcmStockDocMoves'=>'MM','EcmStockDocCorrects'=>'KS','EcmStockDocIns'=>'PZ','EcmStockDocInsideOuts'=>'RW','EcmStockDocOuts'=>'WZ','EcmInvoiceOuts'=>'FK');
$sum['in_quantity']=0;
$sum['in_value']=0;
$sum['out_quantity']=0;
$sum['out_value']=0;
$sum['after_quantity']=$quantity;
$sum['after_value']=$val;
$p= new EcmProduct();
$p->retrieve($product_id);
$product_name=$p->name;
$product_code=$p->code;
$r1 = $db->query("select id,date_entered,quantity,type,price,parent_type,parent_id from ecmstockoperations where product_id='".$product_id."' and stock_id='".$stock."' and date_entered >='".$date_from."' and date_entered <='".$date_to."' order by date_entered asc");
$lp=1;
while($dane=$db->fetchByAssoc($r1)){
$dane['lp']=$lp;
$dane['product_name']=$product_name;
$dane['product_code']=$product_code;
$dane['value']=$dane['quantity']*$dane['price'];
$module=substr($dane['parent_type'], 0, -1);
$doc = new $module();
$doc->retrieve($dane['parent_id']);
if($doc->type=='correct'){
$dane['doc_code']='KF';
} else {
$dane['doc_code']=$doc_name[$dane['parent_type']];
}
$dane['doc_name']=$doc->document_no;
$dane['doc_parent_name']=$doc->parent_name;
$after=getStateAfterOperation($dane['id'],$product_id,$stock);;
$dane['after_quantity']=$after['tt'];
$dane['after_value']=$after['tt2'];
$dane['correct']=$doc->type;
if($dane['type']==0){
if($dane['correct']=='correct'){
$sum['out_quantity']-=$dane['quantity'];
$sum['out_value']-=$dane['value'];
$sum['after_quantity']+=$dane['quantity'];
$sum['after_value']+=$dane['value'];
} else {
$sum['in_quantity']+=$dane['quantity'];
$sum['in_value']+=$dane['value'];
$sum['after_quantity']+=$dane['quantity'];
$sum['after_value']+=$dane['value'];
}
} else {
$sum['out_quantity']+=$dane['quantity'];
$sum['out_value']+=$dane['value'];
$sum['after_quantity']-=$dane['quantity'];
$sum['after_value']-=$dane['value'];
}
if($doc->type=='correct'){
$dane['quantity']=$dane['quantity']*-1;
$dane['value']=$dane['quantity']*$dane['price'];
}
$result[]=$dane;
$lp++;
}
return array($result,$sum);
}
$db = $GLOBALS['db'];
global $app_list_strings;
$datastocks = array ();
$users = array ();
$queryStocks = "SELECT
name,
id
FROM ecmstocks
where deleted= 0";
$rowsStocks = $db->query ( $queryStocks );
$s=array();
while ( $rowStocks = $db->fetchByAssoc ( $rowsStocks ) ) {
$stocks ["name"] = $rowStocks ["name"];
$stocks ["id"] = $rowStocks ["id"];
$datastocks [] = $stocks;
$s[$stocks ["id"]]['name']=$stocks ["name"];
}
if($_GET['date_from']==''){
$_GET['date_from']=date('01.m.Y');
} else {
$date_from=date('Y-m-d 00:00:01',strtotime($_GET['date_from']));
}
if($_GET['date_to']==''){
$_GET['date_to']=date('t.m.Y');
} else {
$date_to=date('Y-m-d 23:59:59',strtotime($_GET['date_to']));
}
$show_result=false;
if(isset($_REQUEST['submit']) && $_REQUEST['submit']!=''){
$show_result=true;
$p = new EcmProduct();
$p->retrieve($_GET['product_id']);
$product_indeks=$p->code;
$lists = return_app_list_strings_language('pl_pl');
$product_unit= $lists['ecmproducts_unit_dom'][$p->unit_id];
$startData=getStartValues($_GET ["product_id"],$_GET ["selectStock"],$date_from);
$start_quantity=$startData[0];
$start_value=$startData[1];
$data=getOperations($_GET ["product_id"],$_GET ["selectStock"],$date_from,$date_to,$start_quantity,$start_value);
$records=$data[0];
$sumTable=$data[1];
}
// create & execute smarty
$smarty = new Sugar_Smarty ();
global $mod_strings;
$smarty->assign ( "MOD", $mod_strings );
$smarty->assign ( "DATA", $records );
$smarty->assign ( "sumTable", $sumTable );
$smarty->assign ( "SHOW_RESULT", $show_result );
$smarty->assign ( "date_from", $_GET['date_from'] );
$smarty->assign ( "date_to", $_GET['date_to'] );
$smarty->assign ( "TOTAL_VALUE_OLD", $total_value_old );
$smarty->assign ( "TOTAL_QUANTITY_OLD", $total_quantity_old );
$smarty->assign ( "TOTAL_VALUE_NOW", $total_value_now );
$smarty->assign ( "TOTAL_QUANTITY_NOW", $total_quantity_now );
$smarty->assign ( "rozchod_w", $rozchod_w );
$smarty->assign ( "rozchod_q", $rozchod_q );
$smarty->assign ( "przychod_w", $przychod_w );
$smarty->assign ( "przychod_q", $przychod_q );
$smarty->assign ("CONSIGNMENTS",$EcmConfig['consignments']);
$smarty->assign ( "STOCKS", $datastocks );
$smarty->assign ( "selectStock", $_GET ["selectStock"]);
$smarty->assign ( "selectProductActive", $_GET ["selectProductActive"] );
$smarty->assign ( "selectProductEol", $_GET ["selectProductEol"] );
$smarty->assign ( "selectShowEmpty", $_GET ["selectShowEmpty"] );
$smarty->assign ( "product_id", $_GET ["product_id"] );
$smarty->assign ( "product_name", $_GET ["product_name"] );
$smarty->assign ( "product_indeks", $product_indeks);
$smarty->assign ( "product_unit", $product_unit );
$smarty->assign ( "start_quantity", $start_quantity );
$smarty->assign ( "start_value", $start_value );
// Eksport do PDF
if( $_GET['toPDF'] == '1' ) {
$output = $smarty->fetch( 'modules/EcmReports/tpls/PDF/ProductOptionHistory.tpl' );
include_once ("include/MPDF57/mpdf.php");
unset($smarty);
$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 25, 10, 5, 5 );
$p->setFooter('Strona {PAGENO} z {nbpg}');
$comp = new EcmSysInfo();
$p->SetHTMLHeader('<p style="text-align:left;font-size: 10px;">'.$comp->name.'<br>
Raport operacji magazynowych<br>Data wydruku: '.date("d.m.Y").'<br>Stan magazynowy za okres: '.$_GET['date_from'].' - '.$_GET['date_to'].'<br>Magazyn: '.$s[$_GET ["selectStock"]]['name']. '<br>Strona {PAGENO} z {nbpg}</p>');
//$p->setTitle($mod_strings["LBL_REPORT_STOCKS_DOCS"]);
//echo $output;
$p->writeHTML( $output );
$p->Output ();
} else {
$smarty->display ( 'modules/EcmReports/tpls/ProductOperationHistory.tpl' );
}
?>