* fix(vat): complete account treatment enforcement * docs(api): refresh account endpoint skill * fix(mcp): preserve ruta 05 compatibility * test(vat): seed migration constraint fixtures * docs(vat): clarify treatment precedence --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
import type { BASAccount } from '@/types'
|
|
import {
|
|
suggestVatTreatment,
|
|
type AccountVatTreatment,
|
|
} from '@/lib/vat/account-vat-treatment'
|
|
import type { AccountMapping } from './types'
|
|
|
|
/**
|
|
* Add reviewable VAT suggestions to identity mappings. SIE itself has no VAT
|
|
* treatment record, so suggestions come only from the account label and are
|
|
* never considered reviewed until the user continues from the mapping step.
|
|
*/
|
|
export function enrichAccountMappingsWithVat(
|
|
mappings: AccountMapping[],
|
|
existingAccounts: BASAccount[],
|
|
): AccountMapping[] {
|
|
const existingByNumber = new Map(
|
|
existingAccounts.map((account) => [account.account_number, account]),
|
|
)
|
|
|
|
return mappings.map((mapping) => {
|
|
if (!mapping.targetAccount || mapping.sourceAccount !== mapping.targetAccount) {
|
|
return {
|
|
...mapping,
|
|
defaultVatTreatment: null,
|
|
defaultVatRate: null,
|
|
vatTreatmentSuggested: false,
|
|
vatTreatmentReviewed: true,
|
|
requiresVatTreatmentReview: false,
|
|
}
|
|
}
|
|
const accountClass = Number(mapping.sourceAccount.charAt(0))
|
|
if (accountClass < 3 || accountClass > 6) return mapping
|
|
|
|
const existing = existingByNumber.get(mapping.targetAccount)
|
|
if (existing?.default_vat_treatment) {
|
|
return {
|
|
...mapping,
|
|
defaultVatTreatment: existing.default_vat_treatment,
|
|
defaultVatRate: existing.default_vat_rate,
|
|
vatTreatmentReviewed: true,
|
|
vatTreatmentSuggested: false,
|
|
requiresVatTreatmentReview: false,
|
|
}
|
|
}
|
|
|
|
const suggestion = suggestVatTreatment(mapping.sourceAccount, mapping.sourceName)
|
|
return {
|
|
...mapping,
|
|
defaultVatTreatment: suggestion?.treatment ?? null,
|
|
defaultVatRate: existing?.default_vat_rate ?? suggestion?.rate ?? null,
|
|
vatTreatmentSuggested: Boolean(suggestion),
|
|
vatTreatmentReviewed: false,
|
|
requiresVatTreatmentReview: accountClass === 3 || accountClass === 4 || Boolean(suggestion),
|
|
}
|
|
})
|
|
}
|
|
|
|
export function applyVatTreatmentReview(
|
|
mappings: AccountMapping[],
|
|
sourceAccount: string,
|
|
treatment: AccountVatTreatment | null,
|
|
rate: number | null,
|
|
): AccountMapping[] {
|
|
return mappings.map((mapping) =>
|
|
mapping.sourceAccount === sourceAccount
|
|
? {
|
|
...mapping,
|
|
defaultVatTreatment: treatment,
|
|
defaultVatRate: rate,
|
|
vatTreatmentSuggested: false,
|
|
vatTreatmentReviewed: true,
|
|
}
|
|
: mapping
|
|
)
|
|
}
|
|
|
|
export function enrichChangedAccountMappingWithVat(
|
|
mappings: AccountMapping[],
|
|
sourceAccount: string,
|
|
existingAccounts: BASAccount[],
|
|
): AccountMapping[] {
|
|
return mappings.map((mapping) =>
|
|
mapping.sourceAccount === sourceAccount
|
|
? enrichAccountMappingsWithVat([mapping], existingAccounts)[0]
|
|
: mapping
|
|
)
|
|
}
|