This commit is contained in:
2026-11-02 08:08:05 +02:00
parent 13a6cb9922
commit 504b01b244
246 changed files with 24499 additions and 185 deletions
+257 -13
View File
@@ -1,6 +1,11 @@
import { Head, useForm } from '@inertiajs/react';
import { useState } from 'react';
import { InventoryNotice, InventoryPagination } from '@/components/inventory';
import { LotCountFields, countTotal } from '@/components/inventory-lot-count';
import type {
CountInput,
LotCountSource,
} from '@/components/inventory-lot-count';
import {
PageTitle,
Panel,
@@ -30,6 +35,43 @@ type Operation = {
warehouse: { name: string };
destination_warehouse?: { name: string } | null;
equipment?: { code: string; description: string } | null;
reversal?: {
source: {
document: { reference: string };
rows: {
source_movement_id: number;
warehouse_name: string;
quantity_milli: number;
}[];
};
};
lot_count?: {
rows: {
lot_id: number | null;
code: string | null;
previous_milli: number;
counted_milli: number;
delta_milli: number;
}[];
};
lot_adjustment?: {
rows: {
lot_id: number | null;
code: string | null;
previous_milli: number;
counted_milli: number;
delta_milli: number;
}[];
};
lot_partition?: {
plan: {
lot_id: number;
quantity: number;
code: string | null;
expires_on: string | null;
metadata_id: number;
}[];
};
};
};
type Props = {
@@ -41,10 +83,18 @@ type Props = {
warehouse_id: number;
quantity: number;
version: number;
lot_control?: {
usable_milli: number;
blocked_milli: number;
reconciled: boolean;
} | null;
}[];
operations: Page<Operation>;
counts: Operation[];
canOperate: boolean;
correctedOperations?: Record<number, string>;
canAdjust?: boolean;
lotCounts?: LotCountSource[];
demo?: boolean;
equipment?: { id: number; code: string; description: string }[];
};
@@ -62,12 +112,15 @@ export default function Operations({
operations,
counts,
canOperate,
correctedOperations = {},
canAdjust = false,
lotCounts = [],
demo = false,
equipment = [],
}: Props) {
const [message, setMessage] = useState('');
const form = useForm({
kind: 'issue',
kind: canOperate ? 'issue' : 'adjustment',
reference: '',
idempotency_key: crypto.randomUUID(),
stock_item_id: String(items[0]?.id ?? ''),
@@ -86,6 +139,28 @@ export default function Operations({
String(b.stock_item_id) === form.data.stock_item_id &&
String(b.warehouse_id) === form.data.warehouse_id,
);
const countSource = lotCounts.find(
(s) =>
String(s.stock_item_id) === form.data.stock_item_id &&
String(s.warehouse_id) === form.data.warehouse_id,
);
const [countInputs, setCountInputs] = useState<
Record<string, CountInput[]>
>({});
const countKey = countSource?.hash ?? '';
const countRows =
countInputs[countKey] ??
countSource?.lots
.filter((lot) => lot.quantity_milli > 0)
.map((lot) => ({
lot_id: lot.lot_id,
new_key: null,
quantity: '',
code: lot.code ?? '',
expires_on: lot.expires_on ?? '',
no_expiry: lot.no_expiry,
})) ??
[];
return (
<>
<Head title="Operações de stock" />
@@ -124,9 +199,9 @@ export default function Operations({
)}
<Panel
title="Registar operação"
description="Só Recepção / Armazém executa movimentos. Administração, Procurement e Finanças consultam. Periodicidade, tolerâncias e investigação de divergências aguardam decisão da empresa."
description="Armazém regista as contagens. Ajustes por lote exigem pessoa independente com competência própria de conferência."
>
{canOperate ? (
{canOperate || canAdjust ? (
<form
className="grid gap-4 sm:grid-cols-2"
onSubmit={(e) => {
@@ -140,6 +215,15 @@ export default function Operations({
form.transform((d) => ({
...d,
ledger_version: balance?.version ?? 0,
...(d.kind === 'count' && countSource
? {
lot_version: countSource.hash,
lot_counts: countRows,
quantity:
countTotal(countRows) ??
'0.000',
}
: {}),
}));
form.post('/stock/operacoes', {
onSuccess: () => {
@@ -164,11 +248,17 @@ export default function Operations({
form.setData('kind', e.target.value)
}
>
{Object.entries(kinds).map(([k, v]) => (
<option key={k} value={k}>
{v}
</option>
))}
{Object.entries(kinds)
.filter(
([k]) =>
canOperate ||
k === 'adjustment',
)
.map(([k, v]) => (
<option key={k} value={k}>
{v}
</option>
))}
</select>
</label>
<label className="grid gap-2 text-sm">
@@ -229,6 +319,19 @@ export default function Operations({
física sobre este saldo; movimentos posteriores
exigem nova contagem.
</p>
{balance?.lot_control && (
<p className="text-sm sm:col-span-2">
{balance.lot_control.reconciled
? `Controlo por lotes · Utilizável: ${quantity(balance.lot_control.usable_milli)} · Indisponível: ${quantity(balance.lot_control.blocked_milli)}. As saídas usam primeiro a validade mais próxima.`
: 'Repartição de lotes divergente. Confira as origens antes de operar.'}{' '}
Transferências exigem controlo nos dois
armazéns e colocam o lote inteiro no destino
em quarentena, incluindo o saldo desse lote
já existente no local. Ajustes por lote
reutilizam uma contagem e exigem conferência
independente.
</p>
)}
{form.data.kind === 'transfer' && (
<label className="grid gap-2 text-sm">
Armazém de destino
@@ -294,6 +397,11 @@ export default function Operations({
Seleccionar contagem
</option>
{counts
.filter(
(c) =>
canOperate ||
!!c.snapshot.lot_count,
)
.filter(
(c) =>
String(c.warehouse_id) ===
@@ -314,6 +422,17 @@ export default function Operations({
))}
</select>
</label>
) : form.data.kind === 'count' && countSource ? (
<LotCountFields
source={countSource}
rows={countRows}
setRows={(rows) =>
setCountInputs({
...countInputs,
[countKey]: rows,
})
}
/>
) : (
<label className="grid gap-2 text-sm">
{form.data.kind === 'count'
@@ -372,7 +491,14 @@ export default function Operations({
</label>
<div className="sm:col-span-2">
<FormErrors errors={form.errors} />
<Button disabled={form.processing}>
<Button
disabled={
form.processing ||
(form.data.kind === 'adjustment' &&
!!balance?.lot_control &&
!canAdjust)
}
>
{demo
? 'Rever simulação'
: 'Confirmar operação'}
@@ -401,7 +527,15 @@ export default function Operations({
>
<div className="flex flex-wrap justify-between gap-2">
<strong>
{o.reference} · {kinds[o.kind]}
{o.reference} ·{' '}
{kinds[o.kind] ??
{
supplier_return:
'Devolução ao fornecedor',
write_off: 'Abate físico',
reversal: 'Estorno integral',
}[o.kind] ??
o.kind}
</strong>
<time>
{new Date(o.recorded_at).toLocaleString(
@@ -411,9 +545,10 @@ export default function Operations({
</div>
<p className="mt-2">
{o.snapshot.stock_item.name} ·{' '}
{quantity(o.quantity_milli)}{' '}
{o.snapshot.stock_item.unit} ·{' '}
{o.snapshot.warehouse.name}
{o.kind === 'reversal'
? `${o.snapshot.reversal?.source.rows.length ?? 0} movimentos corrigidos`
: `${quantity(o.quantity_milli)} ${o.snapshot.stock_item.unit}`}{' '}
· {o.snapshot.warehouse.name}
{o.snapshot.destination_warehouse &&
` → ${o.snapshot.destination_warehouse.name}`}
{o.destination && ` → ${o.destination}`}
@@ -431,6 +566,115 @@ export default function Operations({
</p>
)}
<p className="mt-2">{o.reason}</p>
{correctedOperations[o.id] && (
<p className="mt-2 text-sm font-medium">
Operação estornada integralmente por{' '}
{correctedOperations[o.id]}. O original
permanece no histórico.
</p>
)}
{o.snapshot.reversal && (
<div className="mt-3 text-sm">
<p>
Origem:{' '}
{
o.snapshot.reversal.source
.document.reference
}
</p>
<ul className="mt-1 space-y-1">
{o.snapshot.reversal.source.rows.map(
(row) => (
<li
key={
row.source_movement_id
}
>
{row.warehouse_name} ·{' '}
{row.quantity_milli > 0
? '+'
: ''}
{quantity(
row.quantity_milli,
)}{' '}
{
o.snapshot
.stock_item.unit
}{' '}
· Corrige movimento #
{row.source_movement_id}
</li>
),
)}
</ul>
</div>
)}
{(o.snapshot.lot_count ??
o.snapshot.lot_adjustment) && (
<details className="mt-3 text-sm">
<summary className="cursor-pointer">
Conferência por lote
</summary>
<ul className="mt-2 space-y-1">
{(
o.snapshot.lot_count ??
o.snapshot.lot_adjustment
)?.rows.map((row, index) => (
<li key={row.lot_id ?? index}>
{row.code ??
'Por identificar'}{' '}
· Referência:{' '}
{quantity(
row.previous_milli,
)}{' '}
· Contado:{' '}
{quantity(
row.counted_milli,
)}{' '}
· Diferença:{' '}
{quantity(row.delta_milli)}
</li>
))}
</ul>
</details>
)}
{o.snapshot.lot_partition && (
<details className="mt-3 text-sm">
<summary className="cursor-pointer">
Lotes usados nesta operação
</summary>
<ul className="mt-2 space-y-1">
{o.snapshot.lot_partition.plan.map(
(lot) => (
<li key={lot.lot_id}>
{lot.code ??
`Lote #${lot.lot_id}`}{' '}
·{' '}
{quantity(lot.quantity)}{' '}
{
o.snapshot
.stock_item.unit
}{' '}
· Validade:{' '}
{lot.expires_on ??
'Sem prazo confirmado'}{' '}
· Identificação #
{lot.metadata_id}
</li>
),
)}
</ul>
{o.kind === 'transfer' && (
<p className="mt-2 text-muted-foreground">
A entrada colocou o lote inteiro
no destino em quarentena.
Consulte a conferência actual de
lotes para conhecer a
disponibilidade.
</p>
)}
</details>
)}
{o.snapshot.equipment && (
<p className="mt-2">
Equipamento no momento da saída:{' '}