fix(bookkeeping): stop draft edits wiping line FX metadata and tax_code (#1187)
* fix(bookkeeping): stop draft edits wiping line FX metadata and tax_code Fixes #1174. EditDraftEntryDialog hydrated only account/amounts/text/ dimensions into the form, and updateDraftEntry replaces all lines, so editing the text on a foreign-currency draft silently reset its lines to SEK (currency default, amount_in_currency/exchange_rate nulled) and stripped tax_code entirely (FormLine had no such field). The dialog also displayed EUR drafts as SEK before any save. - FormLine carries tax_code as a pure pass-through; the submit body sends it back. - The dialog hydrates per-line currency/amount_in_currency/ exchange_rate/tax_code, and passes initialCurrency/rate/amount so the form's currency picker and FX fields show the stored values. - A hydrated FX line marks the currency-meta slot as taken so the 19xx fallback cannot double-stamp another line. Blast radius was drafts only (posted entries are immutable), reachable via the form's own currency picker and via v1/MCP lines carrying tax_code. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(bookkeeping): keep the hydrated draft FX rate on mount CodeRabbit finding on #1187, verified real: the currency effect fired on mount for a hydrated foreign draft and replaced the STORED exchange rate with today's Riksbanken rate, so a text-only edit would save a different rate. The initial hydrated currency now skips the automatic fetch; changing currency or date afterwards still refetches. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
5afd031306
commit
01d2a1a946
@@ -8,7 +8,7 @@ import {
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import JournalEntryForm, { type FormLine } from '@/components/bookkeeping/JournalEntryForm'
|
||||
import type { JournalEntry, JournalEntryLine } from '@/types'
|
||||
import type { Currency, JournalEntry, JournalEntryLine } from '@/types'
|
||||
|
||||
interface Props {
|
||||
entry: JournalEntry
|
||||
@@ -27,19 +27,31 @@ interface Props {
|
||||
export default function EditDraftEntryDialog({ entry, open, onOpenChange, onUpdated }: Props) {
|
||||
const t = useTranslations('bookkeeping')
|
||||
|
||||
const initialLines: FormLine[] = ((entry.lines || []) as JournalEntryLine[])
|
||||
const sortedLines = ((entry.lines || []) as JournalEntryLine[])
|
||||
.slice()
|
||||
.sort((a, b) => a.sort_order - b.sort_order)
|
||||
.map((l) => ({
|
||||
account_number: l.account_number,
|
||||
debit_amount: Number(l.debit_amount) > 0 ? String(l.debit_amount) : '',
|
||||
credit_amount: Number(l.credit_amount) > 0 ? String(l.credit_amount) : '',
|
||||
line_description: l.line_description || '',
|
||||
// Carry the line's dimensions into the form: the PATCH replaces all
|
||||
// lines, so omitting this would silently strip existing tags.
|
||||
dimensions:
|
||||
l.dimensions && Object.keys(l.dimensions).length > 0 ? { ...l.dimensions } : undefined,
|
||||
}))
|
||||
|
||||
// The PATCH replaces all lines wholesale, so EVERYTHING a line carries must
|
||||
// round-trip through the form: dimensions, per-line FX metadata (currency /
|
||||
// amount_in_currency / exchange_rate) and tax_code. Omitting any of them
|
||||
// meant a plain text edit silently reset a foreign-currency draft to SEK
|
||||
// and stripped its tax code (issue #1174).
|
||||
const initialLines: FormLine[] = sortedLines.map((l) => ({
|
||||
account_number: l.account_number,
|
||||
debit_amount: Number(l.debit_amount) > 0 ? String(l.debit_amount) : '',
|
||||
credit_amount: Number(l.credit_amount) > 0 ? String(l.credit_amount) : '',
|
||||
line_description: l.line_description || '',
|
||||
currency: l.currency && l.currency !== 'SEK' ? l.currency : undefined,
|
||||
amount_in_currency: l.amount_in_currency ?? undefined,
|
||||
exchange_rate: l.exchange_rate ?? undefined,
|
||||
tax_code: l.tax_code ?? undefined,
|
||||
dimensions:
|
||||
l.dimensions && Object.keys(l.dimensions).length > 0 ? { ...l.dimensions } : undefined,
|
||||
}))
|
||||
|
||||
// Hydrate the form's currency picker + FX fields from the stored FX line so
|
||||
// a EUR draft is displayed as EUR instead of silently reading SEK.
|
||||
const fxLine = sortedLines.find((l) => l.currency && l.currency !== 'SEK')
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
@@ -62,6 +74,9 @@ export default function EditDraftEntryDialog({ entry, open, onOpenChange, onUpda
|
||||
initialDescription={entry.description}
|
||||
initialNotes={entry.notes ?? undefined}
|
||||
initialVoucherSeries={entry.voucher_series}
|
||||
initialCurrency={fxLine ? (fxLine.currency as Currency) : undefined}
|
||||
initialExchangeRate={fxLine?.exchange_rate ?? undefined}
|
||||
initialForeignAmount={fxLine?.amount_in_currency ?? undefined}
|
||||
onUpdated={onUpdated}
|
||||
/>
|
||||
</DialogContent>
|
||||
|
||||
@@ -59,6 +59,9 @@ export interface FormLine {
|
||||
currency?: string
|
||||
amount_in_currency?: number
|
||||
exchange_rate?: number
|
||||
/** Pass-through only (set via API/MCP, never edited in this form): edit
|
||||
* mode replaces all lines, so dropping it would strip the stored code. */
|
||||
tax_code?: string | null
|
||||
/** SIE dimension map {sie_dim_no: object_code}, e.g. {"1":"KS01","6":"P001"}. */
|
||||
dimensions?: Record<string, string>
|
||||
}
|
||||
@@ -81,6 +84,11 @@ interface Props {
|
||||
/** Edit an existing DRAFT in place: the form PATCHes this entry instead of
|
||||
* creating a new one. Only the draft's header + lines are updated. */
|
||||
editEntryId?: string
|
||||
/** Edit mode: hydrate the currency picker + FX fields from the stored draft
|
||||
* so a foreign-currency draft is not displayed (and resaved) as SEK. */
|
||||
initialCurrency?: Currency
|
||||
initialExchangeRate?: number
|
||||
initialForeignAmount?: number
|
||||
/** Fired after a successful draft edit (editEntryId path). */
|
||||
onUpdated?: () => void
|
||||
/** The bank transaction being booked (set by TransactionBookingDialog).
|
||||
@@ -108,6 +116,9 @@ export default function JournalEntryForm({
|
||||
embedded,
|
||||
bare,
|
||||
editEntryId,
|
||||
initialCurrency,
|
||||
initialExchangeRate,
|
||||
initialForeignAmount,
|
||||
onUpdated,
|
||||
duplicateMatchTransaction,
|
||||
onDuplicateMatched,
|
||||
@@ -181,10 +192,10 @@ export default function JournalEntryForm({
|
||||
// the account picker surface standard accounts the company hasn't activated
|
||||
// yet; picking one activates it at commit via the existing rail.
|
||||
const [catalog, setCatalog] = useState<CatalogAccount[]>([])
|
||||
const [entryCurrency, setEntryCurrency] = useState<Currency>('SEK')
|
||||
const [exchangeRate, setExchangeRate] = useState('')
|
||||
const [entryCurrency, setEntryCurrency] = useState<Currency>(initialCurrency ?? 'SEK')
|
||||
const [exchangeRate, setExchangeRate] = useState(initialExchangeRate != null ? String(initialExchangeRate) : '')
|
||||
const [isFetchingRate, setIsFetchingRate] = useState(false)
|
||||
const [foreignAmount, setForeignAmount] = useState('')
|
||||
const [foreignAmount, setForeignAmount] = useState(initialForeignAmount != null ? String(initialForeignAmount) : '')
|
||||
const [periodMismatch, setPeriodMismatch] = useState<'no_period' | 'wrong_period' | null>(null)
|
||||
const [showCreatePeriod, setShowCreatePeriod] = useState(false)
|
||||
const [showClearConfirm, setShowClearConfirm] = useState(false)
|
||||
@@ -339,8 +350,16 @@ export default function JournalEntryForm({
|
||||
}
|
||||
}, [entryDate])
|
||||
|
||||
// Edit mode hydrates the draft's STORED rate: the mount-time fetch must not
|
||||
// replace it with today's rate, or a text-only edit would silently save a
|
||||
// different FX rate. Fetch only after the user changes currency (or date).
|
||||
const skipInitialRateFetch = useRef(initialExchangeRate != null)
|
||||
useEffect(() => {
|
||||
if (entryCurrency !== 'SEK') {
|
||||
if (skipInitialRateFetch.current) {
|
||||
skipInitialRateFetch.current = false
|
||||
return
|
||||
}
|
||||
fetchRate(entryCurrency)
|
||||
}
|
||||
}, [entryCurrency, fetchRate])
|
||||
@@ -861,10 +880,15 @@ export default function JournalEntryForm({
|
||||
if (Object.keys(dims).length > 0) base.dimensions = dims
|
||||
}
|
||||
|
||||
if (l.tax_code) base.tax_code = l.tax_code
|
||||
|
||||
if (l.currency) {
|
||||
base.currency = l.currency
|
||||
if (l.amount_in_currency != null) base.amount_in_currency = l.amount_in_currency
|
||||
if (l.exchange_rate != null) base.exchange_rate = l.exchange_rate
|
||||
// A line already carrying FX metadata (hydrated edit) IS the FX
|
||||
// line: don't also stamp the fallback meta onto another 19xx line.
|
||||
if (l.currency !== 'SEK') currencyMetaApplied = true
|
||||
} else if (isForeign && rate > 0 && l.account_number.startsWith('19') && !currencyMetaApplied) {
|
||||
base.currency = entryCurrency
|
||||
base.amount_in_currency = computedForeignAmount
|
||||
|
||||
Reference in New Issue
Block a user