<?php
if(!$_GET['date'])$date=date("Y-m-d");
else $date=$GLOBALS['timedate']->to_db_date($_GET['date']);
$exp=explode("-",$date);
$date=date("Y-m-d",mktime(0,0,0,$exp[1],$exp[2],$exp[0])+24*3600);
if(!$date)$date=date("Y-m-d");
include_once("modules/EcmProductReports/vtigerConnector.php");
include_once('modules/EcmCharts/chartHelper.php');
$vc=new vtigerConnector();
set_include_path('include/PHPExcel/');

include 'PHPExcel.php';
//include 'PHPExcel/Writer/Excel2007.php';
include 'PHPExcel/Writer/Excel5.php';
include 'PHPExcel/IOFactory.php';

$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("E5 CRM");
$objPHPExcel->getProperties()->setLastModifiedBy("E5 CRM");
$objPHPExcel->getProperties()->setTitle("Office 2007 STATES");
$objPHPExcel->getProperties()->setSubject("Office 2007 STATES");
$objPHPExcel->getProperties()->setDescription("STATES");

$alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ";


$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);


$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->setActiveSheetIndex(0);


if($date==date("Y-m-d",mktime()+24*3600))$dat=date("Y-m-d H:i:s");
else $dat=$date." 23:59:59";
$objPHPExcel->getActiveSheet()->SetCellValue("A1","Inventory states: ".$dat);

$objPHPExcel->getActiveSheet()->SetCellValue("A2","Index");
$objPHPExcel->getActiveSheet()->SetCellValue("B2","Name");
$objPHPExcel->getActiveSheet()->SetCellValue("C2","Category");
$objPHPExcel->getActiveSheet()->SetCellValue("D2","Quantity");
$objPHPExcel->getActiveSheet()->SetCellValue("E2","Price");
$objPHPExcel->getActiveSheet()->SetCellValue("F2","Value");

$i=3;

//$wh[]="product_active='1'";
foreach($_REQUEST['check'] as $ch){
	$whk[]="id='".$ch."'";
}
if(count($whk)>0)$wh[]="(".implode(" or ",$whk).")";

$wh[]="deleted='0'";
if($_REQUEST['category_id'])$wh[]="product_category_id='".$_REQUEST['category_id']."'";
if($_REQUEST['active']=="active")$wh[]="product_active='1'";
if($_REQUEST['active']=="inactive")$wh[]="product_active='0'";

$who[]="deleted='0'";
if($date)$who[]="date_entered<='".$date." 23:59:59'";
if($_REQUEST['stock_id'])$who[]="stock_id='".$_REQUEST['stock_id']."'";

$where=implode(" and ",$wh);

