refactor(psi): rebuilt the delivery and planning data to 2.0 data
This commit is contained in:
@@ -275,10 +275,10 @@ export const runDatamartQuery = async (data: Data) => {
|
|||||||
.replace("[startDate]", `${data.options.startDate}`)
|
.replace("[startDate]", `${data.options.startDate}`)
|
||||||
.replace("[endDate]", `${data.options.endDate}`)
|
.replace("[endDate]", `${data.options.endDate}`)
|
||||||
.replace(
|
.replace(
|
||||||
"and p.IdArtikelvarianten in ([articles])",
|
"and pl.ArticleHumanReadableId IN ([articles]) ",
|
||||||
data.options.articles
|
data.options.articles
|
||||||
? `and p.IdArtikelvarianten in (${data.options.articles})`
|
? `and pl.ArticleHumanReadableId IN (${data.options.articles})`
|
||||||
: "--and p.IdArtikelvarianten in ([articles])",
|
: "--and pl.ArticleHumanReadableId IN ([articles])",
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,74 +1,73 @@
|
|||||||
use [test1_AlplaPROD2.0_Read]
|
use [test1_AlplaPROD2.0_Read]
|
||||||
|
|
||||||
DECLARE @StartDate DATE = '[startDate]' -- 2025-1-1
|
DECLARE @StartDate DATE = '[startDate]'
|
||||||
DECLARE @EndDate DATE = '[endDate]' -- 2025-1-31
|
DECLARE @EndDate DATE = '[endDate]'
|
||||||
SELECT
|
|
||||||
r.[ArticleHumanReadableId]
|
;WITH bol_20 AS ( -- 2.0 BOL, one per release (newest doc wins)
|
||||||
,[ReleaseNumber]
|
SELECT pos.ReleaseId,
|
||||||
,h.CustomerOrderNumber
|
dd.JournalNumber,
|
||||||
,x.CustomerLineItemNumber
|
ROW_NUMBER() OVER (PARTITION BY pos.ReleaseId
|
||||||
,[CustomerReleaseNumber]
|
ORDER BY dd.ShippingDate DESC) AS rn
|
||||||
,[ReleaseState]
|
FROM [outboundDelivery].[DeliveryDocumentPosition] (nolock) pos
|
||||||
,[DeliveryState]
|
JOIN [outboundDelivery].[DeliveryDocument] (nolock) dd
|
||||||
,ea.JournalNummer as BOL_Number
|
ON dd.Id = pos.DeliveryDocumentId
|
||||||
,[ReleaseConfirmationState]
|
-- WHERE dd.DocumentType = <BOL value> -- see note below
|
||||||
,[PlanningState]
|
)
|
||||||
|
SELECT
|
||||||
|
r.[ArticleHumanReadableId]
|
||||||
|
,[ReleaseNumber]
|
||||||
|
,h.CustomerOrderNumber
|
||||||
|
,x.CustomerLineItemNumber
|
||||||
|
,[CustomerReleaseNumber]
|
||||||
|
,[ReleaseState]
|
||||||
|
,[DeliveryState]
|
||||||
|
,COALESCE(ea.JournalNummer, bol_20.JournalNumber) AS BOL_Number -- 1.0 or 2.0
|
||||||
|
,[ReleaseConfirmationState]
|
||||||
|
,[PlanningState]
|
||||||
,format(r.[OrderDate], 'yyyy-MM-dd HH:mm') as OrderDate
|
,format(r.[OrderDate], 'yyyy-MM-dd HH:mm') as OrderDate
|
||||||
--,r.[OrderDate]
|
--,r.[OrderDate]
|
||||||
,FORMAT(r.[DeliveryDate], 'yyyy-MM-dd HH:mm') as DeliveryDate
|
,FORMAT(r.[DeliveryDate], 'yyyy-MM-dd HH:mm') as DeliveryDate
|
||||||
--,r.[DeliveryDate]
|
--,r.[DeliveryDate]
|
||||||
,FORMAT(r.[LoadingDate], 'yyyy-MM-dd HH:mm') as LoadingDate
|
,FORMAT(r.[LoadingDate], 'yyyy-MM-dd HH:mm') as LoadingDate
|
||||||
--,r.[LoadingDate]
|
--,r.[LoadingDate]
|
||||||
,[Quantity]
|
,[Quantity]
|
||||||
,[DeliveredQuantity]
|
,[DeliveredQuantity]
|
||||||
,r.[AdditionalInformation1]
|
,r.[AdditionalInformation1]
|
||||||
,r.[AdditionalInformation2]
|
,r.[AdditionalInformation2]
|
||||||
,[TradeUnits]
|
,[TradeUnits]
|
||||||
,[LoadingUnits]
|
,[LoadingUnits]
|
||||||
,[Trucks]
|
,[Trucks]
|
||||||
,[LoadingToleranceType]
|
,[LoadingToleranceType]
|
||||||
,[SalesPrice]
|
,[SalesPrice]
|
||||||
,[Currency]
|
,[Currency]
|
||||||
,[QuantityUnit]
|
,[QuantityUnit]
|
||||||
,[SalesPriceRemark]
|
,[SalesPriceRemark]
|
||||||
,r.[Remark]
|
,r.[Remark]
|
||||||
,[Irradiated]
|
,[Irradiated]
|
||||||
,r.[CreatedByEdi]
|
,r.[CreatedByEdi]
|
||||||
,[DeliveryAddressHumanReadableId]
|
,[DeliveryAddressHumanReadableId]
|
||||||
,DeliveryAddressDescription
|
,DeliveryAddressDescription
|
||||||
,[CustomerArtNo]
|
,[CustomerArtNo]
|
||||||
,[TotalPrice]
|
,[TotalPrice]
|
||||||
,r.[ArticleAlias]
|
,r.[ArticleAlias]
|
||||||
|
FROM [order].[Release] (nolock) AS r
|
||||||
|
LEFT JOIN [order].LineItem AS x ON r.LineItemId = x.id
|
||||||
|
LEFT JOIN [order].Header AS h ON x.HeaderId = h.id
|
||||||
|
|
||||||
FROM [order].[Release] (nolock) as r
|
-- 1.0 BOL (legacy) — unchanged
|
||||||
|
LEFT JOIN AlplaPROD_test1.dbo.V_LadePlanungenLadeAuftragAbruf (nolock) AS zz
|
||||||
|
ON zz.AbrufIdAuftragsAbruf = r.ReleaseNumber
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT * FROM (
|
||||||
|
SELECT ROW_NUMBER() OVER (PARTITION BY IdJournal ORDER BY add_date DESC) AS RowNum, *
|
||||||
|
FROM [AlplaPROD_test1].[dbo].[T_Lieferungen] (nolock)
|
||||||
|
) t WHERE RowNum = 1
|
||||||
|
) AS ea ON zz.IdLieferschein = ea.IdJournal
|
||||||
|
|
||||||
left join
|
-- 2.0 BOL (new)
|
||||||
[order].LineItem as x on
|
LEFT JOIN bol_20 ON bol_20.ReleaseId = r.Id AND bol_20.rn = 1
|
||||||
|
|
||||||
r.LineItemId = x.id
|
WHERE r.DeliveryDate BETWEEN @StartDate AND @EndDate
|
||||||
|
|
||||||
left join
|
|
||||||
[order].Header as h on
|
|
||||||
x.HeaderId = h.id
|
|
||||||
|
|
||||||
--bol stuff
|
|
||||||
left join
|
|
||||||
AlplaPROD_test1.dbo.V_LadePlanungenLadeAuftragAbruf (nolock) as zz
|
|
||||||
on zz.AbrufIdAuftragsAbruf = r.ReleaseNumber
|
|
||||||
|
|
||||||
left join
|
|
||||||
(select * from (SELECT
|
|
||||||
ROW_NUMBER() OVER (PARTITION BY IdJournal ORDER BY add_date DESC) AS RowNum
|
|
||||||
,*
|
|
||||||
FROM [AlplaPROD_test1].[dbo].[T_Lieferungen] (nolock)) x
|
|
||||||
|
|
||||||
where RowNum = 1) as ea on
|
|
||||||
zz.IdLieferschein = ea.IdJournal
|
|
||||||
|
|
||||||
where
|
|
||||||
--r.ReleaseNumber = 1452
|
|
||||||
|
|
||||||
r.DeliveryDate between @StartDate AND @EndDate
|
|
||||||
and DeliveredQuantity > 0
|
and DeliveredQuantity > 0
|
||||||
--and r.ArticleHumanReadableId in ([articles])
|
--and r.ArticleHumanReadableId in ([articles])
|
||||||
--and Journalnummer = 169386
|
--and Journalnummer = 169386
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
use AlplaPROD_test1
|
|
||||||
/**
|
|
||||||
|
|
||||||
move this over to the delivery date range query once we have the shift data mapped over correctly.
|
|
||||||
|
|
||||||
update the psi stuff on this as well.
|
|
||||||
**/
|
|
||||||
DECLARE @StartDate DATE = '[startDate]' -- 2025-1-1
|
|
||||||
DECLARE @EndDate DATE = '[endDate]' -- 2025-1-31
|
|
||||||
SELECT
|
|
||||||
r.[ArticleHumanReadableId]
|
|
||||||
,[ReleaseNumber]
|
|
||||||
,h.CustomerOrderNumber
|
|
||||||
,x.CustomerLineItemNumber
|
|
||||||
,[CustomerReleaseNumber]
|
|
||||||
,[ReleaseState]
|
|
||||||
,[DeliveryState]
|
|
||||||
,ea.JournalNummer as BOL_Number
|
|
||||||
,[ReleaseConfirmationState]
|
|
||||||
,[PlanningState]
|
|
||||||
--,format(r.[OrderDate], 'yyyy-MM-dd HH:mm') as OrderDate
|
|
||||||
,r.[OrderDate]
|
|
||||||
--,FORMAT(r.[DeliveryDate], 'yyyy-MM-dd HH:mm') as DeliveryDate
|
|
||||||
,r.[DeliveryDate]
|
|
||||||
--,FORMAT(r.[LoadingDate], 'yyyy-MM-dd HH:mm') as LoadingDate
|
|
||||||
,r.[LoadingDate]
|
|
||||||
,[Quantity]
|
|
||||||
,[DeliveredQuantity]
|
|
||||||
,r.[AdditionalInformation1]
|
|
||||||
,r.[AdditionalInformation2]
|
|
||||||
,[TradeUnits]
|
|
||||||
,[LoadingUnits]
|
|
||||||
,[Trucks]
|
|
||||||
,[LoadingToleranceType]
|
|
||||||
,[SalesPrice]
|
|
||||||
,[Currency]
|
|
||||||
,[QuantityUnit]
|
|
||||||
,[SalesPriceRemark]
|
|
||||||
,r.[Remark]
|
|
||||||
,[Irradiated]
|
|
||||||
,r.[CreatedByEdi]
|
|
||||||
,[DeliveryAddressHumanReadableId]
|
|
||||||
,DeliveryAddressDescription
|
|
||||||
,[CustomerArtNo]
|
|
||||||
,[TotalPrice]
|
|
||||||
,r.[ArticleAlias]
|
|
||||||
|
|
||||||
FROM [order].[Release] (nolock) as r
|
|
||||||
|
|
||||||
left join
|
|
||||||
[order].LineItem as x on
|
|
||||||
|
|
||||||
r.LineItemId = x.id
|
|
||||||
|
|
||||||
left join
|
|
||||||
[order].Header as h on
|
|
||||||
x.HeaderId = h.id
|
|
||||||
|
|
||||||
--bol stuff
|
|
||||||
left join
|
|
||||||
AlplaPROD_test1.dbo.V_LadePlanungenLadeAuftragAbruf (nolock) as zz
|
|
||||||
on zz.AbrufIdAuftragsAbruf = r.ReleaseNumber
|
|
||||||
|
|
||||||
left join
|
|
||||||
(select * from (SELECT
|
|
||||||
ROW_NUMBER() OVER (PARTITION BY IdJournal ORDER BY add_date DESC) AS RowNum
|
|
||||||
,*
|
|
||||||
FROM [AlplaPROD_test1].[dbo].[T_Lieferungen] (nolock)) x
|
|
||||||
|
|
||||||
where RowNum = 1) as ea on
|
|
||||||
zz.IdLieferschein = ea.IdJournal
|
|
||||||
|
|
||||||
where
|
|
||||||
r.ArticleHumanReadableId in ([articles])
|
|
||||||
--r.ReleaseNumber = 1452
|
|
||||||
|
|
||||||
and r.DeliveryDate between @StartDate AND @EndDate
|
|
||||||
--and DeliveredQuantity > 0
|
|
||||||
--and Journalnummer = 169386
|
|
||||||
@@ -1,32 +1,72 @@
|
|||||||
use AlplaPROD_test1
|
use [test1_AlplaPROD2.0_Read]
|
||||||
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
|
|
||||||
*/
|
|
||||||
|
|
||||||
select x.IdArtikelvarianten As Article,
|
DECLARE @start_date date = '[startDate]'; --'2025-01-01'
|
||||||
ProduktionAlias as Description,
|
DECLARE @end_date date = '[endDate]'; --'2025-08-09'
|
||||||
standort as MachineId,
|
DECLARE @tz sysname = 'Eastern Standard Time'; -- usday1; use 'Central Standard Time' for usksc1
|
||||||
MaschinenBezeichnung as MachineName,
|
DECLARE @shiftSeconds int = 7*3600; -- 07:00 production-day anchor
|
||||||
--MaschZyklus as PlanningCycleTime,
|
--TODO: add in the proper time zone based on the env, get correcr shift time as well
|
||||||
x.IdProdPlanung as LotNumber,
|
;WITH src AS (
|
||||||
FORMAT(ProdTag, 'MM/dd/yyyy') as ProductionDay,
|
SELECT
|
||||||
x.planMenge as TotalPlanned,
|
pl.RunningNumber, pl.ArticleHumanReadableId, pl.ArticleAlias, pl.ArticleDescription,
|
||||||
ProduktionMenge as QTYPerDay,
|
pl.MachineLocation, pl.MachineDescription, pl.ProductionLotState, pl.ProductionInterrupt,
|
||||||
round(ProduktionMengeVPK, 2) PalDay,
|
pl.PlanQuantityPieces, pl.PlanQuantityLoadingUnit,
|
||||||
Status as finished
|
CAST(pl.ProdStart AT TIME ZONE @tz AS datetime2(0)) AS StartLocal,
|
||||||
--MaschStdAuslastung as nee
|
CAST(pl.PlanEnd AT TIME ZONE @tz AS datetime2(0)) AS EndLocal
|
||||||
|
FROM productionScheduling.ProductionLot AS pl
|
||||||
from dbo.V_ProdLosProduktionJeProdTag_PLANNING (nolock) as x
|
WHERE pl.PlanEnd > pl.ProdStart
|
||||||
|
and pl.publishState = 1
|
||||||
left join
|
and pl.ArticleHumanReadableId IN ([articles]) -- <-- article filter (was IdArtikelvarianten)
|
||||||
dbo.V_ProdPlanung (nolock) as p on
|
--AND pl.RunningNumber = 28094 -- <-- lot filter (was IdProdPlanung); comment out for all
|
||||||
x.IdProdPlanung = p.IdProdPlanung
|
),
|
||||||
|
calc AS (
|
||||||
where ProdTag between @start_date and @end_date
|
SELECT *,
|
||||||
and p.IdArtikelvarianten in ([articles])
|
DATEADD(SECOND, @shiftSeconds,
|
||||||
--and V_ProdLosProduktionJeProdTag_PLANNING.IdKunde = 10
|
CAST(CASE WHEN DATEDIFF(SECOND, CAST(StartLocal AS date), StartLocal) >= @shiftSeconds
|
||||||
--and IdProdPlanung = 18442
|
THEN CAST(StartLocal AS date)
|
||||||
|
ELSE DATEADD(DAY,-1, CAST(StartLocal AS date)) END AS datetime2(0))) AS FirstBoundary
|
||||||
order by ProdTag desc
|
FROM src
|
||||||
|
),
|
||||||
|
days AS ( -- one row per production day the lot touches
|
||||||
|
SELECT RunningNumber, ArticleHumanReadableId, ArticleAlias, ArticleDescription, MachineLocation,
|
||||||
|
MachineDescription, ProductionLotState, PlanQuantityPieces, PlanQuantityLoadingUnit,
|
||||||
|
StartLocal, EndLocal, FirstBoundary AS DayStart
|
||||||
|
FROM calc
|
||||||
|
UNION ALL
|
||||||
|
SELECT RunningNumber, ArticleHumanReadableId, ArticleAlias, ArticleDescription, MachineLocation,
|
||||||
|
MachineDescription, ProductionLotState, PlanQuantityPieces, PlanQuantityLoadingUnit,
|
||||||
|
StartLocal, EndLocal, DATEADD(DAY,1,DayStart)
|
||||||
|
FROM days
|
||||||
|
WHERE DATEADD(DAY,1,DayStart) < EndLocal
|
||||||
|
),
|
||||||
|
seg AS ( -- working seconds inside each production day
|
||||||
|
SELECT *,
|
||||||
|
DATEDIFF(SECOND,
|
||||||
|
CASE WHEN StartLocal > DayStart THEN StartLocal ELSE DayStart END,
|
||||||
|
CASE WHEN EndLocal < DATEADD(DAY,1,DayStart) THEN EndLocal ELSE DATEADD(DAY,1,DayStart) END
|
||||||
|
) AS SegSec
|
||||||
|
FROM days
|
||||||
|
),
|
||||||
|
cum AS ( -- cumulative seconds for telescoping round
|
||||||
|
SELECT *,
|
||||||
|
SUM(SegSec) OVER (PARTITION BY RunningNumber ORDER BY DayStart ROWS UNBOUNDED PRECEDING) AS CumEnd,
|
||||||
|
SUM(SegSec) OVER (PARTITION BY RunningNumber ORDER BY DayStart ROWS UNBOUNDED PRECEDING) - SegSec AS CumStart,
|
||||||
|
SUM(SegSec) OVER (PARTITION BY RunningNumber) AS DenomSec
|
||||||
|
FROM seg
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
ArticleHumanReadableId AS Article,
|
||||||
|
ArticleAlias AS Description,
|
||||||
|
MachineLocation AS MachineId,
|
||||||
|
MachineDescription AS MachineName,
|
||||||
|
RunningNumber AS LotNumber,
|
||||||
|
FORMAT(DayStart, 'MM/dd/yyyy') AS ProductionDay,
|
||||||
|
PlanQuantityPieces AS TotalPlanned,
|
||||||
|
ROUND(PlanQuantityPieces * 1.0 * CumEnd / DenomSec, 0)
|
||||||
|
- ROUND(PlanQuantityPieces * 1.0 * CumStart / DenomSec, 0) AS QTYPerDay,
|
||||||
|
ROUND(PlanQuantityLoadingUnit * CumEnd / DenomSec, 2)
|
||||||
|
- ROUND(PlanQuantityLoadingUnit * CumStart / DenomSec, 2) AS PalDay,
|
||||||
|
ProductionLotState AS finished
|
||||||
|
FROM cum
|
||||||
|
WHERE CAST(DayStart AS date) BETWEEN @start_date AND @end_date -- filter AFTER cumulative calc
|
||||||
|
ORDER BY RunningNumber, DayStart DESC
|
||||||
|
OPTION (MAXRECURSION 366);
|
||||||
|
|||||||
Reference in New Issue
Block a user