<?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"];
$remarks1=$_POST["remarks1"];

$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];
	
	
	$remarks = $_POST['remarks'][$i];
	  $retail_price = $_POST['retail_price'][$i];
	    $st = $_POST['st'][$i];
	  
 $sql5 = "SELECT * FROM invoice_details where id = '$id' and  user_id='$userid'";
$result5=$conn->query($sql5);
$row5 = $result5->fetch_assoc();

   $prate=$row5['prate'] ;
	 $netamount=$sale_qty * $retail_price;
	 $damount=$netamount * $st / 100;
						 $totalpamount=$sale_qty * $prate;
						$gamount=$netamount - $damount;
	
$totaldiff=$netamount - $totalpamount;


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

//$tmpurchase = $tmpurchase + $netamount;
	
	++$i;
	
// do the update and print out some info just to provide some visual feedback


} 
$sqlinactive = "SELECT * FROM invoice_details where  inv_no='$invid'";
$resultinactive=$conn->query($sqlinactive);
while($rowinactive = $resultinactive->fetch_assoc())
 
  {
						 $tmpurchase=$tmpurchase + $rowinactive['amount'];
						 $tdamount=$tdamount + $rowinactive['stamount'];
						 $tnetamount=$tnetamount + $rowinactive['netamount'];
 
  }
  							$tdiscount=$tnetamount * $discount / 100;
  							$totalnetamount=$tnetamount - $tdiscount;
//$sql11 = "Insert into expense_voucher (id,date,tr_no,acc_name_dr,discription,amount,user_id,v_type) values (null,'$today','$v_no','$acc_name_dr','$disc','$ledger_amount','$userid','$v_type')";
//mysql_query($sql11) or die("Error : " . mysql_error());

/*$sql2 = "Insert into invoice (id,v_no,date,month_name,cell,username,status,user_id,netamount,cname,disc,stamount)values (null,'$v_no','$today','$my','$areaname','$username','Open','$userid','$tmpurchase','$cname','$disc','$tmpurchase')";
$conn->query($sql2);*/
$sql2 = "UPDATE invoice SET netamount='$tmpurchase',discount='$tdiscount',gamount='$tnetamount',cashrec='$totalnetamount',dis='$discount',st='$st',stamount='$tdamount',remarks='$remarks1' WHERE id='$invid'";
$conn->query($sql2);
header("location:Open_Invoice.php");

?>
