<?php
header ("Content-Type:text/xml");
include "start.php";
echo "<psync>";
//---------------------------------------------------------------------------------------------------------
  
//===========================Product================================================  
  
  
  
  	$storage_location="";
  
  	//get product storage location:
	$query_product="select * from t_".$key_table_prefix."_product 
				where pdt_code = '".$_REQUEST["ivi_product_code"]."' ";
  	$result_product = mysqli_query($dbconn, $query_product);
	if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
	{
		
		$storage_location=$line_product["pdt_storage_location"];
	}
	
  
    $query_sync="INSERT INTO
                    t_".$key_table_prefix."_invoice_item
                    (
					
					
			 		ivi_ivi_id,
					device_id, 
					
			 		ivi_doc_no ,
			 		ivi_product_code, 
			 		ivi_product_barcode, 
			 		ivi_product_name,
			 		ivi_serial_no, 
			 		ivi_remark,

			 		ivi_price,  
			 		ivi_cost, 
			 		ivi_quantity,
			 		ivi_uom,
					
			 		ivi_attribute,  
			 		ivi_size,
					ivi_color,
					ivi_pattern,
					
			 		ivi_total_addon,

			 		ivi_discount_percentage,
			 		ivi_discount_value,
		 			ivi_discount_total,  
			 		ivi_total_before_gst, 

			 		ivi_gst_mode, 
			 		ivi_gst_code,  
			 		ivi_gst_percentage, 
			 		ivi_gst_amount,  

			 		ivi_total_amount,  
			 		ivi_status,
					
					ivi_salesperson,

					ivi_storage_location,
					
					created_date ,					
					modified_date ,
					client_id 
                    )
                    VALUES
                    (
	                    
					'".$_REQUEST["ivi_id"]."',
					'".$_REQUEST["device_id"]."', 
					
			 		'".$_REQUEST["ivi_doc_no"]."' ,
			 		'".$_REQUEST["ivi_product_code"]."', 
			 		'".$_REQUEST["ivi_product_barcode"]."', 
			 		'".$_REQUEST["ivi_product_name"]."',
			 		'".$_REQUEST["ivi_serial_no"]."', 
			 		'".$_REQUEST["ivi_remark"]."',

			 		'".($_REQUEST["ivi_price"]+0)."',  
			 		'".($_REQUEST["ivi_cost"]+0)."', 
			 		'".($_REQUEST["ivi_quantity"]+0)."',
			 		'".$_REQUEST["ivi_uom"]."',
					
			 		'".$_REQUEST["ivi_attribute"]."',  
			 		'".$_REQUEST["ivi_size"]."',
					'".$_REQUEST["ivi_color"]."',
					'".$_REQUEST["ivi_pattern"]."',
					
					
			 		'".($_REQUEST["ivi_total_addon"]+0)."',

			 		'".($_REQUEST["ivi_discount_percentage"]+0)."',
			 		'".($_REQUEST["ivi_discount_value"]+0)."',
		 			'".($_REQUEST["ivi_discount_total"]+0)."',  
			 		'".($_REQUEST["ivi_total_before_gst"]+0)."', 

			 		'".$_REQUEST["ivi_gst_mode"]."', 
			 		'".$_REQUEST["ivi_gst_code"]."',  
			 		'".($_REQUEST["ivi_gst_percentage"]+0)."', 
			 		'".($_REQUEST["ivi_gst_amount"]+0)."',  

			 		'".($_REQUEST["ivi_total_amount"]+0)."',  
			 		'".$_REQUEST["ivi_status"]."',
					
					'".$_REQUEST["ivi_salesperson"]."',
					'".$storage_location."',

					'".$_REQUEST["created_date"]."' ,					
					'".$_REQUEST["modified_date"]."' ,
					'".$client_id."' 
					
                    )
                    ON DUPLICATE KEY UPDATE
				
					ivi_doc_no ='".$_REQUEST["ivi_doc_no"]."',
					ivi_product_code='".$_REQUEST["ivi_product_code"]."', 
					ivi_product_barcode='".$_REQUEST["ivi_product_barcode"]."', 
					ivi_product_name='".$_REQUEST["ivi_product_name"]."',
					ivi_serial_no='".$_REQUEST["ivi_serial_no"]."', 
					ivi_remark='".$_REQUEST["ivi_remark"]."',

					ivi_price='".($_REQUEST["ivi_price"]+0)."',  
					ivi_cost='".($_REQUEST["ivi_cost"]+0)."', 
					ivi_quantity='".($_REQUEST["ivi_quantity"]+0)."',
					ivi_uom='".$_REQUEST["ivi_uom"]."',
					ivi_attribute='".$_REQUEST["ivi_attribute"]."',  
					ivi_size='".$_REQUEST["ivi_size"]."',
					ivi_color='".$_REQUEST["ivi_color"]."',
					ivi_pattern='".$_REQUEST["ivi_pattern"]."',
					ivi_total_addon='".$_REQUEST["ivi_total_addon"]."',

					ivi_discount_percentage='".($_REQUEST["ivi_discount_percentage"]+0)."',
					ivi_discount_value='".($_REQUEST["ivi_discount_value"]+0)."',
					ivi_discount_total='".($_REQUEST["ivi_discount_total"]+0)."',  
					ivi_total_before_gst='".($_REQUEST["ivi_total_before_gst"]+0)."', 

					ivi_gst_mode='".$_REQUEST["ivi_gst_mode"]."', 
					ivi_gst_code='".$_REQUEST["ivi_gst_code"]."',  
					ivi_gst_percentage='".($_REQUEST["ivi_gst_percentage"]+0)."', 
					ivi_gst_amount='".($_REQUEST["ivi_gst_amount"]+0)."',  

					ivi_total_amount='".($_REQUEST["ivi_total_amount"]+0)."',  
					ivi_status='".$_REQUEST["ivi_status"]."',
					
					ivi_salesperson='".$_REQUEST["ivi_salesperson"]."',
					ivi_storage_location='".$storage_location."',
					
					

					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."_invoice_item i
			set ivi_header_id = (select ivh_id 
							from t_".$key_table_prefix."_invoice_header h
							where ivh_doc_no = '".$_REQUEST["ivi_doc_no"]."'
							and  h.device_id = '".$_REQUEST["device_id"]."'
							) 
			where ivi_doc_no ='".$_REQUEST["ivi_doc_no"]."' and i.device_id = '".$_REQUEST["device_id"]."'
			";
			
	$result_refresh = mysqli_query($dbconn,$query_refresh) or die("Error ".mysqli_error($dbconn));
		
		
		
		
		
		
		
		
		
		
		
		
	//if service module is turn on    
	if($sys_system_master_setting_line["set_module_service"]=="YES")
	{
		
		
		if(trim($_REQUEST["ivi_serial_no"])!="")
		{
			
			
			$query_product_info = "select * from t_".$key_table_prefix."_product 
							where pdt_code = '".$_REQUEST["ivi_product_code"]."' ";
			$result_product_info = mysqli_query($dbconn,$query_product_info);
			if($line_product_info = mysqli_fetch_array($result_product_info,MYSQLI_ASSOC))
			{
				$this_product_id = $line_product_info["pdt_id"];
				$this_product_name = $line_product_info["pdt_name"];
			}
			
			/*
			$query_customer_info = "select * from t_".$key_table_prefix."_customer
							where cus_code = '".$_REQUEST["svi_customer_code"]."' ";
			$result_customer_info = mysqli_query($dbconn,$query_customer_info);
			if($line_customer_info = mysqli_fetch_array($result_customer_info,MYSQLI_ASSOC))
			{
				$this_customer_name = $line_customer_info["cus_name"];
				
			}
			*/
			
			
			
			
			
			//get header 
			$query_item_info = "select * from t_".$key_table_prefix."_invoice_item
							where ivi_doc_no ='".$_REQUEST["ivi_doc_no"]."' and device_id = '".$_REQUEST["device_id"]."' ";
			$result_item_info = mysqli_query($dbconn,$query_item_info);
			if($line_item_info = mysqli_fetch_array($result_item_info,MYSQLI_ASSOC))
			{
				
				
				$this_ivi_id = $line_item_info["ivi_id"];
				$this_ivh_id = $line_item_info["ivi_header_id"];
				
				
				
				//get header 
				$query_header_info = "select * from t_".$key_table_prefix."_invoice_header
								where ivh_id = '".$this_ivh_id."' ";
				$result_header_info = mysqli_query($dbconn,$query_header_info);
				if($line_header_info = mysqli_fetch_array($result_header_info,MYSQLI_ASSOC))
				{
					$this_date_issued = $line_header_info["ivh_date"];
					$this_customer_code = $line_header_info["ivh_customer_code"];
					$this_customer_name = $line_header_info["ivh_customer_name"];
					
				}
				
				
			}
		
			
			
			
			$query_insert_service_item = "insert into t_".$key_table_prefix."_service_item 	
									(
									svi_product_id, 
									svi_customer_code, 
									svi_customer_name, 
									
									svi_serial_no,
									
									svi_product_code, 
									svi_product_name,
									
									svi_product_size,
									svi_product_color,
									svi_product_pattern,
									
									svi_ivh_id, 
									svi_ivi_id, 
									
									svi_date_issued,
									
									created_date,
									modified_date,
									client_id 
									
									)
									values
									(
									'".($this_product_id+0)."',
									'".$this_customer_code."',	
									'".$this_customer_name."',
									
									'".$_REQUEST["ivi_serial_no"]."',
									
									'".$_REQUEST["ivi_product_code"]."',
									'".$this_product_name."'	,
									
									
									'".$_REQUEST["ivi_size"]."'	,
									'".$_REQUEST["ivi_color"]."'	,
									'".$_REQUEST["ivi_pattern"]."',	
									
									'".$this_ivh_id."',
									'".$this_ivi_id."',
									
									'".$this_date_issued."',
									
									'".date("Y-m-d H:i:s")."',
									'".date("Y-m-d H:i:s")."',
									'".$client_id."'
										
									)
								
									ON DUPLICATE KEY UPDATE
									
									
									svi_customer_code = '".$this_customer_code."', 
									svi_customer_name = '".$this_customer_name."', 
									
									svi_product_code = '".$_REQUEST["ivi_product_code"]."',
									svi_product_name = '".$this_product_name."'	,
									
									svi_product_size = '".$_REQUEST["ivi_size"]."'	,
									svi_product_color =  '".$_REQUEST["ivi_color"]."'	,
									svi_product_pattern = '".$_REQUEST["ivi_pattern"]."'	,
									
									svi_ivh_id = '".$this_ivh_id."',
									svi_ivi_id = '".$this_ivi_id."', 
									
									svi_date_issued = '".$this_date_issued."', 
									
									
									modified_date = '".date("Y-m-d H:i:s")."'
							
							";
				
			$result_insert_service_item  = mysqli_query($dbconn,$query_insert_service_item) or die("Error".mysqli_error());
		 	
		
			
		
		
		
		
		
		
		
		
			
			
			
			
		}
		
		
		
		
		
		
		
		
		
	}					    
						    
						    
						    
						    
						    
						    
						    
						    
						      
echo "<upload>
";
echo "<status>SUCCESS</status>
";
echo "<message>".str_replace("&","&amp;","Upload Successful")."</message>
";
echo "</upload>
";

         


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