<?php 
error_reporting(E_ALL);
set_time_limit(999999);

include_once("modules/EcmQuotes/EcmQuote.php");
$q=new EcmQuote();
$q->retrieve($_REQUEST['record']);

$ean = $_REQUEST['show_ean'];

$db = $GLOBALS['db'];

set_include_path('include/PHPExcel/');

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

$labels = return_module_language($q->ecmlanguage, 'EcmQuotes');

$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("E5 CRM");
$objPHPExcel->getProperties()->setLastModifiedBy("E5 CRM");

$alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
$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()->getColumnDimension('J')->setWidth(20);

//$objPHPExcel->getActiveSheet()->getRowDimension('9')->setHeight(50);

$ftt=explode("
",$t['footer_text']);
foreach($ftt as $ft){
	$i++;
	$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,$ft);
}
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('A1',$t['account_name']);
$i+=2;
$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,$labels['LBL_PDF_CONTENT_QUOTE_FOR']);
$objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$labels['LBL_PDF_CONTENT_NUMBER']);
$i++;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,$q->parent_name);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$q->document_no);
$i++;
//$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,$q->parent_address_street);
$objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$labels['LBL_PDF_CONTENT_REGISTER_DATE']);
$i++;
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$q->register_date);
$i++;
//$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,$q->parent_address_postalcode.', '.$q->parent_address_city);
$objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$labels['LBL_PDF_CONTENT_ISSUED_BY']);
$i++;
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$q->assigned_user_name);
$i++;
$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getFont()->setSize(24);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i,$q->name);

$i+=2;

$objPHPExcel->getActiveSheet()->duplicateStyleArray(
		array(
				'fill' => array(
						'type'	=> PHPExcel_Style_Fill::FILL_SOLID,
						'color'	=> array('argb' => 'FFCCFFCC')
				),
				'borders' => array(
						'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
						'right'		=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
				)
		),
		"A".$i.":".($ean=='0'?"I":"J").$i
);

$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('D'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,$labels['LBL_PDF_CONTENT_LIST_POSITION']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$i,$labels['LBL_PDF_CONTENT_LIST_CODE']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i,$labels['LBL_PDF_CONTENT_LIST_NAME']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i,$labels['LBL_PDF_CONTENT_LIST_QTY']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i,$labels['LBL_PDF_CONTENT_LIST_UNIT']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i,$labels['LBL_PDF_CONTENT_LIST_PRICE_NETTO']);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$labels['LBL_PDF_CONTENT_LIST_TOTAL_NETTO']);
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$i,$labels['LBL_PDF_CONTENT_LIST_RECIPIENT_CODE']);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$labels['LBL_PDF_CONTENT_LIST_REMARKS']);

if ($ean!='0')
	$objPHPExcel->getActiveSheet()->SetCellValue('J'.$i,$labels['LBL_PDF_CONTENT_LIST_EAN']);

$ii++;
$i++;
$lv=return_app_list_strings_language($q->ecmlanguage);
if ($ean=='0')
	$ww=$GLOBALS['db']->query("select * from ecmquoteitems where ecmquote_id='".$q->id."' and deleted='0' order by position asc");
else
	$ww=$GLOBALS['db']->query("select qi.*, p.ean, p.ean2 from ecmquoteitems as qi inner join ecmproducts as p on p.id=qi.ecmproduct_id where qi.ecmquote_id='".$q->id."' and qi.deleted='0' order by qi.position asc");
while($p=$GLOBALS['db']->fetchByAssoc($ww)){
	$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,intval($p['position'])+1);
	$objPHPExcel->getActiveSheet()->SetCellValue('B'.$i,$p['code']);
	$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i,html_entity_decode($p['name']));
	$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i,$p['quantity']);
	$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i,$lv['ecmproducts_unit_dom'][$p['dd_unit_id']]);
	$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i,$p['price_netto']);
	$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$p['total_netto']);
	$objPHPExcel->getActiveSheet()->SetCellValue('H'.$i,$p['recipient_code']);
	$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$p['remarks']);
	if ($ean=='1')
		$objPHPExcel->getActiveSheet()->SetCellValue('J'.$i,$p['ean']);
	if ($ean=='2')
		$objPHPExcel->getActiveSheet()->SetCellValue('J'.$i,$p['ean2']);
	$i++;
}

$sumary_start=$i;

$objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$i,$labels['LBL_PDF_CONTENT_TOTAL_NETTO']);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$q->total_netto);

if ($q->no_tax!='1') {
	$vats = explode(",", $q->vats_summary);
	foreach ($vats as $v) {
		$vats2 = explode(":", $v);
		if ($vats2[0]=='') continue;
		
		$i++;
		$objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->SetCellValue('H'.$i,$vats2[0]);
		$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$vats2[1]);
	}

$i++;
$objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$i,$labels['LBL_PDF_CONTENT_TOTAL_BRUTTO']);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i,$q->total_brutto);
}
$summary_end=$i;

$objPHPExcel->getActiveSheet()->duplicateStyleArray(
		array(
				'fill' => array(
						'type'	=> PHPExcel_Style_Fill::FILL_SOLID,
						'color'	=> array('argb' => 'FFCCFFCC')
				),
				'borders' => array(
						'bottom'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
						'right'		=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
				)
		),
		"H".$sumary_start."I".$summary_end
);

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

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

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