feat(parties): fill customer and supplier forms from the register on a valid org number (#2355)
The registry lookup was built for rows that already exist (the detail page's "Hämta uppgifter" records facts on the row's party), so on the create form people typed what SCB already knew. Org number now comes first in both forms; a complete, check-digit valid number of a Swedish legal person is looked up once and fills name, address, postal code, city (and the VAT number where the form shows one) wherever nothing has been typed. A typed value is never replaced; a corrected number replaces only its own earlier fill. A personnummer never reaches the register (client key and server gate), and an environment without SCB credentials answers 503 once and the form stays quiet. - GET /api/parties/registry?org_number=: read-only route over the same SCB client, credential gate and registrySummary reader as the enrich route; writes nothing. - lib/parties/registry-form-fill.ts: registryLookupKey (one rule for what may be looked up) and registryFormFill (contactFill's untouched rule plus name and VAT number), pure and tested. - components/parties/use-registry-autofill.ts + RegistryAutofillNote: debounced, once per distinct number, skips the number an edit dialog opened with, one muted line under the field. - Adressrad 2 is now an input on both forms: the register's c/o goes on line 1 with the street on line 2, as on the row, and both edit dialogs already passed the column in. Claude-Session: https://claude.ai/code/session_01LvMaHcTnwAfxzgYD1fGYX1 Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Jakob Wennberg
Claude Fable 5.1
parent
3b9daf2606
commit
fcda4a75dd
@@ -15,6 +15,9 @@ import { Loader2, Lock, X } from 'lucide-react'
|
||||
import AccountCombobox from '@/components/bookkeeping/AccountCombobox'
|
||||
import { useCanWrite } from '@/lib/hooks/use-can-write'
|
||||
import { getCountryOptions, normalizeCountryCode } from '@/lib/vat/country-codes'
|
||||
import { registryFormFill, type RegistryFormField } from '@/lib/parties/registry-form-fill'
|
||||
import { useRegistryAutofill } from '@/components/parties/use-registry-autofill'
|
||||
import { RegistryAutofillNote } from '@/components/parties/RegistryAutofillNote'
|
||||
import type { CreateSupplierInput } from '@/types'
|
||||
|
||||
interface SupplierFormProps {
|
||||
@@ -87,6 +90,8 @@ export default function SupplierForm({
|
||||
handleSubmit,
|
||||
control,
|
||||
watch,
|
||||
getValues,
|
||||
setValue,
|
||||
formState: { errors },
|
||||
} = useForm<FormData>({
|
||||
resolver: zodResolver(schema),
|
||||
@@ -96,6 +101,7 @@ export default function SupplierForm({
|
||||
email: initialData?.email || '',
|
||||
phone: initialData?.phone || '',
|
||||
address_line1: initialData?.address_line1 || '',
|
||||
address_line2: initialData?.address_line2 || '',
|
||||
postal_code: initialData?.postal_code || '',
|
||||
city: initialData?.city || '',
|
||||
country: normalizeCountryCode(initialData?.country) ?? initialData?.country ?? 'SE',
|
||||
@@ -115,6 +121,39 @@ export default function SupplierForm({
|
||||
},
|
||||
})
|
||||
|
||||
const supplierType = watch('supplier_type')
|
||||
const orgNumber = watch('org_number')
|
||||
// A complete org number of a Swedish company is looked up in SCB's
|
||||
// register once, and the fields it knows are filled where nothing has
|
||||
// been typed (issue #2218). Quiet when the environment has no SCB
|
||||
// credentials. The VAT number is among the fields here: the form shows it.
|
||||
const autofill = useRegistryAutofill({
|
||||
orgNumber,
|
||||
enabled: canWrite && supplierType === 'swedish_business',
|
||||
initialOrgNumber: initialData?.org_number,
|
||||
apply: (now, before) => {
|
||||
const v = getValues()
|
||||
const patch = registryFormFill(
|
||||
{
|
||||
name: v.name ?? '',
|
||||
email: v.email ?? '',
|
||||
phone: v.phone ?? '',
|
||||
address_line1: v.address_line1 ?? '',
|
||||
address_line2: v.address_line2 ?? '',
|
||||
postal_code: v.postal_code ?? '',
|
||||
city: v.city ?? '',
|
||||
vat_number: v.vat_number ?? '',
|
||||
},
|
||||
now,
|
||||
before,
|
||||
)
|
||||
for (const [field, value] of Object.entries(patch)) {
|
||||
setValue(field as RegistryFormField, value ?? '', { shouldDirty: true, shouldValidate: true })
|
||||
}
|
||||
return Object.keys(patch)
|
||||
},
|
||||
})
|
||||
|
||||
const countryValue = watch('country')
|
||||
// A stored value the picker does not list (an unmapped legacy name, or a
|
||||
// code outside the curated list) still has to be visible, or the field
|
||||
@@ -151,6 +190,27 @@ export default function SupplierForm({
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Identification first: on a Swedish company's org number the register fills the rest */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="org_number">{t('org_number_label')}</Label>
|
||||
<Input
|
||||
id="org_number"
|
||||
placeholder={t('org_number_placeholder')}
|
||||
{...register('org_number')}
|
||||
/>
|
||||
<RegistryAutofillNote state={autofill} />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="vat_number">{t('vat_label')}</Label>
|
||||
<Input
|
||||
id="vat_number"
|
||||
placeholder={t('vat_placeholder_se')}
|
||||
{...register('vat_number')}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Name */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="name">{t('name_label')}</Label>
|
||||
@@ -188,29 +248,6 @@ export default function SupplierForm({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Business info */}
|
||||
<div className="space-y-4 pt-4 border-t">
|
||||
<h3>{t('business_section')}</h3>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="org_number">{t('org_number_label')}</Label>
|
||||
<Input
|
||||
id="org_number"
|
||||
placeholder={t('org_number_placeholder')}
|
||||
{...register('org_number')}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="vat_number">{t('vat_label')}</Label>
|
||||
<Input
|
||||
id="vat_number"
|
||||
placeholder={t('vat_placeholder_se')}
|
||||
{...register('vat_number')}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Address */}
|
||||
<div className="space-y-4 pt-4 border-t">
|
||||
<h3>{t('address_section')}</h3>
|
||||
@@ -222,6 +259,14 @@ export default function SupplierForm({
|
||||
{...register('address_line1')}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="address_line2">{t('address_line2_label')}</Label>
|
||||
<Input
|
||||
id="address_line2"
|
||||
placeholder={t('address_line2_placeholder')}
|
||||
{...register('address_line2')}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="postal_code">{t('postal_label')}</Label>
|
||||
|
||||
Reference in New Issue
Block a user