<?php

class Periodic_sync extends CI_Model {



	function do_period_sync($location_code) {

		
		$this->do_periodic_sync_customer_add($location_code);
		$this->do_periodic_sync_customer_card_update($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('periodicSync', 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{
				//$data=base64_decode(json_decode($result)->data);
				$data=json_decode($result)->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 3;///Server not found. please check internet connection.
		}
	}
	
	function do_periodic_sync_customer_card_update($location_code) {


		$this->db->trans_start();
		$sql = "SELECT id,state,updated_by 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)){
			$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['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 3;///Server not found. please check internet connection.
			}
		}else{
			return true;
		}
	}
	function do_periodic_sync_customer_add($location_code) {
		$this->db->trans_start();
		$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)){

			$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['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 3;///Server not found. please check internet connection.
			}
		}else{
			return true;
		}
	}
}

?>
