<?php

class BudgetDebtPartner extends \Eloquent {

	use SoftDeletingTrait;

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

	public function project() {
		return $this->belongsTo('Project');
	}
    
	public function debProject() {
		return $this->belongsTo('Project', 'debet_project_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)
						->whereIsdebt(1)
						->whereDebtPartnerId($this->id)
						->whereType(BudgetOperation::TYPE_INCOMING)
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		$operaionsOutAmount	 = BudgetOperation::whereProjectId($this->project->id)
						->whereIsdebt(1)
						->whereDebtPartnerId($this->id)
						->whereType(BudgetOperation::TYPE_OUTCOMING)
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		$amount							 = $operaionsOutAmount - $operaionsInAmount;

		$operaionsInAmount	 = BudgetOperation::whereProjectId($this->project->id)
						->whereDebtPartnerId($this->id)
						->whereType(BudgetOperation::TYPE_OPENING_DEBT_IN)
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		$operaionsOutAmount	 = BudgetOperation::whereProjectId($this->project->id)
						->whereDebtPartnerId($this->id)
						->whereType(BudgetOperation::TYPE_OPENING_DEBT_OUT)
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		$amount							 = $amount + $operaionsOutAmount - $operaionsInAmount;

		return round($amount / 100, 2);
	}

//	public function getEndSum() {
//		$operaionsInAmount	 = BudgetOperation::whereProjectId($this->project->id)
//						->whereIsdebt(1)
//						->whereDebtPartnerId($this->id)
//						->whereType(BudgetOperation::TYPE_INCOMING)
//						->whereRaw(DB::raw("MONTH(`budget_operations`.`date`)!=MONTH(NOW())"))
//						->sum('amount');
//		$operaionsOutAmount	 = BudgetOperation::whereProjectId($this->project->id)
//						->whereIsdebt(1)
//						->whereDebtPartnerId($this->id)
//						->whereType(BudgetOperation::TYPE_OUTCOMING)
//						->whereRaw(DB::raw("MONTH(`budget_operations`.`date`)!=MONTH(NOW())"))
//						->sum('amount');
//		$amount							 = $operaionsOutAmount - $operaionsInAmount;
//
//		$operaionsInAmount	 = BudgetOperation::whereProjectId($this->project->id)
//						->whereDebtPartnerId($this->id)
//						->whereType(BudgetOperation::TYPE_OPENING_DEBT_IN)
//						->whereRaw(DB::raw("MONTH(`budget_operations`.`date`)!=MONTH(NOW())"))
//						->sum('amount');
//		$operaionsOutAmount	 = BudgetOperation::whereProjectId($this->project->id)
//						->whereDebtPartnerId($this->id)
//						->whereType(BudgetOperation::TYPE_OPENING_DEBT_OUT)
//						->whereRaw(DB::raw("MONTH(`budget_operations`.`date`)!=MONTH(NOW())"))
//						->sum('amount');
//		$amount							 = $amount + $operaionsOutAmount - $operaionsInAmount;
//
//		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)
						->whereIsdebt(1)
						->whereDebtPartnerId($this->id)
						->whereType(BudgetOperation::TYPE_INCOMING)
						->whereRaw( DB::raw( "DATE(`budget_operations`.`date`) < '".date('Y-m-'.$budgetCloseDay)."'" ) )
						->sum('amount');
		$operaionsOutAmount	 = BudgetOperation::whereProjectId($workProject->id)
						->whereIsdebt(1)
						->whereDebtPartnerId($this->id)
						->whereType(BudgetOperation::TYPE_OUTCOMING)
						->whereRaw( DB::raw( "DATE(`budget_operations`.`date`) < '".date('Y-m-'.$budgetCloseDay)."'" ) )
						->sum('amount');
		$amount							 = $operaionsOutAmount - $operaionsInAmount;

		$operaionsInAmount	 = BudgetOperation::whereProjectId($workProject->id)
						->whereDebtPartnerId($this->id)
						->whereType(BudgetOperation::TYPE_OPENING_DEBT_IN)
						->whereRaw( DB::raw( "DATE(`budget_operations`.`date`) < '".date('Y-m-'.$budgetCloseDay)."'" ) )
						->sum('amount');
		$operaionsOutAmount	 = BudgetOperation::whereProjectId($workProject->id)
						->whereDebtPartnerId($this->id)
						->whereType(BudgetOperation::TYPE_OPENING_DEBT_OUT)
						->whereRaw( DB::raw( "DATE(`budget_operations`.`date`) < '".date('Y-m-'.$budgetCloseDay)."'" ) )
						->sum('amount');
		$amount							 = $amount + $operaionsOutAmount - $operaionsInAmount;

		return round($amount / 100, 2);
	}

    /**
	 * Массив всех долговых контрагентов.
	 *
	 * @return array
     * @View: [ $project_id'_'$debet_project_id : 'id' ]
     * @Example: [ 44_45 : 20 ]
	 */
    public static function getDebtPartnersList() {
      $debtPartners = BudgetDebtPartner::all();
      
      $debtPartnersList = [];
      foreach ($debtPartners as $debtPartner){
        if($debtPartner->debet_project_id != null){
          $debtPartnersList[$debtPartner->project_id.'_'.$debtPartner->debet_project_id] = $debtPartner->id;
        }
      }
      
      return $debtPartnersList;
    }
    
}
