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:
Jakob Wennberg
2026-07-25 13:05:43 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 5afd031306
commit 01d2a1a946
2 changed files with 54 additions and 15 deletions
+27 -3
View File
@@ -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