<?php include "../../includes/start_ajax.php";

?>

<?php
$this_url="mod/picking_list/index.php";

 



if($_REQUEST["picking_mode"]!="")
{
	$_SESSION["picking_mode"] = $_REQUEST["picking_mode"];
	
}
if($_SESSION["picking_mode"]=="")
{
	$_SESSION["picking_mode"]="CALENDAR";
	
}










if(($_REQUEST["delivery"]=="YES")and($_REQUEST["ivh_id"]!=""))
{
	
	
	$query_header="select * from t_".$key_table_prefix."_invoice_header 
				where ivh_id = '".$_REQUEST["ivh_id"]."' ";
	$result_header = mysqli_query($dbconn,$query_header);
	if($line_header = mysqli_fetch_array($result_header,MYSQLI_ASSOC))
	{
		
		$query_update="update t_".$key_table_prefix."_invoice_header 
				set ivh_delivery_status = 'DELIVERED' 
				where ivh_id = '".$_REQUEST["ivh_id"]."'";
		$result_update=mysqli_query($dbconn,$query_update);
		
	}
	
}



if(($_REQUEST["delivery"]=="YES")and($_REQUEST["rth_id"]!=""))
{
	
	$query_header="select * from t_".$key_table_prefix."_return_header 
				where rth_id = '".$_REQUEST["rth_id"]."' ";
	$result_header = mysqli_query($dbconn,$query_header);
	if($line_header = mysqli_fetch_array($result_header,MYSQLI_ASSOC))
	{
		$query_update="update t_".$key_table_prefix."_return_header 
				set rth_delivery_status = 'DELIVERED' 
				where rth_id = '".$_REQUEST["rth_id"]."'";
		$result_update=mysqli_query($dbconn,$query_update);
	}
	
}







