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

include("../opendb.php");
include("../test.php");

?>
<script>
 function calcamt(x)
 {  
 var bftxele=parseFloat(document.getElementById(x).value); 

 var taxcgst='cgst'+x;
 var taxsgst='sgst'+x;
 var totele ='tot'+x;
 var tx ='tax'+x;

 if(bftxele<='')
    {
    alert('Enter Amount');
    document.getElementById(x).value=0;
    document.getElementById(x).focus();
    return false;
    }
    var tax   =document.getElementById(tx).value/2;

    var cgstamt =bftxele*tax/100;
    var sgstamt =bftxele*tax/100; 
    document.getElementById(taxcgst).value=(cgstamt).toFixed(2);
    document.getElementById(taxsgst).value=(sgstamt).toFixed(2);
    var totamt=bftxele+cgstamt+sgstamt;

    
    ///var mrp1=document.getElementById(elemrp).value/(1+((tx+txsgst+txigst)/100));
    
    //document.getElementById(ele).value=mrp1.toFixed(2);
    
    document.getElementById(totele).value=(totamt).toFixed(2);
 }
 
 
 function calcamt3(x,event)
{
   var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
   //alert(keyCode);
   var thenum = x.match(/\d+$/)[0];
   var y=thenum;
   
    var bftxele=y;
    var pbillnos='pbillno' +y;
   
   var taxcgst='cgst'+y;
 var taxsgst='sgst'+y;
 var totele ='tot'+y;
 //var tx ='tax'+y;
   var taxpr='cgstp'+y;

 
    if(bftxele<='')
    {
        alert('Enter Amount');
        document.getElementById(y).value=0;
        document.getElementById(y).focus();
        return false;
    }
    var tax = document.getElementById(taxcgst).value;
    
    
    document.getElementById(taxsgst).value=document.getElementById(taxcgst).value;
    var pbillno = document.getElementById(pbillnos).value;
    var taxigvl=document.getElementById(taxpr).value/2;
   
    //alert(taxigvl);
    document.getElementById(bftxele).value =((parseFloat(tax)/parseFloat(taxigvl)*100)).toFixed(2);
    
    var bfrtaxele=document.getElementById(bftxele).value;
    //alert(pbillno);
    var totamt=parseFloat(bfrtaxele)+(parseFloat(tax)*2);
    document.getElementById(totele).value=(totamt).toFixed(2);
    if(keyCode == 13)
    {
        //alert('hi');
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                alert(xmlhttp.responseText);
            //document.getElementById("popScroll2").innerHTML=xmlhttp.responseText;
            }
        }
        //alert(bftxele);
       // alert(pbillno);
        //alert(taxigvl);
        var url1="updpurchase3776local.php?befrtax="+bftxele+"&&pbillno="+pbillno+"&&tax="+taxigvl;
                
         alert(url1);
	   xmlhttp.open("GET","updpurchase3776local.php?befrtax="+bfrtaxele+"&&pbillno="+pbillno+"&&tax="+taxigvl,true);
	   xmlhttp.send();
	   
    }
}
 
 
 
 
function calcamt2(x,event)
{
   var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
   //alert(keyCode);
   var thenum = x.match(/\d+$/)[0];
   var y=thenum;
   //alert(thenum);
    //var bftxele=parseFloat(document.getElementById(y).value);
    var bftxele=y;
    var pbillnos='pbillno' +y;
    var taxigst='igst'+y;
    var totele ='tot'+y;
    var tx ='tax'+y;
    if(bftxele<='')
    {
        alert('Enter Amount');
        document.getElementById(y).value=0;
        document.getElementById(y).focus();
        return false;
    }
    var tax = document.getElementById(tx).value;
    //alert(tax);
    var pbillno = document.getElementById(pbillnos).value;
    //var igstamt =bftxele*tax/100;
    var taxigvl=document.getElementById(taxigst).value;
   
    //alert(taxigvl);
    //document.getElementById(bftxele).value =parseFloat(taxigvl)*parseFloat(tax)/100;
    document.getElementById(bftxele).value =(parseFloat(taxigvl)/parseFloat(tax)*100).toFixed(2);
    
    var bfrtaxele=document.getElementById(bftxele).value;
    //alert(bftxele);
   // document.getElementById(y).value=bftxele.value;
    //parseFloat(document.getElementById(y).value)
    //document.getElementById(taxigst).value=(igstamt).toFixed(2);
    //document.getElementById(taxigst).value=(igstamt).toFixed(2);
    var totamt=parseFloat(bfrtaxele)+parseFloat(taxigvl);
    document.getElementById(totele).value=(totamt).toFixed(2);
    if(keyCode == 13)
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                alert(xmlhttp.responseText);
            //document.getElementById("popScroll2").innerHTML=xmlhttp.responseText;
            }
        }
       /* alert(bftxele);
        alert(pbillno);
        alert(tax);*/
        var url="updpurchase3776.php?befrtax="+bftxele+"&&pbillno="+pbillno+"&&tax="+tax;
        //alert(url);
        xmlhttp.open("GET","updpurchase3776.php?befrtax="+bfrtaxele+"&&pbillno="+pbillno+"&&tax="+tax,true);
	    xmlhttp.send();
    }
}

