feat(dm): standard forecast and orders in

This commit is contained in:
2025-04-23 15:19:34 -05:00
parent 3c9e627021
commit 28b050859a
9 changed files with 172 additions and 19 deletions

View File

@@ -18,7 +18,7 @@ export default function ForecastImport(props: any) {
// create the form data with the correct fileType
const formData = new FormData();
formData.append("postOrders", e.target.files[0]);
formData.append("postForecast", e.target.files[0]);
formData.append("fileType", props.fileType); // extra field
// console.log(formData);
@@ -33,10 +33,13 @@ export default function ForecastImport(props: any) {
},
}
);
console.log("Upload successful:", response.data);
toast.success(
"File Uploaded, please validate processing in alplaprod 2.0"
);
//console.log("Upload successful:", response.data);
toast.success(response?.data?.message);
fileInputRef.current.value = null;
setPosting(false);
// toast.success(
// "File Uploaded, please validate processing in alplaprod 2.0"
// );
setPosting(false);
} catch (error) {
console.log(error);

View File

@@ -20,7 +20,6 @@ export default function OrderImport(props: any) {
const formData = new FormData();
formData.append("postOrders", e.target.files[0]);
formData.append("fileType", props.fileType); // extra field
// console.log(formData);
try {
const response = await axios.post(
@@ -33,10 +32,9 @@ export default function OrderImport(props: any) {
},
}
);
console.log("Upload successful:", response.data);
toast.success(
"File Uploaded, please validate processing in alplaprod 2.0"
);
//console.log("Upload successful:", response.data);
toast.success(response?.data?.message);
fileInputRef.current.value = null;
setPosting(false);
} catch (error) {
console.log(error);

View File

@@ -1,3 +1,4 @@
import { lorealForecast } from "./mappings/loralForecast.js";
import { standardForecast } from "./mappings/standardForcast.js";
export const forecastIn = async (data: any, user: any) => {
@@ -12,7 +13,7 @@ export const forecastIn = async (data: any, user: any) => {
// what type of order are we dealing with?
if (data["fileType"] === "standard") {
//run the standard forecast in
const standard = await standardForecast(data["postPostForecast"], user);
const standard = await standardForecast(data["postForecast"], user);
success = standard.success ?? false;
message = standard.message ?? "Error posting standard forecast";
orderData = standard.data;
@@ -23,7 +24,11 @@ export const forecastIn = async (data: any, user: any) => {
}
if (data["fileType"] === "loreal") {
// orders in
//run the standard forecast in
const loreal = await lorealForecast(data["postForecast"], user);
success = loreal.success ?? false;
message = loreal.message ?? "Error posting standard forecast";
orderData = loreal.data;
}
if (data["fileType"] === "pg") {

View File

@@ -1,11 +1,114 @@
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";
export const standardForecast = 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 sheetName = workbook.SheetNames[0];
const sheet = workbook.Sheets[sheetName];
const headers = [
"CustomerArticleNumber",
"Quantity",
"RequirementDate",
"CustomerID",
];
const forecastData: any = XLSX.utils.sheet_to_json(sheet, {
defval: "",
header: headers,
range: 1,
});
const groupedByCustomer: any = forecastData.reduce(
(acc: any, item: any) => {
const id = item.CustomerID;
if (!acc[id]) {
acc[id] = [];
}
acc[id].push(item);
return acc;
},
{}
);
const foreCastData: any = [];
for (const [customerID, forecast] of Object.entries(groupedByCustomer)) {
//console.log(`Running for Customer ID: ${customerID}`);
const newForecast: any = forecast;
const predefinedObject = {
receivingPlantId: plantToken[0].value,
documentName: `ForecastFromLST-${new Date(
Date.now()
).toLocaleString("en-US")}`,
sender: user.username || "lst-system",
customerId: customerID,
positions: [],
};
// map everything out for each order
const nForecast = newForecast.map((o: any) => {
// const invoice = i.filter(
// (i: any) => i.deliveryAddress === parseInt(customerID)
// );
// if (!invoice) {
// return;
// }
return {
customerArticleNo: o.CustomerArticleNumber,
requirementDate: excelDateStuff(parseInt(o.RequirementDate)),
quantity: o.Quantity,
};
});
// do that fun combining thing
let updatedPredefinedObject = {
...predefinedObject,
positions: [...predefinedObject.positions, ...nForecast],
};
//console.log(updatedPredefinedObject);
// post the orders to the server
const posting: any = await postForecast(updatedPredefinedObject, user);
foreCastData.push({
customer: customerID,
//totalOrders: orders?.length(),
success: posting.success,
message: posting.message,
data: posting.data,
});
}
return {
success: true,
message: "Forecast Posted",
data: [],
data: foreCastData,
};
};

View File

@@ -100,7 +100,27 @@ export const standardOrders = async (data: any, user: any) => {
let postedOrders: any = [];
for (const [customerID, orders] of Object.entries(groupedByCustomer)) {
// console.log(`Running for Customer ID: ${customerID}`);
const newOrders: any = orders;
const filterOrders: any = orders;
const newOrders: any = [];
//newOrders.filter((oo) => openOrders.some((o) => String(o.CustomerOrderNumber) === String(oo.CustomerOrderNumber)));
//console.log(newOrders)
filterOrders.forEach((oo: any) => {
const isMatch = openOrders.some(
(o: any) =>
String(o.CustomerOrderNumber).trim() ===
String(oo.CustomerOrderNumber).trim()
);
if (!isMatch) {
console.log(`ok to update: ${oo.CustomerOrderNumber}`);
newOrders.push(oo);
} else {
console.log(
`Not valid order to update: ${oo.CustomerOrderNumber}`
);
//console.log(oo)
}
});
// filter out the orders that have already been started just to reduce the risk of errors.
newOrders.filter((oo: any) =>
@@ -119,7 +139,7 @@ export const standardOrders = async (data: any, user: any) => {
}
return {
customerId: parseInt(customerID),
invoiceAddressId: invoice[0].invoiceAddress, // matched to the default invoice address
invoiceAddressId: invoice[0]?.invoiceAddress, // matched to the default invoice address
customerOrderNo: o.CustomerOrderNumber,
orderDate: new Date(Date.now()).toLocaleString("en-US"),
positions: [

View File

@@ -1,4 +1,5 @@
import { abbottOrders } from "./mappings/abbottTruckList.js";
import { energizerOrders } from "./mappings/energizerOrdersIn.js";
import { standardOrders } from "./mappings/standardOrders.js";
export const ordersIn = async (data: any, user: any) => {
@@ -29,6 +30,10 @@ export const ordersIn = async (data: any, user: any) => {
if (data["fileType"] === "energizer") {
// orders in
const energizer = await energizerOrders(data["postOrders"], user);
success = energizer.success ?? false;
message = energizer.message ?? "Error posting Energizer Orders";
orderData = energizer.data;
}
if (data["fileType"] === "loreal") {

View File

@@ -53,10 +53,11 @@ app.openapi(
);
if (error) {
console.log(error);
return c.json(
{
success: false,
message: "Error posting forecast",
message: "Error posting Orders",
data: error,
},
400

View File

@@ -54,6 +54,7 @@ app.openapi(
);
if (error) {
console.log(error);
return c.json(
{
success: false,

View File

@@ -1,8 +1,25 @@
export const orderState = `
SELECT top(500)
SELECT top(10000)
CustomerOrderNumber
, OrderState
, r.ReleaseState
, h.CreatedByEdi
--, *
FROM [test1_AlplaPROD2.0_Read].[order].[Header] (nolock)
where OrderState = 0
FROM [test1_AlplaPROD2.0_Read].[order].[Header] (nolock) h
/* get the line items to link to the headers */
left join
[test1_AlplaPROD2.0_Read].[order].[LineItem] (nolock) l on
l.HeaderId = h.id
/* get the releases to link to the headers */
left join
[test1_AlplaPROD2.0_Read].[order].[Release] (nolock) r on
r.LineItemId = l.id
where
--h.CreatedByEdi = 1
r.ReleaseState > 0
--and CustomerOrderNumber in ( '2365862', '2360391')
`;