274 lines
12 KiB
TypeScript
274 lines
12 KiB
TypeScript
import { Head, Link, useForm } from '@inertiajs/react';
|
|
import { useEffect, useRef } from 'react';
|
|
import {
|
|
PageTitle,
|
|
Panel,
|
|
FormErrors,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
import { exactMinorAmount } from '@/components/procurement-payable-insights';
|
|
|
|
type Allocation = { category: string; amount_minor: string };
|
|
type RecordVersion = {
|
|
id: number;
|
|
version: number;
|
|
reason: string;
|
|
data: { allocations: Allocation[] };
|
|
};
|
|
type Props = {
|
|
payment: {
|
|
id: number;
|
|
invoice_id: number;
|
|
invoice_number: string;
|
|
currency: string;
|
|
amount_minor: string;
|
|
};
|
|
categories: string[];
|
|
fingerprint: string;
|
|
current: RecordVersion | null;
|
|
history: {
|
|
data: RecordVersion[];
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
canWrite: boolean;
|
|
actorId: number;
|
|
tenantId: number;
|
|
};
|
|
const decimal = (value: string) => {
|
|
const digits = value.padStart(3, '0');
|
|
return `${digits.slice(0, -2)}.${digits.slice(-2)}`;
|
|
};
|
|
|
|
export default function ExpenseAllocation(props: Props) {
|
|
const payment = props.payment;
|
|
const form = useForm(
|
|
`expense-allocation:${props.tenantId}:${props.actorId}:${payment.id}`,
|
|
{
|
|
operation_key: '',
|
|
version: props.current?.version || 0,
|
|
fingerprint: props.fingerprint,
|
|
reason: '',
|
|
allocations: props.categories.map((category) => ({
|
|
category,
|
|
amount: '',
|
|
})),
|
|
},
|
|
);
|
|
const ref = useRef<HTMLFormElement>(null);
|
|
useEffect(() => {
|
|
const node = ref.current;
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', {
|
|
detail: { form: node, dirty: form.isDirty },
|
|
}),
|
|
);
|
|
return () => {
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', {
|
|
detail: { form: node, dirty: false },
|
|
}),
|
|
);
|
|
};
|
|
}, [form.isDirty]);
|
|
const current =
|
|
form.data.version === (props.current?.version || 0) &&
|
|
form.data.fingerprint === props.fingerprint;
|
|
return (
|
|
<div className="mx-auto grid w-full max-w-6xl gap-6 p-5 lg:p-8">
|
|
<Head title="Imputação da despesa por categoria" />
|
|
<PageTitle
|
|
title="Imputação da despesa por categoria"
|
|
eyebrow={`Pagamento #${payment.id} · ${payment.invoice_number}`}
|
|
description="Finanças atribui explicitamente o pagamento confirmado às categorias das linhas da factura. A soma deve coincidir exactamente com o pagamento; o pagamento e o saldo permanecem preservados."
|
|
/>
|
|
<Link
|
|
className="underline"
|
|
href={`/pagamentos?payment_id=${payment.id}`}
|
|
>
|
|
Voltar ao pagamento
|
|
</Link>
|
|
<Panel
|
|
title={`Pagamento confirmado: ${exactMinorAmount(payment.amount_minor, payment.currency)}`}
|
|
>
|
|
{props.current ? (
|
|
<>
|
|
<h3 className="font-semibold">
|
|
Versão actual {props.current.version}
|
|
</h3>
|
|
<p>{props.current.reason}</p>
|
|
<ul>
|
|
{props.current.data.allocations.map((row) => (
|
|
<li key={row.category}>
|
|
{row.category}:{' '}
|
|
{exactMinorAmount(
|
|
row.amount_minor,
|
|
payment.currency,
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</>
|
|
) : (
|
|
<p>
|
|
Ainda sem imputação por categoria. O relatório conserva
|
|
esta despesa como não imputada.
|
|
</p>
|
|
)}
|
|
</Panel>
|
|
{props.canWrite && (
|
|
<Panel title="Preparar imputação explícita">
|
|
<form
|
|
ref={ref}
|
|
className="space-y-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const operation =
|
|
form.data.operation_key || crypto.randomUUID();
|
|
form.setData('operation_key', operation);
|
|
form.transform((data) => ({
|
|
...data,
|
|
operation_key: operation,
|
|
allocations: data.allocations.filter(
|
|
(row) => row.amount !== '',
|
|
),
|
|
}));
|
|
form.post(`/pagamentos/${payment.id}/categorias`, {
|
|
preserveScroll: true,
|
|
onSuccess: (page) => {
|
|
const saved = page.props
|
|
.current as RecordVersion;
|
|
const data = {
|
|
...form.data,
|
|
operation_key: '',
|
|
reason: '',
|
|
version: saved.version,
|
|
fingerprint: page.props
|
|
.fingerprint as string,
|
|
};
|
|
form.setData(data);
|
|
form.setDefaults(data);
|
|
form.clearErrors();
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
{!current && (
|
|
<p role="status">
|
|
A versão ou a origem mudou. Reveja os valores
|
|
actuais antes de preparar outra imputação.
|
|
</p>
|
|
)}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() =>
|
|
form.setData((data) => ({
|
|
...data,
|
|
version: props.current?.version || 0,
|
|
fingerprint: props.fingerprint,
|
|
operation_key: '',
|
|
allocations: props.categories.map(
|
|
(category) => ({
|
|
category,
|
|
amount: props.current?.data.allocations.find(
|
|
(row) =>
|
|
row.category === category,
|
|
)
|
|
? decimal(
|
|
props.current.data.allocations.find(
|
|
(row) =>
|
|
row.category ===
|
|
category,
|
|
)!.amount_minor,
|
|
)
|
|
: '',
|
|
}),
|
|
),
|
|
}))
|
|
}
|
|
>
|
|
Usar a imputação actual como preparação
|
|
</Button>
|
|
{form.data.allocations.map((row, index) => (
|
|
<label key={row.category} className="grid gap-2">
|
|
{row.category} · {payment.currency}
|
|
<input
|
|
className={controlClass}
|
|
inputMode="decimal"
|
|
value={row.amount}
|
|
onChange={(event) =>
|
|
form.setData(
|
|
'allocations',
|
|
form.data.allocations.map(
|
|
(entry, i) =>
|
|
i === index
|
|
? {
|
|
...entry,
|
|
amount: event
|
|
.target.value,
|
|
}
|
|
: entry,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
))}
|
|
<label className="grid gap-2">
|
|
Fundamento
|
|
<textarea
|
|
className={controlClass}
|
|
required
|
|
maxLength={4000}
|
|
value={form.data.reason}
|
|
onChange={(event) =>
|
|
form.setData('reason', event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing || !current}>
|
|
Guardar imputação
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
)}
|
|
<Panel title="Histórico das imputações">
|
|
{props.history.data.length === 0 && (
|
|
<p>Ainda não há versões registadas.</p>
|
|
)}
|
|
{props.history.data.map((row) => (
|
|
<article className="border-b py-3" key={row.id}>
|
|
<h3 className="font-semibold">Versão {row.version}</h3>
|
|
<p>{row.reason}</p>
|
|
<ul>
|
|
{row.data.allocations.map((entry) => (
|
|
<li key={entry.category}>
|
|
{entry.category}:{' '}
|
|
{exactMinorAmount(
|
|
entry.amount_minor,
|
|
payment.currency,
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</article>
|
|
))}
|
|
<nav
|
|
className="mt-3 flex gap-4"
|
|
aria-label="Versões da imputação"
|
|
>
|
|
{props.history.prev_page_url && (
|
|
<Link href={props.history.prev_page_url}>Anterior</Link>
|
|
)}
|
|
{props.history.next_page_url && (
|
|
<Link href={props.history.next_page_url}>Seguinte</Link>
|
|
)}
|
|
</nav>
|
|
</Panel>
|
|
</div>
|
|
);
|
|
}
|