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

		$transno 	= $conn->real_escape_string($_POST['transno']);
		$notes	 	= $conn->real_escape_string($_POST['notes']);
		$terms_id	 	= $conn->real_escape_string($_POST['mst_terms']);
		$transdate 		= dtosql($_POST['transdate']);
		$autoid 		= $_POST['autoid'];
		$act 			= $_POST['act'];
		$ven_autoid	 	= $conn->real_escape_string($_POST['ven_autoid']);		
		$ven_name 		= $conn->real_escape_string($_ven_name[$ven_autoid]);
		
		$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 poheader 
					(po_autoid,po_transno,po_transdate,po_notes,ven_autoid,po_dateentry,po_terms,ven_name,
					po_subtotal,po_cashdisc,po_disctype,po_gtotal,po_cashdisc_amt,coa_autoid) 
					VALUES 
					('$autoid','$transno','$transdate','$notes','$ven_autoid',CURRENT_TIMESTAMP(),'$terms_id','$ven_name',
					'$subtotal','$cashdisc','$disctype','$gtotal' , '$cashdisc_rp','$coa_autoid')";
			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 poheader SET 
						po_transdate = '$transdate',
						ven_autoid = '$ven_autoid',
						ven_name = '$ven_name',
						po_terms = '$terms_id',
						po_notes = '$notes',
						po_subtotal = '$subtotal',
						po_disctype = '$disctype',
						po_cashdisc = '$cashdisc',
						po_cashdisc_amt = '$cashdisc_rp',
						po_gtotal = '$gtotal',
						coa_autoid = '$coa_autoid'
						WHERE po_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			
			$conn->query("DELETE FROM podetail where po_autoid = '$autoid'");
			include('savedetail.php');			
			header('Location:index?src=2');

			}
		if($act=="post"){
			include('savepost.php');
			}
		if($act=="unpost"){
			$sql = "UPDATE poheader SET po_status = 0 WHERE po_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 = 'PO'";
			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 prheader SET pr_status = 99 WHERE pr_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 = 'PO'";
			if (!$conn->query($sql_stok) === TRUE) { echo "Error: " . $sql_stok . "<br>" . $conn->error;return false;}				
			
			header('Location:index_his?src=99');
			}
?>