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

include("../opendb.php");
include("../test.php");
$y = $_REQUEST['y'];
$m = $_REQUEST['m'];
$d = $_REQUEST['d'];
$y1 = $_REQUEST['y1'];
$m1 = $_REQUEST['m1'];
$d1 = $_REQUEST['d1'];

$disc = $_REQUEST['discount'];

$purdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;
$totact=0;
$tothand=0;
$i=0;
$partcode = $_REQUEST['partcode'];
foreach($partcode as $a)
{
 	$partcode[$i]=$a; 
 	$i++;
}
$i=0;
$partname = $_REQUEST['partname'];
foreach($partname as $a)
{
 	$partname1[$i]=$a; 
 $i++;
}
$i=0;
$supqty = $_REQUEST['supqty'];
foreach($supqty as $a)
{
 $supqty1[$i]=$a; 
 $i++;
}
$i=0;
$foc = $_REQUEST['foc'];
foreach($foc as $a)
{
 $foc[$i]=$a; 
 $i++;
}
$i=0;
$price = $_REQUEST['price'];
foreach($price as $a)
{
 $price[$i]=$a; 
 $i++;
}

/*
$i=0;
foreach($disc as $a)
{
 $disc[$i]=$a; 
 $i++;
}*/
$i=0;
$aprice = $_REQUEST['aprice'];
foreach($aprice as $a)
{
 $aprice[$i]=$a; 
 $totact+=$a;
 $i++;
}

$hand = $_REQUEST['handling'];


$i=0;
$tax = $_REQUEST['tax'];
foreach($tax as $a)
{
 $tax[$i]=$a; 
 $i++;
}

$i=0;
$taxamt = $_REQUEST['taxamt'];
foreach($taxamt as $a)
{
 $taxamt[$i]=$a; 
 $i++;
}
$i=0;
$tot = $_REQUEST['tot'];
foreach($tot as $a)
{
 $tot[$i]=$a; 
 $i++;
}

$i=0;
$total = $_REQUEST['total'];
foreach($total as $a)
{
 $total[$i]=$a; 
 $i++;
}


$i=0;
$cessp = $_REQUEST['cessp'];
foreach($cessp as $a)
{
 $cessp[$i]=$a; 
 $i++;
}
$i=0;
$cessamt = $_REQUEST['cessamt'];
foreach($cessamt as $a)
{
 	$cessamt[$i]=$a; 
 	$i++;
}
$round = $_REQUEST['round'];
$round = $round;

$taxtype = $_REQUEST['taxtype'];
$re=mysqli_query($db,"select max(purno) from sparepurchase where brcode='$brcode' and taxtype='$taxtype'");
$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);


$pbillno = $_REQUEST['pbillno'];
$porderno = $_REQUEST['porderno'];
$sup = $_REQUEST['sup'];

$re44=mysqli_query($db,"select pbillno from sparepurchase where brcode='$brcode' and taxtype='$taxtype' and pbillno='$pbillno' and suplier='$sup'");
if(mysqli_num_rows($re44)>0)
{
	print "Purchase Bill.No already exist for this Supplier";
	return;
}



if($pbillno=='' )
{
	print "Enter Purchase Bill.Date";
	return;
}


