Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Code for #1356
James Cole8f3e4a2dee19
diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.phpindex ebe0ca12e3..f82fa49e95 100644--- a/app/Http/Controllers/BudgetController.php+++ b/app/Http/Controllers/BudgetController.php@@ -87,16 +87,31 @@ class BudgetController extends Controller$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);$largeDiff = false;$warnText = '';- $average = '0';- $current = '0';if (0 === bccomp($amount, '0')) {$budgetLimit = null;}+ // if today is between start and end, use the diff in days between end and today (days left)+ // otherwise, use diff between start and end.+ $today = new Carbon;+ if ($today->gte($start) && $today->lte($end)) {+ $days = $end->diffInDays($today);+ }+ if ($today->lte($start) || $today->gte($end)) {+ $days = $start->diffInDays($end);+ }+ $days = $days === 0 ? 1 : $days;+// calculate left in budget:- $spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);- $currency = app('amount')->getDefaultCurrency();- $left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);+ $spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);+ $currency = app('amount')->getDefaultCurrency();+ $left = app('amount')->formatAnything($currency, bcadd($amount, $spent), true);+ $leftPerDay = 'none';++ // is user has money left, calculate.+ if (1 === bccomp(bcadd($amount, $spent), '0')) {+ $leftPerDay = app('amount')->formatAnything($currency, bcdiv(bcadd($amount, $spent), (string)$days), true);+ }// over or under budgeting, compared to previous budgets?@@ -112,8 +127,8 @@ class BudgetController extends Controller$warnText = (string)trans('firefly.over_budget_warn',[- 'amount' => app('amount')->formatAnything($currency, $average, false),- 'over_amount' => app('amount')->formatAnything($currency, $current, false),+ 'amount' => app('amount')->formatAnything($currency, $average, false),+ 'over_amount' => app('amount')->formatAnything($currency, $current, false),]);}@@ -122,14 +137,15 @@ class BudgetController extends Controllerreturn response()->json([- 'left' => $left,- 'name' => $budget->name,- 'limit' => $budgetLimit ? $budgetLimit->id : 0,- 'amount' => $amount,- 'current' => $current,- 'average' => $average,- 'large_diff' => $largeDiff,- 'warn_text' => $warnText,+ 'left' => $left,+ 'name' => $budget->name,+ 'limit' => $budgetLimit ? $budgetLimit->id : 0,+ 'amount' => $amount,+ 'current' => $current,+ 'average' => $average,+ 'large_diff' => $largeDiff,+ 'left_per_day' => $leftPerDay,+ 'warn_text' => $warnText,]);@@ -219,6 +235,17 @@ class BudgetController extends Controller$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');$pageSize = (int)Preferences::get('listPageSize', 50)->data;+ // if today is between start and end, use the diff in days between end and today (days left)+ // otherwise, use diff between start and end.+ $today = new Carbon;+ if ($today->gte($start) && $today->lte($end)) {+ $days = $end->diffInDays($today);+ }+ if ($today->lte($start) || $today->gte($end)) {+ $days = $start->diffInDays($end);+ }+ $days = $days === 0 ? 1 : $days;+// make date if present:if (null !== $moment || '' !== (string)$moment) {try {@@ -283,27 +310,11 @@ class BudgetController extends Controller$prevText = app('navigation')->periodShow($prev, $range);return view(- 'budgets.index',- compact(- 'available',- 'currentMonth',- 'next',- 'nextText',- 'prev', 'allBudgets',- 'prevText',- 'periodStart',- 'periodEnd',- 'page',- 'budgetInformation',- 'inactive',- 'budgets',- 'spent',- 'budgeted',- 'previousLoop',- 'nextLoop',- 'start',- 'end'- )+ 'budgets.index', compact(+ 'available', 'currentMonth', 'next', 'nextText', 'prev', 'allBudgets', 'prevText', 'periodStart', 'periodEnd', 'days', 'page',+ 'budgetInformation',+ 'inactive', 'budgets', 'spent', 'budgeted', 'previousLoop', 'nextLoop', 'start', 'end'+ ));}diff --git a/public/js/ff/budgets/index.js b/public/js/ff/budgets/index.jsindex 736ba8ae67..8651b947ae 100644--- a/public/js/ff/budgets/index.js+++ b/public/js/ff/budgets/index.js@@ -129,7 +129,13 @@ function updateBudgetedAmounts(e) {budgeted = budgeted + difference;// fill in "left" value:- leftCell.html(data.left);+++ if (data.left_per_day !== 'none') {+ leftCell.html(data.left + '(' + data.left_per_day + ')');+ } else {+ leftCell.html(data.left);+ }// update "budgeted" input:target.val(data.amount);diff --git a/resources/views/budgets/index.twig b/resources/views/budgets/index.twigindex 24212b52de..71dad53b7e 100644--- a/resources/views/budgets/index.twig+++ b/resources/views/budgets/index.twig@@ -146,8 +146,8 @@<th data-defaultsort="disabled" class="hidden-sm hidden-xs" style="width:10%;"> </th><th data-defaultsign="az">{{ 'budget'|_ }}</th><th data-defaultsign="_19" style="width:25%;">{{ 'budgeted'|_ }}</th>- <th data-defaultsign="_19" class="hidden-sm hidden-xs">{{ 'spent'|_ }}</th>- <th data-defaultsign="_19">{{ 'left'|_ }}</th>+ <th data-defaultsign="_19" class="hidden-sm hidden-xs">{{ 'spent'|_ }} ({{ 'per_day'|_|lower }})</th>+ <th data-defaultsign="_19">{{ 'left'|_ }} ({{ 'per_day'|_|lower }})</th></tr></thead><tbody>@@ -188,10 +188,14 @@<td class="hidden-sm hidden-xs spent" data-id="{{ budget.id }}" data-spent="{{ budgetInformation[budget.id]['spent'] }}"data-value="{{ budgetInformation[budget.id]['spent'] }}">{{ budgetInformation[budget.id]['spent']|formatAmount }}+ ({{ (budgetInformation[budget.id]['spent'] / days)|formatAmount }})</td><td class="left" data-id="{{ budget.id }}"data-value="{{ (repAmount + budgetInformation[budget.id]['spent']) }}">{{ (repAmount + budgetInformation[budget.id]['spent'])|formatAmount }}+ {% if repAmount + budgetInformation[budget.id]['spent'] > 0 %}+ ({{ ((repAmount + budgetInformation[budget.id]['spent']) / days)|formatAmount }})+ {% endif %}</td></tr>{% endfor %}