<?php
include('config.php');

		$transno 	= $conn->real_escape_string($_POST['transno']);
		$notes	 	= $conn->real_escape_string($_POST['notes']);
		$outlet_autoid	 	= $conn->real_escape_string($_POST['outlet_autoid']);
		$transdate 		= dtosql($_POST['transdate']);
		$autoid 		= $_POST['autoid'];
		$act 			= $_POST['act'];

		$pos_tableno 		= $_POST['pos_tableno'];
		$pos_order_type 	= $_POST['pos_order_type'];		
		$paym_amount 		= $_POST['paym_amount'];
		$paym_type 		= $_POST['paym_type'];
		
		$row 			= $_POST['row'];
		$coa_autoid		= $_POST['coa_autoid'];
		
		
		
		
		$subtotal 	= $conn->real_escape_string($_POST['subtotal']);
		$cashdisc 	= $conn->real_escape_string($_POST['cashdisc']);
		$disctype 	= $conn->real_escape_string($_POST['mst_disctype']);		
		$gtotal 	= $conn->real_escape_string($_POST['gtotal']);		
		
		if($disctype=="P"){
			$cashdisc_rp = $subtotal * $cashdisc/100;
			}
		else{$cashdisc_rp = $cashdisc;
			}
		
		if($act=="add"){
			$sql = "INSERT INTO posheader 
					(pos_autoid, pos_transno, pos_transdate, outlet_autoid,pos_gtotal, pos_tableno, pos_order_type, pos_paym_amount,pos_paym_type) 
					VALUES 
					('$autoid', '$transno', '$transdate', '$outlet_autoid','$total', '$pos_tableno', '$pos_order_type','$paym_amount','$paym_type') ";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			

			include('savedetail.php');
			header('Location:index?src=1');
		}
		if($act=="edit"){
			$sql = "UPDATE posheader SET 
						po_transdate = '$transdate',
						pos_gtotal = '$total',
						pos_paym_amount = '$paym_amount'
						WHERE po_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			
			$conn->query("DELETE FROM posdetail where po_autoid = '$autoid'");
			include('savedetail.php');			
			header('Location:index?src=2');

			}
		if($act=="post"){
			include('savepost.php');
			}
		if($act=="unpost"){
			$sql = "UPDATE posheader SET pos_status = 0 WHERE pos_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}				
			
			$sql_stok = "DELETE FROM invlog WHERE log_refautoid = '$autoid' AND log_reftype = 'SALES'";
			if (!$conn->query($sql_stok) === TRUE) { echo "Error: " . $sql_stok . "<br>" . $conn->error;return false;}				
			header('Location:index_his?src=2');
			}
		if($act=="delete"){
			$sql = "UPDATE posheader SET pos_status = 99 WHERE pos_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			header('Location:index?src=99');
			}
?>