<?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 ConsumerRedistributionOrder extends AppModel {
	public $belongsTo = array(
		'User' => array('className' => 'User', 'foreignKey' => 'user_id', 'fields' => array('first_name', 'last_name','name'), 'type'=>'inner'),
		'Consumer' => array('className' => 'Consumer', 'foreignKey' => 'consumer_id', 'fields' => array('id', 'first_name','last_name','name'), 'type'=>'inner'),
		'Redistributor' => array('className' => 'Redistributor', 'foreignKey' => 'redistributor_id', 'fields' => array('id', 'first_name','last_name','name'), 'type'=>'inner'),
	);
	
	public $hasMany = array(
		'ConsumerRedistributionSale' => array(
            'className' => 'ConsumerRedistributionSale',
            'foreignKey' => 'consumer_redistribution_order_id',
			'dependent' => true
        ),
      /*   'ConsumerFreeItem' => array(
            'className' => 'ConsumerFreeItem',
            'foreignKey' => 'consumer_order_id',
			'dependent' => true
        ),    */   
        
    );
	
	
	
	function _userSalesCondition(&$conditions, $user){
		
		switch($user['Role']['grade']){
			case 3:
				$conditions[] = 'ConsumerRedistributionOrder.user_id IN (SELECT id FROM users WHERE region_id = '. $user['region_id'].')';
			break;
			case 4:
				$conditions[] = 'ConsumerRedistributionOrder.user_id IN (SELECT id FROM users WHERE depot_id = '. $user['depot_id'].')';
			break;
			case 5:
				$conditions['OR'] = array( 'ConsumerRedistributionOrder.user_id' => $user['id'], '`ConsumerRedistributionOrder`.`user_id` IN (SELECT uid2 FROM user_relations WHERE user_id = '. $user['id'].')'); 
			break;
			case 6:
				
				$conditions[] = '(ConsumerRedistributionOrder.user_id = '.$user['id'].')';
				
			break;
			
		}
	
	}
}