<?php

App::uses('Model', 'Model');



/**

 * Application model for Cake.

 *

 * Add your application-wide methods in the class below, your models

 * will inherit them.

 *

 * @package       app.Model

 */



class Service extends AppModel {

	public $useTable = false;

	

	public function getDistributorList(){

		return $this->query("SELECT id, title, state_id FROM distributors ORDER BY title ASC");

	}
	function comment($key=''){
		$comment = array('1'=>'Free issue', '2'=>'Sampling', '3'=>'Customer on target','4'=>'Redemption');
		if($key && isset($comment[$key])){
			return $comment[$key];
		}else{
			return $comment;
		}
	}
	

	function paymentMode($key=''){

	

		$payment_mode = array('Bank Deposit' => 'Bank Deposit', 'Cash'=>'Cash', 'Cheque'=>'Cheque', 'Other' => 'Other');

		if($key){

			return $payment_mode[$key];

		}else{

			return $payment_mode;

		}

	}

	

	public function bankList(){

		$banks = $this->query("SELECT id, name FROM banks AS Bank ORDER BY name ASC");

		$bankdata = array();

		foreach($banks as $bank){

			$bankdata[] = array('id'=>$bank['Bank']['id'], 'value' => $bank['Bank']['name']);

		}

		if ($bankdata){

			return $bankdata;

		}else{

			return null;

		}

	}

	

	public function expenseTypeList(){

		$expTypes = $this->query("SELECT id, name FROM exp_types AS ExpType ORDER BY name ASC");

		$expdata = array();

		foreach($expTypes as $expType){

			$expdata[] = array('id'=>$expType['ExpType']['id'], 'value' => $expType['ExpType']['name']);

		}

		if ($expdata){

			return $expdata;

		}else{

			return null;

		}

	}

	

	public function reagionList(){

		$regions = $this->query("SELECT id, title FROM regions AS Region ORDER BY title ASC");

		$regionData = array();

		foreach($regions as $region){

			$regionData[] = array('id' => $region['Region']['id'], 'value' => $region['Region']['title']);

		}

		return $regionData;

	}

	

	/*public function statesList(){

		$states = $this->query("SELECT id, state FROM states AS State ORDER BY state ASC");

		$stateData = array();

		foreach($states as $state){

			$stateData[] = array('id' => $state['State']['id'], 'value' => $state['State']['state']);

		}

		if ($stateData){

			return $stateData;

		}else{

			return null;

		}

	}*/

	public function statesList(){
  $states = $this->query("SELECT id, state FROM states AS State ORDER BY state ASC");
  $stateData = array();
  foreach($states as $state){
   $stateData[] = array('id' => $state['State']['id'], 'value' => $state['State']['state']);
  }
  if ($stateData){
  return $stateData;
  }
  else{

			return null;

		}
 }

	public function routesList(){

		$routes = $this->query("SELECT id, title, region_id, depot_id FROM routes AS Route ORDER BY title ASC");

		$routesData = array();

		foreach($routes as $route){

			$routesData[] = array('id' => $route['Route']['id'], 'title' => $route['Route']['title'], 'region_id' => $route['Route']['region_id'], 'depot_id'=>$route['Route']['depot_id']);

		}

		if ($routesData){

			return $routesData;

		}else{

			return null;

		}

	}

	

/** 

 * Request Logs

 *

 */ 

	function saveRequestLogs($request_id, $response, $url, $status, $user_id, $model_id = 0){

		if($request_id && APP_LOGS){

			$this->query("INSERT INTO app_request_logs (request_id, user_id, url, response, status, model_id, created, modified) VALUES ('$request_id', $user_id, '".$url."', '".serialize($response)."', $status, $model_id, '".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s')."' )");

		}

	}

	

	function getRequestLogs($request_id){

		$data = $this->query("SELECT * FROM app_request_logs AS AppRequestLog WHERE request_id='$request_id' ");

		

		if ($data){

			return $data['0']['AppRequestLog'];

		}

		return null;

	}

}