commit melhorias compras4
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-22 16:28:03 +02:00
parent efd97e265b
commit 36dcfdf00c
118 changed files with 6042 additions and 92 deletions
@@ -0,0 +1,262 @@
import { Head, Link, useForm, usePage } from '@inertiajs/react';
import { useEffect, useRef } from 'react';
import { hasPreparationChanges } from '@/lib/requisition-history';
import {
FormErrors,
PageTitle,
Panel,
controlClass,
} from '@/components/procurement';
import { Button } from '@/components/ui/button';
import { money } from '@/types/procurement';
import type { Requisition } from '@/types/procurement';
type Allocation = {
allocation_key: string;
line_id: number;
centre_id: number;
subtotal_minor: number;
total_minor: number;
};
const decimal = (value: number) =>
`${Math.floor(value / 100)}.${String(value % 100).padStart(2, '0')}`;
export default function Distributions({
requisition: r,
centres,
preparation,
}: {
requisition: Omit<Requisition, 'items'> & {
items: (Requisition['items'][number] & { id: number })[];
};
centres: { id: number; code: string; name: string }[];
preparation: { allocations: Allocation[]; reason: string } | null;
}) {
const actor = usePage().props.auth?.user;
const initial = {
operation_key: crypto.randomUUID(),
process_version: r.process_version,
reason: '',
budget_distribution_preparation: (preparation?.allocations ?? []).map(
(row) => ({
allocation_key: row.allocation_key,
line_id: row.line_id,
centre_id: row.centre_id,
subtotal: decimal(row.subtotal_minor),
total: decimal(row.total_minor),
}),
),
};
const form = useForm(
`distribution:${actor?.tenant_id}:${actor?.id}:${r.id}:${r.process_version}`,
initial,
);
const formElement = useRef<HTMLFormElement>(null);
const dirty = hasPreparationChanges(initial, form.data);
useEffect(() => {
window.dispatchEvent(
new CustomEvent('sgc:context-dirty', {
detail: { form: formElement.current, dirty },
}),
);
}, [dirty]);
const rows = form.data.budget_distribution_preparation;
return (
<>
<Head title={`Distribuição · ${r.number}`} />
<div className="mx-auto flex w-full max-w-5xl flex-col gap-6 p-5 lg:p-8">
<PageTitle
eyebrow="Requisição de compra"
title="Distribuir por centros de custo"
description={r.number}
/>
<p>
Indique os valores de cada artigo por centro. As parcelas
devem corresponder exactamente ao valor sem IVA e ao total
do artigo. Guardar prepara a distribuição; a reserva ocorre
ao submeter o pedido.
</p>
<FormErrors errors={form.errors} />
<form
ref={formElement}
className="flex flex-col gap-6"
onSubmit={(event) => {
event.preventDefault();
form.post(`/requisicoes/${r.id}/distribuicoes`);
}}
>
{r.items.map((line) => (
<Panel key={line.id} title={line.description}>
<p>
Sem IVA: {money(line.subtotal_minor)} · Total:{' '}
{money(line.total_minor)}
</p>
{rows.map((row, index) =>
row.line_id !== line.id ? null : (
<fieldset
key={row.allocation_key}
className="my-4 grid gap-3 rounded border p-3 sm:grid-cols-3"
>
<legend>
Parcela{' '}
{
rows.filter(
(item, i) =>
i <= index &&
item.line_id ===
line.id,
).length
}
</legend>
<label>
Centro de custo
<select
required
className={controlClass}
value={row.centre_id || ''}
onChange={(event) =>
form.setData(
'budget_distribution_preparation',
rows.map((item, i) =>
i === index
? {
...item,
centre_id:
Number(
event
.target
.value,
),
}
: item,
),
)
}
>
<option value="">
Seleccione
</option>
{centres.map((centre) => (
<option
key={centre.id}
value={centre.id}
>
{centre.code} ·{' '}
{centre.name}
</option>
))}
</select>
</label>
{(['subtotal', 'total'] as const).map(
(field) => (
<label key={field}>
{field === 'subtotal'
? 'Sem IVA (MZN)'
: 'Total (MZN)'}
<input
required
inputMode="decimal"
pattern="[0-9]+([.][0-9]{1,2})?"
className={controlClass}
value={row[field]}
onChange={(event) =>
form.setData(
'budget_distribution_preparation',
rows.map(
(
item,
i,
) =>
i ===
index
? {
...item,
[field]:
event
.target
.value,
}
: item,
),
)
}
/>
</label>
),
)}
<Button
type="button"
variant="outline"
onClick={() =>
form.setData(
'budget_distribution_preparation',
rows.filter(
(_, i) => i !== index,
),
)
}
>
Remover parcela
</Button>
</fieldset>
),
)}
<Button
type="button"
variant="outline"
disabled={rows.length >= 500}
onClick={() =>
form.setData(
'budget_distribution_preparation',
[
...rows,
{
allocation_key:
crypto.randomUUID(),
line_id: line.id,
centre_id: 0,
subtotal: '',
total: '',
},
],
)
}
>
Adicionar parcela
</Button>
</Panel>
))}
<label>
Motivo da distribuição
<textarea
required
minLength={10}
maxLength={4000}
className={controlClass}
value={form.data.reason}
onChange={(event) =>
form.setData('reason', event.target.value)
}
/>
</label>
<p className="text-sm">
Se os artigos ou valores forem alterados, será
necessário conferir novamente a distribuição. Use ponto
para separar os centavos. Nenhuma diferença será
compensada automaticamente.
</p>
<div className="flex flex-wrap gap-3">
<Button disabled={form.processing || rows.length === 0}>
Guardar distribuição
</Button>
<Button asChild variant="outline">
<Link href={`/requisicoes/${r.id}`}>
Voltar ao pedido
</Link>
</Button>
</div>
</form>
</div>
</>
);
}