firefly-iii/app/Helpers/Collection/BalanceLine.php

181 lines
3.9 KiB
PHP
Raw Normal View History

2015-05-16 07:51:23 -05:00
<?php
/**
* BalanceLine.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-05-16 07:51:23 -05:00
namespace FireflyIII\Helpers\Collection;
2016-04-25 07:53:41 -05:00
use Carbon\Carbon;
2015-05-16 07:51:23 -05:00
use FireflyIII\Models\Budget as BudgetModel;
2016-12-30 01:41:48 -06:00
use FireflyIII\Models\BudgetLimit;
2015-05-16 07:51:23 -05:00
use Illuminate\Support\Collection;
/**
2015-05-16 12:08:54 -05:00
*
2015-05-16 07:51:23 -05:00
* Class BalanceLine
*
* @package FireflyIII\Helpers\Collection
*/
class BalanceLine
{
2015-05-17 05:49:09 -05:00
const ROLE_DEFAULTROLE = 1;
const ROLE_TAGROLE = 2;
const ROLE_DIFFROLE = 3;
2015-05-16 07:51:23 -05:00
/** @var Collection */
protected $balanceEntries;
2015-05-16 08:43:58 -05:00
2015-05-16 07:51:23 -05:00
/** @var BudgetModel */
protected $budget;
2016-12-30 01:41:48 -06:00
/** @var BudgetLimit */
protected $budgetLimit;
2016-12-30 06:45:02 -06:00
/** @var int */
protected $role = self::ROLE_DEFAULTROLE;
2015-05-16 08:43:58 -05:00
2015-05-16 07:51:23 -05:00
/**
*
*/
public function __construct()
{
$this->balanceEntries = new Collection;
2016-02-18 03:04:26 -06:00
2015-05-16 07:51:23 -05:00
}
2015-05-16 08:43:58 -05:00
/**
* @param BalanceEntry $balanceEntry
*/
2015-05-16 07:51:23 -05:00
public function addBalanceEntry(BalanceEntry $balanceEntry)
{
$this->balanceEntries->push($balanceEntry);
}
2015-05-17 05:49:09 -05:00
/**
2016-01-01 14:15:03 -06:00
* @return Collection
2015-05-17 05:49:09 -05:00
*/
2016-02-18 03:04:26 -06:00
public function getBalanceEntries(): Collection
2015-05-17 05:49:09 -05:00
{
2016-01-01 14:15:03 -06:00
return $this->balanceEntries;
}
2015-06-03 14:25:11 -05:00
2016-01-01 14:15:03 -06:00
/**
* @param Collection $balanceEntries
*/
2016-02-05 02:25:15 -06:00
public function setBalanceEntries(Collection $balanceEntries)
2016-01-01 14:15:03 -06:00
{
$this->balanceEntries = $balanceEntries;
2015-05-17 05:49:09 -05:00
}
2015-05-16 07:51:23 -05:00
/**
* @return BudgetModel
*/
2016-02-18 03:04:26 -06:00
public function getBudget(): BudgetModel
2015-05-16 07:51:23 -05:00
{
2016-02-18 03:04:26 -06:00
return $this->budget ?? new BudgetModel;
2015-05-16 07:51:23 -05:00
}
/**
* @param BudgetModel $budget
*/
2016-02-05 02:25:15 -06:00
public function setBudget(BudgetModel $budget)
2015-05-16 07:51:23 -05:00
{
$this->budget = $budget;
}
2016-12-30 06:45:02 -06:00
/**
* @return BudgetLimit
*/
public function getBudgetLimit(): BudgetLimit
{
return $this->budgetLimit;
}
/**
* @param BudgetLimit $budgetLimit
*/
public function setBudgetLimit(BudgetLimit $budgetLimit)
{
$this->budgetLimit = $budgetLimit;
}
2015-05-17 02:18:44 -05:00
/**
2016-08-26 01:21:31 -05:00
* @return Carbon
2015-05-17 05:49:09 -05:00
*/
2016-08-26 01:21:31 -05:00
public function getEndDate()
2015-05-17 05:49:09 -05:00
{
2016-12-30 01:41:48 -06:00
return $this->budgetLimit->end_date ?? new Carbon;
2015-05-17 05:49:09 -05:00
}
2016-01-01 14:15:03 -06:00
/**
2016-08-26 01:21:31 -05:00
* @return int
2016-01-01 14:15:03 -06:00
*/
2016-08-26 01:21:31 -05:00
public function getRole(): int
2016-01-01 14:15:03 -06:00
{
2016-08-26 01:21:31 -05:00
return $this->role;
}
2016-01-01 14:15:03 -06:00
2016-08-26 01:21:31 -05:00
/**
* @param int $role
*/
public function setRole(int $role)
{
$this->role = $role;
2016-01-01 14:15:03 -06:00
}
2016-04-25 07:53:41 -05:00
/**
* @return Carbon
*/
public function getStartDate()
{
2016-12-30 01:41:48 -06:00
return $this->budgetLimit->start_date ?? new Carbon;
2016-04-25 07:53:41 -05:00
}
/**
2016-12-27 13:52:00 -06:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-08-26 01:21:31 -05:00
* @return string
2016-04-25 07:53:41 -05:00
*/
2016-08-26 01:21:31 -05:00
public function getTitle(): string
2016-04-25 07:53:41 -05:00
{
2016-08-26 01:21:31 -05:00
if ($this->getBudget() instanceof BudgetModel && !is_null($this->getBudget()->id)) {
return $this->getBudget()->name;
}
if ($this->getRole() == self::ROLE_DEFAULTROLE) {
2016-12-27 13:52:00 -06:00
return strval(trans('firefly.no_budget'));
2016-08-26 01:21:31 -05:00
}
if ($this->getRole() == self::ROLE_TAGROLE) {
2016-12-27 13:52:00 -06:00
return strval(trans('firefly.coveredWithTags'));
2016-08-26 01:21:31 -05:00
}
if ($this->getRole() == self::ROLE_DIFFROLE) {
2016-12-27 13:52:00 -06:00
return strval(trans('firefly.leftUnbalanced'));
2016-08-26 01:21:31 -05:00
}
2016-04-25 07:53:41 -05:00
2016-08-26 01:21:31 -05:00
return '';
2016-04-25 07:53:41 -05:00
}
2015-05-17 05:49:09 -05:00
/**
* If a BalanceLine has a budget/repetition, each BalanceEntry in this BalanceLine
* should have a "spent" value, which is the amount of money that has been spent
* on the given budget/repetition. If you subtract all those amounts from the budget/repetition's
* total amount, this is returned:
*
2016-02-05 02:25:15 -06:00
* @return string
2015-05-17 02:18:44 -05:00
*/
2016-02-18 03:04:26 -06:00
public function leftOfRepetition(): string
2015-05-17 02:18:44 -05:00
{
2016-12-30 01:41:48 -06:00
$start = $this->budgetLimit->amount ?? '0';
2015-05-17 02:18:44 -05:00
/** @var BalanceEntry $balanceEntry */
foreach ($this->getBalanceEntries() as $balanceEntry) {
2016-02-05 02:25:15 -06:00
$start = bcadd($balanceEntry->getSpent(), $start);
2015-05-17 02:18:44 -05:00
}
return $start;
}
2015-05-20 12:56:14 -05:00
}