 <?php
	include 'inc/conx.php';
	
	$invoice = $_POST['invoice'];
	//$kanta = $_POST['kanta'];
	$discount_all_percent = $_POST['discount_all_percent'];
	$discount_all_flat = $_POST['discount_all_flat'];
	$transport_expense = $_POST['transport_expense'];
	$received = $_POST['received'];
	$sales_desc = $_POST['sales_desc'];

	$update = $db->query("UPDATE expiries SET entire_discount_rs = '$discount_all_flat', entire_discount_pr = '$discount_all_percent', transport_expense = '$transport_expense', received = '$received', description = '$sales_desc' WHERE invoiceNo = '".$invoice."'");


	$q = "SELECT subtotal, entire_discount_rs, entire_discount_pr, transport_expense, received  FROM expiries WHERE invoiceNo = '".$invoice."'";
	$r = $db->query($q);
	$row = $r->fetchArray();

	$grand_total = $row['subtotal'];
	$remaining = $row['subtotal'];
	
	if ($discount_all_percent > 0) {
		$grand_total = $grand_total - ($row['subtotal'] * $row['entire_discount_pr'] / 100);
	}
	if ($discount_all_flat > 0) {
		$grand_total = $grand_total - $row['entire_discount_rs'];
	}
	if ($transport_expense > 0) {
		$grand_total = $grand_total + $row['transport_expense'];
	}
	
	$remaining = $grand_total;
	
	if ($received > 0) {
		$remaining = $grand_total - $row['received'];
	}
	
	$update = $db->query("UPDATE expiries SET grand_total = '$grand_total', remaining = '$remaining' WHERE invoiceNo = '".$invoice."'");

	$q = "SELECT grand_total, remaining FROM expiries WHERE invoiceNo = '".$invoice."'";
	$r = $db->query($q);
	$row = $r->fetchArray();
	
	//$totals = array();
	//array_push($totals, $row['SUMTOTAL']);
	echo json_encode($row);
	die();


?>