chore: replace int amount_cents for string storage and decimal pkg

This commit is contained in:
juancwu 2026-03-14 14:43:39 -04:00
commit c8a1eb5b7a
No known key found for this signature in database
45 changed files with 706 additions and 587 deletions

View file

@ -14,6 +14,7 @@ import (
"git.juancwu.dev/juancwu/budgit/internal/ui/components/paymentmethod"
"git.juancwu.dev/juancwu/budgit/internal/ui/components/radio"
"git.juancwu.dev/juancwu/budgit/internal/ui/components/tagcombobox"
"github.com/shopspring/decimal"
)
type AddExpenseFormProps struct {
@ -215,7 +216,7 @@ templ EditExpenseForm(spaceID string, exp *model.ExpenseWithTagsAndMethod, metho
Name: "amount",
ID: "edit-amount-" + exp.ID,
Type: "number",
Value: fmt.Sprintf("%.2f", float64(exp.AmountCents)/100.0),
Value: model.FormatDecimal(exp.Amount),
Attributes: templ.Attributes{"step": "0.01", "required": "true"},
})
</div>
@ -345,7 +346,7 @@ templ ItemSelectorSection(listsWithItems []model.ListWithUncheckedItems, oob boo
</div>
}
templ BalanceCard(spaceID string, balance int, allocated int, oob bool) {
templ BalanceCard(spaceID string, balance decimal.Decimal, allocated decimal.Decimal, oob bool) {
<div
id="balance-card"
class="border rounded-lg p-4 bg-card text-card-foreground"
@ -354,11 +355,11 @@ templ BalanceCard(spaceID string, balance int, allocated int, oob bool) {
}
>
<h2 class="text-lg font-semibold">Current Balance</h2>
<p class={ "text-3xl font-bold", templ.KV("text-destructive", balance < 0) }>
{ fmt.Sprintf("$%.2f", float64(balance)/100.0) }
if allocated > 0 {
<p class={ "text-3xl font-bold", templ.KV("text-destructive", balance.LessThan(decimal.Zero)) }>
{ model.FormatMoney(balance) }
if allocated.GreaterThan(decimal.Zero) {
<span class="text-base font-normal text-muted-foreground">
({ fmt.Sprintf("$%.2f", float64(allocated)/100.0) } in accounts)
({ model.FormatMoney(allocated) } in accounts)
</span>
}
</p>

View file

@ -13,6 +13,7 @@ import (
"git.juancwu.dev/juancwu/budgit/internal/ui/components/label"
"git.juancwu.dev/juancwu/budgit/internal/ui/components/pagination"
"git.juancwu.dev/juancwu/budgit/internal/ui/components/selectbox"
"github.com/shopspring/decimal"
)
func frequencyLabel(f model.Frequency) string {
@ -32,7 +33,7 @@ func frequencyLabel(f model.Frequency) string {
}
}
templ BalanceSummaryCard(spaceID string, totalBalance int, availableBalance int, oob bool) {
templ BalanceSummaryCard(spaceID string, totalBalance decimal.Decimal, availableBalance decimal.Decimal, oob bool) {
<div
id="accounts-balance-summary"
class="border rounded-lg p-4 bg-card text-card-foreground"
@ -44,20 +45,20 @@ templ BalanceSummaryCard(spaceID string, totalBalance int, availableBalance int,
<div class="grid grid-cols-3 gap-4">
<div>
<p class="text-sm text-muted-foreground">Total Balance</p>
<p class={ "text-xl font-bold", templ.KV("text-destructive", totalBalance < 0) }>
{ fmt.Sprintf("$%.2f", float64(totalBalance)/100.0) }
<p class={ "text-xl font-bold", templ.KV("text-destructive", totalBalance.LessThan(decimal.Zero)) }>
{ model.FormatMoney(totalBalance) }
</p>
</div>
<div>
<p class="text-sm text-muted-foreground">Allocated</p>
<p class="text-xl font-bold">
{ fmt.Sprintf("$%.2f", float64(totalBalance-availableBalance)/100.0) }
{ model.FormatMoney(totalBalance.Sub(availableBalance)) }
</p>
</div>
<div>
<p class="text-sm text-muted-foreground">Available</p>
<p class={ "text-xl font-bold", templ.KV("text-destructive", availableBalance < 0) }>
{ fmt.Sprintf("$%.2f", float64(availableBalance)/100.0) }
<p class={ "text-xl font-bold", templ.KV("text-destructive", availableBalance.LessThan(decimal.Zero)) }>
{ model.FormatMoney(availableBalance) }
</p>
</div>
</div>
@ -79,8 +80,8 @@ templ AccountCard(spaceID string, acct *model.MoneyAccountWithBalance, oob ...bo
<div class="flex justify-between items-start mb-3">
<div>
<h3 class="font-semibold text-lg">{ acct.Name }</h3>
<p class={ "text-2xl font-bold", templ.KV("text-destructive", acct.BalanceCents < 0) }>
{ fmt.Sprintf("$%.2f", float64(acct.BalanceCents)/100.0) }
<p class={ "text-2xl font-bold", templ.KV("text-destructive", acct.Balance.LessThan(decimal.Zero)) }>
{ model.FormatMoney(acct.Balance) }
</p>
</div>
<div class="flex gap-1">
@ -356,7 +357,7 @@ templ RecurringDepositItem(spaceID string, rd *model.RecurringDepositWithAccount
</div>
<div class="flex items-center gap-2">
<span class="font-bold text-green-600 whitespace-nowrap">
+{ fmt.Sprintf("$%.2f", float64(rd.AmountCents)/100.0) }
+{ model.FormatMoney(rd.Amount) }
</span>
// Toggle
@button.Button(button.Props{
@ -636,11 +637,11 @@ templ TransferHistoryItem(spaceID string, t *model.AccountTransferWithAccount) {
<div class="flex items-center gap-2">
if t.Direction == model.TransferDirectionDeposit {
<span class="font-bold text-green-600 whitespace-nowrap">
+{ fmt.Sprintf("$%.2f", float64(t.AmountCents)/100.0) }
+{ model.FormatMoney(t.Amount) }
</span>
} else {
<span class="font-bold text-destructive whitespace-nowrap">
-{ fmt.Sprintf("$%.2f", float64(t.AmountCents)/100.0) }
-{ model.FormatMoney(t.Amount) }
</span>
}
@button.Button(button.Props{
@ -696,7 +697,7 @@ templ EditRecurringDepositForm(spaceID string, rd *model.RecurringDepositWithAcc
Name: "amount",
ID: "edit-rd-amount-" + rd.ID,
Type: "number",
Value: fmt.Sprintf("%.2f", float64(rd.AmountCents)/100.0),
Value: model.FormatDecimal(rd.Amount),
Attributes: templ.Attributes{"step": "0.01", "required": "true", "min": "0.01"},
})
</div>

View file

@ -73,11 +73,11 @@ templ RecurringItem(spaceID string, re *model.RecurringExpenseWithTagsAndMethod,
<div class="flex items-center gap-1 shrink-0">
if re.Type == model.ExpenseTypeExpense {
<p class="font-bold text-destructive">
- { fmt.Sprintf("$%.2f", float64(re.AmountCents)/100.0) }
- { model.FormatMoney(re.Amount) }
</p>
} else {
<p class="font-bold text-green-500">
+ { fmt.Sprintf("$%.2f", float64(re.AmountCents)/100.0) }
+ { model.FormatMoney(re.Amount) }
</p>
}
// Toggle pause/resume
@ -352,7 +352,7 @@ templ EditRecurringForm(spaceID string, re *model.RecurringExpenseWithTagsAndMet
Name: "amount",
ID: "edit-recurring-amount-" + re.ID,
Type: "number",
Value: fmt.Sprintf("%.2f", float64(re.AmountCents)/100.0),
Value: model.FormatDecimal(re.Amount),
Attributes: templ.Attributes{"step": "0.01", "required": "true"},
})
</div>