if(($_REQUEST["inventory_posting"]=="YES")and($_REQUEST["ivh_id"]!=""))
{
	
	$error_msg="";
	
	
	$query_header="select * from t_".$key_table_prefix."_invoice_header 
				where ivh_id = '".$_REQUEST["ivh_id"]."' ";
	$result_header = mysqli_query($dbconn,$query_header);
	if($line_header = mysqli_fetch_array($result_header,MYSQLI_ASSOC))
	{
		
		$query_ivi ="select * from t_".$key_table_prefix."_invoice_item 
			   where not ifnull(ivi_inventory_posting,'') = 'POSTED'
			   and ivi_doc_no ='".$line_header["ivh_doc_no"]."'
			    ; ";
		$result_ivi = mysqli_query($dbconn,$query_ivi);
		while($line_ivi = mysqli_fetch_array($result_ivi,MYSQLI_ASSOC))
		{
		     $default_warehouse_code="";
		     
		     
		     $query_device="select * from t_".$key_table_prefix."_device 
				   where dev_android_id = '".$line_ivi["device_id"]."' ";
		     
		     $resul_device= mysqli_query($dbconn,$query_device);
		     if($line_device = mysqli_fetch_array($resul_device,MYSQLI_ASSOC))
		     {
				
				$default_warehouse_code  = $line_device["dev_default_warehouse_code"];
				$stock_posting_stage  = $line_device["dev_stock_posting_stage"];
		     }
			
		     
		     if(($default_warehouse_code!="")and($stock_posting_stage=="DELIVERY_CONFIRMATION"))
		     {
				post_inventory_balance(
							$line_header["device_id"],
							$line_header["ivh_date"],
							$line_header["ivh_doc_no"],
							$line_ivi["ivi_id"], 
							
							"SALES", 
							
							
							"MINUS", 
							 $default_warehouse_code,
							 $line_ivi["ivi_product_code"], 
							 $line_ivi["ivi_uom"], 
							 $line_ivi["ivi_quantity"], 
							 $line_ivi["ivi_color"], 
							 $line_ivi["ivi_size"], 
							 $line_ivi["ivi_pattern"]
						 );
						 
			 
			   $query_update="update t_".$key_table_prefix."_invoice_item  
					 set ivi_inventory_posting ='POSTED'
					  where ivi_id = '".$line_ivi["ivi_id"]."' ";
					  
			   $result_update = mysqli_query($dbconn,$query_update);
			   
			   
			   
			   
			   if($sys_system_master_setting_line["set_module_internal_inventory_management_with_stock_posting"]=="YES")
			    {
				  
					//get product"
					  $query_get_product = "select * from t_".$key_table_prefix."_product 
									where pdt_code  ='".$line_ivi["ivi_product_code"]."'
									 ";
					  $result_get_product = mysqli_query($dbconn,$query_get_product);
					  if($line_get_product = mysqli_fetch_array($result_get_product,MYSQLI_ASSOC))
					  {
						  $this_product_default_location  = $line_get_product["pdt_storage_location"];
						  
						  
					  }
					  
				  
				  
					    if($this_product_default_location!="")
					    {
						    
						    $query_internal_transaction ="INSERT INTO
											   t_".$key_table_prefix."_internal_inventory_transaction
											   (
												  device_id  ,
												  iit_device_iit_id  ,
												  iit_date  ,
												  iit_location_code  ,
												  iit_product_code  ,
												  
												  iit_type  ,
												  iit_user  ,
												  iit_quantity  ,
											  
												  created_date ,
												  modified_date ,
												  client_id 
											   )
											   VALUES
											   (
												  
												  
												  '".$line_ivi["device_id"]."',
												  '".$line_header["ivh_doc_no"]."-".$line_ivi["ivi_id"]."SALES',
												  
												  '".$line_header["ivh_date"]."',
												  
												  
												  '".$this_product_default_location."',
												  '".$line_ivi["ivi_product_code"]."',
												  
												  'CHECK_OUT',
												  '".$CurrentUserFullName."',
												  '".abs(($line_ivi["ivi_quantity"]+0))."',
												  
												  
												  '".date("Y-m-d H:i:s")."',
												  '".date("Y-m-d H:i:s")."',
												  
												  '0'
											   )
										   ;
										   ";
							  $result_internal_transaction = mysqli_query($dbconn,$query_internal_transaction) or die("Error ".mysqli_error($dbconn));
									 
					    }
			  
			    }
			    
			    
			    
				
				
		     }
			else {
				$error_msg="No Warehouse Profile defined for device: ".$line_ivi["device_id"]."";
			}
			
		   
		}
		
		
	}
	
	if($error_msg!="")
	{
		displaymessage($error_msg,"FAIL");
	}
	
	
}




