import { useState, type MouseEvent } from 'react'; import { ActionIcon, Badge, Button, Checkbox, Group, Table, Text, Tooltip } from '@mantine/core'; import { ArrowRightLeft, ClipboardList, Coins, Eye, FileText, History, MapPin } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; import { warehouseService } from '@/services/warehouse.service'; import { getNextInventoryAction, type InventoryAction, type WarehouseInventoryItem, } from '@/types/warehouse'; import { InventoryStatusBadge } from './badges'; import { extractErrorMessage, formatDate, formatNumber, humanizeEnum } from './options'; import { openPdfBlob } from './pdf'; interface WarehouseInventoryTableProps { items: WarehouseInventoryItem[]; busyId?: string | null; onAdvance: (item: WarehouseInventoryItem, action: InventoryAction) => void; onMove: (item: WarehouseInventoryItem) => void; onHistory: (item: WarehouseInventoryItem) => void; onView?: (item: WarehouseInventoryItem) => void; onInspect?: (item: WarehouseInventoryItem) => void; onFeePreview?: (item: WarehouseInventoryItem) => void; onReleaseDocument?: (item: WarehouseInventoryItem) => void; onHandoverDocument?: (item: WarehouseInventoryItem) => void; onLastMile?: (item: WarehouseInventoryItem) => void; selectedIds?: Set; onToggleSelect?: (id: string) => void; onToggleSelectAll?: () => void; allSelected?: boolean; someSelected?: boolean; } const itemKind = (item: WarehouseInventoryItem) => { if (item.containerId) return { label: 'Container', color: 'blue' }; if (item.cargoId) return { label: 'Cargo', color: 'grape' }; if (item.goodsId) return { label: 'Goods', color: 'orange' }; return { label: '-', color: 'gray' }; }; const actionColor: Record = { store: 'blue', reserve: 'grape', 'ready-for-loading': 'cyan', load: 'teal', dispatch: 'edr-green', 'ready-for-pickup': 'orange', release: 'yellow', deliver: 'green', }; const releaseActionLabel = (item: WarehouseInventoryItem) => item.releaseOrderReference ? 'Truck Leaving' : 'Truck Arrival'; const noteLineValue = (notes: string | null | undefined, label: string) => { const match = notes?.match(new RegExp(`^${label}:\\s*(.+)$`, 'im')); return match?.[1]?.trim() ?? ''; }; const handoverDocumentReference = (item: WarehouseInventoryItem) => item.handoverDocumentReference ?? noteLineValue(item.notes, 'Handover Reference'); function GrnDocumentButton({ item }: { item: WarehouseInventoryItem }) { const { toast } = useToast(); const [loading, setLoading] = useState(false); const openDocument = async (event: MouseEvent) => { event.stopPropagation(); if (!item.grnNumber) { toast({ variant: 'destructive', title: 'GRN document unavailable', description: 'This item has no GRN number yet.' }); return; } setLoading(true); const pdfWindow = window.open('', '_blank'); try { const response = await warehouseService.downloadGrnDocument(item.id); const opened = openPdfBlob(response.data, `grn-${item.grnNumber}.pdf`, pdfWindow); toast({ title: opened ? 'GRN document opened' : 'GRN document downloaded' }); } catch (error) { pdfWindow?.close(); toast({ variant: 'destructive', title: 'GRN document failed', description: extractErrorMessage(error) }); } finally { setLoading(false); } }; return ( ); } export function WarehouseInventoryTable({ items, busyId, onAdvance, onMove, onHistory, onView, onInspect, onFeePreview, onReleaseDocument, onHandoverDocument, onLastMile, selectedIds, onToggleSelect, onToggleSelectAll, allSelected, someSelected, }: WarehouseInventoryTableProps) { const selectable = Boolean(onToggleSelect); if (items.length === 0) { return ( No inventory items found. ); } return ( {selectable && ( )} Booking GRN Facility Warehouse Yard Zone Item Qty Weight Status Arrived Actions {items.map((item) => { const kind = itemKind(item); const busy = busyId === item.id; const nextAction = getNextInventoryAction(item); const canGenerateHandover = item.inspectionStatus === 'PASSED' && Boolean(item.bookingId) && (!item.booking?.tradeDirection || item.booking.tradeDirection === 'IMPORT'); const handoverReference = handoverDocumentReference(item); return ( {selectable && ( onToggleSelect?.(item.id)} /> )} {item.bookingId ? ( {item.bookingId.slice(0, 8)}... ) : ( - )} {item.warehouse?.facility?.name ?? '-'} {item.warehouse?.code ?? '-'} {item.yard?.code ?? '-'} {item.zone?.code ?? '-'} {kind.label} {formatNumber(item.quantity)} {formatNumber(item.weight)} {formatDate(item.arrivedAt)} {onView && ( onView(item)}> )} {nextAction && ( )} {item.status === 'READY_FOR_PICKUP' && ( <> )} {item.status !== 'DISPATCHED' && ( onMove(item)}> )} {onInspect && ( onInspect(item)}> )} {onFeePreview && ( onFeePreview(item)}> )} {onReleaseDocument && item.releaseDate && ( onReleaseDocument(item)}> )} {onHandoverDocument && canGenerateHandover && ( onHandoverDocument(item)}> )} {onLastMile && item.booking?.lastMileDeliveryAddress && ( onLastMile(item)}> )} onHistory(item)}> ); })}
); }