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.

265 lines
15 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_userWiseTranRpt_OLD] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. /*
  9. EXEC proc_userWiseTranRpt @FLAG='UW',@FROMDATE='1/10/2012',@TODATE='10/10/2012',@USERNAME=NULL,@userType='HO'
  10. EXEC proc_userWiseTranRpt @FLAG='UW',@FROMDATE='1/10/2012',@TODATE='10/10/2012',@USERNAME=NULL,@userType='Agent'
  11. EXEC proc_userWiseTranRpt @FLAG='UWHO',@FROMDATE='09/20/2012',@TODATE='09/21/2012',@USERNAME=NULL
  12. */
  13. CREATE procEDURE [dbo].[proc_userWiseTranRpt_OLD]
  14. @flag VARCHAR(20),
  15. @fromDate VARCHAR(20) = NULL,
  16. @toDate VARCHAR(30) = NULL,
  17. @userName VARCHAR(50) = NULL,
  18. @userType VARCHAR(50) = NULL,
  19. @user VARCHAR(50) = NULL
  20. AS
  21. SET NOCOUNT ON;
  22. SET ANSI_NULLS ON;
  23. SET @TODATE = @TODATE + ' 23:59:59'
  24. IF OBJECT_ID('tempdb..#tempTable2') IS NOT NULL
  25. DROP TABLE #tempTable2
  26. CREATE TABLE #tempTable2
  27. (
  28. agentType varchar(50) null
  29. )
  30. IF @userType='HO'
  31. BEGIN
  32. INSERT INTO #tempTable2
  33. SELECT '2901'
  34. END
  35. IF @userType='Agent'
  36. BEGIN
  37. INSERT INTO #tempTable2
  38. SELECT valueId FROM staticDataValue WHERE valueId<>2901 and typeID=2900
  39. END
  40. IF @userType IS NULL
  41. BEGIN
  42. INSERT INTO #tempTable2
  43. SELECT valueId FROM staticDataValue WHERE typeID=2900
  44. END
  45. --select * from #tempTable2
  46. IF @FLAG='UWHO'
  47. BEGIN
  48. IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL
  49. DROP TABLE #tempTable
  50. IF OBJECT_ID('tempdb..#tempTable1') IS NOT NULL
  51. DROP TABLE #tempTable1
  52. CREATE TABLE #tempTable
  53. (
  54. userName varchar(50) null,
  55. branchId int null,
  56. sendCount int null,
  57. sendAmount money null,
  58. paidCount int null,
  59. paidAmount money null
  60. )
  61. CREATE TABLE #tempTable1
  62. (
  63. userName varchar(50) null,
  64. branchId int null,
  65. sendCount int null,
  66. sendAmount money null,
  67. paidCount int null,
  68. paidAmount money null
  69. )
  70. -- Send & Paid Transaction User Wise
  71. insert into #tempTable
  72. select x.userName,x.sBranch,x.TranCount ,x.cAmt ,y.TranCount ,y.pAmt
  73. from
  74. (
  75. select * from
  76. (
  77. SELECT a.userName
  78. FROM applicationUsers a WITH(NOLOCK)
  79. INNER JOIN agentMaster b WITH(NOLOCK) ON a.agentId=b.agentId
  80. INNER JOIN #tempTable2 C WITH(NOLOCK) ON C.agentType=b.agentType
  81. where a.userName =ISNULL(@userName,a.userName)
  82. )a
  83. inner join
  84. (
  85. select sBranch,createdBy ,COUNT(*) TranCount,SUM(cAmt) cAmt
  86. from remitTran with(nolock) where approvedDate between @fromDate and @toDate
  87. and createdBy =ISNULL(@userName,createdBy)
  88. and approvedDate is not null
  89. group by createdBy,sBranch
  90. )b on a.userName=b.createdBy
  91. )x
  92. inner join
  93. (
  94. select * from
  95. (
  96. SELECT a.userName
  97. FROM applicationUsers a WITH(NOLOCK)
  98. INNER JOIN agentMaster b with(nolock) on a.agentId=b.agentId
  99. INNER JOIN #tempTable2 C WITH(NOLOCK) ON C.agentType=b.agentType
  100. where a.userName =ISNULL(@userName,a.userName)
  101. )a
  102. inner join
  103. (
  104. select pBranch,paidBy,COUNT(*) TranCount,SUM(pAmt) pAmt
  105. from remitTran with(nolock) where tranStatus='Paid'
  106. and paidDate between @fromDate and @toDate
  107. and paidBy =ISNULL(@userName,paidBy) and paidDate between @fromDate and @toDate
  108. group by paidBy,pBranch
  109. )b on a.userName=b.paidBy
  110. )y on y.pBranch=x.sBranch and x.createdBy=y.paidBy
  111. insert into #tempTable1(userName,branchId,sendCount,sendAmount,paidCount,paidAmount)
  112. select a.userName,b.sBranch,b.TranCount,b.cAmt,null,null
  113. from
  114. (
  115. select a.userName
  116. from applicationUsers a with(nolock)
  117. inner join agentMaster b with(nolock) on a.agentId=b.agentId
  118. INNER JOIN #tempTable2 C WITH(NOLOCK) ON C.agentType=b.agentType
  119. where a.userName =ISNULL(@userName,a.userName)
  120. )a
  121. inner join
  122. (
  123. select sBranch,createdBy ,COUNT(*) TranCount,SUM(cAmt) cAmt
  124. from remitTran with(nolock) where approvedDate between @fromDate and @toDate
  125. and createdBy =ISNULL(@userName,createdBy)
  126. and approvedDate is not null
  127. group by createdBy,sBranch
  128. )b on a.userName=b.createdBy
  129. DELETE FROM #tempTable1
  130. FROM #tempTable1 t
  131. INNER JOIN #tempTable ds ON t.branchId = ds.branchId
  132. AND ds.userName = t.userName
  133. insert into #tempTable
  134. select * from #tempTable1
  135. delete from #tempTable1
  136. insert into #tempTable1
  137. select a.userName,b.pBranch,null,null,b.TranCount,b.pAmt
  138. from
  139. (
  140. SELECT a.userName
  141. from applicationUsers a with(nolock)
  142. INNER JOIN agentMaster b with(nolock) on a.agentId=b.agentId
  143. INNER JOIN #tempTable2 C WITH(NOLOCK) ON C.agentType=b.agentType
  144. where a.userName =ISNULL(@userName,a.userName)
  145. )a
  146. inner join
  147. (
  148. select pBranch,paidBy,COUNT(*) TranCount,SUM(pAmt) pAmt
  149. from remitTran with(nolock)
  150. where tranStatus='Paid' and paidDate between @fromDate and @toDate
  151. and paidBy =ISNULL(@userName,paidBy)
  152. group by paidBy,pBranch
  153. )b on a.userName=b.paidBy
  154. DELETE FROM #tempTable1
  155. FROM #tempTable1 t
  156. INNER JOIN #tempTable ds ON t.branchId = ds.branchId
  157. AND ds.userName = t.userName
  158. insert into #tempTable
  159. select * from #tempTable1
  160. select distinct userName [HEAD] from #tempTable
  161. select userName [HEAD]
  162. ,b.agentName [Agent Name]
  163. ,isnull(sendCount,0) [#Send Trans]
  164. ,isnull(sendAmount,0) [Send Amount]
  165. ,isnull(paidCount,0) [#Paid Trans]
  166. ,isnull(paidAmount,0) [Paid Amount]
  167. from #tempTable a with(nolock) inner join agentMaster b with(nolock) on a.branchId=b.agentId
  168. order by userName
  169. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  170. SELECT 'From Date' head,@FROMDATE value
  171. UNION ALL
  172. SELECT 'To Date' head,@TODATE value
  173. UNION ALL
  174. SELECT 'User' head,isnull(@USERNAME,'All') value
  175. SELECT 'USER WISE DETAIL TRANSACTION REPORT' title
  176. END
  177. IF @FLAG='UW'
  178. BEGIN
  179. select
  180. c.userName [User Name]
  181. ,isnull(A.TranCount,0) [#Send Tran]
  182. ,isnull(a.cAmt,0) [Send Amount]
  183. ,isnull(b.TranCount,0) [#Paid Tran]
  184. ,isnull(b.pAmt,0) [Paid Amount]
  185. from
  186. (
  187. SELECT a.userName
  188. from applicationUsers a with(nolock)
  189. INNER JOIN agentMaster b with(nolock) on a.agentId=b.agentId
  190. INNER JOIN #tempTable2 C WITH(NOLOCK) ON C.agentType=b.agentType
  191. where a.userName =ISNULL(@userName,a.userName)
  192. )c
  193. left join
  194. (
  195. select createdBy ,COUNT(*) TranCount,SUM(cAmt) cAmt
  196. from remitTran with(nolock)
  197. WHERE approvedDate BETWEEN @FROMDATE AND @TODATE
  198. and createdBy =ISNULL(@userName,createdBy)
  199. and approvedDate is not null
  200. group by createdBy
  201. )a on c.userName=a.createdBy
  202. left join
  203. (
  204. select paidBy,COUNT(*) TranCount,SUM(pAmt) pAmt
  205. from remitTran with(nolock) where tranStatus='Paid' AND paidDate BETWEEN @FROMDATE AND @TODATE
  206. and paidBy =ISNULL(@userName,paidBy)
  207. group by paidBy
  208. )b on c.userName=b.paidBy
  209. where c.userName = ISNULL(@USERNAME,c.userName) AND a.TranCount is not null or b.TranCount is not null
  210. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  211. SELECT 'From Date' head,@FROMDATE value
  212. UNION ALL
  213. SELECT 'To Date' head,@TODATE value
  214. UNION ALL
  215. SELECT 'User' head,isnull(@USERNAME,'All') value
  216. SELECT 'USER WISE SUMMARY TRANSACTION REPORT' title
  217. END
  218. GO