<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dataupdate extends CI_Controller {

	function __construct()
    {
        parent::__construct();
        error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
    }

	function updtdblocal2server()
	{
		$response = 0;
		$post_fields = array();
		
		## last updated on
		$newlastupdatedon = date('Y-m-d H:i:s');
		
		#check var
		$lastupdatedon = $this->input->post("lastupdatedon");
		$clientid = $this->input->post("clientid");
		$userid = $this->input->post("userid");
		$outletid = $this->input->post("outletid");	
		
		## tables		
		$users = $this->input->post("users");
		$customers = $this->input->post("customers");
		$dayoff = $this->input->post("dayoff");
		$billing = $this->input->post("billing");
		$billinginfo= $this->input->post("billinginfo");
		$post_fields['users'] = 0;
		$post_fields['customers'] = 0;
		$post_fields['dayoff'] = 0;
		$post_fields['billing'] = 0;
		$post_fields['billinginfo'] = 0;
		if($lastupdatedon!='' && $clientid>0 && $outletid>0 && $userid>0)
		{				
			if(is_array($users)>0)
			{
				$index = 1;
				foreach($users as $userrow)
				{					    
					$data = array(						
						"fld_name" => $userrow['fld_name'],
						"fld_email" => $userrow['fld_email'],
						"fld_mobile" => $userrow['fld_mobile'],
						"fld_userpass" => $userrow['fld_userpass'],
						"fld_updatedby" => $userrow['fld_updatedby'],
						"fld_updatedon" => $userrow['fld_updatedon'],
						"fld_lastlogin" => $userrow['fld_lastlogin'],
						"fld_lastloginip" => $userrow['fld_lastloginip']
					);
					$this->db->select("*");						
					$this->db->where("fld_id",$userrow['fld_id']);
					$results = $this->db->get('tbl_users');						
					if($results->num_rows()>0)
					{	
					   
						$this->db->where("fld_id",$userrow['fld_id']);
						$this->db->update('tbl_users', $data);	
						$response++;
						$post_fields['users'] = $index;
						$index++;
					}

				}
			}
			
			if(is_array($dayoff)>0)
			{	
				$index = 1;
				foreach($dayoff as $dayoffrow)
				{
					$this->db->select("*");		
					$this->db->where("fld_datetime='".$dayoffrow['fld_datetime']."'");
					$this->db->where("fld_clientid",$clientid);
					$this->db->where("fld_outletid",$outletid);
					$this->db->where("fld_userid",$dayoffrow['fld_userid']);
					$results = $this->db->get('tbl_dayoff');
					//echo $this->db->last_query();	exit;
					if($results->num_rows()>0)
					{
						$post_fields['dayoff'] = $index;
						
					}
					else
					{
						$data = array(						
							"fld_outletid" => $dayoffrow['fld_outletid'],
							"fld_clientid" => $dayoffrow['fld_clientid'],
							"fld_userid" => $dayoffrow['fld_userid'],
							"fld_dayoffdate" => $dayoffrow['fld_dayoffdate'],
							"fld_amount" => $dayoffrow['fld_amount'],
							"fld_cash" => $dayoffrow['fld_cash'],														
							"fld_cc" => $dayoffrow['fld_cc'],														
							"fld_upi" => $dayoffrow['fld_upi'],														
							"fld_cash" => $dayoffrow['fld_cash'],
							"fld_nameaccount" => $dayoffrow['fld_nameaccount'],
							"fld_naremarks" => $dayoffrow['fld_naremarks'],					
							"fld_lastbillid" => $dayoffrow['fld_lastbillid'],
							"fld_datetime" => $dayoffrow['fld_datetime'],											
							"fld_del" => $dayoffrow['fld_del'],												
							"fld_updatedby" => $dayoffrow['fld_updatedby'],
							"fld_updatedon" => $dayoffrow['fld_updatedon']
						);
						$this->db->insert('tbl_dayoff', $data);
						$response++;
						$post_fields['dayoff'] = $index;
						
					}
					$index++;
				}
			}
			
			if(is_array($customers)>0)
			{
				$index = 1;
				foreach($customers as $customerrow)
				{
					$data = array(						
						"fld_name" => $customerrow['fld_name'],
						"fld_email" => $customerrow['fld_email'],
						"fld_discounttype" => $customerrow['fld_discounttype'],
						"fld_discount" => $customerrow['fld_discount'],
						"fld_givenname" => $customerrow['fld_givenname'],
						"fld_givenemail" => $customerrow['fld_givenemail'],
						"fld_givenmobile" => $customerrow['fld_givenmobile'],
						"fld_status" => $customerrow['fld_status'],						
						"fld_del" => $customerrow['fld_del'],
						"fld_updatedby" => $customerrow['fld_updatedby'],
						"fld_updatedon" => $customerrow['fld_updatedon']
					);
					$this->db->select("*");		
					$this->db->where("fld_mobile",$customerrow['fld_mobile']);
					$results = $this->db->get('tbl_customers');
					
					if($results->num_rows()>0)
					{							
						$this->db->where("fld_mobile", $customerrow['fld_mobile']);
						$this->db->update('tbl_customers', $data);	
						
					}
					else
					{
						$data["fld_clientid"] =$customerrow['fld_clientid'];
						$data["fld_outletid"] =$customerrow['fld_outletid'];
						$data["fld_userid"] =$customerrow['fld_userid'];
						$data["fld_mobile"] =$customerrow['fld_mobile'];
						$data["fld_datetime"] =$customerrow['fld_datetime'];
						$this->db->insert('tbl_customers', $data);
						$response++;
						
					}
					$post_fields['customers'] = $index;
					$index++;
				}
			}
			
			if(is_array($billing)>0)
			{
				$index = 1;
				$indexinfo = 1;
				foreach($billing as $billrow)
				{

					$data = array(
					"fld_mobile" => $billrow['fld_mobile'],
					"fld_name" => $billrow['fld_name'],
					"fld_otp" => $billrow['fld_otp'],
					"fld_specialdiscount" => $billrow['fld_specialdiscount'],
					"fld_givenname" => $billrow['fld_givenname'],
					"fld_givenemail" => $billrow['fld_givenemail'],
					"fld_givenmobile" => $billrow['fld_givenmobile'],
					"fld_discounttype" => $billrow['fld_discounttype'],
					"fld_discount" => $billrow['fld_discount'],
					"fld_amount" => $billrow['fld_amount'],
					"fld_netamount" => $billrow['fld_netamount'],
					"fld_giftid" => $billrow['fld_giftid'],
					"fld_gprodid" => $billrow['fld_gprodid'],
					"fld_gqty" => $billrow['fld_gqty'],
					"fld_status" => $billrow['fld_status'],
					"fld_remarks" => $billrow['fld_remarks'],
					"fld_cancellation_reason" => $billrow['fld_cancellation_reason'],
					"fld_cancellation_remarks" => $billrow['fld_cancellation_remarks'],
					"fld_cancellationdate" => $billrow['fld_cancellationdate'],
					"fld_cancelrequestby" => $billrow['fld_cancelrequestby'],						
					"fld_del" => $billrow['fld_del'],
					"fld_updatedby" => $billrow['fld_updatedby'],
					"fld_updatedon" => $billrow['fld_updatedon']
					);
					$billingid = 0;
					$this->db->select("*");		
					$this->db->where("fld_billno", $billrow['fld_billno']);
					$this->db->where("fld_clientid",$clientid);
					$this->db->where("fld_outletid",$outletid);
					$results = $this->db->get('tbl_billing');
					//echo $this->db->last_query();	exit;
					if($results->num_rows()>0)
					{
						$row  = $results->row();
						
						$this->db->where("fld_billno", $billrow['fld_billno']);
						$this->db->where("fld_clientid",$clientid);
						$this->db->where("fld_outletid",$outletid);
						$this->db->update('tbl_billing', $data);
						
						$billingid = $row->fld_id;
					}
					else
					{
						$data["fld_clientid"] =$billrow['fld_clientid'];						
						$data["fld_outletid"] =$billrow['fld_outletid'];						
						$data["fld_userid"] =$billrow['fld_userid'];						
						$data["fld_customerid"] =$billrow['fld_customerid'];						
						$data["fld_billno"] =$billrow['fld_billno'];						
						$data["fld_billdate"] =$billrow['fld_billdate'];						
						$data["fld_datetime"] =$billrow['fld_datetime'];						
						$this->db->insert('tbl_billing', $data);
						$billingid = $this->db->insert_id();

						
						
					}
					$response++;
					$post_fields['billing'] = $index;
					$index++;

					## info rows
					if($billingid>0)
					{	
						
						foreach($billinginfo[$billrow['fld_billno']] as $billinforow)
						{
							$this->db->select("fld_id");
							$this->db->where("fld_billingid", $billingid);					
							$this->db->where("fld_productid", $billinforow['fld_productid']);
							$results2 = $this->db->get('tbl_billinginfo');
							if($results2->num_rows()>0) // update if found
							{
								$row2  = $results2->row();
								$data2 = array(						
								"fld_qty" => $billinforow['fld_qty'],
								"fld_price" => $billinforow['fld_price'],
								"fld_mrp" => $billinforow['fld_mrp']
								);
								$this->db->where("fld_id", $row2->fld_id);
								$this->db->update('tbl_billinginfo', $data2);
								$infodbupdtids[] = $row2->fld_id;
							}
							else
							{
								$data2 = array(						
								"fld_billingid" => $billingid,
								"fld_productid" => $billinforow['fld_productid'],
								"fld_qty" => $billinforow['fld_qty'],
								"fld_price" => $billinforow['fld_price'],
								"fld_mrp" => $billinforow['fld_mrp']
								);

								$this->db->insert('tbl_billinginfo', $data2);								
								$infodbupdtids[] = $this->db->insert_id();
							}

						}
						$this->db->select("*");
						$this->db->where("fld_billingid",$billingid);
						$results_del = $this->db->get('tbl_billinginfo');							
						if($results_del->num_rows()>0) // already exists
						{
							foreach($results_del->result() as $results_del_row)
							{
								if(!in_array($results_del_row->fld_id,$infodbupdtids))
								{												
									$this->db->where("fld_id",$results_del_row->fld_id);
									$this->db->delete('tbl_billinginfo');										
								}
							}
						}
						$post_fields['billinginfo'] = $indexinfo;
						$indexinfo++;							
					}
					
				}
			}	
			
			if($response>0)
			{
				$datahis = array(
				"fld_type" => 'local2server',
				"fld_clientid" => $clientid,
				"fld_outletid" => $outletid,
				"fld_updatedby" => $userid,
				"fld_updatedon" => $newlastupdatedon
				);

				$this->db->insert('tbl_updatehistory', $datahis);
				
				$post_fields["lastupdatedon"] = $newlastupdatedon;
				$response = json_encode($post_fields);
			}				
		}
		echo $response;		
	}
	
	function updtlocal2serverv3()
	{
		$arrbillnos = array();
		$response = 0;
		$post_fields = array();
		
		## last updated on
		$newlastupdatedon = date('Y-m-d H:i:s');
		$lastbillsyncedon = "";
		
		#check var
		$lastupdatedon = $this->input->post("lastupdatedon");
		$clientid = $this->input->post("clientid");
		$userid = $this->input->post("userid");
		$outletid = $this->input->post("outletid");	
		
		## tables		
		$users = $this->input->post("users");
		$customers = $this->input->post("customers");
		$dayoff = $this->input->post("dayoff");
		$billing = $this->input->post("billing");
		$billinginfo= $this->input->post("billinginfo");
		$post_fields['users'] = 0;
		$post_fields['customers'] = 0;
		$post_fields['dayoff'] = 0;
		$post_fields['billing'] = 0;
		$post_fields['billinginfo'] = 0;
		if($lastupdatedon!='' && $clientid>0 && $outletid>0 && $userid>0)
		{				
			if(is_array($users)>0)
			{
				$index = 1;
				foreach($users as $userrow)
				{					    
					$data = array(						
						"fld_name" => $userrow['fld_name'],
						"fld_email" => $userrow['fld_email'],
						"fld_mobile" => $userrow['fld_mobile'],
						"fld_userpass" => $userrow['fld_userpass'],
						"fld_updatedby" => $userrow['fld_updatedby'],
						"fld_updatedon" => $userrow['fld_updatedon'],
						"fld_lastlogin" => $userrow['fld_lastlogin'],
						"fld_lastloginip" => $userrow['fld_lastloginip']
					);
					$this->db->select("*");						
					$this->db->where("fld_id",$userrow['fld_id']);
					$results = $this->db->get('tbl_users');						
					if($results->num_rows()>0)
					{	
					   
						$this->db->where("fld_id",$userrow['fld_id']);
						$this->db->update('tbl_users', $data);	
						$response++;
						$post_fields['users'] = $index;
						$index++;
					}

				}
			}
			
			if(is_array($dayoff)>0)
			{	
				$index = 1;
				foreach($dayoff as $dayoffrow)
				{
					$this->db->select("*");		
					$this->db->where("fld_datetime='".$dayoffrow['fld_datetime']."'");
					$this->db->where("fld_clientid",$clientid);
					$this->db->where("fld_outletid",$outletid);
					$this->db->where("fld_userid",$dayoffrow['fld_userid']);
					$results = $this->db->get('tbl_dayoff');
					//echo $this->db->last_query();	exit;
					if($results->num_rows()>0)
					{
						$post_fields['dayoff'] = $index;
						
					}
					else
					{
						$data = array(						
							"fld_outletid" => $dayoffrow['fld_outletid'],
							"fld_clientid" => $dayoffrow['fld_clientid'],
							"fld_userid" => $dayoffrow['fld_userid'],
							"fld_dayoffdate" => $dayoffrow['fld_dayoffdate'],
							"fld_amount" => $dayoffrow['fld_amount'],
							"fld_cash" => $dayoffrow['fld_cash'],														
							"fld_cc" => $dayoffrow['fld_cc'],														
							"fld_upi" => $dayoffrow['fld_upi'],														
							"fld_cash" => $dayoffrow['fld_cash'],
							"fld_nameaccount" => $dayoffrow['fld_nameaccount'],
							"fld_naremarks" => $dayoffrow['fld_naremarks'],					
							"fld_lastbillid" => $dayoffrow['fld_lastbillid'],
							"fld_datetime" => $dayoffrow['fld_datetime'],											
							"fld_del" => $dayoffrow['fld_del'],												
							"fld_updatedby" => $dayoffrow['fld_updatedby'],
							"fld_updatedon" => $dayoffrow['fld_updatedon']
						);
						$this->db->insert('tbl_dayoff', $data);
						$response++;
						$post_fields['dayoff'] = $index;
						
					}
					$index++;
				}
			}
			
			if(is_array($customers)>0)
			{
				$index = 1;
				foreach($customers as $customerrow)
				{
					$data = array(						
						"fld_name" => $customerrow['fld_name'],
						"fld_email" => $customerrow['fld_email'],
						"fld_discounttype" => $customerrow['fld_discounttype'],
						"fld_discount" => $customerrow['fld_discount'],
						"fld_givenname" => $customerrow['fld_givenname'],
						"fld_givenemail" => $customerrow['fld_givenemail'],
						"fld_givenmobile" => $customerrow['fld_givenmobile'],
						"fld_status" => $customerrow['fld_status'],						
						"fld_del" => $customerrow['fld_del'],
						"fld_updatedby" => $customerrow['fld_updatedby'],
						"fld_updatedon" => $customerrow['fld_updatedon']
					);
					$this->db->select("*");		
					$this->db->where("fld_mobile",$customerrow['fld_mobile']);
					$results = $this->db->get('tbl_customers');
					
					if($results->num_rows()>0)
					{							
						$this->db->where("fld_mobile", $customerrow['fld_mobile']);
						$this->db->update('tbl_customers', $data);	
						
					}
					else
					{
						$data["fld_clientid"] =$customerrow['fld_clientid'];
						$data["fld_outletid"] =$customerrow['fld_outletid'];
						$data["fld_userid"] =$customerrow['fld_userid'];
						$data["fld_mobile"] =$customerrow['fld_mobile'];
						$data["fld_datetime"] =$customerrow['fld_datetime'];
						$this->db->insert('tbl_customers', $data);
						$response++;
						
					}
					$post_fields['customers'] = $index;
					$index++;
				}
			}
			
			if(is_array($billing)>0)
			{
				$index = 1;
				$indexinfo = 1;
				foreach($billing as $billrow)
				{

					$data = array(
					"fld_mobile" => $billrow['fld_mobile'],
					"fld_name" => $billrow['fld_name'],
					"fld_otp" => $billrow['fld_otp'],
					"fld_specialdiscount" => $billrow['fld_specialdiscount'],
					"fld_givenname" => $billrow['fld_givenname'],
					"fld_givenemail" => $billrow['fld_givenemail'],
					"fld_givenmobile" => $billrow['fld_givenmobile'],
					"fld_discounttype" => $billrow['fld_discounttype'],
					"fld_discount" => $billrow['fld_discount'],
					"fld_amount" => $billrow['fld_amount'],
					"fld_netamount" => $billrow['fld_netamount'],
					"fld_giftid" => $billrow['fld_giftid'],
					"fld_gprodid" => $billrow['fld_gprodid'],
					"fld_gqty" => $billrow['fld_gqty'],
					"fld_status" => $billrow['fld_status'],
					"fld_remarks" => $billrow['fld_remarks'],
					"fld_cancellation_reason" => $billrow['fld_cancellation_reason'],
					"fld_cancellation_remarks" => $billrow['fld_cancellation_remarks'],
					"fld_cancellationdate" => $billrow['fld_cancellationdate'],
					"fld_cancelrequestby" => $billrow['fld_cancelrequestby'],						
					"fld_del" => $billrow['fld_del'],
					"fld_munid" => $billrow['fld_munid'],
					"fld_updatedby" => $billrow['fld_updatedby'],
					"fld_updatedon" => $billrow['fld_updatedon']
					);
					if($billrow['fld_paytype'])  $data["fld_paytype"] = $billrow['fld_paytype'];
					
					$lastbillsyncedon = $billrow['fld_updatedon'];
					$billingid = 0;
					$this->db->select("*");		
					$this->db->where("fld_billno", $billrow['fld_billno']);
					$this->db->where("fld_clientid",$clientid);
					$this->db->where("fld_outletid",$outletid);
					$results = $this->db->get('tbl_billing');
					//echo $this->db->last_query();	exit;
					if($results->num_rows()>0)
					{
						$row  = $results->row();
						
						$this->db->where("fld_billno", $billrow['fld_billno']);
						$this->db->where("fld_clientid",$clientid);
						$this->db->where("fld_outletid",$outletid);
						$this->db->update('tbl_billing', $data);
						
						$billingid = $row->fld_id;
					}
					else
					{
						$data["fld_clientid"] =$billrow['fld_clientid'];						
						$data["fld_outletid"] =$billrow['fld_outletid'];						
						$data["fld_userid"] =$billrow['fld_userid'];						
						$data["fld_customerid"] =$billrow['fld_customerid'];						
						$data["fld_billno"] =$billrow['fld_billno'];						
						$data["fld_billdate"] =$billrow['fld_billdate'];						
						$data["fld_datetime"] =$billrow['fld_datetime'];						
						$this->db->insert('tbl_billing', $data);
						$billingid = $this->db->insert_id();
					}
					$response++;
					$post_fields['billing'] = $index;
					$index++;

					## info rows
					if($billingid>0)
					{	
						foreach($billinginfo[$billrow['fld_billno']] as $billinforow)
						{
							$this->db->select("fld_id");
							$this->db->where("fld_billingid", $billingid);					
							$this->db->where("fld_productid", $billinforow['fld_productid']);
							$results2 = $this->db->get('tbl_billinginfo');
							if($results2->num_rows()>0) // update if found
							{
								$row2  = $results2->row();
								$data2 = array(						
								"fld_qty" => $billinforow['fld_qty'],
								"fld_price" => $billinforow['fld_price'],
								"fld_mrp" => $billinforow['fld_mrp']
								);
								$this->db->where("fld_id", $row2->fld_id);
								$this->db->update('tbl_billinginfo', $data2);
								$infodbupdtids[] = $row2->fld_id;
							}
							else
							{
								$data2 = array(						
								"fld_billingid" => $billingid,
								"fld_productid" => $billinforow['fld_productid'],
								"fld_qty" => $billinforow['fld_qty'],
								"fld_price" => $billinforow['fld_price'],
								"fld_mrp" => $billinforow['fld_mrp']
								);

								$this->db->insert('tbl_billinginfo', $data2);								
								$infodbupdtids[] = $this->db->insert_id();
							}

						}
						$this->db->select("*");
						$this->db->where("fld_billingid",$billingid);
						$results_del = $this->db->get('tbl_billinginfo');							
						if($results_del->num_rows()>0) // already exists
						{
							foreach($results_del->result() as $results_del_row)
							{
								if(!in_array($results_del_row->fld_id,$infodbupdtids))
								{												
									$this->db->where("fld_id",$results_del_row->fld_id);
									$this->db->delete('tbl_billinginfo');										
								}
							}
						}
						$arrbillnos[$billrow['fld_billno']] = $billrow['fld_billno'];
						$post_fields['billinginfo'] = $indexinfo;
						$indexinfo++;							
					}
				}
				$strbillnos = "";
				if(count(array_filter($arrbillnos))>0)
				{
					$strbillnos = implode(",", $arrbillnos);;
				}
				$post_fields['billnos'] = $strbillnos;
			}
			if($response>0)
			{
				$datahis = array(
				"fld_type" => 'local2server',
				"fld_clientid" => $clientid,
				"fld_outletid" => $outletid,
				"fld_updatedby" => $userid,
				"fld_updatedon" => $newlastupdatedon
				);
				if($lastbillsyncedon!="") $datahis["fld_lastupdatedbill"] = $lastbillsyncedon;
				$this->db->insert('tbl_updatehistory', $datahis);
				$post_fields["lastupdatedon"] = $newlastupdatedon;
				$response = json_encode($post_fields);
			}				
		}
		echo $response;		
	}

	function updtdbserver2local()
	{
		$response = 0;
		$lastupdatedon = $this->input->post("lastupdatedon");
		$clientid = $this->input->post("clientid");
		$userid = $this->input->post("userid");
		$outletid = $this->input->post("outletid");	

		if($lastupdatedon!='' && $clientid>0 && $outletid>0 && $userid>0)
		{
			$arrTbls = array();
			$arrTbls[] = 'brand';
			$arrTbls[] = 'category';	
			$arrTbls[] = 'zone';
			$arrTbls[] = 'clients';
			$arrTbls[] = 'products';
			$arrTbls[] = 'price';
			$arrTbls[] = 'customers';
			$arrTbls[] = 'dayoff';
			$arrTbls[] = 'gifts';
			$arrTbls[] = 'outlets';			
			$arrTbls[] = 'prodminqty';			
			$arrTbls[] = 'users';			
			$arrTbls[] = 'billing';			
			$arrTbls[] = 'transaction';

			$post_fields = array();
			
			foreach($arrTbls as $tblnm)
			{
			
				$this->db->select("*");		
				$this->db->where("fld_updatedon >'".$lastupdatedon."'");
				if($tblnm!='brand' && $tblnm!='category' && $tblnm!='products' && $tblnm!='clients')
				{
					$this->db->where("fld_clientid",$clientid);
					if($tblnm!='zone' && $tblnm!='price' && $tblnm!='outlets')
					{					
						if($tblnm=='transaction')
						{
							$this->db->group_start();
							$this->db->where("fld_outletid",$outletid);
							$this->db->or_where("fld_fromoutletid",$outletid);
							$this->db->group_end();
						}
						else
						{
							$this->db->where("fld_outletid",$outletid);
						}						
					}
				}
				$this->db->where("fld_del",'0');
				$udtlcdb2svr_rs = $this->db->get('tbl_'.$tblnm);
				//echo "<br> ==> " .$this->db->last_query();	
				if($udtlcdb2svr_rs->num_rows()>0)
				{
					$post_fields[$tblnm] = $udtlcdb2svr_rs->result_array();				
					if($tblnm=='billing' || $tblnm=='transaction')
					{		
						$id =0;
						$billno ="";
						for($i=0;$i<count($post_fields[$tblnm]);$i++)
						{
							$id = $post_fields[$tblnm][$i]['fld_id'];
							$billno = $post_fields[$tblnm][$i]['fld_billno'];
							if($tblnm=='billing')
							{
								$billno = $post_fields[$tblnm][$i]['fld_billno'];
							}							
							elseif($tblnm=='transaction')
							{
								$billno = $post_fields[$tblnm][$i]['fld_refno'];
							}
							if($id>0)
							{
								$this->db->select("*");
								$this->db->where("fld_".$tblnm."id",$id);
								$udtlcdb2svr_rs2 = $this->db->get('tbl_'.$tblnm."info");
								//echo $this->db->last_query();	
								if($udtlcdb2svr_rs2->num_rows()>0)
								{
									$post_fields[$tblnm.'info'][$billno]= $udtlcdb2svr_rs2->result_array();
								}
							}
						}
						
					}
					$response++;
				}
				
			}
				if($response>0)
				{
					$lastupdatedon = date('Y-m-d H:i:s');
					$datahis = array(
					"fld_type" => 'server2local',
					"fld_clientid" => $clientid,
					"fld_outletid" => $outletid,
					"fld_updatedby" => $userid,
					"fld_updatedon" => $lastupdatedon

					);		
					$this->db->insert('tbl_updatehistory', $datahis);
					
					$post_fields["newlastupdatedon"] = $lastupdatedon;
					$response = json_encode($post_fields);
				}
			
			}

		//predata($post_fields);
		echo $response;
	}
	
	function updateLocalSystem()
	{
		$response = 0;
		$task = $this->input->get_post("task");
		$lastupdatedon = $this->input->get_post("lastupdatedon");
		$clientid = $this->input->get_post("clientid");
		$userid = $this->input->get_post("userid");
		$outletid = $this->input->get_post("outletid");
		
		if($task=="allstuff")
		{
			$arrallstuff = array();
			if($outletid>0)
			{
				## start product database
				$arrproducts = array();
				
				## get outlet price
				$arrproductprice = array();
				$this->db->select("PRI.*, OI.fld_name as outletname, ZI.fld_name as zonename");
				$this->db->from("tbl_price PRI");
				$this->db->join("tbl_zone ZI", "PRI.fld_zoneid=ZI.fld_id");
				$this->db->join("tbl_outlets OI", "PRI.fld_zoneid=OI.fld_zoneid");
				$this->db->where("OI.fld_id", $outletid);
				$this->db->where("PRI.fld_del", "0");
				$result = $this->db->get();
				if($result->num_rows()>0)
				{
					while ($row = $result->unbuffered_row())
					{
						$arrproductprice[$row->fld_productid] = array(
							"fld_mrp" => $row->fld_mrp,
							"fld_discount" => $row->fld_discount,
							"fld_price" => $row->fld_price,
							"fld_price3bottles" => $row->fld_price3bottles,
							"fld_price6bottles" => $row->fld_price6bottles,
							"fld_price9bottles" => $row->fld_price9bottles,
							"fld_price12bottles" => $row->fld_price12bottles,
							"fld_price24bottles" => $row->fld_price24bottles,
							"outletname" => $row->outletname
						);
					}
				}
				
				## get incoming stock to outlet
				$arrstockincoming = array();
				$this->db->select("TII.fld_productid, sum(TII.fld_qty) as incomingstock");
				$this->db->from("tbl_transactioninfo TII");
				$this->db->join("tbl_transaction TI", "TII.fld_transactionid=TI.fld_id");
				$this->db->group_start();
				$this->db->where("TI.fld_type", "stockout");
				$this->db->or_where("TI.fld_type", "o2o");
				$this->db->group_end();
				$this->db->where("TI.fld_outletid", $outletid);
				$this->db->where("TI.fld_del", "0");
				$this->db->where("TI.fld_status", "completed");
				/*$this->db->group_start();
				$this->db->where("TI.fld_status", "completed");
				$this->db->or_where("TI.fld_status", "delivered");
				$this->db->group_end();*/
				$this->db->group_by("TII.fld_productid");
				$result02 = $this->db->get();
				if($result02->num_rows()>0)
				{
					while ($row02 = $result02->unbuffered_row())
					{
						$arrstockincoming[$row02->fld_productid] = $row02->incomingstock;
					}
				}
				
				## get outgoingn stock from outlet to another outlet and warehouse
				$arrstockoutgoing = array();
				$this->db->select("TII.fld_productid, sum(TII.fld_qty) as outgoingstock");
				$this->db->from("tbl_transactioninfo TII");
				$this->db->join("tbl_transaction TI", "TII.fld_transactionid=TI.fld_id");
				$this->db->group_start();
				$this->db->where("TI.fld_type", "o2w");
				$this->db->or_where("TI.fld_type", "o2o");
				$this->db->group_end();
				$this->db->where("TI.fld_fromoutletid", $outletid);
				$this->db->where("TI.fld_del", "0");
				$this->db->group_start();
				$this->db->where("TI.fld_status", "completed");
				$this->db->or_where("TI.fld_status", "delivered");
				$this->db->group_end();
				$this->db->group_by("TII.fld_productid");
				$result03 = $this->db->get();
				if($result03->num_rows()>0)
				{
					while ($row03 = $result03->unbuffered_row())
					{
						$arrstockoutgoing[$row03->fld_productid] = $row03->outgoingstock;
					}
				}
				
				## get products min qty
				$arrproductminqty = array();
				$this->db->select("fld_outletid, fld_productid, fld_minqty");
				$this->db->where("fld_outletid", $outletid);
				$result04 = $this->db->get("tbl_prodminqty");
				if($result04->num_rows()>0)
				{
					while ($row04 = $result04->unbuffered_row())
					{
						$arrproductminqty[$row04->fld_id] = $row04->fld_minqty;
					}
				}
				## end of products min qty
				
				## get all products
				$this->db->select("*");
				$result05 = $this->db->get("tbl_products");
				if($result05->num_rows()>0)
				{
					while ($row05 = $result05->unbuffered_row())
					{
						$arrproducts[$row05->fld_id] = array(
							"fld_id" => $row05->fld_id,
							"fld_type" => $row05->fld_type,
							"fld_catid" => $row05->fld_catid,
							"fld_brandid" => $row05->fld_brandid,
							"fld_name" => $row05->fld_name,
							"fld_barcode" => $row05->fld_barcode,
							"fld_barcode02" => $row05->fld_barcode02,
							"fld_barcode03" => $row05->fld_barcode03,
							"fld_barcode04" => $row05->fld_barcode04,
							"fld_noofbottlesincase" => $row05->fld_noofbottlesincase,
							"fld_minstockvalue" => $row05->fld_minstockvalue,
							"fld_minqty" => 0,
							"fld_shortdesc" => $row05->fld_shortdesc,
							"fld_status" => $row05->fld_status,
							"fld_datetime" => $row05->fld_datetime,
							"fld_updatedby" => $row05->fld_updatedby,
							"fld_updatedon" => $row05->fld_updatedon,
							"fld_del" => $row05->fld_del,
							"fld_receivedqty" => 0,
							"fld_mrp" => 0,
							"fld_discount" => 0,
							"fld_price" => 0,
							"fld_price3bottles" => 0,
							"fld_price6bottles" => 0,
							"fld_price9bottles" => 0,
							"fld_price12bottles" => 0,
							"fld_price24bottles" => 0
						);
						
						## update price if exists
						if($arrproductprice[$row05->fld_id])
						{
							$arrproducts[$row05->fld_id]["fld_mrp"] = $arrproductprice[$row05->fld_id]["fld_mrp"];
							$arrproducts[$row05->fld_id]["fld_discount"] = $arrproductprice[$row05->fld_id]["fld_discount"];
							$arrproducts[$row05->fld_id]["fld_price"] = $arrproductprice[$row05->fld_id]["fld_price"];
							$arrproducts[$row05->fld_id]["fld_price3bottles"] = $arrproductprice[$row05->fld_id]["fld_price3bottles"];
							$arrproducts[$row05->fld_id]["fld_price6bottles"] = $arrproductprice[$row05->fld_id]["fld_price6bottles"];
							$arrproducts[$row05->fld_id]["fld_price9bottles"] = $arrproductprice[$row05->fld_id]["fld_price9bottles"];
							$arrproducts[$row05->fld_id]["fld_price12bottles"] = $arrproductprice[$row05->fld_id]["fld_price12bottles"];
							$arrproducts[$row05->fld_id]["fld_price24bottles"] = $arrproductprice[$row05->fld_id]["fld_price24bottles"];
						}
						
						## update minimum stock value
						if($arrproductminqty[$row05->fld_id]) $arrproducts[$row05->fld_id]["fld_minqty"] = $arrproductminqty[$row05->fld_id];
						
						## update stock
						$incomingstockqty = 0;
						$outgoingstockqty = 0;
						$stockqty = 0;
						if($arrstockincoming[$row05->fld_id]) $incomingstockqty = $arrstockincoming[$row05->fld_id];
						if($arrstockoutgoing[$row05->fld_id]) $outgoingstockqty = $arrstockoutgoing[$row05->fld_id];
						$stockqty = $incomingstockqty - $outgoingstockqty; 
						$arrproducts[$row05->fld_id]["fld_receivedqty"] = $stockqty;
					}
					## transfer to array
					$arrallstuff["products"] = $arrproducts;
				}
				## end of product database
				
				## get categories
				$arrcategories = array();
				$this->db->select("*");
				$result06 = $this->db->get("tbl_category");
				if($result06->num_rows()>0)
				{
					while ($row06 = $result06->unbuffered_row())
					{
						$arrcategories[$row06->fld_id] = array(
							"fld_id" => $row06->fld_id,
							"fld_type" => $row06->fld_type,
							"fld_name" => $row06->fld_name,
							"fld_status" => $row06->fld_status,
							"fld_datetime" => $row06->fld_datetime,
							"fld_updatedby" => $row06->fld_updatedby,
							"fld_updatedon" => $row06->fld_updatedon,
							"fld_del" => $row06->fld_del
						);
					}
					## transfer to array
					$arrallstuff["category"] = $arrcategories;
				}
				## end of categories
				
				## get brands
				$arrbrands = array();
				$this->db->select("*");
				$result07 = $this->db->get("tbl_brand");
				if($result07->num_rows()>0)
				{
					while ($row07 = $result07->unbuffered_row())
					{
						$arrbrands[$row07->fld_id] = array(
							"fld_id" => $row07->fld_id,
							"fld_name" => $row07->fld_name,
							"fld_status" => $row07->fld_status,
							"fld_datetime" => $row07->fld_datetime,
							"fld_updatedby" => $row07->fld_updatedby,
							"fld_updatedon" => $row07->fld_updatedon,
							"fld_del" => $row07->fld_del
						);
					}
					## transfer to array
					$arrallstuff["brand"] = $arrbrands;
				}
				## end of brands
				
				## get operator users
				$arrusers = array();
				$this->db->select("*");
				$this->db->where("fld_outletid", $outletid);
				$this->db->where("fld_type", "operator");
				$result08 = $this->db->get("tbl_users");
				if($result08->num_rows()>0)
				{
					while ($row08 = $result08->unbuffered_row())
					{
						$arrusers[$row08->fld_id] = array(
							"fld_id" => $row08->fld_id,
							"fld_clientid" => $row08->fld_clientid,
							"fld_warehouseid" => $row08->fld_warehouseid,
							"fld_outletid" => $row08->fld_outletid,
							"fld_type" => $row08->fld_type,
							"fld_otp" => $row08->fld_otp,
							"fld_discount" => $row08->fld_discount,
							"fld_name" => $row08->fld_name,
							"fld_email" => $row08->fld_email,
							"fld_mobile" => $row08->fld_mobile,
							"fld_username" => $row08->fld_username,
							"fld_userpass" => $row08->fld_userpass,
							"fld_status" => $row08->fld_status,
							"fld_datetime" => $row08->fld_datetime,
							"fld_updatedby" => $row08->fld_updatedby,
							"fld_updatedon" => $row08->fld_updatedon,
							"fld_del" => $row08->fld_del,
							"fld_lastlogin" => $row08->fld_lastlogin,
							"fld_lastloginip" => $row08->fld_lastloginip
						);
					}
					## transfer to array
					$arrallstuff["users"] = $arrusers;
				}
				## end of operator users
				
				## get outlet info
				$arroutletinfo = array();
				$this->db->select("*");
				$this->db->where("fld_id", $outletid);
				$result09 = $this->db->get("tbl_outlets");
				if($result09->num_rows()>0)
				{
					while ($row09 = $result09->unbuffered_row())
					{
						$arroutletinfo[$row09->fld_id] = array(
							"fld_id" => $row09->fld_id,
							"fld_code" => $row09->fld_code,
							"fld_clientid" => $row09->fld_clientid,
							"fld_zoneid" => $row09->fld_zoneid,
							"fld_name" => $row09->fld_name,
							"fld_email" => $row09->fld_email,
							"fld_mobile" => $row09->fld_mobile,
							"fld_invoiceprefix" => $row09->fld_invoiceprefix,
							"fld_address" => $row09->fld_address,
							"fld_managername" => $row09->fld_managername,
							"fld_manageremail" => $row09->fld_manageremail,
							"fld_managermobile" => $row09->fld_managermobile,
							"fld_status" => $row09->fld_status,
							"fld_datetime" => $row09->fld_datetime,
							"fld_updatedby" => $row09->fld_updatedby,
							"fld_updatedon" => $row09->fld_updatedon,
							"fld_del" => $row09->fld_del
						);
					}
					## transfer to array
					$arrallstuff["outlets"] = $arroutletinfo;
				}
				## end of outlet info
				
				## update server time 
				$lastupdatedon = date('Y-m-d H:i:s');
				$datahis = array(
					"fld_type" => 'server2local',
					"fld_clientid" => 1,
					"fld_outletid" => $outletid,
					"fld_updatedby" => 0,
					"fld_updatedon" => $lastupdatedon
				);		
				$this->db->insert('tbl_updatehistory', $datahis);
				## end of server time update
				
				$arrallstuff["updatedon"] = $lastupdatedon;
			}
			$response = json_encode($arrallstuff);
			echo $response;
		}
		else // for general updates from server to local system by biller
		{
			//echo "<br>" . $lastupdatedon . " => " . $clientid . " => " . $outletid . " => " . $userid;
			if($lastupdatedon!='' && $clientid>0 && $outletid>0 && $userid>0)
			{
				## start product database
				$arrproducts = array();
				
				$arrproductids = array();
				## get product ids of all cases
				$this->db->distinct();
				$this->db->select("TII.fld_productid");
				$this->db->from("tbl_transactioninfo TII");
				$this->db->join("tbl_transaction TI", "TII.fld_transactionid=TI.fld_id");
				$this->db->group_start();
				$this->db->where("TI.fld_type", "stockout");
				$this->db->or_where("TI.fld_type", "o2o");
				$this->db->group_end();
				$this->db->where("TI.fld_outletid", $outletid);
				//$this->db->where("TI.fld_del", "0");
				$this->db->where("TI.fld_status", "completed");
				/*$this->db->group_start();
				$this->db->where("TI.fld_status", "completed");
				$this->db->or_where("TI.fld_status", "delivered");
				$this->db->group_end();*/
				$this->db->where("TI.fld_updatedon>=", "$lastupdatedon");
				$this->db->group_by("TII.fld_productid");
				$result002 = $this->db->get();
				//echo "<br>" . $this->db->last_query() . " => " . $result02->num_rows() . "<br>";
				if($result002->num_rows()>0)
				{
					while ($row002 = $result002->unbuffered_row())
					{
						if(!$arrproductids[$row002->fld_productid]) $arrproductids[$row002->fld_productid] = $row002->fld_productid;
					}
				}
				
				$this->db->distinct();
				$this->db->select("TII.fld_productid");
				$this->db->from("tbl_transactioninfo TII");
				$this->db->join("tbl_transaction TI", "TII.fld_transactionid=TI.fld_id");
				$this->db->group_start();
				$this->db->where("TI.fld_type", "o2w");
				$this->db->or_where("TI.fld_type", "o2o");
				$this->db->group_end();
				$this->db->where("TI.fld_fromoutletid", $outletid);
				//$this->db->where("TI.fld_del", "0");
				$this->db->where("TI.fld_status", "completed");
				/*$this->db->group_start();
				$this->db->where("TI.fld_status", "completed");
				$this->db->or_where("TI.fld_status", "delivered");
				$this->db->group_end();*/
				$this->db->where("TI.fld_updatedon>=", "$lastupdatedon");
				$result003 = $this->db->get();
				if($result003->num_rows()>0)
				{
					while ($row003 = $result003->unbuffered_row())
					{
						if(!$arrproductids[$row003->fld_productid]) $arrproductids[$row003->fld_productid] = $row003->fld_productid;
					}
				}
				
				$this->db->distinct();
				$this->db->select("fld_productid");
				$this->db->where("fld_outletid", $outletid);
				$this->db->where("fld_updatedon>=", "$lastupdatedon");
				$result004 = $this->db->get("tbl_prodminqty");
				if($result004->num_rows()>0)
				{
					while ($row004 = $result004->unbuffered_row())
					{
						if(!$arrproductids[$row004->fld_productid]) $arrproductids[$row004->fld_productid] = $row004->fld_productid;
					}
				}
				
				$this->db->distinct();
				$this->db->select("fld_id");
				if(count($arrproductids)>0)
				{
					$this->db->group_start();
					$this->db->where_in("fld_id", $arrproductids);
					$this->db->or_where("fld_updatedon>=", "$lastupdatedon");
					$this->db->group_end();
				}
				else
				{
					$this->db->where("fld_updatedon>=", "$lastupdatedon");
				}
				$result10 = $this->db->get("tbl_products");
				if($result10->num_rows()>0)
				{
					while ($row10 = $result10->unbuffered_row())
					{
						if(!$arrproductids[$row10->fld_id]) $arrproductids[$row10->fld_id] = $row10->fld_id;
					}
				}
				
				$this->db->select("PRI.fld_productid");
				$this->db->from("tbl_price PRI");
				$this->db->join("tbl_zone ZI", "PRI.fld_zoneid=ZI.fld_id");
				$this->db->join("tbl_outlets OI", "PRI.fld_zoneid=OI.fld_zoneid");
				$this->db->where("OI.fld_id", $outletid);
				$this->db->where("PRI.fld_updatedon>=", "$lastupdatedon");
				//$this->db->where("PRI.fld_del", "0");
				$result0 = $this->db->get();
				//echo "<br>" . $this->db->last_query() . " => " . $result->num_rows() . "<br>";
				if($result0->num_rows()>0)
				{
					while ($row0 = $result0->unbuffered_row())
					{
						if(!$arrproductids[$row0->fld_productid]) $arrproductids[$row0->fld_productid] = $row0->fld_productid;
					}
				}
				## end of product ids
				//predata($arrproductids);
				
				if(count($arrproductids)>0)
				{
					## get incoming stock to outlet
					$arrstockincoming = array();
					$this->db->select("TII.fld_productid, sum(TII.fld_qty) as incomingstock");
					$this->db->from("tbl_transactioninfo TII");
					$this->db->join("tbl_transaction TI", "TII.fld_transactionid=TI.fld_id");
					$this->db->group_start();
					$this->db->where("TI.fld_type", "stockout");
					$this->db->or_where("TI.fld_type", "o2o");
					$this->db->group_end();
					$this->db->where("TI.fld_outletid", $outletid);
					$this->db->where("TI.fld_del", "0");
					$this->db->where("TI.fld_status", "completed");
					/*$this->db->group_start();
					$this->db->where("TI.fld_status", "completed");
					$this->db->or_where("TI.fld_status", "delivered");
					$this->db->or_where_in("TII.fld_productid", $arrproductids);
					$this->db->group_end();*/
					$this->db->group_by("TII.fld_productid");
					$result02 = $this->db->get();
					//echo "<br>" . $this->db->last_query() . " => " . $result02->num_rows() . "<br>";
					if($result02->num_rows()>0)
					{
						while ($row02 = $result02->unbuffered_row())
						{
							$arrstockincoming[$row02->fld_productid] = $row02->incomingstock;
						}
					}
					
					## get outgoingn stock from outlet to another outlet and warehouse
					$arrstockoutgoing = array();
					$this->db->select("TII.fld_productid, sum(TII.fld_qty) as outgoingstock");
					$this->db->from("tbl_transactioninfo TII");
					$this->db->join("tbl_transaction TI", "TII.fld_transactionid=TI.fld_id");
					$this->db->group_start();
					$this->db->where("TI.fld_type", "o2w");
					$this->db->or_where("TI.fld_type", "o2o");
					$this->db->group_end();
					$this->db->where("TI.fld_fromoutletid", $outletid);
					$this->db->where("TI.fld_del", "0");
					$this->db->group_start();
					$this->db->where("TI.fld_status", "completed");
					$this->db->or_where("TI.fld_status", "delivered");
					$this->db->or_where_in("TII.fld_productid", $arrproductids);
					$this->db->group_end();
					$this->db->group_by("TII.fld_productid");
					$result03 = $this->db->get();
					if($result03->num_rows()>0)
					{
						while ($row03 = $result03->unbuffered_row())
						{
							if(!$arrproductids[$row03->fld_productid]) $arrproductids[$row03->fld_productid] = $row03->fld_productid;
							$arrstockoutgoing[$row03->fld_productid] = $row03->outgoingstock;
						}
					}
					
					## get products min qty
					$arrproductminqty = array();
					$this->db->select("fld_outletid, fld_productid, fld_minqty");
					$this->db->where("fld_outletid", $outletid);
					//$this->db->where("fld_updatedon>=", "$lastupdatedon");
					$this->db->where_in("fld_productid", $arrproductids);
					$result04 = $this->db->get("tbl_prodminqty");
					if($result04->num_rows()>0)
					{
						while ($row04 = $result04->unbuffered_row())
						{
							if(!$arrproductids[$row04->fld_productid]) $arrproductids[] = $row04->fld_productid;
							$arrproductminqty[$row04->fld_id] = $row04->fld_minqty;
						}
					}
					## end of products min qty
					
					
					## get outlet price
					$arrproductprice = array();
					$this->db->select("PRI.*, OI.fld_name as outletname, ZI.fld_name as zonename");
					$this->db->from("tbl_price PRI");
					$this->db->join("tbl_zone ZI", "PRI.fld_zoneid=ZI.fld_id");
					$this->db->join("tbl_outlets OI", "PRI.fld_zoneid=OI.fld_zoneid");
					$this->db->where("OI.fld_id", $outletid);
					$this->db->where_in("PRI.fld_productid", $arrproductids); // commented on 20-june-2023
					//$this->db->where("PRI.fld_updatedon>=", "$lastupdatedon");
					$this->db->where("PRI.fld_del", "0");
					$result = $this->db->get();
					//echo "<br>" . $this->db->last_query() . " => " . $result->num_rows() . "<br>";
					if($result->num_rows()>0)
					{
						while ($row = $result->unbuffered_row())
						{
							$arrproductids[] = $row->fld_productid;
							
							$arrproductprice[$row->fld_productid] = array(
								"fld_mrp" => $row->fld_mrp,
								"fld_discount" => $row->fld_discount,
								"fld_price" => $row->fld_price,
								"fld_price3bottles" => $row->fld_price3bottles,
								"fld_price6bottles" => $row->fld_price6bottles,
								"fld_price9bottles" => $row->fld_price9bottles,
								"fld_price12bottles" => $row->fld_price12bottles,
								"fld_price24bottles" => $row->fld_price24bottles,
								"outletname" => $row->outletname
							);
						}
					}
					
					## get all products
					$this->db->distinct();
					$this->db->select("*");
					$this->db->where_in("fld_id", $arrproductids);
					$result05 = $this->db->get("tbl_products");
					//echo "<br>" . $this->db->last_query() . " => " . $result05->num_rows() . "<br>";
					if($result05->num_rows()>0)
					{
						while ($row05 = $result05->unbuffered_row())
						{
							$arrproducts[$row05->fld_id] = array(
								"fld_id" => $row05->fld_id,
								"fld_type" => $row05->fld_type,
								"fld_catid" => $row05->fld_catid,
								"fld_brandid" => $row05->fld_brandid,
								"fld_name" => $row05->fld_name,
								"fld_barcode" => $row05->fld_barcode,
								"fld_barcode02" => $row05->fld_barcode02,
								"fld_barcode03" => $row05->fld_barcode03,
								"fld_barcode04" => $row05->fld_barcode04,
								"fld_noofbottlesincase" => $row05->fld_noofbottlesincase,
								"fld_minstockvalue" => $row05->fld_minstockvalue,
								"fld_minqty" => 0,
								"fld_shortdesc" => $row05->fld_shortdesc,
								"fld_status" => $row05->fld_status,
								"fld_datetime" => $row05->fld_datetime,
								"fld_updatedby" => $row05->fld_updatedby,
								"fld_updatedon" => $row05->fld_updatedon,
								"fld_del" => $row05->fld_del,
								"fld_receivedqty" => 0,
								"fld_mrp" => 0,
								"fld_discount" => 0,
								"fld_price" => 0,
								"fld_price3bottles" => 0,
								"fld_price6bottles" => 0,
								"fld_price9bottles" => 0,
								"fld_price12bottles" => 0,
								"fld_price24bottles" => 0
							);

							## update price if exists
							if($arrproductprice[$row05->fld_id])
							{
								$arrproducts[$row05->fld_id]["fld_mrp"] = $arrproductprice[$row05->fld_id]["fld_mrp"];
								$arrproducts[$row05->fld_id]["fld_discount"] = $arrproductprice[$row05->fld_id]["fld_discount"];
								$arrproducts[$row05->fld_id]["fld_price"] = $arrproductprice[$row05->fld_id]["fld_price"];
								$arrproducts[$row05->fld_id]["fld_price3bottles"] = $arrproductprice[$row05->fld_id]["fld_price3bottles"];
								$arrproducts[$row05->fld_id]["fld_price6bottles"] = $arrproductprice[$row05->fld_id]["fld_price6bottles"];
								$arrproducts[$row05->fld_id]["fld_price9bottles"] = $arrproductprice[$row05->fld_id]["fld_price9bottles"];
								$arrproducts[$row05->fld_id]["fld_price12bottles"] = $arrproductprice[$row05->fld_id]["fld_price12bottles"];
								$arrproducts[$row05->fld_id]["fld_price24bottles"] = $arrproductprice[$row05->fld_id]["fld_price24bottles"];
							}
							
							## update minimum stock value
							if($arrproductminqty[$row05->fld_id]) $arrproducts[$row05->fld_id]["fld_minqty"] = $arrproductminqty[$row05->fld_id];
							
							## update stock
							$incomingstockqty = 0;
							$outgoingstockqty = 0;
							$stockqty = 0;
							if($arrstockincoming[$row05->fld_id]) $incomingstockqty = $arrstockincoming[$row05->fld_id];
							if($arrstockoutgoing[$row05->fld_id]) $outgoingstockqty = $arrstockoutgoing[$row05->fld_id];
							$stockqty = $incomingstockqty - $outgoingstockqty; 
							$arrproducts[$row05->fld_id]["fld_receivedqty"] = $stockqty;
						}
						## transfer to array
						$arrallstuff["products"] = $arrproducts;
					}
					## end of product database
				}
				
				## get categories
				$arrcategories = array();
				$this->db->select("*");
				$this->db->where("fld_updatedon>=", "$lastupdatedon");
				$result06 = $this->db->get("tbl_category");
				if($result06->num_rows()>0)
				{
					while ($row06 = $result06->unbuffered_row())
					{
						$arrcategories[$row06->fld_id] = array(
							"fld_id" => $row06->fld_id,
							"fld_type" => $row06->fld_type,
							"fld_name" => $row06->fld_name,
							"fld_status" => $row06->fld_status,
							"fld_datetime" => $row06->fld_datetime,
							"fld_updatedby" => $row06->fld_updatedby,
							"fld_updatedon" => $row06->fld_updatedon,
							"fld_del" => $row06->fld_del
						);
					}
					## transfer to array
					$arrallstuff["category"] = $arrcategories;
				}
				## end of categories
				
				## get gifts
				$arrgifts = array();
				$this->db->select("*");
				$this->db->where("fld_updatedon>=", "$lastupdatedon");
				$result006 = $this->db->get("tbl_gifts");
				if($result006->num_rows()>0)
				{
					while ($row006 = $result006->unbuffered_row())
					{
						$arrgifts[$row006->fld_id] = array(
							"fld_id" => $row006->fld_id,
							"fld_clientid" => $row006->fld_clientid,
							"fld_userid" => $row006->fld_userid,
							"fld_outletid" => $row006->fld_outletid,
							"fld_productid" => $row006->fld_productid,
							"fld_fromvalue" => $row006->fld_fromvalue,
							"fld_tovalue" => $row006->fld_tovalue,
							"fld_name" => $row006->fld_name,
							"fld_status" => $row006->fld_status,
							"fld_del" => $row006->fld_del,
							"fld_datetime" => $row006->fld_datetime,
							"fld_updatedby" => $row006->fld_updatedby,
							"fld_updatedon" => $row006->fld_updatedon
						);
					}
					## transfer to array
					$arrallstuff["gifts"] = $arrgifts;
				}
				## end of gifts
				
				## get brands
				$arrbrands = array();
				$this->db->select("*");
				$this->db->where("fld_updatedon>=", "$lastupdatedon");
				$result07 = $this->db->get("tbl_brand");
				if($result07->num_rows()>0)
				{
					while ($row07 = $result07->unbuffered_row())
					{
						$arrbrands[$row07->fld_id] = array(
							"fld_id" => $row07->fld_id,
							"fld_name" => $row07->fld_name,
							"fld_status" => $row07->fld_status,
							"fld_datetime" => $row07->fld_datetime,
							"fld_updatedby" => $row07->fld_updatedby,
							"fld_updatedon" => $row07->fld_updatedon,
							"fld_del" => $row07->fld_del
						);
					}
					## transfer to array
					$arrallstuff["brand"] = $arrbrands;
				}
				## end of brands
				
				## get products to merge
				$arrprod2merge = array();
				$this->db->select("*");
				$this->db->where("fld_updatedon>=", "$lastupdatedon");
				$result07 = $this->db->get("tbl_mergeditems");
				if($result07->num_rows()>0)
				{
					while ($row07 = $result07->unbuffered_row())
					{
						$arrbrands[$row07->fld_id] = array(
							"fromproductid" => $row07->fld_fromproductid,
							"toproductid" => $row07->fld_toproductid
						);
					}
					## transfer to array
					$arrallstuff["mergeproducts"] = $arrbrands;
				}
				## end of brands
				
				## get operator users
				$arrusers = array();
				$this->db->select("*");
				$this->db->where("fld_outletid", $outletid);
				$this->db->where("fld_type", "operator");
				$this->db->where("fld_updatedon>=", "$lastupdatedon");
				$result08 = $this->db->get("tbl_users");
				if($result08->num_rows()>0)
				{
					while ($row08 = $result08->unbuffered_row())
					{
						$arrusers[$row08->fld_id] = array(
							"fld_id" => $row08->fld_id,
							"fld_clientid" => $row08->fld_clientid,
							"fld_warehouseid" => $row08->fld_warehouseid,
							"fld_outletid" => $row08->fld_outletid,
							"fld_type" => $row08->fld_type,
							"fld_otp" => $row08->fld_otp,
							"fld_discount" => $row08->fld_discount,
							"fld_name" => $row08->fld_name,
							"fld_email" => $row08->fld_email,
							"fld_mobile" => $row08->fld_mobile,
							"fld_username" => $row08->fld_username,
							"fld_userpass" => $row08->fld_userpass,
							"fld_status" => $row08->fld_status,
							"fld_datetime" => $row08->fld_datetime,
							"fld_updatedby" => $row08->fld_updatedby,
							"fld_updatedon" => $row08->fld_updatedon,
							"fld_del" => $row08->fld_del,
							"fld_lastlogin" => $row08->fld_lastlogin,
							"fld_lastloginip" => $row08->fld_lastloginip
						);
					}
					## transfer to array
					$arrallstuff["users"] = $arrusers;
				}
				## end of operator users
				
				## get outlet info
				$arroutletinfo = array();
				$this->db->select("*");
				$this->db->where("fld_id", $outletid);
				$this->db->where("fld_updatedon>=", "$lastupdatedon");
				$result09 = $this->db->get("tbl_outlets");
				if($result09->num_rows()>0)
				{
					while ($row09 = $result09->unbuffered_row())
					{
						$arroutletinfo[$row09->fld_id] = array(
							"fld_id" => $row09->fld_id,
							"fld_code" => $row09->fld_code,
							"fld_clientid" => $row09->fld_clientid,
							"fld_zoneid" => $row09->fld_zoneid,
							"fld_name" => $row09->fld_name,
							"fld_email" => $row09->fld_email,
							"fld_mobile" => $row09->fld_mobile,
							"fld_invoiceprefix" => $row09->fld_invoiceprefix,
							"fld_address" => $row09->fld_address,
							"fld_managername" => $row09->fld_managername,
							"fld_manageremail" => $row09->fld_manageremail,
							"fld_managermobile" => $row09->fld_managermobile,
							"fld_status" => $row09->fld_status,
							"fld_datetime" => $row09->fld_datetime,
							"fld_updatedby" => $row09->fld_updatedby,
							"fld_updatedon" => $row09->fld_updatedon,
							"fld_del" => $row09->fld_del
						);
					}
					## transfer to array
					$arrallstuff["outlets"] = $arroutletinfo;
				}
				## end of outlet info
				
				## get billing info
				$arrbillings = array();
				$this->db->select("*");
				$this->db->where("fld_outletid", $outletid);
				$this->db->where("fld_updatedon>=", "$lastupdatedon");
				$result09 = $this->db->get("tbl_billing");
				if($result09->num_rows()>0)
				{
					while ($row09 = $result09->unbuffered_row())
					{
						$arrbillings[$row09->fld_billno] = array(
							"fld_billno" => $row09->fld_billno,
							"fld_cancellation_reason" => $row09->fld_cancellation_reason,
							"fld_cancellation_remarks" => $row09->fld_cancellation_remarks,
							"fld_cancellationdate" => $row09->fld_cancellationdate,
							"fld_cancelrequestby" => $row09->fld_cancelrequestby,
							"fld_status" => $row09->fld_status,
							"fld_updatedby" => $row09->fld_updatedby,
							"fld_updatedon" => $row09->fld_updatedon
						);
					}
					## transfer to array
					$arrallstuff["billing"] = $arrbillings;
				}
				## end of billing info
				
				## update server time 
				$lastupdatedon = date('Y-m-d H:i:s');
				$datahis = array(
					"fld_type" => 'server2local',
					"fld_clientid" => 1,
					"fld_outletid" => $outletid,
					"fld_updatedby" => 0,
					"fld_updatedon" => $lastupdatedon
				);		
				$this->db->insert('tbl_updatehistory', $datahis);
				## end of server time update
				
				$arrallstuff["updatedon"] = $lastupdatedon;
				
				$response = json_encode($arrallstuff);
				echo $response;
			}
		}
		## end of general updates from server to local system by biller
	}
	
	function updateLocalPricing()
	{
		$response = 0;
		$task = $this->input->get_post("task");
		$lastupdatedon = $this->input->get_post("lastupdatedon");
		$clientid = $this->input->get_post("clientid");
		$userid = $this->input->get_post("userid");
		$outletid = $this->input->get_post("outletid");

		$arrallstuff = array();
		if($outletid>0)
		{
			## start product database
			$arrproducts = array();
			
			## get outlet price
			$arrproductprice = array();
			$this->db->select("PRI.*, OI.fld_name as outletname, ZI.fld_name as zonename");
			$this->db->from("tbl_price PRI");
			$this->db->join("tbl_zone ZI", "PRI.fld_zoneid=ZI.fld_id");
			$this->db->join("tbl_outlets OI", "PRI.fld_zoneid=OI.fld_zoneid");
			$this->db->where("OI.fld_id", $outletid);
			$this->db->where("PRI.fld_del", "0");
			$result = $this->db->get();
			if($result->num_rows()>0)
			{
				while ($row = $result->unbuffered_row())
				{
					$arrproducts[$row->fld_productid] = array(
						"fld_id" => $row->fld_productid,
						"fld_mrp" => $row->fld_mrp,
						"fld_discount" => $row->fld_discount,
						"fld_price" => $row->fld_price,
						"fld_price3bottles" => $row->fld_price3bottles,
						"fld_price6bottles" => $row->fld_price6bottles,
						"fld_price9bottles" => $row->fld_price9bottles,
						"fld_price12bottles" => $row->fld_price12bottles,
						"fld_price24bottles" => $row->fld_price24bottles
					);
				}
			}
			$arrallstuff["products"] = $arrproducts;
		}
		$response = json_encode($arrallstuff);
		echo $response;
		## end of general updates from server to local system by biller
	}
}
