Files
Zumri-Backend/app/utils/excel/confirmOrder.excel.js
T
Isuru Bimsara 81d0e65686 first commit
2026-08-14 22:46:02 +05:30

92 lines
2.0 KiB
JavaScript

/**
* Copyright (c) 2026 Niolla
* All rights reserved.
*
* This source code is proprietary and confidential.
* Unauthorized copying, modification, distribution, or use
* of this file, via any medium, is strictly prohibited.
*/
// app/utills/excel/confirmOrder.excel.js
const ExcelJS = require("exceljs");
/**
* Generate Excel template buffer
*/
exports.generateTemplate = async (items) => {
const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet("Confirm Order");
sheet.columns = [
{ header: "Item ID", key: "id", width: 25 },
{ header: "Item Name", key: "item_name", width: 30 },
{ header: "Original Quantity", key: "quantity", width: 20 },
{ header: "Confirmed Quantity", key: "confirmed_quantity", width: 20 },
];
const headerRow = sheet.getRow(1);
headerRow.eachCell((cell) => {
cell.fill = {
type: "pattern",
pattern: "solid",
fgColor: { argb: "1F4E78" },
};
cell.font = {
bold: true,
color: { argb: "FFFFFF" },
};
cell.alignment = {
vertical: "middle",
horizontal: "center",
};
cell.border = {
top: { style: "thin" },
left: { style: "thin" },
bottom: { style: "thin" },
right: { style: "thin" },
};
});
items.forEach((item) => {
sheet.addRow({
id: item.data_id,
item_name: item.item_name,
quantity: item.quantity,
confirmed_quantity: item.quantity,
});
});
return workbook.xlsx.writeBuffer();
};
/**
* Parse uploaded Excel buffer
*/
exports.parseExcel = async (buffer) => {
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.load(buffer);
const sheet = workbook.getWorksheet("Confirm Order");
if (!sheet) throw new Error("Invalid sheet name");
const confirmedMap = new Map();
sheet.eachRow((row, rowNumber) => {
if (rowNumber === 1) return;
const itemId = row.getCell(1).value;
const confirmedQty = Number(row.getCell(4).value);
if (itemId) {
confirmedMap.set(itemId, confirmedQty);
}
});
return confirmedMap;
};