USE [FastMoneyPro_Remit]
GO

/****** Object:  StoredProcedure [dbo].[PROC_GOLDCARD_ACCOUNT_ENTRY]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[PROC_GOLDCARD_ACCOUNT_ENTRY](
	 @CustomerId BIGINT				
)AS
SET NOCOUNT ON
BEGIN TRY

	DECLARE @GoldCardAcc VARCHAR(100),@USER VARCHAR(50),@customerName VARCHAR(100)
	,@NewCustAcc VARCHAR(100)
	,@GoldCardReferalExpenseAcc VARCHAR(20)='910141091093'
	,@Field2 VARCHAR(50) = 'Gold Card Referal'
	,@GoldCardHolderCustId BIGINT =NULL
	,@ReferalCode VARCHAR(30) =NULL


	SELECT @ReferalCode = referelCode,@NewCustAcc = walletAccountNo,@USER = approvedBy,@customerName = firstName
	FROM dbo.customerMaster(NOLOCK) WHERE customerId = @CustomerId

	SELECT 
		 @GoldCardAcc = walletAccountNo
		,@GoldCardHolderCustId = customerId 
	FROM dbo.customerMaster(NOLOCK) WHERE membershipId = @ReferalCode

	IF 	@GoldCardAcc IS NULL OR  @NewCustAcc IS NULL
	BEGIN
		SELECT '1' ErrorCode,'Invalid account' Msg, @ReferalCode Id
		RETURN
	END

	IF @GoldCardHolderCustId IS NULL
	BEGIN
		SELECT '1' ErrorCode,'Referal code doesnot exists' Msg, @ReferalCode Id
		RETURN
	END
	


	----LOGIC FOR CUSTOMER REGISTRATION AND GIVING 5000 KRW EACH

	--IF EXISTS(SELECT 'A' FROM FastMoneyPro_Account.DBO.VW_PostedAccountDetail(NOLOCK) WHERE field2 = 'Gold Card Referal' AND BILLNO = @CustomerId)
	--BEGIN
	--	SELECT '1' ErrorCode,'Gold card referral voucher already generated' Msg, @ReferalCode Id
	--	RETURN
	--END
	--DECLARE @vVoucherType CHAR(1) = 'J',@txnOn VARCHAR(10),@Naration VARCHAR(MAX)=''

	--DECLARE @SessionId VARCHAR(MAX)
	--SET @SessionId = CONVERT(VARCHAR(40),NEWID())

	--DECLARE @DrAmt MONEY = 10000,@CrAmtNewCust MONEY = 5000,@CrAmtGoldCardCust MONEY = 5000

	--SET @txnOn = CONVERT(VARCHAR,GETDATE(),101)
	--SET @Naration='Referal commission '+@ReferalCode+'-'+@customerName

	--BEGIN TRAN
	--	--PRINT 'Its time to make voucher entry'        
	--	INSERT INTO FastMoneyPro_Account.dbo.temp_tran(entry_user_id,acct_num,part_tran_type,tran_amt,field1,field2,sessionID,refrence,emp_name)
	--	SELECT @USER,@GoldCardReferalExpenseAcc,'DR',@DrAmt,NULL,@Field2,@SessionId,@CustomerId,NULL UNION ALL
	--	SELECT @USER,@NewCustAcc,'CR',@CrAmtNewCust,@CustomerId,@Field2,@SessionId,@CustomerId,NULL UNION ALL
	--	SELECT @USER,@GoldCardAcc,'CR',@CrAmtGoldCardCust,@GoldCardHolderCustId,@Field2,@SessionId,@CustomerId,NULL  
		
	--	insert into TblVirtualBankDepositDetail(customerName,virtualAccountNo,amount,receivedOn,depositor)
	--	select '',@GoldCardAcc,@CrAmtGoldCardCust,GETDATE(),'Gold card '+@ReferalCode


	--	UPDATE customerMaster SET availableBalance = ISNULL(availableBalance,0)+ @CrAmtNewCust WHERE customerId = @CustomerId
	--	UPDATE customerMaster SET availableBalance = ISNULL(availableBalance,0)+ @CrAmtGoldCardCust WHERE customerId = @GoldCardHolderCustId
		  
	--COMMIT TRAN
	
	--EXEC FastMoneyPro_Account.dbo.[spa_saveTempTrn] @flag='i',@sessionID= @SessionId,@date=@txnOn,@narration=@Naration,@company_id=1,@v_type=@vVoucherType,@user = @USER
END TRY
BEGIN CATCH
	IF @@TRANCOUNT>0
		ROLLBACK TRAN

END CATCH
GO
