<?php

class Periodic_sync extends CI_Model {



	function do_period_sync($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->set_dbprefix('');
					$success = $this->db->query("UPDATE tax_types SET rate=0");
					$this->db->set_dbprefix('ospos_');
					
				$this->db->trans_complete();

				return $success;

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

?>
