<?php

class BudgetCost extends \Eloquent {

	use SoftDeletingTrait;

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

	const MANAGED_FULL = 1;
	const MANAGED_HALF = 2;
	const MANAGED_NONE = 3;

	protected $managedNames = array(
			BudgetCost::MANAGED_FULL => 'Полностью управляемые',
			BudgetCost::MANAGED_HALF => 'Частично управляемые',
			BudgetCost::MANAGED_NONE => 'Неуправляемые',
	);

	public function budgetCostGroup(){
		return $this->belongsTo('BudgetCostGroup');
	}

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

	public function setPlanAmountAttribute($value) {
		$value													 = str_replace(',', '.', $value);
		$value													 = str_replace(' ', '', $value);
		$this->attributes['plan_amount'] = round($value * 100, 0);
	}

	public function getPlanAmountAttribute() {
		return round($this->attributes['plan_amount'] / 100, 2);
	}

	public function getSum($projectId, $endDate = null) {
		if (!$endDate) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		$operaionsOutAmount = BudgetOperation::whereProjectId($projectId)
						->whereIsdebt(0)
						->whereBudgetCostId($this->id)
						->whereType(BudgetOperation::TYPE_OUTCOMING)
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		return round($operaionsOutAmount / 100, 2);
	}
	
	public function getSumOutcome($projectId, $endDate = null) {
		if (!$endDate) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		$operaionsOutAmount = BudgetOperation::whereProjectId($projectId)
																					->whereIsnooutcome(0)
																					->whereIsdebt(0)
																					->whereBudgetCostId($this->id)
																					->whereType(BudgetOperation::TYPE_OUTCOMING)
																					->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
																					->sum('amount');
		return round($operaionsOutAmount / 100, 2);
	}
	
