<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	//$page_id	=	1;	
	$cat_name		=	base64_decode(base64_decode(base64_decode($_POST['cat_name'])));
	$piece_trc		=	base64_decode(base64_decode(base64_decode(base64_decode($_POST['piece_trc']))));
	$po_dd			=	$_POST['lod_dt'];
	$po_dd			= 	str_replace("/", "-",$po_dd);
	if($piece_trc    == 'Load')
	{
		$lp = 1;
		foreach($_POST['category'] as $cat_id)
		{
			$spl_id=explode(",",$cat_id);
			
			$catego_id	=	$spl_id[0];
			$design_id	=	$spl_id[1];	
			
			$INSRT_pof	= "INSERT INTO piece_loading(`loading_type`, `master_cat_id`, `design_det_id`,`loading_dt`, `post_dt`)VALUES('Stock', '$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`, `master_cat_id`, `design_det_id`,`loading_dt`, `post_dt`)VALUES('$FTCH_MAX->piece_loading_id', 'Stock', '$catego_id', '$design_id', '$po_dd', '$cur_time')";		
			$QURY_HIS	= mysqli_query($connect,$INSRT_HIS);
		}
	}
	else if($piece_trc    == 'Finish')
	{
		$pack_name	=	$_POST['pack_name']; 
		$cut_name	=	$_POST['cut_name'];
		$fin = str_replace("/", "-",$_POST['fin_dt']);
		$cu_date = date("d-m-Y",strtotime($cur_date));
		$cut_dt = $_POST['cut_dt'];
		$cut_dt = str_replace("/", "-",$cut_dt);
			
		foreach($_POST['category'] as $cat_id)
		{
			$spl_id=explode(",",$cat_id);
			
			$catego_id	=	$spl_id[0];
			$design_id	=	$spl_id[1];	
			
			$INSRT_stock	= "INSERT INTO stock_det(`design_det_id`,`pack_emp_id`,`cut_emp_id`,`die_fin_date`,`cut_die_fin_date`,`post_dt`)VALUES('$design_id','$pack_name','$cut_name','$fin','$cut_dt','$cur_time')";		
			$QURY_stock		= mysqli_query($connect,$INSRT_stock);
			
			$INSRT_pof	= "UPDATE piece_loading SET `update_dt`='$cur_time', piece_status='finish' WHERE piece_status='loading' AND `design_det_id`='$design_id' AND `loading_type`='Stock' AND piece_status='loading'";	
			$QURY_pof	= mysqli_query($connect,$INSRT_pof);
			
			$INSRT_HIS	= "UPDATE piece_loading_history SET `finish_dt`='$po_dd', `update_dt`='$cur_time', `remarks`='',`update_dt`='$cur_time' WHERE `loading_dt`!='' AND `cancel_dt`='' AND `loading_type`='Stock' AND `design_det_id`='$design_id'";
			$QURY_HIS	= mysqli_query($connect,$INSRT_HIS);
		}
	}
	else if($piece_trc    == 'Cancel')
	{
		$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;
			$stock_lbl = "PO";
		}
		else if($trc_type=='Box')
		{
			$to_po_no = NULL;
			$po_val   = "cancel";
			$po_en_id = NULL;
			$stock_lbl = "Stock";
			$po_ent_id = NULL;
		}
		$lp = 1;
		foreach($_POST['category'] as $cat_id)
		{
			$spl_id=explode(",",$cat_id);
			
			$catego_id	=	$spl_id[0];
			$design_id	=	$spl_id[1];	
			if($trc_type=='To Po')
			{
				$SLCT_PO = "SELECT po_entry_det_id FROM po_entry_det WHERE `design_det_id`='$design_id' AND `po_entry_id`='$po_en_id'";
				$QURY_PO = mysqli_query($connect,$SLCT_PO);
				$FTCH_po = mysqli_fetch_object($QURY_PO);
				$po_ent_id = $FTCH_po->po_entry_det_id;
			}
			else if($trc_type=='Box')
			{
				$po_ent_id = NULL;
			}
			
			$SLCT_MAX = "SELECT piece_loading_id FROM piece_loading WHERE `design_det_id`='$design_id' AND `loading_type`='Stock' 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', `po_entry_det_id`='$po_ent_id', `loading_type`='$stock_lbl', `update_dt`='$cur_time', piece_status='$po_val' WHERE `design_det_id`='$design_id' AND `loading_type`='Stock' AND piece_status='loading'";	
			$QURY_pof	= mysqli_query($connect,$INSRT_pof);
			
			$INSRT_HIS	= "UPDATE piece_loading_history SET po_entry_id='$po_en_id', `po_entry_det_id`='$po_ent_id', `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 `design_det_id`='$design_id' AND `loading_type`='Stock'";
			$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">
       window.location='../po_finishing_list.php';
    </script>

