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


  
  
  $query_warehouse_lib="select * from t_".$key_table_prefix."_warehouse ";
  $result_warehouse_lib = mysqli_query($dbconn,$query_warehouse_lib);
  while($line_warehouse_lib = mysqli_fetch_array($result_warehouse_lib,MYSQLI_ASSOC))
  {
	  $lib_warehouse[$line_warehouse_lib["whs_code"]] = $line_warehouse_lib["whs_id"];
	  
	  
  }
  
  
  
  
  
  
  
    $query_sync="INSERT INTO
                    t_".$key_table_prefix."_stock_transaction_item
                    (
					
					
			 	
					device_id, 
					
			 		sti_doc_prefix, 
					sti_doc_no, 
					sti_module, 
					sti_date, 
					
					sti_warehouse_code, 
					sti_warehouse_id,
					 
					sti_warehouse_code2, 
					sti_warehouse_id2,
					 
					sti_stock_code, 
					sti_barcode, 
					sti_factory_code, 
					sti_stock_name, 
					
					sti_color, 
					sti_size, 
					sti_pattern, 
					
					sti_uom,
					sti_base_uom,  
					
					sti_quantity,
					sti_base_quantity,  
					
					sti_conversion,  
					
					sti_unit_cost,  
					sti_base_unit_cost,  
					sti_total_cost,  
					
					sti_gst_mode,  
					sti_gst_code,  
					sti_gst_percentage,  
					
					sti_remark,  
					sti_status,  
					
					sti_before_gst_amount, 
					sti_gst_amount, 
					sti_total_amount, 
					
					sti_device_sti_id ,	
					
					
					sti_storage_location ,	
					sti_storage_location2 ,	
					
					created_date ,					
					modified_date ,
					client_id 
                    )
                    VALUES
                    (
	                    
					
					'".$_REQUEST["device_id"]."', 
					
			 		'".$_REQUEST["sti_doc_prefix"]."' ,
					'".$_REQUEST["sti_doc_no"]."' ,
					'".$_REQUEST["sti_module"]."' ,
					'".$_REQUEST["sti_date"]."' ,
					
					'".$_REQUEST["sti_warehouse_code"]."' ,
					'".($lib_warehouse[$_REQUEST["sti_warehouse_code"]]+0)."' ,
					
					'".$_REQUEST["sti_warehouse_code2"]."' ,
					'".($lib_warehouse[$_REQUEST["sti_warehouse_code2"]]+0)."' ,
					
					'".$_REQUEST["sti_stock_code"]."' ,
					'".$_REQUEST["sti_barcode"]."' ,
					'".$_REQUEST["sti_factory_code"]."' ,
					'".$_REQUEST["sti_stock_name"]."' ,
					
					'".$_REQUEST["sti_color"]."' ,
					'".$_REQUEST["sti_size"]."' ,
					'".$_REQUEST["sti_pattern"]."' ,
					
					'".$_REQUEST["sti_uom"]."' ,
					'".$_REQUEST["sti_base_uom"]."' ,
					
					'".abs($_REQUEST["sti_quantity"])."' ,
					'".abs($_REQUEST["sti_base_quantity"])."' ,
					
					'".$_REQUEST["sti_conversion"]."' ,
					
					'".abs($_REQUEST["sti_unit_cost"])."' ,
					'".abs($_REQUEST["sti_base_unit_cost"])."' ,
					'".abs($_REQUEST["sti_total_cost"])."' ,
					
					'".$_REQUEST["sti_gst_mode"]."' ,
					'".$_REQUEST["sti_gst_code"]."' ,
					'".$_REQUEST["sti_gst_percentage"]."' ,
					
					'".$_REQUEST["sti_remark"]."' ,
					'".$_REQUEST["sti_status"]."' ,
					
					'".abs($_REQUEST["sti_before_gst_amount"])."' ,
					'".abs($_REQUEST["sti_gst_amount"])."' ,
					'".abs($_REQUEST["sti_total_amount"])."' ,
					
					 
					'".$_REQUEST["sti_id"]."' ,					
					
					
					'".$_REQUEST["sti_storage_location"]."' ,	
					'".$_REQUEST["sti_storage_location2"]."' ,	
					

					'".$_REQUEST["created_date"]."' ,					
					'".$_REQUEST["modified_date"]."' ,
					'".$client_id."' 
					
                    )
                    ON DUPLICATE KEY UPDATE
				
					sti_doc_prefix ='".$_REQUEST["sti_doc_prefix"]."',
					sti_module='".$_REQUEST["sti_module"]."', 
					
					
					sti_warehouse_code='".$_REQUEST["sti_warehouse_code"]."', 
					sti_warehouse_id='".($lib_warehouse[$_REQUEST["sti_warehouse_code"]]+0)."', 
					
					sti_warehouse_code2='".$_REQUEST["sti_warehouse_code2"]."', 
					sti_warehouse_id2='".($lib_warehouse[$_REQUEST["sti_warehouse_code2"]]+0)."', 
					
					sti_stock_code='".$_REQUEST["sti_stock_code"]."', 
					sti_barcode='".$_REQUEST["sti_barcode"]."', 
					sti_factory_code='".$_REQUEST["sti_factory_code"]."', 
					sti_stock_name='".$_REQUEST["sti_stock_name"]."', 
					
					sti_color='".$_REQUEST["sti_color"]."', 
					sti_size='".$_REQUEST["sti_size"]."', 
					sti_pattern='".$_REQUEST["sti_pattern"]."', 
					
					sti_uom='".$_REQUEST["sti_uom"]."', 
					sti_base_uom='".$_REQUEST["sti_base_uom"]."', 
					
					sti_quantity='".abs($_REQUEST["sti_quantity"])."', 
					sti_base_quantity='".abs($_REQUEST["sti_base_quantity"])."', 
					
					sti_conversion='".$_REQUEST["sti_conversion"]."', 
					
					sti_unit_cost='".abs($_REQUEST["sti_unit_cost"])."', 
					sti_base_unit_cost='".abs($_REQUEST["sti_base_unit_cost"])."', 
					sti_total_cost='".abs($_REQUEST["sti_total_cost"])."', 
					
					sti_gst_mode='".$_REQUEST["sti_gst_mode"]."', 
					sti_gst_code='".$_REQUEST["sti_gst_code"]."', 
					sti_gst_percentage='".$_REQUEST["sti_gst_percentage"]."', 
					
					sti_remark='".$_REQUEST["sti_remark"]."', 
					sti_status='".$_REQUEST["sti_status"]."', 
					
					sti_before_gst_amount='".abs($_REQUEST["sti_before_gst_amount"])."', 
					sti_gst_amount='".abs($_REQUEST["sti_gst_amount"])."', 
					sti_total_amount='".abs($_REQUEST["sti_total_amount"])."', 
					
					
					sti_storage_location='".$_REQUEST["sti_storage_location"]."', 
					sti_storage_location2='".$_REQUEST["sti_storage_location2"]."', 
					

					created_date ='".$_REQUEST["created_date"]."',					
					modified_date ='".$_REQUEST["modified_date"]."',
					client_id = '".$client_id."' 
					
                    ;
                    ";
     $result_sync = mysqli_query($dbconn,$query_sync) or die("Error ".mysqli_error($dbconn));
    
          
			
			
			
			
	$query_refresh="update t_".$key_table_prefix."_stock_transaction_item i
			set sti_header_id = (select sth_id 
							from t_".$key_table_prefix."_stock_transaction_header h
							where sth_doc_no = '".$_REQUEST["sti_doc_no"]."'
							and  sth_date = '".$_REQUEST["sti_date"]."'
							and  h.device_id = '".$_REQUEST["device_id"]."'
							) 
			where sti_doc_no ='".$_REQUEST["sti_doc_no"]."' 
			and sti_date = '".$_REQUEST["sti_date"]."'
			and i.device_id = '".$_REQUEST["device_id"]."'
			";
			
	$result_refresh = mysqli_query($dbconn,$query_refresh) or die("Error ".mysqli_error($dbconn));
			                     
echo "<upload>
";
echo "<status>SUCCESS</status>
";
echo "<message>".str_replace("&","&amp;","Upload Successful")."</message>
";
echo "</upload>
";

         


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