Create PROCEDURE Club_SaveInfo @TmpId  Numeric(18,0) , @lastId Numeric(18,0) 
AS
	 DECLARE @DefinePrice MONEY
	 DECLARE @TotPrice MONEY = (SELECT Tot_Price_forosh FROM dbo.TmpHeadH WHERE Id_Havaleh = @TmpId)
	 DECLARE @ClubId INT = (SELECT ClubId FROM dbo.TmpHeadH WHERE Id_Havaleh = @TmpId)
	 DECLARE @Year VARCHAR(4) = SUBSTRING(Dbo.MiladiToShamsi(GETDATE()),1,4)
	 DECLARE @Score Smallint
     DECLARE @IsDecimal BIT
 	 DECLARE @TmpValue FLOAT 
	 DECLARE @StrValue VARCHAR(Max)
	 DECLARE @ServiceId TINYINT
     DECLARE @Mobile CHAR(11)
	 DECLARE @Msg SMALLINT

	 SELECT @Score = Score, @IsDecimal = IsDecimal, @DefinePrice= DefinePrice 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 AND ScoreType =1

	 IF ISNULL(@DefinePrice,0) = 0 
	   SET @TmpValue = 0 
	 ELSE 
	 BEGIN 
	   IF @IsDecimal = 1
	   BEGIN 
	     SET @TmpValue = CAST(@TotPrice AS DECIMAL(10,1))  / CAST(@DefinePrice AS DECIMAL(10,1)) * @Score
         SET @StrValue = CAST(@TmpValue AS VARCHAR(Max))
         IF CHARINDEX('.',@TmpValue) > 0 
           SET @TmpValue = CAST(SUBSTRING(@StrValue,1,CHARINDEX('.',@StrValue) + 1)  AS FLOAT)
	   END 
	   ELSE
         SET @TmpValue = CAST(@TotPrice AS INT) / CAST(@DefinePrice AS Int) * @Score
     END


	 BEGIN TRAN
	 IF ISNULL(@ClubId,0) <> 0 
	 BEGIN 
		 INSERT INTO dbo.Tbl_ScoreClub(Id_Club,NuFactor,Year,PriceFactor,Score, Id_Havaleh)
		 SELECT @ClubId, @TmpId, @Year, @TotPrice, @TmpValue,@lastId
	END 
	IF @@ERROR > 0 
	BEGIN 
	  SET @Msg = -5
	  ROLLBACK TRAN
	  RETURN @Msg
	END 
	
	SELECT @Mobile = Mobile FROM dbo.Tbl_ClubH WHERE AutoId = @ClubId
	SELECT @ServiceId = ServiceId FROM dbo.SMS_AutoSendingSettings WHERE ServiceId IN(1,2) 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 
	IF @@ERROR > 0 
	BEGIN 
	  SET @Msg = -5
	  ROLLBACK TRAN
	  RETURN @Msg
	END 
	COMMIT tran

