<?php
session_start();
if(!isset($_SESSION['aid']))
{
	echo '<script>location.href="../index.php"</script>';
}
include "../connection/config.php";

//$flag=$_GET['flag'];
 $iid=$_GET['iid']; 
 $qty=$_GET['qty'];  
 $saleid=$_GET['saleid']; 
 $billno = $_GET['billno'];  

$totsql= mysqli_query($con,"select * from sales_reg2 where iid='$iid' AND saleid='$saleid'");
$totfet= mysqli_fetch_assoc($totsql);
$total=  $totfet['total'];
//$sb=$totfet['sales_billno'];

$Selitem="select * from stock where iid='$iid' ";
$selexe=mysqli_query($con,$Selitem);
$selfet=mysqli_fetch_assoc($selexe);
$g_stock = $selfet['good_stock'];
$o_stock = $selfet['stock_out'];

$good_stock= $g_stock + $qty;
$out_stock = $o_stock - $qty;

$sqli="update stock set good_stock='$good_stock',stock_out='$out_stock' where iid='$iid'";
$exee=mysqli_query($con,$sqli);


$exisprice_sql= mysqli_query($con,"select * from sales_reg1 where saleid='$saleid'");
$exisprice_fet = mysqli_fetch_assoc($exisprice_sql);
$exisprice = $exisprice_fet['net_amount'];
$newprice = $exisprice - $total;

$vsql=mysqli_query($con,"select * from vat");
$vfet=mysqli_fetch_assoc($vsql);
$vat=$vfet['tax'];

$vatprice = $newprice * $vat/100;
$fin_amount = $newprice + $vatprice;

$upprice="update sales_reg1 set net_amount='$newprice',vat='$vatprice',vat_amount='$fin_amount' where saleid='$saleid'";
$exeprice= mysqli_query($con,$upprice);

$sql="delete from sales_reg2 where iid='$iid' AND saleid='$saleid'";
$exe=mysqli_query($con,$sql);

// if($sb!=$billno)
// {
// $s= "delete from sales_reg1 where sales_billno='$billno'";
// $e=  mysqli_query($con,$s);
// }

echo '<script>alert("Are you sure ?")</script>';
echo '<script>location.href="billedit.php?saleid='.$saleid.'&billno='.$billno.'"</script>'; 

?>