Create PROCEDURE [dbo].[spUpdatableGood]
    @WearhouseCode VARCHAR(10),
    @ComputerName NVARCHAR(MAX),
    @CodeType SMALLINT,
    @IsOrgType BIT,
    @ContainAllOrg BIT,
    @OrgCode VARCHAR(MAX),
    @UpdateType SMALLINT,
    @JustSelect BIT
AS
IF @JustSelect = 0
BEGIN
    DECLARE @cmd NVARCHAR(MAX),
            @field NVARCHAR(MAX),
            @join NVARCHAR(MAX),
            @orderBy NVARCHAR(MAX);

    IF @CodeType = 2
    BEGIN
        SET @join = ' Inner join KalaId ON KalaId.K_Code COLLATE  Arabic_CI_AS = tmp.K_Code ';
        SET @orderBy = ' Order By tmp.K_Code  ';
    END;
    IF @CodeType = 1
    BEGIN
        SET @join = ' Inner join KalaId ON KalaId.Brand_Code COLLATE  Arabic_CI_AS = tmp.Brand_Code ';
        SET @orderBy = ' Order By tmp.Brand_Code  ';
    END;

    IF @CodeType = 3
    BEGIN
        SET @join = ' Inner join KalaId ON KalaId.Org_Code COLLATE  Arabic_CI_AS = tmp.Org_Code ';
        SET @orderBy = ' Order By tmp.Org_Code  ';
    END;
    IF @CodeType = 0
    BEGIN
        SET @join = ' Inner join KalaId ON KalaId.Meli_Code COLLATE  Arabic_CI_AS = tmp.M_code ';
        SET @orderBy = ' Order By tmp.M_code ';
    END;

    IF @IsOrgType = 1
    BEGIN
        IF @ContainAllOrg = 1
            SET @cmd = ' KalaId.Darou_Flag = ''6'' AND PO.Sazman_Code = ' + @OrgCode;
        ELSE
            SET @cmd = ' KalaId.Darou_Flag = ''6'' AND PO.Sazman_Code ' + @OrgCode;

        SET @field = ' , PO.Price AS sa_Price, PO.Status ,ISNULL(PO.[BimarPercent],0) CurPercent , PO.Sazman_Code';
        SET @join
            = @join
              + ' INNER JOIN  Price_Sazman PO ON KalaId.K_Code = PO.K_Code AND PO.Status NOT in (0 , 1 , 2 , 3 ) AND '
              + @cmd;
    END;
    ELSE
        SET @field
            = ' , Anbar.Price_Kharid AS sa_Price, Darou_Flag AS Status , 0 as CurPercent , ''01'' as Sazman_Code ';

    SET @cmd
        = 'SELECT DISTINCT ' + 'tmp.Name ,' + 'tmp.M_Code ,' + 'tmp.Price ,' + 'KalaId.Name1 ,' + 'KalaId.K_Code ,'
          + 'Anbar.Price_Forosh AS Old_Price ,' + 'KalaId.Darou_Flag ' + @field + ' , ' + 'tmp.Update_Status ,'
          + 'tmp.K_Code tmpKCode ,' + 'st_Name = CASE WHEN tmp.Update_Status = 1 THEN N''اعمال شود'''
          + 'ELSE N''اعمال نشود''' + 'END ,' + 'tmp.Computer_Name ,' + 'tmp.Condition ,' + 'tmp.[Percent] ,'
          + 'tmp.Brand_Code ,' + 'tmp.Org_Code, '+ ' 0 isReduce ' 		  
		  + 'INTO  ##tmpView ' + 'FROM  ##tmpExcel tmp  ' + @join
          + 'Inner Join Anbar  ON Anbar.K_Code     = KalaId.K_Code ' + 'WHERE tmp.Computer_Name = ' + @ComputerName
          + ' AND Anbar.A_Code = ' + @WearhouseCode + '';

    IF @UpdateType = 2
        SET @cmd = @cmd + ' AND Anbar.Price_Forosh <> tmp.Price AND KalaId.Darou_Flag <> ''6'' ';
    ELSE IF @UpdateType = 1
        SET @cmd = @cmd + ' AND Anbar.Price_Forosh = tmp.Price AND KalaId.Darou_Flag <> ''6'' ';
    ELSE IF @UpdateType = 3
    BEGIN
        SET @cmd
            = @cmd
              + ' AND ( ((PO.Status = ''6'' AND tmp.Price > PO.Price ) OR (PO.Status not in ( ''4'' , ''6'' , ''9'' ) AND tmp.Price > Anbar.Price_Forosh))'
              + ' OR  (PO.Status in (''4'' , ''9'') AND (( PO.Price > 0 AND tmp.Price > PO.Price ) OR (PO.Price = 0 AND tmp.Price > Anbar.Price_Forosh ))) '
              + ' OR ((PO.Status = ''6'' AND tmp.Price < PO.Price ) OR (PO.Status not in (''4'' , ''6'' , ''9'' ) AND tmp.Price < Anbar.Price_Forosh))'
              + ' OR (PO.Status in (''4'' , ''9'') AND ( ( PO.Price > 0 AND tmp.Price < PO.Price ) OR (PO.Price = 0 AND tmp.Price < Anbar.Price_Forosh))) )'
              + ' AND NOT( PO.Status = ''6'' AND Anbar.Price_Forosh = tmp.Price AND ISNULL(PO.BimarPercent , 0) > 0 AND PO.Price = 0 ) ';
    END;

    SET @cmd = @cmd + @orderBy;
    PRINT @cmd;
    EXEC sp_executesql @cmd;

UPDATE ##tmpView
SET isReduce = CASE
                   WHEN
                   (
                       Price < Old_Price
                       AND (
                               Price < sa_Price
                               OR (
                                      sa_Price > 0
                                      OR Status IN ( 4, 5, 9 )
                                  )
                           )
                   ) THEN
                       1
                   ELSE
                       0
               END;
END;
SELECT [Name],
       M_Code,
       Price,
       Name1,
       K_Code,
       Old_Price,
       Darou_Flag,
       sa_Price,
       Status,
       Update_Status,
       st_Name,
       Condition,
       Computer_name,
       CAST([Percent] AS REAL) [Percent],
       CAST(CurPercent AS REAL) curPercent,
       Status_Name = CASE
                         WHEN Darou_Flag <> 6 THEN
                             dbo.Find_Status(Darou_Flag)
                         ELSE
                             dbo.Find_Status(Status)
                     END,
       Brand_Code,
       Org_Code,
       Sazman_Code,
       isReduce

FROM ##tmpView;