if(($_REQUEST["inventory_posting"]=="YES")and($_REQUEST["rth_id"]!=""))
{
	$error_msg="";
	
	
	$query_header="select * from t_".$key_table_prefix."_return_header 
				where rth_id = '".$_REQUEST["rth_id"]."' ";
	$result_header = mysqli_query($dbconn,$query_header);
	if($line_header = mysqli_fetch_array($result_header,MYSQLI_ASSOC))
	{
	
		
		$query_rti ="select * from t_".$key_table_prefix."_return_item 
			   where not ifnull(rti_inventory_posting,'') = 'POSTED'
			   and rti_doc_no ='".$line_header["rth_doc_no"]."'
			   ; ";
		$result_rti = mysqli_query($dbconn,$query_rti);
		while($line_rti = mysqli_fetch_array($result_rti,MYSQLI_ASSOC))
		{
		     $default_warehouse_code="";
		     
		     
		     $query_device="select * from t_".$key_table_prefix."_device 
				   where dev_android_id = '".$line_rti["device_id"]."' ";
		     
		     $resul_device= mysqli_query($dbconn,$query_device);
		     if($line_device = mysqli_fetch_array($resul_device,MYSQLI_ASSOC))
		     {
				$default_warehouse_code  = $line_device["dev_default_warehouse_code"];
				$stock_posting_stage  = $line_device["dev_stock_posting_stage"];
				
				
		     }
			
			
		     
		     if(($default_warehouse_code!="")and($stock_posting_stage=="DELIVERY_CONFIRMATION"))
		     {
				
				
				
				post_inventory_balance(
					
					
							$line_header["device_id"],
							$line_header["rth_date"],
							$line_header["rth_doc_no"],
							$line_rti["rti_id"], 
							
							"RETURN", 
							
							"PLUS", 
							 $default_warehouse_code,
							 $line_rti["rti_product_code"], 
							 $line_rti["rti_uom"], 
							 $line_rti["rti_quantity"], 
							 $line_rti["rti_color"], 
							 $line_rti["rti_size"], 
							 $line_rti["rti_pattern"]
						 );
						 
			 
			   $query_update="update t_".$key_table_prefix."_return_item  
					 set rti_inventory_posting ='POSTED'
					  where rti_id = '".$line_rti["rti_id"]."' ";
					  
			   $result_update = mysqli_query($dbconn,$query_update);
			   
			   
			   
			   if($sys_system_master_setting_line["set_module_internal_inventory_management_with_stock_posting"]=="YES")
			    {
				  
					//get product"
					  $query_get_product = "select * from t_".$key_table_prefix."_product 
									where pdt_code  ='".$line_rti["rti_product_code"]."'
									 ";
					  $result_get_product = mysqli_query($dbconn,$query_get_product);
					  if($line_get_product = mysqli_fetch_array($result_get_product,MYSQLI_ASSOC))
					  {
						  $this_product_default_location  = $line_get_product["pdt_storage_location"];
						  
						  
					  }
					  
				  
				  
					    if($this_product_default_location!="")
					    {
						    
						    $query_internal_transaction ="INSERT INTO
											   t_".$key_table_prefix."_internal_inventory_transaction
											   (
												  device_id  ,
												  iit_device_iit_id  ,
												  iit_date  ,
												  iit_location_code  ,
												  iit_product_code  ,
												  
												  iit_type  ,
												  iit_user  ,
												  iit_quantity  ,
											  
												  created_date ,
												  modified_date ,
												  client_id 
											   )
											   VALUES
											   (
												  
												  
												  '".$line_rti["device_id"]."',
												  '".$line_header["rth_doc_no"]."-".$line_rti["rti_id"]."RETURN',
												  
												  '".$line_header["rth_date"]."',
												  
												  
												  '".$this_product_default_location."',
												  '".$line_rti["rti_product_code"]."',
												  
												  'CHECK_IN',
												  '".$CurrentUserFullName."',
												  '".abs(($line_rti["rti_quantity"]+0))."',
												  
												  
												  '".date("Y-m-d H:i:s")."',
												  '".date("Y-m-d H:i:s")."',
												  
												  '0'
											   )
										   ;
										   ";
							  $result_internal_transaction = mysqli_query($dbconn,$query_internal_transaction) or die("Error ".mysqli_error($dbconn));
									 
					    }
			  
			    }
			    
			    
			    
			    
				
				
		     }
			else {
				$error_msg="No Warehouse Profile defined for device: ".$line_rti["device_id"]."";
			}
		   
		}
		
		
		
		
		
	}
	
	
	if($error_msg!="")
	{
		displaymessage($error_msg,"FAIL");
	}
	
}




echo "<section class='view_toolbar'>";
	 echo "<div class='view_toolbar_left' style='width:50%;'>";
		    echo "<font size=5 ><span class='fa fa-truck fa-1x'></span> <b>Picking List</b></font>";

		    echo "<br>";
		   
	 echo "</div>";
	 echo "<div class='view_toolbar_right'>";

		 echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:right;'  onclick=\"mon_refresh_tab(mon_current_selected_tab);\" >";
		 	echo "<i class='fa fa-square fa-stack-2x color_red'   ></i><i class='fa fa-refresh fa-stack-1x color_white' ></i>";
		 echo "</div>";
		 
	 echo "</div>";
echo "</section>";

    
    	if($_SESSION["picking_mode"]=="CALENDAR")
	{
		include "mode_calendar.php";
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
?>
	
<?php include "../../includes/end_ajax.php";

?>