<?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 ConsumerRedistributionSale 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'=>'left'),
		'Consumer' => array('className' => 'Consumer', 'foreignKey' => 'consumer_id', 'fields' => array('id', 'first_name','last_name'), 'type'=>'inner'),
		'Redistributor' => array('className' => 'Redistributor', 'foreignKey' => 'redistributor_id', 'fields' => array('id', 'first_name','last_name','name'), 'type'=>'inner'),
	);
	
	
	
	

	
	/* public function _userCondition(&$conditions, $user){
		
		switch($user['Role']['grade']){
			case 3:
				$conditions[] = 'Order.user_id IN (SELECT id FROM users WHERE region_id = '. $user['region_id'].')';
			break;
			case 4:
				$conditions[] = 'Order.user_id IN (SELECT id FROM users WHERE depot_id = '. $user['depot_id'].')';
			break;
			case 5:
				if(isset($conditions['OR'])){
					$conditions[]['OR'] = array( 'Order.user_id' => $user['id'], '`Order`.`user_id` IN (SELECT uid2 FROM user_relations WHERE user_id = '. $user['id'].')');
				}else{					
					$conditions['OR'] = array( 'Order.user_id' => $user['id'], '`Order`.`user_id` IN (SELECT uid2 FROM user_relations WHERE user_id = '. $user['id'].')'); 
				}
			break;
			case 6:
				$conditions['Order.user_id'] = $user['id'];
			break;
			default:
			break;
		}
	} */
	
	public function _userRoleCondition(&$conditions, $user, &$paginate){
		
		switch($user['Role']['grade']){
			case 3:
				$paginate['joins'][] = array('table' => '(SELECT id FROM users WHERE region_id = '.$user['region_id'].')', 'type'=>'left', 'alias'=>'User2', 'conditions'=>array('ConsumerRedistributionOrder.user_id = User2.id'));
				
				//$conditions[] = 'Order.user_id IN (SELECT id FROM users WHERE region_id = '. $user['region_id'].')';
				
				$conditions[] = 'User2.id IS NOT NULL';
			break;
			case 4:
				$paginate['joins'][] = array('table' => '(SELECT id FROM users WHERE depot_id = '.$user['depot_id'].')', 'type'=>'left', 'alias'=>'User2', 'conditions'=>array('ConsumerRedistributionOrder.user_id = User2.id'));
				//$conditions[] = 'Order.user_id IN (SELECT id FROM users WHERE depot_id = '. $user['depot_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('ConsumerRedistributionOrder.user_id = UserRelation.uid2'));
				
				//$conditions['OR'] = array( 'Order.user_id' => $user['id'], '`Order`.`user_id` IN (SELECT uid2 FROM user_relations WHERE user_id = '. $user['id'].')'); 
				if(isset($conditions['OR'])){
					$conditions[]['OR'] = array( 'ConsumerRedistributionOrder.user_id' => $user['id'], 'UserRelation.uid2 IS NOT NULL'); 
				}else{
					$conditions['OR'] = array( 'ConsumerRedistributionOrder.user_id' => $user['id'], 'UserRelation.uid2 IS NOT NULL'); 
				}
			break;
			case 6:
				$conditions['ConsumerRedistributionOrder.user_id'] = $user['id'];
			break;
			default:
			break;
		}
	}
	
}