print 'tot act:'.$totact.'tot hand:'.$tothand;
$handper=($totact*$tothand)/100;
$totact_per=$totact+$handper;
for($j=0;$j<$i;$j++)
{
	if($supqty1[$j]>0)
	{
		if($taxtype=='VAT')
		{
			$sql="insert into sparepurchase (purno,porderno,suplier,pbillno,purdate,entrydate,partcode,partname,supqty,mrp,price,disc,aprice,hand,tax,taxamt,tot,brcode,taxtype,jvno,cessp,cessamt) values('$purno','$porderno','$sup','$pbillno','$purdate','$entrydate','$partcode[$j]','$partname1[$j]','$supqty1[$j]','$foc[$j]','$price[$j]','$disc','$total[$j]','$hand','$tax[$j]', '$taxamt[$j]','$tot[$j]','$brcode','$taxtype','$jvno','$cessp[$j]','$cessamt[$j]')";
		}
		else
		{
			$sql="insert into sparepurchase (purno,porderno,suplier,pbillno,purdate,entrydate,partcode,partname,supqty,mrp,price,disc,aprice,hand,tax,taxamt,tot,brcode,taxtype,jvno,cessp,cessamt) values('$purno','$porderno','$sup','$pbillno','$purdate','$entrydate','$partcode[$j]','$partname1[$j]','$supqty1[$j]','$foc[$j]','$price[$j]','$disc','$total[$j]','$hand','$tax[$j]', '$taxamt[$j]','$tot[$j]','$brcode','$taxtype','$jvno','$cessp[$j]','$cessamt[$j]')";
		}
		mysqli_query($db,$sql)or die(mysqli_error($db));
		$sql="update purorder_spare set sup_qty='$supqty1[$j]', sup_purno='$purno', sup_date='$entrydate' ,trans_type='purchase' where orderno='$porderno' and partcode='$partcode[$j]' and brcode='$brcode'";
		mysqli_query($db,$sql)or die(mysqli_error($db));
	}
}
$sql="update sparepurchase set rndoff ='$round'  where porderno='$porderno' and brcode='$brcode' limit 1";
mysqli_query($db,$sql)or die(mysqli_error($db));
if($taxtype=='VAT')
{
 	$taxdrres=mysqli_query($db,"select sum(aprice),tax from sparepurchase where purno='$purno' and taxtype='VAT' and brcode='$brcode' group by tax");
  	while($taxdrrow=mysqli_fetch_array($taxdrres))
  	{
    		$ledger='Spare Purchase VAT '.$taxdrrow[tax].'%';  
    		$sql="insert into journal2 values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$porderno','0')";
     		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' and brcode='$brcode' group by tax");
  	while($taxrow=mysqli_fetch_array($taxres))
  	{
    		$ledger='Input VAT '.$taxrow[tax].'%';  
    		$sql="insert into journal2 values('$entrydate','$jvno','Dr','$ledger','$taxrow[1]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$porderno','0')";
     		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))
  {
    $ledger='Cess Paid '.$taxcessrow[1].'%';
    $sql="insert into journal2 values('$entrydate','$jvno','Dr','$ledger','$taxcessrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$porderno','0')";
     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];
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$sup','0','$msup_tot','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$porderno','0')";
     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 brcode='$brcode' group by tax");
  	while($taxdrrow=mysqli_fetch_array($taxdrres))
  	{
    		$ledger='Spare Purchase CST '.$taxdrrow[tax].'%';  
    		$sql="insert into journal2 values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$porderno','0')";
     		mysqli_query($db,$sql) or die('Led Post Err4'.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='CST Collected '.$taxrow[tax].'%';  
    $sql="insert into journal2 values('$entrydate','$jvno','Dr','$ledger','$taxrow[1]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$porderno','0')";
     mysqli_query($db,$sql) or die('Led Post Err5'.mysqli_error($db));
  }
//************************
 $taxcrres=mysqli_query($db,"select sum(tot),sum(rndoff) from sparepurchase where purno='$purno' and taxtype='CST' and brcode='$brcode'");
  while($taxcrrow=mysqli_fetch_array($taxcrres))
  {
		$msup_tot = $taxcrrow[0]+$taxcrrow[1];
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$sup','0','$msup_tot','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$porderno','0')";
     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'");



//**********************88888 round off
$jvdifrow=mysqli_fetch_row($jvdifres);
$dif=$jvdifrow[0];

if($dif<0)
{
$x=abs($dif);
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','$x', '0','Round ofSpur No: $purno','$brcode','Spare Pur','Pur Order','$purno','$purno')";
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($dif>0)
{
 $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','$dif','Round of adjusted pur No: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$purno')";
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}


}
log_entry($brcode,$user,$REMOTE_ADDR,"spare purchase:$purno Journal - $jvno");
print "<script> alert('Record has been Saved. Close this window'); </script>";
print "<script> document.location='prnpurchase.php?purno=$purno&ttype=$taxtype'; </script>";

?>
