You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
11 KiB

12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_InsertRewardPoints] Script Date: 9/27/2023 5:51:30 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROC [dbo].[proc_InsertRewardPoints] (
  9. @Flag VARCHAR(20)
  10. ,@TranId BIGINT = NULL
  11. ,@CustomerId BIGINT = NULL
  12. ,@ReferralId BIGINT = NULL
  13. ,@rewardPoints INT = NULL
  14. ,@rewardValue MONEY = NULL
  15. )
  16. AS
  17. ;
  18. SET NOCOUNT ON;
  19. SET XACT_ABORT ON;
  20. -- #1526 - Post production fixes for Redeem & Earn
  21. BEGIN
  22. DECLARE @sourceCustomerId INT, @destinationCustomerId INT, @code NVARCHAR(50), @runningBalance MONEY, @totalDrAmount MONEY
  23. , @totalCrAmount MONEY , @transactionPoints INT, @registrationPoints INT, @EquivalentPointsForOne INT, @AvailableBalance MONEY
  24. SELECT @registrationPoints = Points
  25. FROM TBL_PROMOTION_SETUP (NOLOCK)
  26. WHERE PromotionType = 'REGISTRATION'
  27. AND IsActive = 1
  28. SELECT @transactionPoints = Points
  29. FROM TBL_PROMOTION_SETUP (NOLOCK)
  30. WHERE PromotionType = 'TRANSACTION'
  31. AND IsActive = 1
  32. SELECT @EquivalentPointsForOne = Points
  33. FROM TBL_PROMOTION_MASTER_SETUP (NOLOCK)
  34. WHERE IsActive = 1
  35. IF @Flag = 'REGISTER'
  36. BEGIN
  37. --@CustomerId = Customer who is newly registering in the system
  38. --@ReferralId = Customer who is already in the system/ who have referred @CustomerId
  39. --EXEC proc_InsertRewardPoints @Flag = 'REGISTER', @CustomerId = 1, @ReferralId = 2
  40. INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
  41. SELECT @ReferralId,@CustomerId, membershipId, 'REGISTRATION', @ReferralId, 'REFER_EARN', @registrationPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
  42. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) + (@registrationPoints/@EquivalentPointsForOne), @registrationPoints
  43. FROM customerMaster (NOLOCK)
  44. WHERE customerId = @CustomerId
  45. UNION
  46. SELECT @CustomerId,@ReferralId, membershipId, 'REGISTRATION', @ReferralId, 'REFER_EARN', @registrationPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
  47. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@ReferralId) + (@registrationPoints/@EquivalentPointsForOne), @registrationPoints
  48. FROM customerMaster (NOLOCK)
  49. WHERE customerId = @ReferralId
  50. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @registrationPoints WHERE customerId = @CustomerId
  51. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @registrationPoints WHERE customerId = @ReferralId
  52. END
  53. ELSE IF @Flag = 'TRANSACTION'
  54. BEGIN
  55. --@CustomerId = Customer who is doing transaction
  56. --EXEC proc_InsertRewardPoints @Flag = 'TRANSACTION', @CustomerId = 1, @TranId = 1234567890
  57. IF EXISTS(SELECT 'X' FROM REMITTRAN(NOLOCK) WHERE ID = @TranID)
  58. BEGIN
  59. print 'A';
  60. SELECT @sourceCustomerId = sourceCustomerId, @destinationCustomerId = ISNULL(destinationCustomerId,0)
  61. FROM Customer_Promotion (NOLOCK)
  62. WHERE destinationCustomerId = @CustomerId
  63. and codeTYPE = 'REGISTRATION' and rewardType='REFER_EARN';
  64. IF ISNULL(@destinationCustomerId, 0) <> 0
  65. BEGIN
  66. IF NOT EXISTS(SELECT 'X' FROM Customer_Promotion(NOLOCK) WHERE codeTYPE = 'FIRST_TXN' AND destinationCustomerId = @CustomerId)
  67. BEGIN
  68. INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
  69. SELECT @sourceCustomerId, @destinationCustomerId, membershipId, 'FIRST_TXN', @TranId, 'REFER_EARN', @transactionPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
  70. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@destinationCustomerId) + (@transactionPoints/@EquivalentPointsForOne), @transactionPoints
  71. FROM customerMaster (NOLOCK)
  72. WHERE customerId = @destinationCustomerId
  73. UNION
  74. SELECT @destinationCustomerId, @sourceCustomerId, membershipId, 'FIRST_TXN', @TranId, 'REFER_EARN', @transactionPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
  75. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@sourceCustomerId) + (@transactionPoints/@EquivalentPointsForOne), @transactionPoints
  76. FROM customerMaster (NOLOCK)
  77. WHERE customerId = @sourceCustomerId
  78. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @transactionPoints WHERE customerId = @sourceCustomerId
  79. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @transactionPoints WHERE customerId = @destinationCustomerId
  80. END
  81. END
  82. END
  83. END
  84. ELSE IF @Flag = 'DEBIT'
  85. BEGIN
  86. IF ISNULL(@rewardPoints, 0) > 0
  87. BEGIN
  88. --@CustomerId = Customer who is doing transaction
  89. --EXEC proc_InsertRewardPoints @Flag = 'DEBIT', @CustomerId = 1, @rewardPoints = 100, @TranId = 1234567890
  90. SELECT @AvailableBalance = DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId)
  91. IF ISNULL(@rewardPoints, 0) > ISNULL(@AvailableBalance, 0)
  92. BEGIN
  93. SELECT 1 Code, 'Insufficient Balance For Redeem!' msg, NULL id
  94. RETURN;
  95. END
  96. INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
  97. SELECT @CustomerId, 0, membershipId, 'REDEEM', @TranId, 'REFER_EARN', @rewardPoints, GETDATE(), 1, GETDATE(), 'DR'
  98. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) - @rewardPoints, @rewardPoints * @EquivalentPointsForOne
  99. FROM customerMaster (NOLOCK)
  100. WHERE customerId = @CustomerId
  101. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) - @rewardPoints WHERE customerId = @CustomerId
  102. END
  103. END
  104. END