<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

$db = $GLOBALS['db'];

//get out
$out_query = "
select
	sum(so.quantity) as qty,
	sum(so.quantity*price) as total,
	so.stock_id,
	p.group_ks
from
	ecmstockoperations as so
inner join
	ecmproducts as p on p.id=so.product_id
where
	so.type = 1
group by
	so.stock_id, p.group_ks;
";

$res_out = $db->query($out_query);


$in_query = "
select
	sum(so.quantity) as qty,
	sum(so.quantity*price) as total,
	so.stock_id,
	p.group_ks
from
	ecmstockoperations as so
inner join
	ecmproducts as p on p.id=so.product_id
where
	so.type = 0
group by
	so.stock_id, p.group_ks;
";

$res = $db->query($in_query);


$rd = array(); //report data

$total = 0;
$total_qty = 0;
while ($row = $db->fetchByAssoc($res)) {
	if (!$rd[$row['stock_id']])
		$rd[$row['stock_id']] = array();
	if (!$rd[$row['stock_id']][$row['group_ks']])	
		$rd[$row['stock_id']][$row['group_ks']] = array();
	$rd[$row['stock_id']][$row['group_ks']]['qty'] = $row['qty'];
	$rd[$row['stock_id']][$row['group_ks']]['total'] = $row['total'];
	$total+=$row['total'];
	$total_qty+=$row['qty'];	
}

while ($row = $db->fetchByAssoc($res_out)) {
	$rd[$row['stock_id']][$row['group_ks']]['qty'] -= $row['qty'];
	$rd[$row['stock_id']][$row['group_ks']]['total'] -= $row['total'];
	$total-=$row['total'];
	$total_qty-=$row['qty'];
}

echo '<table cellpadding="0" cellspacing="0" border="0" style=""><tr><td class="listViewThS1" style=text-align:center; "width:250px; height:48px;">Ilość całkowita</td><td class="listViewThS1" style=text-align:center; "width:250px; height:48px;">Wartość całkowita</td></tr>';

		echo '<tr>';
		echo '<td class="oddListRowS1" style="text-align:center; width:250px; border-bottom: 1px solid #cccccc;">'.$total_qty.'</td>';
		echo '<td class="oddListRowS1" style="text-align:center; width:250px; border-bottom: 1px solid #cccccc;">'.format_number($total).'</td>';
		echo '</tr>';
echo '</table>';

foreach ($rd as $stock_id=>$stock_data) {
	$s = $db->fetchByAssoc($db->query("SELECT name FROM ecmstocks WHERE id='$stock_id'"));
	echo '<br><br><h2>'.$s['name'].'</h2><br><br>';
	echo '<table cellpadding="0" cellspacing="0" border="0" style=""><tr><td class="listViewThS1" style=text-align:center; "width:250px; height:48px;">Grupa KS</td><td class="listViewThS1" style=text-align:center; "width:250px; height:48px;">Ilość</td><td class="listViewThS1" style=text-align:center; "width:250px; height:48px;">Wartość</td></tr>';
	
	$stock_total = 0;
	$stock_qty = 0;
	foreach ($stock_data as $group_ks=>$value) {
		echo '<tr>';
		echo '<td class="oddListRowS1" style="text-align:center; width:250px; border-bottom: 1px solid #cccccc;">'.$group_ks.'</td>';
		echo '<td class="oddListRowS1" style="text-align:center; width:250px; border-bottom: 1px solid #cccccc;">'.format_number($value['qty']).'</td>';
		echo '<td class="oddListRowS1" style="text-align:center; width:250px; border-bottom: 1px solid #cccccc;">'.format_number($value['total']).'</td>';
		echo '</tr>';
		
		$stock_total+=$value['total'];
		$stock_qty+=$value['qty'];
	}
	
	//total row
		echo '<tr>';
		echo '<td class="oddListRowS1" style="text-align:center; width:250px; border-bottom: 1px solid #cccccc;">Suma</td>';
		echo '<td class="oddListRowS1" style="text-align:center; width:250px; border-bottom: 1px solid #cccccc;">'.format_number($stock_qty).'</td>';
		echo '<td class="oddListRowS1" style="text-align:center; width:250px; border-bottom: 1px solid #cccccc;">'.format_number($stock_total).'</td>';
		echo '</tr>';
	echo '</table>';
}	
?>