<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";	
	
	$po_no			=	base64_decode(base64_decode(base64_decode($_POST['po_no'])));
	$piece_trc		=	base64_decode(base64_decode(base64_decode(base64_decode($_POST['piece_trc']))));
	if($piece_trc    == 'Load')
	{
		$po_dd		=	$_POST['lod_dt'];
		$po_dd		= 	str_replace("/", "-",$po_dd);
		$lp = 1;
		foreach($_POST['category'] as $cat_id)
		{
			$spl_id=explode(",",$cat_id);
			
			$catego_id	=	$spl_id[0];
			$design_id	=	$spl_id[1];		 
			$po_ent_id	=	$spl_id[2];
			
			$INSRT_pof	= "INSERT INTO piece_loading(`loading_type`, `po_entry_id`, `po_entry_det_id`, `master_cat_id`, `design_det_id`,`loading_dt`, `post_dt`)VALUES('PO', '$po_no', '$po_ent_id', '$catego_id', '$design_id', '$po_dd', '$cur_time')";	
			$QURY_pof	= mysqli_query($connect,$INSRT_pof);
			
			$SLCT_MAX = "SELECT MAX(piece_loading_id) AS piece_loading_id FROM piece_loading";
			$QURY_MAX = mysqli_query($connect,$SLCT_MAX);
			$FTCH_MAX = mysqli_fetch_object($QURY_MAX);
			
			$INSRT_HIS	= "INSERT INTO piece_loading_history(`piece_loading_id`,`loading_type`, `po_entry_id`, `po_entry_det_id`, `master_cat_id`, `design_det_id`,`loading_dt`, `post_dt`)VALUES('$FTCH_MAX->piece_loading_id', 'PO', '$po_no', '$po_ent_id', '$catego_id', '$design_id', '$po_dd', '$cur_time')";		
			$QURY_HIS	= mysqli_query($connect,$INSRT_HIS);
		}
	}
	else if($piece_trc    == 'Cancel')
	{
		$po_dd		=	$_POST['lod_dt'];
		$po_dd		= 	str_replace("/", "-",$po_dd);
		$trc_type	=	base64_decode(base64_decode(base64_decode(base64_decode($_POST['trc_type']))));
		if($trc_type=='To Po')
		{
			$to_po_no = base64_decode(base64_decode(base64_decode(base64_decode($_POST['to_po']))));
			$po_val   = "loading";
			$po_en_id = $to_po_no;
		}
		else if($trc_type=='Box')
		{
			$to_po_no = NULL;
			$po_val   = "cancel";
			$po_en_id = $po_no;
		}
		$lp = 1;
		foreach($_POST['category'] as $cat_id)
		{
			$spl_id=explode(",",$cat_id);
			
			$catego_id	=	$spl_id[0];
			$design_id	=	$spl_id[1];		 
			$po_ent_id	=	$spl_id[2];
			
			$SLCT_MAX = "SELECT piece_loading_id FROM piece_loading WHERE `po_entry_id`='$po_no' AND `design_det_id`='$design_id' AND piece_status='loading'";
			$QURY_MAX = mysqli_query($connect,$SLCT_MAX);
			$FTCH_MAX = mysqli_fetch_object($QURY_MAX);
			
			$INSRT_pof	= "UPDATE piece_loading SET po_entry_id='$po_en_id', `update_dt`='$cur_time', piece_status='$po_val' WHERE `po_entry_id`='$po_no' AND `design_det_id`='$design_id' AND piece_status='loading'";	
			$QURY_pof	= mysqli_query($connect,$INSRT_pof);
			
			$INSRT_HIS	= "UPDATE piece_loading_history SET `cancel_type`='$trc_type',`cancel_dt`='$po_dd', `update_dt`='$cur_time', `remarks`='',`update_dt`='$cur_time' WHERE `piece_loading_id`='$FTCH_MAX->piece_loading_id' AND `po_entry_id`='$po_no' AND `design_det_id`='$design_id'";
			$QURY_HIS	= mysqli_query($connect,$INSRT_HIS);
			if($trc_type=='To Po')
			{
				$INSRT_HIS1	= "INSERT INTO piece_loading_history(`piece_loading_id`,`loading_type`, `po_entry_id`, `po_entry_det_id`, `master_cat_id`, `design_det_id`,`loading_dt`, `post_dt`)VALUES('$FTCH_MAX->piece_loading_id', 'PO', '$po_en_id', '$po_ent_id', '$catego_id', '$design_id', '$po_dd', '$cur_time')";		
				$QURY_HIS1	= mysqli_query($connect,$INSRT_HIS1);
			}
		}
	}
?>
	<script type="text/javascript">
		//alert("Data inserted successfully");
		//history.back();
		window.location='../po_finishing_list.php';
	</script>
	
