<?php
$trosk = $modx->getService('trosk','Trosk',$modx->getOption('doodles.core_path',null,$modx->getOption('core_path').'components/trosk/').'model/trosk/',$scriptProperties);
if (!($trosk instanceof Trosk)) return '';

$toXls=$_REQUEST['toxls'];
if(empty($toXls)) return '';

$projectId=$_REQUEST['proj_id'];
if(empty($projectId)) return 'wwww';
$brojevi=$trosk->xlsRenderTrosk($projectId);

//return 'uf';


require_once($modx->getOption('core_path').'components/phpexcel/Classes/PHPExcel.php');
//include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
require_once($modx->getOption('core_path').'components/phpexcel/Classes/PHPExcel/Writer/Excel2007.php');

// Create new PHPExcel object
//echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

// Set properties
//echo date('H:i:s') . " Set properties\n";
$objPHPExcel->getProperties()->setCreator("troskovnik.hr");
$objPHPExcel->getProperties()->setLastModifiedBy("troskovnik.hr");
$objPHPExcel->getProperties()->setTitle("troskovnik.hr");
$objPHPExcel->getProperties()->setSubject("troskovnik.hr");
$objPHPExcel->getProperties()->setDescription("Generirao: troskovnik.hr");


// Add some data
//echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);

$objPHPExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn(1)->setWidth(55);


$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0,1,'kreirao: http://troskovnik.hr');


$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0,3,'');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1,3,'Opis stavke');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2,3,'mjerna jedinica');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3,3,'kolicina');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5,3,'Ponuda 1');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8,3,'Ponuda 2');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11,3,'Ponuda 3');


$i=4;
foreach ($brojevi as $value) {
	$i++;
	$element=explode('|',$value);
	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0,$i,$element[0]);
	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1,$i,$element[1]);
	$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow(1,$i)->getAlignment()->setWrapText(true);
	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2,$i+1,$element[2]);
	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3,$i+1,$element[3]);
	if(!empty($element[3])){
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5,$i,'jed.cijena:');
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6,$i,'ukupno:');
		$k=$i+1;
		$formula='=D'.$k.'*F'.$k.'';
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6,$i+1,$formula);

		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8,$i,'jed.cijena:');
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9,$i,'ukupno:');
		$formula='=D'.$k.'*I'.$k.'';
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9,$i+1,$formula);

		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11,$i,'jed.cijena:');
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12,$i,'ukupno:');
		$formula='=D'.$k.'*L'.$k.'';
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12,$i+1,$formula);
		
		$i++;
	}
}

$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5,$i+2,'UKUPNO:');
$formula='=SUM(G1:G'.$i.')';
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6,$i+2,$formula);

$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8,$i+2,'UKUPNO:');
$formula='=SUM(J1:J'.$i.')';
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9,$i+2,$formula);

$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11,$i+2,'UKUPNO:');
$formula='=SUM(M1:M'.$i.')';
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12,$i+2,$formula);

// Rename sheet
//echo date('H:i:s') . " Rename sheet\n";
$objPHPExcel->getActiveSheet()->setTitle('troskovnik.hr');


// Save Excel 2007 file
//echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

header('Content-Description: File Transfer');
if (headers_sent()) {
	return 'error';
}
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
// force download dialog
if (strpos(php_sapi_name(), 'cgi') === false) {
	header('Content-Type: application/force-download');
	header('Content-Type: application/octet-stream', false);
	header('Content-Type: application/download', false);
	header('Content-Type: application/vnd.ms-excel', false);
} else {
	header('Content-Type: application/vnd.ms-excel');
}
// use the Content-Disposition header to supply a recommended filename
header('Content-Disposition: attachment; filename="troskovnik.hr.xlsx";');
header('Content-Transfer-Encoding: binary');

$objWriter->save('php://output');
exit;
// Echo done
//echo date('H:i:s') . " Done writing file.\r\n";