<?php
/**
 * Controller name
 *
 * @var string
 */
App::uses('AppController', 'Controller');
class OverallController extends AppController {
	var $user;
	
	var $components = array('ReportMonitor');
	
	// Actions that should NOT be monitored
    public $reportMonitorExclude = array('logintoall');
	
	function beforeFilter(){
		$this->user = $this->Auth->user();
		$this->loadModel('SpecialPermission');
		$is_allow = $this->SpecialPermission->find('count', array('conditions'=>array('user_id'=>$this->user['id'], 'action'=>$this->action) ));
		if($is_allow == 0 && ($this->action != 'getapireport' &&  $this->action != 'get_customers_visits_report' &&  $this->action != 'get_active_users_report')){
			$this->redirect('/customerrors/deniedpermission');
			exit;
		}
		
		if (!in_array($this->request->params['action'], $this->reportMonitorExclude)) {
            $this->ReportMonitor->start($this);
        }
	}
	
	public function afterFilter() {
        if (!in_array($this->request->params['action'], $this->reportMonitorExclude)) {
            $this->ReportMonitor->end($this);
        }
    }
	
	function index(){		
		exit;
	}
	
	public function summary_report(){
		$locations = $this->_locations();
		$reports = $this->_report_list();
		if(!empty($this->request->query['from'])){
			$d1 = $this->request->query['from'];
		}/* else{
			$d1 = date('Y-m-d', strtotime("-7days"));
		} */
		if(!empty($this->request->query['to'])){
			$d2 = $this->request->query['to'];
		} /*else{
			$d2 = date('Y-m-d');
		} */
		$this->set(compact('locations', 'reports', 'd1', 'd2'));
	}
	
