<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	//$page_id	=	1;	
	$page_id = base64_decode(base64_decode(base64_decode($_GET['pg_id']))); //THIS IS PAGE ID  
	                  
	//des_cd,pack_name,cut_name,pfd
	
	// IF flsg = 1; THERE IS NO PURCHASE ORDER EXIST FOR THE DESIGN
	// IF flsg = 2; THEN THE PURCHASE ORDER EXIST FOR THE DESIGN AND ALSO THE PARTICULAR DESIGN EXIST IN STOCK
	// IF flsg = 3; THEN THE PURCHASE ORDER EXIST FOR THE DESIGN AND ALSO NO STOCK FOR PARTICULAR DESIGN
	
if($page_id==1) //this is for basic details add page	
{ 		
		$des_cd		=	base64_decode(base64_decode(base64_decode($_POST['des_cd']))); //THIS IS PAGE ID  
		$pack_name	=	$_POST['pack_name']; 
		$cut_name	=	$_POST['cut_name']; 
		$pfd1		=	$_POST['pfd']; 
		$pfd		= 	str_replace("/","-",$pfd1);
		$cut_dt1	=	$_POST['cut_dt']; 
		$cut_dt		= 	str_replace("/","-",$cut_dt1);
		$flsg		=	$_POST['flsg']; 
		$cu_date 	= 	date("d-m-Y",strtotime($cur_date));
		
		
		if($flsg == 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('$des_cd','$pack_name','$cut_name','$pfd','$cut_dt','$cur_time')";		
			//echo $INSRT_stock."&nbsp&nbsp&nbspAA<br>";
			$QURY_stock		= mysqli_query($connect,$INSRT_stock);
		
		}
		else if($flsg == 2)
		{
			$SLCT_s = "SELECT * FROM stock_det WHERE st_fin_status = 'no' AND design_det_id = '$des_cd'";
			$QURY_s = mysqli_query($connect,$SLCT_s);  
			$FTCH_s = mysqli_fetch_object($QURY_s);
			
			$category		=	$_POST['category']; 
			
			$INSRT_pof		= "INSERT INTO po_finish(`po_entry_det_id`, `stock_det_id`, `design_det_id`, `pack_emp_id`, `cut_emp_id`, `die_fin_date`, `cut_die_fin_date`,`post_dt`)VALUES('$category','$FTCH_s->stock_det_id','$des_cd','$pack_name','$cut_name','$pfd','$cut_dt','$cur_time')";		
			//echo $INSRT_pof;
			$QURY_pof		= mysqli_query($connect,$INSRT_pof);
			
			//THI IS FOR UPDATING THE stock STATUS
			$UPDATE = "UPDATE stock_det SET stock_rel_date = '$cu_date',st_fin_status = 'yes',update_dt = '$cur_time' WHERE stock_det_id = '$FTCH_s->stock_det_id'";
			$QRY    = mysqli_query($connect,$UPDATE);
			
			$UPDATE = "UPDATE po_entry_det SET fin_status = 'yes',update_dt = '$cur_time' WHERE po_entry_det_id = '$category'";
			//echo $UPDATE;
			$QRY    = mysqli_query($connect,$UPDATE);
			
		}
		else if($flsg == 3)
		{
			$category		=	$_POST['category']; 
			
			$INSRT_pof		= "INSERT INTO po_finish(`po_entry_det_id`, `design_det_id`, `pack_emp_id`, `cut_emp_id`,`die_fin_date`, `cut_die_fin_date`,`post_dt`)VALUES('$category','$des_cd','$pack_name','$cut_name','$pfd','$cut_dt','$cur_time')";		
			$QURY_pof		= mysqli_query($connect,$INSRT_pof);
			
			$UPDATE = "UPDATE po_entry_det SET fin_status = 'yes',update_dt = '$cur_time' WHERE po_entry_det_id = '$category'";
			//echo $UPDATE;
			$QRY    = mysqli_query($connect,$UPDATE);
			
		}
		
		//echo $category;		
		//THI IS FOR UPDATING THE DESIGN STATUS
		//$UPDATE = "UPDATE design_det SET design_status = 'In PO Entry',update_dt = '$cur_time' WHERE design_det_id = '$cat_id'";
//		$QRY    = mysqli_query($connect,$UPDATE); 
				
		
		
?>
		<script type="text/javascript">
            //alert("Data inserted successfully");
            //history.back();
           window.location='../po_finishing_list.php';
        </script>
<?php
}
if($page_id==2) //this is for basic details add page	
{
	$po_no			=	$_POST['po_no'];
	//$fin_type		=	$_POST['fin_type'];
	$pagg_id		=	$_POST['pagg_id'];
	//echo $fin_type;
	if($pagg_id    == 1)
	{
		$pack_name	=	$_POST['pack_name']; 
		$cut_name	=	$_POST['cut_name'];		 
		$po_dd		=	$_POST['fin_dt'];
		$po_dd		= 	str_replace("/", "-",$po_dd);
		$cut_dt		=	$_POST['cut_dt']; 
		$cut_dt		= 	str_replace("/","-",$cut_dt);
		$lp = 1;
		foreach($_POST['category'] as $cat_id)
		{
			$spl_id=explode(",",$cat_id);
			
			$design_id	=	$spl_id[0];		 
			$po_ent_id	=	$spl_id[1];
			
			$INSRT_pof	= "INSERT INTO po_finish(`po_entry_det_id`, `design_det_id`, `pack_emp_id`, `cut_emp_id`,`die_fin_date`,`cut_die_fin_date`, `post_dt`)VALUES('$po_ent_id','$design_id','$pack_name','$cut_name','$po_dd','$cut_dt','$cur_time')";		
			//echo $INSRT_pof."<br />";
			$QURY_pof	= mysqli_query($connect,$INSRT_pof);
			
			$UPDATE 	= "UPDATE po_entry_det SET fin_status = 'yes',update_dt = '$cur_time' WHERE po_entry_det_id = '$po_ent_id'";
			//echo $UPDATE."<br /><br />";
			$QRY    	= mysqli_query($connect,$UPDATE);
			
			$SLCT_MAX = "SELECT * FROM piece_loading WHERE `design_det_id`='$design_id' AND piece_status='loading' AND po_entry_id = '$po_no'";
			$QURY_MAX = mysqli_query($connect,$SLCT_MAX);
			if(mysqli_num_rows($QURY_MAX)>0)
			{
				$FTCH_MAX = mysqli_fetch_object($QURY_MAX);
				
				$UPDT_pof	= "UPDATE piece_loading SET `update_dt`='$cur_time', piece_status='finish' WHERE `piece_loading_id`='$FTCH_MAX->piece_loading_id' AND piece_status='loading' AND po_entry_id = '$po_no' AND `design_det_id`='$design_id'";	
				//echo $UPDT_pof."<br />";
				$QURY_pof	= mysqli_query($connect,$UPDT_pof);
				
				$INSRT_HIS	= "UPDATE piece_loading_history SET `finish_dt`='$po_dd', `update_dt`='$cur_time' WHERE `loading_dt`!='' AND `cancel_dt`='' AND `piece_loading_id`='$FTCH_MAX->piece_loading_id' AND po_entry_id = '$po_no' AND `design_det_id`='$design_id'";
				//echo $INSRT_HIS."<br /><br />";
				$QURY_HIS	= mysqli_query($connect,$INSRT_HIS);
			}
			if($_POST['po_chk_'.$design_id]==1)
			{
				if(isset($_POST['po_num_'.$design_id]))
				{
					$pce_id 		=	$_POST['po_num_'.$design_id];
					$spl_cat 		= 	explode(",",$pce_id);
					$catego_id		=	$spl_cat[0];
					$lod_po_id		=	$spl_cat[1];
					$lod_par_id		=	$spl_cat[2];		 
					$lod_po_ent_id	=	$spl_cat[3];
					
					$INST_1	= "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', '$lod_po_id', '$lod_po_ent_id', '$catego_id', '$design_id', '$po_dd', '$cur_time')";	
					//echo $INST_1."<br />";
					$QURY_1	= mysqli_query($connect,$INST_1);
					
					$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);
					
					$INST_2	= "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', '$lod_po_id', '$lod_po_ent_id', '$catego_id', '$design_id', '$po_dd', '$cur_time')";		
					//echo $INST_2."<br />";
					$QURY_2	= mysqli_query($connect,$INST_2);		
				}
			}
		}
	}
	else
	{
		//echo $cur_date;
		$lp = 1;
		$cu_date = date("d-m-Y",strtotime($cur_date));
		//$cu_date		= 	str_replace("/", "-", $cur_date);
		foreach($_POST['category'] as $cat_id)
		{
			$spl_id=explode(",",$cat_id);
			
			$design_id	=	$spl_id[0];		 
			$po_ent_id	=	$spl_id[1];
			$stoc_id	=	$spl_id[2];
			$pack_name	=	$spl_id[3]; 
			$cut_name	=	$spl_id[4];		 
			$po_dd		=	str_replace("/", "-",$spl_id[5]);
			$cut_dd		=	str_replace("/", "-",$spl_id[6]);
			
			$INSRT_pof		= "INSERT INTO po_finish(`po_entry_det_id`, `stock_det_id`, `design_det_id`, `pack_emp_id`, `cut_emp_id`,`die_fin_date`,`cut_die_fin_date`, `post_dt`)VALUES('$po_ent_id','$stoc_id','$design_id','$pack_name','$cut_name','$po_dd','$cut_dd','$cur_time')";		
			//echo $INSRT_pof."<br />";
			$QURY_pof		= mysqli_query($connect,$INSRT_pof);
			
			//THI IS FOR UPDATING THE stock STATUS
			$UPDATE_st = "UPDATE stock_det SET stock_rel_date = '$cu_date',st_fin_status = 'yes',update_dt = '$cur_time' WHERE stock_det_id = '$stoc_id'";
			$QRY_st    = mysqli_query($connect,$UPDATE_st);
			
			$UPDATE = "UPDATE po_entry_det SET fin_status = 'yes',update_dt = '$cur_time' WHERE po_entry_det_id = '$po_ent_id'";
			//echo $UPDATE;
			$QRY    = mysqli_query($connect,$UPDATE);
			
			$SLCT_MAX = "SELECT * FROM piece_loading WHERE `design_det_id`='$design_id' AND piece_status='loading' AND po_entry_id = '$po_no'";
			$QURY_MAX = mysqli_query($connect,$SLCT_MAX);
			if(mysqli_num_rows($QURY_MAX)>0)
			{
				$FTCH_MAX = mysqli_fetch_object($QURY_MAX);
				
				$INSRT_pof	= "UPDATE piece_loading SET `update_dt`='$cur_time', piece_status='finish' WHERE `piece_loading_id`='$FTCH_MAX->piece_loading_id' AND piece_status='loading' AND po_entry_id = '$po_no' AND `design_det_id`='$design_id'";	
				$QURY_pof	= mysqli_query($connect,$INSRT_pof);
				
				$INSRT_HIS	= "UPDATE piece_loading_history SET `finish_dt`='$po_dd', `cancel_type`='Stock To Po', `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);
			}
		}
	}
?>
	<script type="text/javascript">
		//alert("Data inserted successfully");
		//history.back();
		window.location='../po_finishing_list.php';
	</script>
<?php 
}
?>
	
