<?
session_start();
$brcode = $_SESSION['brcode'];
$level  = $_SESSION['level'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="../style.css" type="text/css" />
<script src="jquery-ui.min.js"></script>

<link rel="stylesheet" href="jquery-1.10.2.min.js">
<script src="jquery-1.9.1.js"></script>
<script src="jquery-ui.js"></script>
<script src="jquery.nyroModal/js/jquery.nyroModal-ie6.js"></script>
<script src="jquery.nyroModal/js/jquery.nyroModal-ie6.min.js"></script>
<script src="jquery.nyroModal/js/jquery.nyroModal.custom.js"></script>
<script src="jquery.nyroModal/js/jquery.nyroModal.custom.min.js"></script>
<link rel="stylesheet" href="jquery.nyroModal/nyroModal.css" type="text/css" />
<style>
th{background-color:lightsteelblue;color:black}
//tr{background-color:white}
</style>
<script type="text/javascript">
$(function() {
  $('.nyroModal').nyroModal();
});
</script>
<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>
<?
$show = $_REQUEST['show'];
if($show)
{
 $d = $_REQUEST['d'];
 $m = $_REQUEST['m'];
 $y = $_REQUEST['y'];
 $d1 = $_REQUEST['d1'];
 $m1 = $_REQUEST['m1'];
 $y1 = $_REQUEST['y1'];
 $dfrom=$y.'-'.$m.'-'.$d;
 $dto=$y1.'-'.$m1.'-'.$d1;
$sql1="select distinct pbillno, purno,suplier,entrydate,porderno,taxtype,jvno from sparepurchase where entrydate>='$dfrom' and entrydate<='$dto' and brcode='$brcode' order by purno";
//$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($db));
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>Handling Charges <th>Total";
if($level==4)
    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' ";
    $re=mysqli_query($db,$sql) or die(mysqli_error($db));
    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[hand]</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' ";
        $dchkres=mysqli_query($db,$sq) or die(mysqli_error($db));
        $dchkrow=mysqli_fetch_row($dchkres);
        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<td><input type=button value='View' onClick=window.open('viewpurrepo.php?purno=$row1[purno]','mywindow','menubar=0,resizable=1,width=1150,height=950')></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,jvno 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($db));
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>Handling Charges <th>Total";
if($level==4)
 	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($db));
    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[hand]</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($db));
        $dchkrow=mysqli_fetch_row($dchkres1);
        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<td><input type=button value='View' onClick=window.open('viewpurrepo.php?puno=$row1[purno]&jvno=$row1[jvno]','mywindow','menubar=0,resizable=1,width=1150,height=950')></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 edfun1(x,y,jvno)
{
 if(confirm('Do you want to edit P.O. No.'+x+' ?'))
  window.open('edpurvat.php?purno='+x+'&ttype='+y+'&jvno='+jvno);
}
function delfun(x,y)
{
 if(confirm('Do you want to Delete P.O. No.'+x+' ?'))
  document.location='delpur.php?purno='+x+'&ttype='+y;
}
</script>


