<?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 Incentive extends AppModel {
	public $belongsTo = array(
		'User' => array('className' => 'User', 'foreignKey' => 'user_id', 'fields' => array('first_name', 'last_name'), 'type'=>'inner'), 
		'Brand' => array('className' => 'Brand', 'foreignKey' => 'brand_id', 'type'=>'inner'), 
		'Depot' => array('className' => 'Depot', 'foreignKey' => 'depot_id', 'fields' => array('id', 'title','region_id'), 'type'=>'inner'),
		'Customer' => array('className' => 'Customer', 'foreignKey' => 'customer_id', 'fields' => array('id', 'first_name','last_name'), 'type'=>'inner'),
	);
	
	
	function _incentiveConditions(&$conditions, $user){
		switch($user['Role']['grade']){
			case 2:
                if($user['role_id'] == DIVISIONMANAGER){
                    $conditions[] = 'Incentive.user_id IN (SELECT id FROM users WHERE division_id = '.$user['division_id'].')';
                }
                break;
			case 3:
				$conditions[] = 'Incentive.user_id IN (SELECT id FROM users WHERE region_id = '.$user['region_id'].')';
			break;
			case 4:
				$conditions[] = 'Incentive.user_id IN (SELECT id FROM users WHERE depot_id = '.$user['depot_id'].')';
			break;
			case 5:
				$conditions['OR'] = array('Incentive.user_id = '.$user['id'], 'Incentive.user_id IN (SELECT uid2 FROM user_relations WHERE user_id = '.$user['id'].')');
			case 6:
				$conditions['Incentive.user_id'] = $user['id'];
			break;			
			default:
			break;
		}
	}
	
	
}