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.

164 lines
10 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_agentTargetSchedule] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. --EXEC [proc_agentTargetSchedule] @date = '06/21/2016'
  9. CREATE proc [dbo].[proc_agentTargetSchedule]
  10. @date VARCHAR(50) = NULL
  11. AS
  12. SET NOCOUNT ON
  13. SET XACT_ABORT ON
  14. BEGIN
  15. DECLARE @month VARCHAR(50)
  16. ,@year VARCHAR(20)
  17. ,@StartDate VARCHAR(50)
  18. ,@EndDate VARCHAR(50)
  19. ,@endDate2 VARCHAR(20)
  20. /*
  21. if @date is null
  22. set @date = getdate()
  23. select @month = datename(month,@date)
  24. select @year = datepart(year,@date)
  25. SET @StartDate = CONVERT(DateTime, LEFT(@month, 3) + ' 1 '+@year+'', 100);
  26. SET @EndDate = DATEADD(MONTH, 1, @StartDate) - 1;
  27. set @StartDate = convert(varchar,cast(@StartDate as datetime),101)
  28. set @EndDate = convert(varchar,cast(@EndDate as datetime),101)
  29. exec proc_agentTargetSchedule
  30. */
  31. SET @month = 'Ashad'
  32. SET @year = '2073'
  33. SET @StartDate = '06/15/2016'
  34. SET @EndDate = '07/15/2016'
  35. SET @endDate2 = CONVERT(VARCHAR, DATEADD(DAY, 1, @EndDate),101)
  36. SET @date = CONVERT(VARCHAR,GETDATE(),101)
  37. IF (@date >= @StartDate OR @date <= @endDate2)
  38. BEGIN
  39. IF EXISTS(SELECT TOP 1 'x' FROM RemittanceLogData.dbo.agentTarget WITH(NOLOCK)
  40. WHERE yr = @year AND yrMonth = @month and userName is null)
  41. BEGIN
  42. -- ## UPDATE SEND TXN - normal
  43. UPDATE RemittanceLogData.dbo.agentTarget SET
  44. actualTxn = b.actual
  45. FROM RemittanceLogData.dbo.agentTarget a,
  46. (
  47. SELECT ag.sAgent,COUNT('x') actual
  48. FROM remitTran rt WITH(NOLOCK)
  49. INNER JOIN RemittanceLogData.dbo.agentTarget ag WITH(NOLOCK) ON rt.sAgent = ag.sAgent
  50. INNER JOIN tranReceivers rec WITH(NOLOCK) ON rt.id =rec.tranId
  51. WHERE ag.yr = @year
  52. AND yrMonth = @month
  53. AND rt.approvedDate BETWEEN @StartDate AND @EndDate+' 23:59:59'
  54. AND tranType = 'D'
  55. AND ag.userName IS NULL
  56. AND rt.tranStatus <> 'Cancel'
  57. AND ag.sAgent <> 4618
  58. and rec.stdCollegeId is null
  59. GROUP BY ag.sAgent
  60. )b WHERE a.sAgent = b.sAgent AND a.yr = @year AND a.yrMonth = @month and a.userName is null
  61. -- ## UPDATE SEND TXN - COOPERATIVE
  62. UPDATE RemittanceLogData.dbo.agentTarget SET
  63. actualTxn = b.actual
  64. FROM RemittanceLogData.dbo.agentTarget a,
  65. (
  66. SELECT ag.agentId,COUNT('x') actual
  67. FROM remitTran rt WITH(NOLOCK) INNER JOIN RemittanceLogData.dbo.agentTarget ag WITH(NOLOCK) ON rt.sBranch = ag.agentId
  68. INNER JOIN tranReceivers rec WITH(NOLOCK) ON rt.id =rec.tranId
  69. WHERE ag.yr = @year
  70. AND yrMonth = @month
  71. AND ag.userName IS NULL
  72. AND rt.approvedDate BETWEEN @StartDate AND @EndDate+' 23:59:59'
  73. AND tranType = 'D'
  74. AND rt.tranStatus <> 'Cancel'
  75. AND ag.sAgent = 4618
  76. and rec.stdCollegeId is null
  77. GROUP BY ag.agentId
  78. )b WHERE a.agentId = b.agentId
  79. AND a.yr = @year
  80. AND a.yrMonth = @month
  81. AND a.userName is null
  82. END
  83. END
  84. /*
  85. IF @date IS NULL
  86. SET @date = GETDATE()
  87. SELECT @month = DATENAME(month,@date)
  88. SELECT @year = DATEPART(year,@date)
  89. SET @StartDate = CONVERT(DATETIME, LEFT(@month, 3) + ' 1 '+@year+'', 100);
  90. SET @EndDate = DATEADD(MONTH, 1, @StartDate) - 1;
  91. SET @StartDate = CONVERT(VARCHAR,CAST(@StartDate AS DATETIME),101)
  92. SET @EndDate = CONVERT(VARCHAR,CAST(@EndDate AS DATETIME),101)
  93. IF EXISTS(SELECT 'x' FROM RemittanceLogData.dbo.agentTarget WITH(NOLOCK) WHERE yr = @year AND yrMonth = @month AND userName IS NOT NULL)
  94. BEGIN
  95. UPDATE RemittanceLogData.dbo.agentTarget SET
  96. actualTxn = b.actual
  97. FROM RemittanceLogData.dbo.agentTarget a,
  98. (
  99. SELECT
  100. userName = rt.createdBy,
  101. actual = COUNT('x')
  102. FROM remitTran rt WITH(NOLOCK)
  103. INNER JOIN RemittanceLogData.dbo.agentTarget ag WITH(NOLOCK) ON rt.createdBy = ag.username
  104. WHERE ag.yr = @year
  105. AND yrMonth = @month
  106. AND rt.approvedDate BETWEEN @StartDate AND @EndDate+' 23:59:59'
  107. AND tranType = 'I'
  108. AND rt.tranStatus <> 'Cancel'
  109. AND ag.isIMEStaff IS null
  110. GROUP BY rt.createdBy
  111. )b WHERE a.userName = b.userName
  112. AND a.yr = @year
  113. AND a.yrMonth = @month
  114. AND a.isIMEStaff IS null
  115. UPDATE RemittanceLogData.dbo.agentTarget SET
  116. actualTxn = b.actual
  117. FROM RemittanceLogData.dbo.agentTarget a,
  118. (
  119. SELECT
  120. agentId = rt.sBranch,
  121. actual = COUNT('x'),
  122. userName = b.userName
  123. FROM remitTran rt WITH(NOLOCK)
  124. INNER JOIN
  125. (
  126. select distinct au.agentId,au.userName from RemittanceLogData.dbo.agentTarget ag WITH(NOLOCK)
  127. inner join applicationusers au with(nolock) ON ag.userName = au.userName
  128. where ag.isIMEStaff = 'Y'
  129. and ag.yr = @year
  130. AND yrMonth = @month
  131. )b on rt.sBranch = b.agentId
  132. WHERE
  133. rt.approvedDate BETWEEN @StartDate AND @EndDate+' 23:59:59'
  134. AND tranType = 'I'
  135. AND rt.tranStatus <> 'Cancel'
  136. GROUP BY rt.sBranch ,b.userName
  137. )b WHERE a.userName = b.userName
  138. AND a.yr = @year
  139. AND a.yrMonth = @month
  140. AND a.isIMEStaff = 'Y'
  141. END
  142. */
  143. END
  144. GO