test(forecast): loreal forecast starting
This commit is contained in:
@@ -0,0 +1,110 @@
|
|||||||
|
import { db } from "../../../../../../../database/dbclient.js";
|
||||||
|
import { settings } from "../../../../../../../database/schema/settings.js";
|
||||||
|
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
|
||||||
|
import XLSX from "xlsx";
|
||||||
|
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
|
||||||
|
import { postForecast } from "../postForecast.js";
|
||||||
|
|
||||||
|
let customerID = 4;
|
||||||
|
export const lorealForecast = async (data: any, user: any) => {
|
||||||
|
/**
|
||||||
|
* Post a standard forecast based on the standard template.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { data: s, error: e } = await tryCatch(db.select().from(settings));
|
||||||
|
|
||||||
|
if (e) {
|
||||||
|
return {
|
||||||
|
sucess: false,
|
||||||
|
message: `Error getting settings`,
|
||||||
|
data: e,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const plantToken = s.filter((s) => s.name === "plantToken");
|
||||||
|
|
||||||
|
const arrayBuffer = await data.arrayBuffer();
|
||||||
|
const buffer = Buffer.from(arrayBuffer);
|
||||||
|
|
||||||
|
const workbook = XLSX.read(buffer, { type: "buffer" });
|
||||||
|
|
||||||
|
const sheet: any = workbook.Sheets["Alpla HDPE"];
|
||||||
|
const range = XLSX.utils.decode_range(sheet["!ref"]);
|
||||||
|
|
||||||
|
const headers = [];
|
||||||
|
for (let C = range.s.c; C <= range.e.c; ++C) {
|
||||||
|
const cellAddress = XLSX.utils.encode_cell({ r: 0, c: C }); // row 0 = Excel row 1
|
||||||
|
const cell = sheet[cellAddress];
|
||||||
|
headers.push(cell ? cell.v : undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
const forecastData: any = XLSX.utils.sheet_to_json(sheet, {
|
||||||
|
defval: "",
|
||||||
|
header: headers,
|
||||||
|
range: 2,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Extract the customer code (assuming it's always present)
|
||||||
|
const customerCode = forecastData["NORTH HDPE BOTTLES"];
|
||||||
|
|
||||||
|
// Filter out non-date properties (these are your metadata fields)
|
||||||
|
const metadataFields = [
|
||||||
|
"NORTH HDPE BOTTLES",
|
||||||
|
"BLOCKED",
|
||||||
|
"INVENTORY",
|
||||||
|
"CALL-OFF",
|
||||||
|
"PALLET CONSUMPTION",
|
||||||
|
];
|
||||||
|
|
||||||
|
const foreCastData: any = [];
|
||||||
|
|
||||||
|
// process the forcast
|
||||||
|
forecastData.forEach((item: any, index: any) => {
|
||||||
|
//console.log(`Processing item ${index + 1} of ${forecastData.length}`);
|
||||||
|
|
||||||
|
// Extract the customer code
|
||||||
|
const customerCode = item["NORTH HDPE BOTTLES"];
|
||||||
|
|
||||||
|
// Process each date in the current object
|
||||||
|
for (const [date, qty] of Object.entries(item)) {
|
||||||
|
// Skip metadata fields
|
||||||
|
if (metadataFields.includes(date)) continue;
|
||||||
|
|
||||||
|
if (qty === 0) continue;
|
||||||
|
|
||||||
|
// Create your transformed record
|
||||||
|
const record = {
|
||||||
|
customerArticleNo: customerCode,
|
||||||
|
requirementDate: excelDateStuff(parseInt(date)),
|
||||||
|
quantity: qty,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Do something with this record
|
||||||
|
foreCastData.push(record);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const predefinedObject = {
|
||||||
|
receivingPlantId: plantToken[0].value,
|
||||||
|
documentName: `ForecastFromLST-${new Date(Date.now()).toLocaleString(
|
||||||
|
"en-US"
|
||||||
|
)}`,
|
||||||
|
sender: user.username || "lst-system",
|
||||||
|
customerId: customerID,
|
||||||
|
positions: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
let updatedPredefinedObject = {
|
||||||
|
...predefinedObject,
|
||||||
|
positions: [
|
||||||
|
...predefinedObject.positions,
|
||||||
|
...foreCastData.filter((q: any) => q.customerArticleNo != ""),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const posting: any = await postForecast(updatedPredefinedObject, user);
|
||||||
|
return {
|
||||||
|
success: posting.success,
|
||||||
|
message: posting.message,
|
||||||
|
data: posting.data,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user