CREATE PROC [dbo].[UpdatePriceNonInsurance]
    @ComputerName NVARCHAR(30),
    @OrgCode VARCHAR(MAX)
AS
--------------------------------Update Darou Flag Kalaid Table
UPDATE dbo.KalaId
SET Darou_Flag = 6
FROM dbo.KalaId G
    JOIN ##tmpView tmp
        ON tmp.K_Code COLLATE Arabic_CI_AS = G.K_Code
WHERE G.Darou_Flag = 0
      AND tmp.UpdateStatus = 1
      AND tmp.Computer_Name = @ComputerName;
---------------------------------Insert Into  Price Sazman Table
INSERT INTO dbo.Price_Sazman
(
    K_Code,
    Sazman_Code,
    Price,
    Status,
    BimarPercent
)
SELECT tmp.K_Code,
       Insurance.Code,
       CASE
           WHEN tmp.Price <= tmp.Old_Price THEN
               tmp.Price
           ELSE
               tmp.Old_Price
       END,
       CASE WHEN  tmp.[PERCENT] = -100 THEN 4 ELSE 6 END ,
       CASE WHEN  tmp.[PERCENT] = -100 THEN 0 ELSE tmp.[Percent] END 
FROM ##tmpView tmp
    CROSS APPLY
(
    SELECT splitdata Code
    FROM dbo.fnSplitString(@OrgCode , ',')
) AS Insurance
WHERE (
          Darou_flag = 0
          OR tmp.Darou_flag = 6
      )
      AND UpdateStatus = 1
      AND Computer_Name = @ComputerName
	  AND tmp.K_Code NOT IN ( SELECT K_Code FROM ##tmpView tv 
where UpdateStatus = 1 AND Computer_Name = @ComputerName
GROUP BY tv.K_Code HAVING COUNT(tv.K_Code) > 1 );