</script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:lightsteelblue;color:black}
//tr{background-color:white}
</style>
<center>

<?
if(isset($_POST['show']))
{
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
?>
<table>
<TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN AUTOMOTIVE P LTD </b><BR>
	 <?print $brrow[braddr].",".$brrow[brcity].",<br><u> PH - ".$brrow[brphone]?></u>
TIN: <?print $brrow[tin]?>, CST: <?print $brrow[cstno]?>&nbsp;
</td></tr>
<tr><td COLSPAN=2 ALIGN=CENTER>
</td></tr>
</table>
<?

$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];

$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
 $dfrom=$y.'-'.$m.'-'.$d;
 $dto=$y1.'-'.$m1.'-'.$d1;
 $pstvl=$_REQUEST['pstvl'];
 
$sql1="select distinct cgstp from sparepurchase where entrydate>='$dfrom' and entrydate<='$dto' and brcode='$brcode' and (taxtype='CGST' or taxtype='UNREG') order by entrydate,pbillno";
 $re1=mysqli_query($db,$sql1)or die(mysqli_error($db));
print "<b>Purchase Report from ".datetohtml($dfrom).' to '.datetohtml($dto). "- GST";
print "<table border=0 bgcolor='steelblue' cellspacing=1 cellpadding=3>";

$i=1;
 //while($row1=mysqli_fetch_array($re1))
// {
     if($pstvl=='All')
     {
         
         //$sql="select pbillno,purno,suplier,entrydate,porderno,taxtype,purdate,sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(cgstamt) cgstamt,sum(sgstamt) sgstamt,sum(cessamt) cessamt,sum(tot) tot,taxtype,grno from sparepurchase where entrydate between '$dfrom' and '$dto' and cgstp = '$row1[0]' and brcode='$brcode' and (taxtype='CGST' or taxtype='UNREG') group by grno";
         $sql="select pbillno,purno,suplier,entrydate,porderno,taxtype,purdate,sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(cgstamt) cgstamt,sum(sgstamt) sgstamt,sum(cessamt) cessamt,sum(tot) tot,taxtype,grno,cgstp,jvno from sparepurchase where entrydate between '$dfrom' and '$dto' and brcode='$brcode' and (taxtype='CGST' or taxtype='UNREG') and porderno  in('VE1000','VE1153','VE2000') group by pbillno,cgstp order by entrydate,pbillno";
     }
     if($pstvl=='posted')
     {
         $sql="select pbillno,purno,suplier,entrydate,porderno,taxtype,purdate,sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(cgstamt) cgstamt,sum(sgstamt) sgstamt, sum(cessamt) cessamt,sum(tot) tot,taxtype,grno,cgstp,jvno from sparepurchase where entrydate between '$dfrom' and '$dto'  and brcode='$brcode' and jvno>0 and (taxtype='CGST' or taxtype='UNREG') and porderno  in('VE1000','VE1153','VE2000') group by pbillno,cgstp  order by entrydate,pbillno";
         //print $sql;
     }
     if($pstvl=='awapost')
     {
         $sql="select pbillno,purno,suplier,entrydate,porderno,taxtype,purdate,sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(cgstamt) cgstamt,sum(sgstamt) sgstamt, sum(cessamt) cessamt,sum(tot) tot,taxtype,grno,cgstp,jvno from sparepurchase where entrydate between '$dfrom' and '$dto' and brcode='$brcode' and jvno=0 and (taxtype='CGST' or taxtype='UNREG') and porderno  in('VE1000','VE1153','VE2000') group by pbillno,cgstp order by entrydate,pbillno";
     }
 
 $re=mysqli_query($db,$sql) or die(mysqli_error($db));
 $t1=0;
 $t2=0;
 $t2_1=0;
 $t3=0;
 $gtot=0;
