package queries

// 🔹 Sipariş ekranı stok kontrolü (ürün + renk + 2. renk + beden)
// 🔧 Renk1 boşsa (aksesuar vb.) renksiz stokları da çeker
const GetOrderInventory = `
DECLARE @ProductCode NVARCHAR(30) = @p1;
DECLARE @ColorCode   NVARCHAR(30) = @p2;
DECLARE @ColorCode2  NVARCHAR(30) = @p3;

-- 🔧 Normalize giriş parametreleri
SET @ColorCode  = LTRIM(RTRIM(ISNULL(@ColorCode, '')));
SET @ColorCode2 = LTRIM(RTRIM(ISNULL(@ColorCode2, '')));

IF @ColorCode = ''
BEGIN
    SET @ColorCode = '';
END

IF @ColorCode2 = ''
BEGIN
    SET @ColorCode2 = NULL;
END

------------------------------------------------------------
-- 🔹 Ana sorgu
------------------------------------------------------------
SELECT
    Inventory.ItemCode       AS Urun_Kodu,
    Inventory.ColorCode      AS Renk_Kodu,
    ISNULL((
        SELECT TOP 1 ColorDescription 
        FROM cdColorDesc
        WHERE cdColorDesc.ColorCode = Inventory.ColorCode
          AND cdColorDesc.LangCode = N'TR'
    ), '') AS Renk_Aciklamasi,

    -- ✅ NULL bedenleri boş string olarak getir
    ISNULL(Inventory.ItemDim1Code, '') AS Beden,
    ISNULL(Inventory.ItemDim2Code, '') AS Yaka,

    ROUND(
        SUM(Inventory.InventoryQty1) - 
        (SUM(Inventory.ReserveQty1) + SUM(Inventory.DispOrderQty1) + SUM(Inventory.PickingQty1)),
        cdUnitOfMeasure.RoundDigit
    ) AS Kullanilabilir_Envanter

FROM cdItem
JOIN cdUnitOfMeasure
    ON cdItem.UnitOfMeasureCode1 = cdUnitOfMeasure.UnitOfMeasureCode
JOIN (
    SELECT 
        CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
        ItemTypeCode, ItemCode, ColorCode, ItemDim1Code, ItemDim2Code, ItemDim3Code,
        SUM(CASE WHEN SourceTable = 'PickingStates'  THEN Qty1 ELSE 0 END) AS PickingQty1,
        SUM(CASE WHEN SourceTable = 'ReserveStates'  THEN Qty1 ELSE 0 END) AS ReserveQty1,
        SUM(CASE WHEN SourceTable = 'DispOrderStates' THEN Qty1 ELSE 0 END) AS DispOrderQty1,
        SUM(CASE WHEN SourceTable = 'trStock' THEN (In_Qty1 - Out_Qty1) ELSE 0 END) AS InventoryQty1
    FROM (
        SELECT 'PickingStates'  AS SourceTable, CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
               ItemTypeCode, ItemCode, ColorCode, ItemDim1Code, ItemDim2Code, ItemDim3Code,
               Qty1, 0 AS In_Qty1, 0 AS Out_Qty1
        FROM PickingStates
        UNION ALL
        SELECT 'ReserveStates', CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
               ItemTypeCode, ItemCode, ColorCode, ItemDim1Code, ItemDim2Code, ItemDim3Code,
               Qty1, 0, 0
        FROM ReserveStates
        UNION ALL
        SELECT 'DispOrderStates', CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
               ItemTypeCode, ItemCode, ColorCode, ItemDim1Code, ItemDim2Code, ItemDim3Code,
               Qty1, 0, 0
        FROM DispOrderStates
        UNION ALL
        SELECT 'trStock', CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
               ItemTypeCode, ItemCode, ColorCode, ItemDim1Code, ItemDim2Code, ItemDim3Code,
               0, SUM(In_Qty1), SUM(Out_Qty1)
        FROM trStock
        GROUP BY CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
                 ItemTypeCode, ItemCode, ColorCode, ItemDim1Code, ItemDim2Code, ItemDim3Code
    ) AS SourceData
    GROUP BY CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
             ItemTypeCode, ItemCode, ColorCode, ItemDim1Code, ItemDim2Code, ItemDim3Code
) AS Inventory
    ON cdItem.ItemTypeCode = Inventory.ItemTypeCode
   AND cdItem.ItemCode     = Inventory.ItemCode
LEFT JOIN ProductAttributesFilter 
    ON ProductAttributesFilter.ItemCode = Inventory.ItemCode
WHERE 
    Inventory.ItemTypeCode IN (1)
    AND Inventory.WarehouseCode IN ('1-0-12','1-0-21','1-0-10','1-0-2','1-1-3','1-2-4','1-2-5')
    AND cdItem.IsBlocked = 0
    AND Inventory.ItemCode = @ProductCode
    AND (
        -- 🔹 Eğer renk girilmişse o renk; boşsa renksiz stokları getir
        (LTRIM(RTRIM(@ColorCode)) <> '' AND Inventory.ColorCode = @ColorCode)
        OR (LTRIM(RTRIM(@ColorCode)) = '' AND (Inventory.ColorCode IS NULL OR LTRIM(RTRIM(Inventory.ColorCode)) = ''))
    )
    AND (
        -- 🔹 2. renk sadece doluysa filtrelensin
        @ColorCode2 IS NULL
        OR Inventory.ItemDim2Code = @ColorCode2
    )
GROUP BY
    Inventory.ItemCode,
    Inventory.ColorCode,
    ISNULL(Inventory.ItemDim1Code, ''), -- ✅ NULL bedenleri boş string olarak gruplar
    ISNULL(Inventory.ItemDim2Code, ''), -- ✅ NULL yakaları boş string olarak gruplar
    cdUnitOfMeasure.RoundDigit;
`
