package queries

const GetProductSeriesStockRowsQuery = `
DECLARE @Kategori NVARCHAR(100) = NULLIF(LTRIM(RTRIM(@p1)), '');
DECLARE @UrunAnaGrubu NVARCHAR(100) = NULLIF(LTRIM(RTRIM(@p2)), '');
DECLARE @UrunAltGrubuList NVARCHAR(MAX) = NULLIF(LTRIM(RTRIM(@p3)), '');
DECLARE @RenkList NVARCHAR(MAX) = NULLIF(LTRIM(RTRIM(@p4)), '');
DECLARE @Renk2List NVARCHAR(MAX) = NULLIF(LTRIM(RTRIM(@p5)), '');
DECLARE @UrunIcerigiList NVARCHAR(MAX) = NULLIF(LTRIM(RTRIM(@p6)), '');
DECLARE @FitList NVARCHAR(MAX) = NULLIF(LTRIM(RTRIM(@p7)), '');
DECLARE @DropList NVARCHAR(MAX) = NULLIF(LTRIM(RTRIM(@p8)), '');
DECLARE @BedenList NVARCHAR(MAX) = NULLIF(LTRIM(RTRIM(@p9)), '');
DECLARE @Search NVARCHAR(80) = NULLIF(LTRIM(RTRIM(@p11)), '');
DECLARE @RowLimit INT;

IF ISNUMERIC(@p10) = 1
BEGIN
    SET @RowLimit = CONVERT(INT, @p10);
END;

IF @RowLimit IS NULL OR @RowLimit <= 0
BEGIN
    SET @RowLimit = 2147483647;
END;

CREATE TABLE #AttrFiltered
(
    ProductCode NVARCHAR(50) NOT NULL,
    ProductDescription NVARCHAR(255) NULL,
    ProductAtt01Desc NVARCHAR(255) NULL,
    ProductAtt02Desc NVARCHAR(255) NULL,
    ProductAtt10Desc NVARCHAR(255) NULL,
    ProductAtt11Desc NVARCHAR(255) NULL,
    ProductAtt38Desc NVARCHAR(255) NULL,
    ProductAtt41Desc NVARCHAR(255) NULL,
    ProductAtt44Desc NVARCHAR(255) NULL
);

IF OBJECT_ID('dbo.ProductFilterTRCache','U') IS NOT NULL
BEGIN
    INSERT INTO #AttrFiltered
    SELECT
        C.ProductCode,
        C.ProductDescription,
        C.ProductAtt01Desc,
        C.ProductAtt02Desc,
        ISNULL(PF.ProductAtt10Desc, '') AS ProductAtt10Desc,
        C.ProductAtt11Desc,
        C.ProductAtt38Desc,
        C.ProductAtt41Desc,
        C.ProductAtt44Desc
    FROM dbo.ProductFilterTRCache C
    LEFT JOIN ProductFilterWithDescription('TR') PF ON PF.ProductCode = C.ProductCode
    WHERE LEN(C.ProductCode) = 13
      AND (@Search IS NULL OR C.ProductCode LIKE '%' + @Search + '%' OR C.ProductDescription LIKE '%' + @Search + '%')
      AND (@Kategori IS NULL OR C.ProductAtt44Desc = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR C.ProductAtt01Desc = @UrunAnaGrubu)
      AND (@UrunAltGrubuList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(C.ProductAtt02Desc,'') + NCHAR(31), NCHAR(31) + @UrunAltGrubuList + NCHAR(31)) > 0)
      AND (@UrunIcerigiList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(C.ProductAtt41Desc,'') + NCHAR(31), NCHAR(31) + @UrunIcerigiList + NCHAR(31)) > 0)
      AND (@FitList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(C.ProductAtt38Desc,'') + NCHAR(31), NCHAR(31) + @FitList + NCHAR(31)) > 0)
      AND (@DropList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(C.ProductAtt11Desc,'') + NCHAR(31), NCHAR(31) + @DropList + NCHAR(31)) > 0);
END
ELSE
BEGIN
    INSERT INTO #AttrFiltered
    SELECT
        ProductCode,
        ProductDescription,
        ProductAtt01Desc,
        ProductAtt02Desc,
        ProductAtt10Desc,
        ProductAtt11Desc,
        ProductAtt38Desc,
        ProductAtt41Desc,
        ProductAtt44Desc
    FROM ProductFilterWithDescription('TR')
    WHERE LEN(ProductCode) = 13
      AND (@Search IS NULL OR ProductCode LIKE '%' + @Search + '%' OR ProductDescription LIKE '%' + @Search + '%')
      AND (@Kategori IS NULL OR ProductAtt44Desc = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR ProductAtt01Desc = @UrunAnaGrubu)
      AND (@UrunAltGrubuList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(ProductAtt02Desc,'') + NCHAR(31), NCHAR(31) + @UrunAltGrubuList + NCHAR(31)) > 0)
      AND (@UrunIcerigiList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(ProductAtt41Desc,'') + NCHAR(31), NCHAR(31) + @UrunIcerigiList + NCHAR(31)) > 0)
      AND (@FitList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(ProductAtt38Desc,'') + NCHAR(31), NCHAR(31) + @FitList + NCHAR(31)) > 0)
      AND (@DropList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(ProductAtt11Desc,'') + NCHAR(31), NCHAR(31) + @DropList + NCHAR(31)) > 0);
END;

CREATE CLUSTERED INDEX IX_AttrFiltered_ProductCode ON #AttrFiltered(ProductCode);

;WITH Inv AS
(
    SELECT
        X.ItemCode,
        X.ColorCode,
        X.ItemDim1Code,
        X.ItemDim2Code,
        SUM(X.InventoryQty1) AS InventoryQty1,
        SUM(X.PickingQty1) AS PickingQty1,
        SUM(X.ReserveQty1) AS ReserveQty1,
        SUM(X.DispOrderQty1) AS DispOrderQty1
    FROM
    (
        SELECT T.ItemCode, T.ColorCode, T.ItemDim1Code, T.ItemDim2Code,
               SUM(T.In_Qty1 - T.Out_Qty1) AS InventoryQty1, 0 AS PickingQty1, 0 AS ReserveQty1, 0 AS DispOrderQty1
        FROM trStock T
        INNER JOIN #AttrFiltered AF ON AF.ProductCode = T.ItemCode
        WHERE T.ItemTypeCode = 1
          AND LEN(T.ItemCode) = 13
          AND T.WarehouseCode IN (
            '1-0-14','1-0-10','1-0-8','1-2-5','1-2-4','1-0-12','100','1-0-28',
            '1-0-24','1-2-6','1-1-14','1-0-2','1-0-52','1-1-2','1-0-21','1-1-3',
            '1-0-33','101','1-014','1-0-49','1-0-36'
          )
        GROUP BY T.ItemCode, T.ColorCode, T.ItemDim1Code, T.ItemDim2Code

        UNION ALL
        SELECT P.ItemCode, P.ColorCode, P.ItemDim1Code, P.ItemDim2Code, 0, SUM(P.Qty1), 0, 0
        FROM PickingStates P
        INNER JOIN #AttrFiltered AF ON AF.ProductCode = P.ItemCode
        WHERE P.ItemTypeCode = 1 AND LEN(P.ItemCode) = 13
        GROUP BY P.ItemCode, P.ColorCode, P.ItemDim1Code, P.ItemDim2Code

        UNION ALL
        SELECT R.ItemCode, R.ColorCode, R.ItemDim1Code, R.ItemDim2Code, 0, 0, SUM(R.Qty1), 0
        FROM ReserveStates R
        INNER JOIN #AttrFiltered AF ON AF.ProductCode = R.ItemCode
        WHERE R.ItemTypeCode = 1 AND LEN(R.ItemCode) = 13
        GROUP BY R.ItemCode, R.ColorCode, R.ItemDim1Code, R.ItemDim2Code

        UNION ALL
        SELECT D.ItemCode, D.ColorCode, D.ItemDim1Code, D.ItemDim2Code, 0, 0, 0, SUM(D.Qty1)
        FROM DispOrderStates D
        INNER JOIN #AttrFiltered AF ON AF.ProductCode = D.ItemCode
        WHERE D.ItemTypeCode = 1 AND LEN(D.ItemCode) = 13
        GROUP BY D.ItemCode, D.ColorCode, D.ItemDim1Code, D.ItemDim2Code
    ) X
    GROUP BY X.ItemCode, X.ColorCode, X.ItemDim1Code, X.ItemDim2Code
),
Avail AS
(
    SELECT
        I.ItemCode,
        I.ColorCode,
        I.ItemDim1Code,
        I.ItemDim2Code,
        CAST(ROUND(I.InventoryQty1 - I.PickingQty1 - I.ReserveQty1 - I.DispOrderQty1, 2) AS DECIMAL(18,2)) AS Qty
    FROM Inv I
    JOIN cdItem CI
      ON CI.ItemTypeCode = 1
     AND CI.ItemCode = I.ItemCode
    WHERE CI.IsBlocked = 0
      AND (I.InventoryQty1 - I.PickingQty1 - I.ReserveQty1 - I.DispOrderQty1) > 0
      AND NULLIF(LTRIM(RTRIM(ISNULL(I.ColorCode, ''))), '') IS NOT NULL
      AND (@RenkList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(I.ColorCode,'') + NCHAR(31), NCHAR(31) + @RenkList + NCHAR(31)) > 0)
      AND (@Renk2List IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(I.ItemDim2Code,'') + NCHAR(31), NCHAR(31) + @Renk2List + NCHAR(31)) > 0)
      AND (@BedenList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(I.ItemDim1Code,'') + NCHAR(31), NCHAR(31) + @BedenList + NCHAR(31)) > 0)
),
Groups AS
(
    SELECT ItemCode, ColorCode, ItemDim2Code
    FROM
    (
        SELECT
            A.ItemCode,
            A.ColorCode,
            A.ItemDim2Code,
            ROW_NUMBER() OVER (ORDER BY A.ItemCode, A.ColorCode, A.ItemDim2Code) AS rn
        FROM Avail A
        GROUP BY A.ItemCode, A.ColorCode, A.ItemDim2Code
    ) X
    WHERE X.rn <= @RowLimit
)
SELECT
    G.ItemCode AS product_code,
    AF.ProductDescription AS product_description,
    G.ColorCode AS color_code,
    ISNULL(C.ColorDescription, '') AS color_title,
    ISNULL(G.ItemDim2Code, '') AS dim3_code,
    A.ItemDim1Code AS size_code,
    A.Qty AS qty,
    AF.ProductAtt01Desc AS urun_ana_grubu,
    AF.ProductAtt02Desc AS urun_alt_grubu,
    AF.ProductAtt10Desc AS marka,
    AF.ProductAtt11Desc AS drop_val,
    AF.ProductAtt38Desc AS fit,
    AF.ProductAtt41Desc AS urun_icerigi,
    AF.ProductAtt44Desc AS kategori
FROM Groups G
JOIN Avail A
  ON A.ItemCode = G.ItemCode
 AND A.ColorCode = G.ColorCode
 AND ISNULL(A.ItemDim2Code, '') = ISNULL(G.ItemDim2Code, '')
JOIN #AttrFiltered AF ON AF.ProductCode = G.ItemCode
LEFT JOIN cdColorDesc C
  ON C.ColorCode = G.ColorCode
 AND C.LangCode = 'TR'
ORDER BY G.ItemCode, G.ColorCode, G.ItemDim2Code, A.ItemDim1Code
OPTION (RECOMPILE);
`
