From e0df6b8cd7bfe07415b7d0d074cb4b60c94f2b44 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Wed, 23 Apr 2025 15:27:28 -0500 Subject: [PATCH] feat(orders): energizer orders in --- .../dm/ordersIn/mappings/energizerOrdersIn.ts | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 server/services/logistics/controller/dm/ordersIn/mappings/energizerOrdersIn.ts diff --git a/server/services/logistics/controller/dm/ordersIn/mappings/energizerOrdersIn.ts b/server/services/logistics/controller/dm/ordersIn/mappings/energizerOrdersIn.ts new file mode 100644 index 0000000..c085cb4 --- /dev/null +++ b/server/services/logistics/controller/dm/ordersIn/mappings/energizerOrdersIn.ts @@ -0,0 +1,167 @@ +import XLSX from "xlsx"; +import { tryCatch } from "../../../../../../globalUtils/tryCatch.js"; +import { db } from "../../../../../../../database/dbclient.js"; +import { settings } from "../../../../../../../database/schema/settings.js"; +import { query } from "../../../../../sqlServer/prodSqlServer.js"; +import { orderState } from "../../../../../sqlServer/querys/dm/orderState.js"; +import { excelDateStuff } from "../../../../utils/excelDateStuff.js"; +import { invoiceAddress } from "../../../../../sqlServer/querys/dm/invoiceAddress.js"; +import { postOrders } from "../postOrders.js"; + +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. + */ + + const { data: s, error: e } = await tryCatch(db.select().from(settings)); + + if (e) { + return { + sucess: false, + message: `Error getting settings`, + data: e, + }; + } + + // order state + const { data: openOrders, error: oe } = await tryCatch( + query(orderState, "Gets the next 500 orders that have not been started") + ); + + if (oe) { + return { + sucess: false, + message: `Error getting article data`, + data: oe, + }; + } + + // order state + const { data: i, error: ie } = await tryCatch( + query(invoiceAddress, "Gets invoices addresses") + ); + + if (ie) { + return { + sucess: false, + message: `Error getting invoice address data`, + data: ie, + }; + } + 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]; + + // define custom headers + const headers = [ + "ITEM", + "PO", + "ReleaseNo", + "QTY", + "DELDATE", + "COMMENTS", + "What changed", + "CUSTOMERID", + ]; + const orderData = XLSX.utils.sheet_to_json(sheet, { + defval: "", + header: headers, + range: 1, + }); + + // the base of the import + const predefinedObject = { + receivingPlantId: plantToken[0].value, + documentName: `OrdersFromLST-${new Date(Date.now()).toLocaleString( + "en-US" + )}`, + sender: user.username || "lst-system", + externalRefNo: `OrdersFromLST-${new Date(Date.now()).toLocaleString( + "en-US" + )}`, + orders: [], + }; + + let newOrders: any = orderData; + + // filter out the orders that have already been started just to reduce the risk of errors. + newOrders.filter((oo: any) => + openOrders.some( + (o: any) => o.CustomerOrderNumber === oo.CustomerOrderNumber + ) + ); + + // filter out the blanks + newOrders = newOrders.filter((z: any) => z.ITEM !== ""); + + // let postedOrders: any = []; + // for (const [customerID, orders] of Object.entries(orderData)) { + // // console.log(`Running for Customer ID: ${customerID}`); + // const newOrders: any = orderData; + + // // filter out the orders that have already been started just to reduce the risk of errors. + // newOrders.filter((oo: any) => + // openOrders.some( + // (o: any) => o.CustomerOrderNumber === oo.CustomerOrderNumber + // ) + // ); + + // // map everything out for each order + const nOrder = newOrders.map((o: any) => { + const invoice = i.filter( + (i: any) => i.deliveryAddress === parseInt(o.CUSTOMERID) + ); + if (!invoice) { + return; + } + return { + customerId: parseInt(o.CUSTOMERID), + invoiceAddressId: invoice[0].invoiceAddress, // matched to the default invoice address + customerOrderNo: o.PO, + orderDate: new Date(Date.now()).toLocaleString("en-US"), + positions: [ + { + deliveryAddressId: parseInt(o.CUSTOMERID), + customerArticleNo: o.ITEM, + quantity: parseInt(o.QTY), + deliveryDate: o.DELDATE, //excelDateStuff(o.DELDATE), + customerLineItemNo: o.ReleaseNo, // this is how it is currently sent over from abbott + customerReleaseNo: o.ReleaseNo, // same as above + }, + ], + }; + }); + + // // do that fun combining thing + const updatedPredefinedObject = { + ...predefinedObject, + orders: [...predefinedObject.orders, ...nOrder], + }; + + // //console.log(updatedPredefinedObject); + + // // post the orders to the server + const posting: any = await postOrders(updatedPredefinedObject, user); + + return { + customer: nOrder[0].CUSTOMERID, + //totalOrders: orders?.length(), + success: posting.success, + message: posting.message, + data: posting.data, + }; + // } + + // return { + // success: true, + // message: + // "Standard Template was just processed successfully, please check AlplaProd 2.0 to confirm no errors. ", + // data: nOrder, + // }; +};