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



$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];
	  $rfid = $_POST['rfid'][$i];
	  $retail_price = $_POST['cost_price'][$i];
	  $sql5 = "SELECT * FROM items where item_name = '$item_name' and  user_id='$userid'";
$result5=$conn->query($sql5);
$row5 = $result5->fetch_assoc();
$rfid=$row5['rfid'] ;
$pid=$row5['id'] ;
    $item_no=$row5['item_no'] ;
$type_name=$row5['type_name'] ;
   
	 $tamount=$sale_qty * $retail_price;
	 
					
if($sale_qty > 0)
{

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

		}					


	++$i;

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


} 
 $sqlinactive = "SELECT * FROM purchase_details where  inv_no='$invid'";
$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 = "UPDATE purchase SET netamount='$tmpurchase' WHERE id='$invid'";
$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_Purchase.php'>Back</a>";
?>
