Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Fix for #1111
James Cole36354c384686
diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.phpindex 1459349260..b445ca7adc 100644--- a/app/Http/Controllers/BudgetController.php+++ b/app/Http/Controllers/BudgetController.php@@ -82,9 +82,9 @@ class BudgetController extends Controller*/public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget){- $amount = strval($request->get('amount'));- $start = Carbon::createFromFormat('Y-m-d', $request->get('start'));- $end = Carbon::createFromFormat('Y-m-d', $request->get('end'));+ $amount = strval($request->get('amount'));+ $start = Carbon::createFromFormat('Y-m-d', $request->get('start'));+ $end = Carbon::createFromFormat('Y-m-d', $request->get('end'));$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);if (0 === bccomp($amount, '0')) {$budgetLimit = null;@@ -421,7 +421,7 @@ class BudgetController extends Controller$defaultCurrency = app('amount')->getDefaultCurrency();$amount = $request->get('amount');$page = $request->integer('page') === 0 ? 1 : $request->integer('page');-+ $this->repository->cleanupBudgets();$this->repository->setAvailableBudget($defaultCurrency, $start, $end, $amount);Preferences::mark();@@ -504,7 +504,7 @@ class BudgetController extends Controller{$data = $request->getBudgetData();$budget = $this->repository->store($data);-+ $this->repository->cleanupBudgets();$request->session()->flash('success', strval(trans('firefly.stored_new_budget', ['name' => $budget->name])));Preferences::mark();@@ -531,6 +531,7 @@ class BudgetController extends Controller$this->repository->update($budget, $data);$request->session()->flash('success', strval(trans('firefly.updated_budget', ['name' => $budget->name])));+ $this->repository->cleanupBudgets();Preferences::mark();if (1 === intval($request->get('return_to_edit'))) {diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.phpindex e5c8848596..473ff7e234 100644--- a/app/Repositories/Budget/BudgetRepository.php+++ b/app/Repositories/Budget/BudgetRepository.php@@ -60,19 +60,17 @@ class BudgetRepository implements BudgetRepositoryInterface// delete limits with amount 0:BudgetLimit::where('amount', 0)->delete();- // clean up:- $set = BudgetLimit::groupBy(['budget_id', 'start_date', 'end_date'])- ->get(['budget_id', 'start_date', 'end_date', DB::raw('COUNT(*) as ct')]);- foreach ($set as $entry) {- if ($entry->ct > 1) {- $newest = BudgetLimit::where('start_date', $entry->start_date)->where('end_date', $entry->end_date)- ->where('budget_id', $entry->budget_id)->orderBy('updated_at', 'DESC')->first(['budget_limits.*']);- if (!is_null($newest)) {- BudgetLimit::where('start_date', $entry->start_date)->where('end_date', $entry->end_date)- ->where('budget_id', $entry->budget_id)- ->where('id', '!=', $newest->id)->delete();- }+ // do the clean up by hand because Sqlite can be tricky with this.+ $budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']);+ $count = [];+ /** @var BudgetLimit $budgetLimit */+ foreach ($budgetLimits as $budgetLimit) {+ $key = $budgetLimit->budget_id . '-' . $budgetLimit->start_date->format('Y-m-d') . $budgetLimit->end_date->format('Y-m-d');+ if (isset($count[$key])) {+ // delete it!+ BudgetLimit::find($budgetLimit->id)->delete();}+ $count[$key] = true;}return true;@@ -629,6 +627,7 @@ class BudgetRepository implements BudgetRepositoryInterface*/public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): BudgetLimit{+ $this->cleanupBudgets();// count the limits:$limits = $budget->budgetlimits()->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))