<?php
$account=$_GET['account'];
$type=$_GET['type'];

$date_from=$_GET['date_from'];
$date_to=$_GET['date_to'];

set_include_path('include/PHPExcel/');

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

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

$alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ";


$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);

$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()->getColumnDimension('G')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);


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

$objPHPExcel->getActiveSheet()->SetCellValue("A1","Invoice No");
$objPHPExcel->getActiveSheet()->SetCellValue("B1","Type");
$objPHPExcel->getActiveSheet()->SetCellValue("C1","Account");
$objPHPExcel->getActiveSheet()->SetCellValue("D1","Register Date");
$objPHPExcel->getActiveSheet()->SetCellValue("E1","Total Brutto");
$objPHPExcel->getActiveSheet()->SetCellValue("F1","Total Netto");
$objPHPExcel->getActiveSheet()->SetCellValue("G1","Cost");
$objPHPExcel->getActiveSheet()->SetCellValue("H1","PLN Margin");
$objPHPExcel->getActiveSheet()->SetCellValue("I1","PLN Margin %");

$i=2;

$wh[]="deleted='0'";
if($type)$wh[]="type='".$type."'";
if($account)$wh[]="parent_id='".$account."'";
if($date_from)$wh[]="register_date>='".$date_from."'";
if($date_to)$wh[]="register_date<='".$date_to."'";
$where=implode(" and ",$wh);

$z="select document_no,register_date,id,parent_id,parent_name,type,ecmreceipt_id,currency_value from ecmreceipts where ".$where." order by type desc, register_date asc, name asc";
$w=$GLOBALS[db]->query($z);
//echo $z;echo mysql_error();
while($r=$GLOBALS[db]->fetchByAssoc($w)){
	$total_netto=0;
	$total_pur=0;
	$total_brutto=0;
	$total_margin=0;
	if(!$r['currency_value'])$currency_value=1;
	else $currency_value=$r['currency_value'];
	
	$ww=$GLOBALS[db]->query("select price,ecmvat_value,quantity,purchase_price,ecmproduct_id,ecmreceiptitem_id from ecmreceiptitems where ecmreceipt_id='".$r['id']."' and deleted='0'");
	while($rr=$GLOBALS[db]->fetchByAssoc($ww)){
		if($r['type']!="correct"){
			$pprice=$rr['purchase_price'];
			$total_netto+=$currency_value*$rr['price']*$rr['quantity'];
			$total_pur+=$pprice*$rr['quantity'];
			$total_brutto+=$currency_value*$rr['price']*$rr['quantity']*(1+$rr['ecmvat_value']/100);
			$total_margin+=($currency_value*$rr['price']-$pprice)*$rr['quantity'];
		}
		else{
			$rrrr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select price,quantity,purchase_price from ecmreceiptitems where id='".$rr['ecmreceiptitem_id']."'"));
			$pprice=$rrrr['purchase_price'];
			$total_netto+=$currency_value*$rr['price']*$rr['quantity']-$currency_value*$rrrr['price']*$rrrr['quantity'];
			$total_pur+=$pprice*($rr['quantity']-$rrrr['quantity']);
			$total_brutto+=($currency_value*$rr['price']*$rr['quantity']-$currency_value*$rrrr['price']*$rrrr['quantity'])*(1+$rr['ecmvat_value']/100);
			$total_margin+=($currency_value*$rr['price']-$pprice)*$rr['quantity']-($currency_value*$rrrr['price']-$pprice)*$rrrr['quantity'];
		
		}
	}
	if($total_netto-$total_margin>0)$margin=100*$total_margin/($total_netto-$total_margin);
	else $margin=0;
	
	$objPHPExcel->getActiveSheet()->SetCellValue("A".$i,$r['document_no']);
	$objPHPExcel->getActiveSheet()->SetCellValue("B".$i,$r['type']);
	$objPHPExcel->getActiveSheet()->SetCellValue("C".$i,$r['parent_name']);
	$objPHPExcel->getActiveSheet()->SetCellValue("D".$i,$r['register_date']);
	$objPHPExcel->getActiveSheet()->SetCellValue("E".$i,$total_brutto);
	$objPHPExcel->getActiveSheet()->SetCellValue("F".$i,$total_netto);
	$objPHPExcel->getActiveSheet()->SetCellValue("G".$i,($total_netto-$total_margin));
	$objPHPExcel->getActiveSheet()->SetCellValue("H".$i,$total_margin);
	$objPHPExcel->getActiveSheet()->SetCellValue("I".$i,$margin);
	
	$sum_total_netto+=$total_netto;
	$sum_total_pur+=$total_pur;
	$sum_total_brutto+=$total_brutto;
	$sum_total_margin+=$total_margin;

	$i++;
}
	if($sum_total_netto-$sum_total_margin>0)$sum_margin=100*$sum_total_margin/($sum_total_netto-$sum_total_margin);
	else $sum_margin=0;

$objPHPExcel->getActiveSheet()->SetCellValue("E".$i,$sum_total_brutto);
$objPHPExcel->getActiveSheet()->SetCellValue("F".$i,$sum_total_netto);
$objPHPExcel->getActiveSheet()->SetCellValue("G".$i,($sum_total_netto-$sum_total_margin));
$objPHPExcel->getActiveSheet()->SetCellValue("H".$i,$sum_total_margin);
$objPHPExcel->getActiveSheet()->SetCellValue("I".$i,$sum_margin);

$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:I1"
);
$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).":I".($i)
);


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
chmod("cache/upload",0777);
$microtime=str_replace(".","",str_replace(" ","",microtime()));
$name="cache/upload/DailySales".$microtime.".xlsx";
$objWriter->save($name);
chmod($name,0777);

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