CREATE PROC [dbo].[UpdateDrugPercent]
    @ComputerName AS NVARCHAR(MAX),
    @IsAdvanced BIT = 0
AS
PRINT @ComputerName;

UPDATE dbo.Price_Sazman
SET BimarPercent = tmp.[Percent],
    Status = CASE
                 WHEN tmp.Status = 9 THEN
                     9
                 ELSE
                     6
             END,
    Price = CASE
                WHEN tmp.Status = 5 THEN
                    tmp.old_price
                ELSE
                    PO.Price
            END
FROM dbo.Price_Sazman PO
    JOIN ##tmpView tmp
        ON tmp.K_Code COLLATE Arabic_CI_AS = PO.K_Code
           AND tmp.Sazman_Code COLLATE Arabic_CI_AS = PO.Sazman_Code
WHERE Computer_Name = @ComputerName
      AND
      (
          (
              @IsAdvanced = 0
              AND tmp.UpdateStatus = 1
          )
          OR
          (
              @IsAdvanced = 1
              AND tmp.UpdateStatus = 0
          )
      )
      AND tmp.[PERCENT] >= 0;
-------------------------------- Cast to Free Price
UPDATE dbo.Price_Sazman
SET BimarPercent = 0,
    Status = 4,
    Price = CASE
                WHEN tmp.Status = 5 THEN
                    tmp.old_price
                ELSE
                    PO.Price
            END
FROM dbo.Price_Sazman PO
    JOIN ##tmpView tmp
        ON tmp.K_Code COLLATE Arabic_CI_AS = PO.K_Code
           AND tmp.Sazman_Code COLLATE Arabic_CI_AS = PO.Sazman_Code
WHERE tmp.Computer_Name = @ComputerName
      AND
      (
          (
              @IsAdvanced = 0
              AND tmp.UpdateStatus = 1
          )
          OR
          (
              @IsAdvanced = 1
              AND tmp.UpdateStatus = 0
          )
      )
      AND tmp.[PERCENT] = -100;