<?php
session_start();
include 'dbc.php';

date_default_timezone_set("Asia/Karachi");
$today=date("Y-m-d");
$month = date("m",strtotime($today));
		$year = date("Y",strtotime($today));
		 $month_name = date("F", mktime(0, 0, 0, $month, 10));
		 $my=$month_name . " " . $year;
		 $t=time();
$username=$_SESSION["username"];
$userid=$_SESSION["userid"];
$invid=$_POST["invid"];
$discount=$_POST["discount"];


$size = count($_POST['sale_qty']);

$i = 0;
while ($i < $size) {
	$sale_qty= $_POST['sale_qty'][$i];
	$item_name = $_POST['item_name'][$i];
	$id = $_POST['id'][$i];
	$st = $_POST['st'][$i];
	
	
	
	  $retail_price = $_POST['retail_price'][$i];
	  
 $sql5 = "SELECT * FROM invoice_details_st where id = '$id' and  user_id='$userid'";
$result5=$conn->query($sql5);
$row5 = $result5->fetch_assoc();

   $prate=$row5['prate'] ;
	 $netamount=$sale_qty * $retail_price;
	  $gstamount=$netamount * $st / 100;
	  $gsttamount=$netamount + $gstamount;
	 $totalpamount=$sale_qty * $prate;
$totaldiff=$netamount - $totalpamount;


	
	
 	
	$sql1 = "UPDATE invoice_details_st SET srate='$retail_price',qty='$sale_qty',amount='$netamount' ,pamount='$totalpamount',diff='$totaldiff',st='$st',stamount='$gstamount',netamount='$gsttamount' WHERE id='$id'";
$conn->query($sql1);

$tnetamount = $tnetamount + $netamount;
$tgstamount = $tgstamount + $gstamount;
$tgsttamount = $tgsttamount + $gsttamount;
	
	++$i;
	
	$tdiscount=$tgsttamount * $discount / 100;
  $gnetamount=$tgsttamount - $tdiscount;
$sql2 = "UPDATE invoice_st SET netamount='$tnetamount',discount='$tdiscount',gamount='$gnetamount',dis='$discount' ,st='$st',stamount='$tgstamount',tnetamount='$tgsttamount' WHERE id='$invid'";
$conn->query($sql2);
// do the update and print out some info just to provide some visual feedback


} 
header("location:Open_Invoice_SaleTax.php");

?>
