<?php
/**
 * AjaxController
 *
 * @var string
 */
App::uses('AppController', 'Controller');
class AjaxReportController extends AppController {
	
	var $uses = array('Report');
	
	function index(){	
		exit;
	}
	
	function active_users($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();
		
		$query = $this->request->query;	
		if(!empty($query['from'])){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$activeUsers = $this->Report->activeUsers($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'activeUsers'));
		if(isset($this->request->query['export'])){ 
			$this->set('filename', 'ActiveUsers');	
			$this->render('export_activeusers','export_xls');
		}
	}
	
	function nonactive_users($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();		
		$query = $this->request->query;	
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);		
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$nonactiveUsers = $this->Report->nonactiveUsers($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'nonactiveUsers'));
		if(isset($this->request->query['export'])){  
			$this->set('filename', 'NonActiveUsers');	
			$this->render('export_nonactive','export_xls');
		}
	}
	
	function day_calls($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('User');
		$userConditions = array('User.status'=>ACTIVE);
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
			$userConditions['User.region_id'] = $region_id;
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		
		}
		if (!empty($this->request->query['dp'])){
			$userConditions['User.depot_id'] =  $this->request->query['dp'];
		}else if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$userConditions['User.depot_id'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);		
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$dayCalls = $this->Report->dayCalls($params);		
		
		$users = $this->User->find('all', array('conditions'=>$userConditions));
		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'dayCalls', 'users'));
		
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'number_of_call_'.$type);
			$this->render('export_noofcallperday','export_xls');
			
		}
		if (isset($this->request->query['per']) && $this->request->query['per'] == 1){
			if(isset($this->request->query['export'])){   
				$this->set('filename', 'number_of_effective_call_'.$type);
				$this->render('effective_calls_export','export_xls');
			}else{
				$this->render('effective_calls');
			}
		}
	}
	
	function callreports($type='monthly', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}else{
			$type = 'monthly';
		}
		$navParams = $this->_nav_params($type, $opt);		
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$params['user'] = $this->user;
		$dayCalls = $this->Report->callReports($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'dayCalls'));
		
		if(isset($this->request->query['export'])){   
			if($type == 'between'){	
				$fileName = 'calls_report_'.$query['from'].'_to_'.$query['to'];
			}else{
				$fileName = 'calls_report_'.date('F_Y', $params['strtotime']);
			}			
			$this->set('filename', $fileName);
			$this->set('month', date('F Y', $params['strtotime']));
			$this->render('callreports_xls','export_xls');
		}
	}
	
	function effectivecallreports($type='monthly', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}else{
			$type = $type;
		}
		$navParams = $this->_nav_params($type, $opt);		
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$dayCalls = $this->Report->effectiveCallReports($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'dayCalls'));
		
		if(isset($this->request->query['export'])){   
			if($type == 'between'){	
				$fileName = 'effective_calls_report_'.$query['from'].'_to_'.$query['to'];
			}elseif($type == 'weekly'){	
				$fileName = 'effective_calls_report_'.date('d_F_Y', $params['strtotime']).'_to_'.date('d_F_Y', strtotime('+6 days', $params['strtotime']));
			}elseif($type == 'monthly'){	
				$fileName = 'effective_calls_report_'.date('F_Y', $params['strtotime']);
			}else{
				$fileName = 'effective_calls_report_'.date('d_F_Y', $params['strtotime']);
			}			
			$this->set('filename', $fileName);
			$this->set('titleStr', $params['current']);
			$this->render('effectivecallreports_xls','export_xls');
		}
		
	}
	
	function customerreports($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);		
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$customerReport = $this->Report->customerReports($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'customerReport'));
		
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'created_customers_report_'.date('F_Y', $params['strtotime']));
			$this->set('month', date('F Y', $params['strtotime']));
			$this->render('customerreports_xls','export_xls');
		}
	}
