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

150 lines
5.0 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
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
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;
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-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
2016-06-16 13:52:30 -05:00
/**
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-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;
}
}