	public function logintoall(){
		$locations = $this->_locations();
		$this->set('locations', $locations);
	}
	
	
	public function _locations(){
		if( $_SERVER['SERVER_NAME'] == 'localhost'){
			$locations = array(
				0 => array('id'=>'nigeria', 'title'=>'Nigeria', 'url'=>'http://localhost:8080/y3distribution'),
				1=> array('id'=>'uganda', 'title'=>'Uganda', 'url'=>'http://localhost:8080/mvc/y3/uganda'),
				2=> array('id'=>'kenya', 'title'=>'Kenya', 'url'=>'http://localhost:8080/mvc/y3/kenya'),
				3=> array('id'=>'burundi', 'title'=>'Burundi', 'url'=>'http://localhost:8080/mvc/y3/burundi'),
				4=> array('id'=>'benin', 'title'=>'Benin', 'url'=>'http://localhost:8080/y3distribution'),
				5=> array('id'=>'conakry', 'title'=>'Conakry', 'url'=>'http://localhost:8080/y3distribution'),
				6=> array('id'=>'ssudan', 'title'=>'South Sudan', 'url'=>'http://localhost:8080/mvc/y3/sudan'),
				7=> array('id'=>'angola', 'title'=>'Angola', 'url'=>'http://localhost:8080/mvc/y3/angola'),
				8=> array('id'=>'drc', 'title'=>'DRC', 'url'=>'http://localhost:8080/mvc/y3/angola'),
				9=> array('id'=>'tanzania', 'title'=>'Tanzania', 'url'=>'http://localhost:8080/mvc/y3/tanzania2'),
				10=> array('id'=>'guinea', 'title'=>'Guinea', 'url'=>'http://localhost:8080/y3distribution'),
				
				
			
			);

		}else{
			$locations = array(
                               
				/* 0 => array('id'=>'nigeria','title'=>'Nigeria', 'url'=>'http://y3distribution.smalutioncrm.in'),
				1=> array('id'=>'uganda', 'title'=>'Uganda', 'url'=>'http://ugandacrm.smalution.in/'),
				2=> array('id'=>'kenya', 'title'=>'Kenya', 'url'=>'http://kenya.smalutioncrm.in'),
				3=> array('id'=>'burundi', 'title'=>'Burundi', 'url'=>'http://burundi.smalutioncrm.in'),
				4=> array('id'=>'benin','title'=>'Benin', 'url'=>'http://benin.smalutioncrm.in'),
				5=> array('id'=>'conakry', 'title'=>'Guinea Conakry', 'url'=>'http://guineaconakry.smalutioncrm.in'),
				6=> array('id'=>'ssudan', 'title'=>'South Sudan', 'url'=>'http://sudan.smalutioncrm.in'),
				7=> array('id'=>'angola', 'title'=>'Angola', 'url'=>'http://angola.smalution.net/'),
				8=> array('id'=>'drc', 'title'=>'DRC', 'url'=>'http://drccrm.smalution.in/'),
				9=> array('id'=>'tanzania', 'title'=>'Tanzania',  'url'=>'http://tanzania.smalutioncrm.in'),
				10=> array('id'=>'zambia', 'title'=>'Zambia',  'url'=>'http://zambia.smalutioncrm.in'),
				11=> array('id'=>'sa', 'title'=>'South Africa',  'url'=>'http://sa.smalutioncrm.in'),
				12=> array('id'=>'cameroon', 'title'=>'Cameroon',  'url'=>'http://cameroon.smalutioncrm.in'),
				13=> array('id'=>'ivorycoast', 'title'=>'IvoryCoast',  'url'=>'http://ivorycoast.smalutioncrm.in'),
				14=> array('id'=>'burkinafaso', 'title'=>'Burkinafaso',  'url'=>'http://burkinafaso.smalutioncrm.in'),
				15=> array('id'=>'togo', 'title'=>'Togo',  'url'=>'http://togo.smalutioncrm.in'),
				16=> array('id'=>'brazville', 'title'=>'Brazville',  'url'=>'http://brazzaville.smalutioncrm.in/'),
				17=> array('id'=>'niger', 'title'=>'Niger',  'url'=>'http://niger.smalutioncrm.in'),
				18=> array('id'=>'liberia', 'title'=>'Liberia',  'url'=>'http://liberia.smalutioncrm.in'),
				19=> array('id'=>'ghana','title'=>'Ghana', 'url'=>'http://ghana.smalutioncrm.in'),
				20=> array('id'=>'iran','title'=>'Iran', 'url'=>'http://iran.smalutioncrm.in/'), */
				
				
				0 => array('id'=>'nigeria','title'=>'Nigeria', 'url'=>'http://y3distribution.smalutioncrm.in'),
				1=> array('id'=>'uganda', 'title'=>'Uganda', 'url'=>'http://ugandacrm.smalution.in/'),
				//2=> array('id'=>'kenya', 'title'=>'Kenya', 'url'=>'http://kenya.smalutioncrm.in'),
				2=> array('id'=>'burundi', 'title'=>'Burundi', 'url'=>'http://burundi.smalutioncrm.in'),
				3=> array('id'=>'benin','title'=>'Benin', 'url'=>'http://benin.smalutioncrm.in'),
				4=> array('id'=>'conakry', 'title'=>'Guinea Conakry', 'url'=>'http://guineaconakry.smalutioncrm.in'),
				5=> array('id'=>'ssudan', 'title'=>'South Sudan', 'url'=>'http://sudan.smalutioncrm.in'),
				6=> array('id'=>'angola', 'title'=>'Angola', 'url'=>'http://angola.smalution.net'),
				7=> array('id'=>'drc', 'title'=>'DRC', 'url'=>'https://drccrm.smalution.in'),
				8=> array('id'=>'tanzania', 'title'=>'Tanzania',  'url'=>'http://tanzania.smalutioncrm.in'),
				9=> array('id'=>'zambia', 'title'=>'Zambia',  'url'=>'http://zambia.smalutioncrm.in'),
				10=> array('id'=>'sa', 'title'=>'South Africa',  'url'=>'http://sa.smalutioncrm.in'),
				11=> array('id'=>'iran', 'title'=>'Iran', 'url'=>'http://iran.smalutioncrm.in'),
				12=> array('id'=>'ivorycoast', 'title'=>'IvoryCoast',  'url'=>'http://ivorycoast.smalutioncrm.in'),
				//12=> array('id'=>'cameroon', 'title'=>'Cameroon',  'url'=>'http://cameroon.smalutioncrm.in'),
				
				//14=> array('id'=>'burkinafaso', 'title'=>'Burkinafaso',  'url'=>'http://burkinafaso.smalutioncrm.in'),
				//15=> array('id'=>'togo', 'title'=>'Togo',  'url'=>'http://togo.smalutioncrm.in'),
				//16=> array('id'=>'brazville', 'title'=>'Brazville',  'url'=>'http://brazzaville.smalutioncrm.in/'),
				//17=> array('id'=>'niger', 'title'=>'Niger',  'url'=>'http://niger.smalutioncrm.in'),
				//18=> array('id'=>'liberia', 'title'=>'Liberia',  'url'=>'http://liberia.smalutioncrm.in'),
				//19=> array('id'=>'ghana','title'=>'Ghana', 'url'=>'http://ghana.smalutioncrm.in'),
				//20=> array('id'=>'iran','title'=>'Iran', 'url'=>'http://iran.smalutioncrm.in/'),

				

			);
		}
		return $locations;
	}
	 
