From 8e3d2b3d9520730fa2d594b8d6c5757e8094b656 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Fri, 12 Dec 2025 10:53:33 -0600 Subject: [PATCH] fix(dm): new scj custom mapping added for westbend --- .../-components/logistics/dm/DMButtons.tsx | 122 ++++++------- .../controller/dm/ordersIn/mappings/scj.ts | 166 ++++++++++++++++++ .../controller/dm/ordersIn/ordersIn.ts | 101 ++++++----- 3 files changed, 276 insertions(+), 113 deletions(-) create mode 100644 lstV2/server/services/logistics/controller/dm/ordersIn/mappings/scj.ts diff --git a/frontend/src/routes/_old/old/-components/logistics/dm/DMButtons.tsx b/frontend/src/routes/_old/old/-components/logistics/dm/DMButtons.tsx index 3e8fbeb..6c48a15 100644 --- a/frontend/src/routes/_old/old/-components/logistics/dm/DMButtons.tsx +++ b/frontend/src/routes/_old/old/-components/logistics/dm/DMButtons.tsx @@ -3,72 +3,60 @@ import ForecastImport from "./ForecastImport"; import OrderImport from "./OrderImport"; export default function DMButtons() { - const { settings } = useSettingStore(); - const testServers = ["test1", "test2", "test3"]; - const plantToken = settings.filter((n) => n.name === "plantToken"); + const { settings } = useSettingStore(); + const testServers = ["test1", "test2", "test3"]; + const plantToken = settings.filter((n) => n.name === "plantToken"); - //console.log(plantToken); - return ( -
- - {/* dev and testserver sees all */} - {testServers.includes(plantToken[0]?.value) && ( -
- - - - - -
- )} - {plantToken[0]?.value === "usday1" && ( -
- - - -
- )} - {plantToken[0]?.value === "usflo1" && ( -
- -
- )} - {plantToken[0]?.value === "usstp1" && ( -
- )} - {plantToken[0]?.value === "usiow1" && ( -
- -
- )} - {plantToken[0]?.value === "usiow2" && ( -
- -
- )} - {plantToken[0]?.value === "usksc1" && ( -
- -
- )} -
- ); + //console.log(plantToken); + return ( +
+ + {/* dev and testserver sees all */} + {testServers.includes(plantToken[0]?.value) && ( +
+ + + + + + +
+ )} + {plantToken[0]?.value === "usday1" && ( +
+ + + +
+ )} + {plantToken[0]?.value === "usflo1" && ( +
+ +
+ )} + {plantToken[0]?.value === "usstp1" && ( +
+ )} + {plantToken[0]?.value === "usiow1" && ( +
+ +
+ )} + {plantToken[0]?.value === "usiow2" && ( +
+ +
+ )} + {plantToken[0]?.value === "usksc1" && ( +
+ +
+ )} + {plantToken[0]?.value === "usweb1" && ( +
+ +
+ )} +
+ ); } diff --git a/lstV2/server/services/logistics/controller/dm/ordersIn/mappings/scj.ts b/lstV2/server/services/logistics/controller/dm/ordersIn/mappings/scj.ts new file mode 100644 index 0000000..1e8a85d --- /dev/null +++ b/lstV2/server/services/logistics/controller/dm/ordersIn/mappings/scj.ts @@ -0,0 +1,166 @@ +import XLSX from "xlsx"; +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 { invoiceAddress } from "../../../../../sqlServer/querys/dm/invoiceAddress.js"; +import { orderState } from "../../../../../sqlServer/querys/dm/orderState.js"; +import { excelDateStuff } from "../../../../utils/excelDateStuff.js"; +import { postOrders } from "../postOrders.js"; + +export const scjOrders = async (data: any, user: any) => { + /** + * Standard orders meaning that we get the standard file exported and fill it out and uplaod to lst. + */ + + const customerID = 48; + + 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: o, error: oe } = await tryCatch( + query(orderState, "Gets the next 500 orders that have not been started"), + ); + + const openOrders: any = o?.data; + + if (oe) { + return { + sucess: false, + message: `Error getting article data`, + data: oe, + }; + } + + // order state + const { data: invoice, error: ie } = await tryCatch( + query(invoiceAddress, "Gets invoices addresses"), + ); + const i: any = invoice?.data; + + 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: any = workbook.Sheets["Sheet1"]; + const sheet = XLSX.utils.decode_range(sheetName["!ref"]); + + // define custom headers + const headers = [ + "ItemNo", + "Description", + "DeliveryDate", + "Quantity", + "PO", + "Releases", + "remarks", + ]; + const orderData = XLSX.utils.sheet_to_json(sheetName, { + 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.ItemNo !== ""); + + const nOrder = newOrders.map((o: any) => { + const invoice = i.filter((i: any) => i.deliveryAddress === customerID); + if (!invoice) { + return; + } + + if (o.Releases === "") { + return; + } + + if (o.PO === "") { + return; + } + + const date = isNaN(o.DeliveryDate) + ? new Date(o.DeliveryDate) + : excelDateStuff(o.DeliveryDate); + return { + customerId: customerID, + invoiceAddressId: invoice[0].invoiceAddress, // matched to the default invoice address + customerOrderNo: o.PO, + orderDate: new Date(Date.now()).toLocaleString("en-US"), + positions: [ + { + deliveryAddressId: customerID, + customerArticleNo: o.ItemNo, + quantity: parseInt(o.Quantity), + deliveryDate: date, //excelDateStuff(o.DELDATE), + customerLineItemNo: o.PO, // this is how it is currently sent over from abbott + customerReleaseNo: o.Releases, // same as above + remark: o.remarks === "" ? null : o.remarks, + }, + ], + }; + }); + + //console.log(nOrder.filter((o: any) => o !== undefined)); + + // // do that fun combining thing + const updatedPredefinedObject = { + ...predefinedObject, + orders: [ + ...predefinedObject.orders, + ...nOrder.filter((o: any) => o !== undefined), + ], + }; + + //console.log(updatedPredefinedObject.orders[0]); + + // // post the orders to the server + const posting: any = await postOrders(updatedPredefinedObject, user); + + return { + customer: customerID, + //totalOrders: orders?.length(), + success: posting.success, + message: posting.message, + data: posting.data, + }; +}; diff --git a/lstV2/server/services/logistics/controller/dm/ordersIn/ordersIn.ts b/lstV2/server/services/logistics/controller/dm/ordersIn/ordersIn.ts index 404a8e5..279300b 100644 --- a/lstV2/server/services/logistics/controller/dm/ordersIn/ordersIn.ts +++ b/lstV2/server/services/logistics/controller/dm/ordersIn/ordersIn.ts @@ -1,61 +1,70 @@ import { abbottOrders } from "./mappings/abbottTruckList.js"; import { energizerOrders } from "./mappings/energizerOrdersIn.js"; import { macroImportOrders } from "./mappings/macroImport.js"; +import { scjOrders } from "./mappings/scj.js"; import { standardOrders } from "./mappings/standardOrders.js"; export const ordersIn = async (data: any, user: any) => { - /** - * Bulk orders in, and custom file parsing. - */ + /** + * Bulk orders in, and custom file parsing. + */ - let success = true; - let message = ""; - let orderData: any = []; + let success = true; + let message = ""; + let orderData: any = []; - // what type of order are we dealing with? - if (data["fileType"] === "standard") { - // run the standard orders in - const standard = await standardOrders(data["postOrders"], user); - success = standard.success ?? false; - message = standard.message ?? "Error posting Standard Orders"; - orderData = standard.data; - } + // what type of order are we dealing with? + if (data["fileType"] === "standard") { + // run the standard orders in + const standard = await standardOrders(data["postOrders"], user); + success = standard.success ?? false; + message = standard.message ?? "Error posting Standard Orders"; + orderData = standard.data; + } - if (data["fileType"] === "abbott") { - // orders in - const abbott = await abbottOrders(data["postOrders"], user); - success = abbott.success ?? false; - message = abbott.message ?? "Error posting Abbott Orders"; - orderData = abbott.data; - } + if (data["fileType"] === "abbott") { + // orders in + const abbott = await abbottOrders(data["postOrders"], user); + success = abbott.success ?? false; + message = abbott.message ?? "Error posting Abbott Orders"; + orderData = abbott.data; + } - 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"] === "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") { - // orders in - } + if (data["fileType"] === "loreal") { + // orders in + } - if (data["fileType"] === "pg") { - // orders in - } + if (data["fileType"] === "pg") { + // orders in + } - if (data["fileType"] === "macro") { - // orders in - const macro = await macroImportOrders(data["postOrders"], user); - success = macro.success ?? false; - message = macro.message ?? "Error posting Macro Orders"; - orderData = macro.data; - } + if (data["fileType"] === "macro") { + // orders in + const macro = await macroImportOrders(data["postOrders"], user); + success = macro.success ?? false; + message = macro.message ?? "Error posting Macro Orders"; + orderData = macro.data; + } - return { - success, - message, - data: orderData, - }; + if (data["fileType"] === "scj") { + // orders in + const macro = await scjOrders(data["postOrders"], user); + success = macro.success ?? false; + message = macro.message ?? "Error posting Macro Orders"; + orderData = macro.data; + } + + return { + success, + message, + data: orderData, + }; };