CREATE FUNCTION [dbo].[SearchKala](@Price money, @SazmanCode varchar(3), @Acode varchar(3) , @iSearch Int) 
RETURNS 
   @Search_Kala TABLE
   (
    K_Code        varchar(15),
    Name1         nvarchar(100),
    Name2         nvarchar(80),
    A_Code        varchar(3),
    Mojodi        Float,
    Exp_Flag      Bit,
    Serial_flag   Bit,
    Point_Flag    Bit,
    Make_Flag     Bit,
    Hamrah_Flag   Bit,
    CtrMojodi     Bit,
    Barcode       varchar(30),
    Price_Kharid  money,
    Price_Forosh  money,
    Darou_Flag    Char(1),
    Price         money,
    Meli_Code     varchar(10),
    Org_Code     varchar(6),
    Brand_code   VARCHAR(6),
    Saghf_Forosh  Real,
    BimarPercent  Real,
    Parvande_Flag BIT,
	Duty_Flag CHAR(1)     
   )
 AS  
BEGIN 
  IF @iSearch = 0 
  Begin
    INSERT @Search_Kala
    Select K_Code, Name1, Name2, A_Code, Mojodi, Exp_Flag,Serial_flag, Point_Flag, Make_Flag, 
    Hamrah_Flag, CtrMojodi,Barcode, Price_Kharid, Price_Forosh, Darou_flag, Price,Meli_Code, Org_Code, Brand_code,Saghf_Forosh,BimarPercent,Parvande_Flag, Duty_Flag From 
    (SELECT KalaId.K_Code, KalaId.Name1, KalaId.Name2, Anbar.A_Code, Anbar.Mojodi, 
    KalaId.Exp_Flag, KalaId.Serial_flag, KalaId.Point_Flag, KalaId.Make_Flag, KalaId.Hamrah_Flag, 
    KalaId.CtrMojodi, KalaId.Barcode, Anbar.Price_Kharid, Anbar.Price_Forosh,Anbar.Saghf_Forosh,
    (select IsNull(Bimarpercent,0) From Price_Sazman Where Sazman_Code = @SazmanCode And Price_Sazman.K_Code = KalaId.K_Code) BimarPercent,Parvande_Flag,Duty_Flag,
    Case 
      when KalaId.Darou_Flag Not In ('6','9','4') then Kalaid.Darou_Flag 
      when KalaId.Darou_Flag In ('6','9','4')  then (select IsNull(Price_Sazman.Status,0) From Price_Sazman Where Sazman_Code = @SazmanCode And Price_Sazman.K_Code= KalaId.K_Code)
    End As Darou_Flag,
    Case 
      When KalaId.Darou_Flag Not In ('6','9','4') then 0 
      when KalaId.Darou_Flag In ('6','9','4')  then (select IsNull(Price_Sazman.Price,0) From Price_Sazman Where Sazman_Code = @SazmanCode  And Status In ('6','9','4') And Price_Sazman.K_Code= KalaId.K_Code)
    End As Price,
    Kalaid.Meli_Code, Org_Code,Brand_code 
    FROM KalaId INNER JOIN Anbar ON KalaId.K_Code = Anbar.K_Code 
    WHERE Anbar.Active_Flag = '0' AND Anbar.A_Code = @ACode ) PublicTable
    Where Price_Forosh = @price
    Order By Name1
  End
  Else if @iSearch = 1 
  Begin
    INSERT @Search_Kala
    SELECT K_Code, Name1, Name2, A_Code, Mojodi, Exp_Flag, 
    Serial_flag, Point_Flag, Make_Flag, Hamrah_Flag, CtrMojodi, 
    Barcode, Price_Kharid, Price_Forosh, Darou_Flag, Price, Meli_Code, 
	Org_Code, Brand_code,Saghf_Forosh,Bimarpercent,Parvande_Flag, Duty_Flag
	 From 
    (SELECT KalaId.K_Code, KalaId.Name1, KalaId.Name2, Anbar.A_Code, Anbar.Mojodi, KalaId.Exp_Flag, 
    KalaId.Serial_flag, KalaId.Point_Flag, KalaId.Make_Flag, KalaId.Hamrah_Flag, KalaId.CtrMojodi, 
    (select IsNull(Bimarpercent,0) From Price_Sazman Where Sazman_Code = @SazmanCode And Price_Sazman.K_Code = KalaId.K_Code) BimarPercent,Parvande_Flag,Duty_Flag,
    KalaId.Barcode, Anbar.Price_Kharid, Anbar.Price_Forosh, Anbar.Saghf_Forosh, KalaId.Darou_Flag,Cast(0 as Money) Price, 
    Kalaid.Meli_Code,Org_Code,Brand_code FROM KalaId INNER JOIN Anbar ON KalaId.K_Code = Anbar.K_Code 
    WHERE (Anbar.Active_Flag = '0') AND (Anbar.A_Code = @ACode) AND (KalaId.Darou_Flag = '1')) 
    TableKala Where Price_Forosh = @price 
    Order By Name1
  End
  Else if @iSearch = 2 
  Begin
    INSERT @Search_Kala
    Select K_Code, Name1, Name2, A_Code, Mojodi, Exp_Flag,Serial_flag, Point_Flag, Make_Flag, 
    Hamrah_Flag, CtrMojodi,Barcode, Price_Kharid, Price_Forosh, Darou_flag, Price,Meli_Code, Org_Code,Brand_code, Saghf_Forosh,BimarPercent,Parvande_Flag, Duty_Flag From 
    (SELECT KalaId.K_Code, KalaId.Name1, KalaId.Name2, Anbar.A_Code, Anbar.Mojodi, 
    KalaId.Exp_Flag, KalaId.Serial_flag, KalaId.Point_Flag, KalaId.Make_Flag, KalaId.Hamrah_Flag, 
    KalaId.CtrMojodi, KalaId.Barcode, Anbar.Price_Kharid, Anbar.Price_Forosh,Anbar.Saghf_Forosh, 
    (select IsNull(Bimarpercent,0) From Price_Sazman Where Sazman_Code = @SazmanCode And Price_Sazman.K_Code = KalaId.K_Code) BimarPercent,Parvande_Flag,Duty_Flag,
    Case 
      when KalaId.Darou_Flag Not In ('6','9','4') then Kalaid.Darou_Flag 
      when KalaId.Darou_Flag In ('6','9','4')  then (select IsNull(Price_Sazman.Status,0) From Price_Sazman Where Sazman_Code = @SazmanCode And Price_Sazman.K_Code= KalaId.K_Code)
    End As Darou_Flag,
    Case 
      When KalaId.Darou_Flag Not In ('6','9','4') then 0 
      when KalaId.Darou_Flag In ('6','9','4')  then (select IsNull(Price_Sazman.Price,0) From Price_Sazman Where Sazman_Code = @SazmanCode  And Status In ('6','9','4') And Price_Sazman.K_Code= KalaId.K_Code)
    End As Price,
    Kalaid.Meli_Code,Org_Code,Brand_code 
    FROM KalaId INNER JOIN Anbar ON KalaId.K_Code = Anbar.K_Code 
    WHERE Anbar.Active_Flag = '0' AND Anbar.A_Code = @ACode ) PublicTable
    Where Price_Forosh = @price And Darou_Flag <> '1'
    Order By Name1
  End
  RETURN
END


