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.

85 lines
5.6 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_paidTxnCustomerBonusUpdate] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. CREATE proc [dbo].[proc_paidTxnCustomerBonusUpdate]
  9. @flag VARCHAR(50) = NULL
  10. ,@date DATETIME = NULL
  11. AS
  12. DECLARE @FromDate DATETIME, @toDate DATETIME
  13. IF @flag = 'schedule'
  14. BEGIN
  15. SET @FromDate = CONVERT(VARCHAR, GETDATE() - 1, 101)
  16. SET @toDate = CONVERT(VARCHAR, GETDATE(), 101)+' 23:59:59'
  17. IF @date IS NOT NULL
  18. BEGIN
  19. SET @FromDate = CONVERT(VARCHAR, @date, 101)
  20. SET @toDate = @FromDate + ' 23:59:59'
  21. END
  22. DECLARE @customerBonus TABLE(tranId BIGINT, bonusPoint INT, customerId BIGINT)
  23. DECLARE @customerBonusGroup TABLE(bonusPoint MONEY, customerId BIGINT,totBonus MONEY, mobileNo VARCHAR(50))
  24. -->>Get Transaction Id and customer bonus earned from paid transactions
  25. INSERT INTO @customerBonus(tranId, bonusPoint, customerId)
  26. SELECT
  27. rt.id
  28. ,ISNULL(rt.bonusPoint, 0)
  29. ,sen.customerId
  30. FROM remitTran rt WITH(NOLOCK)
  31. INNER JOIN tranSenders sen WITH(NOLOCK) ON rt.id = sen.tranId
  32. INNER JOIN customerMaster cm with(nolock) on sen.membershipId = cm.membershipId
  33. WHERE rt.paidDate BETWEEN @FromDate AND @toDate
  34. AND rt.isBonusUpdated IS NULL
  35. AND ISNULL(rt.bonusPoint, 0) <> 0
  36. AND rt.tranStatus <> 'Cancel'
  37. -->>Group By customerId to get total bonus earned by customer
  38. INSERT INTO @customerBonusGroup(bonusPoint, customerId)
  39. SELECT SUM(bonusPoint), customerId FROM @customerBonus GROUP BY customerId
  40. -->>Update customer bonus earned and deduct pending bonus
  41. UPDATE customerMaster SET
  42. bonusPoint = ISNULL(c.bonusPoint, 0) + ISNULL(cbg.bonusPoint, 0)
  43. ,bonusPointPending = ISNULL(c.bonusPointPending, 0) - ISNULL(cbg.bonusPoint, 0)
  44. FROM customerMaster c
  45. INNER JOIN @customerBonusGroup cbg ON c.customerId = cbg.customerId
  46. -->>Mark transaction as bonus Updated
  47. UPDATE remitTran SET
  48. isBonusUpdated = 'Y'
  49. FROM remitTran rt
  50. INNER JOIN @customerBonus cb ON rt.id = cb.tranId
  51. /*
  52. -- ## SMS Module
  53. UPDATE @customerBonusGroup SET
  54. totBonus = ISNULL(B.bonusPoint,0) ,
  55. mobileNo = mobile
  56. FROM @customerBonusGroup A,
  57. (
  58. SELECT cm.bonusPoint,mobile,cm.customerId FROM customerMaster cm WITH(NOLOCK)
  59. INNER JOIN @customerBonusGroup t ON cm.customerId = t.customerId
  60. )B WHERE A.customerId = B.customerId
  61. INSERT INTO SMSQUEUE(mobileNo,msg,createdDate,createdBy)
  62. SELECT mobileNo,'Tapai ko halko bonus point: '+cast(totBonus as varchar(50))+'.IME Garnu bhayeko ma dhanyabad.Samparka ko laagi 01-4430600, IME',GETDATE(),'system'
  63. FROM @customerBonusGroup WHERE totBonus > 0 and mobileNo <> '' and mobileNo is not null and mobileNo <> '0000000000'
  64. */
  65. RETURN
  66. END
  67. --select to 10 * from SMSQueue order by rowid desc
  68. GO