<?php
$date = $_REQUEST['date'];
//KS
function getTotalKS($id){
	$res = ($GLOBALS['db']->query("select p.group_ks,sum(ks.price*ks.quantity) as s from ecmstockdoccorrectitems as ks inner join ecmproducts as p on ks.ecmproduct_id=p.id where ks.ecmstockdoccorrect_id='".$id."' and ks.deleted='0' group by p.group_ks"));
	$result = array();
	$result['sum'] = 0;
	while ($row = $GLOBALS['db']->fetchByAssoc($res)) {
		$result[$row['group_ks']] = $row['s'];
		$result['sum']+=$row['s'];
	}
	
	return $result;
}
$z="select ks.id,ks.document_no,ks.total,ks.register_date, ks.name as ks_name, s.name from ecmstockdoccorrects as ks inner join ecmstocks as s on ks.stock_id=s.id
where ks.deleted='0' and ks.register_date like '".$date."%' order by ks.register_date asc";

global $app_list_strings;

$w=$GLOBALS['db']->query($z);
$t='Numer,Data rejestracji,Wartość,Wartość TH,Wartość WG,Wartość PP,Magazyn,Opis
';
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	$total = getTotalKS($r['id']);
	$t.=$r['document_no'].','.$r['register_date'].','.round($total['sum'],2).',';
	if (isset($total['1'])) $t.=round($total['1'],2); else $t.='0.00'; $t.=',';
	if (isset($total['2'])) $t.=round($total['2'],2); else $t.='0.00'; $t.=',';
	if (isset($total['3'])) $t.=round($total['3'],2); else $t.='0.00'; $t.=',';
	$t.=$r['name'].','.$r['ks_name'].'
';
}
$filename = "ks.csv";
$handle = fopen($filename, "wb");
$numbytes = fwrite($handle,$t);
$t="";
fclose($handle);
//PZ
function getTotalPz($id){
	$res = ($GLOBALS['db']->query("select p.group_ks,sum(pz.price*pz.quantity) as s from ecmstockdocinitems as pz inner join ecmproducts as p on pz.ecmproduct_id=p.id where pz.ecmstockdocin_id='".$id."' and pz.deleted='0' group by p.group_ks"));
	$result = array();
	$result['sum'] = 0;
	while ($row = $GLOBALS['db']->fetchByAssoc($res)) {
		$result[$row['group_ks']] = $row['s'];
		$result['sum']+=$row['s'];
	}

	return $result;
}
$z="select pz.id,pz.document_no,pz.total,pz.register_date, pz.name as pz_name, s.name from ecmstockdocins as pz inner join ecmstocks as s on pz.stock_id=s.id
where pz.deleted='0' and pz.status='accepted' and pz.register_date like '".$date."%' order by pz.register_date asc";

global $app_list_strings;
$t='Numer,Data rejestracji,Wartość,Wartość TH,Wartość WG,Wartość PP,Magazyn,Opis
';
$w=$GLOBALS['db']->query($z);
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	$total = getTotalPz($r['id']);
	$t.=$r['document_no'].','.$r['register_date'].','.round($total['sum'],2).',';
	if (isset($total['1'])) $t.=round($total['1'],2); else $t.='0.00'; $t.=',';
	if (isset($total['2'])) $t.=round($total['2'],2); else $t.='0.00'; $t.=',';
	if (isset($total['3'])) $t.=round($total['3'],2); else $t.='0.00'; $t.=',';
	$t.=$r['name'].','.$r['pz_name'].'
';
}
$filename = "pz.csv";
$handle = fopen($filename, "wb");
$numbytes = fwrite($handle,$t);
$t="";
fclose($handle);
//MM
function getTotalMM($id){
	$res = ($GLOBALS['db']->query("select p.group_ks,sum(pz.price*pz.quantity) as s from ecmstockdocmoveitems as pz inner join ecmproducts as p on pz.ecmproduct_id=p.id where pz.ecmstockdocmove_id='".$id."' and pz.deleted='0' group by p.group_ks"));
	$result = array();
	$result['sum'] = 0;
	while ($row = $GLOBALS['db']->fetchByAssoc($res)) {
		$result[$row['group_ks']] = $row['s'];
		$result['sum']+=$row['s'];
	}
	return $result;
}
$z="select mm.id,mm.document_no,mm.total,mm.register_date, st.name as too, sf.name as fromm from ecmstockdocmoves as mm inner join ecmstocks as sf on sf.id=mm.stock_out_id inner join ecmstocks as st on st.id=mm.stock_in_id 
where mm.deleted='0' and mm.register_date like '".$date."%' order by register_date asc";
$w=$GLOBALS['db']->query($z);
$t='Numer,Data rejestracji,Wartość,Wartość TH,Wartość WG,Wartość PP,Z magazynu,Na magazyn
';
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	$total = getTotalMM($r['id']);
	$t.=$r['document_no'].','.$r['register_date'].','.round($total['sum'],2).',';
	if (isset($total['1'])) $t.=round($total['1'],2); else $t.='0.00'; $t.=',';
	if (isset($total['2'])) $t.=round($total['2'],2); else $t.='0.00'; $t.=',';
	if (isset($total['3'])) $t.=round($total['3'],2); else $t.='0.00'; $t.=',';
	$t.=$r['fromm'].','.$r['too'].'
