create PROCEDURE [dbo].[SupplementaryInsurance_CalcFormula] 
                @FactorId BIGINT , @TotPrice MONEY , @TransactionId SMALLINT, @InsuranceCode VARCHAR(3), 
				@PatientTechnicalRight Money,  @Formula NVARCHAR(MAX)
AS 

DECLARE @DifferAmount MONEY
DECLARE @PatientAmount MONEY

DECLARE @GoodsAmount MONEY
DECLARE @NonInsuranceAmount MONEY
DECLARE @EquipmentAmount MONEY
DECLARE @IncDecValue MONEY
DECLARE @CalcTechnicalRight INT = (SELECT [PERCENT] FROM dbo.SupplementaryInsurance WHERE Code = @InsuranceCode)*@PatientTechnicalRight/100
UPDATE dbo.TmpDrugHavaleh SET SupplementaryInsurancePrice = 0 WHERE Id_Havaleh = @FactorId

DECLARE @SupplementaryInsurance AS TABLE
(
  Autoid BIGINT,
  InsuranceCode VARCHAR(3),
  GoodsCode VARCHAR(15),
  DifferAmount MONEY , 
  PatientAmount MONEY , 
  GoodsAmount MONEY , 
  NonInsuranceAmount MONEY , 
  EquipmentAmount MONEY ,
  TotPrice MONEY,
  Status TINYINT
)

