From 4cd1b118d05f7340fae8b114b8bd644d195ab9e3 Mon Sep 17 00:00:00 2001 From: M_Kececi Date: Fri, 3 Jul 2026 14:49:50 +0300 Subject: [PATCH] product performance grouped kpi improvements --- svc/queries/product_performance.go | 166 +++++++++++++++++------------ 1 file changed, 100 insertions(+), 66 deletions(-) diff --git a/svc/queries/product_performance.go b/svc/queries/product_performance.go index 065e3ac..cc17de5 100644 --- a/svc/queries/product_performance.go +++ b/svc/queries/product_performance.go @@ -185,97 +185,123 @@ Running AS ( CountDiffQty FROM Collapsed ), -CurrentStock AS ( - SELECT - ProductCode = UPPER(LTRIM(RTRIM(S.ItemCode))), - ColorCode = UPPER(LTRIM(RTRIM(ISNULL(S.ColorCode, '')))), - SizeCode = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim1Code, '')))), - YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim2Code, '')))), - WarehouseCode = LTRIM(RTRIM(S.WarehouseCode)), - InventoryQty1 = SUM(S.In_Qty1 - S.Out_Qty1) - FROM trStock S WITH(NOLOCK) - INNER JOIN ActiveWarehouses W - ON W.WarehouseCode = LTRIM(RTRIM(S.WarehouseCode)) - WHERE S.ItemTypeCode = 1 - AND ( - LEN(LTRIM(RTRIM(S.ItemCode))) = 13 - AND ( - UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'S%' - OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'O%' - OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'N%' - OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'X%' - OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'I%' - OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'A%' - ) - ) - AND (@p3 = '' OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE @p3 + '%') - AND S.DocumentDate < DATEADD(DAY, 1, @p2) - GROUP BY S.ItemCode, S.ColorCode, S.ItemDim1Code, S.ItemDim2Code, LTRIM(RTRIM(S.WarehouseCode)) -), -CurrentPick AS ( +CurrentSource AS ( SELECT + SourceTable = 'PickingStates', + WarehouseCode = LTRIM(RTRIM(P.WarehouseCode)), ProductCode = UPPER(LTRIM(RTRIM(P.ItemCode))), ColorCode = UPPER(LTRIM(RTRIM(ISNULL(P.ColorCode, '')))), SizeCode = UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim1Code, '')))), YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim2Code, '')))), - WarehouseCode = LTRIM(RTRIM(P.WarehouseCode)), - PickingQty1 = SUM(P.Qty1) + Dim3Code = UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim3Code, '')))), + PickingQty1 = SUM(P.Qty1), + ReserveQty1 = CAST(0 AS decimal(18,4)), + DispOrderQty1 = CAST(0 AS decimal(18,4)), + InventoryQty1 = CAST(0 AS decimal(18,4)) FROM PickingStates P WITH(NOLOCK) - INNER JOIN ActiveWarehouses W - ON W.WarehouseCode = LTRIM(RTRIM(P.WarehouseCode)) + INNER JOIN ActiveWarehouses W ON W.WarehouseCode = LTRIM(RTRIM(P.WarehouseCode)) WHERE P.ItemTypeCode = 1 AND LEN(LTRIM(RTRIM(P.ItemCode))) = 13 AND (@p3 = '' OR UPPER(LTRIM(RTRIM(P.ItemCode))) LIKE @p3 + '%') - GROUP BY P.ItemCode, P.ColorCode, P.ItemDim1Code, P.ItemDim2Code, LTRIM(RTRIM(P.WarehouseCode)) -), -CurrentReserve AS ( + GROUP BY P.WarehouseCode, P.ItemCode, P.ColorCode, P.ItemDim1Code, P.ItemDim2Code, P.ItemDim3Code + UNION ALL SELECT + SourceTable = 'ReserveStates', + WarehouseCode = LTRIM(RTRIM(R.WarehouseCode)), ProductCode = UPPER(LTRIM(RTRIM(R.ItemCode))), ColorCode = UPPER(LTRIM(RTRIM(ISNULL(R.ColorCode, '')))), SizeCode = UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim1Code, '')))), YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim2Code, '')))), - WarehouseCode = LTRIM(RTRIM(R.WarehouseCode)), - ReserveQty1 = SUM(R.Qty1) + Dim3Code = UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim3Code, '')))), + PickingQty1 = CAST(0 AS decimal(18,4)), + ReserveQty1 = SUM(R.Qty1), + DispOrderQty1 = CAST(0 AS decimal(18,4)), + InventoryQty1 = CAST(0 AS decimal(18,4)) FROM ReserveStates R WITH(NOLOCK) - INNER JOIN ActiveWarehouses W - ON W.WarehouseCode = LTRIM(RTRIM(R.WarehouseCode)) + INNER JOIN ActiveWarehouses W ON W.WarehouseCode = LTRIM(RTRIM(R.WarehouseCode)) WHERE R.ItemTypeCode = 1 AND LEN(LTRIM(RTRIM(R.ItemCode))) = 13 AND (@p3 = '' OR UPPER(LTRIM(RTRIM(R.ItemCode))) LIKE @p3 + '%') - GROUP BY R.ItemCode, R.ColorCode, R.ItemDim1Code, R.ItemDim2Code, LTRIM(RTRIM(R.WarehouseCode)) -), -CurrentDisp AS ( + GROUP BY R.WarehouseCode, R.ItemCode, R.ColorCode, R.ItemDim1Code, R.ItemDim2Code, R.ItemDim3Code + UNION ALL SELECT + SourceTable = 'DispOrderStates', + WarehouseCode = LTRIM(RTRIM(D.WarehouseCode)), ProductCode = UPPER(LTRIM(RTRIM(D.ItemCode))), ColorCode = UPPER(LTRIM(RTRIM(ISNULL(D.ColorCode, '')))), SizeCode = UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim1Code, '')))), YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim2Code, '')))), - WarehouseCode = LTRIM(RTRIM(D.WarehouseCode)), - DispOrderQty1 = SUM(D.Qty1) + Dim3Code = UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim3Code, '')))), + PickingQty1 = CAST(0 AS decimal(18,4)), + ReserveQty1 = CAST(0 AS decimal(18,4)), + DispOrderQty1 = SUM(D.Qty1), + InventoryQty1 = CAST(0 AS decimal(18,4)) FROM DispOrderStates D WITH(NOLOCK) - INNER JOIN ActiveWarehouses W - ON W.WarehouseCode = LTRIM(RTRIM(D.WarehouseCode)) + INNER JOIN ActiveWarehouses W ON W.WarehouseCode = LTRIM(RTRIM(D.WarehouseCode)) WHERE D.ItemTypeCode = 1 AND LEN(LTRIM(RTRIM(D.ItemCode))) = 13 AND (@p3 = '' OR UPPER(LTRIM(RTRIM(D.ItemCode))) LIKE @p3 + '%') - GROUP BY D.ItemCode, D.ColorCode, D.ItemDim1Code, D.ItemDim2Code, LTRIM(RTRIM(D.WarehouseCode)) + GROUP BY D.WarehouseCode, D.ItemCode, D.ColorCode, D.ItemDim1Code, D.ItemDim2Code, D.ItemDim3Code + UNION ALL + SELECT + SourceTable = 'trStock', + WarehouseCode = LTRIM(RTRIM(S.WarehouseCode)), + ProductCode = UPPER(LTRIM(RTRIM(S.ItemCode))), + ColorCode = UPPER(LTRIM(RTRIM(ISNULL(S.ColorCode, '')))), + SizeCode = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim1Code, '')))), + YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim2Code, '')))), + Dim3Code = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim3Code, '')))), + PickingQty1 = CAST(0 AS decimal(18,4)), + ReserveQty1 = CAST(0 AS decimal(18,4)), + DispOrderQty1 = CAST(0 AS decimal(18,4)), + InventoryQty1 = SUM(S.In_Qty1 - S.Out_Qty1) + FROM trStock S WITH(NOLOCK) + INNER JOIN ActiveWarehouses W ON W.WarehouseCode = LTRIM(RTRIM(S.WarehouseCode)) + WHERE S.ItemTypeCode = 1 + AND LEN(LTRIM(RTRIM(S.ItemCode))) = 13 + AND ( + UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'S%' + OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'O%' + OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'N%' + OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'X%' + OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'I%' + OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE 'A%' + ) + AND (@p3 = '' OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE @p3 + '%') + AND S.DocumentDate < DATEADD(DAY, 1, @p2) + GROUP BY S.WarehouseCode, S.ItemCode, S.ColorCode, S.ItemDim1Code, S.ItemDim2Code, S.ItemDim3Code +), +CurrentInventory AS ( + SELECT + ProductCode, + ColorCode, + SizeCode, + YakaKodu, + Dim3Code, + WarehouseCode, + PickingQty1 = SUM(PickingQty1), + ReserveQty1 = SUM(ReserveQty1), + DispOrderQty1 = SUM(DispOrderQty1), + InventoryQty1 = SUM(InventoryQty1) + FROM CurrentSource + GROUP BY WarehouseCode, ProductCode, ColorCode, SizeCode, YakaKodu, Dim3Code ), CurrentAvailable AS ( SELECT StockDate = @p2, - S.ProductCode, - S.ColorCode, - S.YakaKodu, + I.ProductCode, + I.ColorCode, + I.YakaKodu, StockQty = CAST(ROUND(SUM( CASE - WHEN ISNULL(S.InventoryQty1, 0) - - ISNULL(PK.PickingQty1, 0) - - ISNULL(RS.ReserveQty1, 0) - - ISNULL(DP.DispOrderQty1, 0) < 0 THEN 0 - ELSE ISNULL(S.InventoryQty1, 0) - - ISNULL(PK.PickingQty1, 0) - - ISNULL(RS.ReserveQty1, 0) - - ISNULL(DP.DispOrderQty1, 0) + WHEN ISNULL(I.InventoryQty1, 0) + - ISNULL(I.PickingQty1, 0) + - ISNULL(I.ReserveQty1, 0) + - ISNULL(I.DispOrderQty1, 0) < 0 THEN 0 + ELSE ISNULL(I.InventoryQty1, 0) + - ISNULL(I.PickingQty1, 0) + - ISNULL(I.ReserveQty1, 0) + - ISNULL(I.DispOrderQty1, 0) END), 2 ) AS decimal(18,4)), @@ -288,14 +314,22 @@ CurrentAvailable AS ( PurchaseInQty = CAST(0 AS decimal(18,4)), ConsumptionOutQty = CAST(0 AS decimal(18,4)), CountDiffQty = CAST(0 AS decimal(18,4)) - FROM CurrentStock S - LEFT JOIN CurrentPick PK - ON PK.ProductCode = S.ProductCode AND PK.ColorCode = S.ColorCode AND PK.SizeCode = S.SizeCode AND PK.YakaKodu = S.YakaKodu AND PK.WarehouseCode = S.WarehouseCode - LEFT JOIN CurrentReserve RS - ON RS.ProductCode = S.ProductCode AND RS.ColorCode = S.ColorCode AND RS.SizeCode = S.SizeCode AND RS.YakaKodu = S.YakaKodu AND RS.WarehouseCode = S.WarehouseCode - LEFT JOIN CurrentDisp DP - ON DP.ProductCode = S.ProductCode AND DP.ColorCode = S.ColorCode AND DP.SizeCode = S.SizeCode AND DP.YakaKodu = S.YakaKodu AND DP.WarehouseCode = S.WarehouseCode - GROUP BY S.ProductCode, S.ColorCode, S.YakaKodu + FROM CurrentInventory I + INNER JOIN cdItem WITH(NOLOCK) + ON cdItem.ItemTypeCode = 1 + AND UPPER(LTRIM(RTRIM(cdItem.ItemCode))) = I.ProductCode + AND cdItem.IsBlocked = 0 + WHERE I.InventoryQty1 >= 0 + AND LEN(I.ProductCode) = 13 + AND ( + I.ProductCode LIKE 'S%' + OR I.ProductCode LIKE 'O%' + OR I.ProductCode LIKE 'N%' + OR I.ProductCode LIKE 'X%' + OR I.ProductCode LIKE 'I%' + OR I.ProductCode LIKE 'A%' + ) + GROUP BY I.ProductCode, I.ColorCode, I.YakaKodu ) SELECT StockDate,