<?
session_start();

$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];

include_once('../opendb.php');

if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
//print"<script>alert('hii');</script>";

// $befrtax=$_REQUEST['befrtax'];
 $pbillno=$_REQUEST['pbillno'];
 //$tax=$_REQUEST['tax'];
// $tax=$tax*2;
// $taxamt = $befrtax*$tax/100;
 //$tot = $befrtax + ($taxamt*2);

$grnno=$_REQUEST['grn'];
  $grndate=$_REQUEST['grdate'];
 $entrydate=$grndate;
 
 $sql12=mysqli_query($db,"select jvno from sparepurchasevecv where pbillno LIKE '%$pbillno%'  and brcode = '$brcode' ");
$row1=mysqli_fetch_array($sql12);
if($row1[0]>0)
{
    print  "Already Posted";
    return;
}
$mjvno=mysqli_query($db,"select max(jvno) from journal2 where brcode = '$brcode'");
    $mjvno_row=mysqli_fetch_array($mjvno);
    $jvno=$mjvno_row[0]+1;

   $sq = "update sparepurchasevecv set grno='$grnno', entrydate='$entrydate',jvno='$jvno'  where pbillno = '$pbillno' and brcode = '$brcode' ";
//print $sq;
mysqli_query($db,$sq);
$taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurchasevecv where pbillno like '%$pbillno%'  and brcode='$brcode' ");
while($taxdrrow=mysqli_fetch_array($taxdrres))
{
    //$ledger='Spares Purchase IGST '.$taxdrrow[igstp].'%'; 
    $ledger='Inward Supply - Purchase';
    print $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur vecv','Pur Order','$purno','$grnno','$taxdrrow[tax]')";
    mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
     print $tot123+=$taxdrrow[0];
}



$taxres=mysqli_query($db,"select tax, sum(taxamt) from sparepurchasevecv where pbillno LIKE '%$pbillno%'  and brcode='$brcode' ");
while($taxrow=mysqli_fetch_array($taxres))
{
   // $taxp=$taxrow[0]+$taxrow[0];
    $tax=$taxrow[1];
    //$tot123+=$tax;
    
    $ledger='Input Tax';
    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr) values('$entrydate','$jvno','Dr','$ledger','$tax','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur vecv','Pur Order','$purno','$grnno','$taxrow[tax]')";
    mysqli_query($db,$sql) or die('Led Post Err5'.mysqli_error($db));
}
//************************

$taxcrres=mysqli_query($db,"select sum(tot),suplier from sparepurchasevecv where pbillno LIKE '%$pbillno%'  and brcode='$brcode'");
while($taxcrrow=mysqli_fetch_array($taxcrres))
{
        $tot123=round($tot123);
    $sumvl=$taxcrrow[0];
    $sumvl=round($sumvl);
     $sup=$taxcrrow[1];
    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$sup','0','$tot123','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur vecv','Pur Order','$purno','$grnno')";
    mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
}
$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jvdifrow=mysqli_fetch_row($jvdifres);
$dif=$jvdifrow[0];
if($dif<0)
{
    $x=abs($dif);
	$sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','Round Off','$x', '0','Round ofSpur No: $purno grn:$grnno dt: $purdate','$brcode','Spare Pur vecv','invno','$purno','$grnno')";
	mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($dif>0 && $dif<1)
{
	$sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','Round Off','0','$dif','Round of  pur No: $purno,PBill:$pbillno grn:$grnno dt: $purdate','$brcode','Spare Pur vecv','invno','$purno','$grnno')";
	mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}	
echo "Updated Successfully ..";
return;
