<?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->invoice_kind = $_GET['invoice_kind'];
$s->group_ks = $_GET['group_ks'];
$sdata=$s->getSalesByCategory();
//echo '<pre>';print_r($sdata);echo '</pre>';
include("modules/EcmReports/formCategory.php");

$year=$_GET['year'];

if($year==date("Y")){
	$mmm=(int)date("m")-1;
}
else $mmm=12;
if(!$mmm)$mmm=1;

$trs='<tr>';
$trs_header='<tr style="height: 100px;">';
$trs_grey = '<tr style="background-color: #e6e6e6;">';
$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;">';
$tdsfs2='<td class="oddListRowS1" style="background-color: #BBDDFF;text-align:center;border-bottom: 1px solid #cccccc;">';
$tdsfs3='<td class="oddListRowS1" style="background-color: #BBDDFF;text-align:center;border-right: 1px solid #cccccc;border-bottom: 0px solid #cccccc;">';

$tds_cat='<td class="oddListRowS1" style="text-align: center; border-bottom: 1px solid #cccccc; min-width: 50px;">';
$tds_cat1='<td class="oddListRowS1" style="border-bottom: 1px solid #cccccc; min-width: 220px;">';
$tbs='<table cellpadding="0" cellspacing="0" border="0">';
$tbs1='<table cellpadding="0" cellspacing="0" border="0" width="">';
$tbs2='<table cellpadding="0" cellspacing="0" border="0" style="width:670px">';
$tds12='<td class="listViewThS1" style="text-align: center; width:50px;height:66px;">';
$tds22='<td class="listViewThS1" style="text-align:center;border-top:0px;min-width:100px; height:45px;">';
$tdss='<td colspan="6"  class="listViewThS1" style="text-align: center;min-width:100px;border-bottom:0px;heigh:15px;">';
$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;
$t.='<td valign="top">';

$t.=$tbs2;
$t.=$trs;
$t.=$tds12."Name".$tde;
$t.=$tds12."Inv qty".$tde;
$t.=$tds12."Inv value".$tde;
$t.=$tds12."Ord approved qty".$tde;
$t.=$tds12."Ord approved value".$tde;
$t.=$tre;

$z="select c.name,c.id,sum(ss.quantity) as inv_qty,sum(ss.quantity*p.ems_price) as inv_value from ecmproductcategories as c inner join ecmproducts as p on p.product_category_id=c.id inner join ecmstockstates as ss on ss.product_id=p.id where c.deleted='0' group by c.id order by c.name";

$w=$GLOBALS['db']->query($z);

$i=0;

//add mz 2012-05-17
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.product_category_id='".$r['id']."' and i.deleted='0' and (e.status='accepted' or e.status='mailed') and e.deleted='0'");
	//echo "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.product_category_id='".$r['id']."' and i.deleted='0' and (e.status='accepted' or e.status='mailed') and e.deleted='0'<br><br>";
	while($rr=$GLOBALS['db']->fetchByAssoc($ww)){
		$order_value_approved+=$rr['sv'];
		$order_qty_approved+=$rr['sq'];		
		$order_value+=$rr['sv'];
		$order_qty+=$rr['sq'];
	}

	
	$inv_qty_total+=$r['inv_qty'];
	$inv_value_total+=$r['inv_value'];
		
	$order_value_total_approved+=$order_value_approved;
	$order_qty_total_approved+=$order_qty_approved;	
	
	
	
	$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;
		
	$sum_pur_avg_total+=$sum_pur_avg;
	
	//$stock_m = round(5/2,2);
		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;
				}
	
	$show[$r['id']]=1;

	
	if ($i % 2 == 0) $css = $trs; else $css = $trs_grey;
	$i++;
	$t.=$css.$tds_cat1.'<strong><a href="index.php?module=EcmReports&action=salesByProduct&year='.$_GET['year'].'&active='.$_REQUEST['active'].'&account='.$_REQUEST['account'].'&account_name='.$_REQUEST['account_name'].'&category_id='.$r['id'].'">'.$r['name'].'</a></strong>'.$tde_cat;
	$t.=$tds_cat.'<strong>'.format_number($r['inv_qty']).'</strong>'.$tde_cat;
	$t.=$tds_cat.'<strong>'.format_number($r['inv_value']).'</strong>'.$tde_cat;
	$t.=$tds_cat.'<strong>'.format_number($order_qty_approved).'</strong>'.$tde_cat;
	$t.=$tds_cat.'<strong>'.format_number($order_value_approved).'</strong>'.$tde_cat;
	$t.=$tre;
}

$t.=$trs;
$t.='<td class="oddListRowS1" style="font-size: 12px;">&nbsp;'.$tde;
$t.='<td style="font-size: 12px;">'.number_format($inv_qty_total,0,",",".").$tde;
$t.='<td style="font-size: 12px;">'.number_format($inv_value_total,0,",",".").$tde;
$t.='<td style="font-size: 12px;">'.number_format($order_qty_total_approved,0,",",".").$tde;
$t.='<td style="font-size: 12px;">'.number_format($order_value_total_approved,0,",",".").$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.$tde;
for($i=0;$i<count($months);$i++){
	$t.=$tdss.$months_names[$months[$i]]." ".$year.$tde;
}
$t.=$trs;

