product performance grouped kpi improvements

This commit is contained in:
M_Kececi
2026-07-03 14:34:30 +03:00
parent 4aebb36ff2
commit 2f885a4841
+72 -44
View File
@@ -189,7 +189,9 @@ CurrentStock AS (
SELECT SELECT
ProductCode = UPPER(LTRIM(RTRIM(S.ItemCode))), ProductCode = UPPER(LTRIM(RTRIM(S.ItemCode))),
ColorCode = UPPER(LTRIM(RTRIM(ISNULL(S.ColorCode, '')))), ColorCode = UPPER(LTRIM(RTRIM(ISNULL(S.ColorCode, '')))),
SizeCode = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim1Code, '')))),
YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim2Code, '')))), YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim2Code, '')))),
WarehouseCode = LTRIM(RTRIM(S.WarehouseCode)),
InventoryQty1 = SUM(S.In_Qty1 - S.Out_Qty1) InventoryQty1 = SUM(S.In_Qty1 - S.Out_Qty1)
FROM trStock S WITH(NOLOCK) FROM trStock S WITH(NOLOCK)
INNER JOIN ActiveWarehouses W INNER JOIN ActiveWarehouses W
@@ -208,13 +210,15 @@ CurrentStock AS (
) )
AND (@p3 = '' OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE @p3 + '%') AND (@p3 = '' OR UPPER(LTRIM(RTRIM(S.ItemCode))) LIKE @p3 + '%')
AND S.DocumentDate < DATEADD(DAY, 1, @p2) AND S.DocumentDate < DATEADD(DAY, 1, @p2)
GROUP BY S.ItemCode, S.ColorCode, S.ItemDim2Code GROUP BY S.ItemCode, S.ColorCode, S.ItemDim1Code, S.ItemDim2Code, LTRIM(RTRIM(S.WarehouseCode))
), ),
CurrentPick AS ( CurrentPick AS (
SELECT SELECT
ProductCode = UPPER(LTRIM(RTRIM(P.ItemCode))), ProductCode = UPPER(LTRIM(RTRIM(P.ItemCode))),
ColorCode = UPPER(LTRIM(RTRIM(ISNULL(P.ColorCode, '')))), ColorCode = UPPER(LTRIM(RTRIM(ISNULL(P.ColorCode, '')))),
SizeCode = UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim1Code, '')))),
YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim2Code, '')))), YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim2Code, '')))),
WarehouseCode = LTRIM(RTRIM(P.WarehouseCode)),
PickingQty1 = SUM(P.Qty1) PickingQty1 = SUM(P.Qty1)
FROM PickingStates P WITH(NOLOCK) FROM PickingStates P WITH(NOLOCK)
INNER JOIN ActiveWarehouses W INNER JOIN ActiveWarehouses W
@@ -222,13 +226,15 @@ CurrentPick AS (
WHERE P.ItemTypeCode = 1 WHERE P.ItemTypeCode = 1
AND LEN(LTRIM(RTRIM(P.ItemCode))) = 13 AND LEN(LTRIM(RTRIM(P.ItemCode))) = 13
AND (@p3 = '' OR UPPER(LTRIM(RTRIM(P.ItemCode))) LIKE @p3 + '%') AND (@p3 = '' OR UPPER(LTRIM(RTRIM(P.ItemCode))) LIKE @p3 + '%')
GROUP BY P.ItemCode, P.ColorCode, P.ItemDim2Code GROUP BY P.ItemCode, P.ColorCode, P.ItemDim1Code, P.ItemDim2Code, LTRIM(RTRIM(P.WarehouseCode))
), ),
CurrentReserve AS ( CurrentReserve AS (
SELECT SELECT
ProductCode = UPPER(LTRIM(RTRIM(R.ItemCode))), ProductCode = UPPER(LTRIM(RTRIM(R.ItemCode))),
ColorCode = UPPER(LTRIM(RTRIM(ISNULL(R.ColorCode, '')))), ColorCode = UPPER(LTRIM(RTRIM(ISNULL(R.ColorCode, '')))),
SizeCode = UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim1Code, '')))),
YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim2Code, '')))), YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim2Code, '')))),
WarehouseCode = LTRIM(RTRIM(R.WarehouseCode)),
ReserveQty1 = SUM(R.Qty1) ReserveQty1 = SUM(R.Qty1)
FROM ReserveStates R WITH(NOLOCK) FROM ReserveStates R WITH(NOLOCK)
INNER JOIN ActiveWarehouses W INNER JOIN ActiveWarehouses W
@@ -236,13 +242,15 @@ CurrentReserve AS (
WHERE R.ItemTypeCode = 1 WHERE R.ItemTypeCode = 1
AND LEN(LTRIM(RTRIM(R.ItemCode))) = 13 AND LEN(LTRIM(RTRIM(R.ItemCode))) = 13
AND (@p3 = '' OR UPPER(LTRIM(RTRIM(R.ItemCode))) LIKE @p3 + '%') AND (@p3 = '' OR UPPER(LTRIM(RTRIM(R.ItemCode))) LIKE @p3 + '%')
GROUP BY R.ItemCode, R.ColorCode, R.ItemDim2Code GROUP BY R.ItemCode, R.ColorCode, R.ItemDim1Code, R.ItemDim2Code, LTRIM(RTRIM(R.WarehouseCode))
), ),
CurrentDisp AS ( CurrentDisp AS (
SELECT SELECT
ProductCode = UPPER(LTRIM(RTRIM(D.ItemCode))), ProductCode = UPPER(LTRIM(RTRIM(D.ItemCode))),
ColorCode = UPPER(LTRIM(RTRIM(ISNULL(D.ColorCode, '')))), ColorCode = UPPER(LTRIM(RTRIM(ISNULL(D.ColorCode, '')))),
SizeCode = UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim1Code, '')))),
YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim2Code, '')))), YakaKodu = UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim2Code, '')))),
WarehouseCode = LTRIM(RTRIM(D.WarehouseCode)),
DispOrderQty1 = SUM(D.Qty1) DispOrderQty1 = SUM(D.Qty1)
FROM DispOrderStates D WITH(NOLOCK) FROM DispOrderStates D WITH(NOLOCK)
INNER JOIN ActiveWarehouses W INNER JOIN ActiveWarehouses W
@@ -250,7 +258,7 @@ CurrentDisp AS (
WHERE D.ItemTypeCode = 1 WHERE D.ItemTypeCode = 1
AND LEN(LTRIM(RTRIM(D.ItemCode))) = 13 AND LEN(LTRIM(RTRIM(D.ItemCode))) = 13
AND (@p3 = '' OR UPPER(LTRIM(RTRIM(D.ItemCode))) LIKE @p3 + '%') AND (@p3 = '' OR UPPER(LTRIM(RTRIM(D.ItemCode))) LIKE @p3 + '%')
GROUP BY D.ItemCode, D.ColorCode, D.ItemDim2Code GROUP BY D.ItemCode, D.ColorCode, D.ItemDim1Code, D.ItemDim2Code, LTRIM(RTRIM(D.WarehouseCode))
), ),
CurrentAvailable AS ( CurrentAvailable AS (
SELECT SELECT
@@ -258,11 +266,17 @@ CurrentAvailable AS (
S.ProductCode, S.ProductCode,
S.ColorCode, S.ColorCode,
S.YakaKodu, S.YakaKodu,
StockQty = CAST(ROUND( StockQty = CAST(ROUND(SUM(
ISNULL(S.InventoryQty1, 0) CASE
WHEN ISNULL(S.InventoryQty1, 0)
- ISNULL(PK.PickingQty1, 0) - ISNULL(PK.PickingQty1, 0)
- ISNULL(RS.ReserveQty1, 0) - ISNULL(RS.ReserveQty1, 0)
- ISNULL(DP.DispOrderQty1, 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)
END),
2 2
) AS decimal(18,4)), ) AS decimal(18,4)),
InQty = CAST(0 AS decimal(18,4)), InQty = CAST(0 AS decimal(18,4)),
@@ -276,11 +290,12 @@ CurrentAvailable AS (
CountDiffQty = CAST(0 AS decimal(18,4)) CountDiffQty = CAST(0 AS decimal(18,4))
FROM CurrentStock S FROM CurrentStock S
LEFT JOIN CurrentPick PK LEFT JOIN CurrentPick PK
ON PK.ProductCode = S.ProductCode AND PK.ColorCode = S.ColorCode AND PK.YakaKodu = S.YakaKodu 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 LEFT JOIN CurrentReserve RS
ON RS.ProductCode = S.ProductCode AND RS.ColorCode = S.ColorCode AND RS.YakaKodu = S.YakaKodu 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 LEFT JOIN CurrentDisp DP
ON DP.ProductCode = S.ProductCode AND DP.ColorCode = S.ColorCode AND DP.YakaKodu = S.YakaKodu 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
) )
SELECT SELECT
StockDate, StockDate,
@@ -3867,6 +3882,9 @@ func ListProductPerformanceStockSizes(ctx context.Context, productCode, colorCod
if db.MssqlDB == nil { if db.MssqlDB == nil {
return nil, fmt.Errorf("mssql db nil") return nil, fmt.Errorf("mssql db nil")
} }
productCode = normalizeProductPerformanceProductCode(productCode)
colorCode = normalizeProductPerformanceCode(colorCode)
yakaKodu = normalizeProductPerformanceCode(yakaKodu)
rows, err := db.MssqlDB.QueryContext(ctx, ` rows, err := db.MssqlDB.QueryContext(ctx, `
;WITH ActiveWarehouses AS ( ;WITH ActiveWarehouses AS (
SELECT WarehouseCode SELECT WarehouseCode
@@ -3878,76 +3896,86 @@ func ListProductPerformanceStockSizes(ctx context.Context, productCode, colorCod
), ),
Stock AS ( Stock AS (
SELECT SELECT
SizeCode = LTRIM(RTRIM(ISNULL(S.ItemDim1Code, ''))), SizeCode = UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim1Code, '')))),
WarehouseCode = LTRIM(RTRIM(S.WarehouseCode)),
InventoryQty1 = SUM(S.In_Qty1 - S.Out_Qty1) InventoryQty1 = SUM(S.In_Qty1 - S.Out_Qty1)
FROM trStock S WITH(NOLOCK) FROM trStock S WITH(NOLOCK)
INNER JOIN ActiveWarehouses W INNER JOIN ActiveWarehouses W
ON W.WarehouseCode = LTRIM(RTRIM(S.WarehouseCode)) ON W.WarehouseCode = LTRIM(RTRIM(S.WarehouseCode))
WHERE S.ItemTypeCode = 1 WHERE S.ItemTypeCode = 1
AND LTRIM(RTRIM(S.ItemCode)) = @p1 AND UPPER(LTRIM(RTRIM(S.ItemCode))) = @p1
AND LTRIM(RTRIM(ISNULL(S.ColorCode, ''))) = @p2 AND UPPER(LTRIM(RTRIM(ISNULL(S.ColorCode, '')))) = @p2
AND LTRIM(RTRIM(ISNULL(S.ItemDim2Code, ''))) = @p3 AND UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim2Code, '')))) = @p3
GROUP BY LTRIM(RTRIM(ISNULL(S.ItemDim1Code, ''))) GROUP BY UPPER(LTRIM(RTRIM(ISNULL(S.ItemDim1Code, '')))), LTRIM(RTRIM(S.WarehouseCode))
), ),
Pick AS ( Pick AS (
SELECT SELECT
SizeCode = LTRIM(RTRIM(ISNULL(P.ItemDim1Code, ''))), SizeCode = UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim1Code, '')))),
WarehouseCode = LTRIM(RTRIM(P.WarehouseCode)),
PickingQty1 = SUM(P.Qty1) PickingQty1 = SUM(P.Qty1)
FROM PickingStates P WITH(NOLOCK) FROM PickingStates P WITH(NOLOCK)
INNER JOIN ActiveWarehouses W INNER JOIN ActiveWarehouses W
ON W.WarehouseCode = LTRIM(RTRIM(P.WarehouseCode)) ON W.WarehouseCode = LTRIM(RTRIM(P.WarehouseCode))
WHERE P.ItemTypeCode = 1 WHERE P.ItemTypeCode = 1
AND LTRIM(RTRIM(P.ItemCode)) = @p1 AND UPPER(LTRIM(RTRIM(P.ItemCode))) = @p1
AND LTRIM(RTRIM(ISNULL(P.ColorCode, ''))) = @p2 AND UPPER(LTRIM(RTRIM(ISNULL(P.ColorCode, '')))) = @p2
AND LTRIM(RTRIM(ISNULL(P.ItemDim2Code, ''))) = @p3 AND UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim2Code, '')))) = @p3
GROUP BY LTRIM(RTRIM(ISNULL(P.ItemDim1Code, ''))) GROUP BY UPPER(LTRIM(RTRIM(ISNULL(P.ItemDim1Code, '')))), LTRIM(RTRIM(P.WarehouseCode))
), ),
Reserve AS ( Reserve AS (
SELECT SELECT
SizeCode = LTRIM(RTRIM(ISNULL(R.ItemDim1Code, ''))), SizeCode = UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim1Code, '')))),
WarehouseCode = LTRIM(RTRIM(R.WarehouseCode)),
ReserveQty1 = SUM(R.Qty1) ReserveQty1 = SUM(R.Qty1)
FROM ReserveStates R WITH(NOLOCK) FROM ReserveStates R WITH(NOLOCK)
INNER JOIN ActiveWarehouses W INNER JOIN ActiveWarehouses W
ON W.WarehouseCode = LTRIM(RTRIM(R.WarehouseCode)) ON W.WarehouseCode = LTRIM(RTRIM(R.WarehouseCode))
WHERE R.ItemTypeCode = 1 WHERE R.ItemTypeCode = 1
AND LTRIM(RTRIM(R.ItemCode)) = @p1 AND UPPER(LTRIM(RTRIM(R.ItemCode))) = @p1
AND LTRIM(RTRIM(ISNULL(R.ColorCode, ''))) = @p2 AND UPPER(LTRIM(RTRIM(ISNULL(R.ColorCode, '')))) = @p2
AND LTRIM(RTRIM(ISNULL(R.ItemDim2Code, ''))) = @p3 AND UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim2Code, '')))) = @p3
GROUP BY LTRIM(RTRIM(ISNULL(R.ItemDim1Code, ''))) GROUP BY UPPER(LTRIM(RTRIM(ISNULL(R.ItemDim1Code, '')))), LTRIM(RTRIM(R.WarehouseCode))
), ),
Disp AS ( Disp AS (
SELECT SELECT
SizeCode = LTRIM(RTRIM(ISNULL(D.ItemDim1Code, ''))), SizeCode = UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim1Code, '')))),
WarehouseCode = LTRIM(RTRIM(D.WarehouseCode)),
DispOrderQty1 = SUM(D.Qty1) DispOrderQty1 = SUM(D.Qty1)
FROM DispOrderStates D WITH(NOLOCK) FROM DispOrderStates D WITH(NOLOCK)
INNER JOIN ActiveWarehouses W INNER JOIN ActiveWarehouses W
ON W.WarehouseCode = LTRIM(RTRIM(D.WarehouseCode)) ON W.WarehouseCode = LTRIM(RTRIM(D.WarehouseCode))
WHERE D.ItemTypeCode = 1 WHERE D.ItemTypeCode = 1
AND LTRIM(RTRIM(D.ItemCode)) = @p1 AND UPPER(LTRIM(RTRIM(D.ItemCode))) = @p1
AND LTRIM(RTRIM(ISNULL(D.ColorCode, ''))) = @p2 AND UPPER(LTRIM(RTRIM(ISNULL(D.ColorCode, '')))) = @p2
AND LTRIM(RTRIM(ISNULL(D.ItemDim2Code, ''))) = @p3 AND UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim2Code, '')))) = @p3
GROUP BY LTRIM(RTRIM(ISNULL(D.ItemDim1Code, ''))) GROUP BY UPPER(LTRIM(RTRIM(ISNULL(D.ItemDim1Code, '')))), LTRIM(RTRIM(D.WarehouseCode))
) ),
AvailableBySizeWarehouse AS (
SELECT SELECT
S.SizeCode, S.SizeCode,
StockQty = CAST(ROUND( StockQty =
ISNULL(S.InventoryQty1, 0) CASE
WHEN ISNULL(S.InventoryQty1, 0)
- ISNULL(P.PickingQty1, 0) - ISNULL(P.PickingQty1, 0)
- ISNULL(R.ReserveQty1, 0) - ISNULL(R.ReserveQty1, 0)
- ISNULL(D.DispOrderQty1, 0), - ISNULL(D.DispOrderQty1, 0) < 0 THEN 0
2 ELSE ISNULL(S.InventoryQty1, 0)
) AS FLOAT)
FROM Stock S
LEFT JOIN Pick P ON P.SizeCode = S.SizeCode
LEFT JOIN Reserve R ON R.SizeCode = S.SizeCode
LEFT JOIN Disp D ON D.SizeCode = S.SizeCode
WHERE (
ISNULL(S.InventoryQty1, 0)
- ISNULL(P.PickingQty1, 0) - ISNULL(P.PickingQty1, 0)
- ISNULL(R.ReserveQty1, 0) - ISNULL(R.ReserveQty1, 0)
- ISNULL(D.DispOrderQty1, 0) - ISNULL(D.DispOrderQty1, 0)
) <> 0 END
ORDER BY S.SizeCode FROM Stock S
LEFT JOIN Pick P ON P.SizeCode = S.SizeCode AND P.WarehouseCode = S.WarehouseCode
LEFT JOIN Reserve R ON R.SizeCode = S.SizeCode AND R.WarehouseCode = S.WarehouseCode
LEFT JOIN Disp D ON D.SizeCode = S.SizeCode AND D.WarehouseCode = S.WarehouseCode
)
SELECT
SizeCode,
StockQty = CAST(ROUND(SUM(StockQty), 2) AS FLOAT)
FROM AvailableBySizeWarehouse
GROUP BY SizeCode
HAVING SUM(StockQty) > 0
ORDER BY SizeCode
`, strings.TrimSpace(productCode), strings.TrimSpace(colorCode), strings.TrimSpace(yakaKodu)) `, strings.TrimSpace(productCode), strings.TrimSpace(colorCode), strings.TrimSpace(yakaKodu))
if err != nil { if err != nil {
return nil, err return nil, err