package queries

// GetProductStockAttributeOptionsQuery:
// Cascading filtre secenekleri. Kategori + Urun Ana Grubu zorunlu akisina uygundur.
const GetProductStockAttributeOptionsQuery = `
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)), '');

CREATE TABLE #AttrBase
(
    ProductCode NVARCHAR(50) NOT NULL,
    Kategori NVARCHAR(100) NOT NULL,
    UrunAnaGrubu NVARCHAR(100) NOT NULL,
    UrunAltGrubu NVARCHAR(100) NOT NULL,
    UrunIcerigi NVARCHAR(100) NOT NULL,
    Fit NVARCHAR(100) NOT NULL,
    DropVal NVARCHAR(100) NOT NULL
);

IF OBJECT_ID('dbo.ProductFilterTRCache','U') IS NOT NULL
BEGIN
    INSERT INTO #AttrBase (ProductCode, Kategori, UrunAnaGrubu, UrunAltGrubu, UrunIcerigi, Fit, DropVal)
    SELECT
        UPPER(LTRIM(RTRIM(ProductCode))) AS ProductCode,
        Kategori       = LTRIM(RTRIM(ProductAtt44Desc)),
        UrunAnaGrubu   = LTRIM(RTRIM(ProductAtt01Desc)),
        UrunAltGrubu   = LTRIM(RTRIM(ProductAtt02Desc)),
        UrunIcerigi    = LTRIM(RTRIM(ProductAtt41Desc)),
        Fit            = LTRIM(RTRIM(ProductAtt38Desc)),
        DropVal        = LTRIM(RTRIM(ProductAtt11Desc))
    FROM dbo.ProductFilterTRCache
    WHERE LEN(ProductCode) = 13
      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
ELSE
BEGIN
    INSERT INTO #AttrBase (ProductCode, Kategori, UrunAnaGrubu, UrunAltGrubu, UrunIcerigi, Fit, DropVal)
    SELECT
        UPPER(LTRIM(RTRIM(ProductCode))) AS ProductCode,
        Kategori       = LTRIM(RTRIM(ProductAtt44Desc)),
        UrunAnaGrubu   = LTRIM(RTRIM(ProductAtt01Desc)),
        UrunAltGrubu   = LTRIM(RTRIM(ProductAtt02Desc)),
        UrunIcerigi    = LTRIM(RTRIM(ProductAtt41Desc)),
        Fit            = LTRIM(RTRIM(ProductAtt38Desc)),
        DropVal        = LTRIM(RTRIM(ProductAtt11Desc))
    FROM ProductFilterWithDescription('TR')
    WHERE LEN(ProductCode) = 13
      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;

IF @Kategori IS NULL OR @UrunAnaGrubu IS NULL
BEGIN
    CREATE CLUSTERED INDEX IX_AttrBase_ProductCode ON #AttrBase(ProductCode);
    SELECT 'kategori' AS FieldName, X.FieldValue
    FROM (
        SELECT DISTINCT FieldValue = AB.Kategori
        FROM #AttrBase AB
        WHERE AB.Kategori <> ''
    ) X
    UNION ALL
    SELECT 'urun_ana_grubu', X.FieldValue
    FROM (
        SELECT DISTINCT FieldValue = AB.UrunAnaGrubu
        FROM #AttrBase AB
        WHERE AB.UrunAnaGrubu <> ''
    ) X;
    RETURN;
END;

CREATE CLUSTERED INDEX IX_AttrBase_ProductCode ON #AttrBase(ProductCode);
CREATE NONCLUSTERED INDEX IX_AttrBase_Filter ON #AttrBase(Kategori, UrunAnaGrubu, UrunAltGrubu, UrunIcerigi, Fit, DropVal);

;WITH INV AS
(
    SELECT
        X.ItemCode,
        X.ColorCode,
        X.ItemDim1Code,
        X.ItemDim2Code,
        SUM(X.PickingQty1) AS PickingQty1,
        SUM(X.ReserveQty1) AS ReserveQty1,
        SUM(X.DispOrderQty1) AS DispOrderQty1,
        SUM(X.InventoryQty1) AS InventoryQty1
    FROM
    (
        SELECT
            UPPER(LTRIM(RTRIM(P.ItemCode))) AS ItemCode, P.ColorCode, P.ItemDim1Code, P.ItemDim2Code,
            P.Qty1 AS PickingQty1, 0 AS ReserveQty1, 0 AS DispOrderQty1, 0 AS InventoryQty1
        FROM PickingStates P
        INNER JOIN #AttrBase AB ON AB.ProductCode = UPPER(LTRIM(RTRIM(P.ItemCode)))
        WHERE P.ItemTypeCode = 1
          AND LEN(LTRIM(RTRIM(P.ItemCode))) = 13

        UNION ALL
        SELECT
            UPPER(LTRIM(RTRIM(R.ItemCode))) AS ItemCode, R.ColorCode, R.ItemDim1Code, R.ItemDim2Code,
            0, R.Qty1, 0, 0
        FROM ReserveStates R
        INNER JOIN #AttrBase AB ON AB.ProductCode = UPPER(LTRIM(RTRIM(R.ItemCode)))
        WHERE R.ItemTypeCode = 1
          AND LEN(LTRIM(RTRIM(R.ItemCode))) = 13

        UNION ALL
        SELECT
            UPPER(LTRIM(RTRIM(D.ItemCode))) AS ItemCode, D.ColorCode, D.ItemDim1Code, D.ItemDim2Code,
            0, 0, D.Qty1, 0
        FROM DispOrderStates D
        INNER JOIN #AttrBase AB ON AB.ProductCode = UPPER(LTRIM(RTRIM(D.ItemCode)))
        WHERE D.ItemTypeCode = 1
          AND LEN(LTRIM(RTRIM(D.ItemCode))) = 13

        UNION ALL
        SELECT
            UPPER(LTRIM(RTRIM(T.ItemCode))) AS ItemCode, T.ColorCode, T.ItemDim1Code, T.ItemDim2Code,
            0, 0, 0, SUM(T.In_Qty1 - T.Out_Qty1)
        FROM trStock T
        INNER JOIN #AttrBase AB ON AB.ProductCode = UPPER(LTRIM(RTRIM(T.ItemCode)))
        WHERE T.ItemTypeCode = 1
          AND LEN(LTRIM(RTRIM(T.ItemCode))) = 13
        GROUP BY UPPER(LTRIM(RTRIM(T.ItemCode))), T.ColorCode, T.ItemDim1Code, T.ItemDim2Code
    ) X
    GROUP BY X.ItemCode, X.ColorCode, X.ItemDim1Code, X.ItemDim2Code
),
Avail AS
(
    SELECT
        I.ItemCode,
        Renk = LTRIM(RTRIM(I.ColorCode)),
        RenkAciklama = LTRIM(RTRIM(C.ColorDescription)),
        Renk2 = LTRIM(RTRIM(I.ItemDim2Code)),
        Beden = LTRIM(RTRIM(I.ItemDim1Code)),
        Kullanilabilir = (I.InventoryQty1 - I.PickingQty1 - I.ReserveQty1 - I.DispOrderQty1)
    FROM INV I
    LEFT JOIN cdColorDesc C
      ON C.ColorCode = I.ColorCode
     AND C.LangCode = 'TR'
    WHERE (I.InventoryQty1 - I.PickingQty1 - I.ReserveQty1 - I.DispOrderQty1) > 0
)
SELECT 'kategori' AS FieldName, X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = AB.Kategori
    FROM #AttrBase AB
    WHERE AB.Kategori <> ''
) X
UNION ALL
SELECT 'urun_ana_grubu', X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = AB.UrunAnaGrubu
    FROM #AttrBase AB
    WHERE AB.UrunAnaGrubu <> ''
) X
UNION ALL
SELECT 'urun_alt_grubu', X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = AB.UrunAltGrubu
    FROM #AttrBase AB
    WHERE AB.UrunAltGrubu <> ''
      AND (@Kategori IS NULL OR AB.Kategori = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR AB.UrunAnaGrubu = @UrunAnaGrubu)
) X
UNION ALL
SELECT 'renk', X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = CASE WHEN A.RenkAciklama <> '' THEN A.RenkAciklama ELSE A.Renk END
    FROM Avail A
    INNER JOIN #AttrBase AB ON AB.ProductCode = UPPER(LTRIM(RTRIM(A.ItemCode)))
    WHERE (CASE WHEN A.RenkAciklama <> '' THEN A.RenkAciklama ELSE A.Renk END) <> ''
      AND (@Kategori IS NULL OR AB.Kategori = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR AB.UrunAnaGrubu = @UrunAnaGrubu)
      AND (@UrunAltGrubuList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunAltGrubu,'') + NCHAR(31), NCHAR(31) + @UrunAltGrubuList + NCHAR(31)) > 0)
      AND (@UrunIcerigiList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunIcerigi,'') + NCHAR(31), NCHAR(31) + @UrunIcerigiList + NCHAR(31)) > 0)
      AND (@FitList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.Fit,'') + NCHAR(31), NCHAR(31) + @FitList + NCHAR(31)) > 0)
      AND (@DropList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.DropVal,'') + NCHAR(31), NCHAR(31) + @DropList + NCHAR(31)) > 0)
      AND (@Renk2List IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(A.Renk2,'') + NCHAR(31), NCHAR(31) + @Renk2List + NCHAR(31)) > 0)
      AND (@BedenList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(A.Beden,'') + NCHAR(31), NCHAR(31) + @BedenList + NCHAR(31)) > 0)
) X
UNION ALL
SELECT 'renk2', X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = A.Renk2
    FROM Avail A
    INNER JOIN #AttrBase AB ON AB.ProductCode = UPPER(LTRIM(RTRIM(A.ItemCode)))
    WHERE A.Renk2 <> ''
      AND (@Kategori IS NULL OR AB.Kategori = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR AB.UrunAnaGrubu = @UrunAnaGrubu)
      AND (@UrunAltGrubuList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunAltGrubu,'') + NCHAR(31), NCHAR(31) + @UrunAltGrubuList + NCHAR(31)) > 0)
      AND (@UrunIcerigiList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunIcerigi,'') + NCHAR(31), NCHAR(31) + @UrunIcerigiList + NCHAR(31)) > 0)
      AND (@FitList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.Fit,'') + NCHAR(31), NCHAR(31) + @FitList + NCHAR(31)) > 0)
      AND (@DropList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.DropVal,'') + NCHAR(31), NCHAR(31) + @DropList + NCHAR(31)) > 0)
      AND (@RenkList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL((CASE WHEN A.RenkAciklama <> '' THEN A.RenkAciklama ELSE A.Renk END),'') + NCHAR(31), NCHAR(31) + @RenkList + NCHAR(31)) > 0)
      AND (@BedenList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(A.Beden,'') + NCHAR(31), NCHAR(31) + @BedenList + NCHAR(31)) > 0)
) X
UNION ALL
SELECT 'urun_icerigi', X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = AB.UrunIcerigi
    FROM #AttrBase AB
    WHERE AB.UrunIcerigi <> ''
      AND (@Kategori IS NULL OR AB.Kategori = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR AB.UrunAnaGrubu = @UrunAnaGrubu)
      AND (@UrunAltGrubuList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunAltGrubu,'') + NCHAR(31), NCHAR(31) + @UrunAltGrubuList + NCHAR(31)) > 0)
) X
UNION ALL
SELECT 'fit', X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = AB.Fit
    FROM #AttrBase AB
    WHERE AB.Fit <> ''
      AND (@Kategori IS NULL OR AB.Kategori = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR AB.UrunAnaGrubu = @UrunAnaGrubu)
      AND (@UrunAltGrubuList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunAltGrubu,'') + NCHAR(31), NCHAR(31) + @UrunAltGrubuList + NCHAR(31)) > 0)
      AND (@UrunIcerigiList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunIcerigi,'') + NCHAR(31), NCHAR(31) + @UrunIcerigiList + NCHAR(31)) > 0)
) X
UNION ALL
SELECT 'drop', X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = AB.DropVal
    FROM #AttrBase AB
    WHERE AB.DropVal <> ''
      AND (@Kategori IS NULL OR AB.Kategori = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR AB.UrunAnaGrubu = @UrunAnaGrubu)
      AND (@UrunAltGrubuList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunAltGrubu,'') + NCHAR(31), NCHAR(31) + @UrunAltGrubuList + NCHAR(31)) > 0)
      AND (@UrunIcerigiList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunIcerigi,'') + NCHAR(31), NCHAR(31) + @UrunIcerigiList + NCHAR(31)) > 0)
      AND (@FitList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.Fit,'') + NCHAR(31), NCHAR(31) + @FitList + NCHAR(31)) > 0)
) X
UNION ALL
SELECT 'beden', X.FieldValue
FROM (
    SELECT DISTINCT FieldValue = A.Beden
    FROM Avail A
    INNER JOIN #AttrBase AB ON AB.ProductCode = UPPER(LTRIM(RTRIM(A.ItemCode)))
    WHERE A.Beden <> ''
      AND (@Kategori IS NULL OR AB.Kategori = @Kategori)
      AND (@UrunAnaGrubu IS NULL OR AB.UrunAnaGrubu = @UrunAnaGrubu)
      AND (@UrunAltGrubuList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunAltGrubu,'') + NCHAR(31), NCHAR(31) + @UrunAltGrubuList + NCHAR(31)) > 0)
      AND (@UrunIcerigiList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.UrunIcerigi,'') + NCHAR(31), NCHAR(31) + @UrunIcerigiList + NCHAR(31)) > 0)
      AND (@FitList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.Fit,'') + NCHAR(31), NCHAR(31) + @FitList + NCHAR(31)) > 0)
      AND (@DropList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(AB.DropVal,'') + NCHAR(31), NCHAR(31) + @DropList + NCHAR(31)) > 0)
      AND (@RenkList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL((CASE WHEN A.RenkAciklama <> '' THEN A.RenkAciklama ELSE A.Renk END),'') + NCHAR(31), NCHAR(31) + @RenkList + NCHAR(31)) > 0)
      AND (@Renk2List IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(A.Renk2,'') + NCHAR(31), NCHAR(31) + @Renk2List + NCHAR(31)) > 0)
) X
OPTION (RECOMPILE);
`

