2016-01-27 13:54:14 -06:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2016-01-27 13:54:14 -06:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
2016-02-04 01:53:56 -06:00
|
|
|
|
|
|
|
if ($spent > 0) {
|
|
|
|
$budgetLine = new BudgetLine;
|
|
|
|
$budgetLine->setBudget($budget);
|
|
|
|
$budgetLine->setOverspent($spent);
|
|
|
|
$object->addOverspent($spent);
|
|
|
|
$object->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);
|
|
|
|
$budgetLine->setRepetition($repetition);
|
2016-05-18 00:01:27 -05:00
|
|
|
$budgetLine->setLeft($data['left']);
|
|
|
|
$budgetLine->setSpent($data['expenses']);
|
|
|
|
$budgetLine->setOverspent($data['overspent']);
|
2016-01-27 13:54:14 -06:00
|
|
|
$budgetLine->setBudgeted($repetition->amount);
|
|
|
|
|
|
|
|
$object->addBudgeted($repetition->amount);
|
2016-05-18 00:01:27 -05:00
|
|
|
$object->addSpent($data['spent']);
|
|
|
|
$object->addLeft($data['left']);
|
|
|
|
$object->addOverspent($data['overspent']);
|
2016-01-27 13:54:14 -06:00
|
|
|
$object->addBudgetLine($budgetLine);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
$budgetLine->setSpent($noBudget);
|
|
|
|
$object->addOverspent($noBudget);
|
|
|
|
$object->addBudgetLine($budgetLine);
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
2016-05-08 06:45:23 -05: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;
|
|
|
|
|
|
|
|
}
|
2016-01-28 14:50:20 -06:00
|
|
|
}
|