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

if($_REQUEST["created_date"]=="")
{
$_REQUEST["created_date"] = date("Y-m-d H:i:s");
}                               

if($_REQUEST["modified_date"]=="")
{
$_REQUEST["modified_date"] = date("Y-m-d H:i:s");
}      




	$allow_upload="NO";
	   
	
    $query_setting="select * from t_".$key_table_prefix."_setting  ";
    $result_setting = mysqli_query($dbconn, $query_setting);
    if($line_setting = mysqli_fetch_array($result_setting,MYSQLI_ASSOC))
    {
	    
	    $allow_upload = $line_setting["set_sync_customer_upload"];
    } 
	   
	if($allow_upload=="YES")
	{
		
		
		
			if($_REQUEST["code"]!="")        
		    	 {
		    		 
				 

		    		$new_entry=false;
		    		 
		    		 $this_customer_validity_start="";
		    		 $this_customer_validity_end="";
				 
				
				/* 
				 if($_REQUEST["validity_start"]=="")
				 {
					$_REQUEST["validity_start"] = date("Y-m-d");
				 }
				 
				 if($_REQUEST["validity_end"]=="")
				 {
					$_REQUEST["validity_end"] = date("Y-m-d");
				 }
				 
				 */
				 
				 
				 
				 
				 
		    		$last_modified = "0000-00-00 00:00:00";
		    		 
		    		$query_check="select * from t_".$key_table_prefix."_customer
		    		 		where cus_code = '".$_REQUEST["code"]."' ";
		    		$result_check = mysqli_query($dbconn, $query_check);
		    		if($line_check = mysqli_fetch_array($result_check,MYSQLI_ASSOC))
		    		{
		    			if($line_check["modified_date"]!="")
		    			{
		    				$last_modified = $line_check["modified_date"];	
		    			}
		    			else {
		    				
		    			}
					
					
					
					
		    			
		    			$this_customer_validity_start = $line_check["cus_validity_start"];
					$this_customer_validity_end= $line_check["cus_validity_end"];
		    			
		    		}
		    		else {
		    		
				
				
				

					$this_customer_validity_start="0000-00-00";
					$this_customer_validity_end= "0000-00-00";
					
		    			$new_entry=true;
		    		}
		    		
		    		 
				 
				 
		    		 
		    		 //only update to server if the device version is latest then server version:
		    		 
				 
		    					if(($_REQUEST["modified_date"] > $last_modified)or($new_entry)or($_REQUEST["overwrite"]=="YES"))
		    					{
		    						
		    					
							
								if($_REQUEST["overwrite"]=="YES")
								{
									//follow everything from APP:
									
								}
								else {
									
									
									if($this_customer_validity_start>$_REQUEST["validity_start"])
									{
										
										
										//refer back to server:(dont use app`s value)
										$_REQUEST["validity_start"] = $this_customer_validity_start;
										$_REQUEST["validity_end"] = $this_customer_validity_end;
										
									}
								}
							
							
			    					
								 
								 
								 
							 
							 
							 
							 
		    					 
		    					 $query_sync="INSERT INTO
		    				 				 t_".$key_table_prefix."_customer
		    				 				 (
		    				 				 cus_code,
		    				 				 cus_name,
		    				 				 
		    				 				 cus_gst_no,
		    				 				 cus_group,
		    				 				 
		    				 				 
		    				 				 cus_status,
		    				 				 
		    				 				 cus_credit_block,
		    				 				 
		    				 				 cus_discount_percentage,
		    				 				 cus_address_billing,
		    				 				 cus_address_delivery,
		    				 				 
		    				 				 
		    				 				 cus_mobile_no,
		    				 				 cus_telephone_no,
		    				 				 cus_fax_no,
		    				 				 cus_email,
		    				 				 cus_ic_no,
		    				 				 
		    				 				 
		    				 				 cus_validity_start,
		    				 				 cus_validity_end,
		    				 				 
		    				 				 created_date,
		    				 				 modified_date,
		    				 				 
		    				 				 client_id
		    				 				 )
		    				 				 VALUES
		    				 				 (
		    				 				 '".$_REQUEST["code"]."',
		    				 				 '".$_REQUEST["name"]."',
		    				 				 
		    				 				 '".$_REQUEST["gst_no"]."',
		    				 				 '".$_REQUEST["group"]."',
		    				 				 
		    				 				 '".$_REQUEST["status"]."',
		    				 				 
		    				 				 '".$_REQUEST["credit_block"]."',
		    				 				 
		    				 				 '".$_REQUEST["discount_percentage"]."',
		    				 				 '".$_REQUEST["address_billing"]."',
		    				 				 '".$_REQUEST["address_delivery"]."',
		    				 				 
		    				 				 '".$_REQUEST["mobile_no"]."',
		    				 				 '".$_REQUEST["telephone_no"]."',
		    				 				 '".$_REQUEST["fax_no"]."',
		    				 				 '".$_REQUEST["email"]."',
		    				 				 '".$_REQUEST["ic_no"]."',
		    				 				 
		    				 				 ";
								if($_REQUEST["validity_start"]=="")
			     				{	 
									$query_sync=$query_sync." null," ;
								}
								else {
									$query_sync=$query_sync." '".$_REQUEST["validity_start"]."'," ;
								}
										 
								if($_REQUEST["validity_end"]=="")
								{ 
									$query_sync=$query_sync." null ," ; 
								}
								else {
									$query_sync=$query_sync." '".$_REQUEST["validity_end"]."'," ; 
								}
										 
						$query_sync=$query_sync."
		    				 				 
		    				 				 '".$_REQUEST["created_date"]."',
		    				 				 '".$_REQUEST["modified_date"]."',
		    				 				 
		    				 				 '".$client_id."'
		    				 				 )
		    				 				 ON DUPLICATE KEY UPDATE
		    				 				 
		    				 				 cus_name  = '".$_REQUEST["name"]."',
		    				 				 
		    				 				 cus_gst_no  = '".$_REQUEST["gst_no"]."',
		    				 				 cus_group  = '".$_REQUEST["group"]."',
		    				 				 
		    				 				 
		    				 				 cus_status  = '".$_REQUEST["status"]."',
		    				 				 
		    				 				 cus_credit_block = '".$_REQUEST["credit_block"]."',
		    				 				 
		    				 				 cus_discount_percentage  = '".$_REQUEST["discount_percentage"]."',
		    				 				 cus_address_billing = '".$_REQUEST["address_billing"]."',
		    				 				 cus_address_delivery = '".$_REQUEST["address_delivery"]."',
		    				 				 
		    				 				 
		    				 				 cus_mobile_no = '".$_REQUEST["mobile_no"]."',
		    				 				 cus_telephone_no = '".$_REQUEST["telephone_no"]."',
		    				 				 cus_fax_no = '".$_REQUEST["fax_no"]."',
		    				 				 cus_email = '".$_REQUEST["email"]."',
		    				 				 cus_ic_no = '".$_REQUEST["ic_no"]."',
		    				 				 
		    				 				 
		    				 				 cus_validity_start = '".$_REQUEST["validity_start"]."',
		    				 				 cus_validity_end = '".$_REQUEST["validity_end"]."',
		    				 				 
		    				 				 
		    				 				 
		    				 				 created_date = '".$_REQUEST["created_date"]."',
		    				 				 modified_date = '".$_REQUEST["modified_date"]."'
		    				 				 
		    				 				 ;
		    				 				 ";
		    				 	$result_sync = mysqli_query($dbconn,$query_sync) or die("Error ".mysqli_error($dbconn));

		    				}
		    				
		    	 		 
		    		 
		    	 }
		    			 
		    	
		    		 /*
		      
		        $query_sync="update
		                        t_".$key_table_prefix."_customer
		                        set  cus_validity_start = '".$_REQUEST["cus_validity_start"]."' , 
		    					cus_validity_end = '".$_REQUEST["cus_validity_end"]."'
		    				
		    				
		    				where cus_code = '".$_REQUEST["cus_code"]."'
		    					
		                        ;
		                        ";
		        $result_sync = mysqli_query($dbconn,$query_sync) or die("Error ".mysqli_error($dbconn));
		        
		         */          
		    			
		    			
		
		
	}
	   
	 
			
			
			                     
echo "<upload>
";
echo "<status>SUCCESS</status>
";
echo "<message>".str_replace("&","&amp;","Upload Successful")."</message>
";
echo "</upload>
";

         


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