import XLSX from "xlsx"; import { excelDateStuff } from "../utils/excelToDate.utils.js"; import { postData } from "./logistics.dm.postData.js"; export const energizerForecast = async (data: any, user: any) => { /** * Post a standard forecast based on the standard template. */ const buffer = Buffer.from(data.buffer); const workbook = XLSX.read(buffer, { type: "buffer" }); const sheet: any = workbook.Sheets.Sheet1; // const range = XLSX.utils.decode_range(sheet["!ref"]); // const headers = [ // "CustomerArticleNumber", // "Quantity", // "RequirementDate", // "CustomerID", // ]; // formatting the data const rows = XLSX.utils.sheet_to_json(sheet, { header: 1 }) as any; const posting: any = []; const customerId = 44; for (let i = 1; i < rows.length; i++) { const row: any = rows[i]; const material = row[0]; if (material === undefined) continue; for (let j = 1; j < row.length; j++) { const qty = row[j]; if (qty && qty > 0) { const requirementDate = rows[0][j]; // first row is dates const date = Number.isNaN(requirementDate) ? new Date(requirementDate) : excelDateStuff(requirementDate); posting.push({ customerArticleNo: material, quantity: qty, requirementDate: date, }); } } } //console.log(posting); // the predefined data that will never change const predefinedObject = { receivingPlantId: process.env.PROD_PLANT_TOKEN ?? "test1", documentName: `ForecastFromLST-${new Date(Date.now()).toLocaleString( "en-US", )}`, sender: user.username || "lst-system", customerId: customerId, positions: [], }; // add the new forecast to the predefined data const updatedPredefinedObject = { ...predefinedObject, positions: [...predefinedObject.positions, ...posting], }; //post it const forecastData: any = await postData( { type: "forecast", endpoint: "/public/v1.0/DemandManagement/DELFOR", data: updatedPredefinedObject as any, }, user, ); return { success: forecastData.success, message: forecastData.message, data: forecastData.data, }; };