<?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 Expense extends AppModel {
	public $belongsTo = array(
			'User' => array('className' => 'User', 'foreignKey' => 'user_id', 'fields' => array('first_name', 'last_name'), 'type'=>'inner'), 
			'ExpType' => array('className' => 'ExpType', 'foreignKey' => 'exp_type_id', 'type'=>'left')
		);
	
	function _userCondition(&$conditions, $user){
		switch($user['Role']['grade']){
			
			case 2:
				if($user['role_id'] == DIVISIONMANAGER){
					//$conditions[] = 'Expense.region_id IN (SELECT region_id FROM divisions_regions WHERE division_id = '.$user['division_id'].')';
					$conditions['Expense.division_id'] = $user['division_id'];
				}								
			break;
			
			case 3:
				//$conditions[] = 'Expense.depot_id IN (SELECT id FROM depots WHERE region_id = '.$user['region_id'].')';
				$conditions['Expense.region_id'] = $user['region_id'];
			break;
			case 4:
				$conditions['Expense.depot_id'] = $user['depot_id'];
			break;
			case 5:
				$conditions['Expense.user_id'] = $user['id'];
			break;
			case 6:
				$conditions['Expense.user_id'] = $user['id'];
			break;
			
		}
	}
	
	public function _userRoleCondition(&$conditions, $user, &$paginate){
		
		switch($user['Role']['grade']){
			case 2:
				if($user['role_id'] == DIVISIONMANAGER){					
					$paginate['joins'][] = array('table' => '(SELECT u.id FROM users u JOIN roles r ON u.role_id = r.id WHERE (u.division_id = '.$user['division_id'].' AND r.grade > '.$user['Role']['grade'].') OR u.id = '.$user['id'].' )', 'type'=>'left', 'alias'=>'User2', 'conditions'=>array('Expense.user_id = User2.id'));
				
					$conditions[] = 'User2.id IS NOT NULL';
				}								
			break;
			
			case 3:
				$paginate['joins'][] = array('table' => '(SELECT u.id FROM users u JOIN roles r ON u.role_id = r.id WHERE (u.region_id = '.$user['region_id'].' AND r.grade > '.$user['Role']['grade'].') OR u.id = '.$user['id'].' )', 'type'=>'left', 'alias'=>'User2', 'conditions'=>array('Expense.user_id = User2.id'));
				
					$conditions[] = 'User2.id IS NOT NULL';
			break;
			case 4:
				
				$paginate['joins'][] = array('table' => '(SELECT u.id FROM users u JOIN roles r ON u.role_id = r.id WHERE (u.depot_id = '.$user['depot_id'].' AND r.grade > '.$user['Role']['grade'].') OR u.id = '.$user['id'].' )', 'type'=>'left', 'alias'=>'User2', 'conditions'=>array('Expense.user_id = User2.id'));
				
				$conditions[] = 'User2.id IS NOT NULL';
			break;
			case 5:
				$paginate['joins'][] = array('table' => '(SELECT uid2 FROM user_relations WHERE user_id = '.$user['id'].')', 'type'=>'left', 'alias'=>'UserRelation', 'conditions'=>array('Expense.user_id = UserRelation.uid2'));

				if(isset($conditions['OR'])){
					$conditions[]['OR'] = array( 'Expense.user_id' => $user['id'], 'UserRelation.uid2 IS NOT NULL'); 
				}else{
					$conditions['OR'] = array( 'Expense.user_id' => $user['id'], 'UserRelation.uid2 IS NOT NULL'); 
				}
			break;
			case 6:
				$conditions['Expense.user_id'] = $user['id'];
			break;
			default:
			break;
		}
	}

}