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.

316 lines
20 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_cooperativeReport] 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 proc [dbo].[proc_cooperativeReport]
  9. @flag VARCHAR(10) = NULL
  10. ,@user VARCHAR(30) = NULL
  11. ,@fromDate VARCHAR(50) = NULL
  12. ,@toDate VARCHAR(50) = NULL
  13. ,@agentGrp VARCHAR(50) = NULL
  14. ,@agent VARCHAR(50) = NULL
  15. ,@branch VARCHAR(50) = NULL
  16. ,@table VARCHAR(MAX) = NULL
  17. ,@pageNumber INT = NULL
  18. ,@pageSize INT = NULL
  19. AS
  20. /*
  21. EXEC proc_cooperativeReport @flag ='rpt'
  22. , @user = 'netra'
  23. , @agentGrp = '8026'
  24. , @agent = NULL
  25. , @branch = NULL
  26. , @fromDate = '2016-05-18'
  27. , @toDate = '2016-05-29'
  28. , @pageNumber = 1
  29. , @pageSize = 100
  30. */
  31. SET NOCOUNT ON;
  32. SET XACT_ABORT ON ;
  33. BEGIN TRY
  34. DECLARE @sql1 VARCHAR(MAX)
  35. ,@sql2 VARCHAR(MAX)
  36. ,@sql3 VARCHAR(MAX)
  37. ,@sqlEP VARCHAR(MAX)
  38. ,@sqlPO VARCHAR(MAX)
  39. ,@sqlCancel VARCHAR(MAX)
  40. ,@mainSql VARCHAR(MAX)
  41. IF @flag = 'rpt'
  42. BEGIN
  43. IF @agentGrp IS NULL
  44. BEGIN
  45. SELECT @agentGrp=am.agentGrp FROM dbo.applicationUsers u WITH(NOLOCK)
  46. INNER JOIN dbo.agentMaster am WITH(NOLOCK) ON u.agentId=am.agentId WHERE u.userName=@user
  47. END
  48. -- Domestic Send
  49. SET @sql1 =' SELECT
  50. sAgent
  51. ,sBranch
  52. ,sendCount = count(''x'')
  53. FROM vwRemitTran rt WITH(NOLOCK)
  54. INNER JOIN agentMaster am WITH(NOLOCK) ON rt.sAgent = am.agentId
  55. WHERE (am.agentGrp = 8026 OR am.agentGrp = 9906) and tranType = ''D''
  56. AND rt.approvedDate BETWEEN '''+@fromDate+''' AND '''+ @toDate+' 23:59:59'''
  57. IF @agent IS NOT NULL
  58. SET @sql1 = @sql1 + ' AND sAgent = '''+@agent+''''
  59. IF @branch IS NOT NULL
  60. SET @sql1 = @sql1 + ' AND sBranch = '''+@branch+''''
  61. IF @agentGrp IS NOT NULL
  62. SET @sql1 = @sql1 + ' AND am.agentGrp = '''+@agentGrp+''''
  63. SET @sql1= @sql1+' GROUP BY sBranch,sAgent'
  64. -- Domestic Cancel
  65. SET @sqlCancel =' SELECT
  66. sAgent
  67. ,sBranch
  68. ,cancelCount = count(''x'')
  69. FROM vwRemitTran rt WITH(NOLOCK)
  70. INNER JOIN agentMaster am WITH(NOLOCK) ON rt.sAgent = am.agentId
  71. WHERE (am.agentGrp = 8026 OR am.agentGrp = 9906) and tranType = ''D'' and tranStatus = ''Cancel''
  72. AND rt.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+ @toDate+' 23:59:59'''
  73. IF @agent IS NOT NULL
  74. SET @sqlCancel = @sqlCancel + ' AND sAgent = '''+@agent+''''
  75. IF @branch IS NOT NULL
  76. SET @sqlCancel = @sqlCancel + ' AND sBranch = '''+@branch+''''
  77. IF @agentGrp IS NOT NULL
  78. SET @sqlCancel = @sqlCancel + ' AND am.agentGrp = '''+@agentGrp+''''
  79. SET @sqlCancel= @sqlCancel+' GROUP BY sBranch,sAgent'
  80. --Domestic Paid
  81. SET @sql2 ='SELECT
  82. pAgent
  83. ,pBranch
  84. ,payCount = count(*)
  85. --,payAmount = SUM(pAmt)
  86. FROM vwRemitTran rt WITH(NOLOCK)
  87. INNER JOIN agentMaster am WITH(NOLOCK)
  88. ON rt.pAgent = am.agentId
  89. WHERE (am.agentGrp = 8026 OR am.agentGrp = 9906) AND rt.sCountry = ''Nepal'' and tranType =''D''
  90. AND rt.paidDate BETWEEN '''+@fromDate+''' AND '''+ @toDate+' 23:59:59'''
  91. IF @agent IS NOT NULL
  92. SET @sql2 = @sql2 + ' AND pAgent = '''+@agent+''''
  93. IF @branch IS NOT NULL
  94. SET @sql2 = @sql2 + ' AND pBranch = '''+@branch+''''
  95. IF @agentGrp IS NOT NULL
  96. SET @sql2 = @sql2 + ' AND am.agentGrp = '''+@agentGrp+''''
  97. SET @sql2= @sql2+' GROUP BY pBranch,pAgent'
  98. -- International Paid
  99. SET @sql3 ='SELECT
  100. pAgent
  101. ,pBranch
  102. ,payCount = count(*)
  103. --,payAmount = SUM(pAmt)
  104. FROM vwRemitTran rt WITH(NOLOCK)
  105. INNER JOIN agentMaster am WITH(NOLOCK)
  106. ON rt.pAgent = am.agentId
  107. WHERE (am.agentGrp = 8026 OR am.agentGrp = 9906) AND rt.sCountry <> ''Nepal''
  108. AND rt.paidDate BETWEEN '''+@fromDate+''' AND '''+ @toDate+' 23:59:59'''
  109. IF @agent IS NOT NULL
  110. SET @sql3 = @sql3 + ' AND pAgent = '''+@agent+''''
  111. IF @branch IS NOT NULL
  112. SET @sql3 = @sql3 + ' AND pBranch = '''+@branch+''''
  113. IF @agentGrp IS NOT NULL
  114. SET @sql3 = @sql3 + ' AND am.agentGrp = '''+@agentGrp+''''
  115. SET @sql3= @sql3+' GROUP BY pBranch,pAgent'
  116. -- ## EP
  117. SET @sqlEP ='SELECT
  118. pAgent
  119. ,pBranch
  120. ,payCount = count(''x'')
  121. FROM errPaidTran ep WITH(NOLOCK)
  122. INNER JOIN agentMaster am WITH(NOLOCK) ON ep.oldPBranch = am.agentId
  123. inner join remitTran rt with(nolock) on rt.id = ep.tranId
  124. WHERE (am.agentGrp = 8026 OR am.agentGrp = 9906)
  125. AND ep.approvedDate BETWEEN '''+@fromDate+''' AND '''+ @toDate+' 23:59:59'''
  126. IF @agent IS NOT NULL
  127. SET @sqlEP = @sqlEP + ' AND pAgent = '''+@agent+''''
  128. IF @branch IS NOT NULL
  129. SET @sqlEP = @sqlEP + ' AND pBranch = '''+@branch+''''
  130. IF @agentGrp IS NOT NULL
  131. SET @sqlEP = @sqlEP + ' AND am.agentGrp = '''+@agentGrp+''''
  132. SET @sqlEP= @sqlEP+' GROUP BY pBranch,pAgent '
  133. -- ## PO
  134. SET @sqlPO ='SELECT
  135. newPAgent
  136. ,newPBranch
  137. ,payCount = count(''x'')
  138. FROM errPaidTran ep WITH(NOLOCK)
  139. INNER JOIN agentMaster am WITH(NOLOCK) ON ep.newPBranch = am.agentId
  140. inner join remitTran rt with(nolock) on rt.id = ep.tranId
  141. WHERE (am.agentGrp = 8026 OR am.agentGrp = 9906)
  142. AND ep.newPaidDate BETWEEN '''+@fromDate+''' AND '''+ @toDate+' 23:59:59'''
  143. IF @agent IS NOT NULL
  144. SET @sqlPO = @sqlPO + ' AND newPAgent = '''+@agent+''''
  145. IF @branch IS NOT NULL
  146. SET @sqlPO = @sqlPO + ' AND newPBranch = '''+@branch+''''
  147. IF @agentGrp IS NOT NULL
  148. SET @sqlPO = @sqlPO + ' AND am.agentGrp = '''+@agentGrp+''''
  149. SET @sqlPO= @sqlPO+' GROUP BY newPAgent,newPBranch '
  150. IF CONVERT(VARCHAR,GETDATE(),101) < = CONVERT(VARCHAR,CAST(@toDate AS DATETIME),101)
  151. BEGIN
  152. SET @mainSql ='
  153. select
  154. a.agentId
  155. ,a.agentName sBranchName
  156. ,case when b.agentId = 1002 then a.agentName else b.agentName end sAgentName
  157. ,case when b.agentId = 1002 then a.agentId else a.parentId end parentId
  158. ,balance = CASE
  159. WHEN a.isSettlingAgent = ''Y''
  160. THEN DBO.FNAGetAvailableBalance(a.agentId)
  161. WHEN ISNULL(a.isSettlingAgent,''N'') <> ''Y'' AND ISNULL(B.isSettlingAgent,''N'') = ''Y'' AND a.isHeadOffice = ''Y''
  162. THEN DBO.FNAGetAvailableBalance(B.agentId)
  163. ELSE 0
  164. END
  165. ,a.agentGrp
  166. from agentMaster a with(nolock)
  167. inner join agentMaster b with(nolock) on a.parentId = b.agentId
  168. where ISNULL(a.agentBlock,''U'') <> ''B''
  169. AND ISNULL(B.agentBlock,''U'') <> ''B''
  170. AND (( (a.agentGrp = 8026 OR a.agentGrp = 9906) and a.agentType = 2904)
  171. OR (a.agentType = 2903 and a.actasbranch=''Y'' AND (a.agentGrp = 8026 OR a.agentGrp = 9906)))'
  172. END
  173. ELSE
  174. BEGIN
  175. SET @mainSql ='
  176. select
  177. a.agentId
  178. ,a.agentName sBranchName
  179. ,case when b.agentId = 1002 then a.agentName else b.agentName end sAgentName
  180. ,case when b.agentId = 1002 then a.agentId else a.parentId end parentId
  181. ,balance =
  182. CASE
  183. WHEN a.isSettlingAgent = ''Y''
  184. THEN bal.balance
  185. WHEN ISNULL(a.isSettlingAgent,''N'') <> ''Y'' AND ISNULL(B.isSettlingAgent,''N'') = ''Y'' AND a.isHeadOffice = ''Y''
  186. THEN bal.balance
  187. ELSE 0 END
  188. ,a.agentGrp
  189. from agentMaster a with(nolock)
  190. inner join agentMaster b with(nolock) on a.parentId = b.agentId
  191. LEFT join
  192. (
  193. SELECT agentId,balance = ISNULL(amt,0) FROM RemittanceLogData.dbo.agentClosingBalanceHistory bh WITH(NOLOCK)
  194. WHERE CONVERT(VARCHAR,balDate,101) = CONVERT(VARCHAR,CAST('''+@toDate+''' AS DATETIME),101)
  195. )bal ON a.agentId = bal.agentId OR a.parentId = bal.agentId
  196. where ISNULL(a.agentBlock,''U'') <> ''B''
  197. AND ISNULL(B.agentBlock,''U'') <> ''B''
  198. AND (( (a.agentGrp = 8026 OR a.agentGrp = 9906) and a.agentType = 2904)
  199. OR (a.agentType = 2903 and a.actasbranch=''Y'' AND (a.agentGrp = 8026 OR a.agentGrp = 9906) ))'
  200. END
  201. SET @table ='
  202. SELECT
  203. [SN] = row_number() over(order by a.sBranchName)
  204. , [Agent] = a.sBranchName
  205. , [Domestic Transaction_Send] = isnull(b.sendCount,0)
  206. , [Domestic Transaction_Paid] = isnull(c.payCount,0)
  207. , [Domestic Transaction_Cancel] = isnull(g.cancelCount,0)
  208. , [Int''l <br/>Paid] = isnull(d.payCount,0)
  209. , [EP] = isnull(e.payCount,0)
  210. , [PO] = isnull(f.payCount,0)
  211. , [Closing Balance_Payable] = CASE WHEN balance >= 0 THEN balance ELSE 0 END
  212. , [Closing Balance_Receivable] = CASE WHEN balance < 0 THEN balance ELSE 0 END
  213. FROM
  214. (
  215. '+@mainSql+'
  216. )a
  217. LEFT JOIN
  218. ( '+@sql1 +')b ON a.agentId = b.sBranch
  219. LEFT JOIN
  220. (' +@sql2 +' )c ON a.agentId = c.pBranch
  221. LEFT JOIN
  222. (' +@sql3 +' )d ON a.agentId = d.pBranch
  223. LEFT JOIN
  224. (' +@sqlEP +' )e ON a.agentId = e.pBranch
  225. LEFT JOIN
  226. (' +@sqlPO +' )f ON a.agentId = f.newPBranch
  227. LEFT JOIN
  228. (' +@sqlCancel +' )g ON a.agentId = g.sBranch
  229. where 1=1 '
  230. IF @agent IS NOT NULL
  231. SET @table = @table + ' AND a.parentId = '''+@agent+''''
  232. IF @branch IS NOT NULL
  233. SET @table = @table + ' AND a.agentId = '''+@branch+''''
  234. IF @agentGrp IS NOT NULL
  235. SET @table = @table + ' AND a.agentGrp = '''+@agentGrp+''''
  236. SET @table = @table + ' order by a.sBranchName'
  237. PRINT @table
  238. EXEC(@table)
  239. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  240. SELECT 'From Date' head,@fromDate VALUE
  241. UNION ALL
  242. SELECT 'TO Date' head,@toDate VALUE
  243. UNION ALL
  244. SELECT 'Agent' head,CASE WHEN @agent IS NULL THEN 'All' ELSE (SELECT agentName from agentMaster where agentId=@agent) END VALUE
  245. UNION ALL
  246. SELECT 'Branch' head,CASE WHEN @branch IS NULL THEN 'All' ELSE (SELECT agentName from agentMaster where agentId=@branch) END VALUE
  247. UNION ALL
  248. SELECT 'Agent Group' AS head, detailTitle AS VALUE from dbo.staticDataValue where valueId= @agentGrp
  249. SELECT 'Transaction Report Super Agent (Cooperative)' title
  250. END
  251. END TRY
  252. BEGIN CATCH
  253. IF @@TRANCOUNT > 0
  254. ROLLBACK TRANSACTION
  255. SELECT 1 error_code, ERROR_MESSAGE() mes, NULL id, ERROR_LINE()
  256. print error_line()
  257. END CATCH
  258. GO