<?php
if (isset ( $_REQUEST ['date_from'] )) {
	$date_from = date ( 'Y-m-d', strtotime ( $_REQUEST ['date_from'] ) );
} else {
	$date_from = date ( "01.m.Y" );
}
$data = array ();
if (isset ( $_REQUEST ['date_to'] )) {
	$date_to = date ( 'Y-m-d', strtotime ( $_REQUEST ['date_to'] ) );
} else {
	$date_to = date ( "t.m.Y" );
}
$fired=false;
if (isset ( $_REQUEST ['process'] )) {
	$db = $GLOBALS ['db'];
	$query = "select p.pkwiu,o.parent_name,o.parent_id,sum(case o.type when 'normal' then i.quantity when 'correct' then i.quantity_corrected end)  as sum from ecminvoiceoutitems i
inner join ecminvoiceouts o on i.ecminvoiceout_id=o.id
inner join ecmproducts p on p.id=i.ecmproduct_id
where o.deleted=0 and o.canceled!=1 and o.register_date >='" . $date_from . "' and o.register_date<='" . $date_to . "'
group by p.pkwiu,o.parent_id";
	$res = $db->query ( $query );
	
	while ( $dat = $db->fetchByAssoc ( $res ) ) {
		$data [] = $dat;
	}
	$fired=true;
}
$smarty = new Sugar_Smarty ();

$smarty->assign ( "data", $data );
$smarty->assign ( "fired", $fired );
$smarty->assign ( 'date_from', date ( 'd.m.Y', strtotime ($date_from ) ) );
$smarty->assign ( 'date_to',  date ( 'd.m.Y', strtotime ($date_to ) ) );
echo $smarty->fetch ( 'modules/EcmReports/tpls/pkwiu.tpl' );

?>