From 90be86d972d4660d87e0b8f41e7f2b1a22838e9c Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Mon, 16 Jun 2025 18:30:05 -0500 Subject: [PATCH] feat(datamart): finance inventory audit added --- .../dataMart/controller/getFinanceAudit.ts | 24 +++++++ server/services/dataMart/dataMartService.ts | 2 + .../dataMart/route/getCurrentQuerys.ts | 8 +++ .../dataMart/route/getFinanceAudit.ts | 69 +++++++++++++++++++ .../sqlServer/querys/dataMart/financeAudit.ts | 55 +++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 server/services/dataMart/controller/getFinanceAudit.ts create mode 100644 server/services/dataMart/route/getFinanceAudit.ts create mode 100644 server/services/sqlServer/querys/dataMart/financeAudit.ts diff --git a/server/services/dataMart/controller/getFinanceAudit.ts b/server/services/dataMart/controller/getFinanceAudit.ts new file mode 100644 index 0000000..9396a64 --- /dev/null +++ b/server/services/dataMart/controller/getFinanceAudit.ts @@ -0,0 +1,24 @@ +import { format } from "date-fns-tz"; +import { tryCatch } from "../../../globalUtils/tryCatch.js"; +import { query } from "../../sqlServer/prodSqlServer.js"; +import { financeAudit } from "../../sqlServer/querys/dataMart/financeAudit.js"; + +export const getfinanceAudit = async (date: any) => { + let inventoryAudit: any = []; + + const { data, error } = (await tryCatch( + query(financeAudit.replace("[date]", date), "inventory audit") + )) as any; + + //console.log(data); + + if (error) { + return []; + } + + inventoryAudit = data.data.map((i: any) => { + return { ...i, bookinDate: format(i.bookinDate, "MM/dd/yyyy") }; + }); + + return inventoryAudit; +}; diff --git a/server/services/dataMart/dataMartService.ts b/server/services/dataMart/dataMartService.ts index 449fd1f..20d6c00 100644 --- a/server/services/dataMart/dataMartService.ts +++ b/server/services/dataMart/dataMartService.ts @@ -8,6 +8,7 @@ import getDeliveryByDate from "./route/getDeliveryDateByRange.js"; import fakeEDI from "./route/fakeEDI.js"; import addressCorrections from "./route/getCityStateData.js"; import fifoIndex from "./route/getFifoIndex.js"; +import financeAudit from "./route/getFinanceAudit.js"; const app = new OpenAPIHono(); @@ -21,6 +22,7 @@ const routes = [ fakeEDI, addressCorrections, fifoIndex, + financeAudit, ] as const; const appRoutes = routes.forEach((route) => { diff --git a/server/services/dataMart/route/getCurrentQuerys.ts b/server/services/dataMart/route/getCurrentQuerys.ts index c4a936b..bc863fd 100644 --- a/server/services/dataMart/route/getCurrentQuerys.ts +++ b/server/services/dataMart/route/getCurrentQuerys.ts @@ -111,6 +111,14 @@ const current: any = [ "Returns fifo index for all pallets shipped within the last 90 days.", //criteria: "address", // uncomment this out once the improt process can be faster }, + { + name: "Finance Audit inv", + endpoint: "/api/datamart/getfinanceaudit", + // description: "Returns all inventory, by default excludes running numebrs, also excludes inv locations.", + description: + "Returns all inventory past the date provided, ie: 5/31/2025", + criteria: "date", // uncomment this out once the improt process can be faster + }, ]; app.openapi( diff --git a/server/services/dataMart/route/getFinanceAudit.ts b/server/services/dataMart/route/getFinanceAudit.ts new file mode 100644 index 0000000..3fb9a52 --- /dev/null +++ b/server/services/dataMart/route/getFinanceAudit.ts @@ -0,0 +1,69 @@ +import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi"; +import { apiHit } from "../../../globalUtils/apiHits.js"; +import { responses } from "../../../globalUtils/routeDefs/responses.js"; +import { getfinanceAudit } from "../controller/getFinanceAudit.js"; +import { tryCatch } from "../../../globalUtils/tryCatch.js"; + +const app = new OpenAPIHono({ strict: false }); +const EomStat = z.object({ + plant: z.string().openapi({ example: "Salt Lake City" }), + userRan: z.string().openapi({ example: "smith034" }), + eomSheetVersion: z.string().openapi({ example: "0.0.223" }), +}); + +app.openapi( + createRoute({ + tags: ["dataMart"], + summary: + "Returns inventory to date and current sales prices and booking date of the inventory.", + method: "get", + path: "/getfinanceaudit", + + responses: responses(), + }), + async (c: any) => { + //const body = await c.req.json(); + // make sure we have a vaid user being accessed thats really logged in + const { data, error } = (await tryCatch(c.req.query())) as any; + + //console.log(data.date); + + if (error) { + return { + success: false, + message: "Missing Mandatory Data", + data: error, + }; + } + + if (!data.date) { + return { + success: false, + message: "Missing Date", + data: [], + }; + } + apiHit(c, { endpoint: "/getfinanceaudit", lastBody: data }); + + try { + return c.json( + { + success: true, + message: `Inventory older than ${data.date}`, + data: await getfinanceAudit(data.date), + }, + 200 + ); + } catch (error) { + return c.json( + { + success: false, + message: "There was an error getting inventory data", + data: error, + }, + 400 + ); + } + } +); +export default app; diff --git a/server/services/sqlServer/querys/dataMart/financeAudit.ts b/server/services/sqlServer/querys/dataMart/financeAudit.ts new file mode 100644 index 0000000..c88533f --- /dev/null +++ b/server/services/sqlServer/querys/dataMart/financeAudit.ts @@ -0,0 +1,55 @@ +export const financeAudit = ` +use AlplaPROD_test1 + +select +(select wert from dbo.T_SystemParameter where Bezeichnung = 'Werkskuerzel') as Plant, +b.IdArtikelVarianten +,ArtikelVariantenAlias +,ArtikelVariantenBez +,sum(EinlagerungsMengeVPKSum) totalPal +,sum(EinlagerungsMengeSum) totalPieces +--,ProduktionsDatumMin +,pp.VKPreis as salesPrice +,sp.EKPreis as purhcasePrice +,convert(date, ProduktionsDatumMin, 111) as bookinDate +,DATEDIFF(DAY, convert(date, ProduktionsDatumMin, 111), getdate()) as aged +--,* + +from dbo.V_LagerPositionenBarcodes (nolock) b + +/* purhcase price */ +left join +(select * from (select +IdArtikelvarianten +,VKPreis +,ROW_NUMBER() OVER (PARTITION BY IdArtikelVarianten ORDER BY gueltigabDatum DESC) AS rn +--,* +from T_HistoryVK (nolock))c + +where rn = 1) as pp on +b.IdArtikelVarianten = pp.IdArtikelvarianten + +/* sales price */ +left join +(select * from (select +IdArtikelvarianten +,EKPreis +,ROW_NUMBER() OVER (PARTITION BY IdArtikelVarianten ORDER BY gueltigabDatum DESC) AS rn +--,* +from T_HistoryEK (nolock) )x + +where rn = 1) sp on +sp.IdArtikelvarianten = b.IdArtikelVarianten + +where IdWarenlager not in (1,5,6) +and ProduktionsDatumMin < '[date]' -- '2025-05-31' + +group by b.IdArtikelVarianten +,ArtikelVariantenAlias +,ArtikelVariantenBez +,convert(date, ProduktionsDatumMin, 111) +,pp.VKPreis +,sp.EKPreis + +order by IdArtikelVarianten +`;