USE [FastMoneyPro_Remit] GO /****** Object: StoredProcedure [dbo].[proc_InsertRewardPoints] Script Date: 12/21/2023 12:01:08 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROC [dbo].[proc_InsertRewardPoints] ( @Flag VARCHAR(20) ,@TranId BIGINT = NULL ,@CustomerId BIGINT = NULL ,@ReferralId BIGINT = NULL ,@rewardPoints INT = NULL ,@rewardValue MONEY = NULL ,@type VARCHAR(200) = NULL ,@pageSize VARCHAR(50) = NULL ,@pageNumber VARCHAR(50) = NULL ,@sortBy VARCHAR(50) = NULL ,@sortOrder VARCHAR(50) = NULL ,@newPoints INT = NULL ,@promotionType VARCHAR(50) = NULL ,@user VARCHAR(50) = NULL ) AS ; SET NOCOUNT ON; SET XACT_ABORT ON; DECLARE @table VARCHAR(MAX) ,@select_field_list VARCHAR(MAX) ,@extra_field_list VARCHAR(MAX) ,@sql_filter VARCHAR(MAX) ,@ACC_NUM VARCHAR(30) ,@oldPoints INT -- #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 @registrationPoints = Points FROM TBL_PROMOTION_SETUP (NOLOCK) WHERE PromotionType = 'REGISTRATION' AND IsActive = 1 SELECT @transactionPoints = 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 @ReferralId,@CustomerId, 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 @CustomerId,@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) BEGIN print 'A'; SELECT @sourceCustomerId = sourceCustomerId, @destinationCustomerId = ISNULL(destinationCustomerId,0) FROM Customer_Promotion (NOLOCK) WHERE destinationCustomerId = @CustomerId and codeTYPE = 'REGISTRATION' and rewardType='REFER_EARN'; IF ISNULL(@destinationCustomerId, 0) <> 0 BEGIN IF NOT EXISTS(SELECT 'X' FROM Customer_Promotion(NOLOCK) WHERE codeTYPE = 'FIRST_TXN' AND destinationCustomerId = @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(@destinationCustomerId) + (@transactionPoints/@EquivalentPointsForOne), @transactionPoints FROM customerMaster (NOLOCK) WHERE customerId = @destinationCustomerId UNION SELECT @destinationCustomerId, @sourceCustomerId, 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 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 Redeem!' 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', @rewardPoints, GETDATE(), 1, GETDATE(), 'DR' , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) - @rewardPoints, @rewardPoints * @EquivalentPointsForOne FROM customerMaster (NOLOCK) WHERE customerId = @CustomerId UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) - @rewardPoints WHERE customerId = @CustomerId END END ELSE IF @Flag = 'CREDIT' 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 @type = 'CANCEL' BEGIN SELECT @rewardPoints = rewardPoints FROM remitTran WHERE id = @TranId INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints) SELECT @CustomerId, 0, membershipId, 'REDEEM', @TranId, 'CANCEL_REFUND', @rewardPoints, GETDATE(), 1, GETDATE(), 'CR' , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) - @rewardPoints, @rewardPoints * @EquivalentPointsForOne FROM customerMaster (NOLOCK) WHERE customerId = @CustomerId UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @rewardPoints WHERE customerId = @CustomerId END ELSE IF @type = 'REJECT' BEGIN SELECT @rewardPoints = rewardPoints FROM remitTranTemp WHERE id = @TranId INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints) SELECT @CustomerId, 0, membershipId, 'REDEEM', @TranId, 'REJECT_REFUND', @rewardPoints, GETDATE(), 1, GETDATE(), 'CR' , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) - @rewardPoints, @rewardPoints * @EquivalentPointsForOne FROM customerMaster (NOLOCK) WHERE customerId = @CustomerId UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @rewardPoints WHERE customerId = @CustomerId END END END ELSE IF @flag = 'UPDATE' BEGIN SELECT @oldPoints = points FROM TBL_PROMOTION_SETUP WHERE PromotionType = @promotionType BEGIN TRANSACTION UPDATE TBL_PROMOTION_SETUP SET Points = @newPoints ,UpdatedBy = @user ,UpdatedDate = GETDATE() WHERE PromotionType = @promotionType EXEC proc_applicationLogs 'i', NULL, 'update', 'staticDataValue', @promotionType, @user, @oldPoints, @newPoints IF EXISTS (SELECT 'X' FROM #msg WHERE error_code <> 0 ) BEGIN IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION SELECT 1 error_code, 'Record can not be updated.' mes, @newPoints id RETURN END IF @@TRANCOUNT > 0 COMMIT TRANSACTION SELECT 0 error_code, 'Record updated successfully.' mes, @newPoints id END --ELSE IF @flag = 'UPDATE-MASTER' --BEGIN -- SELECT @oldPoints = points FROM TBL_PROMOTION_SETUP WHERE PromotionType = @promotionType -- UPDATE TBL_PROMOTION_SETUP SET -- Points = @newPoints -- ,UpdatedBy = @user -- ,UpdatedDate = GETDATE() -- WHERE PromotionType = @promotionType -- EXEC proc_applicationLogs 'i', NULL, 'update', 'staticDataValue', @promotionType, @user, @oldValue, @newPoints --END END