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

276 lines
8.8 KiB
PHP
Raw Normal View History

2016-01-27 13:54:14 -06:00
<?php
/**
* BudgetReportHelper.php
2016-04-01 09:44:46 -05:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-01-27 13:54:14 -06:00
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
declare(strict_types = 1);
2016-01-27 13:54:14 -06:00
namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
use FireflyIII\Helpers\Collection\BudgetLine;
2016-04-24 13:00:20 -05:00
use FireflyIII\Models\Budget;
2016-01-27 13:54:14 -06:00
use FireflyIII\Models\LimitRepetition;
2016-05-02 13:49:19 -05:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2016-06-16 13:52:30 -05:00
use FireflyIII\Support\CacheProperties;
2016-01-27 13:54:14 -06:00
use Illuminate\Support\Collection;
/**
* Class BudgetReportHelper
*
* @package FireflyIII\Helpers\Report
*/
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
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) // at 43, its ok.
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5.
*
2016-06-16 13:52:30 -05:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return Collection
*/
public function budgetYearOverview(Carbon $start, Carbon $end, Collection $accounts): Collection
{
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('budget-year');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get();
}
2016-06-16 13:52:30 -05:00
$current = clone $start;
$return = new Collection;
$set = $this->repository->getBudgets();
$budgets = [];
$spent = [];
$headers = $this->createYearHeaders($current, $end);
2016-06-16 13:52:30 -05:00
/** @var Budget $budget */
foreach ($set as $budget) {
$id = $budget->id;
$budgets[$id] = $budget->name;
$current = clone $start;
$budgetData = $this->getBudgetSpentData($current, $end, $budget, $accounts);
$sum = $budgetData['sum'];
$spent[$id] = $budgetData['spent'];
2016-06-16 13:52:30 -05:00
if (bccomp('0', $sum) === 0) {
// not spent anything.
unset($spent[$id]);
unset($budgets[$id]);
}
}
$return->put('headers', $headers);
$return->put('budgets', $budgets);
$return->put('spent', $spent);
$cache->store($return);
return $return;
}
/**
2016-01-27 13:54:14 -06:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return BudgetCollection
*/
2016-02-05 12:54:25 -06:00
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): BudgetCollection
2016-01-27 13:54:14 -06:00
{
$object = new BudgetCollection;
2016-05-18 00:01:27 -05:00
$set = $this->repository->getBudgets();
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-05-05 23:15:46 -05:00
$repetitions = $budget->limitrepetitions()->before($end)->after($start)->get();
2016-01-27 13:54:14 -06:00
// no repetition(s) for this budget:
if ($repetitions->count() == 0) {
2016-05-05 23:15:46 -05:00
// spent for budget in time range:
2016-05-18 00:01:27 -05:00
$spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);
if ($spent > 0) {
$budgetLine = new BudgetLine;
$budgetLine->setBudget($budget)->setOverspent($spent);
$object->addOverspent($spent)->addBudgetLine($budgetLine);
}
2016-01-27 13:54:14 -06:00
continue;
}
// one or more repetitions for budget:
/** @var LimitRepetition $repetition */
foreach ($repetitions as $repetition) {
2016-05-18 00:01:27 -05:00
$data = $this->calculateExpenses($budget, $repetition, $accounts);
2016-05-05 23:15:46 -05:00
2016-01-27 13:54:14 -06:00
$budgetLine = new BudgetLine;
$budgetLine->setBudget($budget)->setRepetition($repetition)
->setLeft($data['left'])->setSpent($data['expenses'])->setOverspent($data['overspent'])
->setBudgeted(strval($repetition->amount));
$object->addBudgeted(strval($repetition->amount))->addSpent($data['spent'])
->addLeft($data['left'])->addOverspent($data['overspent'])->addBudgetLine($budgetLine);
2016-01-27 13:54:14 -06:00
}
}
// stuff outside of budgets:
2016-05-18 00:01:27 -05:00
2016-05-18 07:19:33 -05:00
$noBudget = $this->repository->spentInPeriodWithoutBudget($accounts, $start, $end);
2016-01-27 13:54:14 -06:00
$budgetLine = new BudgetLine;
$budgetLine->setOverspent($noBudget)->setSpent($noBudget);
$object->addOverspent($noBudget)->addBudgetLine($budgetLine);
2016-01-27 13:54:14 -06:00
return $object;
}
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) {
2016-05-06 15:53:08 -05: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-01-27 13:54:14 -06:00
/**
* Take the array as returned by CategoryRepositoryInterface::spentPerDay and CategoryRepositoryInterface::earnedByDay
2016-01-27 13:54:14 -06:00
* and sum up everything in the array in the given range.
*
* @param Carbon $start
* @param Carbon $end
* @param array $array
*
* @return string
*/
protected function getSumOfRange(Carbon $start, Carbon $end, array $array)
{
$sum = '0';
$currentStart = clone $start; // to not mess with the original one
$currentEnd = clone $end; // to not mess with the original one
while ($currentStart <= $currentEnd) {
$date = $currentStart->format('Y-m-d');
if (isset($array[$date])) {
$sum = bcadd($sum, $array[$date]);
}
$currentStart->addDay();
}
return $sum;
}
2016-05-18 00:01:27 -05:00
/**
* @param Budget $budget
* @param LimitRepetition $repetition
* @param Collection $accounts
*
* @return array
*/
private function calculateExpenses(Budget $budget, LimitRepetition $repetition, Collection $accounts): array
{
$array = [];
$expenses = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $repetition->startdate, $repetition->enddate);
$array['left'] = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : '0';
$array['spent'] = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0';
$array['overspent'] = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $repetition->amount);
2016-05-18 07:19:33 -05:00
$array['expenses'] = $expenses;
2016-05-18 00:01:27 -05:00
return $array;
}
/**
* @param Carbon $current
* @param Carbon $end
*
* @return array
*/
private function createYearHeaders(Carbon $current, Carbon $end): array
{
$headers = [];
while ($current < $end) {
$short = $current->format('m-Y');
$headers[$short] = $current->formatLocalized((string)trans('config.month'));
$current->addMonth();
}
return $headers;
}
/**
* @param Carbon $current
* @param Carbon $end
* @param Budget $budget
* @param Collection $accounts
*
* @return array
*/
private function getBudgetSpentData(Carbon $current, Carbon $end, Budget $budget, Collection $accounts): array
{
$sum = '0';
$spent = [];
while ($current < $end) {
$currentEnd = clone $current;
$currentEnd->endOfMonth();
$format = $current->format('m-Y');
$budgetSpent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $current, $currentEnd);
$spent[$format] = $budgetSpent;
$sum = bcadd($sum, $budgetSpent);
$current->addMonth();
}
return [
'spent' => $spent,
'sum' => $sum,
];
}
}