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

$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;

$userid=$_SESSION["userid"];
$username=$_SESSION["username"];

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

$i = 0;
while ($i < $size) {
	$saleqty= $_POST['saleqty'][$i];
	
	$id = $_POST['id'][$i];
	$inv_no= $_POST['inv_no'][$i];
	$itemcode = $_POST['itemcode'][$i];
	$rfid = $_POST['barcode'][$i];
	$item_name= $_POST['item_name'][$i];
	$srate= $_POST['srate'][$i];
	$inv_no= $_POST['inv_no'][$i];
	
	$sql = "SELECT * FROM items where id = '$itemcode' and user_id = '$userid'";
$result=$conn->query($sql);
$row = $result->fetch_assoc();
$prebal=$row['bal'] ;

$netbal=$prebal + $saleqty;
$sql2 = "UPDATE items SET bal='$netbal' WHERE id='$itemcode'";
$conn->query($sql2);


	$netamount=$saleqty * $srate;
	
 	
	$sql1 = "UPDATE purchase_details SET qty='$saleqty',amount='$netamount' WHERE id='$id'";
$conn->query($sql1);

$tnetamount = $tnetamount + $netamount;

/*$sqlpur = "SELECT * FROM purchase where venid = '$venid'";
$resultpur=$conn->query($sqlpur);
$rowpur = $resultpur->fetch_assoc();
$venbal=$rowpur['bal'] ;
$netvenbal=$venbal + $tnetamount;
$sql2ven = "UPDATE vendor SET bal='$netvenbal' WHERE id='$venid'";
$conn->query($sql2ven);*/
	
	++$i;
	
	} 
$sql9 = "UPDATE purchase SET netamount='$tnetamount',status='Close' WHERE id='$inv_no'";
	$conn->query($sql9);
// do the update and print out some info just to provide some visual feedback
$sqlven = "SELECT * FROM purchase where id = '$inv_no'";
$resultven=$conn->query($sqlven);
$rowven = $resultven->fetch_assoc();
$ven_name=$rowven['ven_name'] ;
$sql11 = "Insert into ven_ledger (id,date,month_name,tr_no,ven_name,discription,acc_type,dr_amount,cr_amount,user_id) values (null,'$today','$month_name','$inv_no','$ven_name','Vednor Purchase','$username','0','$tnetamount','$userid')";
$conn->query($sql11);


header("location:Open_Purchase.php");

?>
