<?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 Sale 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'),
		'Customer' => array('className' => 'Customer', 'foreignKey' => 'customer_id', 'fields' => array('id', 'first_name','last_name'), 'type'=>'inner'),
	);
	
	/**
	 * updateStockStatus method
	 * Update current stock status after sales order
	 */ 
	public function updateStockStatus($params){ //pr($params);
		
		if (!empty($params)){
			foreach ( $params as $brand_id => $param ){
				
				$isadded = $this->query("SELECT id FROM current_stocks AS CurrentStock WHERE entity='".$param['entity']."' AND entity_id = ".$param['entity_id']." AND brand_id = $brand_id ");
				
				if (empty($param['case'])){
					$param['case'] = 0;
				}
				if (empty($param['roll'])){
					$param['roll'] = 0;
				}
				if (empty($param['pack'])){
					$param['pack'] = 0;
				}
				if ($isadded){
					$this->query("UPDATE current_stocks SET `case` = `case` - ".$param['case'].", `roll` = `roll` - ".$param['roll'].", `pack` = `pack` - ".$param['pack']." WHERE id = ".$isadded['0']['CurrentStock']['id']);
				}else{
					//brand_group_id
					$this->query("INSERT INTO current_stocks (entity_id, entity, brand_id, brand_group_id, `case`, `roll`, `pack`) VALUES (".$param['entity_id'].", '".$param['entity']."', $brand_id, ".$param['brand_group_id'].", ".$param['case'].", ".$param['roll'].", ".$param['pack'].")");
				}
			}
		}
	}
	
	
	
	public function calQty($brand_id, $unit, $qty){
		$unit = strtolower($unit);
		if ($unit == 'case'){
			return $qty;
		}else{
			
			$brand = $this->query("SELECT * FROM brands as Brand WHERE id = $brand_id  LIMIT 0,1");
			if ($unit == 'roll'){
				$qty = $qty / $brand['0']['Brand']['case_roll'];
			}elseif($unit == 'pack'){
				$qty = $qty / ($brand['0']['Brand']['case_roll']*$brand['0']['Brand']['roll_pack']);
			}elseif($unit == '2pack'){
				$qty = $qty *((1 / ($brand['0']['Brand']['case_roll']*$brand['0']['Brand']['roll_pack'])) * $brand['0']['Brand']['2pack_pack']);
			}
			//pr($brand);
		}
		return $qty;
	}
	
	public function _userCondition(&$conditions, $user){
		
		switch($user['Role']['grade']){
			
			case 2:
				if($user['role_id'] == DIVISIONMANAGER){
					$conditions[] = 'Order.user_id IN (SELECT id FROM users WHERE division_id = '. $user['division_id'].')';
				}								
			break;
			
			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('Order.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('Order.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('Order.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( 'Order.user_id' => $user['id'], 'UserRelation.uid2 IS NOT NULL'); 
				}else{
					$conditions['OR'] = array( 'Order.user_id' => $user['id'], 'UserRelation.uid2 IS NOT NULL'); 
				}
			break;
			case 6:
				$conditions['Order.user_id'] = $user['id'];
			break;
			default:
			break;
		}
	}
	
}