/**
 * Action numberof_customer
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function numberof_customers($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['from']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$numberOfCustomers = $this->Report->numberOfCustomers($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'numberOfCustomers'));
		if(isset($this->request->query['export'])){   
				$this->set('filename', 'Number_Of_Customer');	
				$this->render('export_numbercustomer','export_xls');
		}
	}
 /**
 * Action numberof_visits
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function numberof_visits($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Customer');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = $conditions = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$params['isVisit'] = 1;
		/*
		$numberofVisits = $this->Report->numberofVisits($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'numberofVisits'));
		if(isset($this->request->query['export'])){   
			$this->render('export_visitscustomer','export_xls');
		}*/
		$customerVistited = $this->Report->customerNotvisitConditions($params, $this->paginate, $conditions);
		
		$this->paginate['order'] = array('Customer.first_name'=>'asc', 'Customer.last_name'=>'asc');	
		$this->paginate['group'] = array('Customer.id');

		$customers = $this->paginate('Customer', $conditions);
		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'customers'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'customer_visit');
			$this->render('export_visitscustomer','export_xls');
		}
	}
	
/**
 * Action customer_notvisits
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function customer_notvisits($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Customer');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = $conditions = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$numberofNotVisits = $this->Report->customerNotvisitConditions($params, $this->paginate, $conditions);
		
		$this->paginate['order'] = array('Customer.first_name'=>'asc', 'Customer.last_name'=>'asc');		
		$customers = $this->paginate('Customer', $conditions);
		//pr($customers);
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'customers'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'customer_not_visit');
			$this->render('export_notvisitcustomer','export_xls');
		}
	}
/**
 * Action customer_notvisits
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function average_calls($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);		
		$params = $conditions = array();		
		$query = $this->request->query;		
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$averageCalls = $this->Report->averageCalls($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'averageCalls'));
	}
	
	function customer_trend($type='up', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Customer');
		
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);		
		$query = $this->request->query;
		$params = compact('query', 'type', 'opt');	
		$date = ($opt)?$opt:date('Y-m-d');
		
		$this->Customer->unbindModelAll();
		
		$customerTrend = $this->Report->customerTrend($params, $this->paginate, $conditions);
		  		
		$customers = $this->paginate('Report', $conditions);
		
		//pr($customers);
		$this->set(compact('regions', 'depots','query', 'type', 'customers','date'));
		$this->set($customerTrend);
		if(!empty($this->request->query['export'])){ 
			$offset = $this->request->query['offset'];
 			$this->set('offset',$offset);
			$this->set('filename', 'TrendCustomer_'.$type);
			$this->render('export_trendcustomer','export_xls');
		}
	}
	function _nav_params($type, $opt){
		switch($type){
			case 'daily':			    
				$date = ($opt)?$opt:date('Y-m-d');
				$strtotime = strtotime($date);
				$prev = date('Y-m-d', strtotime('-1 day', $strtotime) );
				$next = date('Y-m-d', strtotime('+1 day', $strtotime) );
				$current = date('M d, Y', $strtotime);
			break;
			case 'weekly':			    
				$date = ($opt)?$opt:date('Y-m-d');
				$strtotime = strtotime('monday this week', strtotime($date));
				$prev = date('Y-m-d', strtotime('-1 week', $strtotime) );
				$next = date('Y-m-d', strtotime('+1 week', $strtotime) );				
				$current = __('From %s to %s', strftime('%b %d, %Y', $strtotime), strftime('%b %d, %Y', strtotime('+6 days', $strtotime)));
			break;
			
			case 'between':			    
				//$date = $this->request->query['from'];
				if (!empty($this->request->query['from'])){
					$date = $this->request->query['from'];
				}else{
					$date = date('Y-m-d');
				}
				
				if (!empty($this->request->query['to'])){
					$date2 = $this->request->query['to'];
				}else{
					$date2 = date('Y-m-d');
				}
				$strtotime = strtotime($date);
				$prev = ''; //date('Y-m-d', strtotime('-1 week', $strtotime) );
				$next = ''; // date('Y-m-d', strtotime('+1 week', $strtotime) );				
				$current = __('From %s to %s', strftime('%b %d, %Y', $strtotime), strftime('%b %d, %Y', strtotime($date2)));
			break;
			case 'monthly':			  
				$date = ($opt)?$opt:date('Y-m-01');
				$strtotime = strtotime($date);
				$prev = date('Y-m', strtotime('-1 month', $strtotime) );
				$next = date('Y-m', strtotime('+1 month', $strtotime) );
				$current = date('M, Y', $strtotime);
			break;			
		}
		$this->set(compact('date', 'strtotime', 'prev', 'next', 'current'));
		return compact('date', 'strtotime', 'prev', 'next', 'current');
	}
	
/******************************************* PK Taks ***********************************/
/**
 * Action total_sale
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function total_sale($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$numberOfSale = $this->Report->numberOfQuantitySale($params);	
		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'numberOfSale'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'total_sales');
			$this->render('export_totlesale','export_xls');
		}
	}

/* Action ridistribtution_sales
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function ridistribtution_sales($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$redistributorOfSale = $this->Report->ridistribtutionSale($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'redistributorOfSale'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'redistribtution_sales');
			$this->render('export_ridistribtutionsale','export_xls');
		}
	}

/* Action totalamount_payment
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function totalamount_payment($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$totalamountOfSale = $this->Report->totalAmountonPayment($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'totalamountOfSale'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'total_payment');
			$this->render('export_totalamountpayment','export_xls');
		}
	}

 /* Action totalexpenceamount_sale
  * @params: @type (string), @opt(date)
  * return : void()
  */
	function totalexpenceamount($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();
		
		$query = $this->request->query;	
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}		
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$totalexpenceamountOfSale = $this->Report->totalExpenceAmounton($params);		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'totalexpenceamountOfSale'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'total_expense');
			$this->render('export_totalexpenceamount','export_xls');
		}
	}

