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

136 lines
2.8 KiB
PHP
Raw Normal View History

2015-05-16 07:51:23 -05:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-05-16 07:51:23 -05:00
namespace FireflyIII\Helpers\Collection;
use FireflyIII\Models\Budget as BudgetModel;
use Illuminate\Support\Collection;
/**
2015-05-16 12:08:54 -05:00
* @codeCoverageIgnore
*
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-02-05 02:25:15 -06:00
/** @var int */
2015-05-17 05:49:09 -05:00
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-01-01 14:15:03 -06:00
public function getBalanceEntries()
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
*/
public function getBudget()
{
return $this->budget;
}
/**
* @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;
}
2015-05-17 02:18:44 -05:00
/**
2015-05-17 05:49:09 -05:00
* @return int
*/
public function getRole()
{
return $this->role;
}
/**
* @param int $role
*/
2016-02-05 02:25:15 -06:00
public function setRole(int $role)
2015-05-17 05:49:09 -05:00
{
$this->role = $role;
}
2016-01-01 14:15:03 -06:00
/**
* @return string
*/
public function getTitle()
{
if ($this->getBudget() instanceof BudgetModel) {
return $this->getBudget()->name;
}
if ($this->getRole() == self::ROLE_DEFAULTROLE) {
return trans('firefly.noBudget');
}
if ($this->getRole() == self::ROLE_TAGROLE) {
return trans('firefly.coveredWithTags');
}
if ($this->getRole() == self::ROLE_DIFFROLE) {
return trans('firefly.leftUnbalanced');
}
return '';
}
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
*/
2015-05-17 05:49:09 -05:00
public function leftOfRepetition()
2015-05-17 02:18:44 -05:00
{
2016-02-05 02:25:15 -06:00
bcscale(2);
$start = $this->budget->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
}