feat(dm): standard forecast and orders in
This commit is contained in:
@@ -18,7 +18,7 @@ export default function ForecastImport(props: any) {
|
|||||||
|
|
||||||
// create the form data with the correct fileType
|
// create the form data with the correct fileType
|
||||||
const formData = new FormData();
|
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
|
formData.append("fileType", props.fileType); // extra field
|
||||||
// console.log(formData);
|
// console.log(formData);
|
||||||
|
|
||||||
@@ -33,10 +33,13 @@ export default function ForecastImport(props: any) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
console.log("Upload successful:", response.data);
|
//console.log("Upload successful:", response.data);
|
||||||
toast.success(
|
toast.success(response?.data?.message);
|
||||||
"File Uploaded, please validate processing in alplaprod 2.0"
|
fileInputRef.current.value = null;
|
||||||
);
|
setPosting(false);
|
||||||
|
// toast.success(
|
||||||
|
// "File Uploaded, please validate processing in alplaprod 2.0"
|
||||||
|
// );
|
||||||
setPosting(false);
|
setPosting(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ export default function OrderImport(props: any) {
|
|||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("postOrders", e.target.files[0]);
|
formData.append("postOrders", e.target.files[0]);
|
||||||
formData.append("fileType", props.fileType); // extra field
|
formData.append("fileType", props.fileType); // extra field
|
||||||
// console.log(formData);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
@@ -33,10 +32,9 @@ export default function OrderImport(props: any) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
console.log("Upload successful:", response.data);
|
//console.log("Upload successful:", response.data);
|
||||||
toast.success(
|
toast.success(response?.data?.message);
|
||||||
"File Uploaded, please validate processing in alplaprod 2.0"
|
fileInputRef.current.value = null;
|
||||||
);
|
|
||||||
setPosting(false);
|
setPosting(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { lorealForecast } from "./mappings/loralForecast.js";
|
||||||
import { standardForecast } from "./mappings/standardForcast.js";
|
import { standardForecast } from "./mappings/standardForcast.js";
|
||||||
|
|
||||||
export const forecastIn = async (data: any, user: any) => {
|
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?
|
// what type of order are we dealing with?
|
||||||
if (data["fileType"] === "standard") {
|
if (data["fileType"] === "standard") {
|
||||||
//run the standard forecast in
|
//run the standard forecast in
|
||||||
const standard = await standardForecast(data["postPostForecast"], user);
|
const standard = await standardForecast(data["postForecast"], user);
|
||||||
success = standard.success ?? false;
|
success = standard.success ?? false;
|
||||||
message = standard.message ?? "Error posting standard forecast";
|
message = standard.message ?? "Error posting standard forecast";
|
||||||
orderData = standard.data;
|
orderData = standard.data;
|
||||||
@@ -23,7 +24,11 @@ export const forecastIn = async (data: any, user: any) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data["fileType"] === "loreal") {
|
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") {
|
if (data["fileType"] === "pg") {
|
||||||
|
|||||||
@@ -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) => {
|
export const standardForecast = 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));
|
||||||
|
|
||||||
|
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 {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "Forecast Posted",
|
message: "Forecast Posted",
|
||||||
data: [],
|
data: foreCastData,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -100,7 +100,27 @@ export const standardOrders = async (data: any, user: any) => {
|
|||||||
let postedOrders: any = [];
|
let postedOrders: any = [];
|
||||||
for (const [customerID, orders] of Object.entries(groupedByCustomer)) {
|
for (const [customerID, orders] of Object.entries(groupedByCustomer)) {
|
||||||
// console.log(`Running for Customer ID: ${customerID}`);
|
// 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.
|
// filter out the orders that have already been started just to reduce the risk of errors.
|
||||||
newOrders.filter((oo: any) =>
|
newOrders.filter((oo: any) =>
|
||||||
@@ -119,7 +139,7 @@ export const standardOrders = async (data: any, user: any) => {
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
customerId: parseInt(customerID),
|
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,
|
customerOrderNo: o.CustomerOrderNumber,
|
||||||
orderDate: new Date(Date.now()).toLocaleString("en-US"),
|
orderDate: new Date(Date.now()).toLocaleString("en-US"),
|
||||||
positions: [
|
positions: [
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { abbottOrders } from "./mappings/abbottTruckList.js";
|
import { abbottOrders } from "./mappings/abbottTruckList.js";
|
||||||
|
import { energizerOrders } from "./mappings/energizerOrdersIn.js";
|
||||||
import { standardOrders } from "./mappings/standardOrders.js";
|
import { standardOrders } from "./mappings/standardOrders.js";
|
||||||
|
|
||||||
export const ordersIn = async (data: any, user: any) => {
|
export const ordersIn = async (data: any, user: any) => {
|
||||||
@@ -29,6 +30,10 @@ export const ordersIn = async (data: any, user: any) => {
|
|||||||
|
|
||||||
if (data["fileType"] === "energizer") {
|
if (data["fileType"] === "energizer") {
|
||||||
// orders in
|
// 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") {
|
if (data["fileType"] === "loreal") {
|
||||||
|
|||||||
@@ -53,10 +53,11 @@ app.openapi(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
console.log(error);
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
success: false,
|
success: false,
|
||||||
message: "Error posting forecast",
|
message: "Error posting Orders",
|
||||||
data: error,
|
data: error,
|
||||||
},
|
},
|
||||||
400
|
400
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ app.openapi(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
console.log(error);
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
success: false,
|
success: false,
|
||||||
|
|||||||
@@ -1,8 +1,25 @@
|
|||||||
export const orderState = `
|
export const orderState = `
|
||||||
SELECT top(500)
|
SELECT top(10000)
|
||||||
CustomerOrderNumber
|
CustomerOrderNumber
|
||||||
, OrderState
|
, OrderState
|
||||||
|
, r.ReleaseState
|
||||||
|
, h.CreatedByEdi
|
||||||
|
|
||||||
--, *
|
--, *
|
||||||
FROM [test1_AlplaPROD2.0_Read].[order].[Header] (nolock)
|
FROM [test1_AlplaPROD2.0_Read].[order].[Header] (nolock) h
|
||||||
where OrderState = 0
|
|
||||||
|
/* 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')
|
||||||
`;
|
`;
|
||||||
|
|||||||
Reference in New Issue
Block a user