<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}

include("../opendb.php");
include("../test.php");
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:lightsteelblue;color:black}
//tr{background-color:white}
</style>
<center>
<form action='<?print $PHP_SELF?>' name='f1'>
<table>
<tr><td bgcolor='steelblue' colspan=2><center><b><font color=white>PURCHASE REPORT</td></tr>
<tr><td>Date From </td><td>dd<input type=text name='d' size=2 value='<?print date("d");?>'>
			mm<input type=text name='m' size=2 value='<?print date("m");?>'>
			yy<input type=text name='y' size=4 value='<?print date("Y");?>'>
</td></tr>
<tr><td>Date To </td><td>dd<input type=text name='d1' size=2 value='<?print date("d");?>'>
			mm<input type=text name='m1' size=2 value='<?print date("m");?>'>
			yy<input type=text name='y1' size=4 value='<?print date("Y");?>'>
</td></tr>
<th colspan=2><INPUT TYPE=SUBMIT VALUE='SHOW' name='show'></td></tr>
</table>
</form>
<?
if($show)
{
 $dfrom=$y.'-'.$m.'-'.$d;
 $dto=$y1.'-'.$m1.'-'.$d1;
$sql1="select distinct pbillno, purno,suplier,entrydate,porderno,taxtype from sparepurchase where entrydate>='$dfrom' and entrydate<='$dto' and brcode='$brcode' and taxtype='vat' order by purno";
 $re1=mysqli_query($db,$sql1)or die(mysqli_error());
print "<b>Purchase Report from ".datetohtml($dfrom).' to '.datetohtml($dto). "- VAT";
print "<table border=0 bgcolor='steelblue' cellspacing=1 cellpadding=3>";
print "<th>PO No <th>PNo <th>PBill No <th>Supplier <th>Entry Date  <th>Before Tax <th>Tax <th>Cess <th>Total";
if($level==5)
 print "<th colspan=2>Action";

 while($row1=mysqli_fetch_array($re1))
 {
 $sql="select sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(taxamt) tax, sum(cessamt) cessamt,sum(tot) tot from sparepurchase where pbillno='$row1[pbillno]' and purno='$row1[purno]' and brcode='$brcode' and taxtype='vat' ";

 $re=mysqli_query($db,$sql) or die(mysqli_error());

while($row=mysqli_fetch_array($re))
{
 print "\n<tr bgcolor='white' onMouseOver=bgColor='silver'; onMouseOut=bgColor='white'>";
 print  "<td>$row1[porderno]</td><td>$row1[purno]</td><td>$row1[pbillno]</td><td>$row1[suplier]</td>";
 print "<td>".datetohtml($row1[entrydate])."</td> ";
 print "<td align=right bgcolor='ivory'>$row[aprice]</td><td align=right>$row[tax]</td>";
 print "<td align=right>$row[cessamt]</td><td align=right>$row[tot]</td>";

$sq="select to_days(curdate())-to_days(entrydate) from sparepurchase where pbillno='$row1[pbillno]' and purno='$row1[purno]' and brcode='$brcode' and taxtype='vat'";
 $dchkres=mysqli_query($db,$sq) or die(mysqli_error());
 $dchkrow=mysqli_fetch_row($dchkres);
 if($level==5)
 {
 print "\n<td><input type=button value='Edit' onClick=edfun('$row1[purno]','$row1[taxtype]')></td>";
 print "\n<td><input type=button value='Del' onClick=delfun('$row1[purno]','$row1[taxtype]')></td>";
}
 print "\n</tr>";
 $gtot+=$row[tot];
}
}
print "<tr><td colspan=8 align=right><b>Total</td><td align=right>".putComa($gtot)."</td>";

print "</table><br> <hr>";
//*********************************
$sql1="select distinct pbillno, purno,suplier,entrydate,porderno,taxtype from sparepurchase where entrydate>='$dfrom' and entrydate<='$dto' and brcode='$brcode' and taxtype='cst' order by purno";
 $re1=mysqli_query($db,$sql1)or die(mysqli_error());
print "<b>Purchase Report from ".datetohtml($dfrom).' to '.datetohtml($dto). "- CST";
print "<table border=0 bgcolor='steelblue' cellspacing=1 cellpadding=3>";
print "<th>PO No <th>PNo <th>PBill No <th>Supplier <th>Entry Date <th>Before Tax <th>Tax <th>Cess <th>Total";
if($level==5)
 print"<th colspan=2>Action";

 while($row1=mysqli_fetch_array($re1))
 {
 $sql="select sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(taxamt) tax, sum(cessamt) cessamt,sum(tot) tot from sparepurchase where pbillno='$row1[pbillno]' and brcode='$brcode' and taxtype='cst' ";
 $re=mysqli_query($db,$sql) or die(mysqli_error());

while($row=mysqli_fetch_array($re))
{
 print "\n<tr bgcolor='white' onMouseOver=bgColor='silver'; onMouseOut=bgColor='white'>";
 print  "<td>$row1[porderno]</td><td>$row1[purno]</td><td>$row1[pbillno]</td><td>$row1[suplier]</td>";
 print "<td>".datetohtml($row1[entrydate])."</td> ";
 print "<td align=right bgcolor='ivory'>$row[aprice]</td><td align=right>$row[tax]</td>";
 print "<td align=right>$row[cessamt]</td><td align=right>$row[tot]</td>";
$sq="select to_days(curdate())-to_days(entrydate) from sparepurchase where pbillno='$row1[pbillno]' and purno='$row1[purno]' and brcode='$brcode' and taxtype='cst'";
 $dchkres1=mysqli_query($db,$sq) or die(mysqli_error());
 $dchkrow=mysqli_fetch_row($dchkres1);
 if($level==5)
 {
 print "\n<td><input type=button value='Edit' onClick=edfun('$row1[purno]','$row1[taxtype]')></td>";
 print "<td><input type=button value='Del' onClick=delfun('$row1[purno]','$row1[taxtype]')></td>";
 print "\n</tr>";
 }
 $gtot+=$row[tot];
}
}
print "<tr><td colspan=8 align=right><b>Total</td><td align=right>".putComa($gtot)."</td>";

print "</table>";

}
?>
<script>
function edfun(x,y)
{
 if(confirm('Do you want to edit P.O. No.'+x+' ?'))
  document.location='edpur.php?purno='+x+'&ttype='+y;
}

function delfun(x,y)
{
 if(confirm('Do you want to Delete P.O. No.'+x+' ?'))
  document.location='delpur.php?purno='+x+'&ttype='+y;
}
</script>


