264 lines
11 KiB
TypeScript
264 lines
11 KiB
TypeScript
import { Head, Link, useForm, usePage } from '@inertiajs/react';
|
|
import { useEffect, useRef } from 'react';
|
|
import { FormErrors, PageTitle, Panel } from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { money } from '@/types/procurement';
|
|
|
|
type Source = {
|
|
origin: number;
|
|
version: number;
|
|
plan_hash: string;
|
|
allocations: {
|
|
allocation_key: string;
|
|
line_id: number;
|
|
centre_id: number;
|
|
subtotal: string;
|
|
total: string;
|
|
}[];
|
|
};
|
|
|
|
export default function Amendment({
|
|
order,
|
|
sources,
|
|
history,
|
|
}: {
|
|
order: { id: number; number: string; currency: string };
|
|
sources: Source[];
|
|
history: {
|
|
id: number;
|
|
created_at: string;
|
|
created_by: number;
|
|
reason: string | null;
|
|
sources: {
|
|
origin: number;
|
|
allocations: {
|
|
allocation_key: string;
|
|
subtotal_minor: number;
|
|
total_minor: number;
|
|
}[];
|
|
}[];
|
|
}[];
|
|
}) {
|
|
const actor = usePage().props.auth?.user;
|
|
const form = useForm(
|
|
`order-parts-amendment:${actor?.tenant_id}:${actor?.id}:${order.id}:${sources.map((s) => `${s.origin}:${s.version}:${s.plan_hash}`).join(',')}`,
|
|
{
|
|
operation_key: crypto.randomUUID(),
|
|
reason: '',
|
|
sources: sources.map((source) => ({
|
|
origin: source.origin,
|
|
version: source.version,
|
|
plan_hash: source.plan_hash,
|
|
allocations: source.allocations.map(
|
|
({ allocation_key, subtotal, total }) => ({
|
|
allocation_key,
|
|
subtotal,
|
|
total,
|
|
}),
|
|
),
|
|
})),
|
|
},
|
|
);
|
|
const ref = useRef<HTMLFormElement>(null);
|
|
useEffect(() => {
|
|
const current = ref.current;
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', {
|
|
detail: { form: current, dirty: form.isDirty },
|
|
}),
|
|
);
|
|
return () => {
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', {
|
|
detail: { form: current, dirty: false },
|
|
}),
|
|
);
|
|
};
|
|
}, [form.isDirty]);
|
|
const update = (
|
|
origin: number,
|
|
allocation: number,
|
|
field: 'subtotal' | 'total',
|
|
value: string,
|
|
) => {
|
|
form.setData(
|
|
'sources',
|
|
form.data.sources.map((source, i) =>
|
|
i !== origin
|
|
? source
|
|
: {
|
|
...source,
|
|
allocations: source.allocations.map((row, j) =>
|
|
j !== allocation
|
|
? row
|
|
: { ...row, [field]: value },
|
|
),
|
|
},
|
|
),
|
|
);
|
|
};
|
|
return (
|
|
<>
|
|
<Head title={`Reafectação · ${order.number}`} />
|
|
<PageTitle
|
|
eyebrow="Compras · Finanças"
|
|
title={`Reafectação · ${order.number}`}
|
|
description="Confirmação explícita de Finanças para esta ordem."
|
|
/>
|
|
<Panel title="Imputações actuais em MZN">
|
|
<p className="mb-4">
|
|
Conserve os montantes de cada artigo e origem. Apenas os
|
|
centros já previstos podem ser reafectados. Valores
|
|
consumidos, reservas não emitidas e documentos originais são
|
|
preservados.
|
|
</p>
|
|
<form
|
|
ref={ref}
|
|
className="grid gap-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
form.post(`/ordens/${order.id}/reafectacao`);
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
{sources.map((source, i) => (
|
|
<fieldset
|
|
key={source.origin}
|
|
className="grid min-w-0 gap-3 rounded border p-4"
|
|
>
|
|
<legend className="px-2 font-semibold">
|
|
Origem #{source.origin} · Versão{' '}
|
|
{source.version}
|
|
</legend>
|
|
{source.allocations.map((allocation, j) => (
|
|
<div
|
|
key={allocation.allocation_key}
|
|
className="grid gap-3 rounded border p-3 sm:grid-cols-2"
|
|
>
|
|
<p className="sm:col-span-2">
|
|
Artigo #{allocation.line_id} · Centro #
|
|
{allocation.centre_id}
|
|
</p>
|
|
<label className="grid gap-1">
|
|
Sem IVA (MZN)
|
|
<Input
|
|
required
|
|
inputMode="decimal"
|
|
value={
|
|
form.data.sources[i]
|
|
.allocations[j].subtotal
|
|
}
|
|
onChange={(e) =>
|
|
update(
|
|
i,
|
|
j,
|
|
'subtotal',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Com IVA (MZN)
|
|
<Input
|
|
required
|
|
inputMode="decimal"
|
|
value={
|
|
form.data.sources[i]
|
|
.allocations[j].total
|
|
}
|
|
onChange={(e) =>
|
|
update(
|
|
i,
|
|
j,
|
|
'total',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
</div>
|
|
))}
|
|
</fieldset>
|
|
))}
|
|
<label className="grid gap-1">
|
|
Motivo
|
|
<textarea
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
className="min-h-24 rounded border p-3"
|
|
value={form.data.reason}
|
|
onChange={(e) =>
|
|
form.setData('reason', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing} type="submit">
|
|
Confirmar reafectação por Finanças
|
|
</Button>
|
|
<Link className="underline" href={`/ordens/${order.id}`}>
|
|
Voltar à ordem
|
|
</Link>
|
|
</form>
|
|
</Panel>
|
|
<Panel title="Histórico de reafectações">
|
|
{history.length === 0 ? (
|
|
<p>Sem reafectações registadas.</p>
|
|
) : (
|
|
history.map((event) => (
|
|
<div key={event.id} className="mb-4 rounded border p-3">
|
|
<p>
|
|
Reafectação #{event.id} · {event.created_at} ·
|
|
Interveniente #{event.created_by}
|
|
</p>
|
|
{event.reason && (
|
|
<p className="whitespace-pre-wrap">
|
|
{event.reason}
|
|
</p>
|
|
)}
|
|
<details>
|
|
<summary>
|
|
Consultar imputações conservadas
|
|
</summary>
|
|
{event.sources.map((source) => (
|
|
<div key={source.origin}>
|
|
<p>Origem #{source.origin}</p>
|
|
{source.allocations.map((row) => (
|
|
<p key={row.allocation_key}>
|
|
Centro #
|
|
{
|
|
sources
|
|
.find(
|
|
(s) =>
|
|
s.origin ===
|
|
source.origin,
|
|
)
|
|
?.allocations.find(
|
|
(a) =>
|
|
a.allocation_key ===
|
|
row.allocation_key,
|
|
)?.centre_id
|
|
}{' '}
|
|
·{' '}
|
|
{money(
|
|
row.subtotal_minor,
|
|
'MZN',
|
|
)}{' '}
|
|
sem IVA ·{' '}
|
|
{money(row.total_minor, 'MZN')}{' '}
|
|
com IVA
|
|
</p>
|
|
))}
|
|
</div>
|
|
))}
|
|
</details>
|
|
</div>
|
|
))
|
|
)}
|
|
</Panel>
|
|
</>
|
|
);
|
|
}
|