<?php
	include("config.php");
	 $id = $_REQUEST['id'];
	 $bid = $_REQUEST['bid'];
	 $qty = $_REQUEST['qty'];
	 $total = $_REQUEST['total'];
	 $code = $_REQUEST['code'];
	 $batch = $_REQUEST['batch'];
	 $sql1="select * from tbl_billing_items where id='$id' AND code='$code' and del_status != 1";
	 $result1=mysqli_query($db,$sql1);
	 $row=mysqli_fetch_array($result1);
	//  $amount=$row['amount'];
	 
	 	$val =  explode("-",$row['purchaseid']);
	 	if($val[1] > $qty){ $avl = $val[1]-$qty; } else { $avl = $val[1]-$qty; }
		$q = "UPDATE tbl_purchaseitems SET aval = aval + $avl WHERE id = $val[0] and batchno ='$batch' ";
		mysqli_query($db,$q);
		$purchaseid = $val[0] .'-'. $qty;
	 
	 $sql="update tbl_billing_items set qty='$qty',amount='$total',purchaseid='$purchaseid' where id='$id' AND code='$code' and batchno ='$batch'  and del_status != 1";
	mysqli_query($db,$sql);
	$qry1 =mysqli_fetch_array(mysqli_query($db,"select sum(amount) as amount from tbl_billing_items where bid= '$bid' and prod_cm_stat = 'CM' ")); 
		$cm_amt = $qry1['amount'];
		$qry2 =mysqli_fetch_array(mysqli_query($db,"select sum(amount) as amount from tbl_billing_items where bid= '$bid' and prod_cm_stat = 'NCM' ")); 
		$ncm_amt = $qry2['amount'];
	$qry3 =mysqli_fetch_array(mysqli_query($db,"select sum(amount) as amount from tbl_billing_items where bid= '$bid' ")); 
		$amt = $qry3['amount'];
	//echo $amount;
	$final=$amt;
	echo $final."~".$cm_amt."~".$ncm_amt."~".$purchaseid."~".$avl."~".$qty."~".$row['purchaseid'];
	?>
	