Files
accounted/components/supplier-invoices/supplier-invoice-defaults.ts
T
4560ccbfc9 perf(forms): supplier-invoice form, register forms and review dialogs read the session cache (#1938)
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>
2026-08-26 14:50:26 +02:00

39 lines
1.6 KiB
TypeScript

import type { CompanySettings, EntityType } from '@/types'
export interface SupplierInvoiceDefaults {
entityType: EntityType
accountingMethod: 'accrual' | 'cash'
/** Company-wide öresavrundning default; overridable per invoice. */
oreRounding: boolean
/** UI gate for kostnadsställe/projekt affordances (same as JournalEntryForm). */
dimensionsEnabled: boolean
/**
* Icke momsregistrerad verksamhet has no right to deduct input VAT: the
* moms controls disappear and every line books at 0 % (the gross amount IS
* the cost). Only an explicit false gates: a missing column keeps the
* registered-company behaviour.
*/
vatRegistered: boolean
}
/**
* Pure derivation of the supplier-invoice editor's settings-driven defaults.
* `fallbackEntityType` is the company row's entity type: /api/settings used
* to fall back to it when company_settings.entity_type is null, and the
* cached settings row does not, so the caller passes it explicitly.
*/
export function deriveSupplierInvoiceDefaults(
settings: CompanySettings | null | undefined,
fallbackEntityType?: EntityType | null,
): SupplierInvoiceDefaults {
const entityType =
(settings?.entity_type as EntityType | null | undefined) ?? fallbackEntityType ?? 'enskild_firma'
return {
entityType,
accountingMethod: settings?.accounting_method === 'cash' ? 'cash' : 'accrual',
oreRounding: typeof settings?.ore_rounding === 'boolean' ? settings.ore_rounding : true,
dimensionsEnabled: settings?.dimensions_enabled === true,
vatRegistered: settings?.vat_registered !== false,
}
}