<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sync extends MX_Controller 
{
	public function __construct() 
	{
		parent::__construct();
	}
	
	public function collaborator () 
	{
		if($this->input->is_cli_request()){
			error_reporting(E_ALL);
			$this->load->model('Services_model');
			$totalpage = 1;
			
			$default = $this->Services_model->getCollaborator(2,1,5000);
			$data = mb_convert_encoding($default, 'UTF-8', 'ISO-8859-1');
			if($data){
				$xml = simplexml_load_string($data);
				$xml->registerXPathNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");	
				$xml = $xml->xpath('//S:Body');
				$xml = $xml[0];
				$xml->registerXPathNamespace("ns2", "http://process.wsim.viettel.com/");
				$totalpage = $xml->xpath('//return//totalpage');
				$totalpage = (int)$totalpage[0];
			}
			
			for ($i=1; $i<=$totalpage; $i++){
				$result = $this->Services_model->getCollaborator(2,$i,500);
				$data = mb_convert_encoding($result, 'UTF-8', 'ISO-8859-1');
				if($data && $data != -1){
					$xml = simplexml_load_string($data);
					$xml->registerXPathNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");
					$xml = $xml->xpath('//S:Body');
					$xml = $xml[0];
					$xml->registerXPathNamespace("ns2", "http://process.wsim.viettel.com/");
					$lst = $xml->xpath('//return//lst');
					foreach($lst as $item){
						$item = (array)$item;
				
						$data = array(
								'address'=> (string)$item['address'],
								'code'=>(string)$item['code'],
								'district'=>(string)$item['district'],
								'email'=>(string)$item['email'],
								'id'=>(int)$item['id'],
								'full_name'=>(string)$item['name'],
								'cellphone'=>(string)$item['phone'],
								'precinct'=>(string)$item['precinct'],
								'province'=>(string)$item['province'],
								'created_time'=>date('Y-m-d H:i:s'),
								'updated_time'=>date('Y-m-d H:i:s')
							);
						write_file('./log/sync_collaborator.log', var_export($data, TRUE), FOPEN_WRITE_CREATE);
						if ((string)$item['phone']){
							$this->Services_model->syncCollaborator($data);
						}
					}
				}else{
					$logMsg = date('Y-m-d H:i:s') . " Result is null \n";
					write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
				}
			}
		}else{
			show_404();
		}
	}
	
	public function deleteCollaborator () 
	{
		
		if($this->input->is_cli_request()){
			error_reporting(E_ALL);
			$this->load->model('Services_model');
			$totalpage = 1;
	
			$default = $this->Services_model->getCollaborator(2,1,5000);
			$data = mb_convert_encoding($default, 'UTF-8', 'ISO-8859-1');
			if($data){
				$xml = simplexml_load_string($data);
				$xml->registerXPathNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");		
				$xml = $xml->xpath('//S:Body');
				$xml = $xml[0];
				$xml->registerXPathNamespace("ns2", "http://process.wsim.viettel.com/");	
				$totalpage = $xml->xpath('//return//totalpage');
				$totalpage = (int)$totalpage[0];
			}

			$collaboratorIds = array();
			
			$logMsg = "Start XML Process: " . date('Y-m-d H:i:s') . "\n";
			write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
			
			for ($i=1; $i<$totalpage; $i++){
				$result = $this->Services_model->getCollaborator(2,$i,5000);
				$data = mb_convert_encoding($result, 'UTF-8', 'ISO-8859-1');
				if($data && $data != -1){
					$xml = simplexml_load_string($data);
					$xml->registerXPathNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");
					$xml = $xml->xpath('//S:Body');
					$xml = $xml[0];
					$xml->registerXPathNamespace("ns2", "http://process.wsim.viettel.com/");
					$lst = $xml->xpath('//return//lst');
					foreach($lst as $item){
						$item = (array)$item;
						if ((string)$item['phone']){
							array_push($collaboratorIds, (int)$item['id']);
						}
					}
				}else{
					$logMsg = date('Y-m-d H:i:s') . " Result is null \n";
					write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
				}
			}
			
			$logMsg = "Finish XML Process: " . date('Y-m-d H:i:s') . "\n";
			write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
			
			$collaboratorIdsSync = $this->Services_model->getAllCollaboratorID();
			
			if ($collaboratorIdsSync){
				foreach($collaboratorIdsSync as $collaborator){
					$id = (int)$collaborator->id;
					if (in_array($id, $collaboratorIds) == false){
						$this->Services_model->deleteCollaborator($id);
					}
				}
			}
		}else{
			show_404();
		}

	}
}