File size: 2,760 Bytes
f9c88e5
 
4ff7230
 
 
 
 
 
 
 
 
 
f9c88e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ff7230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9c88e5
 
 
 
 
 
 
 
 
 
4ff7230
f9c88e5
 
4ff7230
f9c88e5
 
 
4ff7230
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
// purchaseOrderService.js - Generates Purchase Order PDFs

/**
 * Generates a single consolidated Purchase Order PDF for multiple materials.
 * @param {Array<Object>} materialsToOrder - An array of material objects that need to be reordered.
 */
export function generatePurchaseOrder(materialsToOrder) {
    if (!materialsToOrder || materialsToOrder.length === 0) {
        console.error("No materials 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 (Placeholder)
    doc.setFont("helvetica", "bold");
    doc.text("TO:", 20, 70);
    doc.setFont("helvetica", "normal");
    doc.text("________________________ (Supplier Name)", 20, 76);
    doc.text("________________________ (Supplier Address)", 20, 82);


    // Items Table
    const tableColumn = ["Item Description", "Current Stock", "Reorder Point", "Max Stock", "Quantity to Order", "Unit"];
    const tableRows = [];

    materialsToOrder.forEach(material => {
        const quantityToOrder = material.maxStock - material.currentStock;
        const materialRow = [
            material.name,
            material.currentStock,
            material.reorderPoint,
            material.maxStock,
            quantityToOrder > 0 ? quantityToOrder : "N/A", // Handle case where stock is somehow negative
            material.unit
        ];
        tableRows.push(materialRow);
    });

    doc.autoTable({
        head: [tableColumn],
        body: tableRows,
        startY: 95,
        theme: 'grid',
        headStyles: { fillColor: [45, 55, 72] } // Dark blue header
    });
    
    // Footer
    const finalY = doc.lastAutoTable.finalY || 150; // Get Y position after table
    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, 280), { align: 'center'});


    // Save the PDF
    doc.save(`Consolidated_PO_${poNumber}.pdf`);
}