<?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 Warehouse extends AppModel {
	public $belongsTo  = array('State' => array('className' => 'State', 'foreignKey' => 'state_id'), 'Region' => array('className' => 'Region', 'foreignKey' => 'region_id'));
	
	public function getList($region_id = 0){
		$conditions = array();
		if ($region_id){
			$conditions = array('region_id'=>$region_id);
		}
		
		return $this->find('list', array('fields'=>array('id', 'title'),'conditions'=>$conditions, 'order'=>array('title'=>'asc')));
	}
	
	public function _userRoleCondition(&$conditions, $user){
		
		switch($user['Role']['grade']){
			case 2:
				if($user['role_id'] == DIVISIONMANAGER){
					$conditions['Warehouse.division_id'] = $user['division_id'];
				}								
			break;
			case 3:
				$conditions['Warehouse.region_id'] = $user['region_id'];
			break;
			case 4:
				$conditions['Warehouse.depot_id'] = $user['depot_id'];
			break;
			
		}
	}

}