<table cellspacing="0" cellpadding="0" border="0"><tr><td width="19"><img src="themes/Sugar/images/EcmProducts.gif" style="margin-top: 3px; margin-right: 3px;" alt="EcmProducts" width="16" border="0" height="16"></td>
<td width="544"><h2>Warehouse cubics </h2></td></tr></table><br />

<table class="h3Row" width="100%" border="0" cellpadding="0" cellspacing="0">
<tbody>
	<tr>
		<td nowrap="nowrap">
			<h3>Months List</h3>
		</td>
		<td width="100%">
			<img src="include/images/blank.gif" alt="" width="1" height="1">
		</td>
	</tr>
</tbody>
</table>

<?
include_once("modules/EcmReports/cache.php");
include_once("modules/EcmProductReports/vtigerConnector.php");

$vc=new vtigerConnector();
$vc->start=1;

$wc[]="1=1";
$wc[]="purchaseordercf.cf_528='0'";
$wc[]="purchaseorder.tracking_no!=''";
$vc1->where=@implode(" and ",$wc);
$vc->getData();
$cnt=$vc->getCount();
$data=$vc->data;
/*
for($i=0;$i<count($data);$i++){
	if($data[$i]['deliverydate'])$dat[$data[$i]['productcode']]=$data[$i];
}
$data=$dat;
*/
$vc1=new vtigerConnector();
$vc1->start=1;

$wc1[]="1=1";
$wc1[]="purchaseordercf.cf_528='1'";
$wc1[]="purchaseorder.tracking_no!=''";
$vc1->where=@implode(" and ",$wc1);
$vc1->getData();
$cnt=$vc1->getCount();
$data1=$vc1->data;
/*
for($i=0;$i<count($data1);$i++){
	if($data1[$i]['deliverydate'])$dat1[$data1[$i]['productcode']]=$data1[$i];
}
$data1=$dat1;
*/
function getSales($date1,$date2){
	$sale=0;
	$qty=0;
	$w=mysql_query("select p.carton_volume_meter,p.pieces_per_carton,ecminvoiceoutitems.price as price,ecminvoiceoutitems.quantity as quantity,ecminvoiceouts.register_date as date,ecminvoiceouts.type as type,ecminvoiceoutitems.ecminvoiceoutitem_id as item from ecminvoiceoutitems inner join ecminvoiceouts on ecminvoiceoutitems.ecminvoiceout_id=ecminvoiceouts.id inner join ecmproducts as p on p.id=ecminvoiceoutitems.ecmproduct_id where ecminvoiceouts.status='accepted' and ecminvoiceouts.register_date>='".$date1."' and ecminvoiceouts.register_date<='".$date2."'");
	while($r=mysql_fetch_array($w)){
		//if($r['type']!="correct"){
			$sale+=$r['price']*$r['quantity'];
			$qty+=$r['quantity'];
			@$cbm+=$r['quantity']*$r['carton_volume_meter']/$r['pieces_per_carton'];
		//}
		if($r['type']=="correct"){
			$rr=mysql_fetch_array(mysql_query("select price,quantity from ecminvoiceoutitems where id='".$r['item']."'"));
			$sale-=$rr['price']*$rr['quantity'];
			$qty-=$rr['quantity'];
			@$cbm-=$rr['quantity']*$r['carton_volume_meter']/$r['pieces_per_carton'];
			
		}
	}
	
	return array(
		"qty"=>$qty,
		"sale"=>$sale,
		"cbm"=>$cbm,
	);
}
function getQuotes($date1,$date2){
	$sale=0;
	$qty=0;
	$w=mysql_query("select p.carton_volume_meter,p.pieces_per_carton,ecmquoteitems.price as price,ecmquoteitems.quantity as quantity from ecmquoteitems inner join ecmquotes on ecmquoteitems.ecmquote_id=ecmquotes.id inner join ecmproducts as p on p.id=ecmquoteitems.ecmproduct_id where ecmquotes.status!='s10' and ecmquotes.validtill_date>'".$date1."' and ecmquotes.validtill_date<='".$date2."'");
	while($r=mysql_fetch_array($w)){
		$sale+=$r['price']*$r['quantity'];
		$qty+=$r['quantity'];
		@$cbm+=$r['quantity']*$r['carton_volume_meter']/$r['pieces_per_carton'];
	}
	
	return array(
		"qty"=>$qty,
		"sale"=>$sale,
		"cbm"=>$cbm,
	);
}
function getS($year,$month,$mmm){
	//@$c=$s['cbm']/$s['qty'];
	$ww=mysql_query("select id,carton_volume_meter,pieces_per_carton from ecmproducts where product_active='1' and deleted='0'");
	$sum=sum($year,"","","",1);
	while($rr=mysql_fetch_array($ww)){
		$w=mysql_query("select value as quantity from ecmsalesreports_predictions_cat where year='".$year."' and month='".$month."' and bean_id='".$rr['id']."'");
		if(mysql_num_rows($w)>0){
			while($r=mysql_fetch_array($w)){
				if($rr['pieces_per_carton'])$cbm+=$r['quantity']*$rr['carton_volume_meter']/$rr['pieces_per_carton'];
			}
		}
		else{
			$s=getSum($sum,$year,$rr['id']);
			if($rr['pieces_per_carton'])$cbm+=$s['qty']*$rr['carton_volume_meter']/$rr['pieces_per_carton'];
		}
	}
	
	return ($cbm/$mmm);
}
function getCbm($stock_id,$date){
	$w=mysql_query("select o.quantity,p.carton_volume_meter,p.pieces_per_carton,o.type from ecmstockoperations as o inner join ecmproducts as p on p.id=o.product_id where o.date_entered<='".$date."' and o.stock_id='".$stock_id."'");
	while($r=mysql_fetch_array($w)){
		if($r['type']==0){
			@$cbm+=$r['quantity']*$r['carton_volume_meter']/$r['pieces_per_carton'];
		}
		else{
			@$cbm-=$r['quantity']*$r['carton_volume_meter']/$r['pieces_per_carton'];
		}
	}
	return $cbm;
}
function getOrdered($arr,$month){
	if(count($arr)>0){
		foreach($arr as $a){
			if(!eregi($month,$a['deliverydate']))continue;
			$r=mysql_fetch_array(mysql_query("select carton_volume_meter,pieces_per_carton from ecmproducts where code like '".str_replace("_S","",$a['productcode'])."'"));
			@$cbm+=$a['quantity']*$r['carton_volume_meter']/$r['pieces_per_carton'];
			//echo $a['deliverydate']." ".($a['quantity']*$r['carton_volume_meter']/$r['pieces_per_carton'])." ".$a['productcode']." ".$r['pieces_per_carton']." ".$r['carton_volume_meter']."<br>";
		}
	}
	return $cbm;
}
function getMonths($month,$l){
	for($i=1;$i<=$l;$i++){
		$m[]=date("Y-m",mktime()+$i*3600*24*30);
	}
	return $m;
}

