Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Add two new “spentInPeriod” methods that use the collector and not big queries.
James Cole4241ae035e69
diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.phpindex b1d53731e7..0b14e18f91 100644--- a/app/Helpers/Report/BalanceReportHelper.php+++ b/app/Helpers/Report/BalanceReportHelper.php@@ -158,9 +158,7 @@ class BalanceReportHelper implements BalanceReportHelperInterfaceforeach ($accounts as $account) {$balanceEntry = new BalanceEntry;$balanceEntry->setAccount($account);- $spent = $this->budgetRepository->spentInPeriod(- new Collection([$budgetLimit->budget]), new Collection([$account]), $budgetLimit->start_date, $budgetLimit->end_date- );+ $spent = $this->budgetRepository->spentInPeriodCollector(new Collection([$budgetLimit->budget]), new Collection([$account]), $budgetLimit->start_date, $budgetLimit->end_date);$balanceEntry->setSpent($spent);$line->addBalanceEntry($balanceEntry);}diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.phpindex cf6836ce96..f5ad6285dd 100644--- a/app/Helpers/Report/BudgetReportHelper.php+++ b/app/Helpers/Report/BudgetReportHelper.php@@ -59,7 +59,7 @@ class BudgetReportHelper implements BudgetReportHelperInterfaceforeach ($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+ $spent = $this->repository->spentInPeriodCollector(new Collection([$budget]), $accounts, $start, $end);// spent for budget in time rangeif ($spent > 0) {$budgetLine = new BudgetLine;$budgetLine->setBudget($budget)->setOverspent($spent);@@ -104,7 +104,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface$set = new Collection;/** @var Budget $budget */foreach ($budgets as $budget) {- $total = $repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);+ $total = $repository->spentInPeriodCollector(new Collection([$budget]), $accounts, $start, $end);if (bccomp($total, '0') === -1) {$set->push($budget);}@@ -128,7 +128,7 @@ class BudgetReportHelper implements BudgetReportHelperInterfaceprivate function calculateExpenses(Budget $budget, BudgetLimit $budgetLimit, Collection $accounts): array{$array = [];- $expenses = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $budgetLimit->start_date, $budgetLimit->end_date);+ $expenses = $this->repository->spentInPeriodCollector(new Collection([$budget]), $accounts, $budgetLimit->start_date, $budgetLimit->end_date);$array['left'] = bccomp(bcadd($budgetLimit->amount, $expenses), '0') === 1 ? bcadd($budgetLimit->amount, $expenses) : '0';$array['spent'] = bccomp(bcadd($budgetLimit->amount, $expenses), '0') === 1 ? $expenses : '0';$array['overspent'] = bccomp(bcadd($budgetLimit->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $budgetLimit->amount);diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.phpindex fce86e46a1..b3261a066b 100644--- a/app/Http/Controllers/BudgetController.php+++ b/app/Http/Controllers/BudgetController.php@@ -268,7 +268,7 @@ class BudgetController extends Controller/** @var BudgetLimit $entry */foreach ($set as $entry) {- $entry->spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $entry->start_date, $entry->end_date);+ $entry->spent = $repository->spentInPeriodCollector(new Collection([$budget]), $accounts, $entry->start_date, $entry->end_date);$limits->push($entry);}@@ -313,7 +313,7 @@ class BudgetController extends Controller$journals->setPath('/budgets/show/' . $budget->id . '/' . $budgetLimit->id);- $budgetLimit->spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $budgetLimit->start_date, $budgetLimit->end_date);+ $budgetLimit->spent = $repository->spentInPeriodCollector(new Collection([$budget]), $accounts, $budgetLimit->start_date, $budgetLimit->end_date);$limits = new Collection([$budgetLimit]);return view('budgets.show', compact('limits', 'budget', 'budgetLimit', 'journals', 'subTitle'));@@ -405,7 +405,7 @@ class BudgetController extends Controllerforeach ($budgets as $budget) {$budgetId = $budget->id;$return[$budgetId] = [- 'spent' => $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end),+ 'spent' => $this->repository->spentInPeriodCollector(new Collection([$budget]), $accounts, $start, $end),'budgeted' => '0','currentRep' => false,];diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.phpindex 0a8fe543cf..f2dd6ea111 100644--- a/app/Http/Controllers/Chart/BudgetController.php+++ b/app/Http/Controllers/Chart/BudgetController.php@@ -95,7 +95,7 @@ class BudgetController extends Controller$currentEnd = Navigation::endOfPeriod($first, $range);// sub another day because reasons.$currentEnd->subDay();- $spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $currentStart, $currentEnd);+ $spent = $this->repository->spentInPeriodCollector($budgetCollection, new Collection, $currentStart, $currentEnd);$format = Navigation::periodShow($first, $range);$entries[$format] = bcmul($spent, '-1');$first = Navigation::addPeriod($first, $range, 0);@@ -140,7 +140,7 @@ class BudgetController extends Controller$amount = $budgetLimit->amount;$budgetCollection = new Collection([$budget]);while ($start <= $end) {- $spent = $this->repository->spentInPeriod($budgetCollection, new Collection, $start, $start);+ $spent = $this->repository->spentInPeriodCollector($budgetCollection, new Collection, $start, $start);$amount = bcadd($amount, $spent);$format = $start->formatLocalized(strval(trans('config.month_and_day')));$entries[$format] = $amount;@@ -329,7 +329,7 @@ class BudgetController extends Controller{$return = [];if ($limits->count() === 0) {- $spent = $this->repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);+ $spent = $this->repository->spentInPeriodCollector(new Collection([$budget]), new Collection, $start, $end);if (bccomp($spent, '0') !== 0) {$return[$budget->name]['spent'] = bcmul($spent, '-1');$return[$budget->name]['left'] = 0;@@ -375,7 +375,7 @@ class BudgetController extends Controller$name = $budget->name;/** @var BudgetLimit $budgetLimit */foreach ($limits as $budgetLimit) {- $expenses = $this->repository->spentInPeriod(new Collection([$budget]), new Collection, $budgetLimit->start_date, $budgetLimit->end_date);+ $expenses = $this->repository->spentInPeriodCollector(new Collection([$budget]), new Collection, $budgetLimit->start_date, $budgetLimit->end_date);if ($limits->count() > 1) {$name = $budget->name . ' ' . trans(diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.phpindex d272982654..ff3e0b069b 100644--- a/app/Repositories/Budget/BudgetRepository.php+++ b/app/Repositories/Budget/BudgetRepository.php@@ -291,7 +291,7 @@ class BudgetRepository implements BudgetRepositoryInterface});}- )->orderBy('budget_limits.start_date','DESC')->get(['budget_limits.*']);+ )->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);return $set;}@@ -521,6 +521,33 @@ class BudgetRepository implements BudgetRepositoryInterfacereturn bcadd($first, $second);}+ /**+ * @param Collection $budgets+ * @param Collection $accounts+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return string+ */+ public function spentInPeriodCollector(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string+ {+ /** @var JournalCollectorInterface $collector */+ $collector = app(JournalCollectorInterface::class, [$this->user]);+ $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setBudgets($budgets);++ if($accounts->count() > 0) {+ $collector->setAccounts($accounts);+ }+ if($accounts->count() === 0) {+ $collector->setAllAssetAccounts();+ }++ $set = $collector->getJournals();+ $sum = strval($set->sum('transaction_amount'));++ return $sum;+ }+/*** @param Collection $accounts* @param Carbon $start@@ -663,4 +690,40 @@ class BudgetRepository implements BudgetRepositoryInterfacereturn $limit;}++ /**+ * @param Collection $accounts+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return string+ */+ public function spentInPeriodWithoutBudgetCollector(Collection $accounts, Carbon $start, Carbon $end): string+ {+ /** @var JournalCollectorInterface $collector */+ $collector = app(JournalCollectorInterface::class, [$this->user]);+ $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();++ if ($accounts->count() > 0) {+ $collector->setAccounts($accounts);+ }+ if ($accounts->count() === 0) {+ $collector->setAllAssetAccounts();+ }++ $set = $collector->getJournals();+ $set = $set->filter(+ function (Transaction $transaction) {+ if (bccomp($transaction->transaction_amount, '0') === -1) {+ return $transaction;+ }++ return null;+ }+ );++ $sum = strval($set->sum('transaction_amount'));++ return $sum;+ }}diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.phpindex 6ccb7da0bf..4a0f453d11 100644--- a/app/Repositories/Budget/BudgetRepositoryInterface.php+++ b/app/Repositories/Budget/BudgetRepositoryInterface.php@@ -159,6 +159,16 @@ interface BudgetRepositoryInterface*/public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string;+ /**+ * @param Collection $budgets+ * @param Collection $accounts+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return string+ */+ public function spentInPeriodCollector(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string;+/*** @param Collection $accounts* @param Carbon $start@@ -168,6 +178,15 @@ interface BudgetRepositoryInterface*/public function spentInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): string;+ /**+ * @param Collection $accounts+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return string+ */+ public function spentInPeriodWithoutBudgetCollector(Collection $accounts, Carbon $start, Carbon $end): string;+/*** @param array $data*