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.

423 lines
29 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_ThirdpartyTXN_report] 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_ThirdpartyTXN_report]
  9. @user VARCHAR(30) = NULL
  10. ,@dateType VARCHAR(50) = NULL
  11. ,@dateFrom VARCHAR(20) = NULL
  12. ,@dateTo VARCHAR(20) = NULL
  13. ,@tAgent VARCHAR(40) = NULL
  14. ,@sBranch VARCHAR(10) = NULL
  15. ,@status VARCHAR(50) = NULL
  16. ,@reportType VARCHAR(100) = NULL
  17. ,@groupBy VARCHAR(10) = NULL
  18. ,@pCountry VARCHAR(200) = NULL
  19. ,@scharge VARCHAR(20) = NULL
  20. ,@pageNumber INT = NULL
  21. ,@pageSize INT = NULL
  22. ,@isExportFull VARCHAR(1) = NULL
  23. ,@sCountry VARCHAR(200) = NULL
  24. AS
  25. SET NOCOUNT ON
  26. DECLARE @table VARCHAR(MAX)
  27. DECLARE @sql VARCHAR(MAX)
  28. DECLARE @FilterList TABLE(head VARCHAR(50), value VARCHAR(5000))
  29. DECLARE @globalFilter VARCHAR(MAX) = ''
  30. SET @pageNumber = ISNULL(@pageNumber, 1)
  31. SET @pageSize = ISNULL(@pageSize, 100)
  32. DECLARE @displayTranNo CHAR(1) = 'Y',@reportHead VARCHAR(100)
  33. IF @reportType = 'D'
  34. BEGIN
  35. --SET @pageSize = 10000
  36. SET @reportHead = ' Detail'
  37. INSERT @FilterList
  38. SELECT 'Third Party Agent', (select agentName from agentMaster with(nolock) where agentId = @tAgent)
  39. INSERT @FilterList
  40. SELECT 'Branch', case when @sBranch is null then 'All' else (select agentName from agentMaster with(nolock) where agentId = @sBranch)end
  41. INSERT @FilterList
  42. SELECT 'Date Type',
  43. case
  44. when @dateType = 'txnDate' then 'TXN Date'
  45. when @dateType = 'confirmDate' then 'Confirmed Date'
  46. when @dateType = 'paidDate' then 'Paid Date'
  47. when @dateType = 'cancelDate' then 'Cancel Date'
  48. end
  49. IF @dateType = 'txnDate' -->> sending
  50. BEGIN
  51. INSERT @FilterList
  52. SELECT 'From Date', @dateFrom
  53. INSERT @FilterList
  54. SELECT 'To Date', @dateTo
  55. SET @globalFilter = @globalFilter + ' AND rt.createdDate between ''' + @dateFrom + ''' and ''' + @dateTo + ' 23:59:59'''
  56. SET @globalFilter = @globalFilter + ' AND rt.pAgent = ''' + @tAgent + ''''
  57. if @sBranch is not null
  58. begin
  59. INSERT @FilterList
  60. SELECT 'Branch', @sBranch
  61. SET @globalFilter = @globalFilter + ' AND rt.sBranch = ''' + @sBranch + ''''
  62. end
  63. END
  64. IF @dateType = 'confirmDate' -->> sending
  65. BEGIN
  66. INSERT @FilterList
  67. SELECT 'From Date', @dateFrom
  68. INSERT @FilterList
  69. SELECT 'To Date', @dateTo
  70. SET @globalFilter = @globalFilter + ' AND rt.approvedDate between ''' + @dateFrom + ''' and ''' + @dateTo + ' 23:59:59'''
  71. SET @globalFilter = @globalFilter + ' AND rt.pAgent = ''' + @tAgent + ''''
  72. if @sBranch is not null
  73. begin
  74. INSERT @FilterList
  75. SELECT 'Branch', @sBranch
  76. SET @globalFilter = @globalFilter + ' AND rt.sBranch = ''' + @sBranch + ''''
  77. end
  78. END
  79. IF @dateType = 'paidDate' --> receiving
  80. BEGIN
  81. INSERT @FilterList
  82. SELECT 'From Date', @dateFrom
  83. INSERT @FilterList
  84. SELECT 'To Date', @dateTo
  85. SET @globalFilter = @globalFilter + ' AND rt.paidDate between ''' + @dateFrom + ''' and ''' + @dateTo + ' 23:59:59'''
  86. SET @globalFilter = @globalFilter + ' AND rt.sAgent = ''' + @tAgent + ''''
  87. if @sBranch is not null
  88. begin
  89. INSERT @FilterList
  90. SELECT 'Branch', @sBranch
  91. SET @globalFilter = @globalFilter + ' AND rt.pBranch = ''' + @sBranch + ''''
  92. end
  93. END
  94. IF @dateType = 'cancelDate' --> cancel date
  95. BEGIN
  96. INSERT @FilterList
  97. SELECT 'From Date', @dateFrom
  98. INSERT @FilterList
  99. SELECT 'To Date', @dateTo
  100. SET @globalFilter = @globalFilter + ' AND rt.cancelApprovedDate between ''' + @dateFrom + ''' and ''' + @dateTo + ' 23:59:59'''
  101. SET @globalFilter = @globalFilter + ' AND rt.pAgent = ''' + @tAgent + ''''
  102. if @sBranch is not null
  103. begin
  104. INSERT @FilterList
  105. SELECT 'Branch', @sBranch
  106. SET @globalFilter = @globalFilter + ' AND rt.pBranch = ''' + @sBranch + ''''
  107. end
  108. END
  109. if @status is not null and @status ='Post'
  110. SET @globalFilter = @globalFilter +' AND rt.payStatus = '''+@status+''''
  111. if @status is not null and @status <> 'Post'
  112. begin
  113. if @status ='Unpaid'
  114. set @status = 'Payment'
  115. SET @globalFilter = @globalFilter +' AND rt.tranStatus = '''+@status+''' and rt.payStatus <> ''Post'''
  116. END
  117. IF @sCountry IS NOT NULL
  118. SET @globalFilter = @globalFilter +' AND rt.sCountry = '''+REPLACE(@sCountry,'_',' ')+''''
  119. INSERT @FilterList
  120. SELECT 'Status', isnull(@status,'All')
  121. SET @table = '
  122. SELECT
  123. [Tran No] = isnull(rt.holdTranId,rt.id)
  124. ,[Sender Name] = TS.firstName + ISNULL( '' '' + TS.middleName, '''') + ISNULL( '' '' + TS.lastName1, '''') + ISNULL( '' '' + TS.lastName2, '''')
  125. ,[Sender Company] = TS.companyName
  126. ,[Receiver Name] = rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
  127. ,[DOT] = CONVERT(VARCHAR(20), rt.createdDate, 120)
  128. ,[Paid Date] = ISNULL(CONVERT(VARCHAR(50), rt.paidDate, 120), '''')
  129. ,[Ex Rate] = CAST(isnull(rt.customerRate,''1'') AS VARCHAR) + '' '' + rt.payoutCurr
  130. ,[Payment Type] = rt.paymentMethod
  131. ,[Tot Collected_Amt] = rt.cAmt
  132. ,[Tot Collected_Curr] = rt.collCurr
  133. ,[Send_Amt] = rt.tAmt
  134. ,[Send_Curr] = rt.collCurr
  135. ,[Charge_Amt] = rt.serviceCharge
  136. ,[Charge_Curr] = rt.collCurr
  137. ,[Receive_Amt] = rt.pAmt
  138. ,[Receive_Curr] = rt.payoutCurr
  139. ,[User ID] = rt.paidBy
  140. ,[Send Country] = ISNULL(rt.sCountry,'''')
  141. ,[Receive Country] = ISNULL(rt.pCountry,'''')
  142. ,[Tran Status] = case
  143. when rt.tranStatus = ''Payment'' and rt.payStatus <> ''Post''
  144. then ''Unpaid''
  145. when rt.payStatus = ''Post'' and rt.payStatus = ''Post''
  146. then ''Post''
  147. else rt.tranStatus
  148. end
  149. ,rt.tranStatus
  150. ,rt.payStatus
  151. ,[ICN] = dbo.FNADecryptString(rt.controlNo)
  152. FROM remitTran rt WITH(NOLOCK)
  153. INNER JOIN tranSenders TS WITH (NOLOCK) ON rt.id = TS.tranId
  154. INNER JOIN tranReceivers rec WITH (NOLOCK) ON rt.id = rec.tranId
  155. WHERE paymentMethod = ''Cash Payment'' '
  156. SET @table = @table + @globalFilter
  157. IF @isExportFull = 'Y'
  158. BEGIN
  159. SET @sql = '
  160. SELECT
  161. [S.N] ' +
  162. CASE WHEN @displayTranNo = 'Y' THEN ',[Tran No] = ''<span class = "link" onclick ="ViewTranDetail('' + CAST([Tran No] AS VARCHAR(50)) + '');">'' + CAST([Tran No] AS VARCHAR(50)) + ''</span>''' ELSE '' END + '
  163. ,[Sender Name]
  164. ,[Send/Receive Country] = [Send Country] + ''<br />'' + [Receive Country]
  165. ,[Receiver Name]
  166. ,[DOT/Paid Date] = DOT + ''<br />'' + [Paid Date]
  167. ,[Ex Rate]
  168. ,[Payment Type]
  169. ,[Tot Collected_Amt]
  170. ,[Tot Collected_Curr]
  171. ,[Send_Amt]
  172. ,[Send_Curr]
  173. ,[Charge_Amt]
  174. ,[Charge_Curr]
  175. ,[Receive_Amt]
  176. ,[Receive_Curr]
  177. ,[User ID]
  178. ,[Tran Status]
  179. ,[ICN]
  180. ,rowColor = CASE
  181. WHEN payStatus = ''post'' THEN ''#c8e8ea''
  182. WHEN tranStatus = ''Payment'' THEN ''#fef3b8''
  183. WHEN tranStatus = ''Block'' THEN ''#FF6B6B''
  184. WHEN tranStatus = ''Hold'' OR tranStatus = ''Compliance Hold'' OR tranStatus = ''ofac Hold'' THEN ''#bef9dd''
  185. ELSE ''#FFFFFF''
  186. END
  187. FROM (
  188. SELECT
  189. ROW_NUMBER() OVER (ORDER BY [Send Country] ) AS [S.N],*
  190. FROM (' + @table + ') x
  191. ) AS tmp '
  192. EXEC(@sql)
  193. print(@sql)
  194. END
  195. ELSE
  196. BEGIN
  197. SET @sql = 'SELECT
  198. COUNT(*) AS TXNCOUNT
  199. ,' + CAST(@pageSize AS VARCHAR) + ' PAGESIZE
  200. ,' + CAST(@pageNumber AS VARCHAR) + ' PAGENUMBER
  201. FROM (' + @table + ') x'
  202. EXEC (@sql)
  203. SET @sql = '
  204. SELECT
  205. [S.N] ' +
  206. CASE WHEN @displayTranNo = 'Y' THEN ',[Tran No] = ''<span class = "link" onclick ="ViewTranDetail('' + CAST([Tran No] AS VARCHAR(50)) + '');">'' + CAST([Tran No] AS VARCHAR(50)) + ''</span>''' ELSE '' END + '
  207. ,[Sender Name]
  208. ,[Send/Receive Country] = [Send Country] + ''<br />'' + [Receive Country]
  209. ,[Receiver Name]
  210. ,[DOT/Paid Date] = DOT + ''<br />'' + [Paid Date]
  211. ,[Ex Rate]
  212. ,[Payment Type]
  213. ,[Tot Collected_Amt]
  214. ,[Tot Collected_Curr]
  215. ,[Send_Amt]
  216. ,[Send_Curr]
  217. ,[Charge_Amt]
  218. ,[Charge_Curr]
  219. ,[Receive_Amt]
  220. ,[Receive_Curr]
  221. ,[User ID]
  222. ,[Tran Status]
  223. ,[ICN]
  224. ,rowColor = CASE
  225. WHEN payStatus = ''post'' THEN ''#c8e8ea''
  226. WHEN tranStatus = ''Payment'' THEN ''#fef3b8''
  227. WHEN tranStatus = ''Block'' THEN ''#FF6B6B''
  228. WHEN tranStatus = ''Hold'' OR tranStatus = ''Compliance Hold'' OR tranStatus = ''ofac Hold'' THEN ''#bef9dd''
  229. ELSE ''#FFFFFF''
  230. END
  231. FROM (
  232. SELECT
  233. ROW_NUMBER() OVER (ORDER BY [Send Country] ) AS [S.N],*
  234. FROM (' + @table + ') x
  235. ) AS tmp WHERE [S.N] BETWEEN ' + CAST(((@pageNumber - 1) * @pageSize + 1) AS VARCHAR) + ' AND ' + CAST(@pageNumber * @pageSize AS VARCHAR)
  236. PRINT(@sql)
  237. EXEC (@sql)
  238. END
  239. END
  240. IF @reportType = 'S'
  241. BEGIN
  242. SET @reportHead ='Summary'
  243. INSERT @FilterList
  244. SELECT 'Third Party Agent', (select agentName from agentMaster with(nolock) where agentId = @tAgent)
  245. INSERT @FilterList
  246. SELECT 'Branch', case when @sBranch is null then 'All' else (select agentName from agentMaster with(nolock) where agentId = @sBranch)end
  247. INSERT @FilterList
  248. SELECT 'Date Type', case when @dateType = 'txnDate' then 'TXN Date' when @dateType = 'confirmDate' then 'Confirmed Date' else 'Paid Date' end
  249. IF @dateType = 'txnDate' -->> sending
  250. BEGIN
  251. INSERT @FilterList
  252. SELECT 'From Date', @dateFrom
  253. INSERT @FilterList
  254. SELECT 'To Date', @dateTo
  255. SET @globalFilter = @globalFilter + ' AND rt.createdDate between ''' + @dateFrom + ''' and ''' + @dateTo + ' 23:59:59'''
  256. SET @globalFilter = @globalFilter + ' AND rt.pAgent = ''' + @tAgent + ''''
  257. if @sBranch is not null
  258. begin
  259. INSERT @FilterList
  260. SELECT 'Branch', @sBranch
  261. SET @globalFilter = @globalFilter + ' AND rt.sBranch = ''' + @sBranch + ''''
  262. end
  263. END
  264. IF @dateType = 'confirmDate' -->> sending
  265. BEGIN
  266. INSERT @FilterList
  267. SELECT 'From Date', @dateFrom
  268. INSERT @FilterList
  269. SELECT 'To Date', @dateTo
  270. SET @globalFilter = @globalFilter + ' AND rt.approvedDate between ''' + @dateFrom + ''' and ''' + @dateTo + ' 23:59:59'''
  271. SET @globalFilter = @globalFilter + ' AND rt.pAgent = ''' + @tAgent + ''''
  272. if @sBranch is not null
  273. begin
  274. INSERT @FilterList
  275. SELECT 'Branch', @sBranch
  276. SET @globalFilter = @globalFilter + ' AND rt.sBranch = ''' + @sBranch + ''''
  277. end
  278. END
  279. IF @dateType = 'paidDate' --> receiving
  280. BEGIN
  281. INSERT @FilterList
  282. SELECT 'From Date', @dateFrom
  283. INSERT @FilterList
  284. SELECT 'To Date', @dateTo
  285. SET @globalFilter = @globalFilter + ' AND rt.paidDate between ''' + @dateFrom + ''' and ''' + @dateTo + ' 23:59:59'''
  286. SET @globalFilter = @globalFilter + ' AND rt.sAgent = ''' + @tAgent + ''''
  287. if @sBranch is not null
  288. begin
  289. INSERT @FilterList
  290. SELECT 'Branch', @sBranch
  291. SET @globalFilter = @globalFilter + ' AND rt.pBranch = ''' + @sBranch + ''''
  292. end
  293. END
  294. IF @dateType = 'cancelDate' --> receiving
  295. BEGIN
  296. INSERT @FilterList
  297. SELECT 'From Date', @dateFrom
  298. INSERT @FilterList
  299. SELECT 'To Date', @dateTo
  300. SET @globalFilter = @globalFilter + ' AND rt.cancelApprovedDate between ''' + @dateFrom + ''' and ''' + @dateTo + ' 23:59:59'''
  301. SET @globalFilter = @globalFilter + ' AND rt.pAgent = ''' + @tAgent + ''''
  302. if @sBranch is not null
  303. begin
  304. INSERT @FilterList
  305. SELECT 'Branch', @sBranch
  306. SET @globalFilter = @globalFilter + ' AND rt.sBranch = ''' + @sBranch + ''''
  307. end
  308. END
  309. if @status is not null and @status ='Post'
  310. SET @globalFilter = @globalFilter +' AND RT.payStatus = '''+@status+''''
  311. if @status is not null and @status <> 'Post'
  312. SET @globalFilter = @globalFilter +' AND RT.tranStatus = '''+@status+''''
  313. if @groupBy = 'C' -->> Sending country wise
  314. begin
  315. SET @SQL = 'SELECT
  316. [Payout Country]= RT.pCountry
  317. ,[Total <BR/>Paid TXN] = COUNT(*)
  318. ,[Total Paid <BR/>Amount] = SUM(RT.cAmt)
  319. ,[Total <BR/>Agent Commission] = SUM(isnull(RT.pAgentComm,0))
  320. FROM remitTran RT WITH (NOLOCK) where paymentMethod = ''Cash Payment'' '
  321. SET @SQL = @SQL + @globalFilter + '
  322. GROUP BY RT.pCountry'
  323. end
  324. if @groupBy = 'B' -->> Branch wise
  325. begin
  326. SET @SQL = 'SELECT [Branch Name] = RT.pBranchName
  327. ,[Total <BR/>Paid TXN] = COUNT(*)
  328. ,[Total Paid <BR/>Amount] = SUM(RT.pAmt)
  329. ,[Total <BR/>Agent Commission] = SUM(isnull(RT.pAgentComm,0))
  330. FROM remitTran RT WITH (NOLOCK) where paymentMethod = ''Cash Payment'' '
  331. SET @SQL = @SQL + @globalFilter + '
  332. GROUP BY RT.pBranchName'
  333. end
  334. if @groupBy='SCW'----->>sending country wise
  335. begin
  336. SET @SQL = 'SELECT
  337. [Sending Country] = ''<a href="' + dbo.FNAGetUrl() + 'SwiftSystem/Reports/Reports.aspx?reportName=20167300&dateType=' + @dateType + '&fromDate=' + @dateFrom + '&toDate=' + @dateTo +'&tAgent=' + @tAgent + '&status=' + ISNULL(@status, '') + '&rptType=D&sCountry='' + REPLACE(RT.sCountry,'' '',''_'') + ''" title="View Detail">'' + RT.sCountry + ''</a>''
  338. ,[Total <BR/>TXN] = COUNT(''x'')
  339. ,[Total <BR/>Amount] = SUM(RT.cAmt)
  340. ,[Total <BR/>Agent Commission] = SUM(isnull(RT.sSuperAgentComm,0))
  341. FROM remitTran RT WITH (NOLOCK) where paymentMethod = ''Cash Payment'' '
  342. SET @SQL = @SQL + @globalFilter + '
  343. GROUP BY RT.sCountry'
  344. end
  345. if @groupBy='SAW'----->>sending agent wise
  346. begin
  347. SET @SQL = 'SELECT [Branch Name] = RT.sBranchName
  348. ,[Total <BR/>TXN] = COUNT(*)
  349. ,[Total <BR/>Amount] = SUM(RT.cAmt)
  350. ,[Total <BR/>Agent Commission] = SUM(isnull(RT.sSuperAgentComm,0))
  351. FROM remitTran RT WITH (NOLOCK) where paymentMethod = ''Cash Payment'' '
  352. SET @SQL = @SQL + @globalFilter + '
  353. GROUP BY RT.sBranchName'
  354. end
  355. INSERT @FilterList
  356. SELECT 'Status', isnull(@status,'All')
  357. INSERT @FilterList
  358. SELECT 'Group By', case
  359. when @groupBy = 'C' then 'Payout Country Wise'
  360. when @groupBy='SCW' then 'Sending Country Wise'
  361. when @groupBy='SAW' then 'Sending Agent Wise'
  362. else 'Payout Branch Wise' end
  363. PRINT @SQL
  364. EXEC(@SQL)
  365. END
  366. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  367. SELECT * FROM @FilterList
  368. SELECT 'Thirdparty Transaction Report (Cash Payment Only): '+@reportHead title
  369. GO