$z="select id,name,code,product_category_name from ecmproducts where ".$where." order by code asc, name asc";
//echo $z;
$w=mysql_query($z);
echo mysql_error();
$sum_qty=0;
$sum_price=0;
while($r=mysql_fetch_array($w)){	
	$value=0;
	$qty=0;
	$price=0;
	if($date!=date("Y-m-d",mktime()+24*3600)){
		$arr=array();
		$pp1=0;
		$q1=0;
		$ww=$GLOBALS['db']->query("select quantity as qty,id,price from ecmstockoperations where deleted='0' and type='0' and in_id IS NULL and product_id='".$r['id']."' and (".implode(" and ",$who).")");
		while($rrr=$GLOBALS['db']->fetchByAssoc($ww)){
			$www=$GLOBALS['db']->query("select quantity from ecmstockoperations where in_id='".$rrr['id']."' and type='1' and in_id IS NOT NULL and product_id='".$r['id']."' and (".implode(" and ",$who).") and deleted='0'");
			$rqty=0;
			while($rrp=$GLOBALS['db']->fetchByAssoc($www)){
				$rqty+=$rrp['quantity'];
			}
			$qty=$rrr['qty']-$rqty;
			if($qty>0){
				$arr[]=array("qty"=>$qty,"id"=>$rrr['id'],"price"=>$rrr['price']);
			}
		}

		if(count($arr)>0){
			foreach($arr as $v){
				$q1+=$v['qty'];
				$pp1+=$v['qty']*$v['price'];
			}
		}
		$qty=$q1;
		$value=$pp1;
		
		@$price=$value/$qty;
	}
	else{
		$whs=array();
		$whs[]="deleted='0'";
		$whs[]="product_id='".$r['id']."'";
		if($_GET['stock_id'])$whs[]="stock_id='".$_GET['stock_id']."'";
		$whes=implode(" and ",$whs);
		$wss=mysql_query("select quantity,price from ecmstockstates where ".$whes);
		while($rss=mysql_fetch_array($wss)){
			$qty+=$rss['quantity'];
			@$value+=$rss['quantity']*$rss['price'];
			
		}
		@$price=$value/$qty;
	}
	$r['qty']=$qty;
	$r['price']=$price;
	$r['value']=$value;
	$arr[]=array(
			"id"=>$r['id'],
			"qty"=>$qty,
			"code"=>$r['code'],
			"name"=>$r['name'],
			"product_category_id"=>$r['product_category_id'],
			"product_category_name"=>$r['product_category_name'],
			"price"=>$price,
			"value"=>$value,
	);
//}

//if(!$_REQUEST['order_by'])$_REQUEST['order_by']="code";
//if(!$_REQUEST['sorder'])$_REQUEST['sorder']="asc";
//$arr=multisort($arr,array(array('key'=>$_REQUEST['order_by'],'sort'=>$_REQUEST['sorder'])));

//if(count($arr)>0){
	//foreach($arr as $r){
		if($_REQUEST['empty']=="yes" && $qty==0)continue;
		if($_REQUEST['empty']=="no" && $qty!=0)continue;
	
		$objPHPExcel->getActiveSheet()->SetCellValue("A".$i,$r['code']);
		$objPHPExcel->getActiveSheet()->SetCellValue("B".$i,$r['name']);
		$objPHPExcel->getActiveSheet()->SetCellValue("C".$i,$r['product_category_name']);
		$objPHPExcel->getActiveSheet()->SetCellValue("D".$i,$r['qty']);
		$objPHPExcel->getActiveSheet()->SetCellValue("E".$i,$r['price']);
		$objPHPExcel->getActiveSheet()->SetCellValue("F".$i,$r['value']);
	

		$sum_qty+=$r['qty'];
		$sum_price+=$r['value'];
	
		$i++;
	//}
}

$objPHPExcel->getActiveSheet()->SetCellValue("D".$i,$sum_qty);
@$objPHPExcel->getActiveSheet()->SetCellValue("E".$i,$sum_price/$sum_qty);
$objPHPExcel->getActiveSheet()->SetCellValue("F".$i,$sum_price);

$objPHPExcel->getActiveSheet()->duplicateStyleArray(
	array(
		'fill' => array(
			'type'	=> PHPExcel_Style_Fill::FILL_SOLID,
			'color'	=> array('argb' => 'F0F0F0')
		),
		'borders' => array(	
			'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
			'right'		=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
		)
	),
	"A1:F1"
);
$objPHPExcel->getActiveSheet()->duplicateStyleArray(
	array(
		'fill' => array(
			'type'	=> PHPExcel_Style_Fill::FILL_SOLID,
			'color'	=> array('argb' => 'F0F0F0')
		),
		'borders' => array(	
			'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
			'right'		=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
		)
	),
	"A2:F2"
);
$objPHPExcel->getActiveSheet()->duplicateStyleArray(
	array(
		'fill' => array(
			'type'	=> PHPExcel_Style_Fill::FILL_SOLID,
			'color'	=> array('argb' => 'F0F0F0')
		),
		'borders' => array(	
			'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
			'right'		=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
		)
	),
	"A".($i).":F".($i)
);


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
chmod("cache/upload",0777);
$microtime=str_replace(".","",str_replace(" ","",microtime()));
$name="cache/upload/States".$microtime.".xls";
$objWriter->save($name);
chmod($name,0777);

header("Location: ".$name);
?>