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

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

require_once('modules/EcmTexts/EcmText.php');
if(isset($q->ecmlanguage) && $q->ecmlanguage != '') {
	$data = EcmText::LoadText(null,null,"EcmQuotes",$q->ecmlanguage);
	if(isset($data[0]) && isset($data[0]['data']) && isset($data[0]['data']['labels'])) {
		$data = $data[0]['data']['labels'];
		foreach($data as $key => $value) {
			$mod_strings[$value['label']] = $value['translation'];
		}
	}
}

$t=new EcmDocumentTemplate();
$t=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmdocumenttemplates where id='".$q->template_id."'"));

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

$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);




$t->footer_text=str_replace("/$dt_account_name",$t['account_name'],$t['footer_text']);
$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,$mod_strings['LBL_PDF_QUOTE_FOR']);
$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$mod_strings['LBL_PDF_NUMBER']);
$i++;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,$q->parent_name);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$q->document_no);
$i++;
$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$mod_strings['LBL_PDF_DATE_REGISTER']);
$i++;
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$q->register_date);
$i++;
$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$mod_strings['LBL_PDF_OWNER']);
$i++;
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$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.":G".$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()->SetCellValue('A'.$i,$mod_strings['LBL_PDF_LIST_POSITION']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$i,$mod_strings['LBL_PDF_LIST_CODE']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i,$mod_strings['LBL_PDF_LIST_DESCRIPTION']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i,$mod_strings['LBL_PDF_LIST_QUANTITY']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i,$mod_strings['LBL_PDF_LIST_UNIT']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i,$mod_strings['LBL_PDF_LIST_PRICE']);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$mod_strings['LBL_PDF_LIST_TOTAL']);


$ii++;
$i++;
//$lv=return_app_list_strings_language($this->ecmlanguage);
$ww=$GLOBALS['db']->query("select * from ecmquoteitems where ecmquote_id='".$q->id."' and deleted='0'");
while($p=$GLOBALS['db']->fetchByAssoc($ww)){
	$w=$GLOBALS['db']->query("select code,name,quantity,ecmcomponent_id from ecmproductcomponents where ecmproduct_id='".$p['ecmproduct_id']."' and deleted='0' and position='0'");
	if(mysql_num_rows($w)>0){
		$r=$GLOBALS['db']->fetchByAssoc($w);
		$p_name=$r['name'];
		$p_code=$r['code'];
	}
	else {
		$p_name=$p['name'];
		$p_code=$p['code'];
	}

	$total_netto+=$p['price']*$p['quantity'];
	$total_vat+=$p['price']*$p['quantity']*$p['ecmvat_value']/100;
	$vats[$p['ecmvat_value']]+=$p['price']*$p['quantity']*$p['ecmvat_value']/100;
	if($q->show_ean)$p_code.='
'.$p['ean'];
	$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,$ii);
	$objPHPExcel->getActiveSheet()->SetCellValue('B'.$i,$p_code);
	$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i,$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']);
	$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$p['total']);
	
	if($p['ecmproduct_id']){
		$w=$GLOBALS['db']->query("select code,name,quantity,ecmcomponent_id from ecmproductcomponents where ecmproduct_id='".$p['ecmproduct_id']."' and deleted='0' and position>0 order by name asc");
		if(mysql_num_rows($w)>0){
			while($r=$GLOBALS['db']->fetchByAssoc($w)){
				$i++;
				$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i,"+");
				$objPHPExcel->getActiveSheet()->SetCellValue('B'.$i,$r['code']);
				$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i,$r['name']);
				$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i,$r['quantity']);
				$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i,$lv['ecmproducts_unit_dom'][$r['dd_unit_id']]);
			}
			$i++;
		}
	}
	
	$i++;
	$ii++;
}

$sumary_start=$i;

$objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i,$mod_strings['LBL_PDF_TOTAL']);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$total_netto);
foreach($vats as $k=>$v){
	$i++;
	$objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i,$mod_strings['LBL_PDF_VAT']." (".$k."%)");
	$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,$v);
}
$i++;
$objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i,$mod_strings['LBL_PDF_END_TOTAL']);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$i,($total_netto+$total_vat));

$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)
		)
	),
	"F".$sumary_start.":G".$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);
?>