<?php
set_time_limit(9999999);
function sum($year,$c,$all,$acc,$active=""){	
	if($acc)$ah="ecminvoiceouts.parent_name  like '".$acc."' and ";
	else $ah="";
	if($all)$wh="";
	else $wh=" and ecmproducts.product_category_id='".$c."'";
	if(!$active)$aa="";
	elseif($active=="inactive")$aa=" and ecmproducts.product_active='0'";
	else $aa=" and ecmproducts.product_active='1'";
	
	$ww=mysql_query("select ecminvoiceouts.type as type,ecminvoiceouts.ecminvoiceout_id as ecminvoiceout_id,ecmproducts.product_category_id as c,ecmproducts.id as ecmproduct_id,ecmproducts.carton_volume_meter as cvm,ecmproducts.pieces_per_carton as ppc,ecminvoiceouts.register_date as d,ecminvoiceoutitems.purchase_price as purchase_price,ecminvoiceoutitems.price as price,ecminvoiceoutitems.quantity as quantity,ecminvoiceoutitems.ecmproduct_id as ecmproduct_id from ecminvoiceoutitems left join ecminvoiceouts on ecminvoiceouts.id=ecminvoiceoutitems.ecminvoiceout_id inner join ecmproducts on ecmproducts.id=ecminvoiceoutitems.ecmproduct_id where ".$ah."ecminvoiceouts.register_date like '".$year."%' and ecminvoiceoutitems.deleted='0'".$wh." and ecminvoiceouts.type!='correct'".$aa."");
	echo mysql_error();
	while($rr=mysql_fetch_array($ww)){
		//if($rr['type']!="correct"){
			$d=explode("-",$rr['d']);
			$dat=$year."-".$d[1];
			
			if($rr['type']=="correct"){
				$rrr=mysql_fetch_array(mysql_query("select purchase_price from ecminvoiceoutitems where ecmproduct_id='".$rr['ecmproduct_id']."' and ecminvoiceout_id='".$rr['ecminvoiceout_id']."' and deleted='0'"));
				$pprice=$rrr['purchase_price'];
			}
			else $pprice=$rr['purchase_price'];
			
			$sum['all']+=$rr['price']*$rr['quantity'];
			$sum['all_qty']+=$rr['quantity'];
			$sum['all_pur']+=$pprice*$rr['quantity'];
			if($rr['ppc']>0)$sum['all_cbm']+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
			
			$sum[$dat]['all']+=$rr['price']*$rr['quantity'];
			$sum[$dat]['all_qty']+=$rr['quantity'];
			$sum[$dat]['all_pur']+=$pprice*$rr['quantity'];
			if($rr['ppc']>0)$sum[$dat]['all_cbm']+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
				
			$sum[$dat]['category_'.$rr['c']]+=$rr['price']*$rr['quantity'];
			$sum[$dat]['category_qty_'.$rr['c']]+=$rr['quantity'];
			$sum[$dat]['category_pur_'.$rr['c']]+=$pprice*$rr['quantity'];
			if($rr['ppc']>0)$sum[$dat]['category_cbm_'.$rr['c']]+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
		
			$sum[$dat]['product_'.$rr['ecmproduct_id']]+=$rr['price']*$rr['quantity'];
			$sum[$dat]['product_qty_'.$rr['ecmproduct_id']]+=$rr['quantity'];
			$sum[$dat]['product_pur_'.$rr['ecmproduct_id']]+=$pprice*$rr['quantity'];
			if($rr['ppc']>0)$sum[$dat]['product_cbm_'.$rr['ecmproduct_id']]+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
				
			$sum[$year]['category_sum_'.$rr['c']]+=$rr['price']*$rr['quantity'];
			$sum[$year]['category_qty_sum_'.$rr['c']]+=$rr['quantity'];
			$sum[$year]['category_pur_sum_'.$rr['c']]+=$pprice*$rr['quantity'];
			if($rr['ppc']>0)$sum[$year]['category_cbm_sum_'.$rr['c']]+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
				
			$sum[$year]['product_sum_'.$rr['ecmproduct_id']]+=$rr['price']*$rr['quantity'];
			$sum[$year]['product_qty_sum_'.$rr['ecmproduct_id']]+=$rr['quantity'];
			$sum[$year]['product_pur_sum_'.$rr['ecmproduct_id']]+=$pprice*$rr['quantity'];
			if($rr['ppc']>0)$sum[$year]['product_cbm_sum_'.$rr['ecmproduct_id']]+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
		//}
	}
	
	$ww=mysql_query("select ecmproducts.product_category_id as c,ecminvoiceouts.id as invid,ecminvoiceouts.ecminvoiceout_id as ecminvoiceout_id,ecmproducts.id as ecmproduct_id,ecmproducts.carton_volume_meter as cvm,ecmproducts.pieces_per_carton as ppc,ecminvoiceouts.register_date as d,ecminvoiceoutitems.purchase_price as purchase_price,ecminvoiceoutitems.price as price,ecminvoiceoutitems.quantity as quantity,ecminvoiceoutitems.ecmproduct_id as ecmproduct_id from ecminvoiceoutitems inner join ecminvoiceouts on ecminvoiceouts.id=ecminvoiceoutitems.ecminvoiceout_id inner join ecmproducts on ecmproducts.id=ecminvoiceoutitems.ecmproduct_id where ".$ah."ecminvoiceouts.register_date like '".$year."%' and ecminvoiceoutitems.deleted='0'".$wh." and ecminvoiceouts.type='correct'".$aa);
	echo mysql_error();
	while($rr=mysql_fetch_array($ww)){
		$www=mysql_query("select id from ecminvoiceoutitems where ecminvoiceout_id='".$rr['ecminvoiceout_id']."' and ecmproduct_id='".$rr['ecmproduct_id']."' and deleted='0' and (price='".$rr['price']."' and quantity='".$rr['quantity']."')");
		if(mysql_num_rows($www)>0)continue;
		$www=mysql_query("select quantity,price,purchase_price from ecminvoiceoutitems where ecminvoiceout_id='".$rr['ecminvoiceout_id']."' and ecmproduct_id='".$rr['ecmproduct_id']."' and deleted='0' and (price!='".$rr['price']."' or quantity!='".$rr['quantity']."')");
		$rrr=mysql_fetch_array($www);
		
		$cqty=$rr['quantity']-$rrr['quantity'];
		$cprice=$rr['quantity']*$rr['price']-$rrr['quantity']*$rrr['price'];
		$cpprice=$cqty*$rr['purchase_price'];
		
		if($cqty==0 && $rr['price']==$rrr['price'])continue;
	
		$d=explode("-",$rr['d']);
		$dat=$year."-".$d[1];
			
		$sum['all']+=$cprice;
		$sum['all_qty']+=$cqty;
		$sum['all_pur']+=$cpprice;
		if($rr['ppc']>0)$sum['all_cbm']+=$cqty*($rr['cvm']/$rr['ppc']);
			
		$sum[$dat]['all']+=$cprice;
		$sum[$dat]['all_qty']+=$cqty;
		$sum[$dat]['all_pur']+=$cpprice;
		if($rr['ppc']>0)$sum[$dat]['all_cbm']+=$cqty*($rr['cvm']/$rr['ppc']);
				
		//if(!$all){
			$sum[$dat]['category_'.$rr['c']]+=$cprice;
			$sum[$dat]['category_qty_'.$rr['c']]+=$cqty;
			$sum[$dat]['category_pur_'.$rr['c']]+=$cpprice;
			if($rr['ppc']>0)$sum[$dat]['category_cbm_'.$rr['c']]+=$cqty*($rr['cvm']/$rr['ppc']);
		
			$sum[$dat]['product_'.$rr['ecmproduct_id']]+=$cprice;
			$sum[$dat]['product_qty_'.$rr['ecmproduct_id']]+=$cqty;
			$sum[$dat]['product_pur_'.$rr['ecmproduct_id']]+=$cpprice;
			if($rr['ppc']>0)$sum[$dat]['product_cbm_'.$rr['ecmproduct_id']]+=$cqty*($rr['cvm']/$rr['ppc']);
				
			$sum[$year]['category_sum_'.$rr['c']]+=$cprice;
			$sum[$year]['category_qty_sum_'.$rr['c']]+=$cqty;
			$sum[$year]['category_pur_sum_'.$rr['c']]+=$cpprice;
			if($rr['ppc']>0)$sum[$year]['category_cbm_sum_'.$rr['c']]+=$cqty*($rr['cvm']/$rr['ppc']);
				
			$sum[$year]['product_sum_'.$rr['ecmproduct_id']]+=$cprice;
			$sum[$year]['product_qty_sum_'.$rr['ecmproduct_id']]+=$cqty;
			$sum[$year]['product_pur_sum_'.$rr['ecmproduct_id']]+=$cpprice;
			if($rr['ppc']>0)$sum[$year]['product_cbm_sum_'.$rr['ecmproduct_id']]+=$cqty*($rr['cvm']/$rr['ppc']);
		//}
	}
	return $sum;
}
$arr=array(2009);
//echo "test";
foreach($arr as $year){
	$sum=sum($year,"",1,"","");
	$sum['all']=10358061.33;
	
	if($year==20091)$dddd="d";
	else $dddd="";
	$file="modules/EcmReports/cache".$year.$dddd.".dat";
	if(file_exists($file))unlink($file);
	file($file);
	file_put_contents($file,base64_encode(serialize($sum)));
	//print_r(unserialize(base64_decode(file_get_contents($file))));
	//print_r($sum);
}
?>