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

196 lines
4.4 KiB
PHP
Raw Normal View History

2015-05-16 07:51:23 -05:00
<?php
/**
* BalanceLine.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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;
/**
2017-11-15 05:25:49 -06:00
* Class BalanceLine.
2015-05-16 07:51:23 -05:00
*/
class BalanceLine
{
2017-12-17 07:30:53 -06:00
/**
*
*/
2015-05-17 05:49:09 -05:00
const ROLE_DEFAULTROLE = 1;
2017-12-17 07:30:53 -06:00
/**
*
*/
const ROLE_TAGROLE = 2;
/**
*
*/
const ROLE_DIFFROLE = 3;
2015-05-17 05:49:09 -05:00
2017-11-15 05:25:49 -06:00
/** @var Collection */
2015-05-16 07:51:23 -05:00
protected $balanceEntries;
2015-05-16 08:43:58 -05:00
2015-05-16 07:51:23 -05:00
/** @var BudgetModel */
protected $budget;
2017-11-15 05:25:49 -06:00
/** @var BudgetLimit */
2016-12-30 01:41:48 -06:00
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;
}
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)
2017-11-15 05:25:49 -06:00
*
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
{
2017-11-15 05:25:49 -06:00
if ($this->getBudget() instanceof BudgetModel && null !== $this->getBudget()->id) {
2016-08-26 01:21:31 -05:00
return $this->getBudget()->name;
}
2017-11-15 05:25:49 -06:00
if (self::ROLE_DEFAULTROLE === $this->getRole()) {
2016-12-27 13:52:00 -06:00
return strval(trans('firefly.no_budget'));
2016-08-26 01:21:31 -05:00
}
2017-11-15 05:25:49 -06:00
if (self::ROLE_TAGROLE === $this->getRole()) {
2016-12-27 13:52:00 -06:00
return strval(trans('firefly.coveredWithTags'));
2016-08-26 01:21:31 -05:00
}
2017-11-15 05:25:49 -06:00
if (self::ROLE_DIFFROLE === $this->getRole()) {
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
2017-11-15 05:25:49 -06:00
* total amount, this is returned:.
2015-05-17 05:49:09 -05:00
*
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
}