	public function getArchivedSum($projectId, $startDate = null, $endDate = null) {
		if (!isset($startDate)) {
			$startDate = date("d.m.Y", time() - 3600 * 24);
		}
		else {
			$date		 = explode('.', $startDate);
			$startDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		if (!isset($endDate)) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		$operaionsOutAmount = BudgetOperation::withTrashed()
						                              ->whereProjectId($projectId)
																					->whereIsdebt(0)
						                              ->whereIsarchived(1)
																					->whereBudgetCostId($this->id)
																					->whereType(BudgetOperation::TYPE_OUTCOMING)
																					->whereRaw(DB::raw('DATE(date)>=?'), array($startDate))                             
																				  ->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
																					->sum('amount');
		
		return round($operaionsOutAmount / 100, 2);
	}
	
	public function getArchivedSumOutcome($projectId, $startDate = null, $endDate = null) {
		if (!isset($startDate)) {
			$startDate = date("d.m.Y", time() - 3600 * 24);
		}
		else {
			$date		 = explode('.', $startDate);
			$startDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		if (!isset($endDate)) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		$operaionsOutAmount = BudgetOperation::withTrashed()
						                              ->whereProjectId($projectId)
																					->whereIsnooutcome(0)
						                              ->whereIsdebt(0)
						                              ->whereIsarchived(1)
																					->whereBudgetCostId($this->id)
																					->whereType(BudgetOperation::TYPE_OUTCOMING)
																					->whereRaw(DB::raw('DATE(date)>=?'), array($startDate))                             
																				  ->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
																					->sum('amount');
		
		return round($operaionsOutAmount / 100, 2);
	}
	
	public function getTotalSum($endDate = null) {
		if (!$endDate) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		$operaionsOutAmount = BudgetOperation::whereIsdebt(0)
						->whereBudgetCostId($this->id)
						->whereType(BudgetOperation::TYPE_OUTCOMING)
						->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
						->sum('amount');
		return round($operaionsOutAmount / 100, 2);
	}	
	
	public function getTotalSumOutcome($endDate = null) {
		if (!$endDate) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		$operaionsOutAmount = BudgetOperation::whereIsnooutcome(0)
																					->whereBudgetCostId($this->id)
						                              ->whereIsdebt(0)
																					->whereType(BudgetOperation::TYPE_OUTCOMING)
																					->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
																					->sum('amount');
		return round($operaionsOutAmount / 100, 2);
	}
	
	public function getArchiveTotalSum($startDate = null, $endDate = null) {
		
		if (!isset($startDate)) {
			$startDate = date("d.m.Y", time() - 3600 * 24);
		}
		else {
			$date		 = explode('.', $startDate);
			$startDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		if (!isset($endDate)) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		$operaionsOutAmount = BudgetOperation::withTrashed()
																					->whereIsdebt(0)
						                              ->whereIsarchived(1)
																					->whereBudgetCostId($this->id)
																					->whereType(BudgetOperation::TYPE_OUTCOMING)
																					->whereRaw(DB::raw('DATE(date)>=?'), array($startDate))                             
																				  ->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
																					->sum('amount');

		return round($operaionsOutAmount / 100, 2);
	}
	
	public function getArchiveTotalSumOutcome($startDate = null, $endDate = null) {
		
		if (!isset($startDate)) {
			$startDate = date("d.m.Y", time() - 3600 * 24);
		}
		else {
			$date		 = explode('.', $startDate);
			$startDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		if (!isset($endDate)) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		$operaionsOutAmount = BudgetOperation::withTrashed()
																					->whereIsnooutcome(0)
						                              ->whereIsdebt(0)
						                              ->whereIsarchived(1)
																					->whereBudgetCostId($this->id)
																					->whereType(BudgetOperation::TYPE_OUTCOMING)
																					->whereRaw(DB::raw('DATE(date)>=?'), array($startDate))                             
																				  ->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
																					->sum('amount');

		return round($operaionsOutAmount / 100, 2);
	}
	
	public function getManagedName() {
		return $this->managedNames[$this->managed];
	}

	//--------------------------------------- Отчет - расходы по месяцам -------------------------------------------//
	
	public function getProjectArchivedSum($projectId, $startDate = null, $endDate = null) {
		if (!isset($startDate)) {
			$startDate = date("d.m.Y", time() - 3600 * 24);
		}
		else {
			$date		 = explode('.', $startDate);
			$startDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		if (!isset($endDate)) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		$operaionsOutAmount = BudgetOperation::withTrashed()
						                              ->whereProjectId($projectId)
																					->whereIsdebt(0)
						                              ->whereIsarchived(1)
																					->whereBudgetCostId($this->id)
																					->whereType(BudgetOperation::TYPE_OUTCOMING)
																					->whereRaw(DB::raw('DATE(date)>=?'), array($startDate))                             
																				  ->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
																					->select(DB::raw('sum(budget_operations.amount)/100 as sum, MONTH(budget_operations.date) as month, YEAR(budget_operations.date) as year, budget_operations.date'))
						                              ->groupBy(DB::raw('YEAR(budget_operations.date)'))
																					->groupBy(DB::raw('MONTH(budget_operations.date)'))
						                              ->orderBy('budget_operations.date', 'DESC')
																					->get();

		$operaions = array();
		foreach($operaionsOutAmount as $item){
			$id = $item['month'].'-'.$item['year'];
			$operaions[$id] = $item;
		}
		
		return $operaions;
	}
	
	public function getProjectArchivedSumOutcome($projectId, $startDate = null, $endDate = null) {
		if (!isset($startDate)) {
			$startDate = date("d.m.Y", time() - 3600 * 24);
		}
		else {
			$date		 = explode('.', $startDate);
			$startDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		if (!isset($endDate)) {
			$endDate = date('Y-m-d');
		}
		else {
			$date		 = explode('.', $endDate);
			$endDate = "{$date[2]}-{$date[1]}-{$date[0]}";
		}
		
		$operaionsOutAmount = BudgetOperation::withTrashed()
						                              ->whereProjectId($projectId)
																					->whereIsnooutcome(0)
						                              ->whereIsdebt(0)
						                              ->whereIsarchived(1)
																					->whereBudgetCostId($this->id)
																					->whereType(BudgetOperation::TYPE_OUTCOMING)
																					->whereRaw(DB::raw('DATE(date)>=?'), array($startDate))                             
																				  ->whereRaw(DB::raw('DATE(date)<=?'), array($endDate))
																					->select(DB::raw('sum(budget_operations.amount)/100 as sum, MONTH(budget_operations.date) as month, YEAR(budget_operations.date) as year, budget_operations.date'))
						                              ->groupBy(DB::raw('YEAR(budget_operations.date)'))
																					->groupBy(DB::raw('MONTH(budget_operations.date)'))
						                              ->orderBy('budget_operations.date', 'DESC')
																					->get();

		$operaions = array();
		foreach($operaionsOutAmount as $item){
			$id = $item['month'].'-'.$item['year'];
			$operaions[$id] = $item;
		}
		
		return $operaions;
	}

}
