<?php
/**
 * Controller name
 *
 * @var string
 */
App::uses('AppController', 'Controller');
class OverallController extends AppController {
	var $user;
	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->redirect('/customerrors/deniedpermission');
			exit;
		}
	}
	
	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'=>'guinea', 'title'=>'Guinea', 'url'=>'http://localhost:8080/y3distribution'),
				6=> array('id'=>'conakry', 'title'=>'Conakry', 'url'=>'http://localhost:8080/y3distribution'),
				7=> array('id'=>'ssudan', 'title'=>'South Sudan', 'url'=>'http://localhost:8080/mvc/y3/sudan'),
				8=> array('id'=>'angola', 'title'=>'Angola', 'url'=>'http://localhost:8080/mvc/y3/angola'),
				9=> array('id'=>'drc', 'title'=>'DRC', 'url'=>'http://localhost:8080/mvc/y3/angola'),
				10=> array('id'=>'tanzania', 'title'=>'Tanzania', 'url'=>'http://localhost:8080/mvc/y3/tanzania2')
			);

		}else{
			$locations = array(
				0 => array('id'=>'nigeria','title'=>'Nigeria', 'url'=>'http://y3distribution.smalutioncrm.in'),
				1=> array('id'=>'uganda', 'title'=>'Uganda', 'url'=>'http://smalution.net/crmdev/uganda'),
				2=> array('id'=>'kenya', 'title'=>'Kenya', 'url'=>'http://crmdev.smalution.net/kenya'),
				3=> array('id'=>'burundi', 'title'=>'Burundi', 'url'=>'http://smalution.net/crmdev/burundi'),
				4=> array('id'=>'benin','title'=>'Benin', 'url'=>'http://crmdev.smalution.net/benin'),
				5=> array('id'=>'ghana','title'=>'Ghana', 'url'=>'http://crmdev.smalution.net/ghana'),
				6=> array('id'=>'conakry', 'title'=>'Guinea Conakry', 'url'=>'http://crmdev.smalution.net/guinea_conakry'),
				7=> array('id'=>'ssudan', 'title'=>'South Sudan', 'url'=>'http://crmdev.smalution.net/sudan'),
				8=> array('id'=>'angola', 'title'=>'Angola', 'url'=>'http://crmdev.smalution.net/angola'),
				//9=> array('id'=>'drc', 'title'=>'DRC', 'url'=>'http://crmdev.smalution.net/drc'),
				9=> array('id'=>'tanzania', 'title'=>'Tanzania',  'url'=>'http://crmdev.smalution.net/tanzania'),
				10=> array('id'=>'zambia', 'title'=>'Zambia',  'url'=>'http://crmdev.smalution.net/zambia'),
				11=> array('id'=>'togo', 'title'=>'Togo',  'url'=>'http://crmdev.smalution.net/togo'),
				12=> array('id'=>'ivorycoast', 'title'=>'IvoryCoast',  'url'=>'http://crmdev.smalution.net/ivory_coast'),
				13=> array('id'=>'niger', 'title'=>'Niger',  'url'=>'http://crmdev.smalution.net/niger'),
				14=> array('id'=>'cameroon', 'title'=>'Cameroon',  'url'=>'http://crmdev.smalution.net/cameroon'),
				15=> array('id'=>'liberia', 'title'=>'Liberia',  'url'=>'http://crmdev.smalution.net/liberia'),
				16=> array('id'=>'burkinafaso', 'title'=>'Burkinafaso',  'url'=>'http://crmdev.smalution.net/burkinafaso'),
				17=> array('id'=>'sa', 'title'=>'South Africa',  'url'=>'http://crmdev.smalution.net/sa')
			);
		}
		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',
			'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(){
		$url = $this->request->data['url'].'/api/get_reports?from='.$this->request->data['d1'].'&to='.$this->request->data['d2'];
		$result = array();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		//curl_setopt($ch, CURLOPT_PARAM, $this->request->data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		$response = curl_exec($ch);
		curl_close($ch);
		if($response){
			$result['error'] = "0";
			$result['data'] = $response;
		}else{
			$result['error'] = "1";
		}
		//echo $result;
		echo json_encode($result);
		exit;
	}
}