import { useInventoryPreparationIdentity } from '@/hooks/use-inventory-preparation-identity'; import { usePreparationDirty } from '@/hooks/use-preparation-dirty'; import { hasPreparationChanges } from '@/lib/requisition-history'; import { InventoryEventActions } from '@/components/inventory-event-actions'; import { Head, Link, router, useForm } from '@inertiajs/react'; import { useEffect, useState } from 'react'; import CorrectionSourceSearch from '@/components/correction-source-search'; import { FormErrors, PageTitle, Panel, controlClass, } from '@/components/procurement'; import { Button } from '@/components/ui/button'; import SupplyPagination, { type SupplyPageInfo, } from '@/components/supply-pagination'; import InventoryCorrectionFilters, { type CorrectionFilters, } from '@/components/inventory-correction-filters'; 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 = { executionSourceCurrent?: boolean | null; canApprove?: boolean; canReturn?: boolean; canExecute?: boolean; 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 = { sourceSearch?: string; sourceFormKey?: string | null; recordPagination?: SupplyPageInfo | null; filters: CorrectionFilters; focusRecordId?: number | null; focusChanged?: boolean; ready: boolean; actorId: number; today: string; canViewDispositions: boolean; capabilities: Record; records: Entry[]; operations: { id: number; label: string }[]; receipts: { id: number; label: string }[]; history: Record; }; const qty = (milli: number) => (milli / 1000).toLocaleString('pt-MZ', { maximumFractionDigits: 3 }); const states: Record = { 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 capabilities = JSON.stringify( Object.entries(props.capabilities).sort(([a], [b]) => a.localeCompare(b), ), ); const identity = useInventoryPreparationIdentity( `/stock/estornos:prepare:${old?.record_key ?? 'new'}:${old?.version ?? 0}:${old?.state ?? 'new'}:${capabilities}`, ); return ( ); } function PrepareFields({ props, old, identity, }: { props: Props; old?: Entry; identity: string; }) { const [operations, setOperations] = useState(props.operations); const [receipts, setReceipts] = useState(props.receipts); const formKey = `reversal:${old?.id ?? 'new'}`; useEffect(() => { if (!props.sourceFormKey || props.sourceFormKey === formKey) { setOperations(props.operations); setReceipts(props.receipts); } }, [props.sourceFormKey, props.operations, props.receipts, formKey]); const initial = { 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) : '', }, }; const form = useForm(identity, initial); const dirty = hasPreparationChanges(initial, form.data); const ref = usePreparationDirty(dirty); const searchSources = (value: string) => router.get( '/stock/estornos', { records_page: props.recordPagination?.current_page, search: props.filters.search || undefined, state: props.filters.state || undefined, ...(props.focusRecordId ? { record_id: props.focusRecordId } : {}), source_search: value, source_form_key: formKey, selected_operation_id: form.data.data.source_operation_id || undefined, selected_receipt_id: form.data.data.receipt_item_id || undefined, }, { only: [ 'operations', 'receipts', 'sourceSearch', 'sourceFormKey', ], preserveState: true, preserveScroll: true, }, ); return (
{ e.preventDefault(); form.post('/stock/estornos', { onSuccess: () => { const next = { ...initial, operation_key: crypto.randomUUID(), }; form.setData(next); form.clearErrors(); router.remember(next, `${identity}:data`); router.remember({}, `${identity}:errors`); }, }); }} > {form.data.data.source_kind === 'operation' ? ( ) : ( )}