CREATE  Procedure [dbo].[IHIOSearchDrug]
    @Type TINYINT,
    @Expression NVARCHAR(MAX) = NULL,
    @InsurerCode VARCHAR(3),
    @WarehouseCode VARCHAR(3)
AS
SELECT TOP (300)
       G.K_Code GoodsCode,
       G.Name1 PersianName,
       G.Name2 GenericName,
       ISNULL(P.Price, 0) Price,
       ISNULL(W.Price_Forosh, 0) WarehousePrice,
       G.Barcode IRCCode,
       GenericCode = CASE
                         WHEN ISNULL(EIC.Code, '') = '' THEN
                             G.Meli_Code
                         ELSE
                             EIC.Code
                     END,
       [StatusCode] = CAST(ISNULL(   CASE
                                         WHEN G.Darou_Flag = 6 THEN
                                             P.[Status]
                                         ELSE
                                             G.Darou_Flag
                                     END,
                                     0
                                 ) AS SMALLINT),
       ISNULL(W.Mojodi, 0) [Stock],
       SalesCeiling = CASE
                          WHEN ISNULL(EIC.SalesCeiling, 0) = 0 THEN
                              CAST(ISNULL(W.Saghf_Forosh, 0) AS INT)
                          ELSE
                              EIC.SalesCeiling
                      END,
       InsurerPart = CAST(CASE
                              WHEN (G.Darou_Flag = 6)
                                   AND (P.[Status] = 6)
                                   AND (ISNULL(P.BimarPercent, 0) > 0) THEN
                                  100 - P.BimarPercent
                              ELSE
                                  0
                          END AS INT)
FROM dbo.KalaId G WITH (NOLOCK)
    JOIN dbo.Anbar W WITH (NOLOCK)
        ON W.K_Code = G.K_Code
           AND W.A_Code = @WarehouseCode
    LEFT JOIN dbo.ExclusiveInsuranceCode EIC WITH (NOLOCK)
        ON EIC.GoodsCode = G.K_Code
           AND EIC.InsuranceCode = @InsurerCode
    LEFT JOIN dbo.Price_Sazman P WITH (NOLOCK)
        ON P.K_Code = G.K_Code
           AND P.Sazman_Code = @InsurerCode
WHERE (
          (
              @Type = 8
              AND
              (
                  G.K_Code LIKE '%' + @Expression + '%'
                  OR G.Barcode LIKE '%' + @Expression + '%'
                  OR
                  (
                      ISNULL(EIC.Code, '') = ''
                      AND G.Meli_Code LIKE '%' + @Expression + '%'
                  )
                  OR
                  (
                      ISNULL(EIC.Code, '') <> ''
                      AND EIC.Code LIKE '%' + @Expression + '%'
                  )
                  OR G.Name1 LIKE '%' + @Expression + '%'
                  OR W.Price_Forosh LIKE '%' + @Expression + '%'
                  OR G.Name2 LIKE '%' + @Expression + '%'
              )
          )
          OR
          (
              @Type = 9
              AND
              (
                  G.K_Code = @Expression
                  OR G.Barcode = @Expression
              )
          )
          OR
          (
              @Type = 0
              AND
              (
                  (
                      ISNULL(EIC.Code, '') = ''
                      AND G.Meli_Code = @Expression
                  )
                  OR
                  (
                      ISNULL(EIC.Code, '') <> ''
                      AND EIC.Code = @Expression
                  )
              )
          )
      )
      AND W.Active_Flag = 0
ORDER BY CASE
             WHEN @Type = 8 THEN
                 G.Name1
             WHEN @Type = 9 THEN
                 G.K_Code
         END;