<?php

class BudgetAccount extends \Eloquent {

	use SoftDeletingTrait;

	protected $guarded	 = array();
	public static $rules = array();

	public function project() {
		return $this->belongsTo('Project');
	}

	public function cost() {
		return $this->belongsTo('BudgetCost', 'cost_id');
	}

	public function getSum($endDate = null) {
		if (!$endDate) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		$operaionsInAmount	 = BudgetOperation::whereProjectId($this->project->id)
						->whereBudgetInAccountId($this->id)
						->whereIn('type', array(BudgetOperation::TYPE_INCOMING, BudgetOperation::TYPE_EXCHANGE, BudgetOperation::TYPE_OPENING_BALANCE))
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		$operaionsOutAmount	 = BudgetOperation::whereProjectId($this->project->id)
						->whereBudgetOutAccountId($this->id)
						->whereIn('type', array(BudgetOperation::TYPE_OUTCOMING, BudgetOperation::TYPE_EXCHANGE))
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		$amount							 = $operaionsInAmount - $operaionsOutAmount;
		return round($amount / 100, 2);
	}

//	public function getEndSum() {
//		$operaionsInAmount	 = BudgetOperation::whereProjectId($this->project->id)
//						->whereBudgetInAccountId($this->id)
//						->whereIn('type', array(BudgetOperation::TYPE_INCOMING, BudgetOperation::TYPE_EXCHANGE, BudgetOperation::TYPE_OPENING_BALANCE))
//						->whereRaw(DB::raw("MONTH(`budget_operations`.`date`)!=MONTH(NOW())"))
//						->sum('amount');
//		$operaionsOutAmount	 = BudgetOperation::whereProjectId($this->project->id)
//						->whereBudgetOutAccountId($this->id)
//						->whereIn('type', array(BudgetOperation::TYPE_OUTCOMING, BudgetOperation::TYPE_EXCHANGE))
//						->whereRaw(DB::raw("MONTH(`budget_operations`.`date`)!=MONTH(NOW())"))
//						->sum('amount');
//		$amount							 = $operaionsInAmount - $operaionsOutAmount;
//		return round($amount / 100, 2);
//	}
  
	public function getEndSum() {
    $workProject = $this->project;
    $budgetCloseDay = ($workProject->budget_close_day)? $workProject->budget_close_day : 1;
      
		$operaionsInAmount	 = BudgetOperation::whereProjectId($workProject->id)
						->whereBudgetInAccountId($this->id)
						->whereIn('type', array(BudgetOperation::TYPE_INCOMING, BudgetOperation::TYPE_EXCHANGE, BudgetOperation::TYPE_OPENING_BALANCE))
            ->whereRaw( DB::raw( "DATE(`budget_operations`.`date`) < '".date('Y-m-'.$budgetCloseDay)."'" ) )
						->sum('amount');
		$operaionsOutAmount	 = BudgetOperation::whereProjectId($workProject->id)
						->whereBudgetOutAccountId($this->id)
						->whereIn('type', array(BudgetOperation::TYPE_OUTCOMING, BudgetOperation::TYPE_EXCHANGE))
            ->whereRaw( DB::raw( "DATE(`budget_operations`.`date`) < '".date('Y-m-'.$budgetCloseDay)."'" ) )
						->sum('amount');
		$amount							 = $operaionsInAmount - $operaionsOutAmount;
		return round($amount / 100, 2);
	}

	public function getInSum($endDate = null) {
		if (!$endDate) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		$operaionsInAmount = BudgetOperation::whereProjectId($this->project->id)
						->whereIsnoincome(0)
						->whereIstransit(0)
						->whereBudgetInAccountId($this->id)
						->whereType(BudgetOperation::TYPE_INCOMING)
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		return round($operaionsInAmount / 100, 2);
	}

	public function getOutSum($endDate = null) {
		if (!$endDate) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		$operaionsOutAmount = BudgetOperation::whereProjectId($this->project->id)
						->whereIsnooutcome(0)
						->whereBudgetOutAccountId($this->id)
						->whereType(BudgetOperation::TYPE_OUTCOMING)
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		return round($operaionsOutAmount / 100, 2);
	}

    /**
	 * Массив всех счетов по всем проектам. Нужен для получения номера счета или признака касса/буфер по id счета.
	 *
	 * @return array
     * @View: [ 'id' : '40817810123456789123' или 'cache' или 'buffer' ]
	 */
    public static function getAccountsList() {
      $accounts = BudgetAccount::all();
      
      $accList = [];
      foreach ($accounts as $account){
        $accValue = '';
        if($account->current_account > 0){
          $accValue = $account->current_account;
        }else if($account->iscache == 1){
          $accValue = 'cache';
        }else if($account->isbuffer == 1){
          $accValue = 'buffer';
        }
        $accList[$account->id] = $accValue;
      }
      
      return $accList;
    }
}
