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.

313 lines
14 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_paidTranReport] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_paidTranReport]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_paidTranReport] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. CREATE PROCEDURE [dbo].[proc_paidTranReport]
  12. @flag VARCHAR(20),
  13. @fromDate VARCHAR(20) = NULL,
  14. @toDate VARCHAR(30) = NULL,
  15. @sCountry VARCHAR(50) = NULL,
  16. @sZone VARCHAR(50) = NULL,
  17. @sDistrict VARCHAR(50) = NULL,
  18. @sLocation VARCHAR(50) = NULL,
  19. @sAgent VARCHAR(50) = NULL,
  20. @sBranch VARCHAR(50) = NULL,
  21. @rCountry VARCHAR(50) = NULL,
  22. @rZone VARCHAR(50) = NULL,
  23. @rDistrict VARCHAR(50) = NULL,
  24. @rLocation VARCHAR(50) = NULL,
  25. @rAgent VARCHAR(50) = NULL,
  26. @rBranch VARCHAR(50) = NULL,
  27. @tranType VARCHAR(50) = NULL,
  28. @user VARCHAR(50) = NULL
  29. AS
  30. SET NOCOUNT ON;
  31. SET ANSI_NULLS ON;
  32. IF @rZone ='All'
  33. set @rZone = null
  34. if @sZone ='All'
  35. set @sZone= null
  36. IF @rDistrict ='All'
  37. set @rDistrict = null
  38. if @sDistrict ='All'
  39. set @sDistrict= null
  40. IF @rLocation ='All'
  41. set @rLocation = null
  42. if @sLocation ='All'
  43. set @sLocation= null
  44. SET @TODATE = @TODATE + ' 23:59:59'
  45. IF @FLAG='A'
  46. BEGIN
  47. IF OBJECT_ID('tempdb..#TEMP_TABLE') IS NOT NULL
  48. DROP TABLE #TEMP_TABLE
  49. select dbo.GetAgentNameFromId(pAgent) [Payout Agent]
  50. ,ltrim(rtrim(dbo.GetAgentNameFromId(sBranch)))+' : '+'<a href = "#" onclick=OpenInNewWindow("'+dbo.FNAGetURL()+'Remit/Transaction/Reports/SearchTransaction.aspx?commentFlag=N&showBankDetail=N&tranId='+CAST(A.id AS VARCHAR)+'") >' + dbo.FNADecryptString(controlNo) +'</a>'+'</br>'+isnull(sen.firstName,'')+' '+isnull(sen.middleName,'')+' '+isnull(sen.lastName1,'')+' '+isnull(sen.lastName2,'') [Sendng Details]
  51. ,isnull(rec.firstName,'')+' '+isnull(rec.middleName,'')+' '+isnull(rec.lastName1,'')+' '+isnull(rec.lastName2,'') [Receiver Name]
  52. ,paidBy+'<br/>'+ convert(varchar,paidDate,107) [Paid Date]
  53. ,cast(dbo.ShowDecimal(cAmt) as varchar)+' '+collCurr [Send Amount]
  54. ,cast(dbo.ShowDecimal(pAmt) as varchar) [Receive Amount]
  55. INTO #TEMP_TABLE
  56. from remitTran A with(nolock)
  57. LEFT JOIN tranReceivers rec WITH(NOLOCK) ON a.id = rec.tranId
  58. LEFT JOIN tranSenders sen WITH(NOLOCK) ON a.id=sen.tranId
  59. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  60. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  61. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  62. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  63. WHERE tranStatus='Paid'
  64. AND paidDate BETWEEN @fromDate AND @toDate
  65. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  66. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  67. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  68. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  69. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  70. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  71. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  72. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  73. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  74. AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  75. AND a.tranType = 'D'
  76. order by pBranch
  77. SELECT DISTINCT [Payout Agent] HEAD FROM #TEMP_TABLE
  78. SELECT [Payout Agent] HEAD,[Sendng Details],[Receiver Name],[Paid Date],[Send Amount],[Receive Amount] FROM #TEMP_TABLE
  79. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  80. SELECT 'From Date ' head, CONVERT(VARCHAR, @fromDate, 101) value
  81. UNION ALL
  82. SELECT 'To Date ' head, CONVERT(VARCHAR, @toDate, 101) value
  83. UNION ALL
  84. SELECT 'Sending Country ' head,ISNULL(@sCountry,'All')
  85. UNION ALL
  86. SELECT 'Sending Zone ' head,ISNULL(@sZone,'All')
  87. UNION ALL
  88. SELECT 'Sending District ' head,ISNULL(@sDistrict,'All')
  89. UNION ALL
  90. SELECT 'Sending Location ' head,ISNULL((select districtName from api_districtList where districtCode=@sLocation),'All')
  91. UNION ALL
  92. SELECT 'Sending Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  93. UNION ALL
  94. SELECT 'Sending Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  95. UNION ALL
  96. SELECT 'Receiving Country ' head,ISNULL(@rCountry,'All')
  97. UNION ALL
  98. SELECT 'Receiving Zone ' head,ISNULL(@rZone,'All')
  99. UNION ALL
  100. SELECT 'Receiving District ' head,ISNULL(@rDistrict,'All')
  101. UNION ALL
  102. SELECT 'Receiving Location ' head,ISNULL((select districtName from api_districtList where districtCode=@rLocation),'All')
  103. UNION ALL
  104. SELECT 'Receiving Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  105. UNION ALL
  106. SELECT 'Receiving Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  107. SELECT 'PAID TRANSACTION REPORT DETAIL (DOMESTIC)' title
  108. END
  109. IF @flag='B'
  110. BEGIN
  111. select dbo.GetAgentNameFromId(pAgent) [Payout Agent]
  112. ,count(*) [Total Count]
  113. ,dbo.ShowDecimal(sum(pAmt)) [Payout Amount]
  114. from remitTran a with(nolock)
  115. LEFT JOIN tranReceivers rec WITH(NOLOCK) ON a.id = rec.tranId
  116. LEFT JOIN tranSenders sen WITH(NOLOCK) ON a.id=sen.tranId
  117. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  118. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  119. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  120. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  121. where tranStatus='Paid'
  122. AND paidDate BETWEEN @fromDate AND @toDate
  123. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  124. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  125. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  126. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  127. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  128. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  129. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  130. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  131. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  132. AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  133. AND a.tranType = 'D'
  134. group by pAgent
  135. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  136. SELECT 'From Date ' head, CONVERT(VARCHAR, @fromDate, 101) value
  137. UNION ALL
  138. SELECT 'To Date ' head, CONVERT(VARCHAR, @toDate, 101) value
  139. UNION ALL
  140. SELECT 'Sending Country ' head,ISNULL(@sCountry,'All')
  141. UNION ALL
  142. SELECT 'Sending Zone ' head,ISNULL(@sZone,'All')
  143. UNION ALL
  144. SELECT 'Sending District ' head,ISNULL(@sDistrict,'All')
  145. UNION ALL
  146. SELECT 'Sending Location ' head,ISNULL((select districtName from api_districtList where districtCode=@sLocation),'All')
  147. UNION ALL
  148. SELECT 'Sending Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  149. UNION ALL
  150. SELECT 'Sending Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  151. UNION ALL
  152. SELECT 'Receiving Country ' head,ISNULL(@rCountry,'All')
  153. UNION ALL
  154. SELECT 'Receiving Zone ' head,ISNULL(@rZone,'All')
  155. UNION ALL
  156. SELECT 'Receiving District ' head,ISNULL(@rDistrict,'All')
  157. UNION ALL
  158. SELECT 'Receiving Location ' head,ISNULL((select districtName from api_districtList where districtCode=@rLocation),'All')
  159. UNION ALL
  160. SELECT 'Receiving Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  161. UNION ALL
  162. SELECT 'Receiving Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  163. SELECT 'PAID TRANSACTION REPORT SUMMARY (DOMESTIC)' title
  164. END
  165. IF @flag='C'
  166. BEGIN
  167. IF OBJECT_ID('tempdb..#temptable') IS NOT NULL
  168. DROP TABLE #temptable
  169. CREATE TABLE #temptable
  170. (
  171. agentId int null,
  172. sendCount int null,
  173. sendAmount money null,
  174. sendHOmargin money null,
  175. sendServiceCharge money null,
  176. sendAgentComm money null,
  177. sendSuperAgentComm money null,
  178. paidCount int null,
  179. paidAmount money null,
  180. paidHOmargin money null,
  181. paidServiceCharge money null,
  182. paidAgentComm money null,
  183. paidSuperAgentComm money null
  184. )
  185. --insert into #temptable
  186. --select sBranch
  187. -- ,sendCount = count(*)
  188. -- ,sendAmount = SUM(cAmt)
  189. -- ,sendHOMargin = sum(isnull(serviceCharge,0)-isnull(sAgentComm,0)-isnull(sSuperAgentComm,0)-isnull(pAgentComm,0)-isnull(pSuperAgentComm,0))
  190. -- ,sendServiceCharge = sum(isnull(serviceCharge,0))
  191. -- ,sendAgentComm = sum(isnull(sAgentComm,0))
  192. -- ,sendSuperAgentComm = sum(isnull(sSuperAgentComm,0))
  193. -- ,0,0,0,0,0,0
  194. --from remitTran a with(nolock)
  195. --LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  196. --LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  197. --LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  198. --LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  199. --where a.approvedDate between @fromDate AND @toDate
  200. -- AND isnull(convert(varchar,a.cancelApprovedDate,101),'') <> convert(varchar,a.approvedDate,101)
  201. -- AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  202. -- AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  203. -- AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  204. -- AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  205. -- AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  206. -- AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  207. -- AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  208. -- AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  209. -- AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  210. -- AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  211. -- AND a.tranType = 'D'
  212. --group by sBranch
  213. insert into #temptable
  214. select
  215. pAgent
  216. ,0,0,0,0,0,0
  217. ,paidCount = count(*)
  218. ,paidAmount = SUM(pAmt)
  219. ,paidHOMargin = sum(isnull(serviceCharge,0)-isnull(sAgentComm,0)-isnull(sSuperAgentComm,0)-isnull(pAgentComm,0)-isnull(pSuperAgentComm,0))
  220. ,paidServiceCharge = sum(isnull(serviceCharge,0))
  221. ,paidAgentComm = sum(isnull(pAgentComm,0))
  222. ,paidSuperAgentComm = sum(isnull(pSuperAgentComm,0))
  223. from remitTran a with(nolock)
  224. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  225. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  226. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  227. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  228. where tranStatus='Paid'
  229. AND a.paidDate between @fromDate AND @toDate
  230. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  231. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  232. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  233. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  234. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  235. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  236. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  237. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  238. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  239. AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  240. AND a.tranType = 'D'
  241. group by pAgent
  242. select
  243. [Agent Name] = am.agentName,
  244. [#Send Count] = sum(isnull(sendCount,0)),
  245. [#Paid Count] = sum(isnull(paidCount,0)),
  246. [Send Amount] = sum(isnull(sendAmount,0)),
  247. [Paid Amount] = sum(isnull(paidAmount,0)),
  248. [Net Sattlement Amount] = sum(isnull(sendAmount,0)) - sum(ISNULL(paidAmount,0)),
  249. [SC] = sum(isnull(sendAgentComm,0)),
  250. [RC] = sum(isnull(paidAgentComm,0)),
  251. [Total Margin] = sum(isnull(sendAgentComm,0)) - sum(isnull(paidAgentComm,0)),
  252. [HO Margin] = sum(isnull(sendHOmargin,0)) + sum(isnull(paidHOmargin,0))
  253. from #temptable temp with(nolock) inner join agentMaster am with(nolock) on temp.agentId = am.agentId
  254. group by am.agentName
  255. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  256. SELECT 'From Date ' head, CONVERT(VARCHAR, @fromDate, 101) value
  257. UNION ALL
  258. SELECT 'To Date ' head, CONVERT(VARCHAR, @toDate, 101) value
  259. UNION ALL
  260. SELECT 'Sending Country ' head,ISNULL(@sCountry,'All')
  261. UNION ALL
  262. SELECT 'Sending Zone ' head,ISNULL(@sZone,'All')
  263. UNION ALL
  264. SELECT 'Sending District ' head,ISNULL(@sDistrict,'All')
  265. UNION ALL
  266. SELECT 'Sending Location ' head,ISNULL((select districtName from api_districtList where districtCode=@sLocation),'All')
  267. UNION ALL
  268. SELECT 'Sending Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  269. UNION ALL
  270. SELECT 'Sending Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  271. UNION ALL
  272. SELECT 'Receiving Country ' head,ISNULL(@rCountry,'All')
  273. UNION ALL
  274. SELECT 'Receiving Zone ' head,ISNULL(@rZone,'All')
  275. UNION ALL
  276. SELECT 'Receiving District ' head,ISNULL(@rDistrict,'All')
  277. UNION ALL
  278. SELECT 'Receiving Location ' head,ISNULL((select districtName from api_districtList where districtCode=@rLocation),'All')
  279. UNION ALL
  280. SELECT 'Receiving Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  281. UNION ALL
  282. SELECT 'Receiving Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  283. SELECT 'PAID TRANSACTION REPORT SUMMARY WITH COMMISSION (DOMESTIC)' title
  284. END
  285. GO