<?php
header ("Content-Type:text/xml");
include "start.php";
include "../includes/function.php";
echo "<psync>";
//---------------------------------------------------------------------------------------------------------
  
//===========================Product================================================  
  
  
  
  $query_sti ="select * from t_".$key_table_prefix."_stock_transaction_item 
  		where not ifnull(sti_inventory_posting,'') = 'POSTED'
		and device_id = '".$_REQUEST["device_id"]."' ; ";
  $result_sti = mysqli_query($dbconn,$query_sti);
  while($line_sti = mysqli_fetch_array($result_sti,MYSQLI_ASSOC))
  {
	  
	
	  
	$this_doc_date =  $line_sti["sti_date"];
	$this_header_doc_no = $line_sti["sti_doc_no"];

	$default_warehouse_code  = $line_sti["sti_warehouse_code"];
	$default_warehouse_code2  = $line_sti["sti_warehouse_code2"]; //for transfer
	
	
	
	if($line_sti["sti_module"]=="STOCK_IN")
	{
		
		 
		$this_module="STOCK_IN";
		
		
		post_inventory_balance(
			
					$line_sti["device_id"], 
					$this_doc_date,
					$this_header_doc_no, 
					$line_sti["sti_id"],
					
					$this_module,
					
					"PLUS", 
					 $default_warehouse_code,
					 $line_sti["sti_stock_code"], 
					 $line_sti["sti_uom"], 
					 $line_sti["sti_quantity"], 
					 $line_sti["sti_color"], 
					 $line_sti["sti_size"], 
					 $line_sti["sti_pattern"]
				 );
				 
				 
				 
				 
				 
	 
	   	$query_update="update t_".$key_table_prefix."_stock_transaction_item  
			 	set sti_inventory_posting ='POSTED'
			  	where sti_id = '".$line_sti["sti_id"]."' ";
			  
	   	$result_update = mysqli_query($dbconn,$query_update);
	   
	   
	   
	   
	   
		if($sys_system_master_setting_line["set_module_internal_inventory_management_with_stock_posting"]=="YES")
  	   	{
  		   
		   	if($line_sti["sti_storage_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_sti["device_id"]."',
       							   '".$line_sti["sti_header_id"]."-".$line_sti["sti_id"]."',
       							   
       							   '".$this_doc_date."',
       							   
       							   
       							   '".$line_sti["sti_storage_location"]."',
       							   '".$line_sti["sti_stock_code"]."',
       							   
       							   'CHECK_IN',
       							   '".$CurrentUserFullName."',
       							   '".abs(($line_sti["sti_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));
       		   
       				    
       				  
		   	}
		   
		   
  		   	 
  		   
  		    
  		    
  	   	}
	   
	   
	   
		
	}
	if($line_sti["sti_module"]=="STOCK_OUT")
	{
		  
	   	$this_module="STOCK_OUT";
		
		
		post_inventory_balance(
			
					$line_sti["device_id"], 
					$this_doc_date,
					$this_header_doc_no, 
					$line_sti["sti_id"],
					
					$this_module,
					
					"MINUS", 
					 $default_warehouse_code,
					 $line_sti["sti_stock_code"], 
					 $line_sti["sti_uom"], 
					 $line_sti["sti_quantity"], 
					 $line_sti["sti_color"], 
					 $line_sti["sti_size"], 
					 $line_sti["sti_pattern"]
				 );
				 
			  
  
	 
			  
	 	$query_update="update t_".$key_table_prefix."_stock_transaction_item  
			 	set sti_inventory_posting ='POSTED'
			  	where sti_id = '".$line_sti["sti_id"]."' ";
				   
		$result_update = mysqli_query($dbconn,$query_update);
		    
		    
		    
		    
		    
		    
		    
		if($sys_system_master_setting_line["set_module_internal_inventory_management_with_stock_posting"]=="YES")
		 {
		    
			 if($line_sti["sti_storage_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_sti["device_id"]."',
									    '".$line_sti["sti_header_id"]."-".$line_sti["sti_id"]."',
									    
									    '".$this_doc_date."',
									    
									    
									    '".$line_sti["sti_storage_location"]."',
									    '".$line_sti["sti_stock_code"]."',
									    
									    'CHECK_OUT',
									    '".$CurrentUserFullName."',
									    '".abs(($line_sti["sti_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));
				    
							
						   
			 }
	    
	    
			  
		    
			
			
		 }
		
		
		
	}
	
	if($line_sti["sti_module"]=="STOCK_TRANSFER")
     {
	    
		
		
		
		
		
		
	    $this_module="STOCK_TRANSFER";
	    
	    post_inventory_balance(
		    
				    $line_sti["device_id"], 
				    $this_doc_date,
				    $this_header_doc_no, 
				    $line_sti["sti_id"],
				    
				    $this_module,
				    
				    "MINUS", 
					$default_warehouse_code,
					$line_sti["sti_stock_code"], 
					$line_sti["sti_uom"], 
					$line_sti["sti_quantity"], 
					$line_sti["sti_color"], 
					$line_sti["sti_size"], 
					$line_sti["sti_pattern"]
				);
				
	    
	    post_inventory_balance(
		    
				    $line_sti["device_id"], 
				    $this_doc_date,
				    $this_header_doc_no, 
				    $line_sti["sti_id"],
				    
				    $this_module,
				    
				    "PLUS", 
					$default_warehouse_code2,
					$line_sti["sti_stock_code"], 
					$line_sti["sti_uom"], 
					$line_sti["sti_quantity"], 
					$line_sti["sti_color"], 
					$line_sti["sti_size"], 
					$line_sti["sti_pattern"]
				);
				
				
				
		if($sys_system_master_setting_line["set_module_internal_inventory_management_with_stock_posting"]=="YES")
		 {
		    
				 if($line_sti["sti_storage_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_sti["device_id"]."',
										    '".$line_sti["sti_header_id"]."-".$line_sti["sti_id"]."OUT',
										    
										    '".$this_doc_date."',
										    
										    
										    '".$line_sti["sti_storage_location"]."',
										    '".$line_sti["sti_stock_code"]."',
										    
										    'CHECK_OUT',
										    '".$CurrentUserFullName."',
										    '".abs(($line_sti["sti_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));
					    
								
							   
				 }
	    
			 	if($line_sti["sti_storage_location2"]!="")
		  	    {
		  		    
		  		    $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_sti["device_id"]."',
		  								  '".$line_sti["sti_header_id"]."-".$line_sti["sti_id"]."IN',
		  								  
		  								  '".$this_doc_date."',
		  								  
		  								  
		  								  '".$line_sti["sti_storage_location2"]."',
		  								  '".$line_sti["sti_stock_code"]."',
		  								  
		  								  'CHECK_IN',
		  								  '".$CurrentUserFullName."',
		  								  '".abs(($line_sti["sti_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));
		  			  
		  					   
		  					 
		  	    }
			  
		    
			
			
		 }
				
				
				
				
				
	
	    $query_update="update t_".$key_table_prefix."_stock_transaction_item  
			    set sti_inventory_posting ='POSTED'
			    where sti_id = '".$line_sti["sti_id"]."' ";
			 
	    $result_update = mysqli_query($dbconn,$query_update);
	  
	  
	    
     }
		  
		  
		 
		  
		  
	  
	  
	
  }
  
  
  
			                     
echo "<inventory_posting>
";
echo "<status>SUCCESS</status>
";
echo "<message>".str_replace("&","&amp;","Posting Successful")."</message>
";
echo "</inventory_posting>
";

         


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