<?php
ini_set('max_execution_time', 0);
class Regular_sync extends CI_Model {



	function do_regular_sync_user($location_code) {


		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);

		if($status==0){
			$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

			$this->db->trans_start();

			///user sync//
			$this->db->from('employees');
			$employee_data_result= $this->db->get();

			$employee_data=array();
			foreach($employee_data_result->result_array() as $row)
			{
				$employee_data[]=$row;
			}
			$this->db->trans_complete();

			$api_data = array();
			$api_data['api_user'] = API_USER;
			$api_data['api_password'] = API_PASSWORD;
			$api_data['location_code'] =$location_code;
			$api_data['employees'] =$employee_data;
			$api_data_final = json_encode($api_data);


			$result = $client->__soapCall('regularSyncEmployee', array('data' => $api_data_final));

			$json_data=json_decode($result);

			$error_found=json_decode($result)->error_found;
			$message=json_decode($result)->message;
			if($error_found==1){
				return 4;///Un-authorized Access.
			}else{

				$default_permission_array[2][]="sales";
				$default_permission_array[2][]="customers";
				$default_permission_array[2][]="reports";
				$default_permission_array[2][]="employees";
				$default_permission_array[2][]="dbbackup";



				$default_permission_array[3][]="sales";
				$default_permission_array[3][]="customers";
				$default_permission_array[3][]="reports";
				$default_permission_array[3][]="employees";


				$default_permission_array[4][]="sales";
				$default_permission_array[4][]="customers";
				$default_permission_array[4][]="reports";
				$default_permission_array[4][]="employees";
				$default_permission_array[4][]="dbbackup";

				$this->db->trans_start();

				if(count($json_data->data->data)){
					foreach ($json_data->data->data as $key => $value) {

						if($key=="update"){
							$this->db->set_dbprefix('');
							foreach ($value as $update_key => $update_value) {
								$success = $this->db->query($update_value);
							}

							$this->db->set_dbprefix('ospos_');

						}if($key=="role_update"){


							$this->db->set_dbprefix('');
							foreach ($value as $role_update_key => $role_update_value) {

								$success = $this->db->query("DELETE FROM ospos_permissions WHERE person_id='".$role_update_key."'");

								foreach ($default_permission_array[$role_update_value] as $ro_key => $ro_value) {
									$insert_query_permission=" INSERT INTO ospos_permissions(
						module_id,
						person_id
						) VALUES('".$ro_value."','".$role_update_key."')";
									$this->db->query($insert_query_permission);
								}
							}
							$this->db->set_dbprefix('ospos_');
						}
						elseif($key=="delete"){
							$this->db->set_dbprefix('');
							foreach ($value as $delete_key => $delete_value) {
								$success = $this->db->query($delete_value);
							}
							$this->db->set_dbprefix('ospos_');

						}elseif($key=="insert"){
							$this->db->set_dbprefix('');

							foreach ($value as $insert_key => $insert_value) {

								$insert_query=" INSERT INTO ospos_people(
					first_name,
					phone_number,
					email,
					is_customer
					) VALUES('".$insert_value->real_name."','".$insert_value->phone."','".$insert_value->email."','0')";

								$this->db->query($insert_query);

								$person_id=$this->db->insert_id();

								$insert_query_employee=" INSERT INTO ospos_employees(
					username,
					password,
					person_id,
					pos_location_code,
					deleted,
					is_update
					) VALUES('".$insert_value->user_id."','".$insert_value->password."','".$person_id."','".LOCATION_CODE."','0','0')";

								$this->db->query($insert_query_employee);

								foreach ($default_permission_array[$insert_value->role_id] as $ro_key => $ro_value) {
									$insert_query_permission=" INSERT INTO ospos_permissions(
														module_id,
														person_id
														) VALUES('".$ro_value."','".$person_id."')";
									$this->db->query($insert_query_permission);
								}

							}
							$this->db->set_dbprefix('ospos_');
						}
					}
					$this->db->trans_complete();
					return $success;
				}
			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}


	function do_regular_sync_stock_moves($location_code) {


		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);



		if($status==0){
			$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

			$api_data = array();
			$api_data['api_user'] = API_USER;
			$api_data['api_password'] = API_PASSWORD;
			$api_data['location_code'] =$location_code;
			$api_data_final = json_encode($api_data);

			$result = $client->__soapCall('regularSyncStockMoves', array('data' => $api_data_final));

			$error_found=json_decode($result)->error_found;
			$message=json_decode($result)->message;
			if($error_found==1){
				return 4;///Un-authorized Access.
			}else{
				$json_data=json_decode($result)->data;

				if(trim($json_data)){

				ini_set('max_execution_time', 0);
				
					$this->db->trans_start();
					$this->db->set_dbprefix('');
					$success = $this->db->query("TRUNCATE TABLE stock_moves");
					$success = $this->db->query($json_data);
					$this->db->set_dbprefix('ospos_');
					$this->db->trans_complete();
					return $success;

				}else{
					return true;
				}
			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}

	function do_regular_sync_customer_add($location_code) {


		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);



		if($status==0){

			$this->db->trans_start();
			///user sync//
			$this->db->from('people');
			$this->db->where(array('is_customer'=>1,'is_sync'=>0));
			$people_data_result= $this->db->get();

			$people_data=array();
			foreach($people_data_result->result_array() as $row)
			{
				$people_data[]=$row;
			}
			$this->db->trans_complete();

			if(count($people_data)){

				$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

				$api_data = array();
				$api_data['api_user'] = API_USER;
				$api_data['api_password'] = API_PASSWORD;
				$api_data['location_code'] =$location_code;
				$api_data['new_customer'] =$people_data;
				$api_data_final = json_encode($api_data);

				$result = $client->__soapCall('regularSyncCustomerAdd', array('data' => $api_data_final));

				$error_found=json_decode($result)->error_found;
				$message=json_decode($result)->message;
				if($error_found==1){
					return 4;///Un-authorized Access.
				}else{

					$json_data=json_decode($result)->data->added_people_id;

					if(count($json_data)){
						$this->db->trans_start();
						$this->db->set_dbprefix('');

						foreach ($json_data as $p_key => $p_value) {

							$this->db->query("UPDATE ospos_sales SET customer_id='".$p_key."' WHERE customer_id='".$p_value."' AND is_sync=0");
							$success = $this->db->query("DELETE FROM ospos_people WHERE person_id='".$p_value."'");
						}
						$this->db->set_dbprefix('ospos_');
						$this->db->trans_complete();
						return $success;

					}else{
						return true;
					}
				}
			}else{
				return true;
			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}

	function do_regular_sync_customer_card_update($location_code) {


		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);



		if($status==0){

			$this->db->trans_start();
			///user sync//

			$sql = "SELECT id,state FROM pos_ospos_customers WHERE state !=''";

			$query = $this->db->query($sql);

			$people_data=array();

			foreach ($query->result() as $row) {
				$people_data[]=$row;
			}

			$this->db->trans_complete();

			if(count($people_data)){

				$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

				$api_data = array();
				$api_data['api_user'] = API_USER;
				$api_data['api_password'] = API_PASSWORD;
				$api_data['location_code'] =$location_code;
				$api_data['new_customer'] =$people_data;
				$api_data_final = json_encode($api_data);

				$result = $client->__soapCall('regularSyncCustomerCardUpdate', array('data' => $api_data_final));

				$error_found=json_decode($result)->error_found;
				$message=json_decode($result)->message;
				if($error_found==1){
					return 4;///Un-authorized Access.
				}else{

					$json_data=json_decode($result)->data->added_people_id;
					if(count($json_data)){
						$this->db->trans_start();
						$this->db->set_dbprefix('');
						foreach ($json_data as $p_key => $p_value) {
							$success =$this->db->query("UPDATE pos_ospos_customers SET state='' WHERE id='".$p_value."'");
						}
						$this->db->set_dbprefix('ospos_');
						$this->db->trans_complete();
						return $success;

					}else{
						return true;
					}
				}
			}else{
				return true;
			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}

	function do_regular_sync_customer($location_code) {
		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);

		if($status==0){

			$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

			$api_data = array();
			$api_data['api_user'] = API_USER;
			$api_data['api_password'] = API_PASSWORD;
			$api_data['location_code'] =$location_code;
			$api_data_final = json_encode($api_data);


			$result = $client->__soapCall('regularSyncCustomer', array('data' => $api_data_final));

			$error_found=json_decode($result)->error_found;
			$message=json_decode($result)->message;

			if($error_found==1){
				return 4;///Un-authorized Access.
			}else{
				$data=json_decode($result)->data;
				if(count($data)){
					$this->db->trans_start();
					foreach ($data->drop_tables as $drop_tables_key => $drop_tables_value) {
						$drop_tables_value_decode=base64_decode($drop_tables_value);
						$this->db->set_dbprefix('');
						$success = $this->db->query($drop_tables_value_decode);
						$this->db->set_dbprefix('ospos_');
					}

					foreach ($data->create_tables as $create_tables_key => $create_tables_value) {
						$create_tables_value_decode=base64_decode($create_tables_value);
						$this->db->set_dbprefix('');
						$success = $this->db->query($create_tables_value_decode);
						$this->db->set_dbprefix('ospos_');
					}

					foreach ($data->insert_data as $insert_data_key => $insert_data_value) {
						$insert_data_value_decode=base64_decode($insert_data_value);
						$this->db->set_dbprefix('');
						$success = $this->db->query($insert_data_value_decode);
						$this->db->set_dbprefix('ospos_');
					}
					$this->db->trans_complete();

					return $success;
				}else{
					return true;
				}
			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}


	function do_regular_sync_local_sale($location_code) {

		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);

		if($status==0){

			$this->db->trans_start();
			$this->db->set_dbprefix('ospos_');

			$this->db->from('sales');
			$this->db->where(array('is_sync'=>0));
			$ospos_sales_result= $this->db->get();

			$ospos_sales_data=array();
			$ospos_sales_items_data=array();
			$ospos_sales_payments_data=array();



			foreach($ospos_sales_result->result_array() as $row){
				$ospos_sales_data[$row['sale_id']]=$row;


				$this->db->from('sales_items');
				$this->db->where(array('sale_id'=>$row['sale_id']));
				$ospos_sales_items_result= $this->db->get();

				foreach($ospos_sales_items_result->result_array() as $item_row){
					$ospos_sales_items_data[$row['sale_id']][]=$item_row;
				}
				$this->db->from('sales_payments');
				$this->db->where(array('sale_id'=>$row['sale_id']));
				$ospos_sales_payments_result= $this->db->get();

				foreach($ospos_sales_payments_result->result_array() as $payment_row){
					$ospos_sales_payments_data[$row['sale_id']][]=$payment_row;
				}
			}

			$this->db->trans_complete();

			if(count($ospos_sales_payments_data)){

				$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

				$api_data = array();
				$api_data['api_user'] = API_USER;
				$api_data['api_password'] = API_PASSWORD;
				$api_data['location_code'] =$location_code;
				$api_data['ospos_sales'] =$ospos_sales_data;
				$api_data['ospos_sales_items'] =$ospos_sales_items_data;
				$api_data['ospos_sales_payments'] =$ospos_sales_payments_data;

				$api_data_final = json_encode($api_data);
				$result = $client->__soapCall('regularSyncSalesAdd', array('data' => $api_data_final));

				$error_found=json_decode($result)->error_found;
				$message=json_decode($result)->message;
				if($error_found==1){
					return 4;///Un-authorized Access.
				}else{

					$json_data=json_decode($result)->data;

					if(count($json_data->added_receipt_no)){
						$this->db->trans_start();
						$this->db->set_dbprefix('');

						foreach ($json_data->added_receipt_no as $r_key => $r_value) {

							$update_query="UPDATE ospos_sales SET is_sync=1 WHERE receipt_no='".$r_value."'";
							$success = $this->db->query($update_query);
						}

						if(count($json_data->customer_data_update)){
							foreach ($json_data->customer_data_update as $c_key => $c_value) {

								$update_customer="UPDATE pos_ospos_customers SET point=point+'".$c_value->point."', previous_due='".$c_value->previous_due."' WHERE id=".$c_value->customer_id;
								$success = $this->db->query($update_customer);
							}
						}
						$this->db->set_dbprefix('ospos_');
						$this->db->trans_complete();
						return $success;
					}else{
						return true;
					}
				}
			}else{
				return true;
			}

		}else{
			return 3;///Server not found. please check internet connection.
		}
	}


	function do_regular_sync_global_sale($location_code) {

		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);

		if($status==0){
			$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));
			$api_data = array();
			$api_data['api_user'] = API_USER;
			$api_data['api_password'] = API_PASSWORD;
			$api_data['location_code'] =$location_code;

			$api_data_final = json_encode($api_data);
			$result = $client->__soapCall('regularSyncGlobalAdd', array('data' => $api_data_final));

			$error_found=json_decode($result)->error_found;
			$message=json_decode($result)->message;
			if($error_found==1){
				return 4;///Un-authorized Access.
			}else{

				$json_data=json_decode($result)->data;

				if(count($json_data->receipt_no)){

					$receipt_no_str="'".implode("','",$json_data->receipt_no)."'";

					$sql = "SELECT receipt_no FROM ospos_sales WHERE receipt_no IN (".$receipt_no_str.")";

					$query = $this->db->query($sql);

					$receipt_no_exist=array();

					foreach ($query->result() as $row) {
						$receipt_no_exist[]=$row->receipt_no;
					}

					foreach ($json_data->ospos_sales as $s_key => $s_value) {

						if(!in_array($s_value->receipt_no,$receipt_no_exist)){

							$this->db->trans_start();
							$this->db->set_dbprefix('');
							$insert_query="INSERT INTO ospos_sales(sale_time,
customer_id,
employee_id,
comment,
receive_location,
payment_type,
location,
is_sync,
receipt_no,
acc_location,
is_global,
is_delivered,
is_sync_golbal_flag,
bag_big,
bag_medium,
bag_small,
is_global_delivery_sync) VALUES("
							."'".$s_value->sale_time."',"
							."'".$s_value->customer_id."',"
							."'".$s_value->employee_id."',"
							."'".$s_value->comment."',"
							."'".$s_value->receive_location."',"
							."'".$s_value->payment_type."',"
							."'".$s_value->location."',"
							."'1',"
							."'".$s_value->receipt_no."',"
							."'".$s_value->acc_location."',"
							."'".$s_value->is_global."',"
							."'".$s_value->is_delivered."',"
							."'".$s_value->is_sync_golbal_flag."',"
							."'".$s_value->bag_big."',"
							."'".$s_value->bag_medium."',"
							."'".$s_value->bag_small."',"
							."'".$s_value->is_global_delivery_sync."')";

							if($this->db->query($insert_query)){
								$last_inserted_id = $this->db->insert_id();

								foreach ($json_data->ospos_sales_items as $si_key => $si_value) {
									if($si_key==$s_value->sale_id){
										foreach ($si_value as $si_each_key => $si_each_value) {
											$insert_query_item="INSERT INTO ospos_sales_items(sale_id,
								stock_id,
								description,
								lot_no,
								line,
								quantity_purchased,
								item_cost_price,
								item_unit_price,
								discount_percent,
								total_discount,
								total_sale_amount,
								vat_enable_price,
								vat_percentage,
								vat_amount) VALUES("
											."'".$last_inserted_id."',"
											."'".$si_each_value->stock_id."',"
											."'".$si_each_value->description."',"
											."'".$si_each_value->lot_no."',"
											."'".$si_each_value->line."',"
											."'".$si_each_value->quantity_purchased."',"
											."'".$si_each_value->item_cost_price."',"
											."'".$si_each_value->item_unit_price."',"
											."'".$si_each_value->discount_percent."',"
											."'".$si_each_value->total_discount."',"
											."'".$si_each_value->total_sale_amount."',"
											."'".$si_each_value->vat_enable_price."',"
											."'".$si_each_value->vat_percentage."',"
											."'".$si_each_value->vat_amount."')";

											$success=$this->db->query($insert_query_item);
										}
									}
								}
								foreach ($json_data->ospos_sales_payments as $sp_key => $sp_value) {
									if($sp_key==$s_value->sale_id){

										foreach ($sp_value as $sp_each_key => $sp_each_value) {
											$insert_query_payment="INSERT INTO ospos_sales_payments(sale_id,
								payment_type,
								payment_amount,
								bank_id,
								branch_name,
								account_holder_name,
								account_no,
								card_no,
								check_no,
								check_date,
								credit_card_expire_month,
								credit_card_expire_year) VALUES("
											."'".$last_inserted_id."',"
											."'".$sp_each_value->payment_type."',"
											."'".$sp_each_value->payment_amount."',"
											."'".$sp_each_value->bank_id."',"
											."'".$sp_each_value->branch_name."',"
											."'".$sp_each_value->account_holder_name."',"
											."'".$sp_each_value->account_no."',"
											."'".$sp_each_value->card_no."',"
											."'".$sp_each_value->check_no."',"
											."'".$sp_each_value->check_date."',"
											."'".$sp_each_value->credit_card_expire_month."',"
											."'".$sp_each_value->credit_card_expire_year."')";
											$success=$this->db->query($insert_query_payment);
										}
									}
								}
							}
							$this->db->set_dbprefix('ospos_');
							$this->db->trans_complete();
						}
					}
					return $success;
				}else{
					return true;
				}
			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}

	function do_regular_sync_global_sale_delivery($location_code) {

		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);

		if($status==0){



			$this->db->trans_start();
			$this->db->set_dbprefix('ospos_');

			$this->db->from('sales');
			$this->db->where(array('is_global'=>1,'is_delivered'=>1,'is_global_delivery_sync'=>0));
			$ospos_sales_result= $this->db->get();
			foreach($ospos_sales_result->result_array() as $row){
				$ospos_sales_data[$row['receipt_no']]['bag_big']=$row['bag_big'];
				$ospos_sales_data[$row['receipt_no']]['bag_medium']=$row['bag_medium'];
				$ospos_sales_data[$row['receipt_no']]['bag_small']=$row['bag_small'];
			}

			$this->db->trans_complete();

			if(count($ospos_sales_data)){

				$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));
				$api_data = array();
				$api_data['api_user'] = API_USER;
				$api_data['api_password'] = API_PASSWORD;
				$api_data['location_code'] =$location_code;
				$api_data['data'] =$ospos_sales_data;

				$api_data_final = json_encode($api_data);
				$result = $client->__soapCall('regularSyncGlobalUpdate', array('data' => $api_data_final));

				$error_found=json_decode($result)->error_found;
				$message=json_decode($result)->message;
				if($error_found==1){
					return 4;///Un-authorized Access.
				}else{

					$json_data=json_decode($result)->data;
					if(count($json_data)){
						foreach ($json_data as $key => $value) {
							$this->db->trans_start();
							$this->db->set_dbprefix('');

							$update_flag="UPDATE ospos_sales SET is_global_delivery_sync=1 WHERE receipt_no='".$value."'";
							$success = $this->db->query($update_flag);
							$this->db->set_dbprefix('ospos_');
							$this->db->trans_complete();
						}
						return $success;
					}else{
						return true;
					}
				}
			}else{
				return true;
			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}
	
	
	
	
	function do_regular_sync_stock_moves_break_lastmonths($location_code) {

		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);

		if($status==0){
			$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));
			$api_data = array();
			$api_data['api_user'] = API_USER;
			$api_data['api_password'] = API_PASSWORD;
			$api_data['location_code'] =$location_code;
			$api_data_final = json_encode($api_data);

			$result = $client->__soapCall('regularSyncStockMovesDates', array('data' => $api_data_final));

			$error_found=json_decode($result)->error_found;
			$message=json_decode($result)->message;
			
			if($error_found==1){
				return 4;///Un-authorized Access.
			}else{
				$json_data=json_decode($result)->data;

				$end_date=$json_data->max_date;
				$start_date= date("Y-m-d", strtotime("-1 month", strtotime($json_data->max_date)));

				
				$api_data = array();
				$api_data['api_user'] = API_USER;
				$api_data['api_password'] = API_PASSWORD;
				$api_data['location_code'] =$location_code;
				$api_data['start_date'] =$start_date;
				$api_data['end_date'] =$end_date;
				$api_data_final = json_encode($api_data);
				$result = $client->__soapCall('regularSyncStockMovesDateWise', array('data' => $api_data_final));

				$error_found=json_decode($result)->error_found;
				$message=json_decode($result)->message;

				if($error_found==1){
					return 4;///Un-authorized Access.
				}else{
					$json_data=json_decode($result)->data;

					if(trim($json_data)){
						$this->db->trans_start();
						$this->db->set_dbprefix('');
						$this->db->query("DELETE FROM stock_moves WHERE loc_code='".$location_code."' AND DATE_FORMAT(tran_date,'%Y-%m-%d')>='".$start_date."' AND DATE_FORMAT(tran_date,'%Y-%m-%d')<='".$end_date."'");
						$success = $this->db->query($json_data);
						$this->db->set_dbprefix('ospos_');
						$this->db->trans_complete();
						return $success;
					}
				}
			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}
	
	function do_regular_sync_stock_moves_break($location_code) {

		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);

		if($status==0){
			$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));
			$api_data = array();
			$api_data['api_user'] = API_USER;
			$api_data['api_password'] = API_PASSWORD;
			$api_data['location_code'] =$location_code;
			$api_data_final = json_encode($api_data);

			$result = $client->__soapCall('regularSyncStockMovesDates', array('data' => $api_data_final));

			$error_found=json_decode($result)->error_found;
			$message=json_decode($result)->message;
			if($error_found==1){
				return 4;///Un-authorized Access.
			}else{
				$json_data=json_decode($result)->data;


				$start_year = date("Y",strtotime($json_data->min_date));
				$end_year = date("Y",strtotime($json_data->max_date));


				$date_loop=array();

				$loopid=0;
				for($i=$start_year;$i<=$end_year;$i++){

					$date_loop[$loopid]['start_date']=$i."-01-01";
					$date_loop[$loopid]['end_date']=$i."-06-30";
					$loopid++;

					$date_loop[$loopid]['start_date']=$i."-07-01";
					$date_loop[$loopid]['end_date']=$i."-12-31";
					$loopid++;

				}

				$first_loop=1;
				foreach ($date_loop as $key => $value) {
					$api_data = array();
					$api_data['api_user'] = API_USER;
					$api_data['api_password'] = API_PASSWORD;
					$api_data['location_code'] =$location_code;
					$api_data['start_date'] =$value['start_date'];
					$api_data['end_date'] =$value['end_date'];
					$api_data_final = json_encode($api_data);
					$result = $client->__soapCall('regularSyncStockMovesDateWise', array('data' => $api_data_final));

					$error_found=json_decode($result)->error_found;
					$message=json_decode($result)->message;


					if($error_found==1){
						return 4;///Un-authorized Access.
					}else{
						$json_data=json_decode($result)->data;
						if(trim($json_data)){
							$this->db->trans_start();
							$this->db->set_dbprefix('');

							if($first_loop==1){
								$success = $this->db->query("TRUNCATE TABLE stock_moves");
								$first_loop=0;
							}
							$success = $this->db->query($json_data);
							$this->db->set_dbprefix('ospos_');
							$this->db->trans_complete();
						}
					}
				}
				//return $success;

			}
		}else{
			return 3;///Server not found. please check internet connection.
		}
	}
	
}
?>
