package queries

// 🔹 Ürüne göre stok detay sorgusu
const GetInventoryProduct = `
-- 🔹 Ürüne göre stok detay sorgusu (Nebim V3 uyumlu, parametre güvenli)
SELECT
    bsItemTypeDesc.ItemTypeDescription      AS InventoryType,
    inv.WarehouseCode                       AS Depo_Kodu,
    wh.WarehouseDescription                 AS Depo_Adi,
    inv.ItemCode                            AS Urun_Kodu,
    ISNULL(descItem.ItemDescription, '')    AS Madde_Aciklamasi,
    inv.ColorCode                           AS Renk_Kodu,
    ISNULL(descColor.ColorDescription, '')  AS Renk_Aciklamasi,
    inv.ItemDim1Code                        AS Beden,
    attr.ProductAtt01                       AS Urun_Grubu,
    attr.ProductAtt02                       AS Urun_Alt_Grubu,
    attr.ProductAtt41                       AS Kisa_Karisim,
    attr.ProductAtt42                       AS SERI,
    attr.ProductAtt43                       AS FASON_ISCLIK,
    attr.ProductAtt45                       AS ASKILI_YAN,
    inv.ItemDim2Code                        AS YAKA,
    attr.ProductAtt44                       AS GARSON_YETISKIN,
    attr.ProductAtt10                       AS MarkaKodu,
    ROUND(
        SUM(inv.InventoryQty1)
        - (SUM(inv.ReserveQty1) + SUM(inv.DispOrderQty1) + SUM(inv.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 src
    GROUP BY CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
             ItemTypeCode, ItemCode, ColorCode, ItemDim1Code, ItemDim2Code, ItemDim3Code
) AS inv
    ON cdItem.ItemTypeCode = inv.ItemTypeCode
   AND cdItem.ItemCode     = inv.ItemCode
LEFT JOIN ProductAttributesFilter AS attr
    ON attr.ItemCode = inv.ItemCode
LEFT JOIN bsItemTypeDesc
    ON bsItemTypeDesc.ItemTypeCode = inv.ItemTypeCode
   AND bsItemTypeDesc.LangCode = 'TR'
LEFT JOIN cdWarehouseDesc AS wh
    ON wh.WarehouseCode = inv.WarehouseCode
LEFT JOIN cdItemDesc AS descItem
    ON descItem.ItemTypeCode = inv.ItemTypeCode
   AND descItem.ItemCode = inv.ItemCode
   AND descItem.LangCode = 'TR'
LEFT JOIN cdColorDesc AS descColor
    ON descColor.ColorCode = inv.ColorCode
   AND descColor.LangCode = 'TR'
WHERE 
    inv.ItemTypeCode IN (1)
    AND inv.WarehouseCode IN ('1-0-12','1-0-21','1-0-10','1-0-2','1-1-3','1-2-4','1-2-5','')
    AND inv.InventoryQty1 >= 0
    AND cdItem.IsBlocked = 0
    AND inv.ItemCode = @p1  -- ✅ doğrudan parametre, DECLARE yok
GROUP BY
    inv.CompanyCode, inv.OfficeCode, inv.StoreTypeCode, inv.StoreCode,
    inv.WarehouseCode, inv.ItemTypeCode, inv.ItemCode, inv.ColorCode,
    inv.ItemDim1Code, inv.ItemDim2Code, inv.ItemDim3Code,
    cdUnitOfMeasure.RoundDigit, attr.ProductAtt01, attr.ProductAtt02,
    attr.ProductAtt41, attr.ProductAtt42, attr.ProductAtt43, attr.ProductAtt44,
    attr.ProductAtt45, attr.ProductAtt10, bsItemTypeDesc.ItemTypeDescription,
    wh.WarehouseDescription, descItem.ItemDescription, descColor.ColorDescription;

`
