2015-05-16 07:51:23 -05:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* BalanceLine.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:27:31 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
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/>.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05: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;
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-03-28 12:37:59 -05:00
|
|
|
public const ROLE_DEFAULTROLE = 1;
|
2017-12-17 07:30:53 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-03-28 12:37:59 -05:00
|
|
|
public const ROLE_TAGROLE = 2;
|
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
|
|
|
|
*/
|
2018-07-07 14:17:46 -05:00
|
|
|
public function addBalanceEntry(BalanceEntry $balanceEntry): void
|
2015-05-16 07:51:23 -05:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2018-07-07 14:17:46 -05:00
|
|
|
public function setBalanceEntries(Collection $balanceEntries): void
|
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
|
|
|
|
*/
|
2018-07-07 14:17:46 -05:00
|
|
|
public function setBudget(BudgetModel $budget): void
|
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
|
|
|
|
*/
|
2018-07-07 14:17:46 -05:00
|
|
|
public function setBudgetLimit(BudgetLimit $budgetLimit): void
|
2016-12-30 06:45:02 -06:00
|
|
|
{
|
|
|
|
$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
|
|
|
*/
|
2018-07-07 14:17:46 -05:00
|
|
|
public function getEndDate(): Carbon
|
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
|
|
|
|
*/
|
2018-07-07 14:17:46 -05:00
|
|
|
public function setRole(int $role): void
|
2016-08-26 01:21:31 -05:00
|
|
|
{
|
|
|
|
$this->role = $role;
|
2016-01-01 14:15:03 -06:00
|
|
|
}
|
|
|
|
|
2016-04-25 07:53:41 -05:00
|
|
|
/**
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
2018-07-07 14:17:46 -05:00
|
|
|
public function getStartDate(): Carbon
|
2016-04-25 07:53:41 -05:00
|
|
|
{
|
2016-12-30 01:41:48 -06:00
|
|
|
return $this->budgetLimit->start_date ?? new Carbon;
|
2016-04-25 07:53:41 -05: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
|
|
|
{
|
2018-07-07 14:17:46 -05:00
|
|
|
$title = '';
|
2017-11-15 05:25:49 -06:00
|
|
|
if ($this->getBudget() instanceof BudgetModel && null !== $this->getBudget()->id) {
|
2018-07-07 14:17:46 -05:00
|
|
|
$title = $this->getBudget()->name;
|
2016-08-26 01:21:31 -05:00
|
|
|
}
|
2018-07-07 14:17:46 -05:00
|
|
|
if ('' === $title && self::ROLE_DEFAULTROLE === $this->getRole()) {
|
|
|
|
$title = (string)trans('firefly.no_budget');
|
2016-08-26 01:21:31 -05:00
|
|
|
}
|
2018-07-07 14:17:46 -05:00
|
|
|
if ('' === $title && self::ROLE_TAGROLE === $this->getRole()) {
|
|
|
|
$title = (string)trans('firefly.coveredWithTags');
|
2016-08-26 01:21:31 -05:00
|
|
|
}
|
2016-04-25 07:53:41 -05:00
|
|
|
|
2018-07-07 14:17:46 -05:00
|
|
|
return $title;
|
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
|
|
|
}
|