CREATE PROCEDURE [dbo].[Club_SaveInfoDrug] @TmpId  Numeric(18,0) , @lastId Numeric(18,0) , @FactorId UNIQUEIDENTIFIER, @ClubId INT 
AS
	 DECLARE @DefinePrice MONEY
	 DECLARE @TotPrice MONEY = (SELECT TotPriceforosh FROM dbo.TmpDrugH WHERE Id_Havaleh = @TmpId)
	 DECLARE @Year VARCHAR(4) = SUBSTRING(Dbo.MiladiToShamsi(GETDATE()),1,4)
	 DECLARE @Score Money
     DECLARE @IsDecimal BIT
	 DECLARE @StrValue VARCHAR(Max)
	 DECLARE @ServiceId TINYINT
     DECLARE @Mobile CHAR(11)
	 DECLARE @FromDay SMALLINT
	 DECLARE @ToDay SMALLINT
	 DECLARE @ScorePrice MONEY
 	 DECLARE @ChargeScore FLOAT 
	 DECLARE @ChargePrice MONEY 
	 DECLARE @Percent REAL
	 DECLARE @ScoreId INT
	 DECLARE @ScoreType TINYINT
	 DECLARE @Msg SMALLINT

	 DECLARE CursorSelect CURSOR FOR
	 SELECT ISNULL(Score,0), IsDecimal, ISNULL(DefinePrice,0), FromDay, ToDay, ScorePrice, Percent_Club, ScoreType 
	 FROM dbo.ClubCreditSetting 
	 WHERE CONVERT(VARCHAR(10),GETDATE(),111) >= CONVERT(VARCHAR(10),StartDate,111) 
	 AND CONVERT(VARCHAR(10),GETDATE(),111)   <= CONVERT(VARCHAR(10),EndDate,111) 
	 AND IsActive = 1
	 OPEN CursorSelect
	 FETCH NEXT FROM CursorSelect INTO @Score, @IsDecimal, @DefinePrice, @FromDay, @ToDay, @ScorePrice, @Percent, @ScoreType
	 WHILE @@FETCH_STATUS = 0 
	 BEGIN
	    -----------------------------------شارژ عددي--------------------------------- 
		IF @ScoreType = 1
		BEGIN
		IF @IsDecimal = 1
		BEGIN 
		  SET @ChargeScore = CAST(@TotPrice AS DECIMAL(10,1))  / CAST(@DefinePrice AS DECIMAL(10,1)) * @Score
		  SET @StrValue = CAST(@ChargeScore AS VARCHAR(Max))
		  IF CHARINDEX('.',@ChargeScore) > 0 
			SET @ChargeScore = CAST(SUBSTRING(@StrValue,1,CHARINDEX('.',@StrValue) + 1)  AS FLOAT)
		END 
		ELSE
			SET @ChargeScore = CAST(@TotPrice AS INT) / CAST(@DefinePrice AS Int) * @Score
        END
		-----------------------------------شارژ مبلغي----------------------------------
		ELSE IF @ScoreType = 2
		BEGIN
		  IF @Percent > 0 
     		SET @Score = (@Percent/100) * @DefinePrice 
		  IF @ScorePrice > 0 
      		SET @Score = @ScorePrice
		  IF @IsDecimal = 1
		  BEGIN 
			SET @ChargePrice = CAST(@TotPrice AS DECIMAL(10,1))  / CAST(@DefinePrice AS DECIMAL(10,1)) * @Score 
			SET @StrValue = CAST(@ChargePrice AS VARCHAR(Max))
			IF CHARINDEX('.',@ChargePrice) > 0 
			  SET @ChargePrice = CAST(SUBSTRING(@StrValue,1,CHARINDEX('.',@StrValue) + 1)  AS FLOAT)
		  END 
		  ELSE
			SET @ChargePrice = CAST(@TotPrice AS INT) / CAST(@DefinePrice AS Int) * @Score
          IF @ScorePrice > 0 
		     SET @ChargePrice = ROUND(@ChargePrice,0)
        END
		--------------------------------شارژ بر اساس درصد کالا---------------------------
		ELSE IF @ScoreType = 3
		BEGIN
		  SELECT @ChargePrice =  ROUND(ISNULL(SUM([Percent]*TotForosh/100),0),0)  FROM (
		  SELECT K_Code, SUM(K_Qty1) SumQty, SUM(Tot_Forosh) TotForosh FROM dbo.TmpDrugHavaleh
		  WHERE Id_Havaleh = @TmpId
		  GROUP BY K_Code
		  ) AS T INNER JOIN Tbl_ClubKalaPolicy ON T.K_Code = dbo.Tbl_ClubKalaPolicy.K_code
		  WHERE T.SumQty >= K_Qty
		END 
        FETCH NEXT FROM CursorSelect INTO @Score, @IsDecimal, @DefinePrice, @FromDay, @ToDay, @ScorePrice, @Percent, @ScoreType
     END
     CLOSE CursorSelect
	 DEALLOCATE CursorSelect

	-----------------------------------------------------------------------------------
	BEGIN TRY
	BEGIN TRAN
	IF ISNULL(@ClubId,0) <> 0 
	BEGIN 
	  INSERT INTO dbo.Tbl_ScoreClub(Id_Club,NuFactor,Year,PriceFactor,Score, Id_Havaleh, FactorGuid)
	  SELECT @ClubId, @TmpId, @Year, @TotPrice, ISNULL(@ChargeScore,0),@lastId, @FactorId
	  SELECT @ScoreId = SCOPE_IDENTITY()
	  IF ISNULL(@ChargePrice,0) > 0 
	  BEGIN
		INSERT INTO dbo.ClubDetailScore (ScoreId,  StartDate, EndDate, ChargePrice,FactorGuid)
		VALUES(@ScoreId, DATEADD(DAY,@FromDay,GETDATE()), DATEADD(DAY,@ToDay + @FromDay + 1,GETDATE()), @ChargePrice, @FactorId)  
      END 
	END 
	
	SELECT @Mobile = Mobile FROM dbo.Tbl_ClubH WHERE AutoId = @ClubId
	SELECT @ServiceId = ServiceId FROM dbo.SMS_AutoSendingSettings WHERE ServiceId IN(1,2,3) AND IsActive = 1
	IF (ISNULL(@ServiceId,0) <> 0 AND ISNULL(@Mobile,'') <> '')
	BEGIN
	  INSERT INTO dbo.tblReceivedSms(Mobile,[Message])  
	  VALUES(@Mobile, CAST(@ServiceId AS VARCHAR(10)))
	END 

	---------------------(Charge Club )-----------------
    IF ISNULL(@Mobile,'') <> ''
	  EXEC dbo.Club_ChargeDrug @Mobile, @TmpId, @lastId, @FactorId
    ----------------------------------------------------

	COMMIT TRAN
    END TRY 
    BEGIN CATCH
      DECLARE @ErrorMessage NVARCHAR(MAX), @ErrorSeverity INT, @ErrorState INT;
      SELECT @ErrorMessage = ERROR_MESSAGE() + ' Line ' + CAST(ERROR_LINE() AS NVARCHAR(5)), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE();
  	  SET @Msg = -5
      ROLLBACK TRANSACTION;
      RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
	  RETURN @Msg
    END CATCH
