@@ -0,0 +1,273 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
@@ -99,12 +99,14 @@ export default function Payments({
|
||||
invoices,
|
||||
payments,
|
||||
canManage,
|
||||
expenseAllocationsReady = false,
|
||||
focusPaymentId = null,
|
||||
focusInvoiceId = null,
|
||||
}: {
|
||||
invoices: Invoice[];
|
||||
payments: Page<Payment>;
|
||||
canManage: boolean;
|
||||
expenseAllocationsReady?: boolean;
|
||||
focusPaymentId?: number | null;
|
||||
focusInvoiceId?: number | null;
|
||||
}) {
|
||||
@@ -383,6 +385,15 @@ export default function Payments({
|
||||
</p>
|
||||
)}
|
||||
{p.canReview && <Review id={p.id} />}
|
||||
{expenseAllocationsReady &&
|
||||
p.status === 'confirmed' && (
|
||||
<Link
|
||||
className="mt-3 inline-block underline"
|
||||
href={`/pagamentos/${p.id}/categorias`}
|
||||
>
|
||||
Imputação por categoria
|
||||
</Link>
|
||||
)}
|
||||
</Panel>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -60,7 +60,16 @@ type ComparisonRow = {
|
||||
is_top: boolean;
|
||||
weights: Record<string, number>;
|
||||
};
|
||||
type ReportOrder = {
|
||||
id: number;
|
||||
number: string;
|
||||
currency: string;
|
||||
total_minor: number;
|
||||
agreement_limits?: Record<string, FrozenOrderAgreement>;
|
||||
origins?: { id: number; number: string }[];
|
||||
};
|
||||
type Report = {
|
||||
orders?: ReportOrder[];
|
||||
agreement_limits?: Record<string, FrozenOrderAgreement>;
|
||||
generated_at: string;
|
||||
generated_by: string;
|
||||
@@ -112,14 +121,10 @@ type Report = {
|
||||
source_changed: boolean;
|
||||
} | null;
|
||||
award: { id: number; status: string; total_minor: number } | null;
|
||||
order: {
|
||||
id: number;
|
||||
number: string;
|
||||
currency: string;
|
||||
total_minor: number;
|
||||
} | null;
|
||||
order: ReportOrder | null;
|
||||
fulfilment: {
|
||||
receipts: {
|
||||
order_number?: string;
|
||||
id: number;
|
||||
received_on: string | null;
|
||||
delivery_note: string;
|
||||
@@ -129,6 +134,7 @@ type Report = {
|
||||
| {
|
||||
id: number;
|
||||
number: string;
|
||||
order_number?: string;
|
||||
status: string;
|
||||
issued_on: string;
|
||||
total_minor: string;
|
||||
@@ -704,17 +710,48 @@ export default function ReviewReport({
|
||||
) : (
|
||||
<p>Sem adjudicação registada.</p>
|
||||
)}
|
||||
{report.order ? (
|
||||
<p>
|
||||
Ordem {report.order.number} · Valor preservado:{' '}
|
||||
{money(
|
||||
report.order.total_minor,
|
||||
report.order.currency,
|
||||
{(report.orders ?? (report.order ? [report.order] : []))
|
||||
.length === 0 && (
|
||||
<p>Sem ordem visível nesta consulta.</p>
|
||||
)}
|
||||
{(
|
||||
report.orders ??
|
||||
(report.order ? [report.order] : [])
|
||||
).map((order) => (
|
||||
<p key={order.id}>
|
||||
Ordem{' '}
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/ordens/${order.id}`}
|
||||
>
|
||||
{order.number}
|
||||
</Link>{' '}
|
||||
· Valor documental integral:{' '}
|
||||
{money(order.total_minor, order.currency)}.
|
||||
{(order.origins?.length ?? 0) > 1 && (
|
||||
<span className="mt-1 block text-sm">
|
||||
Requisições de origem:{' '}
|
||||
{order.origins?.map((origin) => (
|
||||
<Link
|
||||
key={origin.id}
|
||||
className="mr-3 underline"
|
||||
href={`/requisicoes/${origin.id}`}
|
||||
>
|
||||
{origin.number}
|
||||
</Link>
|
||||
))}
|
||||
</span>
|
||||
)}
|
||||
.
|
||||
</p>
|
||||
) : (
|
||||
<p>Sem ordem emitida.</p>
|
||||
))}
|
||||
{report.orders?.some(
|
||||
(order) => (order.origins?.length ?? 0) > 1,
|
||||
) && (
|
||||
<p>
|
||||
As ordens consolidadas e os seus documentos
|
||||
podem abranger outros pedidos. Os montantes são
|
||||
integrais, sem repartição neste relatório.
|
||||
</p>
|
||||
)}
|
||||
<p className="text-muted-foreground">
|
||||
Valores de documentos emitidos ou da adjudicação são
|
||||
@@ -723,9 +760,29 @@ export default function ReviewReport({
|
||||
</p>
|
||||
</div>
|
||||
</Panel>
|
||||
<AgreementOrderLimitSummary
|
||||
bindings={report.agreement_limits}
|
||||
/>
|
||||
{report.orders ? (
|
||||
report.orders.map(
|
||||
(order) =>
|
||||
Object.keys(order.agreement_limits || {}).length >
|
||||
0 && (
|
||||
<section
|
||||
key={order.id}
|
||||
aria-label={`Referências contratuais da ordem ${order.number}`}
|
||||
>
|
||||
<h3 className="mb-3 font-semibold">
|
||||
Ordem {order.number}
|
||||
</h3>
|
||||
<AgreementOrderLimitSummary
|
||||
bindings={order.agreement_limits}
|
||||
/>
|
||||
</section>
|
||||
),
|
||||
)
|
||||
) : (
|
||||
<AgreementOrderLimitSummary
|
||||
bindings={report.agreement_limits}
|
||||
/>
|
||||
)}
|
||||
<Panel
|
||||
title="Recepção, facturas e registos de pagamento"
|
||||
id="report-fulfilment"
|
||||
@@ -735,7 +792,7 @@ export default function ReviewReport({
|
||||
<section aria-label="Recepções do processo">
|
||||
<h3 className="font-medium">Recepções</h3>
|
||||
{report.fulfilment.receipts.length === 0 ? (
|
||||
<p>Sem recepções registadas.</p>
|
||||
<p>Sem recepções visíveis nesta consulta.</p>
|
||||
) : (
|
||||
<ul className="mt-2 space-y-2">
|
||||
{report.fulfilment.receipts.map(
|
||||
@@ -746,6 +803,13 @@ export default function ReviewReport({
|
||||
? date(receipt.received_on)
|
||||
: 'Data por indicar'}{' '}
|
||||
· Guia: {receipt.delivery_note}.{' '}
|
||||
{receipt.order_number && (
|
||||
<>
|
||||
Ordem{' '}
|
||||
{receipt.order_number}
|
||||
.{' '}
|
||||
</>
|
||||
)}
|
||||
<Link
|
||||
className="underline print:hidden"
|
||||
href={`/recepcoes/${receipt.id}`}
|
||||
@@ -768,13 +832,24 @@ export default function ReviewReport({
|
||||
<section aria-label="Facturas do processo">
|
||||
<h3 className="font-medium">Facturas</h3>
|
||||
{!report.fulfilment.invoices?.length ? (
|
||||
<p>Sem facturas registadas.</p>
|
||||
<p>
|
||||
Sem facturas visíveis nesta
|
||||
consulta.
|
||||
</p>
|
||||
) : (
|
||||
<ul className="mt-2 space-y-2">
|
||||
{report.fulfilment.invoices.map(
|
||||
(invoice) => (
|
||||
<li key={invoice.id}>
|
||||
Factura {invoice.number}{' '}
|
||||
{invoice.order_number && (
|
||||
<>
|
||||
· Ordem{' '}
|
||||
{
|
||||
invoice.order_number
|
||||
}{' '}
|
||||
</>
|
||||
)}
|
||||
·{' '}
|
||||
{date(
|
||||
invoice.issued_on,
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import {
|
||||
ProcurementDeliveryInsights,
|
||||
type DeliveryInsights,
|
||||
} from '@/components/procurement-delivery-insights';
|
||||
import {
|
||||
ProcurementContractInsights,
|
||||
type ContractInsights,
|
||||
} from '@/components/procurement-contract-insights';
|
||||
import {
|
||||
ProcurementPaymentInsights,
|
||||
type PaymentInsights,
|
||||
@@ -41,6 +49,10 @@ type Filters = {
|
||||
from?: string;
|
||||
to?: string;
|
||||
department_id?: string | number;
|
||||
supplier_page?: number;
|
||||
category_page?: number;
|
||||
delivery_page?: number;
|
||||
delivery_state?: string;
|
||||
};
|
||||
const estimate = (minor: string | null) => {
|
||||
if (minor === null) return 'Por completar';
|
||||
@@ -56,6 +68,8 @@ export default function ProcurementReports({
|
||||
insights,
|
||||
payableInsights,
|
||||
paymentInsights,
|
||||
contractInsights,
|
||||
deliveryInsights,
|
||||
}: {
|
||||
records: {
|
||||
data: Row[];
|
||||
@@ -72,6 +86,8 @@ export default function ProcurementReports({
|
||||
insights?: ProcurementInsights;
|
||||
payableInsights?: PayableInsights | null;
|
||||
paymentInsights?: PaymentInsights | null;
|
||||
contractInsights?: ContractInsights;
|
||||
deliveryInsights?: DeliveryInsights;
|
||||
}) {
|
||||
const form = useForm({
|
||||
search: filters.search ?? '',
|
||||
@@ -122,10 +138,24 @@ export default function ProcurementReports({
|
||||
exportHref={`/relatorios/obrigacoes.csv?${params}`}
|
||||
/>
|
||||
)}
|
||||
{deliveryInsights && (
|
||||
<ProcurementDeliveryInsights
|
||||
data={deliveryInsights}
|
||||
pageHref={`/relatorios?${params}`}
|
||||
exportHref={`/relatorios/entregas.csv?${params}`}
|
||||
/>
|
||||
)}
|
||||
{paymentInsights && (
|
||||
<ProcurementPaymentInsights
|
||||
data={paymentInsights}
|
||||
exportHref={`/relatorios/despesa.csv?${params}`}
|
||||
pageHref={`/relatorios?${params}`}
|
||||
/>
|
||||
)}
|
||||
{contractInsights && (
|
||||
<ProcurementContractInsights
|
||||
data={contractInsights}
|
||||
exportHref={`/relatorios/contratos.csv?${params}`}
|
||||
/>
|
||||
)}
|
||||
<Panel
|
||||
|
||||
Reference in New Issue
Block a user