print "<table border=0 cellspacing=1 cellpadding=4 bgcolor='lightsteelblue'>";
$tot_tax = $row1[0]*2;
print "<th colspan=10><h3>Tax : $tot_tax</th></tr>";
print "<th>PNo<th>GRNo <th>Entry Date <th>PBill No<th>P.Date<th>Supplier <th>Tax %  <th>Before Tax <th>CGST Amt<th>SGST Amt<th>TCS <th>Total <th>Taxtype";

while($row=mysqli_fetch_array($re))
{
    
    print "<tr bgcolor='white' onMouseOver=bgColor='silver'; onMouseOut=bgColor='white'>";
    print  "<td>$row[purno]</td><td>$row[grno]</td><td>".datetohtml($row[entrydate])."</td>";
   // print  "<td>$row[pbillno]</td><td>".datetohtml($row[purdate])."</td>";
print  "<td><input type=text readonly value=$row[pbillno] id=pbillno$i></td><td>".datetohtml($row[purdate])."</td>";
       
    print "<td>$row[suplier]</td>";
$taxperc=$row[cgstp]*2;
    print "<td align=right bgcolor='ivory'><input type=text value=$taxperc size=8 name=cgstp[] id='cgstp$i' readonly ></td><td align=right bgcolor='ivory'><input type=text value=$row[aprice] size=8 name=bftx[] id='$i'  readonly></td>";
    print "<td align=right><input type=text value=$row[cgstamt] size=6 id='cgst$i' onkeyup=calcamt3(this.id,event)   ></td>";
    print "<td align=right><input type=text value=$row[sgstamt] size=6 id='sgst$i' readonly></td>";
    print "<td align=right>$row[cessamt]</td><td align=right><input type=text value=$row[tot] size=8 id='tot$i' readonly></td><td align=right>$row[taxtype]</td>";
    print "<input type=hidden value='$tot_tax' id=tax$i>";
    if($row[jvno]==0)
            print "<td bgcolor='red'> A/c posting Pending</td>";
       
    print "</tr>";
    $t1+=$row[aprice];
    $t2+=$row[cgstamt];
    $t2_1+=$row[sgstamt];
    $t3+=$row[cessamt];
    $gtot+=$row[tot];
    $i++;
}
print "<tr bgcolor='white'><td colspan=6 align=right><b>Total</td>";
print "<td align=right><b>".putComa($t1)."</td>";
print "<td align=right><b>".putComa($t2)."</td>";
print "<td align=right><b>".putComa($t2_1)."</td>";
print "<td align=right><b>".putComa($t3)."</td>";
print "<td align=right><b>".putComa($gtot)."</td>";
print "<td align=right><b></td>";

//}


print "</table><br>";

//$sql1="select distinct igstp from sparepurchase where entrydate>='$dfrom' and entrydate<='$dto' and brcode='$brcode' and (taxtype='IGST')";
 //$re1=mysqli_query($db,$sql1)or die(mysqli_error($db));
print "<b>Purchase Report from ".datetohtml($dfrom).' to '.datetohtml($dto). "- IGST";
print "<table border=0 bgcolor='steelblue' cellspacing=1 cellpadding=3>";

