<?php
header ("Content-Type:text/xml");
include "start.php";
include "../includes/function.php";
echo "<psync>";
//---------------------------------------------------------------------------------------------------------
  
//===========================Product================================================  
  
  
  
  $query_ivi ="select * from t_".$key_table_prefix."_invoice_item 
  		where not ifnull(ivi_inventory_posting,'') = 'POSTED'
		and device_id = '".$_REQUEST["device_id"]."' ; ";
  $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 = '".$_REQUEST["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=="DEVICE_SYNC"))
	  {
		  
		  $this_doc_date="";
		  $this_header_doc_no=""; 
		  $this_module="";
		  
		  //get header:
		  $query_header = "select * from t_".$key_table_prefix."_invoice_header
		    				where device_id  ='".$line_ivi["device_id"]."'
						and ivh_id = '".$line_ivi["ivi_header_id"]."'
						 ";
		  $result_header = mysqli_query($dbconn,$query_header);
		  if($line_header = mysqli_fetch_array($result_header,MYSQLI_ASSOC))
		  {
			  $this_doc_date = $line_header["ivh_date"];
			  $this_header_doc_no = $line_header["ivh_doc_no"]; 
			  $this_module = "SALES";
		  }
		  
		  
		  post_inventory_balance(
			  		   $line_ivi["device_id"], 
			             $this_doc_date,
			             $this_header_doc_no, 
					   $line_ivi["ivi_id"],
					   
			             $this_module,
			  
			  		   "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"]."',
										    '".$this_header_doc_no."-".$line_ivi["ivi_id"]."SALES',
										    
										    '".$this_doc_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));
							   
				 }
	    
		 }
			  
			  
		  
		  
	  }
	  
	
  }
  
  
  $query_rti ="select * from t_".$key_table_prefix."_return_item 
  		where not ifnull(rti_inventory_posting,'') = 'POSTED'
		and device_id = '".$_REQUEST["device_id"]."' ; ";
  $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 = '".$_REQUEST["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=="DEVICE_SYNC"))
	  {
		  
		  //get header:
		  $query_header = "select * from t_".$key_table_prefix."_return_header
		    				where device_id  ='".$line_rti["device_id"]."'
						and rth_id = '".$line_rti["rti_header_id"]."'
						 ";
		  $result_header = mysqli_query($dbconn,$query_header);
		  if($line_header = mysqli_fetch_array($result_header,MYSQLI_ASSOC))
		  {
			  $this_doc_date = $line_header["rth_date"];
			  $this_header_doc_no = $line_header["rth_doc_no"]; 
			  $this_module = "RETURN";
		  }
		  
		  
		  
		  post_inventory_balance(
			  
					  $line_rti["device_id"], 
					  $this_doc_date,
					  $this_header_doc_no, 
					  $line_rti["rti_id"],
					  
					  $this_module,
					  
			  		  "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"]."',
										    '".$this_header_doc_no."-".$line_rti["rti_id"]."RETURN',
										    
										    '".$this_doc_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));
							   
				 }
	    
		 }
			  
			  
			  
			  
		  
		  
	  }
	  
	
  }
  
  
			                     
echo "<inventory_posting>
";
echo "<status>SUCCESS</status>
";
echo "<message>".str_replace("&","&amp;","Posting Successful")."</message>
";
echo "</inventory_posting>
";

         


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