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>
59 lines
2.3 KiB
TypeScript
59 lines
2.3 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useMemo, useState } from 'react'
|
|
import { useCompanyOptional } from '@/contexts/CompanyContext'
|
|
import {
|
|
useAccounts,
|
|
useCompanySettings,
|
|
useFiscalPeriods,
|
|
useSuppliers,
|
|
} from '@/lib/reference-data/hooks'
|
|
import { invalidateReferenceData } from '@/lib/reference-data/invalidate'
|
|
import { deriveSupplierInvoiceDefaults } from './supplier-invoice-defaults'
|
|
|
|
/**
|
|
* Reference data for the supplier-invoice editor: suppliers, the BAS chart,
|
|
* company settings (entity type, accounting method, öresavrundning default,
|
|
* dimensions, VAT registration) and fiscal periods.
|
|
*
|
|
* All of it comes from the session cache (lib/reference-data), seeded by the
|
|
* dashboard layout, so the form renders with its moms controls, period and
|
|
* account picker already resolved on the first paint instead of defaulting
|
|
* (vatRegistered=true, series, period) and flipping once four fetches land.
|
|
* The settings-driven values are derived (deriveSupplierInvoiceDefaults),
|
|
* never copied into state, so a background revalidation cannot get them out
|
|
* of step; the per-invoice öresavrundning toggle is the one local override.
|
|
*/
|
|
export function useSupplierInvoiceData() {
|
|
const company = useCompanyOptional()?.company ?? null
|
|
const { suppliers, isLoading: suppliersLoading } = useSuppliers()
|
|
const { accounts } = useAccounts()
|
|
const { settings } = useCompanySettings()
|
|
const { periods, isLoading: periodsLoading } = useFiscalPeriods()
|
|
|
|
const defaults = useMemo(
|
|
() => deriveSupplierInvoiceDefaults(settings, company?.entity_type ?? null),
|
|
[settings, company?.entity_type],
|
|
)
|
|
const [oreRoundingOverride, setOreRoundingOverride] = useState<boolean | null>(null)
|
|
const setOreRounding = useCallback((value: boolean) => setOreRoundingOverride(value), [])
|
|
|
|
/** After an inline supplier create: refresh the shared list everywhere. */
|
|
const refreshSuppliers = useCallback(() => invalidateReferenceData('ref:suppliers'), [])
|
|
|
|
return {
|
|
suppliers,
|
|
refreshSuppliers,
|
|
suppliersLoaded: !suppliersLoading,
|
|
accounts,
|
|
entityType: defaults.entityType,
|
|
accountingMethod: defaults.accountingMethod,
|
|
oreRounding: oreRoundingOverride ?? defaults.oreRounding,
|
|
setOreRounding,
|
|
dimensionsEnabled: defaults.dimensionsEnabled,
|
|
vatRegistered: defaults.vatRegistered,
|
|
periods,
|
|
periodsLoaded: !periodsLoading,
|
|
}
|
|
}
|