import { useEffect, useRef, useState } from 'react'; import { useNavigate, useParams, useSearchParams } from 'react-router-dom'; import { Alert, Box, Button, Card, CircularProgress, Stack, Table, TableBody, TableCell, TableHead, TableRow, Typography, } from '@mui/material'; import { useInvoice, usePostInvoice, useVoidInvoice } from '../../../hooks/useARInvoices'; import { useAuth } from '../../../hooks/useAuth'; import { StatusChip } from '../../../components/common/StatusChip'; import { MoneyCell } from '../../../components/common/MoneyCell'; export function InvoiceDetailPage() { const { id } = useParams(); const navigate = useNavigate(); const [searchParams, setSearchParams] = useSearchParams(); const invoiceId = id ? Number(id) : undefined; const { data: invoice, isLoading } = useInvoice(invoiceId); const postInvoice = usePostInvoice(); const voidInvoice = useVoidInvoice(); const { hasPermission } = useAuth(); const [error, setError] = useState(null); const autoPosted = useRef(false); useEffect(() => { if (searchParams.get('post') === '1' && invoice?.status === 'DRAFT' && !autoPosted.current) { autoPosted.current = true; postInvoice.mutateAsync(invoice.id).catch((err) => { const message = (err as { response?: { data?: { error?: string } } })?.response?.data?.error; setError(message ?? 'Failed to post invoice'); }); setSearchParams({}, { replace: true }); } }, [invoice, postInvoice, searchParams, setSearchParams]); if (isLoading || !invoice) return ; const handlePost = async () => { setError(null); try { await postInvoice.mutateAsync(invoice.id); } catch (err) { const message = (err as { response?: { data?: { error?: string } } })?.response?.data?.error; setError(message ?? 'Failed to post invoice'); } }; const handleVoid = async () => { setError(null); try { await voidInvoice.mutateAsync(invoice.id); } catch (err) { const message = (err as { response?: { data?: { error?: string } } })?.response?.data?.error; setError(message ?? 'Failed to void invoice'); } }; return ( {invoice.invoiceNumber} {invoice.status === 'DRAFT' && hasPermission('ar.invoice.manage') && ( <> )} {error && ( {error} )} Customer navigate(`/ar/customers/${invoice.customerId}`)} > {invoice.customer?.name} Invoice Date {invoice.invoiceDate.slice(0, 10)} Due Date {invoice.dueDate.slice(0, 10)} Reference {invoice.reference || '—'} Balance Description Qty Unit Price Revenue Account Tax Amount {invoice.lines.map((line) => ( {line.description} {line.quantity} {line.revenueGLAccount?.accountName} {line.taxCode?.code ?? '—'} ))} Subtotal Tax Total
{invoice.allocations && invoice.allocations.length > 0 && ( Allocations Source Date Amount {invoice.allocations.map((allocation) => ( {allocation.receipt?.receiptNumber ?? allocation.creditNote?.creditNoteNumber} {allocation.allocatedAt.slice(0, 10)} ))}
)}
); }