/* Action averevenue_sale
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function averevenue_sale($type='daily', $opt=''){
			
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$totalaverevenueOfSale = $this->Report->averevenueonSale($params);		
       
		$this->set(compact('query', 'type','opt', 'totalaverevenueOfSale'));
		
	}
	
/* Action avecost_case
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function avecost_case($type='daily', $opt=''){
			
		$params = array();		
		$query = $this->request->query;
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$avecostCase = $this->Report->avecostCase($params);		
       
		$this->set(compact('query', 'type','opt', 'avecostCase'));
	}


/* Action totalexpenceamount_sale
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function nomaplocation_customer($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Customer');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$conditions = $params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$conditions = array("(Customer.latitude = \"\" OR Customer.latitude IS NULL) AND (Customer.longitude = \"\" OR Customer.longitude IS NULL )");
		
		if (!empty($query['rg'])){
			$conditions['Customer.region_id'] = $query['rg'];
		}
		if (!empty($query['dp'])){
			$conditions['Customer.depot_id'] = $query['dp'];
		}
		$this->paginate['paramType'] = 'querystring';
		//$this->paginate['limit'] = 50;
		/*if (!empty($this->request->query['export'])){
			$noMapCust = $this->Customer->find('all',array('conditions'=>$conditions));
		} else {*/
		    $noMapCust = $this->paginate('Customer', $conditions);
		//}
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'noMapCust'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'customer_not_on_map');
			$this->render('export_nomaplocationcustomer','export_xls');
		}
	}