// GetProductStockQueryByAttributes:
// Urun ozelliklerine gore stok detay sorgusu.
const GetProductStockQueryByAttributes = `
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)), '');

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,
    ProductAtt21Desc NVARCHAR(255) NULL,
    ProductAtt22Desc NVARCHAR(255) NULL,
    ProductAtt23Desc NVARCHAR(255) NULL,
    ProductAtt24Desc NVARCHAR(255) NULL,
    ProductAtt25Desc NVARCHAR(255) NULL,
    ProductAtt26Desc NVARCHAR(255) NULL,
    ProductAtt27Desc NVARCHAR(255) NULL,
    ProductAtt28Desc NVARCHAR(255) NULL,
    ProductAtt29Desc NVARCHAR(255) NULL,
    ProductAtt30Desc NVARCHAR(255) NULL,
    ProductAtt31Desc NVARCHAR(255) NULL,
    ProductAtt32Desc NVARCHAR(255) NULL,
    ProductAtt33Desc NVARCHAR(255) NULL,
    ProductAtt34Desc NVARCHAR(255) NULL,
    ProductAtt35Desc NVARCHAR(255) NULL,
    ProductAtt36Desc NVARCHAR(255) NULL,
    ProductAtt37Desc NVARCHAR(255) NULL,
    ProductAtt38Desc NVARCHAR(255) NULL,
    ProductAtt39Desc NVARCHAR(255) NULL,
    ProductAtt40Desc NVARCHAR(255) NULL,
    ProductAtt41Desc NVARCHAR(255) NULL,
    ProductAtt42Desc NVARCHAR(255) NULL,
    ProductAtt43Desc NVARCHAR(255) NULL,
    ProductAtt44Desc NVARCHAR(255) NULL,
    ProductAtt45Desc NVARCHAR(255) NULL,
    ProductAtt46Desc NVARCHAR(255) NULL
);

IF OBJECT_ID('dbo.ProductFilterTRCache','U') IS NOT NULL
BEGIN
    INSERT INTO #AttrFiltered
    (
        ProductCode,
        ProductDescription,
        ProductAtt01Desc,
        ProductAtt02Desc,
        ProductAtt10Desc,
        ProductAtt11Desc,
        ProductAtt21Desc,
        ProductAtt22Desc,
        ProductAtt23Desc,
        ProductAtt24Desc,
        ProductAtt25Desc,
        ProductAtt26Desc,
        ProductAtt27Desc,
        ProductAtt28Desc,
        ProductAtt29Desc,
        ProductAtt30Desc,
        ProductAtt31Desc,
        ProductAtt32Desc,
        ProductAtt33Desc,
        ProductAtt34Desc,
        ProductAtt35Desc,
        ProductAtt36Desc,
        ProductAtt37Desc,
        ProductAtt38Desc,
        ProductAtt39Desc,
        ProductAtt40Desc,
        ProductAtt41Desc,
        ProductAtt42Desc,
        ProductAtt43Desc,
        ProductAtt44Desc,
        ProductAtt45Desc,
        ProductAtt46Desc
    )
    SELECT
        UPPER(LTRIM(RTRIM(C.ProductCode))) AS ProductCode,
        C.ProductDescription,
        C.ProductAtt01Desc,
        C.ProductAtt02Desc,
        ISNULL(PF.ProductAtt10Desc, '') AS ProductAtt10Desc,
        C.ProductAtt11Desc,
        ISNULL(PF.ProductAtt21Desc, '') AS ProductAtt21Desc,
        ISNULL(PF.ProductAtt22Desc, '') AS ProductAtt22Desc,
        ISNULL(PF.ProductAtt23Desc, '') AS ProductAtt23Desc,
        ISNULL(PF.ProductAtt24Desc, '') AS ProductAtt24Desc,
        ISNULL(PF.ProductAtt25Desc, '') AS ProductAtt25Desc,
        ISNULL(PF.ProductAtt26Desc, '') AS ProductAtt26Desc,
        ISNULL(PF.ProductAtt27Desc, '') AS ProductAtt27Desc,
        ISNULL(PF.ProductAtt28Desc, '') AS ProductAtt28Desc,
        ISNULL(PF.ProductAtt29Desc, '') AS ProductAtt29Desc,
        ISNULL(PF.ProductAtt30Desc, '') AS ProductAtt30Desc,
        ISNULL(PF.ProductAtt31Desc, '') AS ProductAtt31Desc,
        ISNULL(PF.ProductAtt32Desc, '') AS ProductAtt32Desc,
        ISNULL(PF.ProductAtt33Desc, '') AS ProductAtt33Desc,
        ISNULL(PF.ProductAtt34Desc, '') AS ProductAtt34Desc,
        ISNULL(PF.ProductAtt35Desc, '') AS ProductAtt35Desc,
        ISNULL(PF.ProductAtt36Desc, '') AS ProductAtt36Desc,
        ISNULL(PF.ProductAtt37Desc, '') AS ProductAtt37Desc,
        C.ProductAtt38Desc,
        ISNULL(PF.ProductAtt39Desc, '') AS ProductAtt39Desc,
        ISNULL(PF.ProductAtt40Desc, '') AS ProductAtt40Desc,
        C.ProductAtt41Desc,
        ISNULL(PF.ProductAtt42Desc, '') AS ProductAtt42Desc,
        ISNULL(PF.ProductAtt43Desc, '') AS ProductAtt43Desc,
        C.ProductAtt44Desc,
        ISNULL(PF.ProductAtt45Desc, '') AS ProductAtt45Desc,
        ISNULL(PF.ProductAtt46Desc, '') AS ProductAtt46Desc
    FROM dbo.ProductFilterTRCache C
    LEFT JOIN ProductFilterWithDescription('TR') PF ON UPPER(LTRIM(RTRIM(PF.ProductCode))) = UPPER(LTRIM(RTRIM(C.ProductCode)))
    WHERE LEN(C.ProductCode) = 13
      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
	(
		ProductCode,
        ProductDescription,
        ProductAtt01Desc,
        ProductAtt02Desc,
        ProductAtt10Desc,
        ProductAtt11Desc,
        ProductAtt21Desc,
        ProductAtt22Desc,
        ProductAtt23Desc,
        ProductAtt24Desc,
        ProductAtt25Desc,
        ProductAtt26Desc,
        ProductAtt27Desc,
        ProductAtt28Desc,
        ProductAtt29Desc,
        ProductAtt30Desc,
        ProductAtt31Desc,
        ProductAtt32Desc,
        ProductAtt33Desc,
        ProductAtt34Desc,
        ProductAtt35Desc,
        ProductAtt36Desc,
        ProductAtt37Desc,
        ProductAtt38Desc,
        ProductAtt39Desc,
        ProductAtt40Desc,
        ProductAtt41Desc,
        ProductAtt42Desc,
        ProductAtt43Desc,
        ProductAtt44Desc,
        ProductAtt45Desc,
        ProductAtt46Desc
    )
    SELECT
        UPPER(LTRIM(RTRIM(ProductCode))) AS ProductCode,
        ProductDescription,
        ProductAtt01Desc,
        ProductAtt02Desc,
        ProductAtt10Desc,
        ProductAtt11Desc,
        ProductAtt21Desc,
        ProductAtt22Desc,
        ProductAtt23Desc,
        ProductAtt24Desc,
        ProductAtt25Desc,
        ProductAtt26Desc,
        ProductAtt27Desc,
        ProductAtt28Desc,
        ProductAtt29Desc,
        ProductAtt30Desc,
        ProductAtt31Desc,
        ProductAtt32Desc,
        ProductAtt33Desc,
        ProductAtt34Desc,
        ProductAtt35Desc,
        ProductAtt36Desc,
        ProductAtt37Desc,
        ProductAtt38Desc,
        ProductAtt39Desc,
        ProductAtt40Desc,
        ProductAtt41Desc,
        ProductAtt42Desc,
        ProductAtt43Desc,
        ProductAtt44Desc,
        ProductAtt45Desc,
        ProductAtt46Desc
    FROM ProductFilterWithDescription('TR')
    WHERE LEN(ProductCode) = 13
      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.CompanyCode,
        X.OfficeCode,
        X.StoreTypeCode,
        X.StoreCode,
        X.WarehouseCode,
        X.ItemTypeCode,
        X.ItemCode,
        X.ColorCode,
        X.ItemDim1Code,
        X.ItemDim2Code,
        X.ItemDim3Code,
        SUM(X.PickingQty1) AS PickingQty1,
        SUM(X.ReserveQty1) AS ReserveQty1,
        SUM(X.DispOrderQty1) AS DispOrderQty1,
        SUM(X.InventoryQty1) AS InventoryQty1
    FROM
    (
        SELECT
            P.CompanyCode, P.OfficeCode, P.StoreTypeCode, P.StoreCode, P.WarehouseCode,
            P.ItemTypeCode, UPPER(LTRIM(RTRIM(P.ItemCode))) AS ItemCode, P.ColorCode, P.ItemDim1Code, P.ItemDim2Code, P.ItemDim3Code,
            P.Qty1 AS PickingQty1, 0 AS ReserveQty1, 0 AS DispOrderQty1, 0 AS InventoryQty1
        FROM PickingStates P
        INNER JOIN #AttrFiltered AF ON AF.ProductCode = UPPER(LTRIM(RTRIM(P.ItemCode)))
        WHERE P.ItemTypeCode = 1
          AND LEN(LTRIM(RTRIM(P.ItemCode))) = 13
          AND P.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','1-0-4','1-0-29'
          )

        UNION ALL
        SELECT
            R.CompanyCode, R.OfficeCode, R.StoreTypeCode, R.StoreCode, R.WarehouseCode,
            R.ItemTypeCode, UPPER(LTRIM(RTRIM(R.ItemCode))) AS ItemCode, R.ColorCode, R.ItemDim1Code, R.ItemDim2Code, R.ItemDim3Code,
            0, R.Qty1, 0, 0
        FROM ReserveStates R
        INNER JOIN #AttrFiltered AF ON AF.ProductCode = UPPER(LTRIM(RTRIM(R.ItemCode)))
        WHERE R.ItemTypeCode = 1
          AND LEN(LTRIM(RTRIM(R.ItemCode))) = 13
          AND R.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','1-0-4','1-0-29'
          )

        UNION ALL
        SELECT
            D.CompanyCode, D.OfficeCode, D.StoreTypeCode, D.StoreCode, D.WarehouseCode,
            D.ItemTypeCode, UPPER(LTRIM(RTRIM(D.ItemCode))) AS ItemCode, D.ColorCode, D.ItemDim1Code, D.ItemDim2Code, D.ItemDim3Code,
            0, 0, D.Qty1, 0
        FROM DispOrderStates D
        INNER JOIN #AttrFiltered AF ON AF.ProductCode = UPPER(LTRIM(RTRIM(D.ItemCode)))
        WHERE D.ItemTypeCode = 1
          AND LEN(LTRIM(RTRIM(D.ItemCode))) = 13
          AND D.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','1-0-4','1-0-29'
          )

        UNION ALL
        SELECT
            T.CompanyCode, T.OfficeCode, T.StoreTypeCode, T.StoreCode, T.WarehouseCode,
            T.ItemTypeCode, UPPER(LTRIM(RTRIM(T.ItemCode))) AS ItemCode, T.ColorCode, T.ItemDim1Code, T.ItemDim2Code, T.ItemDim3Code,
            0, 0, 0, SUM(T.In_Qty1 - T.Out_Qty1)
        FROM trStock T
        INNER JOIN #AttrFiltered AF ON AF.ProductCode = UPPER(LTRIM(RTRIM(T.ItemCode)))
        WHERE T.ItemTypeCode = 1
          AND LEN(LTRIM(RTRIM(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','1-0-4','1-0-29'
          )
        GROUP BY
            T.CompanyCode, T.OfficeCode, T.StoreTypeCode, T.StoreCode, T.WarehouseCode,
            T.ItemTypeCode, UPPER(LTRIM(RTRIM(T.ItemCode))), T.ColorCode, T.ItemDim1Code, T.ItemDim2Code, T.ItemDim3Code
    ) X
    GROUP BY
        X.CompanyCode, X.OfficeCode, X.StoreTypeCode, X.StoreCode, X.WarehouseCode,
        X.ItemTypeCode, X.ItemCode, X.ColorCode, X.ItemDim1Code, X.ItemDim2Code, X.ItemDim3Code
),
Avail AS
(
    SELECT
        I.CompanyCode,
        I.OfficeCode,
        I.StoreTypeCode,
        I.StoreCode,
        I.WarehouseCode,
        I.ItemTypeCode,
        I.ItemCode,
        I.ColorCode,
        I.ItemDim1Code,
        I.ItemDim2Code,
        I.ItemDim3Code,
        Kullanilabilir = ROUND(I.InventoryQty1 - I.PickingQty1 - I.ReserveQty1 - I.DispOrderQty1, U.RoundDigit),
        RenkAciklama = LTRIM(RTRIM(C.ColorDescription))
    FROM INV I
    JOIN cdItem CI
      ON CI.ItemTypeCode = I.ItemTypeCode
     AND UPPER(LTRIM(RTRIM(CI.ItemCode))) = I.ItemCode
    LEFT JOIN cdUnitOfMeasure U
      ON U.UnitOfMeasureCode = CI.UnitOfMeasureCode1
    LEFT JOIN cdColorDesc C
      ON C.ColorCode = I.ColorCode
     AND C.LangCode = 'TR'
    WHERE I.ItemTypeCode = 1
      AND LEN(LTRIM(RTRIM(I.ItemCode))) = 13
      AND (I.InventoryQty1 - I.PickingQty1 - I.ReserveQty1 - I.DispOrderQty1) > 0
      AND CI.IsBlocked = 0
      AND I.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','1-0-4','1-0-29'
      )
),
Grouped AS
(
    SELECT
        A.ItemCode,
        A.ColorCode,
        A.ItemDim2Code
    FROM Avail A
    INNER JOIN #AttrFiltered AF ON AF.ProductCode = UPPER(LTRIM(RTRIM(A.ItemCode)))
    WHERE (@RenkList IS NULL OR CHARINDEX(NCHAR(31) + ISNULL((CASE WHEN A.RenkAciklama <> '' THEN A.RenkAciklama ELSE A.ColorCode END),'') + NCHAR(31), NCHAR(31) + @RenkList + NCHAR(31)) > 0)
      AND (@Renk2List IS NULL OR CHARINDEX(NCHAR(31) + ISNULL(A.ItemDim2Code,'') + NCHAR(31), NCHAR(31) + @Renk2List + NCHAR(31)) > 0)
      AND (
          @BedenList IS NULL
          OR EXISTS (
                SELECT 1
                FROM Avail AB
                WHERE AB.ItemCode = A.ItemCode
                  AND AB.ColorCode = A.ColorCode
                  AND ISNULL(AB.ItemDim2Code, '') = ISNULL(A.ItemDim2Code, '')
                  AND CHARINDEX(NCHAR(31) + ISNULL(AB.ItemDim1Code,'') + NCHAR(31), NCHAR(31) + @BedenList + NCHAR(31)) > 0
          )
      )
    GROUP BY A.ItemCode, A.ColorCode, A.ItemDim2Code
)
SELECT
    A.WarehouseCode AS Depo_Kodu,
    W.WarehouseDescription AS Depo_Adi,
    IT.ItemTypeDescription AS InventoryType,
    A.ItemCode AS Urun_Kodu,
    AF.ProductDescription AS Madde_Aciklamasi,
    A.ColorCode AS Renk_Kodu,
    A.RenkAciklama AS Renk_Aciklamasi,
    A.ItemDim1Code AS Beden,
    A.ItemDim3Code AS Renk2,
    A.ItemDim2Code AS Yaka,
    A.Kullanilabilir AS Kullanilabilir_Envanter,
    AF.ProductAtt01Desc AS URUN_ANA_GRUBU,
    AF.ProductAtt02Desc AS URUN_ALT_GRUBU,
    AF.ProductAtt10Desc AS MARKA,
    AF.ProductAtt11Desc AS DR,
    AF.ProductAtt21Desc AS KALIP,
    AF.ProductAtt22Desc AS IKINCI_PARCA_KALIP,
    AF.ProductAtt23Desc AS PACA_GENISLIGI,
    AF.ProductAtt24Desc AS UCUNCU_PARCA_KALIP,
    AF.ProductAtt25Desc AS UCUNCU_PARCA_MODEL,
    AF.ProductAtt26Desc AS BIRINCI_PARCA_KUMAS,
    AF.ProductAtt27Desc AS IKINCI_PARCA_KUMAS,
    AF.ProductAtt28Desc AS UCUNCU_PARCA_KUMAS,
    AF.ProductAtt29Desc AS BIRINCI_PARCA_KARISIM,
    AF.ProductAtt30Desc AS IKINCI_PARCA_KARISIM,
    AF.ProductAtt31Desc AS UCUNCU_PARCA_KARISIM,
    AF.ProductAtt32Desc AS YAKA_TIPI,
    AF.ProductAtt33Desc AS DUGME,
    AF.ProductAtt34Desc AS YIRTMAC,
    AF.ProductAtt35Desc AS SEZON_YILI,
    AF.ProductAtt36Desc AS MEVSIM,
    AF.ProductAtt37Desc AS TABAN,
    AF.ProductAtt38Desc AS BIRINCI_PARCA_FIT,
    AF.ProductAtt39Desc AS IKINCI_PARCA_FIT,
    AF.ProductAtt40Desc AS BOS2,
    AF.ProductAtt41Desc AS URUN_ICERIGI,
    AF.ProductAtt42Desc AS SERI_FASON,
    AF.ProductAtt43Desc AS STOK_GIRIS_YONTEMI,
    AF.ProductAtt44Desc AS YETISKIN_GARSON,
    AF.ProductAtt45Desc AS ASKILI_YAN,
    AF.ProductAtt46Desc AS BOS3,
    P.Price AS Fiyat
FROM Avail A
INNER JOIN Grouped G
    ON G.ItemCode = A.ItemCode
   AND G.ColorCode = A.ColorCode
   AND ISNULL(G.ItemDim2Code, '') = ISNULL(A.ItemDim2Code, '')
INNER JOIN #AttrFiltered AF
    ON AF.ProductCode = UPPER(LTRIM(RTRIM(A.ItemCode)))
LEFT JOIN cdWarehouseDesc W
    ON W.WarehouseCode = A.WarehouseCode
   AND W.LangCode = 'TR'
LEFT JOIN bsItemTypeDesc IT
    ON IT.ItemTypeCode = A.ItemTypeCode
   AND IT.LangCode = 'TR'
OUTER APPLY (
    SELECT TOP 1 Price
    FROM prItemBasePrice PB
    WHERE PB.ItemTypeCode = 1
      AND UPPER(LTRIM(RTRIM(PB.ItemCode))) = UPPER(LTRIM(RTRIM(A.ItemCode)))
      AND LEN(LTRIM(RTRIM(PB.ItemCode))) = 13
    ORDER BY PB.PriceDate DESC
) P
OPTION (RECOMPILE);
`
