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.

405 lines
30 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_userWiseTranRpt_New] 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_userWiseTranRpt_New]
  9. @flag VARCHAR(20),
  10. @countryName VARCHAR(50) = NULL,
  11. @agentId INT = NULL,
  12. @branchId INT = NULL,
  13. @userName VARCHAR(50) = NULL,
  14. @fromDate VARCHAR(20) = NULL,
  15. @toDate VARCHAR(30) = NULL,
  16. @user VARCHAR(50) = NULL,
  17. @userType VARCHAR(2) = NULL,
  18. @rCountry VARCHAR(50) = NULL
  19. AS
  20. SET NOCOUNT ON;
  21. SET ANSI_NULLS ON;
  22. DECLARE @TABLE TABLE
  23. (
  24. BRANCHID INT,
  25. BRANCHNAME VARCHAR(200) ,
  26. USERNAME VARCHAR(50),
  27. TXNSEND INT,
  28. AMOUNTSEND MONEY,
  29. TXNPAID INT,
  30. AMOUNTPAID MONEY,
  31. TXNAPPROVED INT,
  32. AMOUNTAPPROVED MONEY,
  33. TXNMODIFICATION INT
  34. )
  35. IF (DATEDIFF(DAY, @fromDate,GETDATE()) > 90 )
  36. BEGIN
  37. IF @FLAG ='detail'
  38. BEGIN
  39. SELECT DISTINCT USERNAME [HEAD] FROM @TABLE
  40. SELECT
  41. [HEAD] = USERNAME
  42. ,[Branch] = BRANCHNAME
  43. ,[#Send Trans] = SUM(ISNULL(TXNSEND,0))
  44. ,[Send Amount] = SUM(ISNULL(AMOUNTSEND,0))
  45. ,[#Paid Trans] = SUM(ISNULL(TXNPAID,0))
  46. ,[Paid Amount] = SUM(ISNULL(AMOUNTPAID,0))
  47. ,[#Approved Trans] = SUM(ISNULL(TXNAPPROVED,0))
  48. ,[Approved Amount] = SUM(ISNULL(AMOUNTAPPROVED,0))
  49. ,[#Amendment Count] = SUM(ISNULL(TXNMODIFICATION,0))
  50. ,agentId = BRANCHID
  51. FROM @TABLE
  52. GROUP BY BRANCHNAME,USERNAME,BRANCHID
  53. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  54. SELECT 'Branch' head,CASE WHEN @branchId IS NULL THEN 'All' ELSE
  55. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@branchId) END VALUE
  56. UNION ALL
  57. SELECT 'User Name' head,ISNULL(@USERNAME,'All') VALUE
  58. UNION ALL
  59. SELECT 'From Date' head,@FROMDATE VALUE
  60. UNION ALL
  61. SELECT 'To Date' head,@TODATE VALUE
  62. UNION ALL
  63. SELECT 'Rec. Country' head, isnull(@rCountry,'All') VALUE
  64. SELECT 'USER WISE DETAIL REPORT <br><font color="red">Date Range is not valid, You can only view transaction upto 90 days.</font>' title
  65. return;
  66. END
  67. ELSE
  68. BEGIN
  69. SELECT
  70. [HEAD] = USERNAME
  71. ,[#SEND Trans] = SUM(ISNULL(TXNSEND,0))
  72. ,[SEND Amount] = SUM(ISNULL(AMOUNTSEND,0))
  73. ,[#Paid Trans] = SUM(ISNULL(TXNPAID,0))
  74. ,[Paid Amount] = SUM(ISNULL(AMOUNTPAID,0))
  75. ,[Approved Trans] = SUM(ISNULL(TXNAPPROVED,0))
  76. ,[Approved Amount] = SUM(ISNULL(AMOUNTAPPROVED,0))
  77. ,[#Amendment Count] = SUM(ISNULL(TXNMODIFICATION,0))
  78. FROM @TABLE GROUP BY USERNAME
  79. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  80. SELECT 'Country' head,@countryName VALUE
  81. UNION ALL
  82. SELECT 'Agent' head,CASE WHEN @agentId IS NULL THEN 'All' ELSE
  83. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@agentId) END VALUE
  84. UNION ALL
  85. SELECT 'Branch' head,CASE WHEN @branchId IS NULL THEN 'All' ELSE
  86. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@branchId) END VALUE
  87. UNION ALL
  88. SELECT 'User Name' head,ISNULL(@USERNAME,'All') VALUE
  89. UNION ALL
  90. SELECT 'From Date' head,@FROMDATE VALUE
  91. UNION ALL
  92. SELECT 'To Date' head,@TODATE VALUE
  93. UNION ALL
  94. SELECT 'Rec. Country' head, isnull(@rCountry,'All') VALUE
  95. SELECT 'USER WISE SUMMARY REPORT<br> <font color="red">Date Range is not valid, You can only view transaction upto 90 days.</font>' title
  96. return;
  97. END
  98. END
  99. IF (DATEDIFF(DAY, @fromDate,@toDate) > 32 )
  100. BEGIN
  101. IF @FLAG ='detail'
  102. BEGIN
  103. SELECT DISTINCT USERNAME [HEAD] FROM @TABLE
  104. SELECT
  105. [HEAD] = USERNAME
  106. ,[Branch] = BRANCHNAME
  107. ,[#Send Trans] = SUM(ISNULL(TXNSEND,0))
  108. ,[Send Amount] = SUM(ISNULL(AMOUNTSEND,0))
  109. ,[#Paid Trans] = SUM(ISNULL(TXNPAID,0))
  110. ,[Paid Amount] = SUM(ISNULL(AMOUNTPAID,0))
  111. ,[#Approved Trans] = SUM(ISNULL(TXNAPPROVED,0))
  112. ,[Approved Amount] = SUM(ISNULL(AMOUNTAPPROVED,0))
  113. ,[#Amendment Count] = SUM(ISNULL(TXNMODIFICATION,0))
  114. ,agentId = BRANCHID
  115. FROM @TABLE
  116. GROUP BY BRANCHNAME,USERNAME,BRANCHID
  117. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  118. SELECT 'Branch' head,CASE WHEN @branchId IS NULL THEN 'All' ELSE
  119. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@branchId) END VALUE
  120. UNION ALL
  121. SELECT 'User Name' head,ISNULL(@USERNAME,'All') VALUE
  122. UNION ALL
  123. SELECT 'From Date' head,@FROMDATE VALUE
  124. UNION ALL
  125. SELECT 'To Date' head,@TODATE VALUE
  126. UNION ALL
  127. SELECT 'Rec. Country' head, isnull(@rCountry,'All') VALUE
  128. SELECT 'USER WISE DETAIL REPORT-<font color="red">Date Range is not valid, Please select date range of 32 days.</font>' title
  129. return;
  130. END
  131. ELSE
  132. BEGIN
  133. SELECT
  134. [HEAD] = USERNAME
  135. ,[#SEND Trans] = SUM(ISNULL(TXNSEND,0))
  136. ,[SEND Amount] = SUM(ISNULL(AMOUNTSEND,0))
  137. ,[#Paid Trans] = SUM(ISNULL(TXNPAID,0))
  138. ,[Paid Amount] = SUM(ISNULL(AMOUNTPAID,0))
  139. ,[Approved Trans] = SUM(ISNULL(TXNAPPROVED,0))
  140. ,[Approved Amount] = SUM(ISNULL(AMOUNTAPPROVED,0))
  141. ,[#Amendment Count] = SUM(ISNULL(TXNMODIFICATION,0))
  142. FROM @TABLE GROUP BY USERNAME
  143. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  144. SELECT 'Country' head,@countryName VALUE
  145. UNION ALL
  146. SELECT 'Agent' head,CASE WHEN @agentId IS NULL THEN 'All' ELSE
  147. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@agentId) END VALUE
  148. UNION ALL
  149. SELECT 'Branch' head,CASE WHEN @branchId IS NULL THEN 'All' ELSE
  150. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@branchId) END VALUE
  151. UNION ALL
  152. SELECT 'User Name' head,ISNULL(@USERNAME,'All') VALUE
  153. UNION ALL
  154. SELECT 'From Date' head,@FROMDATE VALUE
  155. UNION ALL
  156. SELECT 'To Date' head,@TODATE VALUE
  157. UNION ALL
  158. SELECT 'Rec. Country' head, isnull(@rCountry,'All') VALUE
  159. SELECT 'USER WISE SUMMARY REPORT- <font color="red">Date Range is not valid, Please select date range of 32 days.</font>' title
  160. return;
  161. END
  162. END
  163. SET @TODATE = @TODATE + ' 23:59:59'
  164. IF @FLAG='detail'
  165. BEGIN
  166. INSERT INTO @TABLE(BRANCHID,BRANCHNAME,USERNAME,TXNSEND,AMOUNTSEND)
  167. SELECT
  168. sBranch,
  169. sBranchName,
  170. createdBy,
  171. COUNT('x'),
  172. SUM(cAmt)
  173. FROM vwRemitTran WITH(NOLOCK) WHERE createdDate BETWEEN @fromDate AND @toDate
  174. AND sAgent = @agentId
  175. AND sBranch = isnull(@branchId,sBranch)
  176. AND createdby = isnull(@userName,createdBy)
  177. AND isnull(pCountry,'') = isnull(@rCountry,isnull(pCountry,''))
  178. GROUP BY createdBy,sBranchName,sBranch
  179. INSERT INTO @TABLE(BRANCHID,BRANCHNAME,USERNAME,TXNPAID,AMOUNTPAID)
  180. SELECT
  181. pBranch,
  182. pBranchName,
  183. paidBy,
  184. COUNT('x'),
  185. SUM(pAmt)
  186. FROM vwRemitTran WITH(NOLOCK) WHERE paidDate BETWEEN @fromDate AND @toDate
  187. AND pAgent = @agentId
  188. AND pBranch = isnull(@branchId,pBranch)
  189. AND paidBy = isnull(@userName,paidBy)
  190. AND isnull(pCountry,'') = isnull(@rCountry,isnull(pCountry,''))
  191. GROUP BY paidBy,pBranchName,pBranch
  192. INSERT INTO @TABLE(BRANCHID,BRANCHNAME,USERNAME,TXNAPPROVED,AMOUNTAPPROVED)
  193. SELECT
  194. sBranch,
  195. sBranchName,
  196. approvedBy,
  197. COUNT('x'),
  198. SUM(cAmt)
  199. FROM vwRemitTran WITH(NOLOCK) WHERE approvedDate BETWEEN @fromDate AND @toDate
  200. AND sAgent = @agentId
  201. AND sBranch = isnull(@branchId,sBranch)
  202. AND createdby = isnull(@userName,createdBy)
  203. AND isnull(pCountry,'') = isnull(@rCountry,isnull(pCountry,''))
  204. GROUP BY approvedBy,sBranchName,sBranch
  205. INSERT INTO @TABLE(BRANCHID,BRANCHNAME,USERNAME,TXNMODIFICATION)
  206. select c.agentId, c.agentName ,a.createdBy,count('x')
  207. from tranModifyLog a WITH(NOLOCK)
  208. inner join applicationUsers b on a.createdBy=b.userName
  209. inner join agentMaster c with(nolock) on b.agentId=c.agentId
  210. inner join agentMaster d with(nolock) on c.parentId = d.agentId
  211. inner join vwRemitTran e with(nolock) on a.controlNo = e.controlNo
  212. where MsgType='MODIFY' and a.controlNo is not null
  213. AND d.agentId = @agentId
  214. and c.agentId = isnull(@branchId,c.agentId)
  215. and a.createdby = isnull(@userName,a.createdBy)
  216. and a.createdDate BETWEEN @fromDate AND @toDate
  217. AND isnull(e.pCountry,'') = isnull(@rCountry,isnull(e.pCountry,''))
  218. group by a.createdBy,c.agentId,c.agentName
  219. SELECT DISTINCT USERNAME [HEAD] FROM @TABLE
  220. SELECT
  221. [HEAD] = USERNAME
  222. ,[Branch] = BRANCHNAME
  223. ,[#Send Trans] = SUM(ISNULL(TXNSEND,0))
  224. ,[Send Amount] = SUM(ISNULL(AMOUNTSEND,0))
  225. ,[#Paid Trans] = SUM(ISNULL(TXNPAID,0))
  226. ,[Paid Amount] = SUM(ISNULL(AMOUNTPAID,0))
  227. ,[#Approved Trans] = SUM(ISNULL(TXNAPPROVED,0))
  228. ,[Approved Amount] = SUM(ISNULL(AMOUNTAPPROVED,0))
  229. ,[#Amendment Count] = SUM(ISNULL(TXNMODIFICATION,0))
  230. ,agentId = BRANCHID
  231. FROM @TABLE
  232. GROUP BY BRANCHNAME,USERNAME,BRANCHID
  233. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  234. SELECT 'Branch' head,CASE WHEN @branchId IS NULL THEN 'All' ELSE
  235. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@branchId) END VALUE
  236. UNION ALL
  237. SELECT 'User Name' head,ISNULL(@USERNAME,'All') VALUE
  238. UNION ALL
  239. SELECT 'From Date' head,@FROMDATE VALUE
  240. UNION ALL
  241. SELECT 'To Date' head,@TODATE VALUE
  242. UNION ALL
  243. SELECT 'Rec. Country' head, isnull(@rCountry,'All') VALUE
  244. SELECT 'USER WISE DETAIL REPORT' title
  245. END
  246. IF @FLAG='summary'
  247. BEGIN
  248. INSERT INTO @TABLE(BRANCHID,BRANCHNAME,USERNAME,TXNSEND,AMOUNTSEND)
  249. SELECT
  250. sBranch,
  251. sBranchName,
  252. createdBy,
  253. COUNT('x'),
  254. SUM(cAmt)
  255. FROM vwRemitTran WITH(NOLOCK) WHERE createdDate BETWEEN @fromDate AND @toDate
  256. AND sAgent = @agentId
  257. and sBranch = isnull(@branchId,sBranch)
  258. and createdby = isnull(@userName,createdBy)
  259. AND isnull(pCountry,'') = isnull(@rCountry,isnull(pCountry,''))
  260. GROUP BY createdBy,sBranchName,sBranch
  261. INSERT INTO @TABLE(BRANCHID,BRANCHNAME,USERNAME,TXNPAID,AMOUNTPAID)
  262. SELECT
  263. pBranch,
  264. pBranchName,
  265. paidBy,
  266. COUNT('x'),
  267. SUM(pAmt)
  268. FROM vwRemitTran WITH(NOLOCK) WHERE paidDate BETWEEN @fromDate AND @toDate
  269. AND pAgent = @agentId
  270. and pBranch = isnull(@branchId,pBranch)
  271. and paidBy = isnull(@userName,paidBy)
  272. AND isnull(pCountry,'') = isnull(@rCountry,isnull(pCountry,''))
  273. GROUP BY paidBy,pBranchName,pBranch
  274. INSERT INTO @TABLE(BRANCHID,BRANCHNAME,USERNAME,TXNAPPROVED,AMOUNTAPPROVED)
  275. SELECT
  276. sBranch,
  277. sBranchName,
  278. approvedBy,
  279. COUNT('x'),
  280. SUM(cAmt)
  281. FROM vwRemitTran WITH(NOLOCK) WHERE approvedDate BETWEEN @fromDate AND @toDate
  282. AND sAgent = @agentId
  283. and sBranch = isnull(@branchId,sBranch)
  284. and createdby = isnull(@userName,createdBy)
  285. AND isnull(pCountry,'') = isnull(@rCountry,isnull(pCountry,''))
  286. GROUP BY approvedBy,sBranchName,sBranch
  287. INSERT INTO @TABLE(BRANCHID,BRANCHNAME,USERNAME,TXNMODIFICATION)
  288. select c.agentId, c.agentName ,a.createdBy,count('x')
  289. from tranModifyLog a WITH(NOLOCK)
  290. inner join applicationUsers b on a.createdBy=b.userName
  291. inner join agentMaster c with(nolock) on b.agentId=c.agentId
  292. inner join agentMaster d with(nolock) on c.parentId = d.agentId
  293. inner join vwRemitTran e with(nolock) on a.controlNo = e.controlNo
  294. where MsgType='MODIFY' and a.controlNo is not null
  295. AND d.agentId = @agentId
  296. and c.agentId = isnull(@branchId,c.agentId)
  297. and a.createdby = isnull(@userName,a.createdBy)
  298. and a.createdDate BETWEEN @fromDate AND @toDate
  299. AND isnull(e.pCountry,'') = isnull(@rCountry,isnull(e.pCountry,''))
  300. group by a.createdBy,c.agentId,c.agentName
  301. SELECT
  302. [HEAD] = USERNAME
  303. ,[#SEND Trans] = SUM(ISNULL(TXNSEND,0))
  304. ,[SEND Amount] = SUM(ISNULL(AMOUNTSEND,0))
  305. ,[#Paid Trans] = SUM(ISNULL(TXNPAID,0))
  306. ,[Paid Amount] = SUM(ISNULL(AMOUNTPAID,0))
  307. ,[Approved Trans] = SUM(ISNULL(TXNAPPROVED,0))
  308. ,[Approved Amount] = SUM(ISNULL(AMOUNTAPPROVED,0))
  309. ,[#Amendment Count] = SUM(ISNULL(TXNMODIFICATION,0))
  310. FROM @TABLE GROUP BY USERNAME
  311. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  312. SELECT 'Country' head,@countryName VALUE
  313. UNION ALL
  314. SELECT 'Agent' head,CASE WHEN @agentId IS NULL THEN 'All' ELSE
  315. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@agentId) END VALUE
  316. UNION ALL
  317. SELECT 'Branch' head,CASE WHEN @branchId IS NULL THEN 'All' ELSE
  318. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@branchId) END VALUE
  319. UNION ALL
  320. SELECT 'User Name' head,ISNULL(@USERNAME,'All') VALUE
  321. UNION ALL
  322. SELECT 'From Date' head,@FROMDATE VALUE
  323. UNION ALL
  324. SELECT 'To Date' head,@TODATE VALUE
  325. UNION ALL
  326. SELECT 'Rec. Country' head, isnull(@rCountry,'All') VALUE
  327. SELECT 'USER WISE SUMMARY REPORT' title
  328. END
  329. IF @FLAG='MODIFYHISTORY'
  330. BEGIN
  331. select [Branch] = c.agentName
  332. ,[User] = a.createdBy
  333. ,[Date] = a.createdDate
  334. ,[TranId] = '<span class = "link" onclick ="ViewTranDetail(' + CAST(tranId AS VARCHAR(50)) + ');">' + CAST(tranId AS VARCHAR(50)) + '</span>'
  335. ,[ControlNo] = dbo.FNADecryptString(a.controlNo)
  336. ,[Message] = message
  337. ,[Status] = status
  338. ,[Approved By] = resolvedBy
  339. ,[Approved Date] = resolvedDate
  340. from tranModifyLog a WITH(NOLOCK)
  341. inner join applicationUsers b on a.createdBy=b.userName
  342. inner join agentMaster c with(nolock) on b.agentId=c.agentId
  343. inner join vwRemitTran d with(nolock) on a.controlNo = d.controlNo
  344. where MsgType='MODIFY'
  345. and c.agentId = isnull(@branchId,c.agentId)
  346. and a.createdby = isnull(@userName,a.createdBy)
  347. and a.createdDate BETWEEN @fromDate AND @toDate
  348. and a.controlNo is not null
  349. AND isnull(d.pCountry,'') = isnull(@rCountry,isnull(d.pCountry,''))
  350. order by c.agentName,a.createdBY
  351. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  352. SELECT 'Branch' head,CASE WHEN @branchId IS NULL THEN 'All' ELSE
  353. (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentId=@branchId) END VALUE
  354. UNION ALL
  355. SELECT 'User Name' head,ISNULL(@USERNAME,'All') VALUE
  356. UNION ALL
  357. SELECT 'From Date' head,@FROMDATE VALUE
  358. UNION ALL
  359. SELECT 'To Date' head,@TODATE VALUE
  360. UNION ALL
  361. SELECT 'Rec. Country' head, isnull(@rCountry,'All') VALUE
  362. SELECT 'USER WISE MODIFY HISTORY' title
  363. END
  364. GO