<?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"];
$expdate=$_POST["expdate"];
$cno=$_POST["cno"];
$disc=$_POST["disc"];
$areaname=$_POST["areaname"];
$cname=$_POST["cname"];

$v_no=$cno . ":" . $t . ":" .  $month_name;

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

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

	$sql = "Insert into delivery_details (id,date,inv_no,item_no,barcode,type_name,item_name,srate,qty,amount,username,user_id,rfid)values (null,'$today','$v_no','$item_no','$cname','$areaname','$item_name','$retail_price','$sale_qty','$tamount','$username','$userid','$rfid')";
$conn->query($sql);

		}					


	++$i;

// do the update and print out some info just to provide some visual feedback


} 
 $sqlinactive = "SELECT * FROM delivery_details where  inv_no='$v_no'";
$resultinactive=$conn->query($sqlinactive);
while($rowinactive = $resultinactive->fetch_assoc())
 
  {
 $tmpurchase=$tmpurchase + $rowinactive['amount'];
 
  }
//$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 delivery (id,v_no,date,month_name,cell,username,status,user_id,netamount,cname,disc)values (null,'$v_no','$today','$my','$areaname','$username','Open','$userid','$tmpurchase','$cname','$disc')";
$conn->query($sql2);

echo "Record Update";
//echo"<td> <a href ='open_customers.php?areaname=$party_area'>Click to Main Dashboard</a>";
echo"<td> <a href ='Open_Customer.php'>Back</a>";
?>