$t.=$tds22."Qty".$tde;
$t.=$tds22."Sales".$tde;
$t.=$tds22."Purchase".$tde;
$t.=$tds22."Margin".$tde;
$t.=$tds22."Avg qty".$tde;
$t.=$tds22."Avg sales".$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."Avg qty".$tde;
	$t.=$tds22."Avg sales".$tde;
}

$t.=$tre;
$z="select c.name,c.id from ecmproductcategories as c inner join ecmproducts as p on p.product_category_id=c.id inner join ecmstockstates as ss on ss.product_id=p.id where c.deleted='0' group by c.id order by c.name";
$w=$GLOBALS['db']->query($z);

$j=0;

$categories_count = $w->num_rows;
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	if ($_REQUEST['hide_zeros']=='on') {
		if (!isset($show[$r['id']])) continue;
		$categories_count--;	
	}
	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;
	$sum_cat_inv_qty=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>'.format_number($sdata[$i+1][$r['id']]['quantity']).'</strong>'.$tde;
		$tl.=$tdsf2.'<strong>'.format_number($sdata[$i+1][$r['id']]['sales']).'</strong>'.$tde;
		$tl.=$tdsf2.'<strong>'.format_number($sdata[$i+1][$r['id']]['purchase']).'</strong>'.$tde;
		$tl.=$tdsf2.'<strong>'.format_number($margin).'%</strong>'.$tde;
		$tl.=$tdsf2.'<strong>'.format_number($sdata[$i+1][$r['id']]['quantity']/$sdata[$i+1][$r['id']]['inv_qty']).'</strong>'.$tde;
		$tl.=$tdsf2.'<strong>'.format_number($sdata[$i+1][$r['id']]['sales']/$sdata[$i+1][$r['id']]['inv_qty']).'</strong>'.$tde;
		$sum_cat_inv_qty+=$sdata[$i+1][$r['id']]['inv_qty'];
		$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.=$tdsf1.'<strong>'.format_number($sum_cat_qty).'</strong>'.$tde;
	$t.=$tdsf1.'<strong>'.format_number($sum_cat_sales).'</strong>'.$tde;
	$t.=$tdsf1.'<strong>'.format_number($sum_cat_pur).'</strong>'.$tde;
	$t.=$tdsf1.'<strong>'.format_number($margin).'%</strong>'.$tde;
	$t.=$tdsf1.'<strong>'.format_number($sum_cat_qty/$categories_count).'</strong>'.$tde;
	$t.=$tdsf1.'<strong>'.format_number($sum_cat_sales/$categories_count).'</strong>'.$tde;
	$t.=$tl;
	
	$t.=$tre;
}
$t.=$trs;
$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.=$tdsf1.format_number($sum_m_qty[$i+1]).$tde;
	//$tl.=$tdsf1.'test'.$tde;
	//$tl.=$tdsf1.number_format($sum_m_sales[$i+1],0,"","").$tde;
	$tl.=$tdsf1.format_number($sum_m_sales[$i+1]).$tde;
	$tl.=$tdsf1.format_number($sum_m_pur[$i+1]).$tde;
	$tl.=$tdsf1.format_number($margin)."%".$tde;
	$tl.=$tdsf1.format_number($sum_m_qty[$i+1]/$categories_count).$tde;
	$tl.=$tdsf1.format_number($sum_m_sales[$i+1]/$categories_count).$tde;
	//$tl.=$tdsf1.number_format($sum_m_cbm[$i+1],0,",",".").$tde;
}
@$margin=100*($sum_sales-$sum_pur)/$sum_sales;
$t.=$tdsf1.format_number($sum_qty).$tde;
//$t.=$tdsf1.'test'.$tde;
//$t.=$tdsf1.number_format($sum_qty/$mmm,0,"","").$tde;
$t.=$tdsf1.format_number($sum_sales).$tde;
//$t.=$tdsf1.number_format($sum_sales/$mmm,0,",",".").$tde;
$t.=$tdsf1.format_number($sum_pur,0).$tde;
//$t.=$tdsf1.number_format($sum_pur/$mmm,0,",",".").$tde;
$t.=$tdsf1.format_number($margin)."%".$tde;
$t.=$tdsf1.format_number($sum_qty/$mmm).$tde;
$t.=$tdsf1.format_number($sum_sales/$mmm).$tde;
//$t.=$tdsf1.number_format($sum_cbm,0,",",".").$tde;
$t.=$tl;

$t.=$tre;
$t.=$tbe;

$t.='</div>'.$tde;
$t.=$tre;
$t.=$tbe;

echo $t;

?>