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, FormErrors, controlClass, } from '@/components/procurement'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { quantity } from '@/types/inventory'; import type { StockItem, Warehouse, Page } from '@/types/inventory'; type Operation = { id: number; kind: string; reference: string; reason: string; destination?: string; quantity_milli: number; theoretical_milli: number; stock_item_id: number; warehouse_id: number; recorded_at: string; snapshot: { actor: { name: string }; responsible: { name: string }; stock_item: { name: string; unit: string }; 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 = { items: StockItem[]; warehouses: Warehouse[]; users: { id: number; name: string }[]; balances: { stock_item_id: number; warehouse_id: number; quantity: number; version: number; lot_control?: { usable_milli: number; blocked_milli: number; reconciled: boolean; } | null; }[]; operations: Page; counts: Operation[]; canOperate: boolean; correctedOperations?: Record; canAdjust?: boolean; lotCounts?: LotCountSource[]; demo?: boolean; equipment?: { id: number; code: string; description: string }[]; }; const kinds: Record = { issue: 'Saída', transfer: 'Transferência', count: 'Contagem física', adjustment: 'Ajuste de inventário', }; export default function Operations({ items, warehouses, users, balances, operations, counts, canOperate, correctedOperations = {}, canAdjust = false, lotCounts = [], demo = false, equipment = [], }: Props) { const [message, setMessage] = useState(''); const form = useForm({ kind: canOperate ? 'issue' : 'adjustment', reference: '', idempotency_key: crypto.randomUUID(), stock_item_id: String(items[0]?.id ?? ''), warehouse_id: String(warehouses[0]?.id ?? ''), destination_warehouse_id: '', responsible_id: '', destination: '', reason: '', quantity: '', count_id: '', ledger_version: 0, equipment_id: '', }); const balance = balances.find( (b) => 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 >({}); 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 ( <>
{canOperate && form.data.kind === 'issue' && ( )} {demo && (

Demonstração pública com dados fictícios. O formulário apenas simula a revisão.

)} {canOperate || canAdjust ? (
{ e.preventDefault(); if (demo) { setMessage( 'Simulação revista. Nenhum movimento real foi criado.', ); return; } 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: () => { form.setData({ ...form.data, idempotency_key: crypto.randomUUID(), reference: '', quantity: '', reason: '', }); }, }); }} >

Stock teórico:{' '} {quantity(balance?.quantity ?? 0)} · versão{' '} {balance?.version ?? 0}. Confirme a contagem física sobre este saldo; movimentos posteriores exigem nova contagem.

{balance?.lot_control && (

{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.

)} {form.data.kind === 'transfer' && ( )} {form.data.kind === 'issue' && ( )} {form.data.kind === 'adjustment' ? ( ) : form.data.kind === 'count' && countSource ? ( setCountInputs({ ...countInputs, [countKey]: rows, }) } /> ) : ( )}