'use client'; import { useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { Printer, X, Download, ShieldCheck, FileText } from 'lucide-react'; export interface PrintableQuoteItem { description: string; sku?: string; hsn_sac_code?: string; quantity: number; unit_price: number; tax_percent?: number; total_price: number; } export interface PrintableQuoteData { quote_number: string; created_at: string; valid_until?: string; status: string; customer_name?: string; customer_phone?: string; customer_email?: string; company_name?: string; gst_number?: string; device_info?: string; job_no?: string; subtotal: number; discount_amount?: number; tax_amount: number; grand_total: number; notes?: string; terms?: string; items: PrintableQuoteItem[]; is_interstate?: boolean; // false/undefined = Tamil Nadu (CGST+SGST), true = Outside TN (IGST) } interface PrintableQuotationModalProps { isOpen: boolean; onClose: () => void; data: PrintableQuoteData | null; } export function PrintableQuotationModal({ isOpen, onClose, data }: PrintableQuotationModalProps) { const [mounted, setMounted] = useState(false); useEffect(() => { setMounted(true); }, []); if (!mounted || !isOpen || !data) return null; const handlePrint = () => { window.print(); }; const GST_RATE = 18; const grossTotal = Number(data.grand_total || 0); const discount = Number(data.discount_amount || 0); const taxableSub = Math.round((grossTotal / (1 + GST_RATE / 100)) * 100) / 100; const totalGst = Math.round((grossTotal - taxableSub) * 100) / 100; const isInterstate = data.is_interstate || false; const cgst = isInterstate ? 0 : Math.round((totalGst / 2) * 100) / 100; const sgst = isInterstate ? 0 : Math.round((totalGst / 2) * 100) / 100; const igst = isInterstate ? totalGst : 0; const roundedFinal = Math.round(grossTotal); const roundOff = Math.round((roundedFinal - grossTotal) * 100) / 100; const formattedDate = data.created_at ? new Date(data.created_at).toLocaleDateString('en-IN', { day: '2-digit', month: 'short', year: 'numeric' }) : '—'; const validUntilDate = data.valid_until ? new Date(data.valid_until).toLocaleDateString('en-IN', { day: '2-digit', month: 'short', year: 'numeric' }) : 'Valid for 15 days'; const printCss = ` @media print { @page { size: A4 portrait; margin: 8mm; } body > *:not(#printable-quote-portal-container) { display: none !important; } #printable-quote-portal-container { display: block !important; position: absolute !important; left: 0 !important; top: 0 !important; width: 100% !important; margin: 0 !important; padding: 0 !important; background: white !important; color: black !important; } #printable-quotation-modal { position: static !important; box-shadow: none !important; border: none !important; margin: 0 !important; padding: 0 !important; width: 100% !important; max-width: 100% !important; background: white !important; } .print\\:hidden { display: none !important; } } `; return createPortal( (