import { Badge, Button, Card, Divider, Group, Loader, Modal, Stack, Text } from '@mantine/core'; import { CalendarClock, Coins, DoorOpen, FileText } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; import { useFeePreview, useGateClearance, useGenerateInvoice, useInvoicesForInventory, } from '@/hooks/useWarehouses'; import { extractErrorMessage } from './options'; import type { FeePreview, WarehouseInvoiceStatus } from '@/types/warehouse'; const INVOICE_STATUS_COLOR: Record = { DRAFT: 'gray', ISSUED: 'orange', PARTIALLY_PAID: 'yellow', PAID: 'edr-green', CANCELLED: 'gray', }; interface FeePreviewModalProps { opened: boolean; onClose: () => void; inventoryId: string | null; } const LABELS: Record = { DEMURRAGE_FEE: { label: 'Demurrage', color: 'orange' }, STORAGE_FEE: { label: 'Storage', color: 'teal' }, }; function fmtDate(iso: string | null) { if (!iso) return '—'; return new Date(iso).toLocaleDateString(); } function FeeCard({ fee }: { fee: FeePreview }) { const meta = LABELS[fee.ruleType] ?? { label: fee.ruleType, color: 'gray' }; const configured = Boolean(fee.ruleId); return ( {meta.label} {fee.endIsOpen && ( accruing )} {fee.amount.toLocaleString()} {fee.currency} {!configured ? ( No active {meta.label.toLowerCase()} rule configured — amount shown as 0. ) : ( )} ); } function Row({ label, value }: { label: string; value: string }) { return ( {label} {value} ); } /** Batch 5 fee preview + Batch 6 invoice generation / gate clearance for an inventory item. */ export function FeePreviewModal({ opened, onClose, inventoryId }: FeePreviewModalProps) { const { toast } = useToast(); const enabledId = opened ? inventoryId ?? undefined : undefined; const { data, isLoading } = useFeePreview(enabledId); const { data: invoices } = useInvoicesForInventory(enabledId); const generate = useGenerateInvoice(); const gateClear = useGateClearance(); const activeInvoice = (invoices ?? []).find((i) => i.status !== 'CANCELLED'); const handleGenerate = async (confirmZero = false) => { if (!inventoryId) return; try { const inv = await generate.mutateAsync({ inventoryId, confirmZero }); toast({ title: 'Invoice generated', description: `${inv.invoiceNumber} — ${inv.totalAmount} ${inv.currency}` }); } catch (error) { const msg = extractErrorMessage(error); if (/no payable warehouse fee/i.test(msg)) { if (window.confirm('No payable warehouse fee found. Create a zero-amount invoice anyway?')) { handleGenerate(true); } return; } toast({ variant: 'destructive', title: 'Generate failed', description: msg }); } }; const handleGateClearance = async () => { if (!inventoryId) return; try { await gateClear.mutateAsync(inventoryId); toast({ title: 'Gate clearance recorded', description: 'Item released from terminal.' }); onClose(); } catch (error) { toast({ variant: 'destructive', title: 'Release blocked', description: extractErrorMessage(error) }); } }; return ( Storage & Demurrage Preview } centered size="md" > {isLoading ? ( ) : ( {(data ?? []).map((fee) => ( ))} {activeInvoice ? ( {activeInvoice.invoiceNumber} {activeInvoice.status.replace(/_/g, ' ')} {Number(activeInvoice.balanceAmount).toLocaleString()} {activeInvoice.currency} due ) : ( )} Charges accrue from arrival until gate clearance / release. Final release is blocked while a demurrage/storage invoice is unpaid. )} ); }