fix(budget): seed edit fields with two decimals and allow signed amounts for refunds (#2175, #2176)

This commit is contained in:
Maurice
2026-09-01 15:07:26 +02:00
committed by Maurice
parent 49b0ce28d5
commit d1aa573167
18 changed files with 717 additions and 78 deletions
+27 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { splitReservationDateTime, resolveDayId, formatMoney, formatMoneySum, currencyDecimals, localizeAmountInput } from './formatters'
import { splitReservationDateTime, resolveDayId, formatMoney, formatMoneySum, currencyDecimals, localizeAmountInput, amountToInputString } from './formatters'
import { CURRENCIES, SYMBOLS, currenciesWith } from '../components/Budget/BudgetPanel.constants'
import type { Day } from '../types'
@@ -40,6 +40,32 @@ describe('localizeAmountInput (#1624)', () => {
})
})
describe('amountToInputString (#2175)', () => {
it('pads a stored number to the currency decimals so the edit form matches the list', () => {
expect(amountToInputString(4.9, 'EUR')).toBe('4.90')
expect(amountToInputString(5, 'EUR')).toBe('5.00')
expect(amountToInputString(163.2, 'USD')).toBe('163.20')
})
it('gives zero-decimal currencies no fake decimals', () => {
expect(amountToInputString(500, 'JPY')).toBe('500')
expect(amountToInputString(500, 'HUF')).toBe('500')
})
it('pads three-decimal currencies to three', () => {
expect(amountToInputString(1.5, 'KWD')).toBe('1.500')
})
it('cent-rounds float noise before padding (#1964)', () => {
expect(amountToInputString(163.20999999999998, 'EUR')).toBe('163.21')
})
it('keeps the sign of a refund (#2176)', () => {
expect(amountToInputString(-4.9, 'EUR')).toBe('-4.90')
})
it('turns nullish and non-finite values into an empty field', () => {
expect(amountToInputString(null, 'EUR')).toBe('')
expect(amountToInputString(undefined, 'EUR')).toBe('')
expect(amountToInputString(Number.NaN, 'EUR')).toBe('')
})
})
describe('KGS currency (#1400)', () => {
it('is selectable everywhere the shared currency list feeds', () => {
expect(CURRENCIES).toContain('KGS')
+13
View File
@@ -102,6 +102,19 @@ export function cleanAmount(value: number): number {
return Math.round(value * 100) / 100
}
/**
* A stored amount as the edit form should seed it: cent-rounded and padded to
* the currency's decimal count (#2175). The DB hands back a number, so a saved
* 4,90 is 4.9 and a saved 5,00 is 5 — String() then renders "4,9"/"5" in the
* amount field while the list shows "4,90"/"5,00" via Intl. Dot-normalized like
* all amount-input state (see localizeAmountInput); zero-decimal currencies get
* no fake decimals ("500", not "500.00").
*/
export function amountToInputString(value: number | null | undefined, currency: string): string {
if (value == null || !Number.isFinite(value)) return ''
return cleanAmount(value).toFixed(currencyDecimals(currency))
}
/**
* A price held as free text, tidied only when it really is a number. Import
* writes whatever the confirmation said into this field, so anything that does