

ALTER PROC [dbo].[proc_InsertRewardPoints] (
	@Flag VARCHAR(20)
	,@TranId BIGINT = NULL
	,@CustomerId BIGINT = NULL
	,@ReferralId BIGINT = NULL
	,@rewardPoints INT  = NULL
	,@rewardValue MONEY  = NULL
	)
AS;
SET NOCOUNT ON;
SET XACT_ABORT ON;
-- #1526 - Post production fixes for Redeem & Earn 
BEGIN
	DECLARE @sourceCustomerId INT, @destinationCustomerId INT, @code NVARCHAR(50), @runningBalance MONEY, @totalDrAmount MONEY
				, @totalCrAmount MONEY , @transactionPoints INT, @registrationPoints INT, @EquivalentPointsForOne INT, @AvailableBalance MONEY

	SELECT @transactionPoints =  Points 
	FROM TBL_PROMOTION_SETUP (NOLOCK) 
	WHERE PromotionType = 'REGISTRATION'
	AND IsActive = 1

	SELECT @registrationPoints =  Points 
	FROM TBL_PROMOTION_SETUP (NOLOCK) 
	WHERE PromotionType = 'TRANSACTION'
	AND IsActive = 1

	SELECT @EquivalentPointsForOne = Points
	FROM TBL_PROMOTION_MASTER_SETUP (NOLOCK)
	WHERE IsActive = 1

	IF @Flag = 'REGISTER'
	BEGIN
		--@CustomerId = Customer who is newly registering in the system
		--@ReferralId = Customer who is already in the system/ who have referred @CustomerId
		--EXEC proc_InsertRewardPoints @Flag = 'REGISTER', @CustomerId = 1, @ReferralId = 2
		INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
		SELECT @CustomerId, @ReferralId, membershipId, 'REGISTRATION', @ReferralId, 'REFER_EARN', @registrationPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
						, DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) + (@registrationPoints/@EquivalentPointsForOne), @registrationPoints
		FROM customerMaster (NOLOCK)
		WHERE customerId = @CustomerId

		UNION

		SELECT @ReferralId, @ReferralId, membershipId, 'REGISTRATION', @ReferralId, 'REFER_EARN', @registrationPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
						, DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@ReferralId) + (@registrationPoints/@EquivalentPointsForOne), @registrationPoints
		FROM customerMaster (NOLOCK)
		WHERE customerId = @ReferralId

		UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @registrationPoints WHERE customerId = @CustomerId
		UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @registrationPoints WHERE customerId = @ReferralId
	END
	ELSE IF @Flag = 'TRANSACTION'
	BEGIN
		--@CustomerId = Customer who is doing transaction
		--EXEC proc_InsertRewardPoints @Flag = 'TRANSACTION', @CustomerId = 1, @TranId = 1234567890
		IF EXISTS(SELECT 'X' FROM REMITTRAN(NOLOCK) WHERE ID = @TranId AND CAMT <= 100)
		BEGIN
			SELECT @sourceCustomerId = sourceCustomerId, @destinationCustomerId = destinationCustomerId
			FROM Customer_Promotion (NOLOCK)
			WHERE sourceCustomerId = @CustomerId
			and code = 'REGISTRATION'

			IF ISNULL(@destinationCustomerId, 0) <> 0
			BEGIN
				IF NOT EXISTS(SELECT 'X' FROM Customer_Promotion(NOLOCK) WHERE code = 'FIRST_TXN' AND sourceCustomerId = @CustomerId)
				BEGIN
					INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
					SELECT @sourceCustomerId, @destinationCustomerId, membershipId, 'FIRST_TXN', @TranId, 'REFER_EARN', @transactionPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
								, DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@sourceCustomerId) + (@transactionPoints/@EquivalentPointsForOne), @transactionPoints
					FROM customerMaster (NOLOCK)
					WHERE customerId = @sourceCustomerId

					UNION

					SELECT @destinationCustomerId, @sourceCustomerId, membershipId, 'FIRST_TXN', @TranId, 'REFER_EARN', @transactionPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
								, DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@destinationCustomerId) + (@transactionPoints/@EquivalentPointsForOne), @transactionPoints
					FROM customerMaster (NOLOCK)
					WHERE customerId = @destinationCustomerId

					UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @transactionPoints WHERE customerId = @sourceCustomerId
					UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @transactionPoints WHERE customerId = @destinationCustomerId
				END
			END
		END
	END
	ELSE IF @Flag = 'DEBIT'
	BEGIN
		IF ISNULL(@rewardPoints, 0) > 0
		BEGIN
			--@CustomerId = Customer who is doing transaction
			--EXEC proc_InsertRewardPoints @Flag = 'DEBIT', @CustomerId = 1, @rewardPoints = 100, @TranId = 1234567890

			SELECT @AvailableBalance = DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId)

			IF ISNULL(@rewardPoints, 0) > ISNULL(@AvailableBalance, 0)
			BEGIN
				SELECT 1 Code, 'Insufficient Balance For Rededem!' msg, NULL id
				RETURN;
			END
			
			INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
			SELECT @CustomerId, 0, membershipId, 'REDEEM', @TranId, 'REFER_EARN', @rewardValue, GETDATE(), 1, GETDATE(), 'DR'
								, DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) - @rewardValue, @rewardValue * @EquivalentPointsForOne
			FROM customerMaster (NOLOCK)
			WHERE customerId = @CustomerId

			UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) - @rewardPoints WHERE customerId = @CustomerId
		END
	END
END


