From c4e46bf89b1f5252901a76f767ac246e0f11ff25 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 1 Oct 2022 16:35:29 +0200 Subject: [PATCH] Fix issue in budget limits --- .../Budget/BudgetLimitController.php | 27 ++++++++++++++----- public/v1/js/ff/budgets/index.js | 6 +++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Budget/BudgetLimitController.php b/app/Http/Controllers/Budget/BudgetLimitController.php index 34c1de175c..200d7d77b0 100644 --- a/app/Http/Controllers/Budget/BudgetLimitController.php +++ b/app/Http/Controllers/Budget/BudgetLimitController.php @@ -151,10 +151,14 @@ class BudgetLimitController extends Controller // sanity check on amount: if ((float) $amount === 0.0) { - $amount = '1'; + if (null !== $limit) { + $this->blRepository->destroyBudgetLimit($limit); + } + // return empty=ish array: + return response()->json([]); } - if ((int) $amount > 65536) { - $amount = '65536'; + if ((int) $amount > 16777216) { + $amount = '16777216'; } if (null !== $limit) { @@ -175,7 +179,7 @@ class BudgetLimitController extends Controller if ($request->expectsJson()) { $array = $limit->toArray(); - // add some extra meta data: + // add some extra metadata: $spentArr = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection([$budget]), $currency); $array['spent'] = $spentArr[$currency->id]['sum'] ?? '0'; $array['left_formatted'] = app('amount')->formatAnything($limit->transactionCurrency, bcadd($array['spent'], $array['amount'])); @@ -208,10 +212,19 @@ class BudgetLimitController extends Controller // sanity check on amount: if ((float) $amount === 0.0) { - $amount = '1'; + $budgetId = $budgetLimit->budget_id; + $currency = $budgetLimit->transactionCurrency; + $this->blRepository->destroyBudgetLimit($budgetLimit); + $array = [ + 'budget_id' => $budgetId, + 'left_formatted' => app('amount')->formatAnything($currency, '0'), + 'left_per_day_formatted' => app('amount')->formatAnything($currency, '0'), + 'transaction_currency_id' => $currency->id, + ]; + return response()->json($array); } - if ((int) $amount > 65536) { - $amount = '65536'; + if ((int) $amount > 16777216) { // 16 million + $amount = '16777216'; } $limit = $this->blRepository->update($budgetLimit, ['amount' => $amount]); diff --git a/public/v1/js/ff/budgets/index.js b/public/v1/js/ff/budgets/index.js index 1e48156aa6..e37ae4c9ca 100644 --- a/public/v1/js/ff/budgets/index.js +++ b/public/v1/js/ff/budgets/index.js @@ -79,6 +79,7 @@ $(function () { }); function updateBudgetedAmount(e) { + console.log('updateBudgetedAmount'); var input = $(e.currentTarget); var budgetId = parseInt(input.data('id')); var budgetLimitId = parseInt(input.data('limit')); @@ -95,7 +96,7 @@ function updateBudgetedAmount(e) { }).done(function (data) { input.prop('disabled', false); - + input.data('limit', data.id); // update amount left. $('.left_span[data-limit="0"][data-id="' + budgetId + '"]').html(data.left_formatted); if (data.left_per_day > 0) { @@ -113,6 +114,7 @@ function updateBudgetedAmount(e) { amount: input.val(), }).done(function (data) { input.prop('disabled', false); + input.data('limit', data.id); $('.left_span[data-limit="' + budgetLimitId + '"]').html(data.left_formatted); if (data.left_per_day > 0) { $('.left_span[data-limit="' + budgetLimitId + '"]').html(data.left_formatted + '(' + data.left_per_day_formatted + ')'); @@ -214,7 +216,7 @@ function deleteBudgetLimit(e) { var url = deleteBudgetLimitUrl.replace('REPLACEME', budgetLimitId.toString()); $.post(url, {_token: token}).then(function () { $('.bl_entry[data-budget-limit-id="' + budgetLimitId + '"]').remove(); - + }); return false; }