INSERT INTO @SupplementaryInsurance
SELECT Autoid, InsuranceCode,GoodsCode, DifferAmount, PatientAmount, GoodsAmount, NonInsuranceAmount, EquipmentAmount,TotPrice, Status FROM (
SELECT Autoid,InsuranceCode,

K_Code GoodsCode, 

CASE
  WHEN ISNULL(DifferAmount,0) = 0 THEN 0 
  WHEN ISNULL(DifferAmount,0) <> 1  THEN (CASE
 											 WHEN (ISNULL(DifferAmount,0) <= (Tot_Differ/K_Qty1)) AND 
											      (Ceilling = 0 OR Ceilling >= K_Qty1)
											 THEN ISNULL(DifferAmount,0) * K_Qty1

											 WHEN (ISNULL(DifferAmount,0) <= (Tot_Differ/K_Qty1)) AND 
												  (Ceilling <> 0 AND Ceilling < K_Qty1)
											 THEN ISNULL(DifferAmount,0) * Ceilling

											 ELSE Tot_Differ
										   END)

  WHEN ISNULL(DifferAmount,0) = 1 THEN (CASE 
										   WHEN Ceilling = 0  OR  Ceilling >= K_Qty1  THEN Tot_Differ
									       WHEN Ceilling <> 0 AND Ceilling < K_Qty1   THEN Ceilling * (Tot_Differ/K_Qty1) 
										 END)  

END AS DifferAmount,


CASE
  WHEN ISNULL(PatientAmount,0) = 0 THEN 0 
  WHEN ISNULL(PatientAmount,0) <> 1  THEN (CASE
 											 WHEN (ISNULL(PatientAmount,0) <= (Price_Forosh * BimarPercent/100)/K_Qty1) AND 
											      (Ceilling = 0 OR Ceilling >= K_Qty1)
											 THEN ISNULL(PatientAmount,0) * K_Qty1

											 WHEN (ISNULL(PatientAmount,0) <= (Price_Forosh * BimarPercent/100)/K_Qty1) AND 
												  (Ceilling <> 0 AND Ceilling < K_Qty1)
											 THEN ISNULL(PatientAmount,0) * Ceilling

											 ELSE Tot_forosh * BimarPercent/100
										   END)

  WHEN ISNULL(PatientAmount,0) = 1 THEN (CASE 
										   WHEN Ceilling = 0  OR  Ceilling >= K_Qty1  THEN Tot_forosh * BimarPercent/100  
									       WHEN Ceilling <> 0 AND Ceilling < K_Qty1   THEN Ceilling * Price_Forosh * BimarPercent/100
										 END)  
END AS PatientAmount,

CASE
  WHEN Status = 1 THEN  (CASE
                           WHEN ISNULL(GoodsAmount,0) = 0 THEN 0
                           WHEN ISNULL(GoodsAmount,0) <> 1  THEN  (CASE
 																			 WHEN (ISNULL(GoodsAmount,0) <= Price_Forosh) AND 
																				  (Ceilling = 0 OR Ceilling >= K_Qty1)
																			 THEN ISNULL(GoodsAmount,0) * K_Qty1

																			 WHEN (ISNULL(GoodsAmount,0) <= Price_Forosh ) AND 
																				  (Ceilling <> 0 AND Ceilling < K_Qty1)
																			 THEN ISNULL(GoodsAmount,0) * Ceilling

																			 ELSE Tot_forosh 
																		   END)

						   WHEN ISNULL(GoodsAmount,0) = 1  THEN 
																	 (CASE 
																		WHEN Ceilling = 0  OR  Ceilling >= K_Qty1  THEN Tot_forosh 
																		WHEN Ceilling <> 0 AND Ceilling < K_Qty1   THEN Ceilling * Price_Forosh 
																	END)  
                         END 
						)

 ELSE 0                        
END AS GoodsAmount,

CASE
  WHEN Status = 0 THEN  (CASE
                           WHEN ISNULL(NonInsuranceAmount,0) = 0 THEN 0
                           WHEN ISNULL(NonInsuranceAmount,0) <> 1  THEN  (CASE
 																			 WHEN (ISNULL(NonInsuranceAmount,0) <= Price_Forosh) AND 
																				  (Ceilling = 0 OR Ceilling >= K_Qty1)
																			 THEN ISNULL(NonInsuranceAmount,0) * K_Qty1

																			 WHEN (ISNULL(NonInsuranceAmount,0) <= Price_Forosh ) AND 
																				  (Ceilling <> 0 AND Ceilling < K_Qty1)
																			 THEN ISNULL(NonInsuranceAmount,0) * Ceilling

																			 ELSE Tot_forosh 
																		   END)

						   WHEN ISNULL(NonInsuranceAmount,0) = 1  THEN 
																	 (CASE 
																		WHEN Ceilling = 0  OR  Ceilling >= K_Qty1  THEN Tot_forosh 
																		WHEN Ceilling <> 0 AND Ceilling < K_Qty1   THEN Ceilling * Price_Forosh 
																	END)  
                         END 
						)
 ELSE 0                        


END AS NonInsuranceAmount,
CASE
 WHEN Status = 2  THEN  Tot_forosh 
 ELSE 0 
END AS EquipmentAmount, 0 TotPrice, Status
FROM dbo.TmpDrugHavaleh
INNER JOIN  dbo.SupplementaryInsurance_DrugCondition ON GoodsCode = K_Code
WHERE Id_Havaleh  =@FactorId
AND Status <> 7
) AS SupplementaryInsurance
Where InsuranceCode =@InsuranceCode


DECLARE @NonInsuranceFlag BIT
DECLARE @PatientFlag BIT
DECLARE @GoodsFlag BIT
DECLARE @DifferFlag BIT

SELECT @NonInsuranceFlag = NonInsuranceFlag , 
       @PatientFlag = PatientFlag ,
	   @GoodsFlag = GoodsFlag,
	   @DifferFlag = DifferFlag
	   FROM dbo.SupplementaryInsurance
	   WHERE Code = @InsuranceCode
INSERT INTO @SupplementaryInsurance
SELECT Autoid, '', K_Code,
CASE 
  WHEN Status = 7 THEN 0
  WHEN @DifferFlag = 0 AND Tot_Differ > 0 THEN 0 
  WHEN @DifferFlag = 1 AND Tot_Differ > 0 THEN Tot_Differ
