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.

769 lines
28 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_GetRSPTxnSummaryReport] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_GetRSPTxnSummaryReport]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_GetRSPTxnSummaryReport] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. CREATE procEDURE [dbo].[proc_GetRSPTxnSummaryReport]
  12. @flag VARCHAR(50),
  13. @user VARCHAR(50) = NULL,
  14. @sBranch VARCHAR(10) = NULL,
  15. @sAgent VARCHAR(10) = NULL,
  16. @pCountry VARCHAR(50) = NULL,
  17. @pAgentId VARCHAR(10) = NULL,
  18. @status VARCHAR(50) = NULL,
  19. @DateType VARCHAR(20) = NULL,
  20. @fromDate VARCHAR(10) = NULL,
  21. @toDate VARCHAR(10) = NULL,
  22. @countryBankId VARCHAR(10) = NULL,
  23. @pageNumber INT = NULL,
  24. @pageSize INT = NULL
  25. AS
  26. SET NOCOUNT ON;
  27. IF(DATEDIFF(D,@fromDate,GETDATE())>90 )
  28. BEGIN
  29. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  30. EXEC proc_errorHandler '1', '<font color="red"><b>Date Range is not valid, You can only view transaction upto 90 days.</b></font>', NULL
  31. RETURN;
  32. END
  33. IF(DATEDIFF(D,@fromDate,@toDate))>32
  34. BEGIN
  35. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  36. EXEC proc_errorHandler '1', '<font color="red"><b>Date Range is not valid, Please select date range of 32 days.</b></font>', NULL
  37. RETURN;
  38. END
  39. IF OBJECT_ID('tempdb..#listBranch') IS NOT NULL
  40. DROP TABLE #listBranch
  41. DECLARE @SQL VARCHAR(MAX),@userType varchar(2),@regionalBranchId INT,@branchId INT
  42. CREATE TABLE #listBranch (branchId INT,branchName VARCHAR(200))
  43. IF @userType IS NULL
  44. SELECT @userType = usertype,@regionalBranchId = agentId
  45. FROM applicationUsers WITH(NOLOCK) WHERE userName = @user
  46. INSERT INTO #listBranch
  47. select agentId,agentName from agentmaster(nolock) where agentid = @regionalBranchId
  48. IF @flag = 'Detail'
  49. BEGIN
  50. create table #SettlementReport (remarks VARCHAR(100),qty INT,settAmount MONEY,collCurr VARCHAR(5),sn INT)
  51. SET @SQL = '
  52. INSERT INTO #SettlementReport
  53. SELECT ''Remittance Send(+)'' remarks
  54. ,COUNT(*) QTY
  55. ,SUM(camt) - SUM(ISNULL(sAgentComm,0)) - SUM(ISNULL(agentFxGain,0)) [settAmt]
  56. ,collCurr Currency
  57. ,1 sn
  58. FROM vwRemitTran Rt
  59. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  60. WHERE approvedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  61. GROUP BY collCurr
  62. UNION ALL
  63. SELECT ''Remittance Paid(-)'' remarks
  64. ,COUNT(*) QTY
  65. ,[settAmt] = SUM(pamt) + SUM((ISNULL(pAgentComm,0) / (sCurrCostRate+ISNULL(sCurrHoMargin,0)))*ISNULL(pDateCostRate, (pCurrCostRate - ISNULL(pCurrHoMargin,0))))
  66. ,payoutCurr Currency
  67. , 2 sn
  68. FROM vwRemitTran Rt
  69. INNER JOIN #listBranch T ON RT.pBranch=T.branchId
  70. WHERE paidDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  71. GROUP BY payoutCurr
  72. UNION ALL
  73. SELECT ''Remittance Cancel(-)'' remarks
  74. ,COUNT(*) QTY
  75. ,[settAmt] = SUM(camt) - SUM(ISNULL(sAgentComm,0)) - SUM(ISNULL(agentFxGain,0))
  76. ,collCurr Currency
  77. , 3 sn
  78. FROM vwRemitTran Rt
  79. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  80. WHERE cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  81. GROUP BY collCurr
  82. '
  83. PRINT @SQL
  84. EXEC(@SQL)
  85. DECLARE @CNT INT=2,@holdAmt MONEY,@process MONEY,@setCurr VARCHAR(5)
  86. select @holdAmt=settAmount,@setCurr=COLLCURR from #SettlementReport where SN=1
  87. WHILE @CNT<=3
  88. BEGIN
  89. select @process=settAmount from #SettlementReport where sn=@CNT
  90. SET @holdAmt = @holdAmt -ISNULL(@process,0)
  91. SET @CNT = @CNT+1
  92. END
  93. SELECT Remarks,Qty,settAmount [Settlement Amount],COLLCURR [Settlement Curr] FROM (
  94. SELECT remarks,QTY,settAmount,COLLCURR,SN FROM #SettlementReport
  95. UNION ALL
  96. SELECT 'Net Settlement' ,0,@holdAmt,@setCurr,10
  97. ) X ORDER BY SN
  98. -->>SUMMARY REPORT
  99. SET @SQL ='SELECT
  100. [TRN Date] = CAST(CAST(RT.createdDate AS DATE) AS VARCHAR)
  101. ,[Approved Date] = CAST(CAST(RT.approvedDate AS DATE)AS VARCHAR)
  102. ,[Status] = CASE WHEN CAST(RT.createdDate AS DATE)=CAST(RT.approvedDate AS DATE) THEN ''<b><span style="color:blue;"> Same Day Confirmed </span></b>'' WHEN CAST(RT.approvedDate AS DATE) IS NULL THEN ''<b><span style="color:red;">Not Confirmed </spa
  103. n></b>'' ELSE ''<b><span style="color:red;">Not Same Day Confirmed </span></b>'' END
  104. ,[No of Txn.] = COUNT(*)
  105. ,[Collected Amt(LCY)]= SUM(cAmt)
  106. FROM vwremitTran RT WITH (NOLOCK)
  107. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  108. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  109. IF @sBranch IS NOT NULL
  110. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  111. IF @pCountry <>'all'
  112. SET @SQL = @SQL +' AND pCountry='''+@pCountry+''''
  113. IF @pAgentId IS NOT NULL
  114. SET @SQL = @SQL +' AND pAgent='''+@pAgentId+''''
  115. IF @status IS NOT NULL
  116. SET @SQL = @SQL +' AND payStatus IN ('''+@status+''')'
  117. ELSE
  118. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  119. SET @SQL = @SQL +' GROUP BY CAST(RT.createdDate AS DATE),CAST(RT.approvedDate AS DATE)'
  120. PRINT @SQL
  121. EXEC(@SQL)
  122. END
  123. IF @flag = 'BranchWise'
  124. BEGIN
  125. -- total cancel to agent
  126. SET @SQL =' SELECT
  127. [Agent] = sAgentName
  128. ,[Branch] = sBranchName
  129. ,[NOs] = COUNT(*)
  130. ,[Total <BR/>Collected(LCY)] = SUM(RT.cAmt)
  131. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  132. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  133. ,[Total <BR/>Payable(LCY)] = SUM(RT.tAmt)
  134. FROM vwremitTran RT WITH (NOLOCK)
  135. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  136. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  137. IF @pCountry <>'all'
  138. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  139. IF @sBranch IS NOT NULL
  140. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  141. IF @pAgentId IS NOT NULL
  142. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  143. --IF @status IS NOT NULL
  144. IF @status IS NOT NULL
  145. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  146. ELSE
  147. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  148. SET @SQL = @SQL +' GROUP BY RT.sAgentName,RT.sBranchName'
  149. PRINT @SQL
  150. EXEC(@SQL)
  151. -- total cancel to agent
  152. SET @SQL =' SELECT [Agent] = sAgentName
  153. ,[Branch] = sBranchName
  154. ,[NOs] = COUNT(*)
  155. ,[Total <BR/>Collected(LCY)] = SUM(RT.cAmt)
  156. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  157. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  158. ,[Total Cancel <BR/>To Agent(LCY)]= SUM(X.tAmt)
  159. FROM vwremitTran RT WITH (NOLOCK)
  160. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  161. LEFT JOIN
  162. (
  163. SELECT SUM(tAmt) tAmt,sBranch FROM vwremitTran WITH (NOLOCK)
  164. WHERE sBranch = '''+@sBranch+''' AND payStatus=''Cancel''
  165. GROUP BY sBranch
  166. )X ON X.sBranch = RT.sBranch
  167. WHERE RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' AND RT.payStatus IN (''Cancel'')'
  168. IF @sBranch IS NOT NULL
  169. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  170. IF @pCountry <>'all'
  171. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  172. IF @pAgentId IS NOT NULL
  173. SET @SQL = @SQL +' AND (RT.pAgent='''+@pAgentId+''' OR rt.pAgent IS NULL)'
  174. SET @SQL = @SQL +' GROUP BY RT.sAgentName,RT.sBranchName'
  175. ----------------------
  176. SET @SQL =@SQL+ ' UNION ALL
  177. SELECT sAgentName [Agent]
  178. ,sBranchName [Branch]
  179. ,COUNT(*) [NOs]
  180. ,SUM(RT.cAmt)[Total <BR/>Collected(LCY)]
  181. ,ISNULL(SUM(RT.sAgentComm),0) [Agent <BR/>Comm(LCY)]
  182. ,SUM(RT.serviceCharge) [Total <BR/>SCharge(LCY)]
  183. ,SUM(X.tAmt) [Total Cancel <BR/>To Agent(LCY)]
  184. FROM cancelTranHistory RT WITH (NOLOCK)
  185. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  186. LEFT JOIN
  187. (
  188. SELECT SUM(tAmt) tAmt,sBranch FROM cancelTranHistory WITH (NOLOCK)
  189. WHERE sBranch = '''+@sBranch+''' AND payStatus=''Unpaid''
  190. AND cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  191. GROUP BY sBranch
  192. )X ON X.sBranch = RT.sBranch
  193. WHERE RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' AND RT.payStatus IN (''Unpaid'')'
  194. IF @sBranch IS NOT NULL
  195. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  196. IF @pCountry <>'all'
  197. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  198. IF @pAgentId IS NOT NULL
  199. SET @SQL = @SQL +' AND (RT.pAgent='''+@pAgentId+''' OR rt.pAgent IS NULL)'
  200. SET @SQL = @SQL +' GROUP BY RT.sAgentName,RT.sBranchName'
  201. PRINT @SQL
  202. EXEC(@SQL)
  203. END
  204. IF @flag = 'ReceivingAgentCountryWise'
  205. BEGIN
  206. SET @SQL = 'SELECT [Agent Name]=
  207. ''<a href="Reports.aspx?reportName=rsptxnsummaryrpt&beneficiary=''+RT.pCountry+''&agentName='+
  208. ISNULL(@pAgentId,'')+'&branch='+ ISNULL(@sBranch,'') +'&date='+@DateType+'&from='+@fromDate+'&to='+@toDate+
  209. '&rType=ReceivingAgentWise&status='+ISNULL(@status,'')+'"> ''+RT.pCountry+'' </a>''
  210. ,[Total <BR/>Send TRN] = COUNT(*)
  211. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  212. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  213. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  214. ,[Rec.<br/> Curr] = RT.payoutCurr
  215. ,[Total Payable <BR/>to Agent(LCY)] = ISNULL(SUM(RT.tAmt),0)
  216. FROM vwremitTran RT WITH (NOLOCK)
  217. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  218. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  219. IF @sBranch IS NOT NULL
  220. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  221. IF @pCountry <>'all'
  222. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  223. IF @pAgentId IS NOT NULL
  224. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  225. --IF @status IS NOT NULL
  226. IF @status IS NOT NULL
  227. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  228. ELSE
  229. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  230. SET @SQL = @SQL +' GROUP BY RT.pCountry,RT.payoutCurr'
  231. PRINT @SQL
  232. EXEC(@SQL)
  233. END
  234. IF @flag = 'ReceivingAgentWise'
  235. BEGIN
  236. SET @SQL = 'SELECT [Country Name]=
  237. ''<a href="Reports.aspx?reportName=rsptxnsummaryrpt&beneficiary=''+RT.pCountry+''&branch='+
  238. ISNULL(@sBranch,'') +'&date='+@DateType+'&from='+@fromDate+'&to='+@toDate+'&rType=ReceivingAgentDetail&agentName=''+CAST(ISNULL(pAgent,'''') AS VARCHAR)+''&status='+ISNULL(@status,'')
  239. +'"> ''+ISNULL(RT.pAgentName,''Anywhere'')+''-''+RT.pCountry+'' </a>''
  240. ,[Total <BR/>Send TRN] = COUNT(*)
  241. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  242. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  243. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  244. ,[Rec.<br/> Curr] = RT.payoutCurr
  245. ,[Total Payable <BR/>to Agent(LCY)] = ISNULL(SUM(RT.tAmt),0)
  246. FROM vwremitTran RT WITH (NOLOCK)
  247. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  248. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  249. IF @sBranch IS NOT NULL
  250. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  251. IF @pCountry <>'all'
  252. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  253. IF @pAgentId IS NOT NULL
  254. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  255. --IF @status IS NOT NULL
  256. IF @status IS NOT NULL
  257. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  258. ELSE
  259. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  260. SET @SQL = @SQL +' GROUP BY pAgentName,pAgent,RT.pCountry,RT.payoutCurr ORDER BY RT.pCountry'
  261. PRINT @SQL
  262. EXEC(@SQL)
  263. END
  264. IF @flag = 'ReceivingAgentDetail'
  265. BEGIN
  266. SET @SQL = 'SELECT
  267. [TRN Date] = ''<a href="Reports.aspx?reportName=rsptxnsummaryrpt&beneficiary='+@pCountry+
  268. '&date='+@DateType+'&from=''+CAST(CAST(RT.createdDate AS DATE) AS VARCHAR)+''&to=''+CAST(CAST(RT.createdDate AS DATE) AS VARCHAR)+''&rType=ReceivingDateWise&branch='+
  269. ISNULL(@sBranch,'') +'&sAgent=''+CAST(ISNULL(pAgent,0) AS VARCHAR)+''&agentName='+ISNULL(@pAgentId,'')+'&status='+ISNULL(@status,'')+'"> ''+CAST(CAST(RT.createdDate AS DATE) AS VARCHAR)+'' </a>''
  270. ,[Agent Name] = ISNULL(RT.pAgentName,''Anywhere-''+'''+@pCountry+''')
  271. ,[Total <BR/>Send TRN] = COUNT(*)
  272. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  273. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  274. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  275. ,[Customer Rate] = RT.customerRate
  276. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  277. ,[Rec.<br/> Curr] = RT.payoutCurr
  278. ,[Total Payable <BR/>to Agent(LCY)] = ISNULL(SUM(RT.tAmt),0)
  279. FROM vwremitTran RT WITH (NOLOCK)
  280. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  281. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  282. IF @sAgent IS NOT NULL AND @sAgent > 0
  283. SET @SQL = @SQL +' AND RT.sAgent = '''+ @sAgent+''''
  284. IF @sBranch IS NOT NULL
  285. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  286. IF @pCountry <>'all'
  287. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  288. IF @pAgentId IS NOT NULL AND @pAgentId > 0
  289. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  290. IF @pAgentId =0
  291. SET @SQL = @SQL +' AND RT.pAgent IS NULL'
  292. --IF @status IS NOT NULL
  293. IF @status IS NOT NULL
  294. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  295. ELSE
  296. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  297. SET @SQL = @SQL +' GROUP BY pAgentName,CAST(CAST(RT.createdDate AS DATE) AS VARCHAR),RT.customerRate
  298. ,RT.pAgent,RT.payoutCurr'
  299. EXEC(@SQL)
  300. --Cancel datewise trn
  301. SET @SQL = 'SELECT [TRN Date(CancelDate)] = CAST(CAST(RT.cancelApprovedDate AS DATE) AS VARCHAR)
  302. ,[Agent Name] = RT.pAgentName
  303. ,[Total <BR/>Cancel TRN] = COUNT(*)
  304. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  305. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  306. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  307. ,[ExRate] = RT.customerRate
  308. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  309. ,[Rec.<br/> Curr] = RT.payoutCurr
  310. ,[Total Cancel <BR/>to Agent] = ISNULL(SUM(X.tAmt),0)
  311. FROM vwremitTran RT WITH (NOLOCK)
  312. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  313. LEFT JOIN (
  314. SELECT ISNULL(SUM(tAmt),0) tAmt,sBranch FROM vwremitTran WITH (NOLOCK)
  315. WHERE payStatus=''Cancel''
  316. GROUP BY sBranch
  317. )X ON X.sBranch = RT.sBranch
  318. WHERE
  319. RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' AND RT.payStatus IN (''Cancel'')'
  320. IF @sAgent IS NOT NULL AND @sAgent > 0
  321. SET @SQL = @SQL +' AND RT.sAgent = '''+@sAgent+''''
  322. IF @sBranch IS NOT NULL
  323. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  324. IF @pCountry <>'all'
  325. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  326. IF @pAgentId IS NOT NULL AND @pAgentId > 0
  327. SET @SQL = @SQL +' AND (RT.pAgent='''+@pAgentId+''' OR RT.pAgent IS NULL)'
  328. IF @pAgentId =0
  329. SET @SQL = @SQL +' AND RT.pAgent IS NULL'
  330. SET @SQL = @SQL +' GROUP BY pAgentName,CAST(RT.cancelApprovedDate AS DATE),RT.customerRate
  331. ,RT.payoutCurr'
  332. -------------------------------------------------------
  333. SET @SQL =@SQL+ ' UNION ALL
  334. SELECT [TRN Date(CancelDate)] = CAST(CAST(RT.cancelApprovedDate AS DATE) AS VARCHAR)
  335. ,[Agent Name] = ISNULL(RT.pAgentName,''Anywhere-''+rt.pcountry)
  336. ,[Total <BR/>Cancel TRN] = COUNT(*)
  337. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  338. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  339. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  340. ,[ExRate] = RT.customerRate
  341. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  342. ,[Rec.<br/> Curr] = RT.payoutCurr
  343. ,[Total Cancel <BR/>to Agent] = ISNULL(SUM(X.tAmt),0)
  344. FROM cancelTranHistory RT WITH (NOLOCK)
  345. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  346. LEFT JOIN (
  347. SELECT ISNULL(SUM(tAmt),0) tAmt,sBranch FROM cancelTranHistory WITH (NOLOCK)
  348. WHERE payStatus=''Unpaid''
  349. AND cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  350. GROUP BY sBranch
  351. )X ON X.sBranch = RT.sBranch
  352. WHERE RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' AND RT.payStatus IN (''Unpaid'')'
  353. IF @sAgent IS NOT NULL AND @sAgent > 0
  354. SET @SQL = @SQL +' AND RT.sAgent = '''+@sAgent+''''
  355. IF @sBranch IS NOT NULL
  356. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  357. IF @pCountry <>'all'
  358. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  359. IF @pAgentId IS NOT NULL AND @pAgentId > 0
  360. SET @SQL = @SQL +' AND (RT.pAgent='''+@pAgentId+''' OR RT.pAgent IS NULL)'
  361. IF @pAgentId =0
  362. SET @SQL = @SQL +' AND RT.pAgent IS NULL'
  363. SET @SQL = @SQL +' GROUP BY pAgentName,CAST(RT.cancelApprovedDate AS DATE),RT.customerRate,rt.pcountry
  364. ,RT.payoutCurr'
  365. --PRINT @SQL
  366. EXEC(@SQL)
  367. END
  368. IF @flag = 'ReceivingDateWise'
  369. BEGIN
  370. SET @SQL ='
  371. SELECT
  372. [ICN] = DBO.FNADecryptstring(RT.controlNo)
  373. ,[Sender Name] = RT.senderName
  374. ,[Receiver Name] = RT.receiverName
  375. ,[Tran Status] = CASE WHEN RT.tranStatus=''Payment'' THEN ''Unpaid'' ELSE RT.tranStatus END
  376. ,[DOT/Paid Date] = CONVERT(VARCHAR,RT.createdDate,101)+ISNULL(''/''+CONVERT(VARCHAR,RT.paidDate,101),'''')
  377. ,[ExRate] = RT.customerRate
  378. ,[Total Collection <BR/>Amount(LCY)] = CAST(RT.cAmt AS VARCHAR)
  379. ,[Total Sent <BR/>Amount(LCY)] = CAST(RT.tAmt AS VARCHAR)
  380. ,[Charge<BR/>(MYR)] = CAST(RT.serviceCharge AS VARCHAR)
  381. ,[Customer <BR/>Rec. Amount] = CAST(RT.pAmt AS VARCHAR)
  382. ,[Rec.<br/> Curr] = RT.payoutCurr
  383. ,[User Name] = RT.createdBy
  384. FROM vwremitTran RT WITH (NOLOCK)
  385. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  386. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@fromDate+' 23:59:59'''
  387. IF @sAgent IS NOT NULL AND @sAgent > 0
  388. SET @SQL = @SQL +' AND RT.pAgent = '''+@sAgent+''''
  389. IF @sBranch IS NOT NULL
  390. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  391. IF @pAgentId = 0
  392. SET @SQL = @SQL +' AND RT.pAgent IS NULL'
  393. IF @pCountry <>'all'
  394. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  395. IF @pAgentId IS NOT NULL AND @pAgentId > 0
  396. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  397. --IF @status IS NOT NULL
  398. IF @status IS NOT NULL
  399. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  400. ELSE
  401. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  402. PRINT @SQL
  403. EXEC(@SQL)
  404. END
  405. IF @flag = 'SettlementReport'
  406. BEGIN
  407. IF OBJECT_ID('tempdb..#TempResult') IS NOT NULL
  408. DROP TABLE #TempResult
  409. CREATE TABLE #TempResult(SN INT,[Date] Date,Remarks VARCHAR(100),Qty INT,[Collection Amt] MONEY ,[Total Charge] MONEY
  410. ,PayoutAmt MONEY,[Agt Comm] MONEY ,margin MONEY,[Sett. Amount] MONEY,Currency VARCHAR(5))
  411. SET @SQL = '
  412. INSERT INTO #TempResult
  413. SELECT SN
  414. ,CONVERT(VARCHAR,Date,101) Date
  415. ,Remarks
  416. ,CASE WHEN Remarks=''Remittance Send(+)'' then Qty ELSE Qty*-1 END Qty
  417. ,cAmt [Collection Amt]
  418. ,serviceCharge [Total Charge]
  419. ,PayoutAmt
  420. ,sAgentComm [Agt Comm]
  421. ,ISNULL(margin,0) margin
  422. ,CASE WHEN Remarks=''Remittance Send(+)'' then (cAmt-sAgentComm-ISNULL(margin,0)) ELSE (cAmt-sAgentComm-ISNULL(margin,0))*-1 END [Sett. Amount]
  423. ,collCurr
  424. FROM(
  425. SELECT 1 SN,X.Date,remarks,COUNT(*) qty,SUM(X.cAmt) cAmt,SUM(X.serviceCharge) serviceCharge,SUM(X.[PayoutAmt]) [PayoutAmt]
  426. ,SUM(X.sAgentComm) sAgentComm
  427. ,(SUM(settleAmt)-SUM(payAmt))/SUM([agentSettelRate]) [Ex.Gain]
  428. ,SUM(X.cAmt)-SUM(X.sAgentComm) [Sett. Amount]
  429. ,SUM(margin) margin
  430. ,collCurr
  431. FROM (
  432. SELECT CAST(createdDate AS DATE) [Date],''Remittance Send(+)'' remarks,(cAmt) cAmt,serviceCharge,
  433. (cAmt-serviceCharge) [PayoutAmt],sAgentComm
  434. ,agentCrossSettRate [agentSettelRate]
  435. ,(agentCrossSettRate*tAmt) settleAmt
  436. ,(customerRate*tAmt) payAmt
  437. ,((agentCrossSettRate*tAmt)-(customerRate*tAmt))/agentCrossSettRate [margin]
  438. ,collCurr
  439. FROM VWRemitTran RT
  440. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  441. WHERE createdDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  442. IF @sBranch IS NOT NULL
  443. SET @SQL = @SQL +' AND sBranch='''+@sBranch+''''
  444. IF @pCountry <>'all'
  445. SET @SQL = @SQL +' AND pCountry='''+@pCountry+''''
  446. IF @pAgentId IS NOT NULL
  447. SET @SQL = @SQL +' AND pAgent='''+@pAgentId+''''
  448. SET @SQL =@SQL + '
  449. )X
  450. GROUP BY X.Date,remarks,collCurr
  451. UNION ALL
  452. SELECT 2,X.Date,remarks,COUNT(*) qty,SUM(X.cAmt) cAmt,SUM(X.serviceCharge) serviceCharge,SUM(X.[PayoutAmt]) [PayoutAmt],SUM(X.sAgentComm) sAgentComm
  453. ,(SUM(settleAmt)-SUM(payAmt))/SUM([agentSettelRate]) [Ex.Gain]
  454. ,SUM(X.cAmt)-SUM(X.sAgentComm) [Sett. Amount]
  455. ,SUM(margin) margin
  456. ,payoutCurr
  457. FROM (
  458. SELECT CAST(paidDate AS DATE) [Date],''Remittance Paid(-)'' remarks,(cAmt) cAmt,serviceCharge,
  459. (cAmt-serviceCharge) [PayoutAmt],sAgentComm
  460. ,agentCrossSettRate [agentSettelRate]
  461. ,(agentCrossSettRate*tAmt) settleAmt
  462. ,(customerRate*tAmt) payAmt
  463. ,((agentCrossSettRate*tAmt)-(customerRate*tAmt))/agentCrossSettRate [margin]
  464. ,payoutCurr
  465. FROM RemitTran rt WITH (NOLOCK)
  466. INNER JOIN #listBranch T ON RT.pBranch=T.branchId
  467. WHERE paidDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  468. IF @sBranch IS NOT NULL
  469. SET @SQL = @SQL +' AND pBranch='''+@sBranch+''''
  470. IF @pCountry <>'all'
  471. SET @SQL = @SQL +' AND sCountry='''+@pCountry+''''
  472. IF @pAgentId IS NOT NULL
  473. SET @SQL = @SQL +' AND sAgent='''+@pAgentId+''''
  474. SET @SQL =@SQL + '
  475. )X
  476. GROUP BY X.Date,remarks,payoutCurr
  477. UNION ALL
  478. SELECT 3,X.Date,remarks,COUNT(*) qty,SUM(X.cAmt) cAmt,SUM(X.serviceCharge) serviceCharge,SUM(X.[PayoutAmt]) [PayoutAmt],SUM(X.sAgentComm) sAgentComm
  479. ,(SUM(settleAmt)-SUM(payAmt))/SUM([agentSettelRate]) [Ex.Gain]
  480. ,SUM(X.cAmt)-SUM(X.sAgentComm) [Sett. Amount]
  481. ,SUM(margin) margin
  482. ,collCurr
  483. FROM (
  484. SELECT CAST(cancelApprovedDate AS DATE) [Date],''Remittance Cancel(-)'' remarks,(cAmt) cAmt,serviceCharge,
  485. (cAmt-serviceCharge) [PayoutAmt],sAgentComm
  486. ,agentCrossSettRate [agentSettelRate]
  487. ,(agentCrossSettRate*tAmt) settleAmt
  488. ,(customerRate*tAmt) payAmt
  489. ,ISNULL(((agentCrossSettRate*tAmt)-(customerRate*tAmt))/agentCrossSettRate,0) [margin]
  490. ,collCurr
  491. FROM RemitTran rt WITH (NOLOCK)
  492. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  493. WHERE cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  494. IF @sBranch IS NOT NULL
  495. SET @SQL = @SQL +' AND sBranch='''+@sBranch+''''
  496. IF @pCountry <>'all'
  497. SET @SQL = @SQL +' AND pCountry='''+@pCountry+''''
  498. IF @pAgentId IS NOT NULL
  499. SET @SQL = @SQL +' AND pAgent='''+@pAgentId+''''
  500. SET @SQL =@SQL + '
  501. )X
  502. GROUP BY X.Date,remarks,collCurr
  503. ) Y '
  504. PRINT @SQL
  505. EXEC(@SQL)
  506. SELECT ROW_NUMBER() over (ORDER BY SN) SN, Date,Remarks,Qty,[Collection Amt],[Total Charge],PayoutAmt,[Agt Comm],margin,[Sett. Amount],Currency
  507. FROM #TempResult ORDER BY SN
  508. SELECT Remarks = CASE WHEN Remarks='Remittance Send(+)' THEN
  509. '<a href="Reports.aspx?reportName=rsptxnsummaryrpt&beneficiary='+ISNULL(@pCountry,'')+'&agentName='+ISNULL(@pAgentId,'')+'&branch='+ISNULL(@sBranch,'')+'&date='+@DateType+'&from='+@fromDate+'&to='+@toDate+'&rType=SettlementReport_Send&status='+ISNULL(@status,'')+'"> '+REMARKS+' </a>'
  510. WHEN Remarks='Remittance Paid(-)' THEN
  511. '<a href="Reports.aspx?reportName=rsptxnsummaryrpt&beneficiary='+ISNULL(@pCountry,'')+'&agentName='+ISNULL(@pAgentId,'')+'&branch='+ISNULL(@sBranch,'')+'&date='+@DateType+'&from='+@fromDate+'&to='+@toDate+'&rType=SettlementReport_Paid&status='+ISNULL(@status,'')+'"> '+REMARKS+' </a>'
  512. WHEN Remarks='Remittance Cancel(-)' THEN
  513. '<a href="Reports.aspx?reportName=rsptxnsummaryrpt&beneficiary='+ISNULL(@pCountry,'')+'&agentName='+ISNULL(@pAgentId,'')+'&branch='+ISNULL(@sBranch,'')+'&date='+@DateType+'&from='+@fromDate+'&to='+@toDate+'&rType=SettlementReport_Cancel&status='+ISNULL(@status,'')+'"> '+REMARKS+' </a>'
  514. END
  515. ,X.Nos,[Sett. Amount],Currency FROM (
  516. SELECT SN,Remarks,SUM(Qty) Nos,SUM([Sett. Amount]) [Sett. Amount],Currency FROM #TempResult
  517. GROUP BY Remarks,Currency,SN
  518. ) X ORDER BY SN
  519. END
  520. IF @flag='SettlementReport_Send'
  521. BEGIN
  522. SET @SQL = 'SELECT ROW_NUMBER() OVER (ORDER BY Date) SN
  523. ,Date Date
  524. , ''Remittance Send(+)'' Remarks
  525. ,cAmt [Collection Amt]
  526. ,serviceCharge [Total Charge]
  527. ,PayoutAmt
  528. ,sAgentComm [Agt Comm]
  529. ,ROUND(ISNULL(margin,0),4) margin
  530. ,(cAmt-sAgentComm-ISNULL(margin,0)) [Sett. Amount]
  531. FROM(
  532. SELECT X.Date,(X.cAmt) cAmt,(X.serviceCharge) serviceCharge,(X.[PayoutAmt]) [PayoutAmt]
  533. ,(X.sAgentComm) sAgentComm
  534. ,((settleAmt)-(payAmt))/([agentSettelRate]) [Ex.Gain]
  535. ,(X.cAmt)-(X.sAgentComm) [Sett. Amount]
  536. ,(margin) margin
  537. FROM (
  538. SELECT createdDate [Date],(cAmt) cAmt,serviceCharge,
  539. (cAmt-serviceCharge) [PayoutAmt],sAgentComm
  540. ,agentCrossSettRate [agentSettelRate]
  541. ,(agentCrossSettRate*tAmt) settleAmt
  542. ,(customerRate*tAmt) payAmt
  543. ,ISNULL(((agentCrossSettRate*tAmt)-(customerRate*tAmt))/agentCrossSettRate,0) [margin]
  544. FROM VWRemitTran RT WITH (NOLOCK)
  545. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  546. WHERE createdDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  547. IF @sBranch IS NOT NULL
  548. SET @SQL = @SQL +' AND sBranch='''+@sBranch+''''
  549. IF @pCountry <>'all'
  550. SET @SQL = @SQL +' AND pCountry='''+@pCountry+''''
  551. IF @pAgentId IS NOT NULL
  552. SET @SQL = @SQL +' AND pAgent='''+@pAgentId+''''
  553. SET @SQL = @SQL +' ) X
  554. ) Y '
  555. PRINT @SQL
  556. EXEC(@SQL)
  557. END
  558. IF @flag='SettlementReport_Cancel'
  559. BEGIN
  560. SET @SQL = 'SELECT ROW_NUMBER() OVER (ORDER BY Date) SN
  561. ,Date Date
  562. , ''Remittance Cancel(+)'' Remarks
  563. ,cAmt [Collection Amt]
  564. ,serviceCharge [Total Charge]
  565. ,PayoutAmt,sAgentComm [Agt Comm]
  566. ,ROUND(ISNULL(margin,0),4) margin
  567. ,(cAmt-sAgentComm-ISNULL(margin,0)) [Sett. Amount]
  568. FROM(
  569. SELECT X.Date,(X.cAmt) cAmt,(X.serviceCharge) serviceCharge,(X.[PayoutAmt]) [PayoutAmt]
  570. ,(X.sAgentComm) sAgentComm
  571. ,((settleAmt)-(payAmt))/([agentSettelRate]) [Ex.Gain]
  572. ,(X.cAmt)-(X.sAgentComm) [Sett. Amount]
  573. ,(margin) margin
  574. FROM (
  575. SELECT createdDate [Date],(cAmt) cAmt,serviceCharge,
  576. (cAmt-serviceCharge) [PayoutAmt],sAgentComm
  577. ,agentCrossSettRate [agentSettelRate]
  578. ,(agentCrossSettRate*tAmt) settleAmt
  579. ,(customerRate*tAmt) payAmt
  580. ,ISNULL(((agentCrossSettRate*tAmt)-(customerRate*tAmt))/agentCrossSettRate,0) [margin]
  581. FROM RemitTran RT WITH (NOLOCK)
  582. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  583. WHERE cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  584. IF @sBranch IS NOT NULL
  585. SET @SQL = @SQL +' AND sBranch='''+@sBranch+''''
  586. IF @pCountry <>'all'
  587. SET @SQL = @SQL +' AND pCountry='''+@pCountry+''''
  588. IF @pAgentId IS NOT NULL
  589. SET @SQL = @SQL +' AND pAgent='''+@pAgentId+''''
  590. SET @SQL = @SQL +' ) X
  591. ) Y '
  592. PRINT @SQL
  593. EXEC(@SQL)
  594. END
  595. IF @flag='SettlementReport_Paid'
  596. BEGIN
  597. SET @SQL = 'SELECT ROW_NUMBER() OVER (ORDER BY Date) SN
  598. ,Date Date
  599. , ''Remittance Paid(+)'' Remarks
  600. ,cAmt [Collection Amt]
  601. ,serviceCharge [Total Charge]
  602. ,PayoutAmt,sAgentComm [Agt Comm]
  603. ,ROUND(ISNULL(margin,0),4) margin
  604. ,(cAmt-sAgentComm-ISNULL(margin,0)) [Sett. Amount]
  605. FROM(
  606. SELECT X.Date,(X.cAmt) cAmt,(X.serviceCharge) serviceCharge,(X.[PayoutAmt]) [PayoutAmt]
  607. ,(X.sAgentComm) sAgentComm
  608. ,((settleAmt)-(payAmt))/([agentSettelRate]) [Ex.Gain]
  609. ,(X.cAmt)-(X.sAgentComm) [Sett. Amount]
  610. ,(margin) margin
  611. FROM (
  612. SELECT createdDate [Date],(cAmt) cAmt,serviceCharge,
  613. (cAmt-serviceCharge) [PayoutAmt],sAgentComm
  614. ,agentCrossSettRate [agentSettelRate]
  615. ,(agentCrossSettRate*tAmt) settleAmt
  616. ,(customerRate*tAmt) payAmt
  617. ,ISNULL(((agentCrossSettRate*tAmt)-(customerRate*tAmt))/agentCrossSettRate,0) [margin]
  618. FROM RemitTran RT WITH (NOLOCK)
  619. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  620. WHERE paidDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  621. IF @sBranch IS NOT NULL
  622. SET @SQL = @SQL +' AND sBranch='''+@sBranch+''''
  623. IF @pCountry <>'all'
  624. SET @SQL = @SQL +' AND pCountry='''+@pCountry+''''
  625. IF @pAgentId IS NOT NULL
  626. SET @SQL = @SQL +' AND pAgent='''+@pAgentId+''''
  627. SET @SQL = @SQL +' ) X
  628. ) Y '
  629. PRINT @SQL
  630. EXEC(@SQL)
  631. END
  632. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  633. SELECT 'Branch' head,(SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentid=@sBranch) VALUE
  634. UNION ALL
  635. SELECT 'Beneficiary' head,isnull(@pCountry,'All') VALUE
  636. UNION ALL
  637. SELECT 'Agent' head,ISNULL((SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentid=ISNULL(@pAgentId,@sAgent)),'ALL') VALUE
  638. UNION ALL
  639. SELECT 'Transaction Report ' head, CASE WHEN @DateType='createdDate' THEN 'By TRN Date'
  640. WHEN @DateType='approvedDate' THEN 'By Confirm Date'
  641. WHEN @DateType='PaidDate' THEN 'By Paid Date' END value
  642. UNION ALL
  643. SELECT 'From Date' head,@fromDate value
  644. UNION ALL
  645. SELECT 'To Date' head, @toDate
  646. SELECT 'Transaction Summary Report : '+@flag title
  647. GO