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





$query_login="select * from t_".$key_table_prefix."_user
	    where usr_name = '".$_REQUEST["u"]."'
	    and usr_password = '".$_REQUEST["p"]."'
	    and usr_status_id = '1'
	    ";
$result_login= mysqli_query($dbconn,$query_login);
if($line_login = mysqli_fetch_array($result_login,MYSQLI_ASSOC))
{










//===========================Device User================================================
$query_data="select *
                from t_".$key_table_prefix."_user_device

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{


	echo "<user>";
	echo "<name>".str_replace("&","&amp;",$line_data["dusr_name"])."</name>";
	echo "<full_name>".str_replace("&","&amp;",$line_data["dusr_full_name"])."</full_name>";
	echo "<password>".str_replace("&","&amp;",$line_data["dusr_password"])."</password>";
	echo "<group>".str_replace("&","&amp;",$line_data["dusr_group"])."</group>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
	echo "</user>";

}






//get balance base on Device:
$this_device_warehouse="";
$this_device_region_id=0;
$this_device_sync_server_company_and_address="NO";

if($_REQUEST["d"]!="")
{



	$query_device="select * from
				t_".$key_table_prefix."_device
				where dev_android_id = '".$_REQUEST["d"]."'
				";
	$result_device  = mysqli_query($dbconn,$query_device);
	if($line_device = mysqli_fetch_array($result_device,MYSQLI_ASSOC))
	{

		$this_device_warehouse = $line_device["dev_default_warehouse_code"];
		$this_device_route_code = $line_device["dev_route_code"];
		$this_device_sync_server_company_and_address = $line_device["dev_sync_company_info"];





		$query_balance="select
					sum(bal_quantity) as total_balance,
					bal_product_code

	 				from t_".$key_table_prefix."_balance
					where bal_warehouse_code = '".$this_device_warehouse."'
					group by bal_product_code;
					";
		$result_balance = mysqli_query($dbconn,$query_balance);
		while($line_balance =  mysqli_fetch_array($result_balance,MYSQLI_ASSOC))
		{

			$stock_balance[$line_balance["bal_product_code"]] = $line_balance["total_balance"];


		}



		$this_device_region_id = $line_device["dev_price_region_id"];



	}






}





//===========================System Setting================================================
$sys_customer_visibility_mode="STANDARD";

$query_data="select *
                from t_".$key_table_prefix."_setting
                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
if($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{
	$sys_customer_visibility_mode = $line_data["set_customer_visibility"];




echo "<system_setting>";


	echo "<use_server_company>".str_replace("&","&amp;",$this_device_sync_server_company_and_address)."</use_server_company>";


	echo "<company_name>".str_replace("&","&amp;",$line_data["set_company_name"])."</company_name>";
	echo "<company_address>".str_replace("&","&amp;",$line_data["set_address"])."</company_address>";
	echo "<cus_visibility>".str_replace("&","&amp;",$line_data["set_customer_visibility"])."</cus_visibility>";
	echo "<default_route_code>".str_replace("&","&amp;",$this_device_route_code)."</default_route_code>";




	echo "<allow_negative_balance>YES</allow_negative_balance>";




echo "</system_setting>";


}







unset($region_price);

if($this_device_region_id>0)
{
	//get all region price:


	$query_data="select *
	                from t_".$key_table_prefix."_product_region_price
				 where rpr_region_id = '".$this_device_region_id."'
	                 ";

				  //where client_id = '".$client_id."'
	$result_data = mysqli_query($dbconn,$query_data);
	while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
	{

		$region_price[$line_data["rpr_product_id"]] = $line_data["rpr_price"]+0;
	}



}



//===========================Product================================================
$query_data="select *
                from t_".$key_table_prefix."_product


									where ifnull(modified_date,'2000-01-01 00:00:00') >= '".date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-30,date("Y")))."'
                 ";



			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

    $product_data[$line_data["pdt_id"]] = $line_data["pdt_code"];

echo "<product>";
	echo "<code>".str_replace("&","&amp;",$line_data["pdt_code"])."</code>";
	echo "<barcode>".str_replace("&","&amp;",$line_data["pdt_barcode"])."</barcode>";
	echo "<category_code>".str_replace("&","&amp;",$line_data["pdt_category_code"])."</category_code>";
	echo "<menu_group_code>".str_replace("&","&amp;",$line_data["pdt_menu_group_code"])."</menu_group_code>";
	echo "<name>".str_replace("&","&amp;",$line_data["pdt_name"])."</name>";
	echo "<uom>".str_replace("&","&amp;",$line_data["pdt_uom"])."</uom>";
	if(isset($region_price[$line_data["pdt_id"]]))
	{
		echo "<price>".str_replace("&","&amp;",$region_price[$line_data["pdt_id"]])."</price>";
	}
	else {
		echo "<price>".str_replace("&","&amp;",$line_data["pdt_price"])."</price>";	# code...
	}

	echo "<price_2>".str_replace("&","&amp;",$line_data["pdt_price_2"])."</price_2>";
	echo "<price_3>".str_replace("&","&amp;",$line_data["pdt_price_3"])."</price_3>";
	echo "<price_4>".str_replace("&","&amp;",$line_data["pdt_price_4"])."</price_4>";
	echo "<price_5>".str_replace("&","&amp;",$line_data["pdt_price_5"])."</price_5>";
	echo "<discount_allow>".str_replace("&","&amp;",$line_data["pdt_discount_allow"])."</discount_allow>";
	echo "<cost>".str_replace("&","&amp;",$line_data["pdt_cost"])."</cost>";
	echo "<price_group1>".str_replace("&","&amp;",$line_data["pdt_price_group_1"])."</price_group1>";
	echo "<price_group2>".str_replace("&","&amp;",$line_data["pdt_price_group_2"])."</price_group2>";
	echo "<price_group3>".str_replace("&","&amp;",$line_data["pdt_price_group_3"])."</price_group3>";
	echo "<price_group4>".str_replace("&","&amp;",$line_data["pdt_price_group_4"])."</price_group4>";
	echo "<price_group5>".str_replace("&","&amp;",$line_data["pdt_price_group_5"])."</price_group5>";

	echo "<scale_price_1>".str_replace("&","&amp;",$line_data["pdt_scale_price_1"])."</scale_price_1>";
	echo "<scale_price_2>".str_replace("&","&amp;",$line_data["pdt_scale_price_2"])."</scale_price_2>";
	echo "<scale_price_3>".str_replace("&","&amp;",$line_data["pdt_scale_price_3"])."</scale_price_3>";
	echo "<scale_price_4>".str_replace("&","&amp;",$line_data["pdt_scale_price_4"])."</scale_price_4>";
	echo "<scale_price_5>".str_replace("&","&amp;",$line_data["pdt_scale_price_5"])."</scale_price_5>";


	echo "<scale_price_1_min_qty>".str_replace("&","&amp;",$line_data["pdt_scale_price_1_min_qty"])."</scale_price_1_min_qty>";
	echo "<scale_price_2_min_qty>".str_replace("&","&amp;",$line_data["pdt_scale_price_2_min_qty"])."</scale_price_2_min_qty>";
	echo "<scale_price_3_min_qty>".str_replace("&","&amp;",$line_data["pdt_scale_price_3_min_qty"])."</scale_price_3_min_qty>";
	echo "<scale_price_4_min_qty>".str_replace("&","&amp;",$line_data["pdt_scale_price_4_min_qty"])."</scale_price_4_min_qty>";
	echo "<scale_price_5_min_qty>".str_replace("&","&amp;",$line_data["pdt_scale_price_5_min_qty"])."</scale_price_5_min_qty>";

	echo "<gst_code>".str_replace("&","&amp;",$line_data["pdt_gst_code"])."</gst_code>";

	echo "<gst_mode>".str_replace("&","&amp;",$line_data["pdt_gst_mode"])."</gst_mode>";

	if($line_data["pdt_inventory_tracking"]=="YES")
	{
		if(isset($stock_balance[$line_data["pdt_code"]]))
		{
			echo "<balance>".str_replace("&","&amp;",$stock_balance[$line_data["pdt_code"]])."</balance>";

		}
		else {
			echo "<balance>0</balance>";
		}

	}
	else {
		echo "<balance>0</balance>";
	}

	echo "<supplier>".str_replace("&","&amp;",$line_data["pdt_supplier"])."</supplier>";
	echo "<brand>".str_replace("&","&amp;",$line_data["pdt_brand"])."</brand>";
	echo "<select_color>".str_replace("&","&amp;",$line_data["pdt_select_color"])."</select_color>";
	echo "<select_size>".str_replace("&","&amp;",$line_data["pdt_select_size"])."</select_size>";
	echo "<select_pattern>".str_replace("&","&amp;",$line_data["pdt_select_pattern"])."</select_pattern>";

	echo "<featured>".str_replace("&","&amp;",$line_data["pdt_featured"])."</featured>";
	echo "<decimal_quantity>".str_replace("&","&amp;",$line_data["pdt_decimal_quantity"])."</decimal_quantity>";

	echo "<category_1>".str_replace("&","&amp;",$line_data["pdt_category_1"])."</category_1>";
	echo "<category_2>".str_replace("&","&amp;",$line_data["pdt_category_2"])."</category_2>";
	echo "<category_3>".str_replace("&","&amp;",$line_data["pdt_category_3"])."</category_3>";
	echo "<status>".str_replace("&","&amp;",$line_data["pdt_status"])."</status>";

	echo "<inventory_tracking>".str_replace("&","&amp;",$line_data["pdt_inventory_tracking"])."</inventory_tracking>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";


echo "</product>
";


}





//===========================Addon================================================
$query_data="select *
                from t_".$key_table_prefix."_addon

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

$addon_data[$line_data["add_id"]] = $line_data["add_code"];

echo "<addon>";
	echo "<code>".str_replace("&","&amp;",$line_data["add_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["add_name"])."</name>";
	echo "<uom>".str_replace("&","&amp;",$line_data["add_uom"])."</uom>";
	echo "<price>".str_replace("&","&amp;",$line_data["add_price"])."</price>";
	echo "<price_2>".str_replace("&","&amp;",$line_data["add_price_2"])."</price_2>";
	echo "<price_3>".str_replace("&","&amp;",$line_data["add_price_3"])."</price_3>";
	echo "<price_4>".str_replace("&","&amp;",$line_data["add_price_4"])."</price_4>";
	echo "<price_5>".str_replace("&","&amp;",$line_data["add_price_5"])."</price_5>";
	echo "<cost>".str_replace("&","&amp;",$line_data["add_cost"])."</cost>";
	echo "<gst_code>".str_replace("&","&amp;",$line_data["add_gst_code"])."</gst_code>";
	echo "<balance>".str_replace("&","&amp;",$line_data["add_balance"])."</balance>";
	echo "<status>".str_replace("&","&amp;",$line_data["add_status"])."</status>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</addon>";

}










//===========================Addon Matrix================================================
$query_data="select *
                from t_".$key_table_prefix."_addon_assignment

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{
	echo "<addon_matrix>";
	echo "<addon_code>".str_replace("&","&amp;",$addon_data[$line_data["asg_addon_id"]])."</addon_code>";
	echo "<product_code>".str_replace("&","&amp;",$product_data[$line_data["asg_product_id"]])."</product_code>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
	echo "</addon_matrix>";

}




//===========================Category================================================
$query_data="select *
                from t_".$key_table_prefix."_category

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{


echo "<category>";
	echo "<code>".str_replace("&","&amp;",$line_data["cat_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["cat_name"])."</name>";
	echo "<sorting>".str_replace("&","&amp;",$line_data["cat_sorting"])."</sorting>";
	echo "<status>".str_replace("&","&amp;",$line_data["cat_status"])."</status>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</category>";


}







//===========================Color================================================
$query_data="select *
                from t_".$key_table_prefix."_color

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

echo "<color>";
	echo "<code>".str_replace("&","&amp;",$line_data["clr_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["clr_name"])."</name>";
	echo "<status>".str_replace("&","&amp;",$line_data["clr_status"])."</status>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</color>";

}



//===========================Size================================================
$query_data="select *
                from t_".$key_table_prefix."_size

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

echo "<size>";
	echo "<code>".str_replace("&","&amp;",$line_data["siz_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["siz_name"])."</name>";
	echo "<status>".str_replace("&","&amp;",$line_data["siz_status"])."</status>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</size>";

}






//===========================Pattern================================================
$query_data="select *
                from t_".$key_table_prefix."_pattern

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

echo "<pattern>";
	echo "<code>".str_replace("&","&amp;",$line_data["ptn_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["ptn_name"])."</name>";
	echo "<status>".str_replace("&","&amp;",$line_data["ptn_status"])."</status>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</pattern>";


}





//===========================Customer================================================

if($sys_customer_visibility_mode=="SEARCHABLE")
{

	$query_data="select *
	                from t_".$key_table_prefix."_customer
	                 ";

}
else {


	if($sys_customer_visibility_mode=="ROUTE")
	{

		$query_data="select *
		                from t_".$key_table_prefix."_customer
		                 ";

		if($this_device_route_code!="")
		{
			$query_data=$query_data." where cus_route_code ='".$this_device_route_code."' ";
		}
	}
	else {

		//standard
		$query_data="select *
		                from t_".$key_table_prefix."_customer
		                 ";

	}



}

			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

$lib_customer_code[$line_data["cus_id"]] = $line_data["cus_code"];
$line_data["cus_name"]= str_replace("'","`",$line_data["cus_name"]);
$line_data["cus_address_billing"]= str_replace("'","`",$line_data["cus_address_billing"]);
$line_data["cus_address_delivery"]= str_replace("'","`",$line_data["cus_address_delivery"]);

echo "<customer>";
echo "<code>".str_replace("&","&amp;",$line_data["cus_code"])."</code>";
echo "<name>".str_replace("&","&amp;",$line_data["cus_name"])."</name>";
echo "<gst_no>".str_replace("&","&amp;",$line_data["cus_gst_no"])."</gst_no>";
echo "<ic_no>".str_replace("&","&amp;",$line_data["cus_ic_no"])."</ic_no>";
echo "<group>".str_replace("&","&amp;",$line_data["cus_group"])."</group>";
echo "<discount_percentage>".str_replace("&","&amp;",$line_data["cus_discount_percentage"])."</discount_percentage>";
echo "<address_billing>".str_replace("&","&amp;",$line_data["cus_address_billing"])."</address_billing>";

echo "<address_delivery>".str_replace("&","&amp;",$line_data["cus_address_delivery"])."</address_delivery>";
echo "<latitude>".str_replace("&","&amp;",$line_data["cus_latitude"])."</latitude>";
echo "<longitude>".str_replace("&","&amp;",$line_data["cus_longitude"])."</longitude>";
echo "<credit_block>".str_replace("&","&amp;",$line_data["cus_credit_block"])."</credit_block>";
echo "<route_code>".str_replace("&","&amp;",$line_data["cus_route_code"])."</route_code>";
echo "<status>".str_replace("&","&amp;",$line_data["cus_status"])."</status>";
echo "<validity_start>".str_replace("&","&amp;",$line_data["cus_validity_start"])."</validity_start>";
echo "<validity_end>".str_replace("&","&amp;",$line_data["cus_validity_end"])."</validity_end>";
echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</customer>";


}







//===========================Customer Pricing================================================
$query_data="select *
                from t_".$key_table_prefix."_customer_price
                 ";
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

echo "<customer_price>";
	echo "<code>".str_replace("&","&amp;",$lib_customer_code[$line_data["cpr_customer_code"]])."</code>";
	echo "<pdt_code>".str_replace("&","&amp;",$line_data["cpr_product_code"])."</pdt_code>";
	echo "<pdt_price>".str_replace("&","&amp;",$line_data["cpr_price"])."</pdt_price>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</customer_price>";

}











//===========================GST================================================
$query_data="select *
                from t_".$key_table_prefix."_gst

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

echo "<gst>";
	echo "<code>".str_replace("&","&amp;",$line_data["gst_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["gst_name"])."</name>";
	echo "<percentage>".str_replace("&","&amp;",$line_data["gst_percentage"])."</percentage>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</gst>";


}








//===========================promo================================================
$query_data="select *
                from t_".$key_table_prefix."_promo
                where pro_start_date <='".date("Y-m-d")."'
			 and pro_end_date >='".date("Y-m-d")."'
                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

	$barcode_array = explode(chr(10),$line_data["pro_barcode_list"]);
	foreach($barcode_array as $promo_key=>$promo_value)
	{

		if($promo_value!="")
		{


echo "<promo>";
	echo "<code>".str_replace("&","&amp;",$line_data["pro_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["pro_name"])."</name>";
	echo "<start_date>".str_replace("&","&amp;",$line_data["pro_start_date"])."</start_date>";
	echo "<end_date>".str_replace("&","&amp;",$line_data["pro_end_date"])."</end_date>";
	echo "<barcode_list>".str_replace("&","&amp;",$promo_value)."</barcode_list>";
	echo "<discount_percentage>".str_replace("&","&amp;",$line_data["pro_discount_percentage"])."</discount_percentage>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";

echo "</promo>";




		}

	}





}





//===========================Payment Mode================================================
$query_data="select *
                from t_".$key_table_prefix."_payment_mode

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{

echo "<payment>";
	echo "<code>".str_replace("&","&amp;",$line_data["pym_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["pym_name"])."</name>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</payment>";

}






//===========================Staff Profile===============================================
$query_data="select *
                from t_".$key_table_prefix."_staff

                 ";
			  //where client_id = '".$client_id."'
$result_data = mysqli_query($dbconn,$query_data);
while($line_data= mysqli_fetch_array($result_data,MYSQLI_ASSOC))
{


echo "<staff>";
	echo "<code>".str_replace("&","&amp;",$line_data["stf_code"])."</code>";
	echo "<name>".str_replace("&","&amp;",$line_data["stf_name"])."</name>";
	echo "<ic_no>".str_replace("&","&amp;",$line_data["stf_ic_no"])."</ic_no>";
	echo "<birth_date>".str_replace("&","&amp;",$line_data["stf_birth_date"])."</birth_date>";
	echo "<email>".str_replace("&","&amp;",$line_data["stf_email"])."</email>";
	echo "<mobile_no>".str_replace("&","&amp;",$line_data["stf_mobile_no"])."</mobile_no>";
	echo "<status>".str_replace("&","&amp;",$line_data["stf_status"])."</status>";
	echo "<created_date>".str_replace("&","&amp;",$line_data["created_date"])."</created_date>";
	echo "<modified_date>".str_replace("&","&amp;",$line_data["modified_date"])."</modified_date>";
echo "</staff>";


}











//----------------------login------------------------

}
else {

    $login_status="FAIL";
    $message="Invalid Login";


}



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