411 lines
18 KiB
TypeScript
411 lines
18 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 Row = {
|
|
source_movement_id: number;
|
|
warehouse_id: number;
|
|
warehouse_name: string;
|
|
original_quantity_milli: number;
|
|
quantity_milli: number;
|
|
original_type: string;
|
|
lots: { lot_id: number | null; code: string | null; quantity: number }[];
|
|
};
|
|
type Entry = {
|
|
id: number;
|
|
record_key: string;
|
|
version: number;
|
|
state: string;
|
|
reason: string;
|
|
created_at: string;
|
|
data: {
|
|
reference: string;
|
|
source_kind: string;
|
|
source_operation_id: number | null;
|
|
receipt_item_id: number | null;
|
|
event?: { event_on: string; evidence: string };
|
|
};
|
|
snapshot: {
|
|
execution_lots?: {
|
|
lot_id: number;
|
|
movement_id: number;
|
|
quantity: number;
|
|
}[];
|
|
contributors: number[];
|
|
reviewed_by?: number;
|
|
actor: { name: string };
|
|
source: {
|
|
document: { reference: string };
|
|
item: { code: string; name: string; unit: string };
|
|
rows: Row[];
|
|
};
|
|
};
|
|
};
|
|
type Props = {
|
|
ready: boolean;
|
|
actorId: number;
|
|
today: string;
|
|
canViewDispositions: boolean;
|
|
capabilities: Record<string, boolean>;
|
|
records: Entry[];
|
|
operations: { id: number; label: string }[];
|
|
receipts: { id: number; label: string }[];
|
|
history: Record<string, Entry[]>;
|
|
};
|
|
const qty = (milli: number) =>
|
|
(milli / 1000).toLocaleString('pt-MZ', { maximumFractionDigits: 3 });
|
|
const states: Record<string, string> = {
|
|
draft: 'Em preparação',
|
|
returned: 'Para actualização',
|
|
approved: 'Aprovado para estorno',
|
|
executed: 'Estorno integral registado',
|
|
};
|
|
|
|
function Prepare({ props, old }: { props: Props; old?: Entry }) {
|
|
const form = useForm({
|
|
operation_key: crypto.randomUUID(),
|
|
record_key: old?.record_key,
|
|
version: old?.version ?? 0,
|
|
action: 'save',
|
|
reason: '',
|
|
data: {
|
|
reference: old?.data.reference ?? '',
|
|
source_kind: old?.data.source_kind ?? 'operation',
|
|
source_operation_id: old?.data.source_operation_id
|
|
? String(old.data.source_operation_id)
|
|
: '',
|
|
receipt_item_id: old?.data.receipt_item_id
|
|
? String(old.data.receipt_item_id)
|
|
: '',
|
|
},
|
|
});
|
|
return (
|
|
<form
|
|
className="grid gap-4 md:grid-cols-2"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
form.post('/stock/estornos');
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<label className="grid gap-1 text-sm">
|
|
Origem
|
|
<select
|
|
className={controlClass}
|
|
value={form.data.data.source_kind}
|
|
onChange={(e) =>
|
|
form.setData('data', {
|
|
...form.data.data,
|
|
source_kind: e.target.value,
|
|
source_operation_id: '',
|
|
receipt_item_id: '',
|
|
})
|
|
}
|
|
>
|
|
<option value="operation">
|
|
Saída, transferência, ajuste, devolução ou abate
|
|
</option>
|
|
<option value="receipt">Linha física de recepção</option>
|
|
</select>
|
|
</label>
|
|
{form.data.data.source_kind === 'operation' ? (
|
|
<label className="grid gap-1 text-sm">
|
|
Operação original
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={form.data.data.source_operation_id}
|
|
onChange={(e) =>
|
|
form.setData('data', {
|
|
...form.data.data,
|
|
source_operation_id: e.target.value,
|
|
})
|
|
}
|
|
>
|
|
<option value="">
|
|
Seleccione a operação a corrigir
|
|
</option>
|
|
{props.operations.map((op) => (
|
|
<option key={op.id} value={op.id}>
|
|
{op.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
) : (
|
|
<label className="grid gap-1 text-sm">
|
|
Linha de recepção original
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={form.data.data.receipt_item_id}
|
|
onChange={(e) =>
|
|
form.setData('data', {
|
|
...form.data.data,
|
|
receipt_item_id: e.target.value,
|
|
})
|
|
}
|
|
>
|
|
<option value="">Seleccione a linha a anular</option>
|
|
{props.receipts.map((receipt) => (
|
|
<option key={receipt.id} value={receipt.id}>
|
|
{receipt.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
)}
|
|
<label className="grid gap-1 text-sm">
|
|
Referência única do documento de correcção
|
|
<input
|
|
required
|
|
maxLength={100}
|
|
className={controlClass}
|
|
value={form.data.data.reference}
|
|
onChange={(e) =>
|
|
form.setData('data', {
|
|
...form.data.data,
|
|
reference: e.target.value,
|
|
})
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1 text-sm md:col-span-2">
|
|
Erro identificado e evidência da correcção
|
|
<textarea
|
|
required
|
|
minLength={10}
|
|
className={controlClass}
|
|
value={form.data.reason}
|
|
onChange={(e) => form.setData('reason', e.target.value)}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>
|
|
Preparar estorno integral
|
|
</Button>
|
|
<p className="text-sm text-muted-foreground md:col-span-2">
|
|
Artigo, locais, lotes e quantidades são reutilizados
|
|
integralmente da origem. O registo original permanece no
|
|
histórico. Não use estorno para simular consumo, devolução
|
|
parcial ou uma operação nova.
|
|
</p>
|
|
</form>
|
|
);
|
|
}
|
|
function RecordActions({ props, record }: { props: Props; record: Entry }) {
|
|
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 estorno integral' });
|
|
return (
|
|
<InventoryEventActions
|
|
route="/stock/estornos"
|
|
record={record}
|
|
choices={choices}
|
|
today={props.today}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export default function Reversals(props: Props) {
|
|
return (
|
|
<>
|
|
<Head title="Estornos de stock" />
|
|
<div className="mx-auto flex w-full max-w-6xl flex-col gap-6 p-5 lg:p-8">
|
|
<PageTitle
|
|
eyebrow="Armazém"
|
|
title="Estornos de stock"
|
|
description="Corrigir por referência ao original, sem apagar documentos nem repetir quantidades."
|
|
/>
|
|
{props.canViewDispositions && (
|
|
<Link
|
|
href="/stock/ocorrencias"
|
|
className="text-sm underline"
|
|
>
|
|
Devoluções e abates
|
|
</Link>
|
|
)}
|
|
<Panel title="Antes de corrigir">
|
|
<p className="text-sm">
|
|
O estorno é integral e só pode ocorrer uma vez por
|
|
origem. A preparação, aprovação e comprovação da
|
|
execução têm responsabilidades separadas. Quantidades
|
|
repostas em locais com controlo por lote ficam em
|
|
quarentena, incluindo saldo anterior da mesma
|
|
identidade. As retiradas exigem a quantidade física dos
|
|
lotes originais.
|
|
</p>
|
|
<p className="mt-2 text-sm text-muted-foreground">
|
|
A correcção actualiza o stock, a satisfação de reservas
|
|
e as quantidades efectivas de recepção. Não cria
|
|
crédito, restituição ou estorno financeiro. Documentos e
|
|
obrigações já validados precisam de conferência própria
|
|
em Finanças. Não há estorno parcial, estorno de outro
|
|
estorno ou de aceitação de serviços neste percurso.
|
|
</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 correcção">
|
|
<Prepare props={props} />
|
|
</Panel>
|
|
)}
|
|
{props.records.map((record) => (
|
|
<Panel
|
|
key={record.id}
|
|
title={`${record.data.reference} · ${states[record.state]}`}
|
|
description={`Origem: ${record.snapshot.source.document.reference}`}
|
|
>
|
|
<p className="text-sm">
|
|
{record.snapshot.source.item.code} ·{' '}
|
|
{record.snapshot.source.item.name}
|
|
</p>
|
|
<ul className="mt-3 space-y-2 text-sm">
|
|
{record.snapshot.source.rows.map((row) => (
|
|
<li key={row.source_movement_id}>
|
|
{row.warehouse_name} · Movimento
|
|
original #{row.source_movement_id}:{' '}
|
|
{qty(row.original_quantity_milli)} →
|
|
correcção{' '}
|
|
{row.quantity_milli > 0 ? '+' : ''}
|
|
{qty(row.quantity_milli)}{' '}
|
|
{record.snapshot.source.item.unit}
|
|
{row.lots.length > 0 && (
|
|
<ul className="mt-1 ml-4 list-disc">
|
|
{row.lots.map((lot, i) => (
|
|
<li
|
|
key={
|
|
lot.lot_id ?? i
|
|
}
|
|
>
|
|
{lot.code ??
|
|
(lot.lot_id
|
|
? `Lote #${lot.lot_id}`
|
|
: 'Identidade desconhecida a conferir')}{' '}
|
|
·{' '}
|
|
{qty(lot.quantity)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<p className="mt-3 text-sm text-muted-foreground">
|
|
{record.reason}
|
|
</p>
|
|
{props.capabilities.prepare &&
|
|
['draft', 'returned'].includes(
|
|
record.state,
|
|
) && (
|
|
<details className="mt-3">
|
|
<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">
|
|
Correcção em{' '}
|
|
{record.data.event?.event_on} ·{' '}
|
|
{record.data.event?.evidence}. O
|
|
original foi preservado e não pode ser
|
|
estornado novamente.
|
|
</p>
|
|
)}
|
|
{record.snapshot.execution_lots &&
|
|
record.snapshot.execution_lots.length >
|
|
0 && (
|
|
<details className="mt-3 text-sm">
|
|
<summary className="cursor-pointer">
|
|
Repartição efectiva do estorno
|
|
por lote
|
|
</summary>
|
|
<ul className="mt-2 space-y-1">
|
|
{record.snapshot.execution_lots.map(
|
|
(lot) => (
|
|
<li
|
|
key={`${lot.movement_id}-${lot.lot_id}`}
|
|
>
|
|
Lote #{lot.lot_id} ·{' '}
|
|
{lot.quantity > 0
|
|
? '+'
|
|
: ''}
|
|
{qty(lot.quantity)}{' '}
|
|
{
|
|
record.snapshot
|
|
.source.item
|
|
.unit
|
|
}{' '}
|
|
· Movimento #
|
|
{lot.movement_id}
|
|
</li>
|
|
),
|
|
)}
|
|
</ul>
|
|
</details>
|
|
)}
|
|
<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>
|
|
</>
|
|
);
|
|
}
|