package queries

// GetProductStockQuery:
// Urun kodu bazli, STOCK/PICK/RESERVE/DISP ayrik CTE ile optimize sorgu.
const GetProductStockQuery = `
DECLARE @ProductCode NVARCHAR(50) = UPPER(LTRIM(RTRIM(@p1)));

;WITH STOCK AS
(
    SELECT
        CompanyCode,
        OfficeCode,
        StoreTypeCode,
        StoreCode,
        WarehouseCode,
        ItemTypeCode,
        UPPER(LTRIM(RTRIM(ItemCode))) AS ItemCode,
        ColorCode,
        ItemDim1Code,
        ItemDim2Code,
        ItemDim3Code,
        SUM(In_Qty1 - Out_Qty1) AS InventoryQty1
    FROM trStock
    WHERE ItemTypeCode = 1
      AND UPPER(LTRIM(RTRIM(ItemCode))) = @ProductCode
      AND LEN(LTRIM(RTRIM(ItemCode))) = 13
      AND LEN(@ProductCode) = 13
    GROUP BY
        CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
        ItemTypeCode, UPPER(LTRIM(RTRIM(ItemCode))), ColorCode,
        ItemDim1Code, ItemDim2Code, ItemDim3Code
),

PICK AS
(
    SELECT
        CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
        ItemTypeCode, UPPER(LTRIM(RTRIM(ItemCode))) AS ItemCode, ColorCode,
        ItemDim1Code, ItemDim2Code, ItemDim3Code,
        SUM(Qty1) AS PickingQty1
    FROM PickingStates
    WHERE ItemTypeCode = 1
      AND UPPER(LTRIM(RTRIM(ItemCode))) = @ProductCode
      AND LEN(LTRIM(RTRIM(ItemCode))) = 13
      AND LEN(@ProductCode) = 13
    GROUP BY
        CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
        ItemTypeCode, UPPER(LTRIM(RTRIM(ItemCode))), ColorCode,
        ItemDim1Code, ItemDim2Code, ItemDim3Code
),

RESERVE AS
(
    SELECT
        CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
        ItemTypeCode, UPPER(LTRIM(RTRIM(ItemCode))) AS ItemCode, ColorCode,
        ItemDim1Code, ItemDim2Code, ItemDim3Code,
        SUM(Qty1) AS ReserveQty1
    FROM ReserveStates
    WHERE ItemTypeCode = 1
      AND UPPER(LTRIM(RTRIM(ItemCode))) = @ProductCode
      AND LEN(LTRIM(RTRIM(ItemCode))) = 13
      AND LEN(@ProductCode) = 13
    GROUP BY
        CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
        ItemTypeCode, UPPER(LTRIM(RTRIM(ItemCode))), ColorCode,
        ItemDim1Code, ItemDim2Code, ItemDim3Code
),

DISP AS
(
    SELECT
        CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
        ItemTypeCode, UPPER(LTRIM(RTRIM(ItemCode))) AS ItemCode, ColorCode,
        ItemDim1Code, ItemDim2Code, ItemDim3Code,
        SUM(Qty1) AS DispOrderQty1
    FROM DispOrderStates
    WHERE ItemTypeCode = 1
      AND UPPER(LTRIM(RTRIM(ItemCode))) = @ProductCode
      AND LEN(LTRIM(RTRIM(ItemCode))) = 13
      AND LEN(@ProductCode) = 13
    GROUP BY
        CompanyCode, OfficeCode, StoreTypeCode, StoreCode, WarehouseCode,
        ItemTypeCode, UPPER(LTRIM(RTRIM(ItemCode))), ColorCode,
        ItemDim1Code, ItemDim2Code, ItemDim3Code
)

SELECT
    S.WarehouseCode AS Depo_Kodu,
    W.WarehouseDescription AS Depo_Adi,

    bsItemTypeDesc.ItemTypeDescription AS InventoryType,

    S.ItemCode AS Urun_Kodu,
    P.ProductDescription AS Madde_Aciklamasi,

    S.ColorCode AS Renk_Kodu,
    C.ColorDescription AS Renk_Aciklamasi,

    S.ItemDim1Code AS Beden,
    S.ItemDim3Code AS Renk2,
    S.ItemDim2Code AS Yaka,

    ROUND(
        S.InventoryQty1
        - ISNULL(PK.PickingQty1,0)
        - ISNULL(RS.ReserveQty1,0)
        - ISNULL(DP.DispOrderQty1,0),
        cdUnitOfMeasure.RoundDigit
    ) AS Kullanilabilir_Envanter,

    P.ProductAtt01Desc AS URUN_ANA_GRUBU,
    P.ProductAtt02Desc AS URUN_ALT_GRUBU,
    P.ProductAtt10Desc AS MARKA,
    P.ProductAtt11Desc AS DR,
    P.ProductAtt21Desc AS KALIP,
    P.ProductAtt22Desc AS IKINCI_PARCA_KALIP,
    P.ProductAtt23Desc AS PACA_GENISLIGI,
    P.ProductAtt24Desc AS UCUNCU_PARCA_KALIP,
    P.ProductAtt25Desc AS UCUNCU_PARCA_MODEL,
    P.ProductAtt26Desc AS BIRINCI_PARCA_KUMAS,
    P.ProductAtt27Desc AS IKINCI_PARCA_KUMAS,
    P.ProductAtt28Desc AS UCUNCU_PARCA_KUMAS,
    P.ProductAtt29Desc AS BIRINCI_PARCA_KARISIM,
    P.ProductAtt30Desc AS IKINCI_PARCA_KARISIM,
    P.ProductAtt31Desc AS UCUNCU_PARCA_KARISIM,
    P.ProductAtt32Desc AS YAKA_TIPI,
    P.ProductAtt33Desc AS DUGME,
    P.ProductAtt34Desc AS YIRTMAC,
    P.ProductAtt35Desc AS SEZON_YILI,
    P.ProductAtt36Desc AS MEVSIM,
    P.ProductAtt37Desc AS TABAN,
    P.ProductAtt38Desc AS BIRINCI_PARCA_FIT,
    P.ProductAtt39Desc AS IKINCI_PARCA_FIT,
    P.ProductAtt40Desc AS BOS2,
    P.ProductAtt41Desc AS URUN_ICERIGI,
    P.ProductAtt41Desc AS KISA_KAR,
    P.ProductAtt42Desc AS SERI_FASON,
    P.ProductAtt43Desc AS STOK_GIRIS_YONTEMI,
    P.ProductAtt44Desc AS YETISKIN_GARSON,
    P.ProductAtt45Desc AS ASKILI_YAN,
    P.ProductAtt46Desc AS BOS3,

    prFilteredBasePrice.Price AS Fiyat

FROM STOCK S

LEFT JOIN PICK PK
ON PK.CompanyCode=S.CompanyCode
AND PK.OfficeCode=S.OfficeCode
AND PK.StoreTypeCode=S.StoreTypeCode
AND PK.StoreCode=S.StoreCode
AND PK.WarehouseCode=S.WarehouseCode
AND PK.ItemTypeCode=S.ItemTypeCode
AND PK.ItemCode=S.ItemCode
AND PK.ColorCode=S.ColorCode
AND PK.ItemDim1Code=S.ItemDim1Code
AND PK.ItemDim2Code=S.ItemDim2Code
AND PK.ItemDim3Code=S.ItemDim3Code

LEFT JOIN RESERVE RS
ON RS.CompanyCode=S.CompanyCode
AND RS.OfficeCode=S.OfficeCode
AND RS.StoreTypeCode=S.StoreTypeCode
AND RS.StoreCode=S.StoreCode
AND RS.WarehouseCode=S.WarehouseCode
AND RS.ItemTypeCode=S.ItemTypeCode
AND RS.ItemCode=S.ItemCode
AND RS.ColorCode=S.ColorCode
AND RS.ItemDim1Code=S.ItemDim1Code
AND RS.ItemDim2Code=S.ItemDim2Code
AND RS.ItemDim3Code=S.ItemDim3Code

LEFT JOIN DISP DP
ON DP.CompanyCode=S.CompanyCode
AND DP.OfficeCode=S.OfficeCode
AND DP.StoreTypeCode=S.StoreTypeCode
AND DP.StoreCode=S.StoreCode
AND DP.WarehouseCode=S.WarehouseCode
AND DP.ItemTypeCode=S.ItemTypeCode
AND DP.ItemCode=S.ItemCode
AND DP.ColorCode=S.ColorCode
AND DP.ItemDim1Code=S.ItemDim1Code
AND DP.ItemDim2Code=S.ItemDim2Code
AND DP.ItemDim3Code=S.ItemDim3Code

JOIN cdItem
ON UPPER(LTRIM(RTRIM(S.ItemCode))) = UPPER(LTRIM(RTRIM(cdItem.ItemCode)))
AND S.ItemTypeCode = cdItem.ItemTypeCode

LEFT JOIN cdUnitOfMeasure
ON cdItem.UnitOfMeasureCode1 = cdUnitOfMeasure.UnitOfMeasureCode

LEFT JOIN ProductFilterWithDescription('TR') P
ON UPPER(LTRIM(RTRIM(P.ProductCode))) = UPPER(LTRIM(RTRIM(S.ItemCode)))

LEFT JOIN bsItemTypeDesc
ON bsItemTypeDesc.ItemTypeCode = S.ItemTypeCode
AND bsItemTypeDesc.LangCode='TR'

LEFT JOIN cdWarehouseDesc W
ON W.WarehouseCode = S.WarehouseCode
AND W.LangCode='TR'

LEFT JOIN cdColorDesc C
ON C.ColorCode = S.ColorCode
AND C.LangCode='TR'

LEFT JOIN (
    SELECT
        ItemCode,
        ItemTypeCode,
        Price,
        ROW_NUMBER() OVER (PARTITION BY ItemCode, ItemTypeCode ORDER BY PriceDate DESC) AS RowNum
    FROM prItemBasePrice
) prFilteredBasePrice
ON UPPER(LTRIM(RTRIM(prFilteredBasePrice.ItemCode))) = UPPER(LTRIM(RTRIM(S.ItemCode)))
AND prFilteredBasePrice.ItemTypeCode = S.ItemTypeCode
AND prFilteredBasePrice.RowNum = 1

WHERE
    S.ItemTypeCode IN (1)
    AND UPPER(LTRIM(RTRIM(S.ItemCode))) = @ProductCode
    AND LEN(LTRIM(RTRIM(S.ItemCode))) = 13
    AND LEN(@ProductCode) = 13
    AND (
        S.InventoryQty1
        - ISNULL(PK.PickingQty1,0)
        - ISNULL(RS.ReserveQty1,0)
        - ISNULL(DP.DispOrderQty1,0)
    ) > 0
    AND cdItem.IsBlocked = 0
    AND S.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'
    );
`
