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.

109 lines
8.7 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_agentTargetMonthEnd] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. /*
  9. EXEC [proc_agentTargetMonthEnd] @flag = 'month-end', @year = '2014', @month = 'February', @user ='dipesh'
  10. */
  11. CREATE proc [dbo].[proc_agentTargetMonthEnd]
  12. @flag VARCHAR(50) = NULL
  13. ,@year varchar(20) = null
  14. ,@month varchar(50) = null
  15. ,@user varchar(50) = null
  16. AS
  17. SET NOCOUNT ON
  18. SET XACT_ABORT ON
  19. IF @flag='month-end'
  20. BEGIN
  21. declare @StartDate varchar(20),@EndDate varchar(20),@nextMonthDate varchar(20),@nextMonth varchar(50),@nextYear varchar(20),
  22. @msg varchar(max)
  23. SET @StartDate = CONVERT(DateTime, LEFT(@month, 3) + ' 1 '+@year+'', 100);
  24. SET @EndDate = DATEADD(MONTH, 1, @StartDate) - 1;
  25. set @StartDate = convert(varchar,cast(@StartDate as datetime),101)
  26. set @EndDate = convert(varchar,cast(@EndDate as datetime),101)
  27. set @nextMonthDate = dateadd(day,1,@EndDate)
  28. select @nextMonth = datename(month,@nextMonthDate)
  29. select @nextYear = datepart(year,@nextMonthDate)
  30. if not exists(select 'x' from RemittanceLogData.dbo.agentTarget with(nolock) where yr = @year and yrMonth = @month)
  31. begin
  32. set @msg ='No Data Found for the ['+@month+'] to month-end.'
  33. EXEC proc_errorHandler '1', @msg, NULL
  34. return;
  35. end
  36. if not exists(select 'x' from RemittanceLogData.dbo.agentTarget with(nolock) where yr = @nextYear and yrMonth = @nextMonth)
  37. begin
  38. set @msg ='Please setup target for the next month ['+@nextMonth+'] to month end.'
  39. EXEC proc_errorHandler '1', @msg, NULL
  40. return;
  41. end
  42. declare @tempTable table(agentId int,
  43. sendChange money, sendBonus int, sendPoint money,
  44. eduPayChange money,eduPayBonus int, eduPayPoint money,
  45. topupChange money,topupBonus int, topupPoint money, totalBonus int, totalPoint money)
  46. insert into @tempTable (agentId,sendChange,sendPoint, eduPayChange, eduPayPoint,topupChange,topupPoint)
  47. select agentId
  48. ,SC = cast((isnull(actualTxn,0) - isnull(targentTxn,0)) as float)/cast(isnull(targentTxn,0) as float) * 100
  49. ,SP = round(0.70 * (cast(isnull(actualTxn,0) as float)/cast(isnull(targentTxn,0) as float)),3)
  50. ,EC = (cast(isnull(actualEduPay,0) as float) - cast(isnull(targetEduPay,0) as float))/cast(isnull(targetEduPay,0) as float) * 100
  51. ,EP = case when round(0.20 * (cast(isnull(actualEduPay,0) as float)/cast(isnull(targetEduPay,0) as float)),3)
  52. > = 0.2 then 0.2
  53. else round(0.20 * (cast(isnull(actualEduPay,0) as float)/cast(isnull(targetEduPay,0) as float)),3) end
  54. ,TC = cast((isnull(actualTopup,0) - isnull(targetTopup,0)) as float)/cast(isnull(targetTopup,0) as float) * 100
  55. ,TP = case when round(0.10 * (cast(isnull(actualTopup,0) as float)/cast(isnull(targetTopup,0) as float)),3)
  56. >= 0.1 then 0.1
  57. else round(0.10 * (cast(isnull(actualTopup,0) as float)/cast(isnull(targetTopup,0) as float)),3) end
  58. from RemittanceLogData.dbo.agentTarget at with(nolock)
  59. where yr = @year and yrMonth = @month
  60. update @tempTable set sendBonus = case when sendChange >= 10 then '2' when sendChange <= -20 then '-1' else '0' end,
  61. eduPayBonus = case when eduPayChange >= 10 then '2' when eduPayChange <= -20 then '-1' else '0' end,
  62. topupBonus = case when topupChange >= 10 then '2' when topupChange <= -20 then '-1' else '0' end
  63. update @tempTable set totalBonus = sendBonus + eduPayBonus + topupBonus,
  64. totalPoint = sendPoint + eduPayPoint + topupPoint
  65. update RemittanceLogData.dbo.agentTarget
  66. set totPoint = isnull(b.totalPoint,0)
  67. ,totBonus = isnull(a.totBonus,0) + isnull(b.totalBonus,0)
  68. ,monthEndBy = @user
  69. ,monthEndDate = getdate()
  70. from RemittanceLogData.dbo.agentTarget a,
  71. (
  72. select * from @tempTable
  73. )b where a.agentId = b.agentId
  74. and yr = @year and yrMonth = @month
  75. update RemittanceLogData.dbo.agentTarget
  76. set totBonus = isnull(a.totBonus,0) + isnull(b.totalBonus,0)
  77. ,totPoint = isnull(b.totalPoint,0)
  78. from RemittanceLogData.dbo.agentTarget a,
  79. (
  80. select * from @tempTable
  81. )b where a.agentId = b.agentId
  82. and yr = @nextYear and yrMonth = @nextMonth
  83. EXEC proc_errorHandler '0', 'Month-End has been done successfully.', NULL
  84. END
  85. GO