2016-01-27 20:54:14 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BudgetReportHelper.php
|
2016-04-01 16:44:46 +02:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-01-27 20:54:14 +01:00
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-01-27 20:54:14 +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 20:54:14 +01:00
|
|
|
namespace FireflyIII\Helpers\Report;
|
|
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2016-04-24 20:00:20 +02:00
|
|
|
use FireflyIII\Models\Budget;
|
2016-12-30 08:41:48 +01:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2016-05-02 20:49:19 +02:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2016-01-27 20:54:14 +01:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BudgetReportHelper
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Report
|
|
|
|
*/
|
|
|
|
class BudgetReportHelper implements BudgetReportHelperInterface
|
|
|
|
{
|
2016-05-18 07:01:27 +02:00
|
|
|
/** @var BudgetRepositoryInterface */
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BudgetReportHelper constructor.
|
|
|
|
*
|
|
|
|
* @param BudgetRepositoryInterface $repository
|
|
|
|
*/
|
|
|
|
public function __construct(BudgetRepositoryInterface $repository)
|
|
|
|
{
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
2016-01-27 20:54:14 +01:00
|
|
|
|
2016-06-16 20:52:30 +02:00
|
|
|
/**
|
2016-12-28 16:45:44 +01:00
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5.
|
2017-09-16 09:24:48 +02:00
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) // all the arrays make it long.
|
2016-01-27 20:54:14 +01:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
2017-02-11 10:05:58 +01:00
|
|
|
* @return array
|
2016-01-27 20:54:14 +01:00
|
|
|
*/
|
2017-02-11 10:05:58 +01:00
|
|
|
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): array
|
2016-01-27 20:54:14 +01:00
|
|
|
{
|
2017-02-11 10:05:58 +01:00
|
|
|
$set = $this->repository->getBudgets();
|
|
|
|
$array = [];
|
2016-01-27 20:54:14 +01:00
|
|
|
|
2016-05-06 06:15:46 +02:00
|
|
|
/** @var Budget $budget */
|
2016-01-27 20:54:14 +01:00
|
|
|
foreach ($set as $budget) {
|
2016-12-30 08:41:48 +01:00
|
|
|
$budgetLimits = $this->repository->getBudgetLimits($budget, $start, $end);
|
2017-07-15 16:41:07 +02:00
|
|
|
if ($budgetLimits->count() === 0) { // no budget limit(s) for this budget
|
2017-02-11 10:05:58 +01:00
|
|
|
|
2017-01-05 09:08:35 +01:00
|
|
|
$spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);// spent for budget in time range
|
2017-02-11 10:05:58 +01: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-02-04 08:53:56 +01:00
|
|
|
}
|
2016-01-27 20:54:14 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-12-30 08:41:48 +01:00
|
|
|
/** @var BudgetLimit $budgetLimit */
|
|
|
|
foreach ($budgetLimits as $budgetLimit) { // one or more repetitions for budget
|
2017-02-11 10:05:58 +01: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 20:54:14 +01:00
|
|
|
|
2017-02-11 10:05:58 +01:00
|
|
|
'budgeted' => strval($budgetLimit->amount),
|
|
|
|
'spent' => $data['expenses'],
|
|
|
|
'left' => $data['left'],
|
|
|
|
'overspent' => $data['overspent'],
|
|
|
|
];
|
|
|
|
$array[] = $line;
|
2016-01-27 20:54:14 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-11 10:05:58 +01: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 20:54:14 +01:00
|
|
|
|
2017-02-11 10:05:58 +01:00
|
|
|
return $array;
|
2016-01-27 20:54:14 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 20:00:20 +02: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 20:49:19 +02:00
|
|
|
/** @var BudgetRepositoryInterface $repository */
|
|
|
|
$repository = app(BudgetRepositoryInterface::class);
|
2016-04-24 20:00:20 +02:00
|
|
|
$budgets = $repository->getActiveBudgets();
|
|
|
|
|
2016-04-25 18:43:09 +02:00
|
|
|
$set = new Collection;
|
2016-04-24 20:00:20 +02:00
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
2017-01-05 09:08:35 +01:00
|
|
|
$total = $repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);
|
2016-04-24 20:00:20 +02:00
|
|
|
if (bccomp($total, '0') === -1) {
|
|
|
|
$set->push($budget);
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 10:38:51 +02:00
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return $budget->name;
|
|
|
|
}
|
|
|
|
);
|
2016-04-24 20:00:20 +02:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:01:27 +02:00
|
|
|
/**
|
2016-12-30 08:41:48 +01:00
|
|
|
* @param Budget $budget
|
|
|
|
* @param BudgetLimit $budgetLimit
|
|
|
|
* @param Collection $accounts
|
2016-05-18 07:01:27 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-12-30 08:41:48 +01:00
|
|
|
private function calculateExpenses(Budget $budget, BudgetLimit $budgetLimit, Collection $accounts): array
|
2016-05-18 07:01:27 +02:00
|
|
|
{
|
|
|
|
$array = [];
|
2017-01-05 09:08:35 +01:00
|
|
|
$expenses = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $budgetLimit->start_date, $budgetLimit->end_date);
|
2016-12-30 08:41:48 +01:00
|
|
|
$array['left'] = bccomp(bcadd($budgetLimit->amount, $expenses), '0') === 1 ? bcadd($budgetLimit->amount, $expenses) : '0';
|
|
|
|
$array['spent'] = bccomp(bcadd($budgetLimit->amount, $expenses), '0') === 1 ? $expenses : '0';
|
|
|
|
$array['overspent'] = bccomp(bcadd($budgetLimit->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $budgetLimit->amount);
|
2016-05-18 14:19:33 +02:00
|
|
|
$array['expenses'] = $expenses;
|
2016-05-18 07:01:27 +02:00
|
|
|
|
|
|
|
return $array;
|
|
|
|
|
|
|
|
}
|
2016-01-28 21:50:20 +01:00
|
|
|
}
|