331 lines
14 KiB
TypeScript
331 lines
14 KiB
TypeScript
import { useForm } from '@inertiajs/react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { FormErrors, Panel, controlClass } from '@/components/procurement';
|
|
|
|
const identityFields = {
|
|
legal_name: 'Denominação social',
|
|
nuel: 'NUEL',
|
|
address: 'Endereço',
|
|
nuit: 'NUIT',
|
|
phone: 'Telefone',
|
|
email: 'Email',
|
|
legal_representative: 'Representante legal',
|
|
} as const;
|
|
const bankFields = {
|
|
bank: 'Banco',
|
|
branch: 'Balcão',
|
|
account_number: 'Número de conta',
|
|
currency: 'Moeda',
|
|
nib: 'NIB',
|
|
iban: 'IBAN',
|
|
nuib: 'NUIB',
|
|
account_holder: 'Titular da conta',
|
|
} as const;
|
|
const locationFields = {
|
|
name: 'Designação',
|
|
address: 'Endereço',
|
|
phone: 'Telefone',
|
|
email: 'Email',
|
|
} as const;
|
|
type Values<T> = Record<keyof T, string | null>;
|
|
type Bank = Values<typeof bankFields> & { id: string };
|
|
type Establishment = Values<typeof locationFields> & {
|
|
id: string;
|
|
kind: 'headquarters' | 'branch';
|
|
};
|
|
export type CompanyDetails = Partial<Values<typeof identityFields>> & {
|
|
bank_accounts?: Bank[];
|
|
establishments?: Establishment[];
|
|
};
|
|
|
|
function Fields({
|
|
fields,
|
|
values,
|
|
prefix,
|
|
change,
|
|
}: {
|
|
fields: Record<string, string>;
|
|
values: Record<string, string | null>;
|
|
prefix: string;
|
|
change: (key: string, value: string) => void;
|
|
}) {
|
|
return (
|
|
<div className="grid min-w-0 gap-4 sm:grid-cols-2">
|
|
{Object.entries(fields).map(([key, label]) => (
|
|
<label
|
|
className="grid min-w-0 gap-2 text-sm"
|
|
key={key}
|
|
htmlFor={`${prefix}-${key}`}
|
|
>
|
|
{label}
|
|
<input
|
|
id={`${prefix}-${key}`}
|
|
className={`${controlClass} w-full min-w-0`}
|
|
type={
|
|
key === 'email'
|
|
? 'email'
|
|
: key === 'phone'
|
|
? 'tel'
|
|
: 'text'
|
|
}
|
|
maxLength={key === 'address' ? 1000 : 255}
|
|
value={values[key] ?? ''}
|
|
onChange={(e) => change(key, e.target.value)}
|
|
autoComplete="off"
|
|
/>
|
|
{key === 'currency' && (
|
|
<span className="text-muted-foreground">
|
|
Código de três letras, por exemplo MZN.
|
|
</span>
|
|
)}
|
|
{key === 'nuit' && (
|
|
<span className="text-muted-foreground">
|
|
Nove dígitos; conservar os zeros iniciais.
|
|
</span>
|
|
)}
|
|
</label>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function CompanyDetailsSettings({
|
|
details,
|
|
ready,
|
|
version,
|
|
}: {
|
|
details: CompanyDetails | null;
|
|
ready: boolean;
|
|
version: number;
|
|
}) {
|
|
const form = useForm({
|
|
...(Object.fromEntries(
|
|
Object.keys(identityFields).map((key) => [
|
|
key,
|
|
details?.[key as keyof typeof identityFields] ?? '',
|
|
]),
|
|
) as Record<keyof typeof identityFields, string>),
|
|
bank_accounts: details?.bank_accounts ?? [],
|
|
establishments: details?.establishments ?? [],
|
|
version,
|
|
reason: '',
|
|
});
|
|
return (
|
|
<Panel
|
|
title="Dados da empresa"
|
|
description="Identificação institucional, sede, sucursais e várias contas bancárias. Preencha os dados conhecidos. O cadastro bancário não valida contas nem autoriza pagamentos."
|
|
>
|
|
{!ready ? (
|
|
<p>
|
|
Instalação dos campos da empresa pendente. Os dados
|
|
existentes foram preservados.
|
|
</p>
|
|
) : (
|
|
<form
|
|
className="space-y-6"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
form.transform((values) => ({ ...values, version }));
|
|
form.post(
|
|
'/administracao/parametrizacao/company_details',
|
|
{ preserveScroll: true },
|
|
);
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<Fields
|
|
fields={identityFields}
|
|
values={Object.fromEntries(
|
|
Object.keys(identityFields).map((key) => [
|
|
key,
|
|
form.data[key as keyof typeof identityFields],
|
|
]),
|
|
)}
|
|
prefix="company"
|
|
change={(key, value) =>
|
|
form.setData(
|
|
key as keyof typeof identityFields,
|
|
value,
|
|
)
|
|
}
|
|
/>
|
|
<section
|
|
className="space-y-4"
|
|
aria-labelledby="company-establishments"
|
|
>
|
|
<h3 id="company-establishments" className="font-medium">
|
|
Sede e sucursais
|
|
</h3>
|
|
<p className="text-sm text-muted-foreground">
|
|
Identifique a sede uma única vez. Cada sucursal
|
|
conserva o seu endereço e contactos.
|
|
</p>
|
|
{form.data.establishments.length === 0 && (
|
|
<p className="text-sm">
|
|
Nenhum estabelecimento registado.
|
|
</p>
|
|
)}
|
|
{form.data.establishments.map((row, index) => (
|
|
<fieldset
|
|
className="min-w-0 space-y-4 rounded border p-4"
|
|
key={row.id}
|
|
>
|
|
<legend className="px-2">
|
|
Estabelecimento {index + 1}
|
|
</legend>
|
|
<label
|
|
className="grid gap-2 text-sm"
|
|
htmlFor={`establishment-${row.id}-kind`}
|
|
>
|
|
Tipo
|
|
<select
|
|
id={`establishment-${row.id}-kind`}
|
|
className={controlClass}
|
|
value={row.kind}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'establishments',
|
|
form.data.establishments.map(
|
|
(r) =>
|
|
r.id === row.id
|
|
? {
|
|
...r,
|
|
kind: e.target
|
|
.value as Establishment['kind'],
|
|
}
|
|
: r,
|
|
),
|
|
)
|
|
}
|
|
>
|
|
<option value="headquarters">
|
|
Sede
|
|
</option>
|
|
<option value="branch">Sucursal</option>
|
|
</select>
|
|
</label>
|
|
<Fields
|
|
fields={locationFields}
|
|
values={row}
|
|
prefix={`establishment-${row.id}`}
|
|
change={(key, value) =>
|
|
form.setData(
|
|
'establishments',
|
|
form.data.establishments.map((r) =>
|
|
r.id === row.id
|
|
? { ...r, [key]: value }
|
|
: r,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
</fieldset>
|
|
))}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() =>
|
|
form.setData('establishments', [
|
|
...form.data.establishments,
|
|
{
|
|
id: crypto.randomUUID(),
|
|
kind: form.data.establishments.some(
|
|
(r) => r.kind === 'headquarters',
|
|
)
|
|
? 'branch'
|
|
: 'headquarters',
|
|
name: '',
|
|
address: '',
|
|
phone: '',
|
|
email: '',
|
|
},
|
|
])
|
|
}
|
|
>
|
|
Adicionar estabelecimento
|
|
</Button>
|
|
</section>
|
|
<section
|
|
className="space-y-4"
|
|
aria-labelledby="company-bank-accounts"
|
|
>
|
|
<h3 id="company-bank-accounts" className="font-medium">
|
|
Contas bancárias da empresa
|
|
</h3>
|
|
{form.data.bank_accounts.length === 0 && (
|
|
<p className="text-sm">Nenhuma conta registada.</p>
|
|
)}
|
|
{form.data.bank_accounts.map((row, index) => (
|
|
<fieldset
|
|
className="min-w-0 space-y-4 rounded border p-4"
|
|
key={row.id}
|
|
>
|
|
<legend className="px-2">
|
|
Conta {index + 1}
|
|
</legend>
|
|
<Fields
|
|
fields={bankFields}
|
|
values={row}
|
|
prefix={`bank-${row.id}`}
|
|
change={(key, value) =>
|
|
form.setData(
|
|
'bank_accounts',
|
|
form.data.bank_accounts.map((r) =>
|
|
r.id === row.id
|
|
? { ...r, [key]: value }
|
|
: r,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
</fieldset>
|
|
))}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() =>
|
|
form.setData('bank_accounts', [
|
|
...form.data.bank_accounts,
|
|
{
|
|
id: crypto.randomUUID(),
|
|
bank: '',
|
|
branch: '',
|
|
account_number: '',
|
|
currency: '',
|
|
nib: '',
|
|
iban: '',
|
|
nuib: '',
|
|
account_holder: '',
|
|
},
|
|
])
|
|
}
|
|
>
|
|
Adicionar conta bancária
|
|
</Button>
|
|
</section>
|
|
<label
|
|
className="grid gap-2 text-sm"
|
|
htmlFor="company-details-reason"
|
|
>
|
|
Motivo da alteração
|
|
<textarea
|
|
id="company-details-reason"
|
|
className={controlClass}
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
value={form.data.reason}
|
|
onChange={(e) =>
|
|
form.setData('reason', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>
|
|
Guardar dados da empresa
|
|
</Button>
|
|
</form>
|
|
)}
|
|
</Panel>
|
|
);
|
|
}
|