Fix amount display in budget overview.

This commit is contained in:
James Cole
2026-05-03 09:24:03 +02:00
parent 3a9ac03358
commit 04704392f3
5 changed files with 84 additions and 82 deletions
@@ -252,6 +252,7 @@ final class IndexController extends Controller
new Collection()->push($budget),
$currency
);
$spentAmount = $spent[$currency->id]['sum'] ?? '0';
$array['budgeted'][] = [
'id' => $limit->id,
@@ -289,7 +290,7 @@ final class IndexController extends Controller
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]['spent_outside'] = Steam::negative(bcsub($spentInLimits[$currency->id], $spentArr[$currency->id]['sum']));
$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;
@@ -80,8 +80,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
->where('budget_limits.transaction_currency_id', $currency->id)
->whereNull('budgets.deleted_at')
->where('budgets.active', true)
->where('budgets.user_id', $this->user->id)
;
->where('budgets.user_id', $this->user->id);
if ($budgets instanceof Collection && $budgets->count() > 0) {
$query->whereIn('budget_limits.budget_id', $budgets->pluck('id')->toArray());
}
@@ -92,13 +91,13 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
/** @var BudgetLimit $budgetLimit */
foreach ($set as $budgetLimit) {
if ($budgetLimit->start_date->isSameDay($start) && $budgetLimit->end_date->isSameDay($end)) {
$result = bcadd((string) $budgetLimit->amount, $result);
$result = bcadd((string)$budgetLimit->amount, $result);
continue;
}
$period = Period::make($start, $end, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
$amountPerDay = $this->getDailyAmount($budgetLimit);
$result = bcadd($result, bcmul((string) $period->length(), $amountPerDay));
$result = bcadd($result, bcmul((string)$period->length(), $amountPerDay));
}
return $result;
@@ -140,8 +139,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))
->first()
;
->first();
}
public function getAllBudgetLimits(?Carbon $start = null, ?Carbon $end = null): Collection
@@ -152,16 +150,14 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
->with(['budget'])
->where('budgets.user_id', $this->user->id)
->whereNull('budgets.deleted_at')
->get(['budget_limits.*'])
;
->get(['budget_limits.*']);
}
// one of the two is NULL.
if (!$start instanceof Carbon xor !$end instanceof Carbon) {
$query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
->with(['budget'])
->whereNull('budgets.deleted_at')
->where('budgets.user_id', $this->user->id)
;
->where('budgets.user_id', $this->user->id);
if ($end instanceof Carbon) {
// end date must be before $end.
$query->where('end_date', '<=', $end->format('Y-m-d 00:00:00'));
@@ -194,14 +190,13 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d'));
});
})
->get(['budget_limits.*'])
;
->get(['budget_limits.*']);
}
public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, ?Carbon $start = null, ?Carbon $end = null): Collection
{
return $this->getAllBudgetLimits($start, $end)->filter(
static fn (BudgetLimit $budgetLimit): bool => $budgetLimit->transaction_currency_id === $currency->id
static fn(BudgetLimit $budgetLimit): bool => $budgetLimit->transaction_currency_id === $currency->id
);
}
@@ -240,8 +235,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
->orWhere(static function (Builder $q3) use ($start, $end): void {
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00'));
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 23:59:59'));
})
;
});
})->orWhere(static function (Builder $q4) use ($start, $end): void {
// or start is before start AND end is after end.
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 23:59:59'));
@@ -249,15 +243,14 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
});
})
->orderBy('budget_limits.start_date', 'DESC')
->get(['budget_limits.*'])
;
->get(['budget_limits.*']);
}
public function getDailyAmount(BudgetLimit $budgetLimit): string
{
$limitPeriod = Period::make($budgetLimit->start_date, $budgetLimit->end_date, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
$days = $limitPeriod->length();
$amount = bcdiv($budgetLimit->amount, (string) $days, 12);
$amount = bcdiv($budgetLimit->amount, (string)$days, 12);
Log::debug(sprintf(
'Total amount for budget limit #%d is %s. Nr. of days is %d. Amount per day is %s',
$budgetLimit->id,
@@ -272,7 +265,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
#[Override]
public function getNoteText(BudgetLimit $budgetLimit): string
{
return (string) $budgetLimit->notes()->first()?->text;
return (string)$budgetLimit->notes()->first()?->text;
}
#[Override]
@@ -309,7 +302,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
// find the budget:
/** @var null|Budget $budget */
$budget = $this->user->budgets()->find((int) $data['budget_id']);
$budget = $this->user->budgets()->find((int)$data['budget_id']);
if (null === $budget) {
throw new FireflyException('200004: Budget does not exist.');
}
@@ -320,8 +313,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
->where('budget_limits.start_date', $data['start_date']->format('Y-m-d'))
->where('budget_limits.end_date', $data['end_date']->format('Y-m-d'))
->where('budget_limits.transaction_currency_id', $currency->id)
->first(['budget_limits.*'])
;
->first(['budget_limits.*']);
if (null !== $limit) {
throw new FireflyException('200027: Budget limit already exists.');
}
@@ -335,14 +327,16 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
$limit = new BudgetLimit();
$limit->budget()->associate($budget);
$limit->start_date = $data['start_date']->format('Y-m-d');
$limit->start_date_tz = $data['start_date']->format('e');
$limit->end_date = $data['end_date']->format('Y-m-d');
$limit->end_date_tz = $data['end_date']->format('e');
$limit->amount = $data['amount'];
$limit->generated = $data['generated'] ?? false;
$limit->period = $data['period'] ?? '';
$limit->transaction_currency_id = $currency->id;
$limit->save();
$noteText = (string) ($data['notes'] ?? '');
$noteText = (string)($data['notes'] ?? '');
if ('' !== $noteText) {
$this->setNoteText($limit, $noteText);
}
@@ -398,7 +392,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface, UserGroup
// update notes if they exist.
if (array_key_exists('notes', $data)) {
$this->setNoteText($budgetLimit, (string) $data['notes']);
$this->setNoteText($budgetLimit, (string)$data['notes']);
}
Log::debug(sprintf('Updated budget limit with ID #%d', $budgetLimit->id));
$generateMessages = $data['fire_webhooks'] ?? true;
+7
View File
@@ -1814,6 +1814,13 @@ return [
'options' => 'Options',
// budgets:
'spent_this_period' => 'Spent on this budget',
'spent_this_period_per_day'=> 'Spent on this budget per day (:days day(s))',
'spent_in_budget_limit_outside_period' => 'Spent on this budget, but NOT in this period',
'spent_in_budget_limit_outside_period_per_day' => 'Spent on this budget, but NOT in this period per day (:days day(s))',
'left_in_budget_limit_overview' => 'Left in this budget',
'left_in_budget_limit_per_day'=> 'Left in this budget per day (:days day(s))',
'nothing_left_in_budget' => 'The budget is now empty',
'daily_budgets' => 'Daily budgets',
'weekly_budgets' => 'Weekly budgets',
'monthly_budgets' => 'Monthly budgets',
@@ -2,21 +2,21 @@
{% 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">
{# the amount left #}
{{ formatAmountBySymbol(budgetLimit.left, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}
<span title="{{ 'left_in_budget_limit_overview'|_ }}">{{ formatAmountBySymbol(budgetLimit.left, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}</span>
{# if the budget limit is in the past, this is not interesting. #}
{# if there is nothing left, this is not interesting. #}
{% if not budgetLimit.in_past and -1 == bccomp('0',budgetLimit.left) %}
{% if 0 == budgetLimit.active_days_left %}
({{ formatAmountBySymbol(budgetLimit.left, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})
<span title="{{ trans('firefly.left_in_budget_limit_per_day', {days: 0}) }}">({{ 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) }})
<span title="{{ trans('firefly.left_in_budget_limit_per_day', {days: budgetLimit.active_days_left}) }}">({{ formatAmountBySymbol(budgetLimit.left / budgetLimit.active_days_left, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})</span>
{% endif %}
{% endif %}
{# if there is nothing left, just format 0.00 #}
{% if not budgetLimit.in_past and -1 != bccomp('0',budgetLimit.left) %}
({{ formatAmountBySymbol('0', budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})
<span title="{{ 'nothing_left_in_budget'|_ }}">({{ formatAmountBySymbol('0', budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})</span>
{% endif %}
</span><br />
{% endfor %}
@@ -1,10 +1,10 @@
{# this is spent in budget limits: #}
{% for budgetLimit in budget.budgeted %}
{{ formatAmountBySymbol(budgetLimit.spent, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}
<span title="{{ 'spent_this_period'|_ }}">{{ formatAmountBySymbol(budgetLimit.spent, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }}</span>
{% if 0 == budgetLimit.active_days_passed %}
({{ formatAmountBySymbol(budgetLimit.spent, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})
<span title="{{ trans('firefly.spent_this_period_per_day', {days: 0}) }}">({{ formatAmountBySymbol(budgetLimit.spent, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})</span>
{% else %}
({{ formatAmountBySymbol(budgetLimit.spent / budgetLimit.active_days_passed, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})
<span title="{{ trans('firefly.spent_this_period_per_day', {days: budgetLimit.active_days_passed}) }}">({{ formatAmountBySymbol(budgetLimit.spent / budgetLimit.active_days_passed, budgetLimit.currency_symbol, budgetLimit.currency_decimal_places) }})</span>
{% endif %}
<br />
{% endfor %}
@@ -12,11 +12,11 @@
{# 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) }}
<span title="{{ 'spent_in_budget_limit_outside_period'|_ }}">{{ formatAmountBySymbol(spent.spent_outside, spent.currency_symbol, spent.currency_decimal_places) }}</span>
{% if 0 == activeDaysPassed %}
({{ formatAmountBySymbol(spent.spent_outside, spent.currency_symbol, spent.currency_decimal_places) }})
<span title="{{ trans('firefly.spent_in_budget_limit_outside_period_per_day', {days: 0}) }}">({{ formatAmountBySymbol(spent.spent_outside, spent.currency_symbol, spent.currency_decimal_places) }})</span>
{% else %}
({{ formatAmountBySymbol(spent.spent_outside / activeDaysPassed, spent.currency_symbol, spent.currency_decimal_places) }})
<span title="{{ trans('firefly.spent_in_budget_limit_outside_period_per_day', {days: activeDaysPassed}) }}">({{ formatAmountBySymbol(spent.spent_outside / activeDaysPassed, spent.currency_symbol, spent.currency_decimal_places) }})</span>
{% endif %}
<br />
{% endif %}