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.

557 lines
22 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_customerReport] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_customerReport]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_customerReport] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. --EXEC proc_customerReport @flag = 'registration-rpt',@startDate = '10/01/2017',@endDate = '12/31/2018',@user = 'admin',@country = null,@branch = NULL
  12. CREATE PROC [dbo].[proc_customerReport]
  13. (
  14. @flag VARCHAR(50)
  15. ,@startDate VARCHAR(30) = NULL
  16. ,@endDate VARCHAR(30) = NULL
  17. ,@user VARCHAR(100) = NULL
  18. ,@country VARCHAR(50) = NULL
  19. ,@branch VARCHAR(80) = NULL
  20. ,@searchType VARCHAR(20) = NULL
  21. )
  22. AS
  23. SET NOCOUNT ON;
  24. SET XACT_ABORT ON;
  25. BEGIN
  26. IF @flag = 'registration-rpt'
  27. BEGIN
  28. SELECT [BRANCH NAME] = agentName,
  29. [NATIVE COUNTRY] = C.countryName,
  30. [TOTAL REGISTERED CUSTOMERS] = '<a href="#" onclick="OpenInNewWindow(''/RemittanceSystem/RemittanceReports/Reports.aspx?reportName=customerdetailreport&startDate=' + @startDate + '&endDate='+ @endDate +
  31. '&country=' + CAST(ISNULL(CM.nativeCountry, '') AS VARCHAR) + '&branch=' + CAST(ISNULL(X.agentId, '') as varchar)+'&flag=detail-customer'')">' + CAST(COUNT(1) AS VARCHAR) + '</a>'
  32. FROM customerMaster CM(NOLOCK)
  33. LEFT JOIN (
  34. SELECT AU.userName, AM.agentName, AM.agentId FROM agentMaster AM(NOLOCK)
  35. INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  36. WHERE AM.agentId = ISNULL(@branch, AM.agentId)
  37. )X ON X.userName = CM.verifiedBy
  38. INNER JOIN countryMaster C(NOLOCK) ON C.countryId = CM.nativeCountry
  39. WHERE CM.approvedBy IS NOT NULL
  40. AND X.agentName IS NOT NULL
  41. AND CM.nativeCountry = ISNULL(@country, CM.nativeCountry)
  42. AND CM.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  43. GROUP BY X.agentName, C.countryName, X.agentId, C.countryName, CM.nativeCountry
  44. ORDER BY C.countryName
  45. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  46. select 'Country' head, CASE WHEN @country IS NULL THEN 'All' ELSE (SELECT countryName FROM countryMaster (NOLOCK) WHERE countryId = @country) END UNION ALL
  47. select 'Branch' head, ISNULL((SELECT agentName FROM agentMaster (NOLOCK) WHERE agentId = @branch), 'All') union all-- CASE WHEN @branch IS NULL THEN 'All' ELSE (SELECT agentName FROM agentMaster (NOLOCK) WHERE agentId = @branch) END UNION ALL
  48. SELECT 'From Date' head, @startDate value union all
  49. SELECT 'To Date' head, @endDate value
  50. SELECT 'Customer Registration Report' title
  51. END
  52. ELSE IF @flag = 'detail-customer'
  53. BEGIN
  54. SELECT [CUSTOMER NAME] = firstName,
  55. [EMAIL] = email,
  56. [REGISTERED DATE] = CM.createdDate,
  57. [APPROVED DATE] = CM.verifiedDate,
  58. [BANK NAME] = KB.BankName,
  59. [BANK ACCOUNT NUMBER] = CM.bankAccountNo,
  60. [WALLET ACCOUNT NUMBER] = CM.walletAccountNo,
  61. [AVAILBALE BALANCE] = CM.availableBalance
  62. FROM customerMaster CM(NOLOCK)
  63. LEFT JOIN (
  64. SELECT AU.userName, AM.agentName FROM agentMaster AM(NOLOCK)
  65. INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  66. WHERE AM.agentId = ISNULL(@branch, AM.agentId)
  67. )X ON X.userName = CM.verifiedBy
  68. INNER JOIN countryMaster C(NOLOCK) ON C.countryId = CM.nativeCountry
  69. INNER JOIN KoreanBankList KB(NOLOCK) ON KB.rowId = CM.bankName
  70. WHERE CM.nativeCountry = ISNULL(@country, CM.nativeCountry)
  71. AND X.agentName IS NOT NULL
  72. AND CM.approvedBy IS NOT NULL
  73. AND CM.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  74. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  75. select 'Country' head, CASE WHEN @country IS NULL THEN 'All' ELSE (SELECT countryName FROM countryMaster (NOLOCK) WHERE countryId = @country) END UNION ALL
  76. select 'Branch' head, ISNULL((SELECT agentName FROM agentMaster (NOLOCK) WHERE agentId = @branch), 'All') union all-- CASE WHEN @branch IS NULL THEN 'All' ELSE (SELECT agentName FROM agentMaster (NOLOCK) WHERE agentId = @branch) END UNION ALL
  77. SELECT 'From Date' head, @startDate value union all
  78. SELECT 'To Date' head, @endDate value
  79. SELECT 'Customer Registration Detail Report' title
  80. END
  81. ELSE IF @flag = 'transaction-rpt'
  82. BEGIN
  83. DECLARE @MAINTABLE TABLE (AgentName VARCHAR(80), Country VARCHAR(50), Transactions INT)
  84. DECLARE @CUSTOMERTABLE TABLE (AgentName VARCHAR(80),customerId BIGINT)
  85. DECLARE @CUSTOMERTXNTABLE TABLE (AgentName VARCHAR(80), Transactions INT, Country VARCHAR(50))
  86. DECLARE @BRANCHTABLE TABLE (AgentName VARCHAR(80), Transactions INT, Country VARCHAR(50))
  87. INSERT INTO @CUSTOMERTABLE (AgentName, customerId)
  88. SELECT ISNULL(X.agentName, 'System HO'),CM.customerId
  89. FROM customerMaster CM(NOLOCK)
  90. LEFT JOIN (
  91. SELECT AU.userName, AM.agentName, AM.agentId FROM agentMaster AM(NOLOCK)
  92. INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  93. WHERE AM.agentId = ISNULL(@branch, AM.agentId)
  94. )X ON X.userName = CM.approvedBy
  95. WHERE CM.approvedBy IS NOT NULL
  96. --AND X.agentName IS NOT NULL
  97. AND CM.availableBalance IS NOT NULL
  98. --GROUP BY X.agentName, CM.customerId
  99. INSERT INTO @CUSTOMERTXNTABLE (AgentName, Transactions, Country)
  100. SELECT CT.AgentName, COUNT(1) Transactions, X.pCountry FROM @CUSTOMERTABLE CT
  101. LEFT JOIN (
  102. SELECT TS.customerId, RT.pCountry, RT.approvedDate
  103. FROM remitTran RT(NOLOCK)
  104. INNER JOIN tranSenders TS(NOLOCK) ON TS.tranId = RT.id
  105. INNER JOIN countryMaster CM(NOLOCK) ON CM.countryName = RT.pCountry
  106. WHERE RT.tranType IN('O','M') and RT.tranStatus <> 'cancel'
  107. AND CM.countryId = ISNULL(@country, CM.countryId)
  108. ) X ON X.customerId = CT.customerId
  109. WHERE X.customerId IS NOT NULL
  110. AND X.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  111. GROUP BY X.pCountry, CT.AgentName
  112. INSERT INTO @BRANCHTABLE (AgentName, Transactions, Country)
  113. SELECT X.agentName, COUNT(1) Transactions, C.countryName
  114. FROM remitTran RT(NOLOCK)
  115. LEFT JOIN (
  116. SELECT AU.userName, AM.agentName FROM agentMaster AM(NOLOCK)
  117. INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  118. WHERE AM.agentId = ISNULL(@branch, AM.agentId)
  119. )X ON X.userName = RT.createdBy
  120. INNER JOIN countryMaster C(NOLOCK) ON C.countryName = RT.pCountry
  121. WHERE C.countryId = ISNULL(@country, C.countryId)
  122. AND X.agentName IS NOT NULL
  123. AND RT.tranType = 'I' and RT.tranStatus <> 'cancel'
  124. AND RT.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  125. GROUP BY X.agentName, C.countryName
  126. --SELECT * FROM @CUSTOMERTXNTABLE
  127. --SELECT * FROM @BRANCHTABLE
  128. INSERT INTO @MAINTABLE (AgentName, Transactions, Country)
  129. SELECT DISTINCT AgentName, Transactions, Country FROM @CUSTOMERTXNTABLE UNION ALL
  130. SELECT DISTINCT AgentName, Transactions, Country FROM @BRANCHTABLE
  131. SELECT [BRANCH NAME] = MT.AgentName,
  132. [NATIVE COUNTRY] = MT.Country,
  133. [TOTAL TRANSACTIONS] = '<a href="#" onclick="OpenInNewWindow(''/RemittanceSystem/RemittanceReports/Reports.aspx?reportName=customerdetailreport&startDate=' + @startDate + '&endDate='+ @endDate +
  134. '&country=' + CAST(ISNULL(Country, '') AS VARCHAR) + '&branch=' + CAST(ISNULL(AM.agentId, '') AS VARCHAR)+'&flag=detail-tran-customer'')">' + CAST(SUM(Transactions) AS VARCHAR) + '</a>'
  135. FROM @MAINTABLE MT
  136. INNER JOIN agentMaster AM(NOLOCK) ON AM.agentName = MT.AgentName
  137. GROUP BY MT.AgentName, MT.Country, AM.agentId
  138. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  139. SELECT 'Country' head, CASE WHEN @country IS NULL THEN 'All' ELSE (SELECT countryName FROM countryMaster (NOLOCK) WHERE countryId = @country) END UNION ALL
  140. SELECT 'Branch' head, ISNULL((SELECT agentName FROM agentMaster (NOLOCK) WHERE agentId = @branch), 'All') UNION ALL
  141. SELECT 'From Date' head, @startDate value UNION ALL
  142. SELECT 'To Date' head, @endDate value
  143. SELECT 'Transaction Report' title
  144. END
  145. ELSE IF @flag = 'detail-tran-customer'
  146. BEGIN
  147. DECLARE @CUSTOMERTBL TABLE (customerId VARCHAR(20))
  148. INSERT INTO @CUSTOMERTBL (customerId)
  149. SELECT CM.customerId
  150. FROM customerMaster CM(NOLOCK)
  151. LEFT JOIN (
  152. SELECT AU.userName, AM.agentName FROM agentMaster AM(NOLOCK)
  153. INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  154. WHERE AM.agentId = ISNULL(@branch, AM.agentId)
  155. )X ON X.userName = CM.verifiedBy
  156. WHERE X.agentName IS NOT NULL
  157. AND CM.approvedBy IS NOT NULL
  158. AND CM.availableBalance IS NOT NULL
  159. SELECT [GME CONTROLNO] = DBO.decryptDb(controlNo)
  160. ,[SENDER NAME] = senderName
  161. ,[RECEIVER NAME] = receiverName
  162. ,[TRANSACTION TYPE] = paymentMethod
  163. ,[COLL AMOUNT] = RT.cAmt
  164. ,[PAYOUT AMOUNT] = RT.pAmt
  165. ,[PAYOUT AGENT] = RT.pAgentName
  166. ,[PAYOUT BANK] = RT.pBankName
  167. FROM remitTran RT(NOLOCK)
  168. INNER JOIN tranSenders TS(NOLOCK) ON TS.tranId = RT.id
  169. INNER JOIN @CUSTOMERTBL C ON C.customerId = TS.customerId
  170. WHERE RT.pCountry = ISNULL(@country, RT.pCountry)
  171. AND RT.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  172. AND RT.tranStatus <> 'Cancel'
  173. AND RT.tranType IN('O','M')
  174. UNION ALL
  175. SELECT [GME CONTROLNO] = DBO.decryptDb(controlNo)
  176. ,[SENDER NAME] = senderName
  177. ,[RECEIVER NAME] = receiverName
  178. ,[TRANSACTION TYPE] = paymentMethod
  179. ,[COLL AMOUNT] = RT.cAmt
  180. ,[PAYOUT AMOUNT] = RT.pAmt
  181. ,[PAYOUT AGENT] = RT.pAgentName
  182. ,[PAYOUT BANK] = RT.pBankName
  183. FROM remitTran RT(NOLOCK)
  184. WHERE RT.pCountry = ISNULL(@country, RT.pCountry)
  185. AND RT.sAgent = ISNULL(@branch, RT.sAgent)
  186. AND RT.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  187. AND RT.tranStatus <> 'Cancel'
  188. AND RT.tranType = 'I'
  189. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  190. SELECT 'Country' head, ISNULL(@country, 'All') UNION ALL
  191. SELECT 'Branch' head, ISNULL((SELECT agentName FROM agentMaster (NOLOCK) WHERE agentId = @branch), 'All') UNION ALL
  192. SELECT 'From Date' head, @startDate value UNION ALL
  193. SELECT 'To Date' head, @endDate value
  194. SELECT 'Transaction Detail Report' title
  195. END
  196. ELSE IF @flag = 'register-matrix'
  197. BEGIN
  198. IF OBJECT_ID('tempdb..#TEMP') IS NOT NULL DROP TABLE #TEMP
  199. CREATE TABLE #TEMP (NAME VARCHAR(50), COUNTRY VARCHAR(50), QTY INT, AGENTID INT)
  200. INSERT INTO #TEMP (NAME , COUNTRY , QTY, AGENTID )
  201. SELECT [NAME] = agentName,
  202. [COUNTRY] = C.countryName,
  203. [QTY] = COUNT(1),
  204. AGENTID = X.agentId
  205. FROM customerMaster CM(NOLOCK)
  206. LEFT JOIN (
  207. SELECT AU.userName, AM.agentName, AM.agentId FROM agentMaster AM(NOLOCK)
  208. INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  209. WHERE AM.agentId = ISNULL(@branch, AM.agentId)
  210. )X ON X.userName = CM.approvedBy
  211. INNER JOIN countryMaster C(NOLOCK) ON C.countryId = CM.nativeCountry
  212. WHERE CM.approvedBy IS NOT NULL
  213. AND X.agentName IS NOT NULL
  214. AND CM.nativeCountry = ISNULL(@country, CM.nativeCountry)
  215. AND CM.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  216. GROUP BY X.agentName, C.countryName, X.agentId, C.countryName, CM.nativeCountry
  217. ORDER BY C.countryName
  218. IF NOT EXISTS(SELECT 1 FROM #TEMP)
  219. BEGIN
  220. SELECT SNO = 1, [ERROR_MESSAGE] = 'NO DATA FOUND FOR THIS FILTER'
  221. RETURN
  222. END
  223. DECLARE @columns NVARCHAR(MAX), @sql NVARCHAR(MAX);
  224. SET @columns = '';
  225. SELECT
  226. @columns += N', p.' + QUOTENAME(NAME)
  227. FROM (SELECT DISTINCT NAME FROM #TEMP) AS x;
  228. SET @columns = ', p.Country ' + @columns
  229. SET @sql = N'
  230. SELECT *
  231. FROM
  232. (
  233. SELECT NAME, COUNTRY, QTY = ISNULL(QTY, 0) FROM #TEMP
  234. ) AS j
  235. PIVOT
  236. (
  237. SUM(QTY) FOR NAME IN ('
  238. + STUFF(REPLACE(REPLACE(REPLACE(@columns, 'p.Country ,', ''), ', p.[', ',['), 'p.', ''), 1, 1, '')
  239. + ')
  240. ) AS p;';
  241. PRINT @sql;
  242. EXEC sp_executesql @sql;
  243. END
  244. ELSE IF @flag = 'matrix-detail'
  245. BEGIN
  246. SELECT [CUSTOMER NAME] = firstName,
  247. [EMAIL] = email,
  248. [REGISTERED DATE] = CM.createdDate,
  249. [APPROVED DATE] = CM.verifiedDate,
  250. [BANK NAME] = KB.BankName,
  251. [BANK ACCOUNT NUMBER] = CM.bankAccountNo,
  252. [WALLET ACCOUNT NUMBER] = CM.walletAccountNo,
  253. [AVAILBALE BALANCE] = CM.availableBalance
  254. FROM customerMaster CM(NOLOCK)
  255. LEFT JOIN (
  256. SELECT AU.userName, AM.agentName FROM agentMaster AM(NOLOCK)
  257. INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  258. WHERE AM.agentName = ISNULL(@branch, AM.agentName)
  259. )X ON X.userName = CM.verifiedBy
  260. INNER JOIN countryMaster C(NOLOCK) ON C.countryId = CM.nativeCountry
  261. INNER JOIN KoreanBankList KB(NOLOCK) ON KB.rowId = CM.bankName
  262. WHERE C.countryName = ISNULL(@country, C.countryName)
  263. AND X.agentName IS NOT NULL
  264. AND CM.approvedBy IS NOT NULL
  265. AND CM.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  266. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  267. select 'Country' head, @country UNION ALL
  268. select 'Branch' head, @branch UNION ALL
  269. SELECT 'From Date' head, @startDate value union all
  270. SELECT 'To Date' head, @endDate value
  271. SELECT 'Customer Registration Detail Report' title
  272. END
  273. ELSE IF @flag = 'trn-matrix'
  274. BEGIN
  275. SELECT RT.pCountry, COUNT(1) CNT, TS.customerId INTO #TEMPtrnmatrix
  276. FROM remitTran RT(NOLOCK)
  277. INNER JOIN tranSenders TS(NOLOCK) ON TS.tranId = RT.id
  278. WHERE RT.tranType IN('O','M') and RT.tranStatus <> 'cancel'
  279. AND RT.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  280. AND RT.pCountry = ISNULL(@country, RT.pCountry)
  281. GROUP BY RT.pCountry,TS.customerId
  282. --SELECT approvedBy,* FROM customerMaster(NOLOCK) WHERE customerId=45077
  283. ALTER TABLE #TEMPtrnmatrix ADD BranchName VARCHAR(100)
  284. --SELECT ISNULL(X.agentName, 'System HO'),CM.customerId
  285. --FROM customerMaster CM(NOLOCK)
  286. --INNER JOIN #TEMP T ON T.customerId = cm.customerId
  287. --LEFT JOIN (
  288. -- SELECT AU.userName, AM.agentName, AM.agentId FROM agentMaster AM(NOLOCK)
  289. -- INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  290. -- --WHERE AM.agentId = ISNULL(@branch, AM.agentId)
  291. --)X ON X.userName = CM.approvedBy
  292. --WHERE CM.approvedBy IS NOT NULL
  293. UPDATE T SET T.BranchName = ISNULL(X.agentName, 'System HO') FROM customerMaster CM(NOLOCK)
  294. INNER JOIN #TEMPtrnmatrix T ON T.customerId = cm.customerId
  295. LEFT JOIN (
  296. SELECT AU.userName, AM.agentName, AM.agentId FROM agentMaster AM(NOLOCK)
  297. INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  298. WHERE AM.agentId = ISNULL(@branch, AM.agentId)
  299. )X ON X.userName = CM.approvedBy
  300. WHERE CM.approvedBy IS NOT NULL
  301. UPDATE #TEMPtrnmatrix SET BRANCHNAME='Unknown Branch' WHERE BRANCHNAME IS NULL
  302. INSERT INTO #TEMPtrnmatrix (BRANCHNAME, CNT, pCountry)
  303. SELECT RT.sAgentName, COUNT(1) Transactions,RT.pCountry
  304. FROM remitTran RT(NOLOCK)
  305. WHERE 1=1
  306. AND RT.pCountry= ISNULL(@country, RT.pCountry)
  307. AND RT.sAgent = ISNULL(@branch, RT.sAgent)
  308. AND RT.tranType = 'I' and RT.tranStatus <> 'cancel'
  309. AND RT.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  310. GROUP BY RT.sAgentName,RT.pCountry
  311. --INSERT INTO @MAINTABLE (AgentName, Transactions, Country)
  312. --SELECT BRANCHNAME,SUM(CNT) CNT,pCountry FROM #TEMP GROUP BY BRANCHNAME,pCountry
  313. IF OBJECT_ID('tempdb..#TEMPTRN') IS NOT NULL DROP TABLE #TEMPTRN
  314. CREATE TABLE #TEMPTRN (NAME VARCHAR(50), COUNTRY VARCHAR(50), QTY INT)
  315. INSERT INTO #TEMPTRN (NAME, COUNTRY, QTY)
  316. SELECT BRANCHNAME,pCountry,SUM(CNT) CNT FROM #TEMPtrnmatrix GROUP BY BRANCHNAME,pCountry
  317. --SELECT NAME = MT.AgentName,
  318. -- COUNTRY = MT.Country,
  319. -- QTY = SUM(Transactions)
  320. --FROM @MAINTABLE MT
  321. --INNER JOIN agentMaster AM(NOLOCK) ON AM.agentName = MT.AgentName
  322. --GROUP BY MT.AgentName, MT.Country, AM.agentId
  323. IF NOT EXISTS(SELECT 1 FROM #TEMPTRN)
  324. BEGIN
  325. SELECT SNO = 1, [ERROR_MESSAGE] = 'NO DATA FOUND FOR THIS FILTER'
  326. RETURN
  327. END
  328. SET @columns = '';
  329. SELECT
  330. @columns += N', p.' + QUOTENAME(NAME)
  331. FROM (SELECT DISTINCT NAME FROM #TEMPTRN) AS x;
  332. SET @columns = ', p.Country ' + @columns
  333. SET @sql = N'
  334. SELECT *
  335. FROM
  336. (
  337. SELECT NAME, COUNTRY, QTY = ISNULL(QTY, 0) FROM #TEMPTRN
  338. ) AS j
  339. PIVOT
  340. (
  341. SUM(QTY) FOR NAME IN ('
  342. + STUFF(REPLACE(REPLACE(REPLACE(@columns, 'p.Country ,', ''), ', p.[', ',['), 'p.', ''), 1, 1, '')
  343. + ')
  344. ) AS p;';
  345. PRINT @sql;
  346. EXEC sp_executesql @sql;
  347. END
  348. ELSE IF @flag = 'matrix-trn-detail'
  349. BEGIN
  350. INSERT INTO @CUSTOMERTBL (customerId)
  351. SELECT TS.customerId
  352. FROM remitTran RT(NOLOCK)
  353. INNER JOIN tranSenders TS(NOLOCK) ON TS.tranId = RT.id
  354. WHERE RT.tranType IN('O','M') and RT.tranStatus <> 'cancel'
  355. AND RT.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  356. AND RT.pCountry = ISNULL(@country, RT.pCountry)
  357. GROUP BY RT.pCountry,TS.customerId
  358. --INSERT INTO @CUSTOMERTBL (customerId)
  359. --SELECT CM.customerId
  360. --FROM customerMaster CM(NOLOCK)
  361. --LEFT JOIN (
  362. -- SELECT AU.userName, AM.agentName FROM agentMaster AM(NOLOCK)
  363. -- INNER JOIN applicationUsers AU(NOLOCK) ON AU.agentId = AM.agentId
  364. -- WHERE AM.agentName = ISNULL(@branch, AM.agentName)
  365. --)X ON X.userName = CM.verifiedBy
  366. --WHERE X.agentName IS NOT NULL
  367. --AND CM.approvedBy IS NOT NULL
  368. --AND CM.availableBalance IS NOT NULL
  369. SELECT [GME CONTROLNO] = DBO.decryptDb(controlNo)
  370. ,[SENDER NAME] = senderName
  371. ,[RECEIVER NAME] = receiverName
  372. ,[TRANSACTION TYPE] = paymentMethod
  373. ,[COLL AMOUNT] = RT.cAmt
  374. ,[PAYOUT AMOUNT] = RT.pAmt
  375. ,[PAYOUT AGENT] = RT.pAgentName
  376. ,[PAYOUT BANK] = RT.pBankName
  377. FROM remitTran RT(NOLOCK)
  378. INNER JOIN tranSenders TS(NOLOCK) ON TS.tranId = RT.id
  379. INNER JOIN @CUSTOMERTBL C ON C.customerId = TS.customerId
  380. WHERE RT.pCountry = ISNULL(@country, RT.pCountry)
  381. AND RT.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  382. AND RT.tranStatus <> 'Cancel'
  383. AND RT.tranType IN('O','M')
  384. UNION ALL
  385. SELECT [GME CONTROLNO] = DBO.decryptDb(controlNo)
  386. ,[SENDER NAME] = senderName
  387. ,[RECEIVER NAME] = receiverName
  388. ,[TRANSACTION TYPE] = paymentMethod
  389. ,[COLL AMOUNT] = RT.cAmt
  390. ,[PAYOUT AMOUNT] = RT.pAmt
  391. ,[PAYOUT AGENT] = RT.pAgentName
  392. ,[PAYOUT BANK] = RT.pBankName
  393. FROM remitTran RT(NOLOCK)
  394. WHERE RT.pCountry = ISNULL(@country, RT.pCountry)
  395. AND RT.sAgentName = ISNULL(@branch, RT.sAgentName)
  396. AND RT.approvedDate BETWEEN @startDate AND @endDate + ' 23:59:59'
  397. AND RT.tranStatus <> 'Cancel'
  398. AND RT.tranType = 'I'
  399. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  400. SELECT 'Country' head, ISNULL(@country, 'All') UNION ALL
  401. SELECT 'Branch' head, ISNULL(@branch, 'All') UNION ALL
  402. SELECT 'From Date' head, @startDate value UNION ALL
  403. SELECT 'To Date' head, @endDate value
  404. SELECT 'Transaction Detail Report' title
  405. END
  406. ELSE IF @flag = 'LocalTopup-Rpt'
  407. BEGIN
  408. SELECT
  409. cy.countryName AS CountryName,
  410. SUM(kj.refundAmount) AS BuyAmount,
  411. COUNT(*) AS BuyCount
  412. FROM powercallHistory ph
  413. INNER JOIN KJ_AUTO_REFUND kj ON kj.customerSummary = ph.processId
  414. INNER JOIN customerMaster cm ON cm.customerId = kj.customerId
  415. INNER JOIN countryMaster cy ON cy.countryId = cm.nativeCountry
  416. WHERE kj.action = 'SUCCESS' AND kj.refundType ='PowerCall'
  417. AND cm.nativeCountry = ISNULL(@country,cm.nativeCountry) AND kj.customerSummary = ph.processId
  418. AND ph.errorCode = '0' AND ph.responesTime BETWEEN @startDate AND @endDate + ' 23:59:59'
  419. GROUP BY cy.countryName
  420. END
  421. ELSE IF @flag = 'LocalTopup-Rpt-Detail'
  422. BEGIN
  423. IF @searchType = 'BuyAmount'
  424. BEGIN
  425. SELECT
  426. kj.customerSummary AS ProcessId,
  427. cy.countryName AS CountryName,
  428. kj.customerId AS CustomerId,
  429. cm.firstName AS FirstName,
  430. kj.refundAmount AS BuyAmount,
  431. ph.phoneNo AS PhoneNo
  432. FROM powercallHistory(NOLOCK) ph
  433. INNER JOIN KJ_AUTO_REFUND(NOLOCK) kj ON kj.customerSummary = ph.processId
  434. INNER JOIN customerMaster(NOLOCK) cm ON cm.customerId = kj.customerId
  435. INNER JOIN countryMaster(NOLOCK) cy ON cy.countryId = cm.nativeCountry
  436. WHERE kj.action = 'SUCCESS' AND kj.refundType ='PowerCall'
  437. AND kj.customerSummary = ph.processId AND ph.errorCode = '0'
  438. AND cy.countryName = @country AND ph.responesTime BETWEEN @startDate AND @endDate + ' 23:59:59'
  439. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  440. --RETURN
  441. END
  442. ELSE
  443. BEGIN
  444. SELECT
  445. kj.customerSummary AS ProcessId,
  446. cy.countryName AS CountryName,
  447. kj.customerId AS CustomerId,
  448. cm.firstName AS FirstName,
  449. kj.refundAmount AS BuyAmount,
  450. ph.phoneNo AS PhoneNo
  451. FROM powercallHistory(NOLOCK) ph
  452. INNER JOIN KJ_AUTO_REFUND(NOLOCK) kj ON kj.customerSummary = ph.processId
  453. INNER JOIN customerMaster(NOLOCK) cm ON cm.customerId = kj.customerId
  454. INNER JOIN countryMaster(NOLOCK) cy ON cy.countryId = cm.nativeCountry
  455. WHERE kj.action = 'SUCCESS' AND kj.refundType ='PowerCall'
  456. AND kj.customerSummary = ph.processId AND ph.errorCode = '0'
  457. AND cy.countryName = @country AND ph.responesTime BETWEEN @startDate AND @endDate + ' 23:59:59'
  458. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  459. --RETURN
  460. END
  461. END
  462. ELSE IF @flag = 'LocalTransaction-Rpt'
  463. BEGIN
  464. SELECT
  465. cy.countryName AS CountryName,
  466. SUM(dh.reqAmt) AS BuyAmount,
  467. COUNT(*) AS BuyCount
  468. FROM KFTC_DOMESTIC_HISTORY(NOLOCK) dh
  469. INNER JOIN customerMaster(NOLOCK) cm ON cm.customerId = dh.customerId
  470. INNER JOIN countryMaster(NOLOCK) cy ON cy.countryId = cm.nativeCountry
  471. WHERE dh.resRspCode = 'A0000' AND transferType = 'deposit' AND
  472. dh.reqTransferDtime BETWEEN @startDate AND @endDate + ' 23:59:59'
  473. AND cm.nativeCountry = ISNULL(@country,cm.nativeCountry)
  474. GROUP BY cy.countryName
  475. END
  476. ELSE IF @flag = 'LocalTranscation-Rpt-Detail'
  477. BEGIN
  478. IF @searchType = 'BuyAmount'
  479. BEGIN
  480. SELECT
  481. dh.processId AS ProcessId,
  482. cy.countryName AS CountryName,
  483. dh.customerId AS CustomerId,
  484. cm.firstName AS FirstName,
  485. dh.reqAmt AS SendAmount,
  486. cm.mobile AS PhoneNo
  487. FROM KFTC_DOMESTIC_HISTORY(NOLOCK) dh
  488. INNER JOIN customerMaster(NOLOCK) cm ON cm.customerId = dh.customerId
  489. INNER JOIN countryMaster(NOLOCK) cy ON cy.countryId = cm.nativeCountry
  490. WHERE dh.resRspCode = 'A0000' AND transferType = 'deposit'
  491. AND cy.countryName = @country AND dh.reqTransferDtime BETWEEN @startDate AND @endDate + ' 23:59:59'
  492. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  493. END
  494. END
  495. END