';
}
$filename = "mm.csv";
$handle = fopen($filename, "wb");
$numbytes = fwrite($handle,$t);
$t="";
fclose($handle);
//RW
function getTotalRW($id){
	$res=$GLOBALS['db']->query("select p.group_ks, sum(rwi.price*rwi.quantity) as s from ecmstockdocinsideoutitems as rwi inner join ecmproducts as p on (rwi.ecmproduct_id=p.id and p.product_category_id!='d7f876b0-1a3d-43a1-7c9b-511ba40df3d1') where rwi.ecmstockdocinsideout_id='".$id."' and rwi.deleted='0' group by p.group_ks");
	$result = array();
	$result['sum'] = 0;
	while ($row = $GLOBALS['db']->fetchByAssoc($res)) {
		$result[$row['group_ks']] = $row['s'];
		$result['sum']+=$row['s'];
	}
	
	return $result;
}
$z="select rw.id,rw.document_no,rw.total,rw.register_date, s.name, rw.name as rw_name from ecmstockdocinsideouts as rw inner join ecmstocks as s on rw.stock_id=s.id
where rw.deleted='0' and rw.status='accepted' and rw.register_date like '".$date."%' order by rw.register_date asc";
global $app_list_strings;
$t='Numer,Data rejestracji,Wartość,Wartość TH,Wartość WG,Wartość PP,Magazyn,Opis
';
$w=$GLOBALS['db']->query($z);
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	$total = getTotalRW($r['id']);
	$t.=$r['document_no'].','.$r['register_date'].','.round($total['sum'],2).',';
	if (isset($total['1'])) $t.=round($total['1'],2); else $t.='0.00'; $t.=',';
	if (isset($total['2'])) $t.=round($total['2'],2); else $t.='0.00'; $t.=',';
	if (isset($total['3'])) $t.=round($total['3'],2); else $t.='0.00'; $t.=',';
	$t.=$r['name'].','.$r['rw_name'].'
';
}
$filename = "rw.csv";
$handle = fopen($filename, "wb");
$numbytes = fwrite($handle,$t);
$t="";
fclose($handle);
//PW
function getTotalPw($id){
	$res=$GLOBALS['db']->query("select p.group_ks, sum(pw.price*pw.quantity) as s from ecmstockdocinsideinitems as pw inner join ecmproducts as p on pw.ecmproduct_id=p.id where pw.ecmstockdocinsidein_id='".$id."' and pw.deleted='0' group by p.group_ks");

	
	$result = array();
	$result['sum'] = 0;
	while ($row = $GLOBALS['db']->fetchByAssoc($res)) {
		$result[$row['group_ks']] = $row['s'];
		$result['sum']+=$row['s'];
	}
	
	return $result;
}
$z="select pw.id,pw.document_no,pw.total,pw.register_date, s.name, pw.name as pw_name from ecmstockdocinsideins as pw inner join ecmstocks as s on pw.stock_id=s.id
where pw.deleted='0' and pw.register_date like '".$date."%' order by pw.register_date asc";

global $app_list_strings;
$t='Numer,Data rejestracji,Wartość,Wartość TH,Wartość WG,Wartość PP,Magazyn,Opis
';
$w=$GLOBALS['db']->query($z);
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	$total = getTotalPw($r['id']);
	$t.=$r['document_no'].','.$r['register_date'].','.round($total['sum'],2).',';
	if (isset($total['1'])) $t.=round($total['1'],2); else $t.='0.00'; $t.=',';
	if (isset($total['2'])) $t.=round($total['2'],2); else $t.='0.00'; $t.=',';
	if (isset($total['3'])) $t.=round($total['3'],2); else $t.='0.00'; $t.=',';
	$t.=$r['name'].','.$r['pw_name'].'
';
}
$filename = "pw.csv";
$handle = fopen($filename, "wb");
$numbytes = fwrite($handle,$t);
$t="";
fclose($handle);
?>
<h2>Wygenerowane raporty</h2></br>
<table width="30%" border="1">
	<tr>
		<td width="50%">KS</td>
		<td><a href="./ks.csv">ks.csv</a></td>
	</tr>
	<tr>
		<td width="50%">PZ</td>
		<td><a href="./pz.csv">pz.csv</a></td>
	</tr>
	<tr>
		<td width="50%">MM</td>
		<td><a href="./mm.csv">mm.csv</a></td>
	</tr>
	<tr>
		<td width="50%">RW</td>
		<td><a href="./rw.csv">rw.csv</a></td>
	</tr>
	<tr>
		<td width="50%">PW</td>
		<td><a href="./pw.csv">pw.csv</a></td>
	</tr>
</table>