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

163 lines
5.5 KiB
PHP
Raw Normal View History

2016-01-27 20:54:14 +01:00
<?php
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2016-01-27 20:54:14 +01:00
/**
* BudgetReportHelper.php
2016-04-01 16:44:46 +02:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-01-27 20:54:14 +01:00
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
use FireflyIII\Helpers\Collection\BudgetLine;
2016-04-24 20:00:20 +02:00
use FireflyIII\Models\Budget;
2016-01-27 20:54:14 +01:00
use FireflyIII\Models\LimitRepetition;
2016-05-06 06:15:46 +02:00
use FireflyIII\Models\TransactionJournal;
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
{
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return BudgetCollection
*/
2016-02-05 19:54:25 +01:00
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): BudgetCollection
2016-01-27 20:54:14 +01:00
{
$object = new BudgetCollection;
2016-05-02 20:49:19 +02:00
/** @var BudgetRepositoryInterface $repository */
2016-05-06 06:15:46 +02:00
$repository = app(BudgetRepositoryInterface::class);
$set = $repository->getBudgets();
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-05-06 06:15:46 +02:00
$repetitions = $budget->limitrepetitions()->before($end)->after($start)->get();
2016-01-27 20:54:14 +01:00
// no repetition(s) for this budget:
if ($repetitions->count() == 0) {
2016-05-06 06:15:46 +02:00
// spent for budget in time range:
$spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);
if ($spent > 0) {
$budgetLine = new BudgetLine;
$budgetLine->setBudget($budget);
$budgetLine->setOverspent($spent);
$object->addOverspent($spent);
$object->addBudgetLine($budgetLine);
}
2016-01-27 20:54:14 +01:00
continue;
}
// one or more repetitions for budget:
/** @var LimitRepetition $repetition */
foreach ($repetitions as $repetition) {
2016-05-06 06:15:46 +02:00
2016-01-27 20:54:14 +01:00
$budgetLine = new BudgetLine;
$budgetLine->setBudget($budget);
$budgetLine->setRepetition($repetition);
2016-05-15 19:23:19 +02:00
$expenses = $repository->spentInPeriod(new Collection([$budget]), $accounts, $repetition->startdate, $repetition->enddate);
2016-02-05 19:54:25 +01:00
$left = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : '0';
2016-01-27 20:54:14 +01:00
$spent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0';
$overspent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $repetition->amount);
$budgetLine->setLeft($left);
2016-01-30 09:41:48 +01:00
$budgetLine->setSpent($expenses);
2016-01-27 20:54:14 +01:00
$budgetLine->setOverspent($overspent);
$budgetLine->setBudgeted($repetition->amount);
$object->addBudgeted($repetition->amount);
$object->addSpent($spent);
$object->addLeft($left);
$object->addOverspent($overspent);
$object->addBudgetLine($budgetLine);
}
}
// stuff outside of budgets:
2016-05-06 06:15:46 +02:00
$outsideBudget = $repository->journalsInPeriodWithoutBudget($accounts, $start, $end);
$noBudget = '0';
/** @var TransactionJournal $journal */
foreach ($outsideBudget as $journal) {
$noBudget = bcadd($noBudget, TransactionJournal::amount($journal));
}
2016-01-27 20:54:14 +01:00
$budgetLine = new BudgetLine;
$budgetLine->setOverspent($noBudget);
$budgetLine->setSpent($noBudget);
$object->addOverspent($noBudget);
$object->addBudgetLine($budgetLine);
return $object;
}
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) {
2016-05-06 22:53:08 +02: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-01-27 20:54:14 +01:00
/**
* Take the array as returned by CategoryRepositoryInterface::spentPerDay and CategoryRepositoryInterface::earnedByDay
2016-01-27 20:54:14 +01: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;
}
}