Spaces:
Sleeping
Sleeping
File size: 9,300 Bytes
6234767 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | import React, { useState } from 'react';
import { Transaction } from '../types';
import { Download, X, Eye } from 'lucide-react';
import jsPDF from 'jspdf';
import 'jspdf-autotable';
interface PdfInvoiceProps {
transaction: Transaction;
className?: string;
children?: React.ReactNode;
}
const PdfInvoice: React.FC<PdfInvoiceProps> = ({ transaction, className, children }) => {
const [showPreview, setShowPreview] = useState(false);
const [pdfUrl, setPdfUrl] = useState<string>('');
const formatINR = (v: number) => `₹${v.toFixed(2)}`;
const formatDate = (dateStr: string) => {
const date = new Date(dateStr);
return date.toLocaleDateString('en-IN', { day: '2-digit', month: '2-digit', year: 'numeric' });
};
const generatePDF = (download: boolean = false) => {
const doc = new jsPDF();
const subtotal = transaction.subtotal || 0;
const packing = transaction.expenses?.poti_amount || 0;
const cess = transaction.expenses?.cess_amount || 0;
const adat = transaction.expenses?.adat_amount || 0;
const hamali = (transaction.expenses?.hamali_amount || 0) + (transaction.expenses?.packaging_hamali_amount || 0);
const grandTotal = transaction.total_amount || 0;
const paidAmount = transaction.paid_amount || 0;
const balanceAmount = transaction.balance_amount || 0;
// Header - Invoice Date on left, Bill No on right
doc.setFontSize(13);
doc.setFont('helvetica', 'bold');
doc.text(`Invoice Date: ${formatDate(transaction.bill_date)}`, 14, 15);
doc.text(`Bill No: ${transaction.bill_number}`, 196, 15, { align: 'right' });
// Party Details Section - LEFT ALIGNED
doc.setFontSize(11);
doc.setFont('helvetica', 'bold');
doc.text('Party Details', 14, 25);
doc.setLineWidth(0.5);
doc.line(14, 26, 55, 26);
// Party info - explicitly left aligned at x=14
doc.setFont('helvetica', 'normal');
doc.setFontSize(9);
const partyName = transaction.party_name || '-';
const partyCity = transaction.party_city || '-';
const partyPhone = transaction.party_phone || '-';
doc.text(`Name: ${partyName}`, 14, 31, { align: 'left' });
doc.text(`Place: ${partyCity}`, 14, 36, { align: 'left' });
doc.text(`Mobile: ${partyPhone}`, 14, 41, { align: 'left' });
// Items Table
const tableData = transaction.items.map((item, idx) => {
const potiWeights = Array.isArray(item.poti_weights) ? item.poti_weights.join(', ') : (typeof item.poti_weights === 'string' ? item.poti_weights : '-');
const amount = item.net_weight * item.rate_per_kg;
return [idx + 1, `${item.mirchi_name}\n${potiWeights}`, item.poti_count, item.net_weight, item.rate_per_kg, formatINR(amount)];
});
(doc as any).autoTable({
startY: 47,
head: [['#', 'Product Type', 'Bags', 'Net (kg)', 'Rate (₹)', 'Amount (₹)']],
body: tableData,
theme: 'grid',
styles: { fontSize: 8, cellPadding: 2, lineWidth: 0.5, lineColor: [0, 0, 0] },
headStyles: { fillColor: [255, 255, 255], textColor: [0, 0, 0], fontStyle: 'bold' },
bodyStyles: { textColor: [0, 0, 0] },
columnStyles: {
0: { halign: 'center', cellWidth: 10 },
1: { halign: 'left', cellWidth: 60 },
2: { halign: 'center', cellWidth: 20 },
3: { halign: 'center', cellWidth: 25 },
4: { halign: 'center', cellWidth: 25 },
5: { halign: 'right', cellWidth: 35 }
}
});
// Summary Table - Right side
const finalY = (doc as any).lastAutoTable.finalY + 5;
const summaryData = [
['Sub Total', formatINR(subtotal)],
['Packing', formatINR(packing)],
['CESS', formatINR(cess)],
['Adat', formatINR(adat)],
['Hamali', formatINR(hamali)],
['Total Amount', formatINR(grandTotal)]
];
(doc as any).autoTable({
startY: finalY,
body: summaryData,
theme: 'grid',
styles: { fontSize: 8, cellPadding: 2, lineWidth: 0.5, lineColor: [0, 0, 0] },
bodyStyles: { textColor: [0, 0, 0] },
columnStyles: {
0: { halign: 'left', cellWidth: 50 },
1: { halign: 'right', cellWidth: 35 }
},
margin: { left: 111 }
});
// Payment Status Section - LEFT ALIGNED
const paymentY = (doc as any).lastAutoTable.finalY + 10;
doc.setFontSize(11);
doc.setFont('helvetica', 'bold');
doc.text('Payment Status', 14, paymentY, { align: 'left' });
doc.setLineWidth(0.5);
doc.line(14, paymentY + 1, 60, paymentY + 1);
doc.setFont('helvetica', 'normal');
doc.setFontSize(9);
let currentY = paymentY + 6;
if (transaction.payments && transaction.payments.length > 0) {
transaction.payments.forEach((payment) => {
const mode = payment.mode === 'cash' ? 'Cash' : payment.mode === 'online' ? 'Online' : payment.mode;
doc.text(`${mode}: ${formatINR(payment.amount)}`, 14, currentY, { align: 'left' });
currentY += 5;
});
doc.setFont('helvetica', 'bold');
doc.text(`Total Paid: ${formatINR(paidAmount)}`, 14, currentY, { align: 'left' });
currentY += 5;
} else {
doc.text(`Paid Amount: ${formatINR(paidAmount)}`, 14, currentY, { align: 'left' });
currentY += 5;
}
if (balanceAmount > 0) {
doc.setFont('helvetica', 'bold');
doc.setTextColor(211, 47, 47);
doc.text(`Due Amount: ${formatINR(balanceAmount)}`, 14, currentY, { align: 'left' });
doc.setTextColor(0, 0, 0);
}
// Signature - Right side
doc.setFont('helvetica', 'normal');
doc.setFontSize(9);
doc.text('(Authorised Signatory)', 196, paymentY + 20, { align: 'right' });
if (download) {
doc.save(`Invoice_${transaction.bill_number}.pdf`);
} else {
const pdfBlob = doc.output('blob');
const url = URL.createObjectURL(pdfBlob);
setPdfUrl(url);
setShowPreview(true);
}
};
const handleDownload = () => {
generatePDF(true);
setShowPreview(false);
if (pdfUrl) {
URL.revokeObjectURL(pdfUrl);
}
};
const handleClose = () => {
setShowPreview(false);
if (pdfUrl) {
URL.revokeObjectURL(pdfUrl);
}
};
return (
<>
<button
onClick={() => generatePDF(false)}
className={className || "p-2 bg-white text-red-600 border border-red-600 rounded-md hover:bg-red-50 transition-colors flex items-center justify-center shadow-sm"}
title="Preview & Download PDF"
>
{children || <Eye size={16} />}
</button>
{showPreview && (
<div className="fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center p-4">
<div className="bg-white rounded-lg shadow-2xl max-w-6xl w-full max-h-[90vh] flex flex-col">
<div className="border-b border-gray-200 px-6 py-4 flex justify-between items-center">
<h2 className="text-xl font-bold text-gray-800">PDF Preview</h2>
<div className="flex gap-2">
<button
onClick={handleDownload}
className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors flex items-center gap-2"
>
<Download size={18} />
Download PDF
</button>
<button
onClick={handleClose}
className="p-2 hover:bg-gray-100 rounded-lg transition-colors"
>
<X size={24} className="text-gray-600" />
</button>
</div>
</div>
<div className="flex-1 overflow-auto p-4 bg-gray-100">
<iframe
src={pdfUrl}
className="w-full h-full min-h-[600px] bg-white rounded shadow-lg"
title="PDF Preview"
/>
</div>
</div>
</div>
)}
</>
);
};
export default PdfInvoice; |