2016-01-27 21:35:59 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BalanceReportHelper.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-01-27 21:35:59 +01:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02: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 14:41:58 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-01-27 21:35:59 +01:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-05-20 12:27:31 +02:00
|
|
|
|
2016-01-27 21:35:59 +01:00
|
|
|
namespace FireflyIII\Helpers\Report;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Helpers\Collection\Balance;
|
|
|
|
use FireflyIII\Helpers\Collection\BalanceEntry;
|
|
|
|
use FireflyIII\Helpers\Collection\BalanceHeader;
|
|
|
|
use FireflyIII\Helpers\Collection\BalanceLine;
|
2016-12-30 08:41:48 +01:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2016-01-27 21:35:59 +01:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
|
|
use Illuminate\Support\Collection;
|
2016-12-22 21:45:04 +01:00
|
|
|
use Log;
|
2016-01-27 21:35:59 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class BalanceReportHelper.
|
2016-01-27 21:35:59 +01:00
|
|
|
*/
|
|
|
|
class BalanceReportHelper implements BalanceReportHelperInterface
|
|
|
|
{
|
2018-07-07 23:14:16 +02:00
|
|
|
/** @var BudgetRepositoryInterface Budget repository */
|
2016-01-27 21:35:59 +01:00
|
|
|
protected $budgetRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ReportHelper constructor.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param BudgetRepositoryInterface $budgetRepository
|
|
|
|
*/
|
2016-05-18 07:01:27 +02:00
|
|
|
public function __construct(BudgetRepositoryInterface $budgetRepository)
|
2016-01-27 21:35:59 +01:00
|
|
|
{
|
|
|
|
$this->budgetRepository = $budgetRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Generate a balance report.
|
|
|
|
*
|
2016-12-06 08:59:08 +01:00
|
|
|
* @param Collection $accounts
|
2016-01-27 21:35:59 +01:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Balance
|
|
|
|
*/
|
2016-12-06 08:59:08 +01:00
|
|
|
public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): Balance
|
2016-01-27 21:35:59 +01:00
|
|
|
{
|
2016-12-22 21:45:04 +01:00
|
|
|
Log::debug('Start of balance report');
|
2016-12-30 08:41:48 +01:00
|
|
|
$balance = new Balance;
|
|
|
|
$header = new BalanceHeader;
|
|
|
|
$budgetLimits = $this->budgetRepository->getAllBudgetLimits($start, $end);
|
2016-01-27 21:35:59 +01:00
|
|
|
foreach ($accounts as $account) {
|
2016-12-22 21:45:04 +01:00
|
|
|
Log::debug(sprintf('Add account %s to headers.', $account->name));
|
2016-01-27 21:35:59 +01:00
|
|
|
$header->addAccount($account);
|
|
|
|
}
|
|
|
|
|
2016-12-30 08:41:48 +01:00
|
|
|
/** @var BudgetLimit $budgetLimit */
|
|
|
|
foreach ($budgetLimits as $budgetLimit) {
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null !== $budgetLimit->budget) {
|
2017-09-27 08:45:27 +02:00
|
|
|
$line = $this->createBalanceLine($budgetLimit, $accounts);
|
|
|
|
$balance->addBalanceLine($line);
|
|
|
|
}
|
2016-01-27 21:35:59 +01:00
|
|
|
}
|
2017-09-09 06:41:45 +02:00
|
|
|
$noBudgetLine = $this->createNoBudgetLine($accounts, $start, $end);
|
2016-01-27 21:35:59 +01:00
|
|
|
|
2016-05-11 09:08:18 +02:00
|
|
|
$balance->addBalanceLine($noBudgetLine);
|
2016-01-27 21:35:59 +01:00
|
|
|
$balance->setBalanceHeader($header);
|
|
|
|
|
2016-12-22 21:45:04 +01:00
|
|
|
Log::debug('Clear unused budgets.');
|
2016-04-29 11:05:52 +02:00
|
|
|
// remove budgets without expenses from balance lines:
|
|
|
|
$balance = $this->removeUnusedBudgets($balance);
|
|
|
|
|
2016-12-22 21:45:04 +01:00
|
|
|
Log::debug('Return report.');
|
|
|
|
|
2016-01-27 21:35:59 +01:00
|
|
|
return $balance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Create one balance line.
|
|
|
|
*
|
2016-12-30 08:41:48 +01:00
|
|
|
* @param BudgetLimit $budgetLimit
|
|
|
|
* @param Collection $accounts
|
2016-01-27 21:35:59 +01:00
|
|
|
*
|
|
|
|
* @return BalanceLine
|
|
|
|
*/
|
2016-12-30 08:41:48 +01:00
|
|
|
private function createBalanceLine(BudgetLimit $budgetLimit, Collection $accounts): BalanceLine
|
2016-01-27 21:35:59 +01:00
|
|
|
{
|
2016-12-30 08:41:48 +01:00
|
|
|
$line = new BalanceLine;
|
|
|
|
$line->setBudget($budgetLimit->budget);
|
|
|
|
$line->setBudgetLimit($budgetLimit);
|
2016-01-27 21:35:59 +01:00
|
|
|
|
|
|
|
// loop accounts:
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$balanceEntry = new BalanceEntry;
|
|
|
|
$balanceEntry->setAccount($account);
|
2017-02-17 06:42:36 +01:00
|
|
|
$spent = $this->budgetRepository->spentInPeriod(
|
2017-11-15 10:52:29 +01:00
|
|
|
new Collection([$budgetLimit->budget]),
|
|
|
|
new Collection([$account]),
|
|
|
|
$budgetLimit->start_date,
|
|
|
|
$budgetLimit->end_date
|
2017-02-17 06:42:36 +01:00
|
|
|
);
|
2016-01-27 21:35:59 +01:00
|
|
|
$balanceEntry->setSpent($spent);
|
|
|
|
$line->addBalanceEntry($balanceEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Create a line for transactions without a budget.
|
|
|
|
*
|
2016-01-27 21:35:59 +01:00
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return BalanceLine
|
|
|
|
*/
|
2016-05-11 09:08:18 +02:00
|
|
|
private function createNoBudgetLine(Collection $accounts, Carbon $start, Carbon $end): BalanceLine
|
2016-01-27 21:35:59 +01:00
|
|
|
{
|
|
|
|
$empty = new BalanceLine;
|
|
|
|
|
|
|
|
foreach ($accounts as $account) {
|
2017-01-05 09:10:04 +01:00
|
|
|
$spent = $this->budgetRepository->spentInPeriodWoBudget(new Collection([$account]), $start, $end);
|
2016-01-27 21:35:59 +01:00
|
|
|
// budget
|
|
|
|
$budgetEntry = new BalanceEntry;
|
|
|
|
$budgetEntry->setAccount($account);
|
|
|
|
$budgetEntry->setSpent($spent);
|
|
|
|
$empty->addBalanceEntry($budgetEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $empty;
|
|
|
|
}
|
|
|
|
|
2016-04-29 11:05:52 +02:00
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Remove unused budgets from the report.
|
|
|
|
*
|
2018-07-07 21:17:46 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2016-04-29 11:05:52 +02:00
|
|
|
* @param Balance $balance
|
2016-08-26 08:21:31 +02:00
|
|
|
*
|
2016-04-29 11:05:52 +02:00
|
|
|
* @return Balance
|
|
|
|
*/
|
|
|
|
private function removeUnusedBudgets(Balance $balance): Balance
|
|
|
|
{
|
|
|
|
$set = $balance->getBalanceLines();
|
|
|
|
$newSet = new Collection;
|
2018-08-06 19:14:30 +02:00
|
|
|
/** @var BalanceLine $entry */
|
2016-04-29 11:05:52 +02:00
|
|
|
foreach ($set as $entry) {
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null !== $entry->getBudget()->id) {
|
2016-04-29 11:05:52 +02:00
|
|
|
$sum = '0';
|
2018-07-07 21:17:46 +02:00
|
|
|
/** @var BalanceEntry $balanceEntry */
|
2016-04-29 11:05:52 +02:00
|
|
|
foreach ($entry->getBalanceEntries() as $balanceEntry) {
|
|
|
|
$sum = bcadd($sum, $balanceEntry->getSpent());
|
|
|
|
}
|
|
|
|
if (bccomp($sum, '0') === -1) {
|
|
|
|
$newSet->push($entry);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$newSet->push($entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
$balance->setBalanceLines($newSet);
|
|
|
|
|
|
|
|
return $balance;
|
|
|
|
}
|
2016-01-28 21:50:20 +01:00
|
|
|
}
|