diff --git a/server/services/dataMart/controller/psiGetArticleData.ts b/server/services/dataMart/controller/psiGetArticleData.ts new file mode 100644 index 0000000..a597d3a --- /dev/null +++ b/server/services/dataMart/controller/psiGetArticleData.ts @@ -0,0 +1,47 @@ +import { tryCatch } from "../../../globalUtils/tryCatch.js"; +import { createLog } from "../../logger/logger.js"; +import { query } from "../../sqlServer/prodSqlServer.js"; +import { articleInfo } from "../../sqlServer/querys/psiReport/articleData.js"; + +// type ArticleData = { +// id: string +// } +export const getGetPSIArticleData = async (avs: string) => { + let articles: any = []; + + if (!avs) { + return { + success: false, + message: `Missing av's please send at least one over`, + data: [], + }; + } + + const { data, error } = (await tryCatch( + query(articleInfo.replace("[articles]", avs), "PSI article info") + )) as any; + + if (error) { + createLog( + "error", + "datamart", + "datamart", + `There was an error getting the article info: ${JSON.stringify( + error + )}` + ); + return { + success: false, + messsage: `There was an error getting the article info`, + data: error, + }; + } + + articles = data.data; + + return { + success: true, + message: "PSI Article Data", + data: articles, + }; +}; diff --git a/server/services/dataMart/dataMartService.ts b/server/services/dataMart/dataMartService.ts index 20d6c00..51ab8c6 100644 --- a/server/services/dataMart/dataMartService.ts +++ b/server/services/dataMart/dataMartService.ts @@ -9,6 +9,7 @@ import fakeEDI from "./route/fakeEDI.js"; import addressCorrections from "./route/getCityStateData.js"; import fifoIndex from "./route/getFifoIndex.js"; import financeAudit from "./route/getFinanceAudit.js"; +import psiArticleData from "./route/getPsiArticleData.js"; const app = new OpenAPIHono(); @@ -23,6 +24,7 @@ const routes = [ addressCorrections, fifoIndex, financeAudit, + psiArticleData, ] as const; const appRoutes = routes.forEach((route) => { diff --git a/server/services/dataMart/route/getPsiArticleData.ts b/server/services/dataMart/route/getPsiArticleData.ts new file mode 100644 index 0000000..3cbc060 --- /dev/null +++ b/server/services/dataMart/route/getPsiArticleData.ts @@ -0,0 +1,61 @@ +import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi"; +import { responses } from "../../../globalUtils/routeDefs/responses.js"; +import { tryCatch } from "../../../globalUtils/tryCatch.js"; +import { getDeliveryByDateRange } from "../controller/getDeliveryByDateRange.js"; +import { apiHit } from "../../../globalUtils/apiHits.js"; +import { getGetPSIArticleData } from "../controller/psiGetArticleData.js"; + +const app = new OpenAPIHono({ strict: false }); +const Body = z.object({ + includeRunnningNumbers: z.string().openapi({ example: "x" }), +}); +app.openapi( + createRoute({ + tags: ["dataMart"], + summary: "Returns the psiarticleData.", + method: "get", + path: "/psiarticledata", + request: { + body: { + content: { + "application/json": { schema: Body }, + }, + }, + }, + responses: responses(), + }), + async (c) => { + const articles: any = c.req.queries(); + + // make sure we have a vaid user being accessed thats really logged in + apiHit(c, { endpoint: "/psiarticledata" }); + //console.log(articles["avs"][0]); + const { data, error } = await tryCatch( + getGetPSIArticleData(articles ? articles["avs"][0] : null) + ); + + if (error) { + console.log(error); + return c.json( + { + success: false, + message: "There was an error getting the articles.", + data: error, + }, + 400 + ); + } + + //console.log(data); + + return c.json( + { + success: data.success, + message: data.message, + data: data.data, + }, + data.success ? 200 : 400 + ); + } +); +export default app; diff --git a/server/services/sqlServer/querys/psiReport/articleData.ts b/server/services/sqlServer/querys/psiReport/articleData.ts new file mode 100644 index 0000000..90a99b2 --- /dev/null +++ b/server/services/sqlServer/querys/psiReport/articleData.ts @@ -0,0 +1,40 @@ +export const articleInfo = ` +use [test1_AlplaPROD2.0_Read] + +select a.Id, +a.HumanReadableId as av, +a.Alias as alias, +p.LoadingUnitsPerTruck as loadingUnitsPerTruck, +p.LoadingUnitsPerTruck * p.LoadingUnitPieces as qtyPerTruck, +p.LoadingUnitPieces, +case when i.MinQuantity IS NOT NULL then round(cast(i.MinQuantity as float), 2) else 0 end as min, +case when i.MaxQuantity IS NOT NULL then round(cast(i.MaxQuantity as float),2) else 0 end as max +from masterData.Article (nolock) as a + +/* sales price */ +left join +(select * + from (select + id, + PackagingId, + ArticleId, + DefaultCustomer, + ROW_NUMBER() OVER (PARTITION BY ArticleId ORDER BY ValidAfter DESC) AS RowNum + from masterData.SalesPrice (nolock) + where DefaultCustomer = 1) as x + where RowNum = 1 + ) as s +on a.id = s.ArticleId + +/* pkg instuctions */ +left join +masterData.PackagingInstruction (nolock) as p +on s.PackagingId = p.id + +/* stock limits */ +left join +masterData.StockLimit (nolock) as i +on a.id = i.ArticleId + +where a.active = 1 and a.HumanReadableId in ([articles]) +`; diff --git a/server/services/sqlServer/querys/psiReport/deliveredData.ts b/server/services/sqlServer/querys/psiReport/deliveredData.ts new file mode 100644 index 0000000..24960bf --- /dev/null +++ b/server/services/sqlServer/querys/psiReport/deliveredData.ts @@ -0,0 +1,19 @@ +export const psiDeliveredData = ` +use AlplaPROD_test1 + +declare @start_date nvarchar(30) = [startDate] --'2025-01-01' +declare @end_date nvarchar(30) = [endDate] --'2025-08-09' + + +select IdArtikelVarianten, +ArtikelVariantenBez, +sum(Menge) totalDelivered, +case when convert(time, upd_date) between '00:00' and '07:00' then convert(date, upd_date - 1) else convert(date, upd_date) end as ShippingDate + +from dbo.V_LadePlanungenLadeAuftragAbruf (nolock) + +where upd_date between CONVERT(datetime, @start_date + ' 7:00') and CONVERT(datetime, @end_date + ' 7:00') and IdArtikelVarianten in ([articles]) + +group by IdArtikelVarianten, upd_date, +ArtikelVariantenBez +`; diff --git a/server/services/sqlServer/querys/psiReport/deliveryData.ts b/server/services/sqlServer/querys/psiReport/deliveryData.ts new file mode 100644 index 0000000..9ad7fe1 --- /dev/null +++ b/server/services/sqlServer/querys/psiReport/deliveryData.ts @@ -0,0 +1,27 @@ +export const psiDeliveredData = ` + +use [test1_AlplaPROD2.0_Read] + +declare @start_date nvarchar(30) = [startDate] --'2025-01-01' +declare @end_date nvarchar(30) = [endDate] --'2025-08-09' + +select +ArticleHumanReadableId, +ArticleAlias, +cast(Quantity as int) Quantity, +--cast(DeliveryDate as time) as deliveryTime, +--cast(DeliveryDate as date) as originalDeliveryDate, +case when cast(DeliveryDate as time) between '00:00' and '07:00' + then DATEADD(DAY, -1, CONVERT(DATE, DeliveryDate)) + else cast(DeliveryDate as date) + end as ShippingDate +--,* +from [order].[Release] + +where case when cast(DeliveryDate as time) between '00:00' and '07:00' + then DATEADD(DAY, -1, CONVERT(DATE, DeliveryDate)) + else cast(DeliveryDate as date) + end between @start_date and @end_date + and ArticleHumanReadableId in ([articles]) + +order by DeliveryDate`; diff --git a/server/services/sqlServer/querys/psiReport/planningNumbersByAv.ts b/server/services/sqlServer/querys/psiReport/planningNumbersByAv.ts new file mode 100644 index 0000000..e3d1570 --- /dev/null +++ b/server/services/sqlServer/querys/psiReport/planningNumbersByAv.ts @@ -0,0 +1,33 @@ +export const planningNumbersByAVDate = ` +use AlplaPROD_test1 +declare @start_date nvarchar(30) = [startDate] --'2025-01-01' +declare @end_date nvarchar(30) = [endDate] --'2025-08-09' +/* +articles will need to be passed over as well as the date structure we want to see +*/ + +-- planned lots in planning + +select V_ProdLosProduktionJeProdTag_PLANNING.IdArtikelvarianten As Article, +ProduktionAlias as Description, +standort as MachineId, +MaschinenBezeichnung as MachineName, +--MaschZyklus as PlanningCycleTime, +V_ProdLosProduktionJeProdTag_PLANNING.IdProdPlanung as LotNumber, +FORMAT(ProdTag, 'MM/dd/yyyy') as ProductionDay, +V_ProdLosProduktionJeProdTag_PLANNING.planMenge as TotalPlanned, +ProduktionMenge as QTYPerDay, +round(ProduktionMengeVPK, 2) PalDay, +Status as finished +--MaschStdAuslastung as nee + +from dbo.V_ProdLosProduktionJeProdTag_PLANNING (nolock) + +left join +dbo.V_ProdPlanung (nolock) on +V_ProdLosProduktionJeProdTag_PLANNING .IdProdPlanung = V_ProdPlanung.IdProdPlanung + +where V_ProdLosProduktionJeProdTag_PLANNING.IdArtikelvarianten in ([articles]) and ProdTag between @start_date and @end_date --and IdProdPlanung = 18442 + +order by ProdTag +`; diff --git a/server/services/sqlServer/querys/psiReport/prodcuctionNumbers.ts b/server/services/sqlServer/querys/psiReport/prodcuctionNumbers.ts new file mode 100644 index 0000000..be13677 --- /dev/null +++ b/server/services/sqlServer/querys/psiReport/prodcuctionNumbers.ts @@ -0,0 +1,21 @@ +export const productionNumbers = ` +use [test1_AlplaPROD2.0_Reporting] + +declare @startDate nvarchar(30) = [startDate] --'2024-12-30' +declare @endDate nvarchar(30) = [endDate] --'2025-08-09' + +select MachineLocation, +ArticleHumanReadableId as article, +sum(Quantity) as Produced, +count(Quantity) as palletsProdued, +FORMAT(convert(date, ProductionDay), 'M/d/yyyy') as ProductionDay, +ProductionLotHumanReadableId as productionLot + +from [reporting_productionControlling].[ScannedUnit] (nolock) + +where convert(date, ProductionDay) between @startDate and @endDate and ArticleHumanReadableId in ([articles]) and BookedOut is null + +group by MachineLocation, ArticleHumanReadableId,ProductionDay, ProductionLotHumanReadableId + +order by ProductionDay +`;