	private function _report_list(){
		$reports = array(
			'num_of_customers'=>'No of customers',
			'visited_customers'=>'Visited customers',
			'non_visited_customers'=>'Non Visited customers',
			'no_of_created_customers'=>'No of created customers',
			'customer_trend_up' => 'Customer trend up',
			'customer_trend_same' => 'Customer trend same',
			'customer_trend_down' => 'Customer trend down',
			'actual_users' => 'Actual Users',
			'active_users'=>'Active users',
			'per_active_users'=>'% of Active Users',
			'non_active_users'=>'Non active users',
			'total_calls'=>'Total no of calls',
			'target_calls'=>'Target calls',
			'effective_sale_call'=>'Effective sales calls',
			'sale_inunit'=>'Sales in Unit',
			'rs_sale_inunit'=>'Redistribution sales in units',
			'expenses'=>'Expenses'
		);
		return $reports;
	}	
	
	function getapireport(){ //pr($this->request->data);
		//$url = $this->request->data['url'].'/api/get_reports?from='.$this->request->data['d1'].'&to='.$this->request->data['d2']; pr($url);		
		$url = $this->request->data['url'].'/api/get_reports?from='.$this->request->data['d1'].'&to='.$this->request->data['d2'].'&t-month='.$this->request->data['tmonth']; //pr($url);
		$result = array();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		//curl_setopt($ch, CURLOPT_PARAM, $this->request->data);
		curl_setopt($ch, CURLOPT_FAILONERROR, 0); // Fail on errors
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
					
		$response = curl_exec($ch); //pr($response);
		
		if (curl_errno($ch)) {
			$error_msg = curl_error($ch);
			//echo $error_msg;
		}

		curl_close($ch);
		if($response){
			$result['error'] = "0";
			$result['data'] = $response;
		}else{
			$result['error'] = "1";
		}
		//echo $result;
		echo json_encode($result);
		exit;
	}
	
	public function customers_visits_report(){
		$locations = $this->_locations();
		
		/* $locations = array(
				0 => array('id'=>'nigeria','title'=>'Nigeria', 'url'=>'http://y3distribution.smalutioncrm.in'),
				1=> array('id'=>'burundi', 'title'=>'Burundi', 'url'=>'http://burundi.smalutioncrm.in'),
			); */
		
		if(!empty($this->request->query['from'])){
			$d1 = $this->request->query['from'];
		}/* else{
			$d1 = date('Y-m-01');
		} */
		if(!empty($this->request->query['to'])){
			$d2 = $this->request->query['to'];
		}/* else{
			$d2 = date('Y-m-d');
		} */
		$this->set(compact('locations', 'd1', 'd2'));
	}
	
	function get_customers_visits_report(){ //pr($this->request->data);
		//$url = $this->request->data['url'].'/api/get_customers_visits_report?from='.$this->request->data['d1'].'&to='.$this->request->data['d2']; //pr($url);
		$url = $this->request->data['url'].'/api/get_customers_visits_report?from='.$this->request->data['d1'].'&to='.$this->request->data['d2'].'&t-month='.$this->request->data['tmonth']; //pr($url);
		$result = $response = array();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		//curl_setopt($ch, CURLOPT_PARAM, $this->request->data);
		curl_setopt($ch, CURLOPT_FAILONERROR, 0); // Fail on errors
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
					
		$response = curl_exec($ch); //pr($response);
		
		if (curl_errno($ch)) {
			$error_msg = curl_error($ch);
			//echo $error_msg;
		}

		curl_close($ch); 
		if($response){
			$result['error'] = "0";
			$result['data'] = $response;
		}else{
			$result['error'] = "1";
		}
		//echo $result;
		echo json_encode($result);
		exit;
	}
	
	public function active_users_report(){ 
		$locations = $this->_locations();
		
		/* $locations = array(
				0 => array('id'=>'nigeria','title'=>'Nigeria', 'url'=>'http://y3distribution.smalutioncrm.in'),
				1=> array('id'=>'burundi', 'title'=>'Burundi', 'url'=>'http://burundi.smalutioncrm.in'),
			); */
		
		if(!empty($this->request->query['from'])){
			$d1 = $this->request->query['from'];
		}/* else{
			$d1 = date('Y-m-01');
		} */
		if(!empty($this->request->query['to'])){
			$d2 = $this->request->query['to'];
		}/* else{
			$d2 = date('Y-m-d');
		} */
		$this->set(compact('locations', 'd1', 'd2'));
	}
	
	function get_active_users_report(){ 
		$url = $this->request->data['url'].'/api/get_active_users_report?from='.$this->request->data['d1'].'&to='.$this->request->data['d2']; pr($url);
		$result = $response = array();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		//curl_setopt($ch, CURLOPT_PARAM, $this->request->data);
		curl_setopt($ch, CURLOPT_FAILONERROR, 0); // Fail on errors
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
					
		$response = curl_exec($ch); //pr($response);
		
		if (curl_errno($ch)) {
			$error_msg = curl_error($ch);
			//echo $error_msg;
		}

		curl_close($ch); 
		if($response){
			$result['error'] = "0";
			$result['data'] = $response;
		}else{
			$result['error'] = "1";
		}
		//echo $result;
		echo json_encode($result);
		exit;
	}
}
