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.

210 lines
18 KiB

9 months ago
9 months ago
9 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
9 months ago
9 months ago
9 months ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_InsertRewardPoints] Script Date: 12/21/2023 12:01:08 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. ,@type VARCHAR(200) = NULL
  16. ,@pageSize VARCHAR(50) = NULL
  17. ,@pageNumber VARCHAR(50) = NULL
  18. ,@sortBy VARCHAR(50) = NULL
  19. ,@sortOrder VARCHAR(50) = NULL
  20. ,@newPoints INT = NULL
  21. ,@promotionType VARCHAR(50) = NULL
  22. ,@user VARCHAR(50) = NULL
  23. )
  24. AS
  25. ;
  26. SET NOCOUNT ON;
  27. SET XACT_ABORT ON;
  28. DECLARE @table VARCHAR(MAX)
  29. ,@select_field_list VARCHAR(MAX)
  30. ,@extra_field_list VARCHAR(MAX)
  31. ,@sql_filter VARCHAR(MAX)
  32. ,@ACC_NUM VARCHAR(30)
  33. ,@oldPoints INT
  34. -- #1526 - Post production fixes for Redeem & Earn
  35. BEGIN
  36. DECLARE @sourceCustomerId INT, @destinationCustomerId INT, @code NVARCHAR(50), @runningBalance MONEY, @totalDrAmount MONEY
  37. , @totalCrAmount MONEY , @transactionPoints INT, @registrationPoints INT, @EquivalentPointsForOne INT, @AvailableBalance MONEY
  38. SELECT @registrationPoints = Points
  39. FROM TBL_PROMOTION_SETUP (NOLOCK)
  40. WHERE PromotionType = 'REGISTRATION'
  41. AND IsActive = 1
  42. SELECT @transactionPoints = Points
  43. FROM TBL_PROMOTION_SETUP (NOLOCK)
  44. WHERE PromotionType = 'TRANSACTION'
  45. AND IsActive = 1
  46. SELECT @EquivalentPointsForOne = Points
  47. FROM TBL_PROMOTION_MASTER_SETUP (NOLOCK)
  48. WHERE IsActive = 1
  49. IF @Flag = 'REGISTER'
  50. BEGIN
  51. --@CustomerId = Customer who is newly registering in the system
  52. --@ReferralId = Customer who is already in the system/ who have referred @CustomerId
  53. --EXEC proc_InsertRewardPoints @Flag = 'REGISTER', @CustomerId = 1, @ReferralId = 2
  54. INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
  55. SELECT @ReferralId,@CustomerId, membershipId, 'REGISTRATION', @ReferralId, 'REFER_EARN', @registrationPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
  56. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) + (@registrationPoints/@EquivalentPointsForOne), @registrationPoints
  57. FROM customerMaster (NOLOCK)
  58. WHERE customerId = @CustomerId
  59. UNION
  60. SELECT @CustomerId,@ReferralId, membershipId, 'REGISTRATION', @ReferralId, 'REFER_EARN', @registrationPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
  61. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@ReferralId) + (@registrationPoints/@EquivalentPointsForOne), @registrationPoints
  62. FROM customerMaster (NOLOCK)
  63. WHERE customerId = @ReferralId
  64. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @registrationPoints WHERE customerId = @CustomerId
  65. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @registrationPoints WHERE customerId = @ReferralId
  66. END
  67. ELSE IF @Flag = 'TRANSACTION'
  68. BEGIN
  69. --@CustomerId = Customer who is doing transaction
  70. --EXEC proc_InsertRewardPoints @Flag = 'TRANSACTION', @CustomerId = 1, @TranId = 1234567890
  71. IF EXISTS(SELECT 'X' FROM REMITTRAN(NOLOCK) WHERE ID = @TranID)
  72. BEGIN
  73. print 'A';
  74. SELECT @sourceCustomerId = sourceCustomerId, @destinationCustomerId = ISNULL(destinationCustomerId,0)
  75. FROM Customer_Promotion (NOLOCK)
  76. WHERE destinationCustomerId = @CustomerId
  77. and codeTYPE = 'REGISTRATION' and rewardType='REFER_EARN';
  78. IF ISNULL(@destinationCustomerId, 0) <> 0
  79. BEGIN
  80. IF NOT EXISTS(SELECT 'X' FROM Customer_Promotion(NOLOCK) WHERE codeTYPE = 'FIRST_TXN' AND destinationCustomerId = @CustomerId)
  81. BEGIN
  82. INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
  83. SELECT @sourceCustomerId, @destinationCustomerId, membershipId, 'FIRST_TXN', @TranId, 'REFER_EARN', @transactionPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
  84. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@destinationCustomerId) + (@transactionPoints/@EquivalentPointsForOne), @transactionPoints
  85. FROM customerMaster (NOLOCK)
  86. WHERE customerId = @destinationCustomerId
  87. UNION
  88. SELECT @destinationCustomerId, @sourceCustomerId, membershipId, 'FIRST_TXN', @TranId, 'REFER_EARN', @transactionPoints/@EquivalentPointsForOne, GETDATE(), 1, GETDATE(), 'CR'
  89. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@sourceCustomerId) + (@transactionPoints/@EquivalentPointsForOne), @transactionPoints
  90. FROM customerMaster (NOLOCK)
  91. WHERE customerId = @sourceCustomerId
  92. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @transactionPoints WHERE customerId = @sourceCustomerId
  93. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @transactionPoints WHERE customerId = @destinationCustomerId
  94. END
  95. END
  96. END
  97. END
  98. ELSE IF @Flag = 'DEBIT'
  99. BEGIN
  100. IF ISNULL(@rewardPoints, 0) > 0
  101. BEGIN
  102. --@CustomerId = Customer who is doing transaction
  103. --EXEC proc_InsertRewardPoints @Flag = 'DEBIT', @CustomerId = 1, @rewardPoints = 100, @TranId = 1234567890
  104. SELECT @AvailableBalance = DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId)
  105. IF ISNULL(@rewardPoints, 0) > ISNULL(@AvailableBalance, 0)
  106. BEGIN
  107. SELECT 1 Code, 'Insufficient Balance For Redeem!' msg, NULL id
  108. RETURN;
  109. END
  110. INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
  111. SELECT @CustomerId, 0, membershipId, 'REDEEM', @TranId, 'REFER_EARN', @rewardPoints, GETDATE(), 1, GETDATE(), 'DR'
  112. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) - @rewardPoints, @rewardPoints * @EquivalentPointsForOne
  113. FROM customerMaster (NOLOCK)
  114. WHERE customerId = @CustomerId
  115. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) - @rewardPoints WHERE customerId = @CustomerId
  116. END
  117. END
  118. ELSE IF @Flag = 'CREDIT'
  119. BEGIN
  120. IF ISNULL(@rewardPoints, 0) > 0
  121. BEGIN
  122. --@CustomerId = Customer who is doing transaction
  123. --EXEC proc_InsertRewardPoints @Flag = 'DEBIT', @CustomerId = 1, @rewardPoints = 100, @TranId = 1234567890
  124. SELECT @AvailableBalance = DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId)
  125. IF @type = 'CANCEL'
  126. BEGIN
  127. SELECT @rewardPoints = rewardPoints FROM remitTran WHERE id = @TranId
  128. INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
  129. SELECT @CustomerId, 0, membershipId, 'REDEEM', @TranId, 'CANCEL_REFUND', @rewardPoints, GETDATE(), 1, GETDATE(), 'CR'
  130. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) - @rewardPoints, @rewardPoints * @EquivalentPointsForOne
  131. FROM customerMaster (NOLOCK)
  132. WHERE customerId = @CustomerId
  133. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @rewardPoints WHERE customerId = @CustomerId
  134. END
  135. ELSE IF @type = 'REJECT'
  136. BEGIN
  137. SELECT @rewardPoints = rewardPoints FROM remitTranTemp WHERE id = @TranId
  138. INSERT INTO Customer_Promotion(sourceCustomerId, destinationCustomerId, code, codeType, referenceId, rewardType, amount, createdDate, status, approvedDate, tranType, runningBalance, EquivalentPoints)
  139. SELECT @CustomerId, 0, membershipId, 'REDEEM', @TranId, 'REJECT_REFUND', @rewardPoints, GETDATE(), 1, GETDATE(), 'CR'
  140. , DBO.FNA_GET_AVAILABLE_BALANCE_POINTS(@CustomerId) - @rewardPoints, @rewardPoints * @EquivalentPointsForOne
  141. FROM customerMaster (NOLOCK)
  142. WHERE customerId = @CustomerId
  143. UPDATE customerMaster SET bonusPoint = ISNULL(bonusPoint, 0) + @rewardPoints WHERE customerId = @CustomerId
  144. END
  145. END
  146. END
  147. ELSE IF @flag = 'UPDATE'
  148. BEGIN
  149. SELECT @oldPoints = points FROM TBL_PROMOTION_SETUP WHERE PromotionType = @promotionType
  150. BEGIN TRANSACTION
  151. UPDATE TBL_PROMOTION_SETUP SET
  152. Points = @newPoints
  153. ,UpdatedBy = @user
  154. ,UpdatedDate = GETDATE()
  155. WHERE PromotionType = @promotionType
  156. EXEC proc_applicationLogs 'i', NULL, 'update', 'staticDataValue', @promotionType, @user, @oldPoints, @newPoints
  157. IF EXISTS (SELECT 'X' FROM #msg WHERE error_code <> 0 )
  158. BEGIN
  159. IF @@TRANCOUNT > 0
  160. ROLLBACK TRANSACTION
  161. SELECT 1 error_code, 'Record can not be updated.' mes, @newPoints id
  162. RETURN
  163. END
  164. IF @@TRANCOUNT > 0
  165. COMMIT TRANSACTION
  166. SELECT 0 error_code, 'Record updated successfully.' mes, @newPoints id
  167. END
  168. --ELSE IF @flag = 'UPDATE-MASTER'
  169. --BEGIN
  170. -- SELECT @oldPoints = points FROM TBL_PROMOTION_SETUP WHERE PromotionType = @promotionType
  171. -- UPDATE TBL_PROMOTION_SETUP SET
  172. -- Points = @newPoints
  173. -- ,UpdatedBy = @user
  174. -- ,UpdatedDate = GETDATE()
  175. -- WHERE PromotionType = @promotionType
  176. -- EXEC proc_applicationLogs 'i', NULL, 'update', 'staticDataValue', @promotionType, @user, @oldValue, @newPoints
  177. --END
  178. END