firefly-iii/app/Helpers/Report/BudgetReportHelper.php

164 lines
5.7 KiB
PHP
Raw Normal View History

2016-01-27 13:54:14 -06:00
<?php
/**
* BudgetReportHelper.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-01-27 13:54:14 -06:00
*
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/>.
2016-01-27 13:54:14 -06:00
*/
declare(strict_types=1);
2016-01-27 13:54:14 -06:00
namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
2016-04-24 13:00:20 -05:00
use FireflyIII\Models\Budget;
2016-12-30 01:41:48 -06:00
use FireflyIII\Models\BudgetLimit;
2016-05-02 13:49:19 -05:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2016-01-27 13:54:14 -06:00
use Illuminate\Support\Collection;
/**
2017-11-15 05:25:49 -06:00
* Class BudgetReportHelper.
2016-01-27 13:54:14 -06:00
*/
class BudgetReportHelper implements BudgetReportHelperInterface
{
2016-05-18 00:01:27 -05:00
/** @var BudgetRepositoryInterface */
private $repository;
/**
* BudgetReportHelper constructor.
*
* @param BudgetRepositoryInterface $repository
*/
public function __construct(BudgetRepositoryInterface $repository)
{
$this->repository = $repository;
}
2016-01-27 13:54:14 -06:00
2016-06-16 13:52:30 -05:00
/**
2016-12-28 09:45:44 -06:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5.
2017-09-16 02:24:48 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) // all the arrays make it long.
2017-11-15 05:25:49 -06:00
*
2016-01-27 13:54:14 -06:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
2017-02-11 03:05:58 -06:00
* @return array
2016-01-27 13:54:14 -06:00
*/
2017-02-11 03:05:58 -06:00
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): array
2016-01-27 13:54:14 -06:00
{
2017-02-11 03:05:58 -06:00
$set = $this->repository->getBudgets();
$array = [];
2016-01-27 13:54:14 -06:00
2016-05-05 23:15:46 -05:00
/** @var Budget $budget */
2016-01-27 13:54:14 -06:00
foreach ($set as $budget) {
2016-12-30 01:41:48 -06:00
$budgetLimits = $this->repository->getBudgetLimits($budget, $start, $end);
2017-11-15 05:25:49 -06:00
if (0 === $budgetLimits->count()) { // no budget limit(s) for this budget
$spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end); // spent for budget in time range
2017-02-11 03:05:58 -06:00
if (bccomp($spent, '0') === -1) {
$line = [
'type' => 'budget',
'id' => $budget->id,
'name' => $budget->name,
'budgeted' => '0',
'spent' => $spent,
'left' => '0',
'overspent' => '0',
];
$array[] = $line;
}
2016-01-27 13:54:14 -06:00
continue;
}
2016-12-30 01:41:48 -06:00
/** @var BudgetLimit $budgetLimit */
foreach ($budgetLimits as $budgetLimit) { // one or more repetitions for budget
2017-02-11 03:05:58 -06:00
$data = $this->calculateExpenses($budget, $budgetLimit, $accounts);
$line = [
'type' => 'budget-line',
'start' => $budgetLimit->start_date,
'end' => $budgetLimit->end_date,
'limit' => $budgetLimit->id,
'id' => $budget->id,
'name' => $budget->name,
2016-01-27 13:54:14 -06:00
2018-04-02 07:42:07 -05:00
'budgeted' => (string)$budgetLimit->amount,
2017-02-11 03:05:58 -06:00
'spent' => $data['expenses'],
'left' => $data['left'],
'overspent' => $data['overspent'],
];
$array[] = $line;
2016-01-27 13:54:14 -06:00
}
}
2017-02-11 03:05:58 -06:00
$noBudget = $this->repository->spentInPeriodWoBudget($accounts, $start, $end); // stuff outside of budgets
$line = [
'type' => 'no-budget',
'budgeted' => '0',
'spent' => $noBudget,
'left' => '0',
'overspent' => '0',
];
$array[] = $line;
2016-01-27 13:54:14 -06:00
2017-02-11 03:05:58 -06:00
return $array;
2016-01-27 13:54:14 -06:00
}
2016-04-24 13:00:20 -05:00
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return Collection
*/
public function getBudgetsWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection
{
2016-05-02 13:49:19 -05:00
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
2016-04-24 13:00:20 -05:00
$budgets = $repository->getActiveBudgets();
2016-04-25 11:43:09 -05:00
$set = new Collection;
2016-04-24 13:00:20 -05:00
/** @var Budget $budget */
foreach ($budgets as $budget) {
2017-01-05 02:08:35 -06:00
$total = $repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);
2016-04-24 13:00:20 -05:00
if (bccomp($total, '0') === -1) {
$set->push($budget);
}
}
2016-04-27 03:38:51 -05:00
$set = $set->sortBy(
function (Budget $budget) {
return $budget->name;
}
);
2016-04-24 13:00:20 -05:00
return $set;
}
2016-05-18 00:01:27 -05:00
/**
2016-12-30 01:41:48 -06:00
* @param Budget $budget
* @param BudgetLimit $budgetLimit
* @param Collection $accounts
2016-05-18 00:01:27 -05:00
*
* @return array
*/
2016-12-30 01:41:48 -06:00
private function calculateExpenses(Budget $budget, BudgetLimit $budgetLimit, Collection $accounts): array
2016-05-18 00:01:27 -05:00
{
$array = [];
2017-01-05 02:08:35 -06:00
$expenses = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $budgetLimit->start_date, $budgetLimit->end_date);
2017-11-15 05:25:49 -06:00
$array['left'] = 1 === bccomp(bcadd($budgetLimit->amount, $expenses), '0') ? bcadd($budgetLimit->amount, $expenses) : '0';
$array['spent'] = 1 === bccomp(bcadd($budgetLimit->amount, $expenses), '0') ? $expenses : '0';
$array['overspent'] = 1 === bccomp(bcadd($budgetLimit->amount, $expenses), '0') ? '0' : bcadd($expenses, $budgetLimit->amount);
2016-05-18 07:19:33 -05:00
$array['expenses'] = $expenses;
2016-05-18 00:01:27 -05:00
return $array;
}
}