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.

101 lines
8.5 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_agentTargetRpt] 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_agentTargetRpt] @flag ='rpt',@year ='2014',@month ='February',@agentId='5563'
  10. */
  11. CREATE proc [dbo].[proc_agentTargetRpt]
  12. (
  13. @flag VARCHAR(50)
  14. ,@agentId VARCHAR(50) = NULL
  15. ,@year varchar(50) = null
  16. ,@month varchar(50) = null
  17. ,@pageNumber INT = 1
  18. ,@pageSize INT = 50
  19. ,@user varchar(50) = null
  20. )
  21. AS
  22. SET NOCOUNT ON
  23. SET XACT_ABORT ON
  24. declare @sql varchar(max)
  25. IF @flag='rpt'
  26. BEGIN
  27. declare @tempTable table(agentId int,
  28. sendTarget int, sendActual int, sendChange money, sendBonus int, sendPoint money,
  29. eduPayTarget int, eduPayActual int, eduPayChange money,eduPayBonus int, eduPayPoint money,
  30. topupTarget int, topupActual int, topupChange money,topupBonus int, topupPoint money, totalBonus int, totalPoint money)
  31. insert into @tempTable (agentId,sendTarget,sendActual,sendChange,sendPoint, eduPayTarget , eduPayActual , eduPayChange, eduPayPoint ,topupTarget , topupActual , topupChange,topupPoint)
  32. select agentId
  33. ,ST = isnull(targentTxn,0)
  34. ,SA = isnull(actualTxn,0)
  35. ,SC = cast((isnull(actualTxn,0) - isnull(targentTxn,0)) as float)/cast(isnull(targentTxn,0) as float) * 100
  36. ,SP = round(0.70 * (cast(isnull(actualTxn,0) as float)/cast(isnull(targentTxn,0) as float)),3)
  37. ,ET = isnull(targetEduPay,0)
  38. ,EA = isnull(actualEduPay,0)
  39. ,EC = (cast(isnull(actualEduPay,0) as float) - cast(isnull(targetEduPay,0) as float))/cast(isnull(targetEduPay,0) as float) * 100
  40. ,EP = case when round(0.20 * (cast(isnull(actualEduPay,0) as float)/cast(isnull(targetEduPay,0) as float)),3)
  41. > = 0.2 then 0.2
  42. else round(0.20 * (cast(isnull(actualEduPay,0) as float)/cast(isnull(targetEduPay,0) as float)),3) end
  43. ,TT = isnull(targetTopup,0)
  44. ,TA = isnull(actualTopup,0)
  45. ,TC = cast((isnull(actualTopup,0) - isnull(targetTopup,0)) as float)/cast(isnull(targetTopup,0) as float) * 100
  46. ,TP = case when round(0.10 * (cast(isnull(actualTopup,0) as float)/cast(isnull(targetTopup,0) as float)),3)
  47. >= 0.1 then 0.1
  48. else round(0.10 * (cast(isnull(actualTopup,0) as float)/cast(isnull(targetTopup,0) as float)),3) end
  49. from RemittanceLogData.dbo.agentTarget at with(nolock)
  50. where yr = @year and yrMonth = @month and
  51. agentId = isnull(@agentId,at.agentId)
  52. update @tempTable set sendBonus = case when sendChange >= 10 then '2' when sendChange <= -20 then '-1' else '0' end,
  53. eduPayBonus = case when eduPayChange >= 10 then '2' when eduPayChange <= -20 then '-1' else '0' end,
  54. topupBonus = case when topupChange >= 10 then '2' when topupChange <= -20 then '-1' else '0' end
  55. update @tempTable set totalBonus = sendBonus + eduPayBonus + topupBonus,
  56. totalPoint = sendPoint + eduPayPoint + topupPoint
  57. select [Agent_Id] = a.agentId
  58. ,[Agent_Zone] = am.agentState
  59. ,[Agent_District] = am.agentDistrict
  60. ,[Agent_Name] = am.agentName
  61. ,[Send Transaction_Target] = sendTarget
  62. ,[Send Transaction_Actual] = sendActual
  63. ,[Send Transaction_Change] = sendChange
  64. ,[Send Transaction_Bonus] = sendBonus
  65. ,[Send Transaction_Point] = sendPoint
  66. ,[EduPay_Target] = eduPayTarget
  67. ,[EduPay_Actual] = eduPayActual
  68. ,[EduPay_Change] = eduPayChange
  69. ,[EduPay_Bonus] = eduPayBonus
  70. ,[EduPay_Point] = eduPayPoint
  71. ,[Topup_Target] = topupTarget
  72. ,[Topup_Actual] = topupActual
  73. ,[Topup_Change] = topupChange
  74. ,[Topup_Bonus] = topupBonus
  75. ,[Topup_Point] = topupPoint
  76. ,[Total_Bonus] = totalBonus
  77. ,[Total_Point] = totalPoint
  78. from @tempTable a inner join agentMaster am with(nolock) on a.agentId = am.agentId
  79. order by totalBonus desc
  80. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  81. SELECT 'Year' head, @year value
  82. union all
  83. SELECT 'Month' head, @month value
  84. union all
  85. select 'Agent' head, case when @agentId is not null then (select agentName from agentMaster with(nolock) where agentId = @agentId)
  86. else 'All' end
  87. SELECT 'Agent Target Report ' title
  88. END
  89. GO