File size: 3,023 Bytes
f9c88e5
 
4ff7230
60d4775
 
 
4ff7230
60d4775
 
 
4ff7230
 
 
f9c88e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60d4775
f9c88e5
 
 
60d4775
f9c88e5
 
 
4ea623c
 
4ff7230
 
3969441
60d4775
4ea623c
4ff7230
4ea623c
 
 
 
4ff7230
 
 
f9c88e5
 
 
 
 
 
4ea623c
 
f9c88e5
 
 
4ea623c
f9c88e5
 
4ea623c
 
 
 
 
f9c88e5
 
 
60d4775
 
f9c88e5
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
// purchaseOrderService.js - Generates Purchase Order PDFs

/**
 * Generates a single consolidated and customized Purchase Order PDF.
 * @param {Array<Object>} itemsToOrder - An array of objects, each with { material, quantity }.
 * @param {string} supplierName - The name of the supplier for the PO.
 */
export function generatePurchaseOrder(itemsToOrder, supplierName) {
    if (!itemsToOrder || itemsToOrder.length === 0) {
        console.error("No items provided for PO generation.");
        return;
    }

    const { jsPDF } = window.jspdf;
    const doc = new jsPDF();

    const poNumber = `PO-${Date.now()}`;
    const today = new Date().toLocaleDateString();

    // Header
    doc.setFont("helvetica", "bold");
    doc.setFontSize(22);
    doc.text("PURCHASE ORDER", 105, 22, { align: 'center' });

    // Company Info
    doc.setFontSize(12);
    doc.text("FROM:", 20, 40);
    doc.setFont("helvetica", "normal");
    doc.text("Win Super Power Antenna", 20, 46);
    doc.text("Sri Lanka", 20, 52);

    // PO Details
    doc.setFont("helvetica", "bold");
    doc.text("PO Number:", 130, 40);
    doc.setFont("helvetica", "normal");
    doc.text(poNumber, 160, 40);

    doc.setFont("helvetica", "bold");
    doc.text("Date:", 130, 46);
    doc.setFont("helvetica", "normal");
    doc.text(today, 160, 46);

    // Supplier Info
    doc.setFont("helvetica", "bold");
    doc.text("TO:", 20, 70);
    doc.setFont("helvetica", "normal");
    doc.text(supplierName || "________________________ (Supplier Name)", 20, 76);
    doc.text("________________________ (Supplier Address)", 20, 82);


    // --- MODIFIED: Standardized table columns for the supplier (No Prices) ---
    const tableColumn = ["#", "Item Description", "Order Quantity", "Unit"];
    const tableRows = [];

    itemsToOrder.forEach((item, index) => {
        const { material, quantity } = item;
        // --- MODIFIED: Row data now matches the new standardized columns ---
        const materialRow = [
            index + 1,
            material.name,
            quantity,
            material.unit
        ];
        tableRows.push(materialRow);
    });

    doc.autoTable({
        head: [tableColumn],
        body: tableRows,
        startY: 95,
        theme: 'grid',
        headStyles: { fillColor: [45, 55, 72] }
        // --- MODIFIED: Removed the didDrawPage callback to eliminate the Grand Total section ---
    });
    
    // Footer
    const finalY = doc.lastAutoTable.finalY || 150;
    doc.setFontSize(10);
    doc.setTextColor(150);
    doc.text("Please deliver items to the address above. Contact us with any questions regarding this order.", 105, Math.max(finalY + 20, 270), { align: 'center'});

    // Professional signature line
    doc.setFont("helvetica", "normal");
    doc.text("Authorized By: ________________________", 20, Math.max(finalY + 40, 285));


    // Save the PDF
    const safeSupplierName = supplierName.replace(/[\s/]/g, '_') || 'General';
    doc.save(`PO_${safeSupplierName}_${poNumber}.pdf`);
}