END AS DifferAmount,

CASE
  WHEN Status = 7 THEN 0
  WHEN @PatientFlag = 0 AND Status IN(5,6,8) THEN 0
  WHEN @PatientFlag = 1 AND Status IN(5,6,8) THEN Tot_forosh * BimarPercent/100  
END AS PatientAmount,

CASE
  WHEN Status = 7 THEN 0
  WHEN @GoodsFlag = 0 AND Status = 1 THEN 0
  WHEN @GoodsFlag = 1 AND Status = 1 Then  Tot_forosh
END AS GoodsAmount, 

CASE
  WHEN Status = 7 THEN 0
  WHEN @NonInsuranceFlag = 0 AND Status = 0 THEN 0
  WHEN @NonInsuranceFlag = 1 AND Status = 0 THEN Tot_forosh
END AS NonInsuranceAmount,

CASE
  WHEN Status = 2 THEN Tot_forosh 
  ELSE 0
END AS EquipmentAmount,0, Status
FROM dbo.TmpDrugHavaleh
WHERE Autoid NOT IN(SELECT Autoid FROM @SupplementaryInsurance)
AND Id_Havaleh = @FactorId

---------------------------------------------------(SupplementaryInsurance)------------------------------------------------------
UPDATE @SupplementaryInsurance SET TotPrice = SumValue FROM @SupplementaryInsurance
INNER JOIN 
(
SELECT Status, GoodsCode, REPLACE(SUBSTRING(@Formula, 0, CHARINDEX('A', @Formula) - 1), '0.', '')*ISNULL(NonInsuranceAmount,0)/100 +
		REPLACE( SUBSTRING(
								@Formula,
								CHARINDEX('A', @Formula) + 2,
								CHARINDEX('B', @Formula) - CHARINDEX('A', @Formula) - 3
							),
					'0.',
					''
				)*ISNULL(GoodsAmount,0)/100 +
		REPLACE(
					SUBSTRING(
								@Formula,
								CHARINDEX('B', @Formula) + 2,
								CHARINDEX('C', @Formula) - CHARINDEX('B', @Formula) - 3
							),
					'0.',
					''
				)*ISNULL(DifferAmount,0)/100 +
		REPLACE(
					SUBSTRING(
								@Formula,
								CHARINDEX('C', @Formula) + 2,
								CHARINDEX('D', @Formula) - CHARINDEX('C', @Formula) - 3
							),
					'0.',
					''
				)*ISNULL(PatientAmount,0)/100 +
		REPLACE(
					SUBSTRING(
								@Formula,
								CHARINDEX('D', @Formula) + 2,
								CHARINDEX('E', @Formula) - CHARINDEX('D', @Formula) - 3
							),
					'0.',
					''
				) *ISNULL(EquipmentAmount,0)/100 SumValue FROM @SupplementaryInsurance
) AS T ON T.GoodsCode = [@SupplementaryInsurance].GoodsCode
AND [@SupplementaryInsurance].status = T.Status

------------------------------(Update Price In TmpDrugHavaleh And Over_Under_Factor_Drug)-----------------------------
UPDATE dbo.TmpDrugHavaleh SET SupplementaryInsurancePrice = [@SupplementaryInsurance].TotPrice FROM dbo.TmpDrugHavaleh
INNER JOIN @SupplementaryInsurance ON GoodsCode = K_Code
AND [@SupplementaryInsurance].Status = TmpDrugHavaleh.status
WHERE Id_Havaleh = @FactorId

SELECT @IncDecValue = (SELECT SUM(TotPrice) FROM @SupplementaryInsurance) + @CalcTechnicalRight
UPDATE Tmp_Over_Under_Factor_Drug Set Price = @IncDecValue,[Percent] = @IncDecValue * 100 /@TotPrice
WHERE Id_Havaleh = @FactorId AND Code_Over_Under = @TransactionId
