<?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'];
		$supp_name	 	= $conn->real_escape_string($_POST['supp_name']);		
		$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_chk = "SELECT COUNT(*) as num FROM poheader WHERE po_autoid = '$autoid'";
			$dcon_chk 	= $conn->query($sql_chk) or die($sql_chk);
			$rs_chk 	= $dcon_chk->fetch_array();
			if($rs_chk['num']>=1){
				header('Location:index?src=1');				
				}
		    
			$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(),'0','$supp_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 = '$supp_name',
						po_notes = '$notes',
						po_subtotal = '$subtotal'
						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 poheader SET po_status = 99 WHERE po_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			header('Location:index?src=99');
			}
?>