fix(dm): energizer forecast to correct the date and qty push over

This commit is contained in:
2025-12-11 14:42:22 -06:00
parent 420826de9b
commit 7a9ea16f48

View File

@@ -1,95 +1,102 @@
import { addDays } from "date-fns";
import XLSX from "xlsx";
import { db } from "../../../../../../../database/dbclient.js"; import { db } from "../../../../../../../database/dbclient.js";
import { settings } from "../../../../../../../database/schema/settings.js"; import { settings } from "../../../../../../../database/schema/settings.js";
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js"; import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
import XLSX from "xlsx"; import { createLog } from "../../../../../logger/logger.js";
import { postForecast } from "../postForecast.js"; import { sendEmail } from "../../../../../notifications/controller/sendMail.js";
import { query } from "../../../../../sqlServer/prodSqlServer.js"; import { query } from "../../../../../sqlServer/prodSqlServer.js";
import { activeArticle } from "../../../../../sqlServer/querys/dataMart/article.js"; import { activeArticle } from "../../../../../sqlServer/querys/dataMart/article.js";
import { addDays } from "date-fns"; import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
import { sendEmail } from "../../../../../notifications/controller/sendMail.js"; import { postForecast } from "../postForecast.js";
import { createLog } from "../../../../../logger/logger.js";
export const energizerForecast = async (data: any, user: any) => { export const energizerForecast = async (data: any, user: any) => {
/** /**
* Post a standard forecast based on the standard template. * Post a standard forecast based on the standard template.
*/ */
const { data: s, error: e } = await tryCatch(db.select().from(settings)); const { data: s, error: e } = await tryCatch(db.select().from(settings));
if (e) { if (e) {
return { return {
success: false, success: false,
message: `Error getting settings`, message: `Error getting settings`,
data: e, data: e,
}; };
} }
const plantToken = s.filter((s) => s.name === "plantToken"); const plantToken = s.filter((s) => s.name === "plantToken");
const arrayBuffer = await data.arrayBuffer(); const arrayBuffer = await data.arrayBuffer();
const buffer = Buffer.from(arrayBuffer); const buffer = Buffer.from(arrayBuffer);
const workbook = XLSX.read(buffer, { type: "buffer" }); const workbook = XLSX.read(buffer, { type: "buffer" });
const sheet: any = workbook.Sheets["Sheet1"]; const sheet: any = workbook.Sheets["Sheet1"];
const range = XLSX.utils.decode_range(sheet["!ref"]); const range = XLSX.utils.decode_range(sheet["!ref"]);
const headers = [ const headers = [
"CustomerArticleNumber", "CustomerArticleNumber",
"Quantity", "Quantity",
"RequirementDate", "RequirementDate",
"CustomerID", "CustomerID",
]; ];
// formatting the data // formatting the data
const rows = XLSX.utils.sheet_to_json(sheet, { header: 1 }) as any; const rows = XLSX.utils.sheet_to_json(sheet, { header: 1 }) as any;
const posting: any = []; const posting: any = [];
const customerId = 44; const customerId = 44;
for (let i = 1; i < rows.length; i++) { for (let i = 1; i < rows.length; i++) {
const row: any = rows[i]; const row: any = rows[i];
const material = row[0]; const material = row[0];
if (material == undefined) continue; if (material == undefined) continue;
for (let j = 1; j < row.length; j++) { for (let j = 1; j < row.length; j++) {
const qty = row[j]; const qty = row[j];
if (qty && qty !== 0) { if (qty && qty > 0) {
const requirementDate = rows[0][j]; // first row is dates const requirementDate = rows[0][j]; // first row is dates
const date = isNaN(requirementDate)
? new Date(requirementDate)
: excelDateStuff(requirementDate);
posting.push({ console.log(isNaN(requirementDate), requirementDate, date);
customerArticleNo: material, posting.push({
quantity: qty, customerArticleNo: material,
requirementDate: new Date(requirementDate), quantity: qty,
}); requirementDate: date,
} });
} }
} }
}
// the predefined data that will never change //console.log(posting);
const predefinedObject = {
receivingPlantId: plantToken[0].value,
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 // the predefined data that will never change
let updatedPredefinedObject = { const predefinedObject = {
...predefinedObject, receivingPlantId: plantToken[0].value,
positions: [...predefinedObject.positions, ...posting], documentName: `ForecastFromLST-${new Date(Date.now()).toLocaleString(
}; "en-US",
)}`,
sender: user.username || "lst-system",
customerId: customerId,
positions: [],
};
//post it // add the new forecast to the predefined data
const forecastData: any = await postForecast(updatedPredefinedObject, user); let updatedPredefinedObject = {
...predefinedObject,
positions: [...predefinedObject.positions, ...posting],
};
return { //post it
success: forecastData.success, const forecastData: any = await postForecast(updatedPredefinedObject, user);
message: forecastData.message,
data: forecastData.data, return {
}; success: forecastData.success,
message: forecastData.message,
data: forecastData.data,
};
}; };