<?php
header ("Content-Type:text/xml");
include "start.php";
include "../includes/function.php";
echo "<psync>";
//---------------------------------------------------------------------------------------------------------
  
//===========================Product================================================  
  




	$query_iit ="select * 
	
			from t_".$key_table_prefix."_internal_inventory_transaction
		   where not ifnull(iit_inventory_posting_status,'') = 'POSTED'
		   and device_id = '".$_REQUEST["device_id"]."' 
		   order by iit_type, iit_location_code , iit_id 
		   ; ";
	$result_iit = mysqli_query($dbconn,$query_iit);
	
	while($line_iit = mysqli_fetch_array($result_iit,MYSQLI_ASSOC))
	{
	
	
		$query_product="select * from t_".$key_table_prefix."_product 
					where pdt_code = '".$line_iit["iit_product_code"]."' ";
		$result_product = mysqli_query($dbconn,$query_product);
		if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
		{
			
			
			$query_location="select * from t_".$key_table_prefix."_storage_location 
						where loc_code = '".$line_iit["iit_location_code"]."' ";
						
						
			$result_location = mysqli_query($dbconn,$query_location);
		     if($line_location = mysqli_fetch_array($result_location,MYSQLI_ASSOC))
			{
				
				
				
				$default_warehouse_code = $line_location["loc_warehouse_code"];


				if($line_iit["iit_type"]=="INTERNAL_USE")
				{
					
					//internal usage:
					
					$this_module="INTERNAL_USE";
					$this_doc_date=substr($line_iit["created_date"],0,10);
					
					
					$this_color=""; 
					$this_size="";
					$this_pattern="";
					
					
					post_inventory_balance(
						
								$line_iit["device_id"], 
								$this_doc_date,
								$line_iit["device_id"]."-".$line_iit["iit_device_iit_id"],
								$line_iit["iit_id"],
								
								$this_module,
								
								"MINUS", 
								 $default_warehouse_code,
								 $line_iit["iit_product_code"], 
								 $line_product["pdt_uom"], 
								 $line_iit["iit_quantity"], 
								 $this_color, 
								 $this_size, 
								 $this_pattern
							 );
							 
						  
			  
				 
						  
					$query_update="update t_".$key_table_prefix."_internal_inventory_transaction  
							set iit_inventory_posting_status ='POSTED' ,
							    iit_inventory_posting_warehouse_code ='".$default_warehouse_code."'
							where iit_id = '".$line_iit["iit_id"]."' ";
							   
					$result_update = mysqli_query($dbconn,$query_update);
					
					
					
				}
				
				
				if($line_iit["iit_type"]=="CHECK_IN")
				{
					
					
					$query_sync="INSERT INTO
				                     t_".$key_table_prefix."_storage_location_movement
				                     (
			 						iot_doc_no,
									iot_location_code,
	   
								   	iot_type,
								   
								   	iot_product_code,
								   	iot_product_name,
								   	iot_quantity,
								   	iot_uom,

								   	iot_user,
								   
								   	iot_transaction_datetime,
								   
								   	device_id,
									
				 					created_date ,					
				 					modified_date ,
				 					client_id 
				                     )
				                     VALUES
				                     (
				 	                    
									'".$line_iit["device_id"]."-".$line_iit["iit_device_iit_id"]."',
				 					'".$line_iit["iit_location_code"]."',
				 					'".$line_iit["iit_type"]."', 
									
									'".$line_iit["iit_product_code"]."', 
									'".$line_product["pdt_name"]."', 
									'".$line_iit["iit_quantity"]."', 
									'".$line_product["pdt_uom"]."', 
								
								
				 					'".$line_iit["iit_user"]."',
									
									'".$line_iit["created_date"]."' ,					
									
									'".$line_iit["device_id"]."' ,					

				 					'".$line_iit["created_date"]."' ,					
				 					'".$line_iit["modified_date"]."' ,
				 					'".$client_id."' 
				 					
				                     )
				                    
								
				                     ;
				                     ";
				     $result_sync = mysqli_query($dbconn,$query_sync) or die("Error ".mysqli_error($dbconn));
				     
				                
					$query_update="update t_".$key_table_prefix."_internal_inventory_transaction  
							set iit_inventory_posting_status ='POSTED' ,
							    iit_inventory_posting_warehouse_code ='".$default_warehouse_code."'
							where iit_id = '".$line_iit["iit_id"]."' ";
							   
					$result_update = mysqli_query($dbconn,$query_update);
					
					
					
					
					
					
					//update storage location inventory:
					//=========================================================================================
					$query_lba="select * from t_".$key_table_prefix."_storage_location_balance 
							where lba_location_code = '".$line_iit["iit_location_code"]."'
							and lba_product_code = '".$line_iit["iit_product_code"]."'
							;";
					$result_lba = mysqli_query($dbconn,$query_lba);
				     if($line_lba = mysqli_fetch_array($result_lba,MYSQLI_ASSOC))
					{
						//update balance(plus):
						
						$query_update_lba="update t_".$key_table_prefix."_storage_location_balance  
										set
										lba_quantity = lba_quantity + '".($line_iit["iit_quantity"]+0)."',
										modified_date = '".date("Y-m-d H:i:s")."'
										
										where lba_location_code = '".$line_iit["iit_location_code"]."'
										and lba_product_code = '".$line_iit["iit_product_code"]."'
										";
										
										
						$result_update_lba = mysqli_query($dbconn,$query_update);
						
						
						
					}
					else {
						//insert balance(plus):
						$query_insert_lba="insert into t_".$key_table_prefix."_storage_location_balance  
										(
										lba_location_code,
										lba_product_code,
										lba_quantity,
										created_date,
										modified_date
										)
										VALUES
										(
										'".$line_iit["iit_location_code"]."',
										'".$line_iit["iit_product_code"]."',
										'".($line_iit["iit_quantity"]+0)."',
										'".date("Y-m-d H:i:s")."',
										'".date("Y-m-d H:i:s")."'
										);
										";
						$result_insert_lba = mysqli_query($dbconn,$query_insert_lba);
						
						
						
					}
					
					
					
					
					
					
				}
				
				
				if($line_iit["iit_type"]=="CHECK_OUT")
				{
					
					$query_sync="INSERT INTO
				                     t_".$key_table_prefix."_storage_location_movement
				                     (
			 						iot_doc_no,
									iot_location_code,
	   
								   	iot_type,
								   
								   	iot_product_code,
								   	iot_product_name,
								   	iot_quantity,
								   	iot_uom,

								   	iot_user,
								   
								   	iot_transaction_datetime,
								   
								   	device_id,
									
				 					created_date ,					
				 					modified_date ,
				 					client_id 
				                     )
				                     VALUES
				                     (
				 	                    
									'".$line_iit["device_id"]."-".$line_iit["iit_device_iit_id"]."',
				 					'".$line_iit["iit_location_code"]."',
				 					'".$line_iit["iit_type"]."', 
									
									'".$line_iit["iit_product_code"]."', 
									'".$line_product["pdt_name"]."', 
									'".$line_iit["iit_quantity"]."', 
									'".$line_product["pdt_uom"]."', 
								
								
				 					'".$line_iit["iit_user"]."',
									
									'".$line_iit["created_date"]."' ,					
									
									'".$line_iit["device_id"]."' ,					

				 					'".$line_iit["created_date"]."' ,					
				 					'".$line_iit["modified_date"]."' ,
				 					'".$client_id."' 
				 					
				                     )
				                    
								
				                     ;
				                     ";
				     $result_sync = mysqli_query($dbconn,$query_sync) or die("Error ".mysqli_error($dbconn));
				     
					$query_update="update t_".$key_table_prefix."_internal_inventory_transaction  
							set iit_inventory_posting_status ='POSTED' ,
							    iit_inventory_posting_warehouse_code ='".$default_warehouse_code."'
							where iit_id = '".$line_iit["iit_id"]."' ";
							   
					$result_update = mysqli_query($dbconn,$query_update);
					           
				 			
					
					
					
					
					//update storage location inventory:
					//=========================================================================================
					$query_lba="select * from t_".$key_table_prefix."_storage_location_balance 
							where lba_location_code = '".$line_iit["iit_location_code"]."'
							and lba_product_code = '".$line_iit["iit_product_code"]."'
							;";
					$result_lba = mysqli_query($dbconn,$query_lba);
				     if($line_lba = mysqli_fetch_array($result_lba,MYSQLI_ASSOC))
					{
						//update balance(minus):
						
						$query_update_lba="update t_".$key_table_prefix."_storage_location_balance  
										set
										lba_quantity = lba_quantity - '".($line_iit["iit_quantity"]+0)."',
										modified_date = '".date("Y-m-d H:i:s")."'
										
										where lba_location_code = '".$line_iit["iit_location_code"]."'
										and lba_product_code = '".$line_iit["iit_product_code"]."'
										";
										
										
						$result_update_lba = mysqli_query($dbconn,$query_update);
						
						
						
					}
					else {
						//insert negative balance(minus):
						$query_insert_lba="insert into t_".$key_table_prefix."_storage_location_balance  
										(
										lba_location_code,
										lba_product_code,
										lba_quantity,
										created_date,
										modified_date
										)
										VALUES
										(
										'".$line_iit["iit_location_code"]."',
										'".$line_iit["iit_product_code"]."',
										'".(0-$line_iit["iit_quantity"])."',
										'".date("Y-m-d H:i:s")."',
										'".date("Y-m-d H:i:s")."'
										);
										";
						$result_insert_lba = mysqli_query($dbconn,$query_insert_lba);
						
						
						
					}
					
					
					
				}
				
				
				
				
				
				
				
				
				
				
				
				
				
			}
			
			
		}
	
	
	
	
		
		
		
		
		
	}
	
	
	
	
	
 
 
  
			                     
echo "<inventory_posting>
";
echo "<status>SUCCESS</status>
";
echo "<message>".str_replace("&","&amp;","Posting Successful")."</message>
";
echo "</inventory_posting>
";

         


//---------------------------------------------------------------------------------------------------------
echo "</psync>";
?>