<?php 

global $mod_strings;
if ($_REQUEST ['date_from'] == "") {
	$_REQUEST ['date_from'] = date ( "d.m.Y" );
	$date_from = $_REQUEST ['date_from'];
} else {
	$date_from = $_REQUEST ['date_from'];
}

if ($_REQUEST ['date_to'] == "") {
	$_REQUEST ['date_to'] = date ( "t.m.Y" );
	$date_to = $_REQUEST ['date_to'];
} else {
	$date_to = $_REQUEST ['date_to'];
}

if(isset($_REQUEST['submit'])){
	$db=$GLOBALS['db'];
	$query="
SELECT 
    d.id,
    d.document_no,
    d.register_date,
    d.parent_id,
    d.currency_id,
    IF(d.currency_id != 'PLN',
        d.total_netto * d.currency_value_nbp,
        d.total_netto) AS total_netto,
    d.parent_name,
    IF(d.type = 'normal',
        'Normalny',
        'Korekta') AS type,
    IF(d.currency_id != 'PLN',
        d.total_vat * d.currency_value_nbp,
        d.total_vat) AS total_vat,
    IF(d.currency_id != 'PLN',
        d.total_brutto * d.currency_value_nbp,
        d.total_brutto) AS total_brutto,
    SUM(IF(p.group_ks = 1,
        IF(d.currency_id != 'PLN',
            if(d.type='normal',i.total_netto,i.total_netto_corrected) * d.currency_value_nbp,
            if(d.type='normal',i.total_netto,i.total_netto_corrected)),
        0)) AS 'TH',
    SUM(IF(p.group_ks = 2, IF(d.currency_id != 'PLN',
            if(d.type='normal',i.total_netto,i.total_netto_corrected) * d.currency_value_nbp,
            if(d.type='normal',i.total_netto,i.total_netto_corrected)), 0)) AS 'WG',
    SUM(IF(p.group_ks = 3, IF(d.currency_id != 'PLN',
            if(d.type='normal',i.total_netto,i.total_netto_corrected) * d.currency_value_nbp,
            if(d.type='normal',i.total_netto,i.total_netto_corrected)), 0)) AS 'S',
    SUM(IF(p.group_ks = 4, IF(d.currency_id != 'PLN',
            if(d.type='normal',i.total_netto,i.total_netto_corrected) * d.currency_value_nbp,
            if(d.type='normal',i.total_netto,i.total_netto_corrected)), 0)) AS 'T',
            
             (SUM(IF(p.group_ks = 1,
        IF(d.currency_id != 'PLN',
            if(d.type='normal',i.total_netto,i.total_netto_corrected) * d.currency_value_nbp,
            if(d.type='normal',i.total_netto,i.total_netto_corrected)),
        0)) +
    SUM(IF(p.group_ks = 2, IF(d.currency_id != 'PLN',
            if(d.type='normal',i.total_netto,i.total_netto_corrected) * d.currency_value_nbp,
            if(d.type='normal',i.total_netto,i.total_netto_corrected)), 0))+
    SUM(IF(p.group_ks = 3, IF(d.currency_id != 'PLN',
            if(d.type='normal',i.total_netto,i.total_netto_corrected) * d.currency_value_nbp,
            if(d.type='normal',i.total_netto,i.total_netto_corrected)), 0)) +
    SUM(IF(p.group_ks = 4, IF(d.currency_id != 'PLN',
            if(d.type='normal',i.total_netto,i.total_netto_corrected) * d.currency_value_nbp,
            if(d.type='normal',i.total_netto,i.total_netto_corrected)), 0)) ) as razem       
FROM
    ecminvoiceoutitems i
        INNER JOIN
    ecminvoiceouts d ON d.id = i.ecminvoiceout_id
        INNER JOIN
    ecmproducts p ON p.id = i.ecmproduct_id
WHERE
    d.register_date >= '".date("Y-m-d",strtotime($date_from))."'
        AND d.register_date <= '".date("Y-m-d",strtotime($date_to))."'
        AND d.deleted = 0
        AND d.canceled = 0
GROUP BY d.id

";
$totals=[];
	$res=$db->query($query);
	$data=[];
	while($dane=$db->fetchByAssoc($res)){
		$data[]=$dane;
		$totals['total_netto']=$totals['total_netto']+$dane['total_netto'];
		$totals['total_vat']=$totals['total_vat']+$dane['total_vat'];
		$totals['total_brutto']=$totals['total_brutto']+$dane['total_brutto'];
		$totals['TH']=$totals['TH']+$dane['TH'];
		$totals['WG']=$totals['WG']+$dane['WG'];
		$totals['S']=$totals['S']+$dane['S'];
		$totals['T']=$totals['T']+$dane['T'];
	}
	
}

$smarty = new Sugar_Smarty ();

$smarty->assign ( "date_from", $date_from);
$smarty->assign ( "date_to", $date_to);
$smarty->assign ( "totals", $totals);
$smarty->assign ( "DATA", $data );
$smarty->assign ( "dateFormat", "%d.%m.%Y" );

if($_REQUEST['to_pdf']==1){
	include_once ("include/MPDF57/mpdf.php");
	$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 5, 5, 5, 5 );
	$content= $smarty->fetch ( 'modules/EcmReports/tpls/PDF/ReportSalesByProdutGroups.html');
	
	$p->WriteHTML ( $content );
	//echo $content;
	// draw PDF
	
	$p->Output ();

} else {
	echo $smarty->display ( 'modules/EcmReports/tpls/ReportSalesByProdutGroups.html' );
}


?>