<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.php'; </script>";
 return;
}
include("includes/top.php");
include("opendb.php");
$sql1=mysqli_query($db,"select distinct pbillno,suplier,entrydate,purno from sparepurchase where ac_postdt IS NULL and brcode='$brcode' order by purno")or die('Led Post Err3'.mysqli_error($db));

$dt_tm=date("Y-m-d");
if(mysqli_num_rows($sql1)<=0)
{
print "<script> alert('Posting Already Done...!!!'); </script>";
return;
}
else
{
while($row=mysqli_fetch_array($sql1))
{
       $pbillno = $row[0];
       $suplier = strtoupper($row[1]);
       $entrydate = $row[2];
       $purno = $row[3];
       $re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
       $jvrow=mysqli_fetch_row($re1);
       $jvno = $jvrow[0]+1;
       $chre=mysqli_query($db,"select * from ledger where led='$suplier' and brcode='$brcode'");
       if(mysqli_num_rows($chre)<=0)
       {
          $code=substr($suplier,0,3).date(ymds);
          $code=str_replace(' ','-',$code);
          $sql="insert into ledger values('$suplier','SUNDRY CREDITORS','0','Dr','$code','$brcode','0000-00-00')";
          echo $sql."<br>";
          mysqli_query($db,$sql);
       }
       $taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurchase where pbillno='$pbillno' and brcode='$brcode' group by tax");
       while($taxdrrow=mysqli_fetch_array($taxdrres))
       {
              if ($taxdrrow[0]>0)
              {
              $ledger='Spare Purchase VAT '.$taxdrrow[tax].'%';
              $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$purno')";
              mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
              }
       }
       $taxres=mysqli_query($db,"select tax, sum(taxamt) from sparepurchase where purno='$purno' and brcode='$brcode' group by tax");
       while($taxrow=mysqli_fetch_array($taxres))
       {
              if($taxrow[1]>0)
              {
              $ledger='Input VAT '.$taxrow[tax].'%';
              $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxrow[1]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$purno')";
              mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
              }
       }
       //*************** cess
       $taxcessres=mysqli_query($db,"select sum(cessamt),cessp from sparepurchase where purno='$purno' and taxtype='VAT' and brcode='$brcode' group by purno")or die('cess err.'.mysqli_error($db));
       while($taxcessrow=mysqli_fetch_array($taxcessres))
       {
              if($taxcessrow[0]>0)
              {
              $ledger='Cess Paid '.$taxcessrow[1].'%';
              $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxcessrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$purno')";
              mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
              }
       }
       //************************
       $taxcrres=mysqli_query($db,"select sum(tot),sum(rndoff) from sparepurchase where purno='$purno' and taxtype='VAT' and brcode='$brcode'");
       while($taxcrrow=mysqli_fetch_array($taxcrres))
       {
              $msup_tot = $taxcrrow[0]+$taxcrrow[1];
              if($msup_tot>0)
              {
                  
              $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$suplier','0','$msup_tot','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$purno')";
              mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
              }
       }                   
       mysqli_query($db,"update sparepurchase set ac_postdt='$dt_tm',jvno='$jvno' where purno='$purno' and brcode='$brcode'");
}
print "<script> alert('Posting Done Successfully...!!!');</script>";
}
?>