From 25785703db4d7989ab54bd47dda16f38a4407be3 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Mon, 14 Apr 2025 12:26:56 -0500 Subject: [PATCH] feat(exports): added in a button to export the lanes to cycle count --- .../cycleCountChecks/cyclecountCheck.ts | 28 +++++++------ .../cycleCountChecks/exportCycleCountData.ts | 42 +++++++++++++++++++ server/services/logistics/logisticsService.ts | 2 + 3 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 server/services/logistics/controller/warehouse/cycleCountChecks/exportCycleCountData.ts diff --git a/server/services/logistics/controller/warehouse/cycleCountChecks/cyclecountCheck.ts b/server/services/logistics/controller/warehouse/cycleCountChecks/cyclecountCheck.ts index af6b2ff..94aace1 100644 --- a/server/services/logistics/controller/warehouse/cycleCountChecks/cyclecountCheck.ts +++ b/server/services/logistics/controller/warehouse/cycleCountChecks/cyclecountCheck.ts @@ -9,14 +9,14 @@ import { cycleCountCheck } from "../../../../sqlServer/querys/warehouse/cycleCou // setting timer for updating stockCheck on a restart will always check. let lastCheck = 0; -export const lanes: any = []; +export let lanes: any = []; export const getLanesToCycleCount = async () => { const currentTime: any = timeZoneFix(); // store the lanes in memeory createLog("info", "warehouse", "logistics", "Lane triggered update."); lastCheck = currentTime; - const ageQuery = cycleCountCheck.replaceAll("[ageOfRow]", `1000`); + const ageQuery = cycleCountCheck.replaceAll("[ageOfRow]", "90"); const { data: prodLanes, error: pl } = await tryCatch( query(ageQuery, "Get Stock lane date.") ); @@ -39,23 +39,25 @@ export const getLanesToCycleCount = async () => { warehouseName: prodLanes[i]?.warehouseName || "na", Description: prodLanes[i]?.Description, LastMoveDate: prodLanes[i]?.LastMoveDate - ? format(prodLanes[i]?.LastInv, "M/d/yyyy") + ? format(prodLanes[i]?.LastMoveDate, "M/d/yyyy") : undefined, LastInv: format(prodLanes[i]?.LastInv, "M/d/yyyy"), rowType: prodLanes[i].rowType, - DaysSinceLast: - differenceInDays( - new Date(prodLanes[i].LastInv), - new Date(prodLanes[i].LastMoveDate) - ) <= 0 - ? 0 - : differenceInDays( - new Date(prodLanes[i].LastInv), - new Date(prodLanes[i].LastMoveDate) - ), + DaysSinceLast: differenceInDays( + new Date(Date.now()), + new Date(prodLanes[i].LastInv) + ), upd_date: format(new Date(Date.now()), "M/d/yyyy"), }; + const existing = lanes.filter( + (l: any) => l.laneID === prodLanes[i]?.laneID + ); + + if (existing) { + lanes = lanes.filter((l: any) => l.laneID !== prodLanes[i]?.laneID); + } + lanes.push(createLane); createLog( "debug", diff --git a/server/services/logistics/controller/warehouse/cycleCountChecks/exportCycleCountData.ts b/server/services/logistics/controller/warehouse/cycleCountChecks/exportCycleCountData.ts new file mode 100644 index 0000000..834a73e --- /dev/null +++ b/server/services/logistics/controller/warehouse/cycleCountChecks/exportCycleCountData.ts @@ -0,0 +1,42 @@ +import * as XLSX from "xlsx"; +import { lanes } from "./cyclecountCheck.js"; + +export const lanesToExcel = async (age: string | null) => { + // Convert JSON data to an array of arrays (AOA) + + let processLanes = lanes; + if (age) { + processLanes = lanes.filter( + (l: any) => l.DaysSinceLast >= parseInt(age) + ); + } + + const headers = Object.keys(processLanes[0]); // Get headers from JSON keys + const data = processLanes.map((item: any) => + headers.map((header) => item[header]) + ); + + // Create the workbook and worksheet + const wb = XLSX.utils.book_new(); + const ws = XLSX.utils.aoa_to_sheet([headers, ...data]); // Combine headers and data + + // Auto-resize columns based on the longest content in each column + const colWidths = headers.map((_, colIndex) => { + let maxLength = 0; + data.forEach((row: any) => { + const cellValue = row[colIndex] ? row[colIndex].toString() : ""; + maxLength = Math.max(maxLength, cellValue.length); + }); + return { wch: maxLength + 2 }; // Add a little padding + }); + + ws["!cols"] = colWidths; // Set the column widths + + // Add the worksheet to the workbook + XLSX.utils.book_append_sheet(wb, ws, "CycleCount"); + + // Write the workbook to a buffer and return it + const excelBuffer = XLSX.write(wb, { bookType: "xlsx", type: "buffer" }); + + return excelBuffer; +}; diff --git a/server/services/logistics/logisticsService.ts b/server/services/logistics/logisticsService.ts index 6dc97ad..8db407d 100644 --- a/server/services/logistics/logisticsService.ts +++ b/server/services/logistics/logisticsService.ts @@ -10,6 +10,7 @@ import getSiloAdjustments from "./route/siloAdjustments/getSiloAdjustments.js"; import { getLanesToCycleCount } from "./controller/warehouse/cycleCountChecks/cyclecountCheck.js"; import getCycleCountCheck from "./route/getCycleCountChecks.js"; import getPPOO from "./route/getPPOO.js"; +import getcyclecount from "./route/getCycleCountLanes.js"; const app = new OpenAPIHono(); @@ -26,6 +27,7 @@ const routes = [ getCycleCountCheck, //warehouse getPPOO, + getcyclecount, ] as const; // app.route("/server", modules);