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.

114 lines
5.3 KiB

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