<?
session_start();
include_once('../opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}

$pdatebr=mysqli_query($db,"select vecvpur from branch where brcode='$brcode' ") or die ('err'.mysqli_error($db));
$pdatebr1=mysqli_fetch_row($pdatebr);

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

 $sql1=("select sum(aprice) from sparepurchase where pbillno LIKE '%$pbillno%'  and brcode = '$brcode' and igstp = '$tax' order by aprice desc limit 1");



$sql1=mysqli_query($db,"select sum(aprice) from sparepurchase where pbillno LIKE '%$pbillno%'  and brcode = '$brcode' and igstp = '$tax' order by aprice desc limit 1");

$row1=mysqli_fetch_array($sql1);
$mdif = $row1[0]-$befrtax;

$max_part1=mysqli_query($db,"select partcode,aprice,jvno,purno,purdate,entrydate,grno,suplier from sparepurchase where pbillno LIKE '%$pbillno%' and brcode = '$brcode' and igstp = '$tax' order by aprice desc limit 1");
$max_row1=mysqli_fetch_array($max_part1);
$partcode=$max_row1[0];
$price_now = $max_row1[1];
$befrtax1 = $price_now-$mdif;
$taxamt1 = $befrtax1*$tax/100;
$tot1 = $befrtax1 + $taxamt1;
$jvno  =  $max_row1[2];

$purno =  $max_row1[3];
$purdate  = $max_row1[4];
$entrydate  = $max_row1[5];
$grnno  = $max_row1[6];

$sup    = $max_row1[7];

$sq="select to_days(curdate())-to_days('$entrydate')";
	$dtre=mysqli_query($db,$sq)or die(mysqli_error($db));
	$dtrow=mysqli_fetch_row($dtre);
	print $dtrow[0];
	if($dtrow[0]>$pdatebr1[0] || $dtrow[0]<0)
	{
 		print "<script> alert('Invalid entry Date, Entry possible for last 7 days only'); history.back(1); </script>";
 		return;
	}
	
if($jvno==0)
{
    $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 sparepurchase set aprice=$befrtax1,igstamt=$taxamt1,price=$befrtax1/supqty,tot=$tot1,jvno=$jvno where pbillno LIKE '%$pbillno%' and brcode = '$brcode' and igstp = $tax and partcode = '$partcode' and aprice = $price_now  and jvno = $jvno limit 1";
    mysqli_query($db,"update sparepurchase set jvno=$jvno where pbillno LIKE '%$pbillno%' and brcode = '$brcode' and jvno = 0");
}
else
    $sq = "update sparepurchase set aprice=$befrtax1,igstamt=$taxamt1,price=$befrtax1/supqty,tot=$tot1 where pbillno LIKE '%$pbillno%' and brcode = '$brcode' and igstp = $tax and partcode = '$partcode'  and aprice =$price_now and jvno = '$jvno' limit 1";
//print $sq;
mysqli_query($db,$sq);
$sq     = "delete from journal2 where brcode = '$brcode' and jvno = '$jvno' and j_type = 'Spare Pur Eich' limit 10";
mysqli_query($db,$sq);

$taxdrres=mysqli_query($db,"select sum(aprice),igstp from sparepurchase where pbillno LIKE '%$pbillno%'  and taxtype='IGST' and itemtype='Spares' and brcode='$brcode' group by igstp");

 $t=("select sum(aprice),igstp from sparepurchase where pbillno LIKE '%$pbillno%' and taxtype='IGST' and itemtype='Spares' and brcode='$brcode' group by igstp");


while($taxdrrow=mysqli_fetch_array($taxdrres))
{
    //$ledger='Spares Purchase IGST '.$taxdrrow[igstp].'%';  
   $ledger='Inward Supply - Purchase';
    $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 ,grn:$grnno','$brcode','Spare Pur Eich','Pur Order','$purno','$grnno','$taxdrrow[igstp]')";
    mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
}

$taxdrres=mysqli_query($db,"select sum(aprice),igstp from sparepurchase where pbillno LIKE '%$pbillno%' and taxtype='IGST' and itemtype='Lubricant' and brcode='$brcode' group by igstp");
while($taxdrrow=mysqli_fetch_array($taxdrres))
{
   // $ledger='Lubricant Purchase CGST '.$taxdrrow[igstp].'%'; 
     $ledger='Inward Supply - Purchase';
    $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,grn:$grnno','$brcode','Spare Pur Eich','Pur Order','$purno','$grnno','$taxdrrow[igstp]')";
    mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
}
$taxres=mysqli_query($db,"select igstp, sum(igstamt) from sparepurchase where pbillno LIKE '%$pbillno%' and taxtype='IGST' and brcode='$brcode' group by igstp");
while($taxrow=mysqli_fetch_array($taxres))
{
    //$ledger='IGST Collected '.$taxrow[igstp].'%';  
    $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','$taxrow[1]','0','Spare purno: $purno,PBill:$pbillno,grn:$grnno','$brcode','Spare Pur Eich','Pur Order','$purno','$grnno','$taxrow[igstp]')";
    mysqli_query($db,$sql) or die('Led Post Err5'.mysqli_error($db));
}
//************************
$taxcrres=mysqli_query($db,"select sum(tot) from sparepurchase where pbillno LIKE '%$pbillno%' and taxtype='IGST' and brcode='$brcode'");
while($taxcrrow=mysqli_fetch_array($taxcrres))
{
    $sumvl=$taxcrrow[0];
    $sumvl=round($sumvl);
    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$sup','0','$sumvl','Spare purno: $purno,PBill:$pbillno,grn:$grnno','$brcode','Spare Pur Eich','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 Eich','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 Eich','invno','$purno','$grnno')";
	mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}	
echo "Updated Successfully I..";
return;
