Files
crm.e5.pl/modules/EcmReports/salesByProduct.php
2024-04-27 09:23:34 +02:00

337 lines
14 KiB
PHP

<style type="text/css">
.oddListRowS1 {
font-size: 12px;
padding-left: 30px;
padding-right: 8px;
padding-bottom: 5px;
padding-top: 3px;
vertical-align: middle;
}
</style>
<?php
include_once("modules/EcmReports/class.sales.php");
$s=new Sales();
if(!$_GET['year'])$_GET['year']=date("Y");
$s->date_from=$_GET['year']."-01-01";
$s->date_to=$_GET['year']."-12-31";
$s->account_id=$_GET['account'];
$s->product_active=$_GET['active'];
$s->product_status=$_GET['status'];
$s->category_id=$_REQUEST['category_id'];
$sdata=$s->getSalesByProduct();
//echo '<pre>';print_r($sdata);echo '</pre>';
include("modules/EcmReports/formProduct.php");
$year=$_GET['year'];
if($year==date("Y")){
$mmm=(int)date("m")-1;
}
else $mmm=12;
if(!$mmm)$mmm=1;
$trs='<tr>';
$tre='</tr>';
$trs_cat='<tr style="background-color: #e6e6e6;">';
$tds='<td class="oddListRowS1">';
$tdsf1='<td class="oddListRowS1" style="text-align:center;border-left: 1px solid #cccccc; border-bottom: 1px solid #cccccc;">';
$tdsf2='<td class="oddListRowS1" style="text-align:center; border-bottom: 1px solid #cccccc;">';
$tdsf3='<td class="oddListRowS1" style="text-align:center;border-right: 1px solid #cccccc; border-bottom: 1px solid #cccccc;">';
$tdsfs1='<td class="oddListRowS1" style="background-color: #BBDDFF;text-align:center;border-bottom: 1px solid #cccccc; max-width:50px;">';
$tdsfs2='<td class="oddListRowS1" style="background-color: #BBDDFF;text-align:center;border-bottom: 1px solid #cccccc; max-width:50px;">';
$tdsfs3='<td class="oddListRowS1" style="background-color: #BBDDFF;text-align:center;border-right: 1px solid #cccccc;border-bottom: 1px solid #cccccc;">';
$tds_cat='<td class="oddListRowS1" style="border-bottom: 1px solid #cccccc; text-align: center;">';
$tds_cat2='<td class="oddListRowS1" style="border-bottom: 1px solid #cccccc; text-align: center;">';
$tds_cat1='<td class="oddListRowS1" style="width:250px; border-bottom: 1px solid #cccccc;">';
$tbs='<table cellpadding="0" cellspacing="0" border="0" class="ListView" style="max-width:670px;">';
$tbs1='<table cellpadding="0" cellspacing="0" border="0" >';
$tbs2='<table cellpadding="0" cellspacing="0" border="0" class="ListView">';
$tds11='<td class="listViewThS1" style="width:200px;height:52px;">tt';
$tds12='<td class="listViewThS1" style="width:50px;height:48px;">';
$tds13='<td class="listViewThS1" style="width:50px;height:48px;">';
$tds13='<td class="listViewThS1" style="width:60px;height:48px;">';
$tds2='<td colspan="2" class="listViewThS1" style="text-align:center;border-left: 1px solid #cccccc;border-right: 1px solid #cccccc;min-width:100px;">';
$tds22='<td class="listViewThS1" style="text-align:center;border-left: 1px solid #cccccc;border-right: 1px solid #cccccc;min-width:100px; height:28px">';
$tds3='<td colspan="3" class="listViewThS1" style="text-align:center;border-left: 1px solid #cccccc;border-right: 1px solid #cccccc;min-width:100px;">';
$tds4='<td colspan="5" class="listViewThS1" style="text-align:center;border-left: 1px solid #cccccc;border-right: 1px solid #cccccc;min-width:100px;">';
$tdss='<td colspan="8" class="listViewThS1" style="text-align:center;border-left: 1px solid #cccccc;border-right: 1px solid #cccccc;min-width:100px;max-width:500px;">';
$tbe='</table>';
$tde='</td>';
$tdr='</tr>';
$months=array("01","02","03","04","05","06","07","08","09","10","11","12");
$months_names=array("01"=>"January","02"=>"February","03"=>"March","04"=>"April","05"=>"May","06"=>"June","07"=>"July","08"=>"August","09"=>"September","10"=>"October","11"=>"November","12"=>"December");
echo '<script language="javascript" src="modules/EcmProducts/helper.js"></script>';
echo '<script language="javascript" src="modules/EcmProducts/mintajax.js"></script>';
$t.=$tbs1;
$t.=$trs;
$trs_grey = '<tr style="background-color: #e6e6e6;">';
$t.='<td valign="top" style="border 1px dashed red;">';
$t.=$tbs2;
$t.=$trs;
$t.=$tds11."Name".$tde1;
$t.=$tds12."Inv qty".$tde1;
$t.=$tds13."Inv value".$tde1;
$t.=$tds13."Ord approved qty".$tde1;
$t.=$tds13."Ord approved value".$tde1;
//$t.=$tds13."Order value".$tde1;
//$t.=$tds13."Order qty".$tde1;
$t.=$tds13."Ord cretaed qty".$tde1;
$t.=$tds13."Ord cretaed value".$tde1;
//$t.=$tds13."Ord approved qty".$tde1;
//$t.=$tds13."Ord approved value".$tde1;
$t.=$tds13."Stock m".$tde1;
//$t.=$tds13."CBM".$tde1;
$t.=$tre;
$active_str="";
if($_REQUEST['active']){
if($_REQUEST['active']=="active")$active_str="and p.product_active='1'";
if($_REQUEST['active']=="inactive")$active_str="and p.product_active='0'";
}
$status_str="";
if ($_REQUEST['status']) $status_str="and status='".$_REQUEST['status']."'";
$z="select p.code,p.name,p.id, sum(ss.quantity) as inv_qty, sum(ss.quantity)*p.ems_price as inv_value from ecmproducts as p left join ecmstockstates as ss on ss.product_id=p.id where p.deleted='0' and p.product_category_id='".$_REQUEST['category_id']."'".$active_str.$status_str." group by p.id order by code";
//echo $z.'<br>';;
$w=$GLOBALS['db']->query($z);echo mysql_error();
$last=$s->getSumLast($_REQUEST['account'],$_REQUEST['active']);
$i=0;
global $show;
while($r=$GLOBALS['db']->fetchByAssoc($w)){
$order_value=0;
$order_qty=0;
$order_value_created=0;
$order_qty_created=0;
$order_value_approved=0;
$order_qty_approved=0;
$order_value_delivered=0;
$order_qty_delivered=0;
$cbm=0;
$ww=$GLOBALS['db']->query("select sum(i.product_quantity) as sq,sum(i.product_quantity*i.product_price) as sv from ecmproducts as p inner join ecmproducts_ecmpurchaseorders as i on i.ecmproduct_id=p.id inner join ecmpurchaseorders as e on e.id=i.ecmpurchaseorder_id where p.id='".$r['id']."' and i.deleted='0' and e.status='not_accepted' and e.deleted='0'");
while($rr=$GLOBALS['db']->fetchByAssoc($ww)){
$order_value_created+=$rr['sv'];
$order_qty_created+=$rr['sq'];
$order_value+=$rr['sv'];
$order_qty+=$rr['sq'];
}
$ww=$GLOBALS['db']->query("select sum(i.product_quantity) as sq,sum(i.product_quantity*i.product_price) as sv from ecmproducts as p inner join ecmproducts_ecmpurchaseorders as i on i.ecmproduct_id=p.id inner join ecmpurchaseorders as e on e.id=i.ecmpurchaseorder_id where p.id='".$r['id']."' and i.deleted='0' and (e.status='accepted' or e.status='mailed') and e.deleted='0'");
while($rr=$GLOBALS['db']->fetchByAssoc($ww)){
$order_value_approved+=$rr['sv'];
$order_qty_approved+=$rr['sq'];
$order_value+=$rr['sv'];
$order_qty+=$rr['sq'];
}
if ($_REQUEST['hide_zeros']=='on')
if (($r['inv_qty']==0) &&
($r['inv_value']==0) &&
($order_qty_approved==0) &&
($order_value_approwed==0) &&
($order_qty_created==0) &&
($order_value_created==0) &&
($stock_m==0)) {
continue;
}
$pur12m+=$last[$r['id']];
@$stock_m=$r['inv_value']/($last[$r['id']]/12);
if(count($sdata)>0){
foreach($sdata as $sd){
$cbm+=$sd[$r['id']]['cbm'];
}
}
$cbm_total+=$cbm;
$inv_qty_total+=$r['inv_qty'];
$inv_value_total+=$r['inv_value'];
$order_value_total+=$order_value;
$order_qty_total+=$order_qty;
$stock_m_total+=$stock_m;
$order_value_total_created+=$order_value_created;
$order_qty_total_created+=$order_qty_created;
$order_value_total_approved+=$order_value_approved;
$order_qty_total_approved+=$order_qty_approved;
$order_value_total_delivered+=$order_value_delivered;
$order_qty_total_delivered+=$order_qty_delivered;
$sum_pur = 0;
for($j=0;$j<count($months);$j++){
$sum_pur+=$sdata[$j+1][$r['id']]['purchase'];
}
$sum_pur_avg = round($sum_pur / $mmm,0);
if ($sum_pur!=0)
$stock_m = $r['inv_value'] / $sum_pur_avg;
//$stock_m = $sum_pur_avg;
$sum_pur_avg_total+=$sum_pur_avg;
$show[$r['id']]=1;
if ($i % 2 == 0) $css = $trs; else $css = $trs_grey;
$i++;
$t.=$css.'<td style="max-width:250px;overflow:hidden;white-space:nowrap;border-bottom: 1px solid #cccccc;border-bottom: 1px solid #cccccc;" title="'.$r['code'].'&nbsp;&nbsp;&nbsp;'.$r['name'].'">'.$r['code'].'&nbsp;&nbsp;&nbsp;<strong>'.$r['name'].'</strong>'.$tde_cat;
$t.=$tds_cat2.'<strong>'.number_format($r['inv_qty'],0,"","").'</strong>'.$tde_cat;
$t.=$tds_cat.'<strong>'.number_format($r['inv_value'],0,",",".").'</strong>'.$tde_cat;
//$t.=$tds_cat.'<strong>'.number_format($order_qty,0,",",".").'</strong>'.$tde_cat;
//$t.=$tds_cat.'<strong>'.number_format($order_value,0,",",".").'</strong>'.$tde_cat;
$t.=$tds_cat.'<strong>'.number_format($order_qty_approved,0,",",".").'</strong>'.$tde_cat;
$t.=$tds_cat.'<strong>'.number_format($order_value_approved,0,",",".").'</strong>'.$tde_cat;
$t.=$tds_cat.'<strong>'.number_format($order_qty_created,0,",",".").'</strong>'.$tde_cat;
$t.=$tds_cat.'<strong>'.number_format($order_value_created,0,",",".").'</strong>'.$tde_cat;
//$t.=$tds_cat.'<strong>'.number_format($order_qty_approved,0,",",".").'</strong>'.$tde_cat;
//$t.=$tds_cat.'<strong>'.number_format($order_value_approved,0,",",".").'</strong>'.$tde_cat;
$t.=$tds_cat.'<strong>'.number_format($stock_m,2,",",".").'</strong>'.$tde_cat;
//$t.=$tds_cat.'<strong>'.number_format($cbm,0,",",".").'</strong>'.$tde_cat;
$t.=$tre;
}
$t.=$trs;
$t.='<td class="oddListRowS1" nowrap>'.$tde;
$t.='<td class="oddListRowS1" nowrap>'.number_format($inv_qty_total,0,",",".").$tde;
$t.='<td class="oddListRowS1" nowrap>'.number_format($inv_value_total,0,",",".").$tde;
//$t.='<td class="oddListRowS1" nowrap>'.number_format($order_value_total,0,",",".").$tde;
//$t.='<td class="oddListRowS1" nowrap>'.number_format($order_qty_total,0,",",".").$tde;
$t.='<td class="oddListRowS1" nowrap>'.number_format($order_qty_total_approved,0,",",".").$tde;
$t.='<td class="oddListRowS1" nowrap>'.number_format($order_value_total_approved,0,",",".").$tde;
$t.='<td class="oddListRowS1" nowrap>'.number_format($order_qty_total_created,0,",",".").$tde;
$t.='<td class="oddListRowS1" nowrap>'.number_format($order_value_total_created,0,",",".").$tde;
//$t.='<td class="oddListRowS1" nowrap>'.number_format($order_qty_total_approved,0,",",".").$tde;
//$t.='<td class="oddListRowS1" nowrap>'.number_format($order_value_total_approved,0,",",".").$tde;
//$t.='<td class="oddListRowS1" nowrap>'.@number_format(($inv_value_total/($pur12m/12)),0,",",".").$tde;
$t.='<td class="oddListRowS1" nowrap>'.@number_format($inv_value_total/$sum_pur_avg_total,2,",",".").$tde;
$t.=$tre;
$t.=$tbe;
$t.=$tde;
$t.='<td valign="top"><div id="plist" style="overflow: auto; width: 700px;">';
//start 2 column
$t.=$tbs;
$t.=$trs;
$t.=$tdss.$year.$tdes;
for($i=0;$i<count($months);$i++){
$t.=$tds4.$months_names[$months[$i]]." ".$year.$tde;
}
$t.=$trs;
$t.=$tds22."Qty".$tde;
$t.=$tds22."Avg qty".$tde;
$t.=$tds22."Sales".$tde;
$t.=$tds22."Avg sales".$tde;
$t.=$tds22."Purchase".$tde;
$t.=$tds22."Avg purchase".$tde;
$t.=$tds22."Margin".$tde;
$t.=$tds22."CBM".$tde;
for($i=0;$i<count($months);$i++){
$t.=$tds22."Qty".$tde;
$t.=$tds22."Sales".$tde;
$t.=$tds22."Purchase".$tde;
$t.=$tds22."Margin".$tde;
$t.=$tds22."CBM".$tde;
}
$t.=$tre;
$z="select p.code,p.name,p.id from ecmproducts as p left join ecmstockstates as ss on ss.product_id=p.id where p.deleted='0' and p.product_category_id='".$_REQUEST['category_id']."'".$active_str.$status_str." group by p.id order by code";
$w=$GLOBALS['db']->query($z);
//echo '<br>'.$z;
$j=0;
while($r=$GLOBALS['db']->fetchByAssoc($w)){
if ($_REQUEST['hide_zeros']=='on') {
//echo 'hide';
if (!isset($show[$r['id']])) continue;
}
if ($j % 2 == 0) $css = $trs; else $css = $trs_grey;
$j++;
$sum_cat_qty=0;
$sum_cat_sales=0;
$sum_cat_pur=0;
$sum_cat_cbm=0;
$t.=$css;
$tl="";
for($i=0;$i<count($months);$i++){
@$margin=100*($sdata[$i+1][$r['id']]['sales']-$sdata[$i+1][$r['id']]['purchase'])/$sdata[$i+1][$r['id']]['sales'];
$tl.=$tdsf1.'<strong>'.number_format($sdata[$i+1][$r['id']]['quantity'],0,"","").'</strong>'.$tde;
$tl.=$tdsf2.'<strong>'.number_format($sdata[$i+1][$r['id']]['sales'],0,",",".").'</strong>'.$tde;
$tl.=$tdsf2.'<strong>'.number_format($sdata[$i+1][$r['id']]['purchase'],0,",",".").'</strong>'.$tde;
$tl.=$tdsf2.'<strong>'.number_format($margin,0,",",".").'%</strong>'.$tde;
$tl.=$tdsf2.'<strong>'.number_format($sdata[$i+1][$r['id']]['cbm'],0,",",".").'</strong>'.$tde;
$sum_cat_qty+=$sdata[$i+1][$r['id']]['quantity'];
$sum_cat_sales+=$sdata[$i+1][$r['id']]['sales'];
$sum_cat_pur+=$sdata[$i+1][$r['id']]['purchase'];
$sum_cat_cbm+=$sdata[$i+1][$r['id']]['cbm'];
$sum_m_qty[$i+1]+=$sdata[$i+1][$r['id']]['quantity'];
$sum_m_sales[$i+1]+=$sdata[$i+1][$r['id']]['sales'];
$sum_m_pur[$i+1]+=$sdata[$i+1][$r['id']]['purchase'];
$sum_m_cbm[$i+1]+=$sdata[$i+1][$r['id']]['cbm'];
$sum_qty+=$sdata[$i+1][$r['id']]['quantity'];
$sum_sales+=$sdata[$i+1][$r['id']]['sales'];
$sum_pur+=$sdata[$i+1][$r['id']]['purchase'];
$sum_cbm+=$sdata[$i+1][$r['id']]['cbm'];
}
@$margin=100*($sum_cat_sales-$sum_cat_pur)/$sum_cat_sales;
$t.=$tds_cat.'<strong>'.number_format($sum_cat_qty,0,"","").'</strong>'.$tde;
$t.=$tds_cat.'<strong>'.number_format($sum_cat_qty/$mmm,0,"","").'</strong>'.$tde;
$t.=$tds_cat.'<strong>'.number_format($sum_cat_sales,0,",",".").'</strong>'.$tde;
$t.=$tds_cat.'<strong>'.number_format($sum_cat_sales/$mmm,0,",",".").'</strong>'.$tde;
$t.=$tds_cat.'<strong>'.number_format($sum_cat_pur,0,",",".").'</strong>'.$tde;
$t.=$tds_cat.'<strong>'.number_format($sum_cat_pur/$mmm,0,",",".").'</strong>'.$tde;
$t.=$tds_cat.'<strong>'.number_format($margin,0,",",".").'%</strong>'.$tde;
$t.=$tds_cat.'<strong>'.number_format($sum_cat_cbm,0,",",".").'</strong>'.$tde;
$t.=$tl;
$t.=$tre;
}
$t.=$trs_cat;
$tl="";
for($i=0;$i<count($months);$i++){
@$margin=100*($sum_m_sales[$i+1]-$sum_m_pur[$i+1])/$sum_m_sales[$i+1];
$tl.=$tdsfs1.number_format($sum_m_qty[$i+1],0,"","").$tde;
$tl.=$tdsfs2.number_format($sum_m_sales[$i+1],0,"","").$tde;
$tl.=$tdsfs2.number_format($sum_m_pur[$i+1],0,",",".").$tde;
$tl.=$tdsfs2.number_format($margin,0,",",".")."%".$tde;
$tl.=$tdsfs2.number_format($sum_m_cbm[$i+1],0,",",".").$tde;
}
@$margin=100*($sum_sales-$sum_pur)/$sum_sales;
$t.=$tdsfs1.number_format($sum_qty,0,"","").$tde;
$t.=$tdsfs2.number_format($sum_qty/$mmm,0,"","").$tde;
$t.=$tdsfs2.number_format($sum_sales,0,",",".").$tde;
$t.=$tdsfs2.number_format($sum_sales/$mmm,0,",",".").$tde;
$t.=$tdsfs2.number_format($sum_pur,0,",",".").$tde;
$t.=$tdsfs2.number_format($sum_pur/$mmm,0,",",".").$tde;
$t.=$tdsfs2.number_format($margin,0,",",".")."%".$tde;
$t.=$tdsfs2.number_format($sum_cbm,0,",",".").$tde;
$t.=$tl;
$t.=$tbe;
$t.='</div>'.$tde;
$t.=$tre;
$t.=$tbe;
echo $t;
?>