/* Action totalexpenceamount_sale
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function active_customer($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Customer');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$activeCust = $conditions = array(); 

		$this->Report->totalNumberOfActiveCustomer($params, $this->paginate, $conditions, $this->Customer, $activeCust);		
		
		$activeCust = $this->paginate('Customer', $conditions);
		
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'activeCust'));
		if(isset($this->request->query['export'])){   
				$this->render('export_activecustomer','export_xls');
		}
		
	}
/* Action totalexpenceamount_sale
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function activepurch_customer($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		
		$purchaseCustomers = $this->Report->purchaseCustomer($params);		
       
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'purchaseCustomers'));
	}
	
/* Action total_customers
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function total_customers($type='s', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Customer');
		//$this->Customer->unbindModelAll();
		$conditions = array('Customer.status!=2');
		
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
			$conditions['Customer.region_id'] =  $region_id;
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
				$conditions['Customer.region_id'] =  $region_id;
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		if (!empty($this->request->query['dp'])){
			$conditions['Customer.depot_id'] =  $this->request->query['dp'];
		}

		$order = $this->paginate['order'] = array('Customer.first_name'=>'asc', 'Customer.last_name'=>'asc');	
		if (!empty($this->request->query['export'])){
			$customers = $this->Customer->find('all',array('conditions'=>$conditions,'order'=>$order));
		} else {
		    $customers = $this->paginate('Customer', $conditions);
		}
		$this->set(compact('regions', 'depots', 'customers'));
		if(isset($this->request->query['export'])){   
			$this->render('export_totalcustomers','export_xls');
		}
	}
	
/* Action total_redistributors
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function total_redistributors($type='', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Redistributor');
		//$this->Customer->unbindModelAll();
		$conditions = array('Redistributor.status!=2');
		
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
			$conditions['Redistributor.region_id'] =  $region_id;
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
				$conditions['Redistributor.region_id'] =  $region_id;
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		if (!empty($this->request->query['dp'])){
			$conditions['Redistributor.depot_id'] =  $this->request->query['dp'];
		}
		 $order = $this->paginate['order'] = array('Redistributor.first_name'=>'asc', 'Redistributor.last_name'=>'asc');	
       /* if (!empty($this->request->query['export'])){
			$redistributors = $this->Redistributor->find('all',array('conditions'=>$conditions,'order'=>$order));
		} else {	*/	   	
		     $redistributors = $this->paginate('Redistributor', $conditions);
		//}
		$this->set(compact('regions', 'depots', 'redistributors'));
		if(isset($this->request->query['export'])){   
				$this->set('filename','redistributorscustomers');
				$this->render('export_redistributorscustomers','export_xls');
		}
	}
	
	public function customer_status(){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Customer');
		$this->Customer->unbindModelAll();
		$cids = $conditions = array();
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
			$conditions['Customer.region_id'] = $this->request->query['rg'];
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
				$conditions['Customer.region_id'] = $this->request->query['rg'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		
		if (!empty($this->request->query['dp'])){
			$conditions['Customer.depot_id'] = $this->request->query['dp'];
		}
		if (!empty($this->request->query['sts'])){
			$conditions['cStatus.verify_status'] = $this->request->query['sts'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		$isAll = false;
		$joins = array(array('table'=>'depots', 'alias'=>'Depot2', 'type'=>'left', 'conditions'=>array('Customer.depot_id = Depot2.id') ), array('table'=>'customers_status', 'type'=>'left', 'alias' => 'cStatus','conditions'=> array('Customer.id = cStatus.customer_id') ) );
		$fields = array('Customer.id', 'Customer.first_name', 'Customer.last_name', 'Customer.phone', 'Depot2.title', 'cStatus.verify_status');
		$order = array('CONCAT_WS(" ", Customer.first_name, Customer.last_name)'=>'asc');
		if(isset($this->request->query['export'])){ 
			$isAll = true;
			if(!empty($this->request->query['offset'])){
				$offset = (int) $this->request->query['offset'];
			}else{
				$offset = 0;
			}
			$customers = $this->Customer->find('all', array('conditions'=>$conditions, 'fields'=>$fields, 'joins'=>$joins, 'order'=>$order, 'limit'=>'10000', 'offset'=>$offset ));
		}else{
			$cids = $params = array();
			$this->paginate['fields'] = $fields;
			$this->paginate['order'] = $order;	
			$this->paginate['joins'] = $joins;	
			$customers = $this->paginate('Customer', $conditions);
			/*if ($customers){
				foreach ($customers as $customer){
					$cids[] = $customer['Customer']['id'];
				}
			}*/
		}
		$cstatus = array(1=>__('Unverified'), 2=>__('Verified'), 3=>__('Fully Verified') );
		//$customer_status = $this->Report->customer_status($cids, $isAll);
		$this->set(compact('regions', 'depots', 'customers', 'customer_status','cstatus'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'customer_status');
			$this->set('offset', $offset);
			$this->render('customer_status_xls','export_xls');
			//$this->render('customer_status_xls');
		}
	}
	
/* Action total_customers
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function route_customers($type='s', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Customer');
		$this->loadModel('Route');
		
		$conditions = array('Customer.status!=2');
		$rcond = array();
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
			$conditions['Customer.region_id'] =  $region_id;
			$rcond['region_id'] = $region_id;
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		if (!empty($this->request->query['dp'])){
			$conditions['Customer.depot_id'] =  $this->request->query['dp'];
			$depot_id = $this->request->query['dp'];
			$rcond['depot_id'] = $depot_id;
		}
		if (!empty($this->request->query['rt'])){
			$conditions['Customer.route_id'] =  $this->request->query['rt'];
		}
		$routes = $this->Route->find('list', array('conditions'=>$rcond, 'fields'=>array('id','title'), 'order'=>array('title'=>'asc')));
		$joins = array(array('table'=>'routes', 'type'=>'left', 'alias'=>'Route', 'conditions'=>array('Customer.route_id = Route.id')));
		$fields = array('Customer.id', 'Customer.first_name', 'Customer.last_name','Customer.phone', 'Customer.created','Region.id', 'Region.title', 'Depot.id', 'Depot.title', 'Route.id', 'Route.title');
		$this->Customer->virtualFields['routeName'] = 'Route.title';
		$order = $this->paginate['order'] = array('routeName'=>'asc','Customer.first_name'=>'asc', 'Customer.last_name'=>'asc');	
		$this->paginate['fields'] = $fields;
		$this->paginate['joins'] = $joins;
		if (!empty($this->request->query['export'])){
			if(!empty($this->request->query['offset'])){
				$offset = (int) $this->request->query['offset'];
			}else{
				$offset = 0;
			}
			
			$customers = $this->Customer->find('all',array('conditions'=>$conditions,'order'=>$order, 'fields'=>$fields, 'joins'=>$joins, 'limit'=>'10000', 'offset'=>$offset));
		} else {
		    $customers = $this->paginate('Customer', $conditions);
		}
		$this->set(compact('regions', 'depots', 'customers','routes','offset'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'route_customers');
			$this->render('export/route_customers','export_xls');
		}
	}
	
	
/**
 * Action routecustomer_notvisits
 * @params: @type (string), @opt(date)
 * return : void()
 */
	function routecustomer_notvisits($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Route');
		$this->loadModel('Customer');
		$rcond = array();
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
			$rcond['region_id'] = $region_id;
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
				$rcond['region_id'] = $region_id;
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		
		$params = $conditions = array();
		if (!empty($this->request->query['dp'])){
			$rcond['depot_id'] = $this->request->query['dp'];
		}
		if (!empty($this->request->query['rt'])){
			$conditions['Customer.route_id'] =  $this->request->query['rt'];
		}
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);	
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$routes = $this->Route->find('list', array('conditions'=>$rcond, 'fields'=>array('id','title'), 'order'=>array('title'=>'asc')));
		$numberofNotVisits = $this->Report->routeCustNotvisitCond($params, $this->paginate, $conditions);
		$fields = array('Customer.id', 'Customer.first_name', 'Customer.last_name','Customer.phone', 'Customer.created','User.first_name', 'User.last_name', 'Region.id', 'Region.title', 'Depot.id', 'Depot.title', 'Route.id', 'Route.title');
		$this->Customer->virtualFields['routeName'] = 'Route.title';
		$order = $this->paginate['order'] = array('routeName'=>'asc','Customer.first_name'=>'asc', 'Customer.last_name'=>'asc');	
		$this->paginate['fields'] = $fields;
		$customers = $this->paginate('Customer', $conditions);
		//pr($customers);
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'customers','routes'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'route_customer_not_visit');
			$this->render('export/routecustomer_notvisits','export_xls');
		}
	}
	
	public function sale_on_route($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Route');
		$this->loadModel('Brand');
		$rcond = array();
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
			$rcond['Route.region_id'] = $region_id;
		}else{
			$region_id = 0;
			if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
				$rcond['Route.region_id'] = $region_id;
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		
		if (!empty($this->request->query['dp'])){
			$rcond['Route.depot_id'] = $this->request->query['dp'];
		}
		if (!empty($this->request->query['rt'])){
			$rcond['Route.id'] = $this->request->query['rt'];
		}
		$regions = $this->Region->getList();
		$brands = $this->Brand->find('list', array('fields'=>array('id','name'), 'conditions'=>array('status'=>1)));
		$depots = $this->Depot->getList($region_id);
		$routes = $this->Route->find('list', array('conditions'=>$rcond, 'fields'=>array('id','title'), 'order'=>array('title'=>'asc')));
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$routeList = $this->Route->find('all', array('conditions'=>$rcond));
		$result = $this->Route->routeSalesReport($params);
		//pr($result);
		$this->set(compact('regions', 'depots', 'brands','query', 'type','opt', 'customers','routes','routeList','result'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'sale_on_route_in_unit');
			$this->render('export/sale_on_route','export_xls');
		}
	}
	
	public function call_route($type='daily', $opt=''){
		$this->loadModel('Region');
		$this->loadModel('Depot');
		$this->loadModel('Route');
		$rcond = array();
		if (!empty($this->request->query['rg'])){
			$region_id = $this->request->query['rg'];
			$rcond['Route.region_id'] = $region_id;
		}else{
			$region_id = 0;if(!empty($this->user['region_id']) && $this->user['Role']['grade'] >=3){
				$this->request->query['rg'] = $region_id = $this->user['region_id'];
				$rcond['Route.region_id'] = $region_id;
			}
		}
		if(!empty($this->user['depot_id']) && $this->user['Role']['grade'] >=4){
			$this->request->query['dp'] = $this->user['depot_id'];
		}
		if (!empty($this->request->query['dp'])){
			$rcond['Route.depot_id'] = $this->request->query['dp'];
		}
		if (!empty($this->request->query['rt'])){
			$rcond['Route.id'] = $this->request->query['rt'];
		}
		$regions = $this->Region->getList();
		$depots = $this->Depot->getList($region_id);
		$routes = $this->Route->find('list', array('conditions'=>$rcond, 'fields'=>array('id','title'), 'order'=>array('title'=>'asc')));
		$params = array();
		
		$query = $this->request->query;		
		if(!empty($query['from']) || !empty($query['to']) ){
			$type = 'between';
		}
		$navParams = $this->_nav_params($type, $opt);
		$params = array_merge($navParams, compact('query', 'type','opt'));
		$routeList = $this->Route->find('all', array('conditions'=>$rcond));
		$result = $this->Route->routeSalesReport($params);
		$this->set(compact('regions', 'depots', 'query', 'type','opt', 'customers','routes','routeList', 'result'));
		if(isset($this->request->query['export'])){   
			$this->set('filename', 'call_on_route');
			$this->render('export/call_route','export_xls');
		}
	}
	
}
