456 lines
19 KiB
TypeScript
456 lines
19 KiB
TypeScript
import { InventoryEventActions } from '@/components/inventory-event-actions';
|
|
import { Head, Link, useForm } from '@inertiajs/react';
|
|
import {
|
|
FormErrors,
|
|
PageTitle,
|
|
Panel,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
type Source = {
|
|
item: { code: string; name: string; unit: string };
|
|
warehouse: { name: string };
|
|
lot: { code: string | null } | null;
|
|
receipt: {
|
|
number: string;
|
|
supplier_name: string;
|
|
order_number: string;
|
|
} | null;
|
|
};
|
|
type RecordEntry = {
|
|
id: number;
|
|
record_key: string;
|
|
version: number;
|
|
state: string;
|
|
reason: string;
|
|
created_at: string;
|
|
data: {
|
|
kind: string;
|
|
stock_item_id: number;
|
|
warehouse_id: number;
|
|
lot_id: number | null;
|
|
receipt_item_id: number | null;
|
|
quantity_milli: number;
|
|
reference: string;
|
|
stock_operation_id?: number;
|
|
event?: { event_on: string; evidence: string };
|
|
};
|
|
snapshot: {
|
|
source: Source;
|
|
contributors: number[];
|
|
reviewed_by?: number;
|
|
actor: { name: string };
|
|
};
|
|
};
|
|
type Props = {
|
|
canViewReversals: boolean;
|
|
correctedOperations?: Record<number, string>;
|
|
canViewOperations: boolean;
|
|
ready: boolean;
|
|
today: string;
|
|
actorId: number;
|
|
capabilities: Record<string, boolean>;
|
|
records: RecordEntry[];
|
|
history: Record<string, RecordEntry[]>;
|
|
items: { id: number; code: string; name: string; unit: string }[];
|
|
warehouses: { id: number; code: string; name: string }[];
|
|
receipts: {
|
|
id: number;
|
|
stock_item_id: number;
|
|
warehouse_id: number;
|
|
accepted_milli: number;
|
|
returned_milli: number;
|
|
label: string;
|
|
}[];
|
|
lots: {
|
|
id: number;
|
|
stock_item_id: number;
|
|
warehouse_id: number;
|
|
code: string | null;
|
|
quantity_milli: number;
|
|
receipt_item_id: number | null;
|
|
reconciled: boolean;
|
|
}[];
|
|
};
|
|
const quantity = (n: number) =>
|
|
(n / 1000).toLocaleString('pt-MZ', { maximumFractionDigits: 3 });
|
|
const states: Record<string, string> = {
|
|
draft: 'Em preparação',
|
|
returned: 'Devolvido para actualização',
|
|
approved: 'Aprovado para saída física',
|
|
executed: 'Saída física registada',
|
|
};
|
|
|
|
function Prepare({ props, old }: { props: Props; old?: RecordEntry }) {
|
|
const form = useForm({
|
|
operation_key: crypto.randomUUID(),
|
|
record_key: old?.record_key,
|
|
version: old?.version ?? 0,
|
|
action: 'save',
|
|
reason: '',
|
|
data: {
|
|
kind: old?.data.kind ?? 'supplier_return',
|
|
stock_item_id: old ? String(old.data.stock_item_id) : '',
|
|
warehouse_id: old ? String(old.data.warehouse_id) : '',
|
|
lot_id: old?.data.lot_id ? String(old.data.lot_id) : '',
|
|
receipt_item_id: old?.data.receipt_item_id
|
|
? String(old.data.receipt_item_id)
|
|
: '',
|
|
quantity: old ? String(old.data.quantity_milli / 1000) : '',
|
|
reference: old?.data.reference ?? '',
|
|
},
|
|
});
|
|
const change = (update: Partial<typeof form.data.data>) =>
|
|
form.setData('data', { ...form.data.data, ...update });
|
|
const lots = props.lots.filter(
|
|
(l) =>
|
|
String(l.stock_item_id) === form.data.data.stock_item_id &&
|
|
String(l.warehouse_id) === form.data.data.warehouse_id &&
|
|
(form.data.data.kind !== 'supplier_return' ||
|
|
String(l.receipt_item_id) === form.data.data.receipt_item_id),
|
|
);
|
|
return (
|
|
<form
|
|
className="grid gap-4 md:grid-cols-2"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
form.post('/stock/ocorrencias');
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<label className="grid gap-1 text-sm">
|
|
Operação
|
|
<select
|
|
className={controlClass}
|
|
value={form.data.data.kind}
|
|
onChange={(e) =>
|
|
change({
|
|
kind: e.target.value,
|
|
lot_id: '',
|
|
receipt_item_id: '',
|
|
stock_item_id: '',
|
|
warehouse_id: '',
|
|
})
|
|
}
|
|
>
|
|
<option value="supplier_return">
|
|
Devolução ao fornecedor
|
|
</option>
|
|
<option value="write_off">Abate físico</option>
|
|
</select>
|
|
</label>
|
|
{form.data.data.kind === 'supplier_return' && (
|
|
<label className="grid gap-1 text-sm">
|
|
Recepção de origem
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={form.data.data.receipt_item_id}
|
|
onChange={(e) => {
|
|
const source = props.receipts.find(
|
|
(r) => String(r.id) === e.target.value,
|
|
);
|
|
change({
|
|
receipt_item_id: e.target.value,
|
|
stock_item_id: source
|
|
? String(source.stock_item_id)
|
|
: '',
|
|
warehouse_id: source
|
|
? String(source.warehouse_id)
|
|
: '',
|
|
lot_id: '',
|
|
});
|
|
}}
|
|
>
|
|
<option value="">Seleccione a recepção aceite</option>
|
|
{props.receipts.map((r) => (
|
|
<option key={r.id} value={r.id}>
|
|
{r.label} · Devolvível da origem:{' '}
|
|
{quantity(r.accepted_milli - r.returned_milli)}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
)}
|
|
<label className="grid gap-1 text-sm">
|
|
Artigo
|
|
<select
|
|
required
|
|
disabled={form.data.data.kind === 'supplier_return'}
|
|
className={controlClass}
|
|
value={form.data.data.stock_item_id}
|
|
onChange={(e) =>
|
|
change({ stock_item_id: e.target.value, lot_id: '' })
|
|
}
|
|
>
|
|
<option value="">Seleccione</option>
|
|
{props.items.map((i) => (
|
|
<option key={i.id} value={i.id}>
|
|
{i.code} · {i.name} ({i.unit})
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="grid gap-1 text-sm">
|
|
Local da saída
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={form.data.data.warehouse_id}
|
|
onChange={(e) =>
|
|
change({ warehouse_id: e.target.value, lot_id: '' })
|
|
}
|
|
>
|
|
<option value="">Seleccione</option>
|
|
{props.warehouses.map((w) => (
|
|
<option key={w.id} value={w.id}>
|
|
{w.code} · {w.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="grid gap-1 text-sm">
|
|
Lote exacto, quando controlado
|
|
<select
|
|
className={controlClass}
|
|
value={form.data.data.lot_id}
|
|
onChange={(e) => change({ lot_id: e.target.value })}
|
|
>
|
|
<option value="">Sem lote seleccionado</option>
|
|
{lots.map((l) => (
|
|
<option
|
|
key={l.id}
|
|
disabled={!l.reconciled}
|
|
value={l.id}
|
|
>
|
|
{l.code ?? `Por identificar #${l.id}`} · Quantidade
|
|
física: {quantity(l.quantity_milli)}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="grid gap-1 text-sm">
|
|
Quantidade a retirar
|
|
<input
|
|
required
|
|
className={controlClass}
|
|
inputMode="decimal"
|
|
pattern="\d{1,9}(\.\d{1,3})?"
|
|
value={form.data.data.quantity}
|
|
onChange={(e) => change({ quantity: e.target.value })}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1 text-sm">
|
|
Referência única do documento de saída
|
|
<input
|
|
required
|
|
maxLength={100}
|
|
className={controlClass}
|
|
value={form.data.data.reference}
|
|
onChange={(e) => change({ reference: e.target.value })}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1 text-sm md:col-span-2">
|
|
Motivo e evidência da proposta
|
|
<textarea
|
|
required
|
|
minLength={10}
|
|
className={controlClass}
|
|
value={form.data.reason}
|
|
onChange={(e) => form.setData('reason', e.target.value)}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>Guardar preparação</Button>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
function RecordActions({
|
|
props,
|
|
record,
|
|
}: {
|
|
props: Props;
|
|
record: RecordEntry;
|
|
}) {
|
|
const choices: { value: string; label: string }[] = [];
|
|
if (
|
|
props.capabilities.review &&
|
|
!record.snapshot.contributors.includes(props.actorId)
|
|
) {
|
|
if (['draft', 'returned'].includes(record.state))
|
|
choices.push({ value: 'approve', label: 'Conferir e aprovar' });
|
|
if (['draft', 'returned', 'approved'].includes(record.state))
|
|
choices.push({
|
|
value: 'return',
|
|
label: 'Devolver para actualização',
|
|
});
|
|
}
|
|
if (
|
|
props.capabilities.execute &&
|
|
record.state === 'approved' &&
|
|
record.snapshot.reviewed_by !== props.actorId
|
|
)
|
|
choices.push({ value: 'execute', label: 'Confirmar saída física' });
|
|
return (
|
|
<InventoryEventActions
|
|
route="/stock/ocorrencias"
|
|
record={record}
|
|
choices={choices}
|
|
today={props.today}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export default function Dispositions(props: Props) {
|
|
return (
|
|
<>
|
|
<Head title="Devoluções e abates" />
|
|
<div className="mx-auto flex w-full max-w-6xl flex-col gap-6 p-5 lg:p-8">
|
|
<PageTitle
|
|
eyebrow="Armazém"
|
|
title="Devoluções e abates"
|
|
description="Preparar, conferir e comprovar a saída de material, preservando as suas origens."
|
|
/>
|
|
{props.canViewReversals && (
|
|
<Link href="/stock/estornos" className="text-sm underline">
|
|
Estornos de stock ligados ao original
|
|
</Link>
|
|
)}
|
|
<Panel title="Responsabilidades separadas">
|
|
<p className="text-sm">
|
|
A preparação reutiliza a recepção ou o lote existente.
|
|
Outra pessoa aprova; Armazém comprova a saída, por
|
|
pessoa diferente de quem aprovou. Material em quarentena
|
|
ou vencido pode sair por estes percursos sem ser
|
|
considerado consumo.
|
|
</p>
|
|
<p className="mt-2 text-sm text-muted-foreground">
|
|
A devolução ou abate físico não cria crédito,
|
|
restituição, despesa ou pagamento. Facturas e obrigações
|
|
já validadas conservam o histórico e precisam da
|
|
regularização financeira correspondente. Novas facturas
|
|
consideram as quantidades já devolvidas.
|
|
</p>
|
|
</Panel>
|
|
{!props.ready ? (
|
|
<Panel title="Instalação pendente">
|
|
<p>
|
|
O diário de stock ainda não está instalado neste
|
|
ambiente.
|
|
</p>
|
|
</Panel>
|
|
) : (
|
|
<>
|
|
{props.capabilities.prepare && (
|
|
<Panel title="Preparar saída">
|
|
<Prepare props={props} />
|
|
</Panel>
|
|
)}
|
|
{props.records.map((record) => (
|
|
<Panel
|
|
key={record.id}
|
|
title={`${record.data.reference} · ${record.data.kind === 'supplier_return' ? 'Devolução ao fornecedor' : 'Abate físico'}`}
|
|
description={
|
|
record.data.stock_operation_id &&
|
|
props.correctedOperations?.[
|
|
record.data.stock_operation_id
|
|
]
|
|
? `Saída estornada por ${props.correctedOperations[record.data.stock_operation_id]}`
|
|
: states[record.state]
|
|
}
|
|
>
|
|
<p className="text-sm">
|
|
{record.snapshot.source.item.code} ·{' '}
|
|
{record.snapshot.source.item.name} ·{' '}
|
|
{quantity(record.data.quantity_milli)}{' '}
|
|
{record.snapshot.source.item.unit} ·{' '}
|
|
{record.snapshot.source.warehouse.name}
|
|
</p>
|
|
{record.snapshot.source.receipt && (
|
|
<p className="mt-2 text-sm">
|
|
Origem:{' '}
|
|
{record.snapshot.source.receipt.number}{' '}
|
|
·{' '}
|
|
{
|
|
record.snapshot.source.receipt
|
|
.order_number
|
|
}{' '}
|
|
·{' '}
|
|
{
|
|
record.snapshot.source.receipt
|
|
.supplier_name
|
|
}
|
|
</p>
|
|
)}
|
|
{record.snapshot.source.lot && (
|
|
<p className="mt-2 text-sm">
|
|
Lote:{' '}
|
|
{record.snapshot.source.lot.code ??
|
|
`Por identificar #${record.data.lot_id}`}
|
|
</p>
|
|
)}
|
|
<p className="mt-2 text-sm text-muted-foreground">
|
|
{record.reason}
|
|
</p>
|
|
{props.capabilities.prepare &&
|
|
['draft', 'returned'].includes(
|
|
record.state,
|
|
) && (
|
|
<details className="mt-4">
|
|
<summary className="cursor-pointer text-sm">
|
|
Actualizar preparação
|
|
</summary>
|
|
<Prepare
|
|
props={props}
|
|
old={record}
|
|
/>
|
|
</details>
|
|
)}
|
|
<RecordActions props={props} record={record} />
|
|
{record.state === 'executed' && (
|
|
<p className="mt-3 text-sm font-medium">
|
|
Saída em {record.data.event?.event_on} ·{' '}
|
|
{record.data.event?.evidence}.
|
|
Regularização financeira/contabilística
|
|
não concluída por este registo.{' '}
|
|
{props.canViewOperations && (
|
|
<Link
|
|
href="/stock/operacoes"
|
|
className="underline"
|
|
>
|
|
Consultar movimentos
|
|
</Link>
|
|
)}
|
|
</p>
|
|
)}
|
|
<details className="mt-4 text-sm">
|
|
<summary className="cursor-pointer">
|
|
Histórico preservado
|
|
</summary>
|
|
<ul className="mt-2 space-y-2">
|
|
{props.history[record.record_key]?.map(
|
|
(version) => (
|
|
<li key={version.id}>
|
|
Versão {version.version} ·{' '}
|
|
{states[version.state]} ·{' '}
|
|
{
|
|
version.snapshot.actor
|
|
.name
|
|
}{' '}
|
|
· {version.created_at} ·{' '}
|
|
{version.reason}
|
|
</li>
|
|
),
|
|
)}
|
|
</ul>
|
|
</details>
|
|
</Panel>
|
|
))}
|
|
</>
|
|
)}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|