'use client' import { Badge } from '@/components/ui/badge' import { Separator } from '@/components/ui/separator' import { getVatTreatmentLabel } from '@/lib/invoice/vat-rules' import { formatCurrency } from '@/lib/utils' import type { Customer, Currency, VatTreatment } from '@/types' interface ReviewItem { description: string quantity: number unit: string unit_price: number vat_rate?: number } interface InvoiceReviewContentProps { customer: Customer invoiceDate: string dueDate: string currency: Currency items: ReviewItem[] subtotal: number vatRate: number vatAmount: number total: number vatTreatment: VatTreatment yourReference?: string ourReference?: string notes?: string } export function InvoiceReviewContent({ customer, invoiceDate, dueDate, currency, items, subtotal, vatRate, vatAmount, total, vatTreatment, yourReference, ourReference, notes, }: InvoiceReviewContentProps) { const customerTypeLabel: Record = { individual: 'Privatperson', swedish_business: 'Svenskt företag', eu_business: 'EU-företag', non_eu_business: 'Utanför EU', } // Check if items have mixed VAT rates const hasPerLineVat = items.some((item) => item.vat_rate !== undefined) const uniqueRates = hasPerLineVat ? new Set(items.map((item) => item.vat_rate ?? vatRate)) : new Set([vatRate]) const showVatColumn = hasPerLineVat && uniqueRates.size > 1 // Calculate per-rate VAT breakdown const vatByRate = new Map() if (hasPerLineVat) { for (const item of items) { const rate = item.vat_rate ?? vatRate const lineTotal = item.quantity * item.unit_price const lineVat = Math.round(lineTotal * rate / 100 * 100) / 100 vatByRate.set(rate, (vatByRate.get(rate) || 0) + lineVat) } } return (
{/* Customer info */}

{customer.name}

{customer.email}

{customerTypeLabel[customer.customer_type] || customer.customer_type}
{/* VAT treatment */} {getVatTreatmentLabel(vatTreatment)} {/* Dates */}
Fakturadatum

{invoiceDate}

Förfallodatum

{dueDate}

{/* Line items table */} {showVatColumn && } {items.map((item, index) => ( {showVatColumn && ( )} ))}
Beskrivning Antal Enhet À-prisMomsBelopp
{item.description} {item.quantity} {item.unit} {formatCurrency(item.unit_price, currency)}{item.vat_rate ?? vatRate}% {formatCurrency(item.quantity * item.unit_price, currency)}
{/* Totals */}
Delsumma {formatCurrency(subtotal, currency)}
{vatByRate.size > 1 ? ( // Per-rate breakdown Array.from(vatByRate.entries()) .filter(([, vat]) => vat > 0) .sort(([a], [b]) => b - a) .map(([rate, vat]) => (
Moms {rate}% {formatCurrency(vat, currency)}
)) ) : (
Moms ({vatRate}%) {formatCurrency(vatAmount, currency)}
)}
Totalt {formatCurrency(total, currency)}
{/* References/notes */} {(yourReference || ourReference || notes) && (
{yourReference &&

Er referens: {yourReference}

} {ourReference &&

Vår referens: {ourReference}

} {notes &&

Anteckning: {notes}

}
)}
) }