﻿


/****** Object:  StoredProcedure [dbo].[USP_TRN_AccountActivities_AddNewActivity]    Script Date: 7/11/2014 5:40:02 PM ******/
 CREATE PROCEDURE [dbo].[TRN_AccountTransactions_Add]     
	--@HttpRequestInfo				nvarchar(max) ,
	@UserId							bigint,
	@AccountId						bigint,
	@AccountTransactionTypeId	    smallint,
	@Amount							Decimal(22,8),
	@AmountUSD						Decimal(18,2),
	@CurrentBalance					Decimal(22,8),
	@CurrentBonusBalance			Decimal(22,8),
    @ExternalReferenceId			varchar(66),
	@OperatorId						int,
	@Comment						nvarchar(1000) ,
	@WalletAddress					varchar(42),
	@PendingId						Bigint			= null,
	@KnokId							bigint			= null,
	@TransId						bigint OUTPUT
AS 


SET NOCOUNT ON        
DECLARE @TranStarted BIT,
		@Symbol varchar(30),
		@AccountTransactionDate datetime,
		@RC int
           
SET @TranStarted = 0            
IF( @@TRANCOUNT = 0 )            
	BEGIN            
		BEGIN TRANSACTION            
		SET @TranStarted = 1            
	END            
	ELSE            
		SET @TranStarted = 0  
		          
BEGIN TRY   
                                        
	-- select @Balance Balance    

	select @Symbol=  Symbol 
	from Accounts
	where AccountId=@AccountId

/*
	IF @AmountUSD IS NULL
	BEGIN
		select @AmountUSD=@Amount*Rate
		from [Knoks_Rates]..CurrentPrices
		where base=@Symbol
		and   NonBase='USD'
	END
*/



	set @AccountTransactionDate=getdate()


	INSERT INTO [dbo].[AccountTransactions]
			   ([AccountId]
			   ,[UserId]
			   ,[AccountTransactionDate]
			   ,[AccountTransactionTypeId]
			   ,[Symbol]
			   ,[Amount]
			   ,[AmountUSD]
			   ,[ExternalReferenceId]
			   ,[Comment]
			   ,[Origin]
			   ,[CurrentBalance]
			   ,[CurrentBonusBalance]
			   ,[OperatorId]
			   ,[WalletAddress]
			   ,[PendingId]
			   ,[KnokId])
		VALUES(@AccountId,
				@UserId,
				@AccountTransactionDate,
				@AccountTransactionTypeId,
				@Symbol,
				@Amount,
				@AmountUSD,
				@ExternalReferenceId,
				@Comment,
				SUSER_SNAME(),
				@CurrentBalance,
				@CurrentBonusBalance,
				@OperatorId,
				@WalletAddress,
				@PendingId,
				@KnokId)
      
		SET @TransId =SCOPE_IDENTITY()    

	---SELECT @UserSequenceID = NEXT VALUE FOR dbo.UserSequence
/*
	UPDATE dbo.UserState 
	SET FirstDepositDate = case @AccountTransactionTypeId
								When 1 then  COALESCE(FirstDepositDate,@AccountTransactionDate)
								Else FirstDepositDate
							end,
		--FirstTradingDate = case When @AccountTransactionTypeId >2 then  COALESCE(FirstTradingDate,@AccountActivityDate)
								--Else FirstTradingDate
							--end,
		LastModifyDate = @AccountTransactionDate,
		LastModifyOrigin = SUSER_SNAME(),
		LastModifyOperatorId=@OperatorId			
	WHERE UserId = @UserId;  
*/

	UPDATE dbo.Accounts 
	SET CurrentBalance = @CurrentBalance,
		CurrentBonusBalance = @CurrentBonusBalance,
		LastModifyDate = GetDate(),
		LastModifyOrigin = SUSER_SNAME(),
		LastModifyOperatorId=@OperatorId
	WHERE AccountId = @AccountId;  
                        
	IF( @TranStarted = 1 )            
		COMMIT TRANSACTION            

	RETURN 0            
END TRY            
BEGIN CATCH            
    IF( @TranStarted = 1 )            
		ROLLBACK TRANSACTION            

	EXEC [USP_LogError]           
	EXEC [USP_RethrowError]   
	
	IF @RC = 0 
		SET @RC = 1			         
	RETURN @RC
END CATCH