CREATE PROCEDURE [dbo].[GetExpirationDate]
    @InputOriginDate VARCHAR(10),
    @WarehouseCodes VARCHAR(MAX),
    @Less1M BIT = 0,
    @Less3M BIT = 0,
    @Less6M BIT = 0,
    @Less1y BIT = 0,
    @Bigger1y BIT = 0
AS
DECLARE @OriginDate DATE = dbo.ShamsiToMiladi(@InputOriginDate);
DECLARE @WarehouseList TABLE
(
    Code VARCHAR(5),
    Name NVARCHAR(MAX)
);
INSERT INTO @WarehouseList
SELECT splitdata,
       W.Name
FROM fnSplitString(@WarehouseCodes, ',') WL
    JOIN dbo.AnbarId W
        ON W.A_Code = WL.splitdata;

SELECT W.A_Code WearhouseCode,
       G.K_Code Code,
       G.Barcode EquvalentCode,
       G.Name1 [Name],
       G.Name2 GenericName,
       W.Mojodi Stock,
       W.Price_Kharid BuyPrice,
       W.Price_Forosh SalesPrice,
       W.Price_Omdeh WholePrice,
       E.K_Qty ExpirationStock,
       dbo.MiladiToShamsi(E.Exp_Date) ExpirationShamsiDate,
       CAST(E.Exp_Date AS DATE) ExpirationDate,
       -1 * DATEDIFF(DAY, E.Exp_Date, @OriginDate) DayDiff,
       DateNumber = CASE
                        WHEN (E.Exp_Date < CAST(DATEADD(MONTH, 1, @OriginDate) AS DATE))
                             AND (@Less1M = 1) THEN
                            1
                        WHEN (E.Exp_Date
                             BETWEEN @OriginDate AND CAST(DATEADD(MONTH, 3, @OriginDate) AS DATE)
                             )
                             AND (@Less3M = 1) THEN
                            3
                        WHEN (E.Exp_Date
                             BETWEEN @OriginDate AND CAST(DATEADD(MONTH, 6, @OriginDate) AS DATE)
                             )
                             AND (@Less6M = 1) THEN
                            6
                        WHEN (E.Exp_Date
                             BETWEEN @OriginDate AND CAST(DATEADD(YEAR, 1, @OriginDate) AS DATE)
                             )
                             AND (@Less1y = 1) THEN
                            12
                        WHEN
                        (
                            E.Exp_Date > CAST(DATEADD(YEAR, 1, @OriginDate) AS DATE)
                            AND (@Bigger1y = 1)
                        ) THEN
                            DATEDIFF(MONTH, E.Exp_Date, @OriginDate)
                    END
FROM dbo.KalaId G
    JOIN dbo.Anbar W
        ON W.K_Code = G.K_Code
           AND W.A_Code IN
               (
                   SELECT Code FROM @WarehouseList
               )
    JOIN dbo.Exp_Date E
        ON E.K_Code = G.K_Code
           AND E.A_Code = W.A_Code
WHERE (
          @Less1M = 1
          AND E.Exp_Date < CAST(DATEADD(MONTH, 1, @OriginDate) AS DATE)
      )
      OR
      (
          @Less3M = 1
          AND E.Exp_Date
      BETWEEN @OriginDate AND CAST(DATEADD(MONTH, 3, @OriginDate) AS DATE)
      )
      OR
      (
          @Less6M = 1
          AND E.Exp_Date
      BETWEEN @OriginDate AND CAST(DATEADD(MONTH, 6, @OriginDate) AS DATE)
      )
      OR
      (
          @Less1y = 1
          AND E.Exp_Date
      BETWEEN @OriginDate AND CAST(DATEADD(YEAR, 1, @OriginDate) AS DATE)
      )
      OR
      (
          @Bigger1y = 1
          AND (E.Exp_Date > CAST(DATEADD(YEAR, 1, @OriginDate) AS DATE))
      )
ORDER BY E.Exp_Date DESC;