<?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']);
		$order_name	= $conn->real_escape_string($_POST['pos_order_name']);
		
		$transdate 		= dtosql($_POST['transdate']);
		$autoid 		= $_POST['autoid'];
		$act 			= $_POST['act'];

		$pos_tableno 		= $_POST['pos_tableno'];
		$paym_type 			= $_POST['paym_type'];
		//$pos_order_type 	= $_POST['pos_order_type'];		
		$pos_order_type 	= "REGULAR";		

		
		
		$row 			= $_POST['row'];
		$coa_autoid		= $_POST['coa_autoid'];
		
		
		
		
		$subtotal 	= $conn->real_escape_string($_POST['subtotal']);
		$pos_disc 	= $conn->real_escape_string($_POST['pos_disc']);
		$pos_ongkir = $conn->real_escape_string($_POST['pos_ongkir']);
		$gtotal 	= $conn->real_escape_string($_POST['gtotal']);		
		
		$cust_autoid = $_POST['cust_autoid'];
		
		$sqld = "SELECT * FROM mst_customer WHERE cust_autoid = '$cust_autoid'";
		$dcond = $conn->query($sqld) or die($sqld);
		$rowd = $dcond->fetch_array();
		$cust_name = $conn->real_escape_string($rowd['cust_name']);

		
		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,pos_order_name,
					pos_userentry,pos_dateentry,pos_notes,pos_disc,pos_ongkir) 
					VALUES 
					('$autoid', '$transno', '$transdate', '$outlet_autoid','$subtotal', '$pos_tableno', '$pos_order_type','$gtotal','$paym_type','$order_name',
					'$userid', CURRENT_TIMESTAMP(),'$notes','$pos_disc','$pos_ongkir') ";
		//	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 soheader SET 
						so_gtotal = '$subtotal',
						cust_autoid = '$cust_autoid',
						cust_name = '$cust_name'
						WHERE so_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			
			$conn->query("DELETE FROM sodetail where so_autoid = '$autoid'");
			include('savedetail.php');			
			header('Location:index?src=2');

			}
		if($act=="post"){
			include('savepost.php');
			}
		if($act=="unpost"){
			$sql = "UPDATE soheader SET so_status = 0 WHERE so_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 soheader SET so_status = 99 WHERE so_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			header('Location:index?src=99');
			}
?>