<?php
set_time_limit(999999);
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 PRICEBOOK");
$objPHPExcel->getProperties()->setSubject("Office 2007 PRICEBOOK");
$objPHPExcel->getProperties()->setDescription("PRICEBOOK");

$alf1="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for($i=0;$i<strlen($alf1);$i++)$alf[$i]=$alf1[$i];
$alf[26]="AA";
$alf[27]="AB";
$alf[28]="AC";
$alf[29]="AD";
$alf[30]="AE";
$alf[31]="AF";
$alf[32]="AG";
$alf[33]="AH";
$alf[34]="AI";
$alf[35]="AJ";
$alf[36]="AK";
$alf[37]="AL";
$alf[38]="AM";
$alf[39]="AN";
$alf[40]="AO";
$alf[41]="AP";
$alf[42]="AQ";
$alf[43]="AR";
$alf[44]="AS";
$alf[45]="AT";
$alf[46]="AU";
$alf[47]="AV";
$alf[48]="AW";
$alf[49]="AX";
$alf[50]="AY";
$alf[51]="AZ";
$alf[52]="BA";
$alf[53]="BB";
$alf[54]="BC";
$alf[55]="BD";
$alf[56]="BE";
$alf[]="BF";
$alf[]="BG";
$alf[]="BH";
$alf[]="BI";
$alf[]="BJ";
$alf[]="BK";
$alf[]="BL";
$alf[]="BM";
$alf[]="BN";
$alf[]="BO";
$alf[]="BP";
$alf[]="BQ";
$alf[]="BR";
$alf[]="BS";
$alf[]="BT";
$alf[]="BU";
$alf[]="BV";
$alf[]="BW";
$alf[]="BX";
$alf[]="BY";
$alf[]="BZ";
$alf[]="CA";
$alf[]="CB";
$alf[]="CC";
$alf[]="CD";
$alf[]="CE";
$alf[]="CF";
$alf[]="CG";
$alf[]="CH";
$alf[]="CI";
$alf[]="CJ";
$alf[]="CK";
$alf[]="CL";
$alf[]="CM";
$alf[]="CN";
$alf[]="CO";
$alf[]="CP";
$alf[]="CQ";
$alf[]="CR";
$alf[]="CS";
$alf[]="CT";
$alf[]="CU";
$alf[]="CV";
$alf[]="CW";
$alf[]="CX";
$alf[]="CY";
$alf[]="CZ";
$alf[]="DA";
$alf[]="DB";
$alf[]="DC";
$alf[]="DD";
$alf[]="DE";
$alf[]="DF";
$alf[]="DG";
$alf[]="DH";
$alf[]="DI";
$alf[]="DJ";
$alf[]="DK";
$alf[]="DL";
$alf[]="DM";
$alf[]="DN";
$alf[]="DO";
$alf[]="DP";
$alf[]="DQ";
$alf[]="DR";
$alf[]="DS";
$alf[]="DT";
$alf[]="DU";
$alf[]="DV";
$alf[]="DW";
$alf[]="DX";
$alf[]="DY";
$alf[]="DZ";

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(150);


$objPHPExcel->getActiveSheet()->SetCellValue('A1','Kod');
$objPHPExcel->getActiveSheet()->SetCellValue('B1','Nazwa');


//get products
$res = $GLOBALS['db']->query("SELECT id, name, code FROM ecmproducts WHERE product_category_id='77b2bbbc-7ae1-47c3-a5d9-484da1665e3e' AND code LIKE '%RE%' ORDER BY code");

$r=2;
while ($row = $GLOBALS['db']->fetchByAssoc($res)) {
	$objPHPExcel->getActiveSheet()->SetCellValue('A'.$r,$row['code']);
	$objPHPExcel->getActiveSheet()->SetCellValue('B'.$r,$row['name']);
	$r++;
}


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

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