mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Clean up budget report partial
This commit is contained in:
@@ -15,8 +15,6 @@ namespace FireflyIII\Helpers\Report;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
|
||||
use FireflyIII\Helpers\Collection\BudgetLine;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
@@ -48,44 +46,63 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return BudgetCollection
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): BudgetCollection
|
||||
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): array
|
||||
{
|
||||
$object = new BudgetCollection;
|
||||
$set = $this->repository->getBudgets();
|
||||
$set = $this->repository->getBudgets();
|
||||
$array = [];
|
||||
|
||||
/** @var Budget $budget */
|
||||
foreach ($set as $budget) {
|
||||
$budgetLimits = $this->repository->getBudgetLimits($budget, $start, $end);
|
||||
if ($budgetLimits->count() == 0) { // no budget limit(s) for this budget
|
||||
|
||||
$spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);// spent for budget in time range
|
||||
if ($spent > 0) {
|
||||
$budgetLine = new BudgetLine;
|
||||
$budgetLine->setBudget($budget)->setOverspent($spent);
|
||||
$object->addOverspent($spent)->addBudgetLine($budgetLine);
|
||||
if (bccomp($spent, '0') === -1) {
|
||||
$line = [
|
||||
'type' => 'budget',
|
||||
'id' => $budget->id,
|
||||
'name' => $budget->name,
|
||||
'budgeted' => '0',
|
||||
'spent' => $spent,
|
||||
'left' => '0',
|
||||
'overspent' => '0',
|
||||
];
|
||||
$array[] = $line;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
/** @var BudgetLimit $budgetLimit */
|
||||
foreach ($budgetLimits as $budgetLimit) { // one or more repetitions for budget
|
||||
$data = $this->calculateExpenses($budget, $budgetLimit, $accounts);
|
||||
$budgetLine = new BudgetLine;
|
||||
$budgetLine->setBudget($budget)->setBudgetLimit($budgetLimit)
|
||||
->setLeft($data['left'])->setSpent($data['expenses'])->setOverspent($data['overspent'])
|
||||
->setBudgeted(strval($budgetLimit->amount));
|
||||
|
||||
$object->addBudgeted(strval($budgetLimit->amount))->addSpent($data['spent'])
|
||||
->addLeft($data['left'])->addOverspent($data['overspent'])->addBudgetLine($budgetLine);
|
||||
$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,
|
||||
|
||||
'budgeted' => strval($budgetLimit->amount),
|
||||
'spent' => $data['expenses'],
|
||||
'left' => $data['left'],
|
||||
'overspent' => $data['overspent'],
|
||||
];
|
||||
$array[] = $line;
|
||||
}
|
||||
}
|
||||
$noBudget = $this->repository->spentInPeriodWoBudget($accounts, $start, $end); // stuff outside of budgets
|
||||
$budgetLine = new BudgetLine;
|
||||
$budgetLine->setOverspent($noBudget)->setSpent($noBudget);
|
||||
$object->addOverspent($noBudget)->addBudgetLine($budgetLine);
|
||||
$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;
|
||||
|
||||
return $object;
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user