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.

259 lines
17 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_tranReport] 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_tranReport @flag = 'r', @user = 'admin', @fromDate = '2012-6-6', @toDate = '2012-7-2',
  10. @reportType = 'S', @pageNumber = '1', @pageSize = '100', @sessionId = 'sdqz13btxutwh4ecp1n02gc2'
  11. */
  12. CREATE procEDURE [dbo].[proc_tranReport]
  13. @flag VARCHAR(20)
  14. ,@user VARCHAR(30)
  15. ,@fromDate DATETIME = NULL
  16. ,@toDate DATETIME = NULL
  17. ,@reportType CHAR(1)
  18. ,@pageNumber INT = NULL
  19. ,@pageSize INT = NULL
  20. ,@sessionId VARCHAR(60)
  21. AS
  22. SET NOCOUNT ON;
  23. SET ANSI_NULLS ON;
  24. --SET @toDate = @toDate + ' 23:59:59'
  25. DECLARE @agentId INT,@maxReportViewDays INT
  26. SET @pageSize = ISNULL(@pageSize,500)
  27. SET @pageNumber = ISNULL(@pageNumber,1)
  28. SELECT @maxReportViewDays=ISNULL(maxReportViewDays,60),@agentId = agentId FROM applicationUsers WHERE userName = @user
  29. IF CAST(DATEDIFF(DD,@fromDate,@toDate) AS INT)>@maxReportViewDays
  30. BEGIN
  31. SELECT CASE WHEN @reportType = 'S' THEN 'Transaction Report-Send' ELSE 'Transaction Report-Pay' END title
  32. EXEC proc_errorHandler '1', 'Report viewing range exceeded.', @agentId
  33. RETURN
  34. END
  35. IF @reportType = 'S'
  36. BEGIN
  37. IF NOT EXISTS(SELECT 'A' FROM tbl_tranReport WITH (NOLOCK) WHERE SESSIONID = @sessionId AND [reportType] = 'S'
  38. AND [fromDate] = @fromDate AND [toDate] = CAST(@toDate AS DATE) )
  39. BEGIN
  40. DELETE FROM tbl_tranReport WHERE SESSIONID = @sessionId AND [reportType] = 'S'
  41. INSERT INTO tbl_tranReport
  42. ([rowId],
  43. [reportType],
  44. [date],
  45. [controlNo],
  46. [trnAmt],
  47. [serviceCharge],
  48. [sendingAgent],
  49. [senderName],
  50. [senderAddress],
  51. [receiverName],
  52. [receiverAddress],
  53. [tranStatus],
  54. [sessionId],
  55. [fromDate],
  56. [toDate],
  57. [agentId])
  58. SELECT
  59. ROW_NUMBER() OVER ( ORDER BY agentName ASC)
  60. ,'S'
  61. ,[Date] = rt.approvedDate
  62. ,[Reference No] ='<a href = "#" onclick="OpenInNewWindow('''+dbo.FNAGetURL()+'Remit/Transaction/Reports/SearchTransaction.aspx?tranId=' + CAST(rt.id AS VARCHAR) + ''')">' + dbo.FNADecryptString(rt.controlNo) + '</a>'
  63. ,[Sent Amount] = rt.tAmt
  64. ,[Service <br /> Charge] = rt.serviceCharge
  65. ,[Sending Agent] = sa.agentName
  66. ,[Sender Name] = ts.firstName + ISNULL(' ' + ts.middleName, '') + ISNULL(' ' + ts.lastName1, '') + ISNULL(' ' + ts.lastName2, '')
  67. ,[Sender <br /> Address] = ts.address
  68. ,[Receiver Name] = tr.firstName + ISNULL(' ' + tr.middleName, '') + ISNULL(' ' + tr.lastName1, '') + ISNULL(' ' + tr.lastName2, '')
  69. ,[Receiver Address] = tr.address
  70. ,[Tran Status] = rt.tranStatus
  71. ,@sessionId
  72. ,@fromDate
  73. ,CAST(@toDate AS DATE)
  74. ,@agentId
  75. FROM (
  76. SELECT
  77. approvedDate,id,controlNo,tAmt,serviceCharge,
  78. tranStatus,sAgent
  79. FROM remitTran rt WITH(NOLOCK)
  80. WHERE approvedDate BETWEEN ISNULL(@fromDate, '1900-01-01')
  81. AND ISNULL(@toDate + '23:59:59', '2100-12-31')
  82. AND sBranch = CASE WHEN @agentId = 1 THEN sBranch ELSE @agentId END
  83. ) rt
  84. INNER JOIN tranSenders ts WITH(NOLOCK) ON rt.id = ts.tranId
  85. INNER JOIN tranReceivers tr WITH(NOLOCK) ON rt.id = tr.tranId
  86. INNER JOIN agentMaster sa WITH(NOLOCK) ON rt.sAgent = sa.agentId
  87. --LEFT JOIN staticDataValue sdv WITH(NOLOCK) ON rt.tranStatus = sdv.valueId
  88. ORDER BY [Date] DESC
  89. SELECT TXNCOUNT = COUNT('A'),
  90. PAGESIZE = @pageSize
  91. FROM tbl_tranReport
  92. WHERE SESSIONID = @sessionId AND [reportType] = 'S'
  93. SELECT
  94. [Date] = CONVERT(VARCHAR,[DATE],101),
  95. [Reference No] = [controlNo],
  96. [Sent Amount] = [trnAmt],
  97. [Service <br /> Charge] = [serviceCharge],
  98. [Sending Agent] = [sendingAgent],
  99. [Sender <br /> Address] = [senderName],
  100. [Receiver Name] = [senderAddress],
  101. [Receiver Name] = [receiverName],
  102. [Receiver Address] = [receiverAddress],
  103. [Tran Status] = [tranStatus]
  104. FROM tbl_tranReport WITH (NOLOCK)
  105. WHERE SESSIONID = @sessionId AND [reportType] = 'S'
  106. AND ROWID BETWEEN ((@PageNumber - 1) * @PageSize + 1) AND @pageSize * @pageNumber
  107. END
  108. ELSE
  109. BEGIN
  110. SELECT TXNCOUNT = COUNT('A'),
  111. PAGESIZE = @pageSize
  112. FROM tbl_tranReport
  113. WHERE SESSIONID = @sessionId AND [reportType] = 'S'
  114. SELECT
  115. [Date] = CONVERT(VARCHAR,[DATE],101),
  116. [Reference No] = [controlNo],
  117. [Sent Amount] = [trnAmt],
  118. [Service <br /> Charge] = [serviceCharge],
  119. [Sending Agent] = [sendingAgent],
  120. [Sender <br /> Address] = [senderName],
  121. [Receiver Name] = [senderAddress],
  122. [Receiver Name] = [receiverName],
  123. [Receiver Address] = [receiverAddress],
  124. [Tran Status] = [tranStatus]
  125. FROM tbl_tranReport WITH (NOLOCK)
  126. WHERE SESSIONID = @sessionId AND [reportType] = 'S'
  127. AND ROWID BETWEEN ((@PageNumber - 1) * @PageSize + 1) AND @pageSize * @pageNumber
  128. END
  129. END
  130. ELSE IF @reportType = 'P'
  131. BEGIN
  132. IF NOT EXISTS(SELECT 'A' FROM tbl_tranReport WITH (NOLOCK) WHERE SESSIONID = @sessionId AND [reportType] = 'P'
  133. AND [fromDate] = @fromDate AND [toDate] = CAST(@toDate AS DATE) )
  134. BEGIN
  135. DELETE FROM tbl_tranReport WHERE SESSIONID = @sessionId AND [reportType] = 'P'
  136. INSERT INTO tbl_tranReport
  137. ([rowId],
  138. [reportType],
  139. [date],
  140. [controlNo],
  141. [trnAmt],
  142. [senderName],
  143. [senderAddress],
  144. [receiverName],
  145. [receiverAddress],
  146. [tranStatus],
  147. [sessionId],
  148. [fromDate],
  149. [toDate],
  150. [agentId])
  151. SELECT
  152. ROW_NUMBER() OVER (ORDER BY rt.createdDate ASC)
  153. ,'P'
  154. ,[Date] = rt.paidDate
  155. ,[Reference No] ='<a href = "#" onclick="OpenInNewWindow('''+dbo.FNAGetURL()+'Remit/Transaction/Reports/SearchTransaction.aspx?tranId=' + CAST(rt.id AS VARCHAR) + ''')">' + dbo.FNADecryptString(rt.controlNo) + '</a>'
  156. ,[Payout Amount] = rt.pAmt
  157. ,[Sender Name] = ts.firstName + ISNULL(' ' + ts.middleName, '') + ISNULL(' ' + ts.lastName1, '') + ISNULL(' ' + ts.lastName2, '')
  158. ,[Sender <br /> Address] = ts.address
  159. ,[Receiver Name] = tr.firstName + ISNULL(' ' + tr.middleName, '') + ISNULL(' ' + tr.lastName1, '') + ISNULL(' ' + tr.lastName2, '')
  160. ,[Receiver Address] = tr.address
  161. ,[Tran Status] = rt.tranStatus
  162. ,@sessionId
  163. ,@fromDate
  164. ,CAST(@toDate AS DATE)
  165. ,@agentId
  166. FROM (
  167. SELECT
  168. *
  169. FROM remitTran rt WITH(NOLOCK)
  170. WHERE paidDate BETWEEN ISNULL(@fromDate, '1900-01-01') AND ISNULL(@toDate + '23:59:59', '2100-12-31')
  171. AND pBranch = CASE WHEN @agentId = 1 THEN pBranch ELSE @agentId END
  172. ) rt
  173. INNER JOIN tranSenders ts WITH(NOLOCK) ON rt.id = ts.tranId
  174. INNER JOIN tranReceivers tr WITH(NOLOCK) ON rt.id = tr.tranId
  175. INNER JOIN agentMaster sa WITH(NOLOCK) ON rt.sAgent = sa.agentId
  176. --LEFT JOIN staticDataValue sdv WITH(NOLOCK) ON rt.tranStatus = sdv.valueId
  177. ORDER BY [Date] DESC
  178. SELECT TXNCOUNT = COUNT('A'),
  179. PAGESIZE = @pageSize
  180. FROM tbl_tranReport
  181. WHERE SESSIONID = @sessionId AND [reportType] = 'P'
  182. SELECT
  183. [Date] = CONVERT(VARCHAR,[DATE],101),
  184. [Reference No] = [controlNo],
  185. [Sent Amount] = [trnAmt],
  186. [Sender <br /> Address] = [senderName],
  187. [Receiver Name] = [senderAddress],
  188. [Receiver Name] = [receiverName],
  189. [Receiver Address] = [receiverAddress],
  190. [Tran Status] = [tranStatus]
  191. FROM tbl_tranReport WITH (NOLOCK)
  192. WHERE SESSIONID = @sessionId AND [reportType] = 'P'
  193. AND ROWID BETWEEN ((@PageNumber - 1) * @PageSize + 1) AND @pageSize * @pageNumber
  194. END
  195. ELSE
  196. BEGIN
  197. SELECT TXNCOUNT = COUNT('A'),
  198. PAGESIZE = @pageSize
  199. FROM tbl_tranReport
  200. WHERE SESSIONID = @sessionId AND [reportType] = 'P'
  201. SELECT
  202. [Date] = CONVERT(VARCHAR,[DATE],101),
  203. [Reference No] = [controlNo],
  204. [Sent Amount] = [trnAmt],
  205. [Sender <br /> Address] = [senderName],
  206. [Receiver Name] = [senderAddress],
  207. [Receiver Name] = [receiverName],
  208. [Receiver Address] = [receiverAddress],
  209. [Tran Status] = [tranStatus]
  210. FROM tbl_tranReport WITH (NOLOCK)
  211. WHERE SESSIONID = @sessionId AND [reportType] = 'P'
  212. AND ROWID BETWEEN ((@PageNumber - 1) * @PageSize + 1) AND @pageSize * @pageNumber
  213. END
  214. END
  215. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  216. SELECT 'From Date' head, CONVERT(VARCHAR(10), @fromDate, 101) value
  217. UNION ALL
  218. SELECT 'To Date' head, CONVERT(VARCHAR(10), @toDate, 101) value
  219. SELECT title = CASE WHEN @reportType = 'S' THEN 'Transaction Report-Send' ELSE 'Transaction Report-Pay' END
  220. GO