<?
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");
$purdate=$_REQUEST['supinvdt'];
$entrydate=$_REQUEST['invdt'];
$savecount=$_REQUEST['savecount'];
$partcode=$_REQUEST['partcode'];
$partname=$_REQUEST['partname'];
$qty=$_REQUEST['qty'];
$unitprice=$_REQUEST['unitprice'];

$befrtxamt=$_REQUEST['befrtxamt'];
$vat=$_REQUEST['vat'];
$taxamt=$_REQUEST['taxamt'];
$tot=$_REQUEST['tot'];
$taxtype=$_REQUEST['taxtype'];
$porderno=$_REQUEST['porderno'];
$sup=$_REQUEST['sup'];
$pbillno=$_REQUEST['pbillno'];
$handling=$_REQUEST['handling'];
$itmtyp=$_REQUEST['itmtyp'];
$grnno=$_REQUEST['grnno'];
$totact=0;
$tothand=0;

if($savecount==0)
{
$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}
$i=0;
foreach($partname as $a)
{
 $partname[$i]=$a; 
 $i++;
}
$i=0;
foreach($qty as $a)
{
 $qty[$i]=$a; 
 $i++;
}

$i=0;
foreach($unitprice as $a)
{
 $unitprice[$i]=$a; 
 $i++;
}

$i=0;
foreach($befrtxamt as $a)
{
 $befrtxamt[$i]=$a; 
 $i++;
}


$i=0;
foreach($vat as $a)
{
 $vat[$i]=$a; 
 $i++;
}

$i=0;
foreach($taxamt as $a)
{
 $taxamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($tot as $a)
{
 $tot[$i]=$a; 
 $i++;
}
$i=0;
foreach($itmtyp as $a)
{
 $itmtyp[$i]=$a; 
 $i++;
}
if($sup=='-select-')
	{
		echo "Error!! Please enter Suppliername";
		exit;
    }
    else
    {

$re=mysqli_query($db,"select max(purno) from sparepurchase where brcode='$brcode'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $purno=1;
else
 $purno=($prow[0]+1);


$re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
$jvrow=mysqli_fetch_row($re1);
if($jvrow[0]<=0)
 $jvno=1;
else
 $jvno=($jvrow[0]+1);

$flg1=0;


for($j=0;$j<$i;$j++)
{
if($qty[$j]>0)
{
$sql="insert into sparepurchase(purno,suplier,pbillno,purdate,entrydate,partcode,partname,supqty,price,aprice,hand,tax,taxamt,tot,brcode,taxtype,jvno,ac_postdt,itemtype,grno) values('$purno','$sup','$pbillno','$purdate','$entrydate','$partcode[$j]','$partname[$j]','$qty[$j]','$unitprice[$j]','$befrtxamt[$j]','$handling','$vat[$j]', '$taxamt[$j]','$tot[$j]','$brcode','$taxtype','$jvno','0000-00-00','$itmtyp[$j]','$grnno')";

mysqli_query($db,$sql)or die(mysqli_error($db));

}
$flg1++;
}
        $chre=mysqli_query($db,"select * from ledger where led='$sup' and brcode='$brcode'");
        if(mysqli_num_rows($chre)<=0)
        {
            $code=substr($sup,0,3).date(ymds);
            $code=str_replace(' ','-',$code);
            $sql="insert into ledger values('$sup','SUNDRY CREDITORS','0','Cr','$code','$brcode','0000-00-00')";
            echo $sql."<br>";
            mysqli_query($db,$sql) or die(mysqli_error($db));
        }





if($taxtype=='VAT'||$taxtype=='UNREG')
{
 $taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurchase where purno='$purno' and (taxtype='VAT' or taxtype='UNREG') and itemtype='Spares' and brcode='$brcode' group by tax");
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='Spares Purchase '.$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','$grnno')";
    
     mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
  }

$taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurchase where purno='$purno' and (taxtype='VAT' or taxtype='UNREG') and itemtype='Lubricant' and brcode='$brcode' group by tax");
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='Lubricant Purchase '.$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','$grnno')";
    
     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 (taxtype='VAT' or taxtype='UNREG') and brcode='$brcode' group by tax");
  while($taxrow=mysqli_fetch_array($taxres))
  {
    $ledger='Local Sales Tax paid '.$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','$grnno')";
    
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
  }

//*************** cess
 //echo "select sum(cessamt),cessp from sparepurchase where purno='$purno' and (taxtype='VAT' or taxtype='UNREG') and brcode='$brcode' group by purno";
 $taxcessres=mysqli_query($db,"select sum(cessamt),cessp from sparepurchase where purno='$purno' and (taxtype='VAT' or taxtype='UNREG') and brcode='$brcode' group by purno")or die('cess err.'.mysqli_error($db));
  while($taxcessrow=mysqli_fetch_array($taxcessres))
  {
    $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','$grnno')";
 
     mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  }


//************************
 $taxcrres=mysqli_query($db,"select sum(tot) from sparepurchase where purno='$purno' and (taxtype='VAT' or taxtype='UNREG')  and brcode='$brcode'");
  while($taxcrrow=mysqli_fetch_array($taxcrres))
  {
      $sumvl=$taxcrrow[0]+$handling;
      $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','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    
     mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
  }
} // end if
else
{
//********************* CST

$taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurchase where purno='$purno' and taxtype='CST' and itemtype='Spares' and brcode='$brcode' group by tax");
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='Purchase-CST '.$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','$grnno')";
    
     mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
  }


 
 $taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurchase where purno='$purno' and taxtype='CST' and itemtype='Lubricant' and brcode='$brcode' group by tax");
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='Lubricant Purchase-CST '.$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','$grnno')";
    
     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 taxtype='CST' and brcode='$brcode' group by tax");
  while($taxrow=mysqli_fetch_array($taxres))
  {
    $ledger='Central Sales Tax Paid '.$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','$grnno')";
  
     mysqli_query($db,$sql) or die('Led Post Err5'.mysqli_error($db));
  }
//************************

 $taxcrres=mysqli_query($db,"select sum(tot) from sparepurchase where purno='$purno' and taxtype='CST' and brcode='$brcode'");
  while($taxcrrow=mysqli_fetch_array($taxcrres))
  {
      $sumvl=$taxcrrow[0]+$handling;
      $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','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    
    mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  }



}
$taxcrres=mysqli_query($db,"select sum(hand) from sparepurchase where purno='$purno' and brcode='$brcode'");
if($handling>0)
{
 $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','Handling Charges','$handling','0','Handling Charges for pur: $purno,PBill:$pbillno','$brcode','Spare Pur','invno','$purno','$grnno')";
 
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}





$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");



//**********************88888 round off
$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','$brcode','Spare Pur','invno','$purno','$grnno')";

mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($dif>0)
{
 $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 adjusted pur No: $purno','$brcode','Spare Pur','invno','$purno','$grnno')";
 
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}



        if($flg1==$i) 
 		{
			//print "<script> window.open('workshop_printinv.php?inno=$inv'); </script>";
			print "Data  saved with purchase number:".$purno;
			echo "<script> document.location='item_purchase.php';</script>";
			//echo "<script> document.getElementById('savecount').value=1;document.location='i.php';</script>";
		} 
    }
}
//log_entry($brcode,$user,$REMOTE_ADDR,"spare purchase:$purno Journal - $jvno");
//print "<script> alert('Record has been Saved. Close this window'); </script>";



?>