$tds1='<td class="listViewThS1">';
$trs='<tr>';
$tre='</tr>';
$tds='<td class="oddListRowS1">';
$tde='</td>';
$tbs='<table cellpadding="0" cellspacing="0" border="0" class="ListView" style="width:100%;">';
$tbe='</table>';

$mmm=(int)date("m")-1;

$t.=$tbs;
$t.=$trs;
	$t.=$tds1;
		$t.="Month";
	$t.=$tde;
	$t.=$tds1;
		$t.="First day";
	$t.=$tde;
	$t.=$tds1;
		$t.="Sales reg.";
	$t.=$tde;
	$t.=$tds1;
		$t.="Sales promo";
	$t.=$tde;
	$t.=$tds1;
		$t.="Ordered";
	$t.=$tde;
	$t.=$tds1;
		$t.="Last day";
	$t.=$tde;
$t.=$tre;

$months=getMonths(date("m"),6);

$cbm_first_day=getCbm("c7afd71a-4c3a-bde4-138d-4acaee1644e4",date("Y-m")."-01");
$s=getSales(date("Y-m")."-01",date("Y-m")."-31");
$cbm_ordered=getOrdered($data,date("Y-m"));
$cbm_ordered1=getOrdered($data1,date("Y-m"));
//$cbm=getS(date("Y"),date("m"),$mmm);
$as=getSales(date("Y")."-01-01",date("Y-m")."-01");
$qs=getQuotes(date("Y-m")."-01",date("Y-m")."-31");
//print_r($as);
$cbm=$as['cbm']/$mmm;
$cbm_q=$qs['cbm'];
if(date("m")=="06")$cbm_fd=753.4;
else $cbm_fd=($cbm_first_day+$cbm_ordered1);
$cbm_last_day=$cbm_fd-$cbm-$cbm_q+$cbm_ordered;

$t.='<tr>';
$t.='<td class="oddListRowS1" width="5%">'.date("Y-m").'</td>';
$t.='<td class="oddListRowS1" width="5%">'.number_format($cbm_fd,2,",",".").'</td>';
$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm),2,",",".").'</td>';
$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm_q),2,",",".").'</td>';
$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm_ordered),2,",",".").'</td>';
$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm_last_day),2,",",".").'</td>';
$t.='</tr>';
$mmm=(int)date("m")-1;
foreach($months as $m){
	$cbm_first_day=$cbm_last_day;
	$cbm_ordered=getOrdered($data,$m);
	$e=explode("-",$m);
	$qs=getQuotes($m."-01",$m."-31");
	$cbm_q=$qs['cbm'];
	//$cbm=getS($e[0],(int)$e[1],$mmm);
	$cbm_last_day=$cbm_first_day+$cbm_ordered-$cbm-$cbm_q;;
	$t.='<tr>';
	$t.='<td class="oddListRowS1" width="5%">'.$m.'</td>';
	$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm_first_day),2,",",".").'</td>';
	$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm),2,",",".").'</td>';
	$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm_q),2,",",".").'</td>';
	$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm_ordered),2,",",".").'</td>';
	$t.='<td class="oddListRowS1" width="5%">'.number_format(($cbm_last_day),2,",",".").'</td>';
	$t.='</tr>';
}
	   
$t.='</tbody>';
$t.='</table>';
echo $t;

?>