'use client'; import { useMemo, useState } from 'react'; import { Page, Text, View, Document, Image, StyleSheet, Font, pdf, } from '@react-pdf/renderer'; import { Icon } from '@iconify/react'; import Button from '@/components/Button'; import { Purchase } from '@/types/api/purchase/purchase'; import { formatDate, formatNumber } from '@/lib/helper'; Font.register({ family: 'Helvetica', src: 'helvetica', }); const pdfStyles = StyleSheet.create({ page: { fontSize: 10, fontFamily: 'Helvetica', padding: 20, backgroundColor: '#FFFFFF', }, header: { marginBottom: 20, }, logo: { width: 120, height: 30, marginBottom: 8, }, companyInfo: { fontSize: 12, fontWeight: 'bold', marginBottom: 4, color: '#1f74bf', // Primary color from globals.css }, address: { fontSize: 8, color: '#666666', maxWidth: 400, marginBottom: 10, }, divider: { borderBottomWidth: 1, borderBottomColor: '#000000', borderBottomStyle: 'solid', marginBottom: 15, }, titleSection: { flexDirection: 'row', marginBottom: 20, justifyContent: 'space-between', alignItems: 'flex-start', }, title: { fontSize: 18, fontWeight: 'bold', flex: 3, color: '#1f74bf', // Primary color }, poInfo: { flex: 1, fontSize: 9, textAlign: 'right', }, sectionTitle: { fontSize: 12, fontWeight: 'bold', marginBottom: 8, color: '#1f74bf', // Primary color }, table: { borderWidth: 1, borderColor: '#000000', marginBottom: 15, }, tableRow: { flexDirection: 'row', }, tableHeader: { backgroundColor: '#F5F5F5', }, tableCell: { flex: 1, borderRightWidth: 1, borderRightColor: '#000000', borderRightStyle: 'solid', padding: 8, fontSize: 9, }, tableCellLast: { flex: 1, padding: 8, fontSize: 9, }, tableCellHeader: { flex: 1, borderRightWidth: 1, borderRightColor: '#000000', borderRightStyle: 'solid', padding: 8, fontSize: 9, fontWeight: 'bold', backgroundColor: '#F5F5F5', }, tableCellHeaderLast: { flex: 1, padding: 8, fontSize: 9, fontWeight: 'bold', backgroundColor: '#F5F5F5', }, tableCellRight: { flex: 1, borderRightWidth: 1, borderRightColor: '#000000', borderRightStyle: 'solid', padding: 8, fontSize: 9, textAlign: 'right', }, tableCellRightLast: { flex: 1, padding: 8, fontSize: 9, textAlign: 'right', }, tableBorderBottom: { borderBottomWidth: 1, borderBottomColor: '#000000', borderBottomStyle: 'solid', }, grandTotalRow: { flexDirection: 'row', borderTopWidth: 1, borderTopColor: '#000000', borderTopStyle: 'solid', }, grandTotalLabel: { flex: 3, padding: 8, fontSize: 9, fontWeight: 'bold', textAlign: 'right', borderRightWidth: 1, borderRightColor: '#000000', borderRightStyle: 'solid', }, grandTotalValue: { flex: 1, padding: 8, fontSize: 9, fontWeight: 'bold', textAlign: 'right', borderRightWidth: 0, }, allocationSection: { marginBottom: 15, }, allocationTable: { borderWidth: 1, borderColor: '#000000', }, innerTable: { marginTop: 5, }, innerRow: { flexDirection: 'row', borderBottomWidth: 0.5, borderBottomColor: '#ccc', borderBottomStyle: 'solid', }, innerCell: { flex: 1, padding: 4, fontSize: 8, }, footer: { marginTop: 30, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'flex-start', }, footerCompany: { fontSize: 12, fontWeight: 'bold', textAlign: 'right', flex: 1, color: '#1f74bf', }, specialInstructionTable: { width: '60%', maxWidth: 300, borderWidth: 1, borderColor: '#000000', flex: 1, }, }); interface PurchaseOrderInvoiceProps { data?: Purchase; className?: string; } const PurchaseOrderInvoice = ({ data }: PurchaseOrderInvoiceProps) => { const [, setIsGeneratingPDF] = useState(false); const purchaseData = data; const grandTotal = useMemo(() => { return ( purchaseData?.items?.reduce( (sum, item) => sum + (item.total_price || 0), 0 ) || 0 ); }, [purchaseData?.items]); const handleDownloadPDF = async () => { if (!purchaseData) { alert('No purchase order data available'); return; } setIsGeneratingPDF(true); try { const PDFDocument = () => ( {/* Header Section */} PT MITRA BERLIAN UNGGAS SOHO Building Lt.3 (Paris Van Java), Jalan Karang Tinggal, Kel. Cipedes, Kec. Sukajadi, Kota Bandung 40162 {/* Purchase Order Title */} PURCHASE ORDER PO Number: {purchaseData?.po_number || '-'} Date:{' '} {purchaseData?.po_date ? formatDate(purchaseData.po_date, 'DD-MMM-YYYY') : formatDate(new Date(), 'DD-MMM-YYYY')} {/* Vendor and Ship To Table */} Vendor Ship To {purchaseData?.supplier?.name || '-'} {purchaseData?.supplier?.pic || '-'} {purchaseData?.supplier?.phone || '-'} /{' '} {purchaseData?.supplier?.email || '-'} {purchaseData?.supplier?.address || '-'} PT MITRA BERLIAN UNGGAS {purchaseData?.location?.name || '-'} {/* Item Description Table */} Item Description Unit Price Total Quantity Total Amount {purchaseData?.items?.map((item, index) => { const isLastItem = index === (purchaseData?.items?.length || 0) - 1; return ( {item.product?.name || '-'} Rp.{formatNumber(item.price || 0)} {formatNumber(item.total_qty || 0)} Rp.{formatNumber(item.total_price || 0)} ); }) || []} {/* Grand Total Row inside table */} Grand Total Rp.{formatNumber(grandTotal)} {/* Product Allocation Section */} Product Allocation Warehouse Name PIC Address Detail Product Allocation {purchaseData?.warehouse?.name || '-'} {purchaseData?.created_user?.name || '-'} {purchaseData?.created_user?.email || '-'} {purchaseData?.location?.name || '-'} {/* Inner table for product allocation */} {purchaseData?.items?.map((item, index) => ( {item.product?.name || '-'} {formatNumber(item.total_qty || 0)} )) || []} {/* Footer with Special Instructions */} Special Instruction {purchaseData?.notes || '-'} PT MITRA BERLIAN UNGGAS ); const blob = await pdf().toBlob(); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `${purchaseData?.po_number || 'purchase-order'}.pdf`; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); } catch (error) { console.error('Error generating PDF:', error); alert('Failed to generate PDF. Please try again.'); } finally { setIsGeneratingPDF(false); } }; if (!purchaseData) { return (
No purchase order data available
); } return purchaseData?.po_number && purchaseData.po_number !== 'Belum dibuat' ? ( ) : null; }; export default PurchaseOrderInvoice;