diff --git a/frontend/src/components/logistics/dm/DMButtons.tsx b/frontend/src/components/logistics/dm/DMButtons.tsx index 68158b4..579d7f3 100644 --- a/frontend/src/components/logistics/dm/DMButtons.tsx +++ b/frontend/src/components/logistics/dm/DMButtons.tsx @@ -22,6 +22,7 @@ export default function DMButtons() { name={"Energizer Truck List"} /> + )} {plantToken[0]?.value === "usday1" && ( @@ -44,6 +45,16 @@ export default function DMButtons() { {plantToken[0]?.value === "usstp1" && (
)} + {plantToken[0]?.value === "usiow1" && ( +
+ +
+ )} + {plantToken[0]?.value === "usiow2" && ( +
+ +
+ )} ); } diff --git a/server/services/logistics/controller/dm/forecast/forecastIn.ts b/server/services/logistics/controller/dm/forecast/forecastIn.ts index e1d2a15..02320a4 100644 --- a/server/services/logistics/controller/dm/forecast/forecastIn.ts +++ b/server/services/logistics/controller/dm/forecast/forecastIn.ts @@ -1,4 +1,5 @@ import { lorealForecast } from "./mappings/loralForecast.js"; +import { pNgForecast } from "./mappings/pNgForecast.js"; import { standardForecast } from "./mappings/standardForcast.js"; export const forecastIn = async (data: any, user: any) => { @@ -32,7 +33,11 @@ export const forecastIn = async (data: any, user: any) => { } if (data["fileType"] === "pg") { - // orders in + //run the standard forecast in + const pg = await pNgForecast(data["postForecast"], user); + success = pg.success ?? false; + message = pg.message ?? "Error posting standard forecast"; + orderData = pg.data; } return { diff --git a/server/services/logistics/controller/dm/forecast/mappings/pNgForecast.ts b/server/services/logistics/controller/dm/forecast/mappings/pNgForecast.ts new file mode 100644 index 0000000..07f0943 --- /dev/null +++ b/server/services/logistics/controller/dm/forecast/mappings/pNgForecast.ts @@ -0,0 +1,160 @@ +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"; +import { query } from "../../../../../sqlServer/prodSqlServer.js"; +import { activeArticle } from "../../../../../sqlServer/querys/dataMart/article.js"; + +export const pNgForecast = 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 { data: a, error: ae } = await tryCatch( + query(activeArticle, "p&g active av") + ); + + if (ae) { + return { + success: false, + message: "Error getting active av", + data: [], + }; + } + + const article: any = a?.data; + + 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: any = workbook.Sheets[sheetName]; + const sheet: any = workbook.Sheets["SchedAgreementUIConfigSpreadshe"]; + const range = XLSX.utils.decode_range(sheet["!ref"]); + + const headers = []; + for (let C = range.s.c; C <= range.e.c; ++C) { + const cellAddress = XLSX.utils.encode_cell({ r: 0, c: C }); // row 0 = Excel row 1 + const cell = sheet[cellAddress]; + headers.push(cell ? cell.v : undefined); + } + + //console.log(headers); + 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: 139, + 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: parseInt(o["Customer Item No."]), + requirementDate: excelDateStuff(parseInt(o["Request Date"])), + quantity: o["Remaining Qty to be Shipped"], + }; + }); + + // check to make sure the av belongs in this plant. + const onlyNumbers = nForecast.filter((n: any) => n.quantity > 0); + const filteredForecast: any = []; + + for (let i = 0; i < nForecast.length; i++) { + //console.log(nForecast[i].customerArticleNo); + const activeAV = article.filter( + (c: any) => + c?.CustomerArticleNumber === + nForecast[i]?.customerArticleNo.toString() + ); + + if (activeAV.length > 0) { + filteredForecast.push(onlyNumbers[i]); + } + } + + if (filteredForecast.length === 0) { + console.log("Nothing to post"); + return { + success: true, + message: "No forecast to be posted", + data: foreCastData, + }; + } + + // do that fun combining thing + let updatedPredefinedObject = { + ...predefinedObject, + positions: [...predefinedObject.positions, ...filteredForecast], + }; + + //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: foreCastData, + }; +}; diff --git a/server/services/logistics/controller/dm/ordersIn/postOrders.ts b/server/services/logistics/controller/dm/ordersIn/postOrders.ts index bebabb5..92ed650 100644 --- a/server/services/logistics/controller/dm/ordersIn/postOrders.ts +++ b/server/services/logistics/controller/dm/ordersIn/postOrders.ts @@ -17,7 +17,6 @@ export const postOrders = async (data: any, user: any) => { data: data, }); - //console.log(results.data); //console.log(results.status); if (results.data.errors) { return {