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.

547 lines
40 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_GetOverseasTxnSummaryRpt] 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 procEDURE [dbo].[proc_GetOverseasTxnSummaryRpt]
  9. @flag VARCHAR(50),
  10. @user VARCHAR(50) = NULL,
  11. @sBranch VARCHAR(10) = NULL,
  12. @sAgent VARCHAR(10) = NULL,
  13. @pCountry VARCHAR(50) = NULL,
  14. @pAgentId VARCHAR(10) = NULL,
  15. @status VARCHAR(50) = NULL,
  16. @DateType VARCHAR(20) = NULL,
  17. @fromDate VARCHAR(10) = NULL,
  18. @toDate VARCHAR(10) = NULL,
  19. @countryBankId VARCHAR(10) = NULL,
  20. @pageNumber INT = NULL,
  21. @pageSize INT = NULL
  22. AS
  23. SET NOCOUNT ON;
  24. IF(DATEDIFF(D,@fromDate,GETDATE())>90 )
  25. BEGIN
  26. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  27. EXEC proc_errorHandler '1', '<font color="red"><b>Date Rage is not valid, You can only view transaction upto 90 days.</b></font>', NULL
  28. RETURN;
  29. END
  30. IF(DATEDIFF(D,@fromDate,@toDate))>32
  31. BEGIN
  32. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  33. EXEC proc_errorHandler '1', '<font color="red"><b>Date Rage is not valid, Please select date range of 32 days.</b></font>', NULL
  34. RETURN;
  35. END
  36. IF OBJECT_ID('tempdb..#listBranch') IS NOT NULL
  37. DROP TABLE #listBranch
  38. DECLARE @SQL VARCHAR(MAX),@userType varchar(2),@regionalBranchId INT,@branchId INT
  39. CREATE TABLE #listBranch (branchId INT,branchName VARCHAR(200))
  40. IF @userType IS NULL
  41. SELECT @userType=usertype,@regionalBranchId=agentId
  42. FROM applicationUsers WITH(NOLOCK) WHERE userName=@user
  43. IF @userType = 'RH'
  44. BEGIN
  45. INSERT INTO #listBranch
  46. SELECT DISTINCT b.agentId branchId, b.agentName branchName
  47. FROM (
  48. SELECT
  49. am.agentId
  50. ,am.agentName
  51. FROM agentMaster am WITH(NOLOCK)
  52. INNER JOIN regionalBranchAccessSetup rba WITH(NOLOCK) ON am.agentId = rba.memberAgentId
  53. WHERE rba.agentId = @regionalBranchId
  54. AND ISNULL(rba.isDeleted, 'N') = 'N'
  55. AND ISNULL(rba.isActive, 'N') = 'Y'
  56. AND am.isInternal ='Y'
  57. AND am.agentType = '2904'
  58. UNION ALL
  59. SELECT agentId, agentName
  60. FROM agentMaster WITH(NOLOCK) WHERE agentId = @regionalBranchId
  61. ) b WHERE b.agentId=ISNULL(@sBranch,b.agentId)
  62. ORDER BY agentName ASC
  63. END
  64. ELSE IF @userType ='HO'
  65. BEGIN
  66. INSERT INTO #listBranch
  67. SELECT b.agentId branchId, b.agentName branchName
  68. FROM agentMaster a WITH(NOLOCK)
  69. INNER JOIN agentMaster b WITH(NOLOCK) ON b.parentId = a.agentId
  70. WHERE ISNULL(b.isDeleted, 'N') <> 'Y'
  71. AND b.agentType = '2904'
  72. AND ISNULL(a.isActive, 'N') = 'Y'
  73. AND b.agentId = @sBranch
  74. --AND a.agentId = @sAgent
  75. END
  76. ELSE IF @userType ='AH'
  77. BEGIN
  78. INSERT INTO #listBranch
  79. SELECT DISTINCT A.agentId,A.agentName
  80. FROM agentMaster A WITH(NOLOCK)
  81. INNER JOIN applicationUsers U WITH (NOLOCK) ON A.agentId = U.agentId
  82. WHERE parentId = (SELECT parentId FROM agentMaster B WITH (NOLOCK)
  83. INNER JOIN applicationUsers AU WITH (NOLOCK) ON B.agentId=AU.agentId WHERE AU.userName = @user)
  84. END
  85. ELSE
  86. BEGIN
  87. INSERT INTO #listBranch
  88. SELECT agentId , agentName
  89. FROM agentMaster a WITH(NOLOCK) WHERE agentId = @regionalBranchId
  90. END
  91. IF @flag = 'Detail'
  92. BEGIN
  93. create table #SettlementReport (remarks VARCHAR(100),qty INT,settAmount MONEY,collCurr VARCHAR(5),sn INT)
  94. SET @SQL = '
  95. INSERT INTO #SettlementReport
  96. SELECT ''Remittance Send(+)'' remarks
  97. ,COUNT(*) QTY
  98. ,SUM(camt) - SUM(ISNULL(sAgentComm,0)) - SUM(ISNULL(agentFxGain,0)) [settAmt]
  99. ,collCurr Currency
  100. ,1 sn
  101. FROM vwRemitTran Rt
  102. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  103. WHERE approvedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  104. GROUP BY collCurr
  105. UNION ALL
  106. SELECT ''Remittance Paid(-)'' remarks
  107. ,COUNT(*) QTY
  108. ,[settAmt] = SUM(pamt) + SUM((ISNULL(pAgentComm,0) / (sCurrCostRate+ISNULL(sCurrHoMargin,0)))*ISNULL(pDateCostRate, (pCurrCostRate - ISNULL(pCurrHoMargin,0))))
  109. ,payoutCurr Currency
  110. , 2 sn
  111. FROM vwRemitTran Rt
  112. INNER JOIN #listBranch T ON RT.pBranch=T.branchId
  113. WHERE paidDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  114. GROUP BY payoutCurr
  115. UNION ALL
  116. SELECT ''Remittance Cancel(-)'' remarks
  117. ,COUNT(*) QTY
  118. ,[settAmt] = SUM(camt) - SUM(ISNULL(sAgentComm,0)) - SUM(ISNULL(agentFxGain,0))
  119. ,collCurr Currency
  120. , 3 sn
  121. FROM vwRemitTran Rt
  122. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  123. WHERE cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  124. GROUP BY collCurr
  125. '
  126. PRINT @SQL
  127. EXEC(@SQL)
  128. DECLARE @CNT INT=2,@holdAmt MONEY,@process MONEY,@setCurr VARCHAR(5)
  129. select @holdAmt=settAmount,@setCurr=COLLCURR from #SettlementReport where SN=1
  130. WHILE @CNT<=3
  131. BEGIN
  132. select @process=settAmount from #SettlementReport where sn=@CNT
  133. SET @holdAmt = @holdAmt -ISNULL(@process,0)
  134. SET @CNT = @CNT+1
  135. END
  136. SELECT Remarks,Qty,settAmount [Settlement Amount],COLLCURR [Settlement Curr] FROM (
  137. SELECT remarks,QTY,settAmount,COLLCURR,SN FROM #SettlementReport
  138. UNION ALL
  139. SELECT 'Net Settlement' ,0,@holdAmt,@setCurr,10
  140. ) X ORDER BY SN
  141. -->>SUMMARY REPORT
  142. SET @SQL ='SELECT
  143. [TRN Date] = CAST(CAST(RT.createdDate AS DATE) AS VARCHAR)
  144. ,[Approved Date] = CAST(CAST(RT.approvedDate AS DATE)AS VARCHAR)
  145. ,[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 </span></b>'' ELSE ''<b><span style="color:red;">Not Same Day Confirmed </span></b>'' END
  146. ,[No of Txn.] = COUNT(*)
  147. ,[Collected Amt(LCY)]= SUM(cAmt)
  148. FROM vwremitTran RT WITH (NOLOCK)
  149. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  150. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  151. IF @sBranch IS NOT NULL
  152. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  153. IF @pCountry <>'all'
  154. SET @SQL = @SQL +' AND pCountry='''+@pCountry+''''
  155. IF @pAgentId IS NOT NULL
  156. SET @SQL = @SQL +' AND pAgent='''+@pAgentId+''''
  157. IF @status IS NOT NULL
  158. SET @SQL = @SQL +' AND payStatus IN ('''+@status+''')'
  159. ELSE
  160. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  161. SET @SQL = @SQL +' GROUP BY CAST(RT.createdDate AS DATE),CAST(RT.approvedDate AS DATE)'
  162. PRINT @SQL
  163. EXEC(@SQL)
  164. END
  165. IF @flag = 'BranchWise'
  166. BEGIN
  167. SET @SQL =' SELECT
  168. [Agent] = sAgentName
  169. ,[Branch] = sBranchName
  170. ,[NOs] = COUNT(*)
  171. ,[Total <BR/>Collected(LCY)] = SUM(RT.cAmt)
  172. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  173. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  174. ,[Total <BR/>Payable(LCY)] = SUM(RT.tAmt)
  175. FROM vwremitTran RT WITH (NOLOCK)
  176. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  177. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  178. IF @pCountry <>'all'
  179. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  180. IF @sBranch IS NOT NULL
  181. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  182. IF @pAgentId IS NOT NULL
  183. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  184. IF @status IS NOT NULL
  185. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  186. ELSE
  187. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  188. SET @SQL = @SQL +' GROUP BY RT.sAgentName,RT.sBranchName'
  189. PRINT @SQL
  190. EXEC(@SQL)
  191. SET @SQL =' SELECT [Agent] = sAgentName
  192. ,[Branch] = sBranchName
  193. ,[NOs] = COUNT(*)
  194. ,[Total <BR/>Collected(LCY)] = SUM(RT.cAmt)
  195. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  196. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  197. ,[Total Cancel <BR/>To Agent(LCY)]= SUM(X.tAmt)
  198. FROM vwremitTran RT WITH (NOLOCK)
  199. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  200. LEFT JOIN
  201. (
  202. SELECT SUM(tAmt) tAmt,sBranch FROM vwremitTran WITH (NOLOCK)
  203. WHERE sBranch = '''+@sBranch+''' AND payStatus=''Cancel''
  204. GROUP BY sBranch
  205. )X ON X.sBranch = RT.sBranch
  206. WHERE RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' AND RT.payStatus IN (''Cancel'')'
  207. IF @sBranch IS NOT NULL
  208. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  209. IF @pCountry <>'all'
  210. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  211. IF @pAgentId IS NOT NULL
  212. SET @SQL = @SQL +' AND (RT.pAgent='''+@pAgentId+''' OR rt.pAgent IS NULL)'
  213. SET @SQL = @SQL +' GROUP BY RT.sAgentName,RT.sBranchName'
  214. SET @SQL =@SQL+ ' UNION ALL
  215. SELECT sAgentName [Agent]
  216. ,sBranchName [Branch]
  217. ,COUNT(*) [NOs]
  218. ,SUM(RT.cAmt)[Total <BR/>Collected(LCY)]
  219. ,ISNULL(SUM(RT.sAgentComm),0) [Agent <BR/>Comm(LCY)]
  220. ,SUM(RT.serviceCharge) [Total <BR/>SCharge(LCY)]
  221. ,SUM(X.tAmt) [Total Cancel <BR/>To Agent(LCY)]
  222. FROM cancelTranHistory RT WITH (NOLOCK)
  223. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  224. LEFT JOIN
  225. (
  226. SELECT SUM(tAmt) tAmt,sBranch FROM cancelTranHistory WITH (NOLOCK)
  227. WHERE sBranch = '''+@sBranch+''' AND payStatus=''Unpaid''
  228. AND cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  229. GROUP BY sBranch
  230. )X ON X.sBranch = RT.sBranch
  231. WHERE RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' AND RT.payStatus IN (''Unpaid'')'
  232. IF @sBranch IS NOT NULL
  233. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  234. IF @pCountry <>'all'
  235. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  236. IF @pAgentId IS NOT NULL
  237. SET @SQL = @SQL +' AND (RT.pAgent='''+@pAgentId+''' OR rt.pAgent IS NULL)'
  238. SET @SQL = @SQL +' GROUP BY RT.sAgentName,RT.sBranchName'
  239. PRINT @SQL
  240. EXEC(@SQL)
  241. END
  242. IF @flag = 'ReceivingAgentCountryWise'
  243. BEGIN
  244. SET @SQL = 'SELECT [Agent Name]=
  245. ''<a href="Reports.aspx?reportName=40112500txnsummaryrpt&pCountry=''+RT.pCountry+''&sAgent='+ @sAgent+'&sBranch='+ ISNULL(@sBranch,'') +'&dateType='+@DateType+'&fromDate='+@fromDate+'&toDate='+@toDate+'&rptType=ReceivingAgentWise&status='+ISNULL(@status,'')+'"> ''+RT.pCountry+'' </a>''
  246. ,[Total <BR/>Send TRN] = COUNT(*)
  247. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  248. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  249. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  250. ,[Rec.<br/> Curr] = RT.payoutCurr
  251. ,[Total Payable <BR/>to Agent(LCY)] = ISNULL(SUM(RT.tAmt),0)
  252. FROM vwremitTran RT WITH (NOLOCK)
  253. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  254. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  255. IF @sBranch IS NOT NULL
  256. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  257. IF @pCountry <>'all'
  258. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  259. IF @pAgentId IS NOT NULL
  260. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  261. IF @status IS NOT NULL
  262. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  263. ELSE
  264. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  265. SET @SQL = @SQL +' GROUP BY RT.pCountry,RT.payoutCurr'
  266. PRINT @SQL
  267. EXEC(@SQL)
  268. END
  269. IF @flag = 'ReceivingAgentWise'
  270. BEGIN
  271. SET @SQL = 'SELECT [Country Name]=
  272. ''<a href="Reports.aspx?reportName=40112500txnsummaryrpt&pCountry=''+RT.pCountry+''&sBranch='+ ISNULL(@sBranch,'') +'&dateType='+@DateType+'&fromDate='+@fromDate+'&toDate='+@toDate+'&rptType=ReceivingAgentDetail&pAgent=''+CAST(ISNULL(pAgent,'''') AS VARCHAR)+''&status='+ISNULL(@status,'')+'"> ''+ISNULL(RT.pAgentName,''Anywhere'')+''-''+RT.pCountry+'' </a>''
  273. ,[Total <BR/>Send TRN] = COUNT(*)
  274. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  275. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  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 @sBranch IS NOT NULL
  283. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  284. IF @pCountry <>'all'
  285. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  286. IF @pAgentId IS NOT NULL
  287. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  288. IF @status IS NOT NULL
  289. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  290. ELSE
  291. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  292. SET @SQL = @SQL +' GROUP BY pAgentName,pAgent,RT.pCountry,RT.payoutCurr ORDER BY RT.pCountry'
  293. PRINT @SQL
  294. EXEC(@SQL)
  295. END
  296. IF @flag = 'ReceivingAgentDetail'
  297. BEGIN
  298. SET @SQL = 'SELECT
  299. [TRN Date] = ''<a href="Reports.aspx?reportName=40112500txnsummaryrpt&pCountry='+@pCountry+'&dateType='+@DateType+'&fromDate=''+CAST(CAST(RT.createdDate AS DATE) AS VARCHAR)+''&toDate=''+CAST(CAST(RT.createdDate AS DATE) AS VARCHAR)+''&rptType=ReceivingDateWise&sBranch='+ ISNULL(@sBranch,'') +'&pAgent=''+CAST(ISNULL(pAgent,0) AS VARCHAR)+''&status='+ISNULL(@status,'')+'"> ''+CAST(CAST(RT.createdDate AS DATE) AS VARCHAR)+'' </a>''
  300. ,[Agent Name] = ISNULL(RT.pAgentName,''Anywhere-''+'''+@pCountry+''')
  301. ,[Total <BR/>Send TRN] = COUNT(*)
  302. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  303. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  304. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  305. ,[Customer Rate] = RT.customerRate
  306. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  307. ,[Rec.<br/> Curr] = RT.payoutCurr
  308. ,[Total Payable <BR/>to Agent(LCY)] = ISNULL(SUM(RT.tAmt),0)
  309. FROM vwremitTran RT WITH (NOLOCK)
  310. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  311. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
  312. IF @sAgent IS NOT NULL AND @sAgent > 0
  313. SET @SQL = @SQL +' AND RT.sAgent = '''+ @sAgent+''''
  314. IF @sBranch IS NOT NULL
  315. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  316. IF @pCountry <>'all'
  317. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  318. IF @pAgentId IS NOT NULL AND @pAgentId > 0
  319. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  320. IF @pAgentId =0
  321. SET @SQL = @SQL +' AND RT.pAgent IS NULL'
  322. IF @status IS NOT NULL
  323. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  324. ELSE
  325. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  326. SET @SQL = @SQL +' GROUP BY pAgentName,CAST(CAST(RT.createdDate AS DATE) AS VARCHAR),RT.customerRate
  327. ,RT.pAgent,RT.payoutCurr'
  328. EXEC(@SQL)
  329. --Cancel datewise trn
  330. SET @SQL = 'SELECT [TRN Date(CancelDate)] = CAST(CAST(RT.cancelApprovedDate AS DATE) AS VARCHAR)
  331. ,[Agent Name] = RT.pAgentName
  332. ,[Total <BR/>Cancel TRN] = COUNT(*)
  333. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  334. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  335. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  336. ,[ExRate] = RT.customerRate
  337. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  338. ,[Rec.<br/> Curr] = RT.payoutCurr
  339. ,[Total Cancel <BR/>to Agent] = ISNULL(SUM(X.tAmt),0)
  340. FROM vwremitTran RT WITH (NOLOCK)
  341. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  342. LEFT JOIN (
  343. SELECT ISNULL(SUM(tAmt),0) tAmt,sBranch FROM vwremitTran WITH (NOLOCK)
  344. WHERE payStatus=''Cancel''
  345. GROUP BY sBranch
  346. )X ON X.sBranch = RT.sBranch
  347. WHERE
  348. RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' AND RT.payStatus IN (''Cancel'')'
  349. IF @sAgent IS NOT NULL AND @sAgent > 0
  350. SET @SQL = @SQL +' AND RT.sAgent = '''+@sAgent+''''
  351. IF @sBranch IS NOT NULL
  352. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  353. IF @pCountry <>'all'
  354. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  355. IF @pAgentId IS NOT NULL AND @pAgentId > 0
  356. SET @SQL = @SQL +' AND (RT.pAgent='''+@pAgentId+''' OR RT.pAgent IS NULL)'
  357. IF @pAgentId =0
  358. SET @SQL = @SQL +' AND RT.pAgent IS NULL'
  359. SET @SQL = @SQL +' GROUP BY pAgentName,CAST(RT.cancelApprovedDate AS DATE),RT.customerRate
  360. ,RT.payoutCurr'
  361. -------------------------------------------------------
  362. SET @SQL =@SQL+ ' UNION ALL
  363. SELECT [TRN Date(CancelDate)] = CAST(CAST(RT.cancelApprovedDate AS DATE) AS VARCHAR)
  364. ,[Agent Name] = ISNULL(RT.pAgentName,''Anywhere-''+rt.pcountry)
  365. ,[Total <BR/>Cancel TRN] = COUNT(*)
  366. ,[Total Collection <BR/>Amount(LCY)] = SUM(RT.cAmt)
  367. ,[Total <BR/>SCharge(LCY)] = SUM(RT.serviceCharge)
  368. ,[Agent <BR/>Comm(LCY)] = ISNULL(SUM(RT.sAgentComm),0)
  369. ,[ExRate] = RT.customerRate
  370. ,[Customer <BR/>Rec. Amount] = SUM(RT.pAmt)
  371. ,[Rec.<br/> Curr] = RT.payoutCurr
  372. ,[Total Cancel <BR/>to Agent] = ISNULL(SUM(X.tAmt),0)
  373. FROM cancelTranHistory RT WITH (NOLOCK)
  374. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  375. LEFT JOIN (
  376. SELECT ISNULL(SUM(tAmt),0) tAmt,sBranch FROM cancelTranHistory WITH (NOLOCK)
  377. WHERE payStatus=''Unpaid''
  378. AND cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59''
  379. GROUP BY sBranch
  380. )X ON X.sBranch = RT.sBranch
  381. WHERE RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' AND RT.payStatus IN (''Unpaid'')'
  382. IF @sAgent IS NOT NULL AND @sAgent > 0
  383. SET @SQL = @SQL +' AND RT.sAgent = '''+@sAgent+''''
  384. IF @sBranch IS NOT NULL
  385. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  386. IF @pCountry <>'all'
  387. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  388. IF @pAgentId IS NOT NULL AND @pAgentId > 0
  389. SET @SQL = @SQL +' AND (RT.pAgent='''+@pAgentId+''' OR RT.pAgent IS NULL)'
  390. IF @pAgentId =0
  391. SET @SQL = @SQL +' AND RT.pAgent IS NULL'
  392. SET @SQL = @SQL +' GROUP BY pAgentName,CAST(RT.cancelApprovedDate AS DATE),RT.customerRate,rt.pcountry
  393. ,RT.payoutCurr'
  394. --PRINT @SQL
  395. EXEC(@SQL)
  396. END
  397. IF @flag = 'ReceivingDateWise'
  398. BEGIN
  399. SET @SQL ='
  400. SELECT
  401. [ICN] = DBO.FNADecryptstring(RT.controlNo)
  402. ,[Sender Name] = RT.senderName
  403. ,[Receiver Name] = RT.receiverName
  404. ,[Tran Status] = CASE WHEN RT.tranStatus=''Payment'' THEN ''Unpaid'' ELSE RT.tranStatus END
  405. ,[DOT/Paid Date] = CONVERT(VARCHAR,RT.createdDate,101)+ISNULL(''/''+CONVERT(VARCHAR,RT.paidDate,101),'''')
  406. ,[ExRate] = RT.customerRate
  407. ,[Total Collection <BR/>Amount(LCY)] = CAST(RT.cAmt AS VARCHAR)
  408. ,[Total Sent <BR/>Amount(LCY)] = CAST(RT.tAmt AS VARCHAR)
  409. ,[Charge<BR/>(MYR)] = CAST(RT.serviceCharge AS VARCHAR)
  410. ,[Customer <BR/>Rec. Amount] = CAST(RT.pAmt AS VARCHAR)
  411. ,[Rec.<br/> Curr] = RT.payoutCurr
  412. ,[User Name] = RT.createdBy
  413. FROM vwremitTran RT WITH (NOLOCK)
  414. INNER JOIN #listBranch T ON RT.sBranch=T.branchId
  415. WHERE RT.'+@DateType+' BETWEEN '''+@fromDate+''' AND '''+@fromDate+' 23:59:59'''
  416. IF @sBranch IS NOT NULL
  417. SET @SQL = @SQL +' AND RT.sBranch='''+@sBranch+''''
  418. IF @pAgentId = 0
  419. SET @SQL = @SQL +' AND RT.pAgent IS NULL'
  420. IF @pCountry <>'all'
  421. SET @SQL = @SQL +' AND RT.pCountry='''+@pCountry+''''
  422. IF @pAgentId IS NOT NULL AND @pAgentId > 0
  423. SET @SQL = @SQL +' AND RT.pAgent='''+@pAgentId+''''
  424. IF @status IS NOT NULL
  425. SET @SQL = @SQL +' AND RT.payStatus IN ('''+@status+''')'
  426. ELSE
  427. SET @SQL = @SQL +' AND RT.payStatus IN (''Unpaid'',''Paid'',''Post'')'
  428. PRINT @SQL
  429. EXEC(@SQL)
  430. END
  431. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  432. SELECT 'Beneficiary' head,isnull(@pCountry,'All') VALUE
  433. UNION ALL
  434. SELECT 'Sending Agent' head,ISNULL((SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentid=@sAgent),'ALL') VALUE
  435. UNION ALL
  436. SELECT 'Sending Branch' head,(SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentid=@sBranch) VALUE
  437. UNION ALL
  438. SELECT 'Date Type ' head, CASE WHEN @DateType='createdDate' THEN 'By TRN Date'
  439. WHEN @DateType='approvedDate' THEN 'By Confirm Date'
  440. WHEN @DateType='PaidDate' THEN 'By Paid Date' END value
  441. UNION ALL
  442. SELECT 'From Date' head,@fromDate value
  443. UNION ALL
  444. SELECT 'To Date' head, @toDate
  445. SELECT 'Transaction Summary Report : '+@flag title
  446. GO