<?php
mysql_connect('127.0.0.1', 'e5crm', '5z#JaL');
mysql_query('use e5crm');
$date = '2013-01';
function getTotal($id){
	$r=mysql_fetch_array(mysql_query("select sum(price*quantity) as s from ecmstockdoccorrectitems where ecmstockdoccorrect_id='".$id."' and deleted='0'"));
	return $r['s'];
}
$z="select id,document_no,total,register_date from ecmstockdoccorrects 
where deleted='0' and status='accepted' and register_date like '".$date."%' order by register_date asc";

$w=mysql_query($z);
while($r=mysql_fetch_array($w)){
	$t.=$r['document_no'].','.$r['register_date'].','.getTotal($r['id']).',
';
}
$filename = "ks.csv";
$handle = fopen($filename, "wb");
$numbytes = fwrite($handle,$t);
$t="";
fclose($handle);
function getTotalPZ($id){
	$r=mysql_fetch_array(mysql_query("select sum(price*quantity) as s from ecmstockdocinitems where ecmstockdocin_id='".$id."' and deleted='0'"));
	return $r['s'];
}
$z="select id,document_no,total,register_date from ecmstockdocins where deleted='0' and status='accepted' and register_date like '".$date."%' order by register_date asc";
$w=mysql_query($z);
while($r=mysql_fetch_array($w)){
	$t.=$r['document_no'].','.$r['register_date'].','.getTotalPZ($r['id']).',
';
}
$filename = "pz.csv";
$handle = fopen($filename, "wb");
$numbytes = fwrite($handle,$t);
fclose($handle);
?>
