Files
accounted/extensions/general/invoice-inbox/lib/fallback-kontering.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

70 lines
2.4 KiB
TypeScript

/**
* Skeleton kontering for a matched transaction the engine has no proposal for.
*
* When suggest-booking comes back empty (unknown supplier, withheld
* foreign-currency rule, engine failure) the manual-booking dialog used to
* open with nothing at all, even though the matched bank row already tells us
* the amount in kronor and the settlement account. This rebuilds what
* BookDirectlyDialog's buildPrefillLines seeded before the dialog swap:
* the transaction's SEK amount against the settlement account, with the
* counter-account left blank for the user to pick. No VAT split: with a
* matched transaction the old prefill skipped document VAT too, since the
* document total and the bank movement are not guaranteed to agree.
*
* The SEK amount goes through resolveSekAmountOrNull: a foreign row with
* neither a stored SEK value nor a rate has no honest kronor figure, and
* prefilling the raw foreign number would relabel 100 EUR as 100 kr. In that
* case we return no lines and the dialog opens blank, as it does today.
*/
import { resolveSekAmountOrNull } from '@/lib/bookkeeping/currency-utils'
import { roundOre } from '@/lib/money'
export interface FallbackKonteringTx {
amount: number
amount_sek?: number | null
currency?: string | null
exchange_rate?: number | null
}
export interface FallbackKonteringLine {
account_number: string
debit_amount: number
credit_amount: number
description: string
}
export function buildFallbackKonteringLines(
tx: FallbackKonteringTx,
settlementAccount: string,
): FallbackKonteringLine[] {
const sek = resolveSekAmountOrNull(tx.amount, tx.amount_sek, tx.currency, tx.exchange_rate)
if (sek == null) return []
const total = roundOre(Math.abs(sek))
if (total <= 0) return []
const costLine: FallbackKonteringLine = {
account_number: '',
debit_amount: 0,
credit_amount: 0,
description: '',
}
const settlementLine: FallbackKonteringLine = {
account_number: settlementAccount,
debit_amount: 0,
credit_amount: 0,
description: '',
}
if (sek < 0) {
// Money left the account: debit the (unknown) cost side, credit the bank.
costLine.debit_amount = total
settlementLine.credit_amount = total
return [costLine, settlementLine]
}
// Money came in (refund, credit note payout): debit the bank instead.
settlementLine.debit_amount = total
costLine.credit_amount = total
return [settlementLine, costLine]
}