Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Clean up budget amounts.
James Cole2842432204da
diff --git a/app/Http/Controllers/Budget/BudgetLimitController.php b/app/Http/Controllers/Budget/BudgetLimitController.phpindex 8eebf7795c..20072cde67 100644--- a/app/Http/Controllers/Budget/BudgetLimitController.php+++ b/app/Http/Controllers/Budget/BudgetLimitController.php@@ -46,6 +46,9 @@ use Illuminate\Routing\Redirector;use Illuminate\Support\Collection;use Illuminate\Support\Facades\Log;use Illuminate\View\View;+use Spatie\Period\Boundaries;+use Spatie\Period\Period;+use Spatie\Period\Precision;/*** Class BudgetLimitController@@ -196,13 +199,7 @@ class BudgetLimitController extends Controllerif ($request->expectsJson()) {$array = $limit->toArray();// add some extra metadata:- $spentArr = $this->opsRepository->sumExpenses(- $limit->start_date,- $limit->end_date,- null,- new Collection()->push($budget),- $currency- );+ $spentArr = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection()->push($budget), $currency);$array['spent'] = $spentArr[$currency->id]['sum'] ?? '0';$array['left_formatted'] = Amount::formatAnything($limit->transactionCurrency, bcadd($array['spent'], (string) $array['amount']));$array['amount_formatted'] = Amount::formatAnything($limit->transactionCurrency, $limit['amount']);@@ -268,10 +265,16 @@ class BudgetLimitController extends Controller$budgetLimit->transactionCurrency);$daysLeft = $this->activeDaysLeft($limit->start_date, $limit->end_date);++ $limitPeriod = Period::make($limit->start_date, $limit->end_date, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());+ $inPast = $limitPeriod->startsBefore(now()) && $limitPeriod->endsBefore(now());++ // create aray.$array['spent'] = $spentArr[$budgetLimit->transactionCurrency->id]['sum'] ?? '0';$array['left_formatted'] = Amount::formatAnything($limit->transactionCurrency, bcadd($array['spent'], (string) $array['amount']));$array['amount_formatted'] = Amount::formatAnything($limit->transactionCurrency, $limit['amount']);$array['days_left'] = (string) $daysLeft;+ $array['in_past'] = $inPast;$array['left_per_day'] = 0 === $daysLeft? bcadd((string) $array['spent'], (string) $array['amount']): bcdiv(bcadd((string) $array['spent'], (string) $array['amount']), $array['days_left']);diff --git a/app/Http/Controllers/Budget/IndexController.php b/app/Http/Controllers/Budget/IndexController.phpindex ef43c56848..17fe470257 100644--- a/app/Http/Controllers/Budget/IndexController.php+++ b/app/Http/Controllers/Budget/IndexController.php@@ -47,6 +47,9 @@ use Illuminate\Http\Request;use Illuminate\Support\Collection;use Illuminate\Support\Facades\Log;use Illuminate\View\View;+use Spatie\Period\Boundaries;+use Spatie\Period\Period;+use Spatie\Period\Precision;/*** Class IndexController@@ -117,6 +120,10 @@ final class IndexController extends Controller$prevLoop = $this->getPreviousPeriods($start, $range);$nextLoop = $this->getNextPeriods($start, $range);+ // number of days for consistent budgeting.+ $activeDaysPassed = $this->activeDaysPassed($start, $end); // see method description.+ $activeDaysLeft = $this->activeDaysLeft($start, $end); // see method description.+// get all available budgets:$availableBudgets = $this->getAllAvailableBudgets($start, $end);// get all active budgets:@@ -133,9 +140,6 @@ final class IndexController extends Controller$spent = $spentArr[$this->primaryCurrency->id]['sum'] ?? '0';unset($spentArr);}- // number of days for consistent budgeting.- $activeDaysPassed = $this->activeDaysPassed($start, $end); // see method description.- $activeDaysLeft = $this->activeDaysLeft($start, $end); // see method description.// get all inactive budgets, and simply list them:$inactive = $this->repository->getInactiveBudgets();@@ -221,44 +225,65 @@ final class IndexController extends Controller// complement budget with budget limits in range, and expenses in currency X in range./** @var Budget $current */- foreach ($collection as $current) {- Log::debug(sprintf('Working on budget #%d ("%s")', $current->id, $current->name));- $array = $current->toArray();+ foreach ($collection as $budget) {+ Log::debug(sprintf('Working on budget #%d ("%s")', $budget->id, $budget->name));+ $array = $budget->toArray();$array['spent'] = [];- $array['spent_total'] = [];$array['budgeted'] = [];- $array['attachments'] = $this->repository->getAttachments($current);- $array['auto_budget'] = $this->repository->getAutoBudget($current);- $budgetLimits = $this->blRepository->getBudgetLimits($current, $start, $end);+ $array['attachments'] = $this->repository->getAttachments($budget);+ $array['auto_budget'] = $this->repository->getAutoBudget($budget);+ $budgetLimits = $this->blRepository->getBudgetLimits($budget, $start, $end);+ $spentInLimits = [];/** @var BudgetLimit $limit */foreach ($budgetLimits as $limit) {Log::debug(sprintf('Working on budget limit #%d', $limit->id));- $currency = $limit->transactionCurrency ?? $primaryCurrency;- $amount = Steam::bcround($limit->amount, $currency->decimal_places);- $array['budgeted'][] = [+ // number of days for consistent budgeting.+ $activeDaysPassed = $this->activeDaysPassed($limit->start_date, $limit->end_date); // see method description.+ $activeDaysLeft = $this->activeDaysLeft($limit->start_date, $limit->end_date); // see method description.+ $limitPeriod = Period::make($limit->start_date, $limit->end_date, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());+ $inPast = $limitPeriod->startsBefore(now()) && $limitPeriod->endsBefore(now());+ $currency = $limit->transactionCurrency ?? $primaryCurrency;+ $amount = Steam::bcround($limit->amount, $currency->decimal_places);+ $spent = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, null, new Collection()->push($budget), $currency);+ $spentAmount = $spent[$currency->id]['sum'] ?? '0';+ $array['budgeted'][] = ['id' => $limit->id,'amount' => $amount,'notes' => $this->blRepository->getNoteText($limit),'start_date' => $limit->start_date->isoFormat($this->monthAndDayFormat),'end_date' => $limit->end_date->isoFormat($this->monthAndDayFormat),'in_range' => $limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end),+ 'in_past' => $inPast,'total_days' => $limit->start_date->diffInDays($limit->end_date) + 1,'currency_id' => $currency->id,'currency_symbol' => $currency->symbol,'currency_name' => $currency->name,'currency_decimal_places' => $currency->decimal_places,+ 'spent' => $spentAmount,+ 'left' => bcadd($amount, $spentAmount),+ 'active_days_passed' => $activeDaysPassed,+ 'active_days_left' => $activeDaysLeft,];+ $spentInLimits[$currency->id] = array_key_exists($currency->id, $spentInLimits)+ ? bcadd($spentInLimits[$currency->id], $spentAmount)+ : $spentAmount;Log::debug(sprintf('The amount budgeted for budget limit #%d is %s %s', $limit->id, $currency->code, $amount));+ Log::debug(sprintf('spentInLimits[%s] is now %s', $currency->code, $spentInLimits[$currency->id]));}-// #10463+ Log::debug('Looping currencies');/** @var TransactionCurrency $currency */foreach ($currencies as $currency) {- $spentArr = $this->opsRepository->sumExpenses($start, $end, null, new Collection()->push($current), $currency);+ $spentInLimits[$currency->id] = array_key_exists($currency->id, $spentInLimits) ? $spentInLimits[$currency->id] : '0';+ $spentArr = $this->opsRepository->sumExpenses($start, $end, null, new Collection()->push($budget), $currency);++ Log::debug(sprintf('Working on currency %s, spentInLimits is %s', $currency->code, $spentInLimits[$currency->id]));+if (array_key_exists($currency->id, $spentArr) && array_key_exists('sum', $spentArr[$currency->id])) {$array['spent'][$currency->id]['spent'] = $spentArr[$currency->id]['sum'];+ $array['spent'][$currency->id]['spent_outside'] = bcmul(bcsub($spentInLimits[$currency->id], $spentArr[$currency->id]['sum']), '-1');$array['spent'][$currency->id]['currency_id'] = $currency->id;$array['spent'][$currency->id]['currency_symbol'] = $currency->symbol;$array['spent'][$currency->id]['currency_decimal_places'] = $currency->decimal_places;diff --git a/public/v1/js/ff/budgets/index.js b/public/v1/js/ff/budgets/index.jsindex 63444d096b..f597c0c9ce 100644--- a/public/v1/js/ff/budgets/index.js+++ b/public/v1/js/ff/budgets/index.js@@ -100,7 +100,7 @@ function updateBudgetedAmount(e) {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) {+ if (data.left_per_day > 0 && !data.in_past) {$('.left_span[data-limit="0"][data-id="' + budgetId + '"]').html(data.left_formatted + '(' + data.left_per_day_formatted + ')');}// update budgeted amount@@ -117,7 +117,7 @@ function updateBudgetedAmount(e) {input.prop('disabled', false);input.data('limit', data.id);$('.left_span[data-limit="' + budgetLimitId + '"]').html(data.left_formatted);- if (data.left_per_day > 0) {+ if (data.left_per_day > 0 && !data.in_past) {$('.left_span[data-limit="' + budgetLimitId + '"]').html(data.left_formatted + '(' + data.left_per_day_formatted + ')');}updateTotalBudgetedAmount(data.transaction_currency_id);diff --git a/resources/views/budgets/index.twig b/resources/views/budgets/index.twigindex b80d34798d..215665e884 100644--- a/resources/views/budgets/index.twig+++ b/resources/views/budgets/index.twig@@ -324,89 +324,11 @@{% endif %}</td><td class="hidden-sm hidden-xs spent" data-id="{{ budget.id }}" style="text-align:right;">-- {% for spentInfo in budget.spent %}- {{ formatAmountBySymbol(spentInfo.spent, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }}- {% if 0 == activeDaysPassed %}- ({{ formatAmountBySymbol(spentInfo.spent, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})- {% else %}- ({{ formatAmountBySymbol(spentInfo.spent / activeDaysPassed, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})- {% endif %}- <br/>- {% endfor %}- {% for budgetLimit in budget.budgeted %}- {% if null == budget.spent[budgetLimit.currency_id] %}- {{ formatAmountBySymbol(0, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}<br/>- {% endif %}- {% endfor %}+ {% include('budgets.partials.amount-spent') %}</td>{# this cell displays the amount left in the budget, per budget limit. #}<td class="left" data-id="{{ budget.id }}" style="text-align: right;">- {% for spentInfo in budget.spent %}- {% set countLimit = 0 %}- {% for budgetLimit in budget.budgeted %}- {# now looping a single budget limit. #}- {% if spentInfo.currency_id == budgetLimit.currency_id and budgetLimit.in_range %}- {# the code below is used for budget limits INSIDE the current view range. #}- {% set countLimit = countLimit + 1 %}-- <span class="left_span" data-currency="{{ spentInfo.currency_id }}" data-limit="{{ budgetLimit.id }}"- data-value="{{ spentInfo.spent + budgetLimit.amount }}" class="amount_left">- {# the amount left is automatically calculated. #}- {{ formatAmountBySymbol(spentInfo.spent + budgetLimit.amount, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }}- {% if spentInfo.spent + budgetLimit.amount > 0 %}- {% if 0 == activeDaysLeft %}- ({{ formatAmountBySymbol(spentInfo.spent + budgetLimit.amount, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})- {% else %}- ({{ formatAmountBySymbol((spentInfo.spent + budgetLimit.amount) / activeDaysLeft, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})- {% endif %}- {% else %}- ({{ formatAmountBySymbol(0, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})- {% endif %}- </span>- <br/>- {% endif %}- {% if spentInfo.currency_id == budgetLimit.currency_id and not budgetLimit.in_range and 0.0 == budgetLimit.total_days %}- <span class="left_span" data-currency="{{ spentInfo.currency_id }}" data-limit="{{ budgetLimit.id }}"- data-value="{{ spentInfo.spent + budgetLimit.amount }}" class="amount_left">- {{ formatAmountBySymbol(spentInfo.spent + budgetLimit.amount, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }}- </span>- <span class="text-muted">({{ 'unknown'|_ }})</span>- {% endif %}- {% if spentInfo.currency_id == budgetLimit.currency_id and not budgetLimit.in_range and 0.0 != budgetLimit.total_days %}- <span class="left_span" data-currency="{{ spentInfo.currency_id }}" data-limit="{{ budgetLimit.id }}"- data-value="{{ spentInfo.spent + budgetLimit.amount }}" class="amount_left">- {{ formatAmountBySymbol(spentInfo.spent + budgetLimit.amount, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }}- </span>- ({{ formatAmountBySymbol((spentInfo.spent + budgetLimit.amount) / budgetLimit.total_days, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})- {% endif %}-- {% endfor %}-- {% if countLimit == 0 %}- {# display nothing #}- {% endif %}- {% endfor %}- {% for budgetLimit in budget.budgeted %}- {% if null == budget.spent[budgetLimit.currency_id] %}- <span class="left_span" data-currency="{{ spentInfo.currency_id }}" data-limit="{{ budgetLimit.id }}"- data-value="{{ spentInfo.spent + budgetLimit.amount }}" class="amount_left">- {{ formatAmountBySymbol(budgetLimit.amount, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}- {% if budgetLimit.in_range %}- {% if 0 == activeDaysLeft %}- ({{ formatAmountBySymbol(budgetLimit.amount, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})- {% else %}- ({{ formatAmountBySymbol(budgetLimit.amount / activeDaysLeft, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})- {% endif %}- {% endif %}- {% if not budgetLimit.in_range %}- {# For issue #10441, add per day if the budget limit is out of range. #}- ({{ formatAmountBySymbol(budgetLimit.amount / budgetLimit.total_days, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})- {% endif %}- </span>- <br/>- {% endif %}- {% endfor %}+ {% include('budgets.partials.amount-left') %}</td></tr>{% endfor %}diff --git a/resources/views/budgets/partials/amount-left.twig b/resources/views/budgets/partials/amount-left.twignew file mode 100644index 0000000000..e72d89ec61--- /dev/null+++ b/resources/views/budgets/partials/amount-left.twig@@ -0,0 +1,87 @@+{# The amount left can only be shown for actual budget limits. #}+{% for budgetLimit in budget.budgeted %}+ <span class="left_span" data-currency="{{ budgetLimit.currency_id }}" data-limit="{{ budgetLimit.id }}" data-value="{{ budgetLimit.left }}" class="amount_left">+ {{ formatAmountBySymbol(budgetLimit.left, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}+ {% if not budgetLimit.in_past %}+ {% if 0 == budgetLimit.active_days_left %}+ ({{ formatAmountBySymbol(budgetLimit.left, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})+ {% else %}+ ({{ formatAmountBySymbol(budgetLimit.left / budgetLimit.active_days_left, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})+ {% endif %}+ {% endif %}+</span><br />+{% endfor %}+++{#+{% for spentInfo in budget.spent %}+ {% set countLimit = 0 %}+ <!-- loop each budget limit collected for this budget in this period. -->+ {% for budgetLimit in budget.budgeted %}+ <!-- now looping a single budget limit. -->+ {% if spentInfo.currency_id == budgetLimit.currency_id and budgetLimit.in_range %}+ <!-- the code below is used for budget limits INSIDE the current view range. -->+ {% set countLimit = countLimit + 1 %}++ <span class="left_span" data-currency="{{ spentInfo.currency_id }}" data-limit="{{ budgetLimit.id }}"+ data-value="{{ spentInfo.spent + budgetLimit.amount }}" class="amount_left">+ <!--the amount left is automatically calculated. -->+ {{ formatAmountBySymbol(spentInfo.spent + budgetLimit.amount, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }}+ {% if spentInfo.spent + budgetLimit.amount > 0 %}+ {% if 0 == activeDaysLeft %}+ ({{ formatAmountBySymbol(spentInfo.spent + budgetLimit.amount, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})+ {% else %}+ ({{ formatAmountBySymbol((spentInfo.spent + budgetLimit.amount) / activeDaysLeft, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})+ {% endif %}+ {% else %}+ ({{ formatAmountBySymbol(0, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})+ {% endif %}+ </span>+ <br/>+ {% endif %}++ {% if spentInfo.currency_id == budgetLimit.currency_id and not budgetLimit.in_range and 0.0 == budgetLimit.total_days %}+ <span class="left_span" data-currency="{{ spentInfo.currency_id }}" data-limit="{{ budgetLimit.id }}"+ data-value="{{ spentInfo.spent + budgetLimit.amount }}" class="amount_left">+ {{ formatAmountBySymbol(spentInfo.spent + budgetLimit.amount, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }}+ </span>+ <span class="text-muted">({{ 'unknown'|_ }})</span>+ {% endif %}+ {% if spentInfo.currency_id == budgetLimit.currency_id and not budgetLimit.in_range and 0.0 != budgetLimit.total_days %}++ <span class="left_span" data-currency="{{ spentInfo.currency_id }}" data-limit="{{ budgetLimit.id }}"+ data-value="{{ spentInfo.spent + budgetLimit.amount }}" class="amount_left">+ {{ formatAmountBySymbol(spentInfo.spent + budgetLimit.amount, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }}+ </span>+ ({{ formatAmountBySymbol((spentInfo.spent + budgetLimit.amount) / budgetLimit.total_days, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})+ {% endif %}++ {% endfor %}++ {% if countLimit == 0 %}+ <!-- display nothing -->+ {% endif %}+ -->+{% endfor %}+ {% for budgetLimit in budget.budgeted %}+ {% if null == budget.spent[budgetLimit.currency_id] %}+ <span class="left_span" data-currency="{{ spentInfo.currency_id }}" data-limit="{{ budgetLimit.id }}"+ data-value="{{ spentInfo.spent + budgetLimit.amount }}" class="amount_left">+ {{ formatAmountBySymbol(budgetLimit.amount, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}+ {% if budgetLimit.in_range %}+ {% if 0 == activeDaysLeft %}+ ({{ formatAmountBySymbol(budgetLimit.amount, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})+ {% else %}+ ({{ formatAmountBySymbol(budgetLimit.amount / activeDaysLeft, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})+ {% endif %}+ {% endif %}+ {% if not budgetLimit.in_range %}+ <!-- For issue #10441, add per day if the budget limit is out of range. -->+ ({{ formatAmountBySymbol(budgetLimit.amount / budgetLimit.total_days, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})+ {% endif %}+ </span>+ <br/>+ {% endif %}++{% endfor %}+#}diff --git a/resources/views/budgets/partials/amount-spent.twig b/resources/views/budgets/partials/amount-spent.twignew file mode 100644index 0000000000..99734c59e5--- /dev/null+++ b/resources/views/budgets/partials/amount-spent.twig@@ -0,0 +1,42 @@+{# this is spent in budget limits: #}+{% for budgetLimit in budget.budgeted %}+ {{ formatAmountBySymbol(budgetLimit.spent, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}+ {% if 0 == budgetLimit.active_days_passed %}+ ({{ formatAmountBySymbol(budgetLimit.spent, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})+ {% else %}+ ({{ formatAmountBySymbol(budgetLimit.spent / budgetLimit.active_days_passed, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})+ {% endif %}+<br />+{% endfor %}++{# this is spent NOT in budget limits: #}+{% for spent in budget.spent %}+ {% if 0 != bccomp('0', spent.spent_outside) %}+ {{ formatAmountBySymbol(spent.spent_outside, spent.currency_symbol, spent.currency_decimal_places) }}+ {% if 0 == activeDaysPassed %}+ ({{ formatAmountBySymbol(spent.spent_outside, spent.currency_symbol, spent.currency_decimal_places) }})+ {% else %}+ ({{ formatAmountBySymbol(spent.spent_outside / activeDaysPassed, spent.currency_symbol, spent.currency_decimal_places) }})+ {% endif %}+ <br />+ {% endif %}+{% endfor %}++{#+++{% for spentInfo in budget.spent %}+ {{ formatAmountBySymbol(spentInfo.spent, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }}+ {% if 0 == activeDaysPassed %}+ ({{ formatAmountBySymbol(spentInfo.spent, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})+ {% else %}+ ({{ formatAmountBySymbol(spentInfo.spent / activeDaysPassed, spentInfo.currency_symbol, spentInfo.currency_decimal_places) }})+ {% endif %}+ <br/>+{% endfor %}+{% for budgetLimit in budget.budgeted %}+ {% if null == budget.spent[budgetLimit.currency_id] %}+ {{ formatAmountBySymbol(0, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}<br/>+ {% endif %}+{% endfor %}+#}