Files
crm.e5.pl/modules/EcmProductCategories/createXLS.php
2024-04-27 09:23:34 +02:00

235 lines
7.3 KiB
PHP

<?php
ini_set('display_errors',1);
$account=$_GET['account'];
$account_id=$_GET['account_id'];
$account_name=$_GET['account_name'];
$type=$_GET['type'];
$date_from=$_GET['date_from'];
$date_to=$_GET['date_to'];
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 ORDERED PRODUCTS");
$objPHPExcel->getProperties()->setSubject("Office 2007 ORDERED PRODUCTS");
$objPHPExcel->getProperties()->setDescription("ORDERED PRODUCTS");
$alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$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()->setTitle('Simple');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue("A1","Produkt");
$objPHPExcel->getActiveSheet()->SetCellValue("B1","Kod");
$objPHPExcel->getActiveSheet()->SetCellValue("C1","Ilość");
$objPHPExcel->getActiveSheet()->SetCellValue("D1","Kwota");
$objPHPExcel->getActiveSheet()->SetCellValue("E1","Rozbicie");
$i=2;
$wh[]="fk.deleted='0'";
$wh[]="fk.canceled='0'";
if($type)$wh[]="fk.type='".$type."'";
$ids=array();
if(count($account)>0 && count($account)<2){
foreach ($account as $item){
$wh[]="cat.id='".$item."'";
}
} else {
foreach ($account as $item){
$ids[]="'".$item."'";
}
$ac=implode(",",$ids);
$wh[]='cat.id IN ('.$ac.')';
}
if($date_from)$wh[]="fk.register_date>='".$date_from."'";
if($date_to)$wh[]="fk.register_date<='".$date_to."'";
$where=implode(" and ",$wh);
$z="select fk.id,p.name,p.code,p.ecmproduct_id, sum(
CASE WHEN fk.type!='correct'
THEN
CASE WHEN fk.currency_value is null or fk.currency_value='' or fk.currency_value=0
THEN
p.subtotal
ELSE
p.subtotal*fk.currency_value
END
ELSE
CASE WHEN p.old_subtotal IS null OR p.old_subtotal='' THEN
0
ELSE
CASE WHEN fk.currency_value is null or fk.currency_value='' or fk.currency_value=0
THEN
p.subtotal-p.old_subtotal
ELSE
(p.subtotal-p.old_subtotal)*fk.currency_value
END
END
END
) as razem,
sum(
CASE WHEN fk.type!='correct'
THEN
p.quantity
ELSE
p.quantity-p.old_quantity
END
) as quantity,p.subprice,cat.name as cname,cat.id as cid from ecminvoiceouts as fk
inner join ecminvoiceoutitems as p on p.ecminvoiceout_id=fk.id
inner join ecmproducts as pr on pr.id=p.ecmproduct_id
inner join ecmproductcategories as cat on cat.id=pr.product_category_id
where ".$where."
group by p.ecmproduct_id order by pr.product_category_id,razem desc;";
$w=$GLOBALS[db]->query($z);
echo $z;
//echo $z;echo mysql_error();
$sum_margin=0;
$sum_margin_pln=0;
$count_total=0;
$sum=array();
$ac=1;
while($r=$GLOBALS[db]->fetchByAssoc($w)){
$ilosc+=$r['quantity'];
$razem+=$r['razem'];
$b=$r['cid'];
$sum[$b]['total']+=$r['razem'];
$sum[$b]['quantity']+=$r['quantity'];
$sum[$b]['cat']=$r['cname'];
$sum[$b]['c']=0;
foreach($sum as $k=>$v ){
if(($k!=$r['cid'] && $sum[$k]['c']==0)){
$objPHPExcel->getActiveSheet()->SetCellValue("A".$i,'Podsumowanie '.$sum[$k]['cat']);
$objPHPExcel->getActiveSheet()->SetCellValue("B".$i,'');
$objPHPExcel->getActiveSheet()->SetCellValue("C".$i,$sum[$k]['quantity']);
$objPHPExcel->getActiveSheet()->SetCellValue("D".$i,$sum[$k]['total']);
$sum[$k]['c']=1;
$i++;
}
}
$objPHPExcel->getActiveSheet()->SetCellValue("A".$i,$r['name']);
$objPHPExcel->getActiveSheet()->SetCellValue("B".$i,$r['code']);
$objPHPExcel->getActiveSheet()->SetCellValue("C".$i,$r['quantity']);
$objPHPExcel->getActiveSheet()->SetCellValue("D".$i,$r['razem']);
$z2="select fk.id,p.name,p.code,p.ecmproduct_id, sum(
CASE WHEN fk.type!='correct'
THEN
CASE WHEN fk.currency_value is null or fk.currency_value='' or fk.currency_value=0
THEN
p.subtotal
ELSE
p.subtotal*fk.currency_value
END
ELSE
CASE WHEN p.old_subtotal IS null OR p.old_subtotal='' THEN
0
ELSE
CASE WHEN fk.currency_value is null or fk.currency_value='' or fk.currency_value=0
THEN
p.subtotal-p.old_subtotal
ELSE
(p.subtotal-p.old_subtotal)*fk.currency_value
END
END
END
) as razem,
sum(
CASE WHEN fk.type!='correct'
THEN
p.quantity
ELSE
p.quantity-p.old_quantity
END
) as quantity,p.subprice,fk.parent_id,fk.parent_name
from ecminvoiceouts as fk
inner join ecminvoiceoutitems as p on p.ecminvoiceout_id=fk.id
inner join ecmproducts as pr on pr.id=p.ecmproduct_id
inner join ecmproductcategories as cat on cat.id=pr.product_category_id
where ".$where." and p.ecmproduct_id='".$r['ecmproduct_id']."'
group by fk.parent_id order by razem desc;";
$w2=$GLOBALS[db]->query($z2);
while($r2=$GLOBALS[db]->fetchByAssoc($w2)){
$i++;
$objPHPExcel->getActiveSheet()->SetCellValue("E".$i,$r2['parent_name']);
$objPHPExcel->getActiveSheet()->SetCellValue("F".$i,$r2['quantity']);
$objPHPExcel->getActiveSheet()->SetCellValue("G".$i,$r2['razem']);
}
foreach($sum as $k=>$v ){
if(($w->num_rows==$ac && $sum[$k]['c']==0)){
$i++;
$objPHPExcel->getActiveSheet()->SetCellValue("A".$i,'Podsumowanie '.$sum[$k]['cat']);
$objPHPExcel->getActiveSheet()->SetCellValue("B".$i,'');
$objPHPExcel->getActiveSheet()->SetCellValue("C".$i,$sum[$k]['quantity']);
$objPHPExcel->getActiveSheet()->SetCellValue("D".$i,$sum[$k]['total']);
$sum[$k]['c']=1;
}
}
$i++;
$ac++;
}
$objPHPExcel->getActiveSheet()->SetCellValue("C".$i,$ilosc);
$objPHPExcel->getActiveSheet()->SetCellValue("D".$i,$razem);
$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/CategoryRaport".$microtime.".xlsx";
$objWriter->save($name);
chmod($name,0777);
header("Location: ".$name);
?>