import Button from '@/components/Button'; import { Marketing } from '@/types/api/marketing/marketing'; import { Icon } from '@iconify/react'; import { Document, Image, Page, pdf, Text, View } from '@react-pdf/renderer'; import { useMemo, useState } from 'react'; import { formatDate, formatNumber } from '@/lib/helper'; import pdfStyles from '@/components/pages/marketing/pdf/styles/MarketingPDFStyles'; import toast from 'react-hot-toast'; interface SalesOrderExportProps { data?: Marketing; className?: string; } const SalesOrderExport = ({ data }: SalesOrderExportProps) => { const [isGeneratingPDF, setIsGeneratingPDF] = useState(false); const salesData = data; const handleDownloadPDF = async () => { if (!salesData) { toast.error('No sales order data available'); return; } setIsGeneratingPDF(true); try { const blob = await pdf().toBlob(); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `${salesData?.so_number || 'sales-order'}.pdf`; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); } catch (error) { toast.error('Failed to generate PDF. Please try again.'); } finally { setIsGeneratingPDF(false); } }; if (!salesData) { return (
No sales order data available
); } return salesData?.so_number && salesData.so_number !== 'Belum dibuat' ? ( ) : null; }; export default SalesOrderExport; const PDFDocument = ({ data }: { data: Marketing }) => { const grandTotal = useMemo(() => { return data?.sales_order?.reduce((a, b) => a + b.total_price, 0) ?? 0; }, [data?.sales_order]); return ( {/* Header Section */} PT LUMBUNG TELUR INDONESIA SOHO Building Lt.3 (Paris Van Java), Jalan Karang Tinggal, Kel. Cipedes, Kec. Sukajadi, Kota Bandung 40162 {/* Sales Order Title */} SALES ORDER SO Number: {data?.so_number || '-'} Date:{' '} {data?.so_date ? formatDate(data.so_date, 'DD MMM YYYY') : formatDate(new Date(), 'DD MMM YYYY')} {/* Customer Table */} Customer Sales {data?.customer?.name || '-'} ({data?.customer?.type || '-'}) {data?.customer.email || ''} - {data?.customer.phone || ''} {data?.customer.address || ''} PT LUMBUNG TELUR INDONESIA {data?.sales_person?.name || '-'} {data?.sales_person.email} {/* Product Sales Order Table */} Product Sold Item Description From Unit Price Quantity Total Amount {data?.sales_order?.map((item, index) => { const isLastItem = index === (data?.sales_order?.length || 0) - 1; return ( {item.product_warehouse?.product?.name || '-'} {item.product_warehouse?.warehouse?.name || '-'} Rp{formatNumber(item.unit_price || 0)} {formatNumber(item.qty || 0)} Rp{formatNumber(item.total_price || 0)} ); }) || []} {/* Grand Total Row inside table */} Grand Total Rp{formatNumber(grandTotal)} {/* Footer with Special Instructions */} Notes {data?.notes || '-'} PT LUMBUNG TELUR INDONESIA ); };