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

		$transno 	= $conn->real_escape_string($_POST['transno']);
		$notes	 	= $conn->real_escape_string($_POST['invt_notes']);
		$transdate 	= dtosql($_POST['transdate']);
		$transtype 	= $_POST['transtype'];
		$autoid 	= $_POST['autoid'];
		$act 		= $_POST['act'];
		
		
		$sqltrans = "SELECT * FROM invtranstype WHERE itt_autoid = '$transtype'";
		$trans_con = $conn->query($sqltrans);
		$rst = $trans_con->fetch_assoc();
		$trans_code = $rst['itt_code'];
		$trans_attr = $rst['itt_code'];
		
		$row = $_POST['row'];
		
		$outlet = $_POST['outlet_autoid'];
		$fwhs 		= $outlet;
		$twhs 		= $outlet;
		
		
		if($act=="add"){
			$sql = "INSERT INTO invtrans 
					(invt_autoid,invt_transno,invt_transdate,invt_notes,
						itt_autoid,itt_code,invt_userentry,invt_dateentry,invt_fwhs,invt_twhs,outlet_id) 
					VALUES 
					('$autoid','$transno','$transdate','$notes','$transtype','$trans_code','$userid',CURRENT_TIMESTAMP()
					,'$fwhs','$twhs','$outlet')";
			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 invtrans SET 
						invt_transdate = '$transdate',
						invt_notes = '$notes',
						itt_autoid = '$transtype',
						itt_code = '$trans_code',
						outlet_id = '$outlet'
						WHERE invt_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			
			$conn->query("DELETE FROM invtransd where invt_autoid = '$autoid'");
			
			include('savedetail.php');
			
			header('Location:index?src=2');

			}
		if($act=="post"){
			include('savepost.php');
			}
		if($act=="unpost"){
			
			$sql = "UPDATE invtrans SET invt_status = 1, invt_lastuser = '$userid', invt_lastdate = CURRENT_TIMESTAMP() WHERE invt_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			
			$delete = "DELETE FROM invlog WHERE log_refautoid = '$autoid' AND log_reftype = 'INVT'";
			if (!$conn->query($delete) === TRUE) { echo "Error: " . $delete . "<br>" . $conn->error;return false;}	
			header('Location:index?src=10');

			}
		if($act=="delete"){
			$sql = "UPDATE invtrans SET invt_status = 99, invt_lastuser = '$userid', invt_lastdate = CURRENT_TIMESTAMP() WHERE invt_autoid = '$autoid'";
			if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
			header('Location:index?src=99');
			}
?>