$j=1;
//while($row1=mysqli_fetch_array($re1))
//{
    if($pstvl=='All')
    {
      $sql="select pbillno,purno,suplier,entrydate,porderno,taxtype,purdate,sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(igstamt) tax, sum(cessamt) cessamt,sum(tot) tot,taxtype,grno,jvno,igstp from sparepurchase where entrydate between '$dfrom' and '$dto' and brcode='$brcode' and (taxtype='IGST') and porderno  in('VE1000','VE1153') group by pbillno,igstp order by entrydate,pbillno";
    }
    if($pstvl=='posted')
    {
        $sql="select pbillno,purno,suplier,entrydate,porderno,taxtype,purdate,sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(igstamt) tax, sum(cessamt) cessamt,sum(tot) tot,taxtype,grno,jvno,igstp from sparepurchase where entrydate between '$dfrom' and '$dto'  and brcode='$brcode' and jvno>0 and (taxtype='IGST') and porderno  in('VE1000','VE1153')  group by pbillno,igstp order by entrydate,pbillno";
    }
    if($pstvl=='awapost')
    {
        $sql="select pbillno,purno,suplier,entrydate,porderno,taxtype,purdate,sum(supqty),sum(aprice) aprice, sum(hand) hand,sum(igstamt) tax, sum(cessamt) cessamt,sum(tot) tot,taxtype,grno,jvno,igstp from sparepurchase where entrydate between '$dfrom' and '$dto'  and brcode='$brcode' and jvno=0 and (taxtype='IGST') and porderno  in('VE1000','VE1153') group by pbillno,igstp order by entrydate,pbillno";
    }
    $re=mysqli_query($db,$sql) or die(mysqli_error($db));
    $t1=0;
    $t2=0;
    $t3=0;
    $gtot=0;
    print "<table border=0 cellspacing=1 cellpadding=4 bgcolor='lightsteelblue'>";
    
   // print "<th colspan=10><h3>Tax : $tot_tax</th></tr>";
    print "<th>PNo<th>GRNo <th>Entry Date <th>PBill No<th>P.Date<th>Supplier   <th>Tax %<th>Before Tax <th>IGST <th>Cess <th>Total <th>Taxtype";
    while($row=mysqli_fetch_array($re))
    {
        $tot_tax= $row[igstp];
        print "<tr bgcolor='white' onMouseOver=bgColor='silver'; onMouseOut=bgColor='white'>";
        print  "<td>$row[purno]</td><td>$row[grno]</td><td>".datetohtml($row[entrydate])."</td>";
        print  "<td><input type=text readonly value=$row[pbillno] id=pbillno$i></td><td>".datetohtml($row[purdate])."</td>";
        print "<td>$row[suplier]</td><td bgcolor='yellow'>$row[igstp]</td>";
        print "<td align=right bgcolor='ivory'><input type=text value=$row[aprice] size=8 name=bftx[] id='$i' readonly ></td>
        <td align=right><input type=text value=$row[tax] size=8 id='igst$i' onkeyup=calcamt2(this.id,event) align=right></td>";
        print "<td align=right>$row[cessamt]</td><td align=right><input type=text value=$row[tot] size=8 id='tot$i'></td><td align=right>$row[taxtype]</td>";
        print "<input type=hidden value='$tot_tax' id=tax$i>";
        if($row[jvno]==0)
            print "<td bgcolor='red'>A/c posting Pending</td>";
        print "</tr>";
        $t1+=$row[aprice];
        $t2+=$row[tax];
        $t3+=$row[cessamt];
        $gtot+=$row[tot];
        $i++;
    }
    print "<tr bgcolor='white'><td colspan=5 align=right><b>Total</td>";
    print "<td align=right><b>".putComa($t1)."</td>";
    print "<td align=right><b>".putComa($t2)."</td>";
    print "<td align=right><b>".putComa($t3)."</td>";
    print "<td align=right><b>".putComa($gtot)."</td>";
    print "<td align=right><b></td>";
//}

//*********************************
/*$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($db));
print "<b>Purchase Report from ".datetohtml($dfrom).' to '.datetohtml($dto). "- CST";
print "<table border=0 bgcolor='steelblue' cellspacing=1 cellpadding=3>";
print "<th>PNo <th>PBill No <th>Date <th>Supplier <th>Before Tax <th>Tax <th>Cess <th>Total ";

 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 entrydate between '$dfrom' and '$dto' and 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 "<tr bgcolor='white' onMouseOver=bgColor='silver'; onMouseOut=bgColor='white'>";
 print  "<td>$row1[purno]</td><td>$row1[pbillno]</td>";
 print "<td>".datetohtml($row1[entrydate])."</td> ";
 print "<td>$row[suplier]</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>";
print "</tr>";
 $gtot+=$row[tot];
}
}
print "<tr><td colspan=7 align=right><b>Total</td><td align=right>".putComa($gtot)."</td>";

print "</table>";*/
return;
}
?>

<form action='<?print $PHP_SELF?>' name='f1' method='post'>
<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>
<tr>
    
    <?
    
    if($user=='nisanth'||$user=='joy')
    {
        ?>
        <td>Search BY</td><td><select name='pstvl'>
   <option value='All'>All</option> 
   <option value='posted'>Posted</option> 
   <option value='awapost'>Awaiting Posting</option> 
</select></td>
<?
}
else
{
?>
<td>Search BY</td><td><select name='pstvl'>
   
   <option value='awapost'>Awaiting Posting</option> 
</select></td>
<?
}?>

</tr>
<th colspan=2><INPUT TYPE=SUBMIT VALUE='SHOW' name='show'></td></tr>
</table>
</form>
<script>

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


</script>


