The supplier-invoice editor issued four requests on every mount (suppliers, accounts, settings, fiscal periods) and defaulted vatRegistered=true, entity type and rounding until /api/settings landed, so the moms controls visibly flipped. The register forms fetched the whole chart of accounts to fill one konto combobox, and each transaction review dialog refetched accounts, cash accounts or settings per open. - use-supplier-invoice-data: thin composition of useSuppliers, useAccounts, useCompanySettings and useFiscalPeriods; the settings-driven gates come from a pure deriveSupplierInvoiceDefaults() (tested) instead of state that flips when the fetch returns; the per-invoice öresavrundning toggle is the one local override. Inline supplier create invalidates the shared list instead of patching local state. - SupplierForm, ArticleForm (posting accounts), QuickReviewDialog, InvoiceMatchDialog, supplier-invoices/[id] (payment dialog chart): useAccounts; ArticleForm's inline account create invalidates the chart. - BulkBookDialog, MatchVoucherDialog, DuplicateBookingDialog: cash accounts from useCashAccounts (resolveAccount over the cached list; an empty list still resolves to 1930 with the fallback note). - QuickReviewDialog, BulkBookDialog, NewEmployeeDialog, customers list (default payment terms), salary run page (payment format, bank, IBAN, dimensions): derived from useCompanySettings; the salary page's post-settings-modal refetch becomes a cache invalidation. raw-reference-fetch ratchet: 45 -> 35 files. Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { deriveSupplierInvoiceDefaults } from '../supplier-invoice-defaults'
|
|
import type { CompanySettings } from '@/types'
|
|
|
|
const settings = (overrides: Record<string, unknown>) => overrides as unknown as CompanySettings
|
|
|
|
describe('deriveSupplierInvoiceDefaults', () => {
|
|
it('uses registered-company defaults while settings are missing', () => {
|
|
expect(deriveSupplierInvoiceDefaults(null)).toEqual({
|
|
entityType: 'enskild_firma',
|
|
accountingMethod: 'accrual',
|
|
oreRounding: true,
|
|
dimensionsEnabled: false,
|
|
vatRegistered: true,
|
|
})
|
|
expect(deriveSupplierInvoiceDefaults(undefined, 'aktiebolag').entityType).toBe('aktiebolag')
|
|
})
|
|
|
|
it('reads every gate from the settings row', () => {
|
|
expect(
|
|
deriveSupplierInvoiceDefaults(
|
|
settings({
|
|
entity_type: 'aktiebolag',
|
|
accounting_method: 'cash',
|
|
ore_rounding: false,
|
|
dimensions_enabled: true,
|
|
vat_registered: false,
|
|
}),
|
|
),
|
|
).toEqual({
|
|
entityType: 'aktiebolag',
|
|
accountingMethod: 'cash',
|
|
oreRounding: false,
|
|
dimensionsEnabled: true,
|
|
vatRegistered: false,
|
|
})
|
|
})
|
|
|
|
it('only an explicit vat_registered=false gates VAT; null keeps the registered behaviour', () => {
|
|
expect(deriveSupplierInvoiceDefaults(settings({ vat_registered: null })).vatRegistered).toBe(true)
|
|
expect(deriveSupplierInvoiceDefaults(settings({})).vatRegistered).toBe(true)
|
|
})
|
|
|
|
it('falls back to the company entity type only when the settings row has none', () => {
|
|
expect(deriveSupplierInvoiceDefaults(settings({ entity_type: null }), 'aktiebolag').entityType).toBe('aktiebolag')
|
|
expect(deriveSupplierInvoiceDefaults(settings({ entity_type: 'enskild_firma' }), 'aktiebolag').entityType).toBe(
|
|
'enskild_firma',
|
|
)
|
|
})
|
|
|
|
it('treats an unknown accounting method or non-boolean rounding as the defaults', () => {
|
|
const d = deriveSupplierInvoiceDefaults(settings({ accounting_method: 'weird', ore_rounding: 'yes' }))
|
|
expect(d.accountingMethod).toBe('accrual')
|
|
expect(d.oreRounding).toBe(true)
|
|
})
|
|
})
|