 <?php
	include 'inc/conx.php';
	
	$invoice = $_POST['invoice'];
	
	//$discount_all_flat = $_POST['discount_all_flat'];
	//$discount_all_percent = $_POST['discount_all_percent'];
	//$discount_all_flat = $_POST['discount_all_flat'];
	$transport_expense = $_POST['transport_expense'];
	$purchase_expense1 = $_POST['purchase_expense1'];
	$purchase_expense2 = $_POST['purchase_expense2'];
	$purchase_expense3 = $_POST['purchase_expense3'];
	$purchase_expense4 = $_POST['purchase_expense4'];
	$purchase_expense5 = $_POST['purchase_expense5'];
	$purchase_expense6 = $_POST['purchase_expense6'];
	$purchase_expense7 = $_POST['purchase_expense7'];
	$purchase_expense8 = $_POST['purchase_expense8'];
	$purchase_expense9 = $_POST['purchase_expense9'];
	$purchase_expense10 = $_POST['purchase_expense10'];
	$received = $_POST['received'];
	$sales_desc = $_POST['sales_desc'];

	$update = $db->query("UPDATE customerinvoice SET transport_expense = '$transport_expense', received = '$received', description = '$sales_desc',  purchase_expense1 = '$purchase_expense1',  purchase_expense2 = '$purchase_expense2',  purchase_expense3 = '$purchase_expense3',  purchase_expense4 = '$purchase_expense4',  purchase_expense5 = '$purchase_expense5',  purchase_expense6 = '$purchase_expense6',  purchase_expense7 = '$purchase_expense7',  purchase_expense8 = '$purchase_expense8',  purchase_expense9 = '$purchase_expense9',  purchase_expense10 = '$purchase_expense10'  WHERE invoiceNo = '".$invoice."'");

	$q = "SELECT subtotal,transport_expense, received, purchase_expense1, purchase_expense2, purchase_expense3, purchase_expense4, purchase_expense5, purchase_expense6, purchase_expense7, purchase_expense8, purchase_expense9, purchase_expense10 FROM customerinvoice WHERE invoiceNo = '".$invoice."'";
	$r = $db->query($q);
	$row = $r->fetchArray();

	$grand_total = $row['subtotal'];
	$remaining = $row['subtotal'];
	
	
	if ($transport_expense > 0) {
		$grand_total = $grand_total - $row['transport_expense'];
	}
	if ($row['purchase_expense1'] > 0 || $row['purchase_expense1'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense1'];
	}
	if ($row['purchase_expense2'] > 0 || $row['purchase_expense2'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense2'];
	}
	if ($row['purchase_expense3'] > 0 || $row['purchase_expense3'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense3'];
	}
	if ($row['purchase_expense4'] > 0 || $row['purchase_expense4'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense4'];
	}
	if ($row['purchase_expense5'] > 0 || $row['purchase_expense5'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense5'];
	}
	if ($row['purchase_expense6'] > 0 || $row['purchase_expense6'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense6'];
	}
	if ($row['purchase_expense7'] > 0 || $row['purchase_expense7'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense7'];
	}
	if ($row['purchase_expense8'] > 0 || $row['purchase_expense8'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense8'];
	}
	if ($row['purchase_expense9'] > 0 || $row['purchase_expense9'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense9'];
	}
	if ($row['purchase_expense10'] > 0 || $row['purchase_expense10'] < 0) {
		$grand_total = $grand_total + $row['purchase_expense10'];
	}
	
	$remaining = $grand_total;
	
	if ($received > 0) {
		$remaining = $grand_total - $row['received'];
	}
	
	$update = $db->query("UPDATE customerinvoice SET grand_total = '$grand_total', remaining = '$remaining' WHERE invoiceNo = '".$invoice."'");

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


?>