fix(dm): energizer orders missing remark
This commit is contained in:
@@ -1,172 +1,172 @@
|
|||||||
import XLSX from "xlsx";
|
import XLSX from "xlsx";
|
||||||
import { tryCatch } from "../../../../../../globalUtils/tryCatch.js";
|
|
||||||
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 { query } from "../../../../../sqlServer/prodSqlServer.js";
|
import { query } from "../../../../../sqlServer/prodSqlServer.js";
|
||||||
|
import { invoiceAddress } from "../../../../../sqlServer/querys/dm/invoiceAddress.js";
|
||||||
import { orderState } from "../../../../../sqlServer/querys/dm/orderState.js";
|
import { orderState } from "../../../../../sqlServer/querys/dm/orderState.js";
|
||||||
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
|
import { excelDateStuff } from "../../../../utils/excelDateStuff.js";
|
||||||
import { invoiceAddress } from "../../../../../sqlServer/querys/dm/invoiceAddress.js";
|
|
||||||
import { postOrders } from "../postOrders.js";
|
import { postOrders } from "../postOrders.js";
|
||||||
|
|
||||||
export const energizerOrders = async (data: any, user: any) => {
|
export const energizerOrders = async (data: any, user: any) => {
|
||||||
/**
|
/**
|
||||||
* Standard orders meaning that we get the standard file exported and fill it out and uplaod to lst.
|
* Standard orders meaning that we get the standard file exported and fill it out and uplaod to lst.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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 {
|
||||||
sucess: false,
|
sucess: false,
|
||||||
message: `Error getting settings`,
|
message: `Error getting settings`,
|
||||||
data: e,
|
data: e,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// order state
|
// order state
|
||||||
const { data: o, error: oe } = await tryCatch(
|
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;
|
const openOrders: any = o?.data;
|
||||||
|
|
||||||
if (oe) {
|
if (oe) {
|
||||||
return {
|
return {
|
||||||
sucess: false,
|
sucess: false,
|
||||||
message: `Error getting article data`,
|
message: `Error getting article data`,
|
||||||
data: oe,
|
data: oe,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// order state
|
// order state
|
||||||
const { data: invoice, error: ie } = await tryCatch(
|
const { data: invoice, error: ie } = await tryCatch(
|
||||||
query(invoiceAddress, "Gets invoices addresses")
|
query(invoiceAddress, "Gets invoices addresses"),
|
||||||
);
|
);
|
||||||
const i: any = invoice?.data;
|
const i: any = invoice?.data;
|
||||||
|
|
||||||
if (ie) {
|
if (ie) {
|
||||||
return {
|
return {
|
||||||
sucess: false,
|
sucess: false,
|
||||||
message: `Error getting invoice address data`,
|
message: `Error getting invoice address data`,
|
||||||
data: ie,
|
data: ie,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
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 sheetName = workbook.SheetNames[0];
|
const sheetName = workbook.SheetNames[0];
|
||||||
const sheet = workbook.Sheets[sheetName];
|
const sheet = workbook.Sheets[sheetName];
|
||||||
|
|
||||||
// define custom headers
|
// define custom headers
|
||||||
const headers = [
|
const headers = [
|
||||||
"ITEM",
|
"ITEM",
|
||||||
"PO",
|
"PO",
|
||||||
"ReleaseNo",
|
"ReleaseNo",
|
||||||
"QTY",
|
"QTY",
|
||||||
"DELDATE",
|
"DELDATE",
|
||||||
"COMMENTS",
|
"COMMENTS",
|
||||||
"What changed",
|
"What changed",
|
||||||
"CUSTOMERID",
|
"CUSTOMERID",
|
||||||
"Remark",
|
"Remark",
|
||||||
];
|
];
|
||||||
const orderData = XLSX.utils.sheet_to_json(sheet, {
|
const orderData = XLSX.utils.sheet_to_json(sheet, {
|
||||||
defval: "",
|
defval: "",
|
||||||
header: headers,
|
header: headers,
|
||||||
range: 1,
|
range: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
// the base of the import
|
// the base of the import
|
||||||
const predefinedObject = {
|
const predefinedObject = {
|
||||||
receivingPlantId: plantToken[0].value,
|
receivingPlantId: plantToken[0].value,
|
||||||
documentName: `OrdersFromLST-${new Date(Date.now()).toLocaleString(
|
documentName: `OrdersFromLST-${new Date(Date.now()).toLocaleString(
|
||||||
"en-US"
|
"en-US",
|
||||||
)}`,
|
)}`,
|
||||||
sender: user.username || "lst-system",
|
sender: user.username || "lst-system",
|
||||||
externalRefNo: `OrdersFromLST-${new Date(Date.now()).toLocaleString(
|
externalRefNo: `OrdersFromLST-${new Date(Date.now()).toLocaleString(
|
||||||
"en-US"
|
"en-US",
|
||||||
)}`,
|
)}`,
|
||||||
orders: [],
|
orders: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
let newOrders: any = orderData;
|
let newOrders: any = orderData;
|
||||||
|
|
||||||
// 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) =>
|
||||||
openOrders.some(
|
openOrders.some(
|
||||||
(o: any) => o.CustomerOrderNumber === oo.CustomerOrderNumber
|
(o: any) => o.CustomerOrderNumber === oo.CustomerOrderNumber,
|
||||||
)
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// filter out the blanks
|
// filter out the blanks
|
||||||
newOrders = newOrders.filter((z: any) => z.ITEM !== "");
|
newOrders = newOrders.filter((z: any) => z.ITEM !== "");
|
||||||
|
|
||||||
// let postedOrders: any = [];
|
// let postedOrders: any = [];
|
||||||
// for (const [customerID, orders] of Object.entries(orderData)) {
|
// for (const [customerID, orders] of Object.entries(orderData)) {
|
||||||
// // console.log(`Running for Customer ID: ${customerID}`);
|
// // console.log(`Running for Customer ID: ${customerID}`);
|
||||||
// const newOrders: any = orderData;
|
// const newOrders: any = orderData;
|
||||||
|
|
||||||
// // 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) =>
|
||||||
// openOrders.some(
|
// openOrders.some(
|
||||||
// (o: any) => o.CustomerOrderNumber === oo.CustomerOrderNumber
|
// (o: any) => o.CustomerOrderNumber === oo.CustomerOrderNumber
|
||||||
// )
|
// )
|
||||||
// );
|
// );
|
||||||
|
|
||||||
// // map everything out for each order
|
// // map everything out for each order
|
||||||
const nOrder = newOrders.map((o: any) => {
|
const nOrder = newOrders.map((o: any) => {
|
||||||
const invoice = i.filter(
|
const invoice = i.filter(
|
||||||
(i: any) => i.deliveryAddress === parseInt(o.CUSTOMERID)
|
(i: any) => i.deliveryAddress === parseInt(o.CUSTOMERID),
|
||||||
);
|
);
|
||||||
if (!invoice) {
|
if (!invoice) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
customerId: parseInt(o.CUSTOMERID),
|
customerId: parseInt(o.CUSTOMERID),
|
||||||
invoiceAddressId: invoice[0].invoiceAddress, // matched to the default invoice address
|
invoiceAddressId: invoice[0].invoiceAddress, // matched to the default invoice address
|
||||||
customerOrderNo: o.PO,
|
customerOrderNo: o.PO,
|
||||||
orderDate: new Date(Date.now()).toLocaleString("en-US"),
|
orderDate: new Date(Date.now()).toLocaleString("en-US"),
|
||||||
positions: [
|
positions: [
|
||||||
{
|
{
|
||||||
deliveryAddressId: parseInt(o.CUSTOMERID),
|
deliveryAddressId: parseInt(o.CUSTOMERID),
|
||||||
customerArticleNo: o.ITEM,
|
customerArticleNo: o.ITEM,
|
||||||
quantity: parseInt(o.QTY),
|
quantity: parseInt(o.QTY),
|
||||||
deliveryDate: o.DELDATE, //excelDateStuff(o.DELDATE),
|
deliveryDate: o.DELDATE, //excelDateStuff(o.DELDATE),
|
||||||
customerLineItemNo: o.ReleaseNo, // this is how it is currently sent over from abbott
|
customerLineItemNo: o.ReleaseNo, // this is how it is currently sent over from abbott
|
||||||
customerReleaseNo: o.ReleaseNo, // same as above
|
customerReleaseNo: o.ReleaseNo, // same as above
|
||||||
remark: o.remark === "" ? null : o.remark,
|
remark: o.COMMENTS === "" ? null : o.COMMENTS,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// // do that fun combining thing
|
// // do that fun combining thing
|
||||||
const updatedPredefinedObject = {
|
const updatedPredefinedObject = {
|
||||||
...predefinedObject,
|
...predefinedObject,
|
||||||
orders: [...predefinedObject.orders, ...nOrder],
|
orders: [...predefinedObject.orders, ...nOrder],
|
||||||
};
|
};
|
||||||
|
|
||||||
// //console.log(updatedPredefinedObject);
|
// //console.log(updatedPredefinedObject);
|
||||||
|
|
||||||
// // post the orders to the server
|
// // post the orders to the server
|
||||||
const posting: any = await postOrders(updatedPredefinedObject, user);
|
const posting: any = await postOrders(updatedPredefinedObject, user);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
customer: nOrder[0].CUSTOMERID,
|
customer: nOrder[0].CUSTOMERID,
|
||||||
//totalOrders: orders?.length(),
|
//totalOrders: orders?.length(),
|
||||||
success: posting.success,
|
success: posting.success,
|
||||||
message: posting.message,
|
message: posting.message,
|
||||||
data: posting.data,
|
data: posting.data,
|
||||||
};
|
};
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// return {
|
// return {
|
||||||
// success: true,
|
// success: true,
|
||||||
// message:
|
// message:
|
||||||
// "Standard Template was just processed successfully, please check AlplaProd 2.0 to confirm no errors. ",
|
// "Standard Template was just processed successfully, please check AlplaProd 2.0 to confirm no errors. ",
|
||||||
// data: nOrder,
|
// data: nOrder,
|
||||||
// };
|
// };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user