feat(dm): abbott trucklist will do orders and forecast now

This commit is contained in:
2025-12-11 15:56:34 -06:00
parent 2b5e77993b
commit 501709546d
2 changed files with 246 additions and 151 deletions

View File

@@ -1 +1,96 @@
export const abbottForecast = async () => {};
import XLSX from "xlsx";
import { db } from "../../../../../../../database/dbclient.js";
import { settings } from "../../../../../../../database/schema/settings.js";
import { delay } from "../../../../../../globalUtils/delay.js";
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
import { postForecast } from "../postForecast.js";
export const abbottForecast = async (sheet: any, user: any) => {
const customerId = 8;
const posting: any = [];
const { data: s, error: e } = await tryCatch(db.select().from(settings));
if (e) {
return {
success: false,
message: `Error getting settings`,
data: e,
};
}
const plantToken = s.filter((s) => s.name === "plantToken");
const customHeaders = [
"date",
"time",
"newton8oz",
"newton10oz",
"E",
"F",
"fDate",
"f8ozqty",
"I",
"J",
"K",
"L",
"M",
"f10ozqty",
];
const forecastData = XLSX.utils.sheet_to_json(sheet, {
range: 5, // Start at row 5 (index 4)
header: customHeaders,
defval: "", // Default value for empty cells
});
for (let i = 1; i < forecastData.length; i++) {
const row: any = forecastData[i];
//console.log(row);
//if (row.fDate == undefined) continue;
if (row.fDate !== "") {
const date = isNaN(row.fDate)
? new Date(row.fDate)
: excelDateStuff(row.fDate);
// for 8oz do
if (row.f8ozqty > 0) {
posting.push({
customerArticleNo: "45300DA",
quantity: row.f8ozqty,
requirementDate: date,
});
}
if (row.f10ozqty > 0) {
posting.push({
customerArticleNo: "43836DA",
quantity: row.f10ozqty,
requirementDate: date,
});
}
}
}
// the predefined data that will never change
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
let updatedPredefinedObject = {
...predefinedObject,
positions: [...predefinedObject.positions, ...posting],
};
const forecast: any = await postForecast(updatedPredefinedObject, user);
return {
success: forecast.success,
message: forecast.message,
data: forecast.data,
};
};

View File

@@ -1,12 +1,13 @@
import { addDays, addHours, isAfter, parse } from "date-fns";
import XLSX from "xlsx";
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
import { db } from "../../../../../../../database/dbclient.js";
import { settings } from "../../../../../../../database/schema/settings.js";
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
import { query } from "../../../../../sqlServer/prodSqlServer.js";
import { bulkOrderArticleInfo } from "../../../../../sqlServer/querys/dm/bulkOrderArticleInfo.js";
import { addDays, addHours, isAfter, parse } from "date-fns";
import { orderState } from "../../../../../sqlServer/querys/dm/orderState.js";
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
import { abbottForecast } from "../../forecast/mappings/abbott.js";
import { postOrders } from "../postOrders.js";
// customeris/articles stuff will be in basis once we move to iowa
@@ -32,8 +33,8 @@ export const abbottOrders = async (data: any, user: any) => {
const { data: article, error: ae } = await tryCatch(
query(
bulkOrderArticleInfo.replace("[articles]", articles),
"Get Article data for bulk orders"
)
"Get Article data for bulk orders",
),
);
const a: any = article?.data;
if (ae) {
@@ -46,7 +47,7 @@ export const abbottOrders = async (data: any, user: any) => {
// order state
const { data: o, error: oe } = await tryCatch(
query(orderState, "Gets the next 500 orders that have not been started")
query(orderState, "Gets the next 500 orders that have not been started"),
);
const openOrders: any = o?.data;
@@ -69,6 +70,7 @@ export const abbottOrders = async (data: any, user: any) => {
const sheetName = workbook.SheetNames[0];
const sheet = workbook.Sheets[sheetName];
abbottForecast(sheet, user);
// Define custom headers
const customHeaders = ["date", "time", "newton8oz", "newton10oz"];
const orderData = XLSX.utils.sheet_to_json(sheet, {
@@ -81,11 +83,11 @@ export const abbottOrders = async (data: any, user: any) => {
const predefinedObject = {
receivingPlantId: plantToken[0].value,
documentName: `OrdersFromLST-${new Date(Date.now()).toLocaleString(
"en-US"
"en-US",
)}`,
sender: user.username || "lst-system",
externalRefNo: `OrdersFromLST-${new Date(Date.now()).toLocaleString(
"en-US"
"en-US",
)}`,
orders: [],
};
@@ -95,7 +97,7 @@ export const abbottOrders = async (data: any, user: any) => {
.filter(
(o: any) =>
(o.newton8oz && o.newton8oz.trim() !== "") ||
(o.newton10oz && o.newton10oz.trim() !== "")
(o.newton10oz && o.newton10oz.trim() !== ""),
)
.map((o: any) => ({
date: excelDateStuff(o.date, o.time),
@@ -105,10 +107,8 @@ export const abbottOrders = async (data: any, user: any) => {
: o.newton10oz.replace(/\s+/g, ""),
customerArticlenumber:
o.newton8oz != ""
? a.filter((a: any) => a.av === 118)[0]
.CustomerArticleNumber
: a.filter((a: any) => a.av === 120)[0]
.CustomerArticleNumber,
? a.filter((a: any) => a.av === 118)[0].CustomerArticleNumber
: a.filter((a: any) => a.av === 120)[0].CustomerArticleNumber,
qty:
o.newton8oz != ""
? a.filter((a: any) => a.av === 118)[0].totalTruckLoad
@@ -129,7 +129,7 @@ export const abbottOrders = async (data: any, user: any) => {
const filterOrders: any = correctedOrders;
filterOrders.forEach((oo: any) => {
const isMatch = openOrders.some(
(o: any) => String(o.po).trim() === String(oo.po).trim()
(o: any) => String(o.po).trim() === String(oo.po).trim(),
);
if (!isMatch) {
//console.log(`ok to update: ${oo.po}`);