<?php

include_once("modules/EcmPaymentStates/helper.php");

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 PAYMENTS");
$objPHPExcel->getProperties()->setSubject("Office 2007 PAYMENTS");
$objPHPExcel->getProperties()->setDescription("PAYMENTS");

$alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ";


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


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

$objPHPExcel->getActiveSheet()->SetCellValue("A1","Number");
$objPHPExcel->getActiveSheet()->SetCellValue("B1","Type");
$objPHPExcel->getActiveSheet()->SetCellValue("C1","Name");
$objPHPExcel->getActiveSheet()->SetCellValue("D1","To");
$objPHPExcel->getActiveSheet()->SetCellValue("E1","Total");
$objPHPExcel->getActiveSheet()->SetCellValue("F1","Document Date");
$objPHPExcel->getActiveSheet()->SetCellValue("G1","User");

$i=2;

if($_REQUEST['end']=="...")$_REQUEST['end']=0;
$inv=getInvoices($_REQUEST['account_id'],$_REQUEST['start'],$_REQUEST['end']);
foreach($inv as $in){
	$invs[]="p.id='".$in."'";
}

$z="select p.total,p.document_no,p.register_date,p.ecminvoiceout_id from ecmpayments_ecminvoiceouts as p where p.deleted='0' and (".implode(" or ",$invs).") order by p.register_date desc";
$w=$GLOBALS['db']->query($z);
while($r=$GLOBALS['db']->fetchByAssoc($w)){

	if($rr['type']=="correct")$type="Correct";
	else $type="Normal";
	if(!$rr['name'])$name="blank";
	else $name=$rr['name'];
	
	$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecminvoiceouts where document_no='".$r['document_no']."'"));
	if(!$rr['register_date'])$rr['register_date']=$r['register_date'];
	$rrr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select user_name from users where id='".$rr['assigned_user_id']."'"));
	$user=$rrr['user_name'];
	
	$objPHPExcel->getActiveSheet()->SetCellValue("A".$i,$r['document_no']);
	$objPHPExcel->getActiveSheet()->SetCellValue("B".$i,$type);
	$objPHPExcel->getActiveSheet()->SetCellValue("C".$i,$name);
	$objPHPExcel->getActiveSheet()->SetCellValue("D".$i,$rr['parent_name']);
	$objPHPExcel->getActiveSheet()->SetCellValue("E".$i,$r['total']);
	$objPHPExcel->getActiveSheet()->SetCellValue("F".$i,$rr['register_date']);
	$objPHPExcel->getActiveSheet()->SetCellValue("G".$i,$user);
	
	$i++;
}

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


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

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