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.

452 lines
19 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_paidTranReportInternational] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_paidTranReportInternational]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_paidTranReportInternational] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. /*
  12. EXEC proc_paidTranReportInternational @FLAG='A',@FROMDATE='10/1/2012',@TODATE='10/31/2012'
  13. EXEC proc_paidTranReportInternational @FLAG='C',@FROMDATE='09/20/2012',@TODATE='09/12/2012'
  14. */
  15. CREATE PROCEDURE [dbo].[proc_paidTranReportInternational]
  16. @flag VARCHAR(20),
  17. @fromDate VARCHAR(20) = NULL,
  18. @toDate VARCHAR(30) = NULL,
  19. @sCountry VARCHAR(50) = NULL,
  20. @sZone VARCHAR(50) = NULL,
  21. @sDistrict VARCHAR(50) = NULL,
  22. @sLocation VARCHAR(50) = NULL,
  23. @sAgent VARCHAR(50) = NULL,
  24. @sBranch VARCHAR(50) = NULL,
  25. @rCountry VARCHAR(50) = NULL,
  26. @rZone VARCHAR(50) = NULL,
  27. @rDistrict VARCHAR(50) = NULL,
  28. @rLocation VARCHAR(50) = NULL,
  29. @rAgent VARCHAR(50) = NULL,
  30. @rBranch VARCHAR(50) = NULL,
  31. @tranType VARCHAR(50) = NULL,
  32. @user VARCHAR(50) = NULL
  33. AS
  34. SET NOCOUNT ON;
  35. SET ANSI_NULLS ON;
  36. IF @rZone ='All'
  37. set @rZone = null
  38. if @sZone ='All'
  39. set @sZone= null
  40. IF @rDistrict ='All'
  41. set @rDistrict = null
  42. if @sDistrict ='All'
  43. set @sDistrict= null
  44. IF @rLocation ='All'
  45. set @rLocation = null
  46. if @sLocation ='All'
  47. set @sLocation= null
  48. SET @TODATE = @TODATE + ' 23:59:59'
  49. IF @FLAG='A'
  50. BEGIN
  51. IF OBJECT_ID('tempdb..#TEMP_TABLE') IS NOT NULL
  52. DROP TABLE #TEMP_TABLE
  53. select dbo.GetAgentNameFromId(isnull(pBranch,pBankBranch)) [Payout Agent]
  54. ,ltrim(rtrim(dbo.GetAgentNameFromId(sBranch)))+' ('+sCountry+') : '+dbo.FNADecryptString(controlNo)+'</br>'+isnull(sen.firstName,'')+' '+isnull(sen.middleName,'')+' '+isnull(sen.lastName1,'')+' '+isnull(sen.lastName2,'') [Sendng Details]
  55. ,isnull(rec.firstName,'')+' '+isnull(rec.middleName,'')+' '+isnull(rec.lastName1,'')+' '+isnull(rec.lastName2,'') [Receiver Name]
  56. ,paidBy+'<br/>'+ convert(varchar,paidDate,107) [Paid Date]
  57. ,cast(dbo.ShowDecimal(cAmt) as varchar)+' '+collCurr [Send Amount]
  58. ,cast(dbo.ShowDecimal(pAmt) as varchar) [Receive Amount]
  59. INTO #TEMP_TABLE
  60. from remitTran A with(nolock)
  61. LEFT JOIN tranReceivers rec WITH(NOLOCK) ON a.id = rec.tranId
  62. LEFT JOIN tranSenders sen WITH(NOLOCK) ON a.id=sen.tranId
  63. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  64. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  65. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  66. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  67. WHERE tranStatus='Paid'
  68. AND paidDate BETWEEN @fromDate AND @toDate
  69. AND sCountry<>'Nepal'
  70. AND sCountry = ISNULL(@sCountry,sCountry)
  71. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  72. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  73. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  74. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  75. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  76. AND isnull(pCountry,'') = ISNULL(@rCountry,isnull(pCountry,''))
  77. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  78. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  79. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  80. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  81. AND isnull(pBranch,pBankBranch) = ISNULL(@rBranch,isnull(pBranch,''))
  82. order by pBranch
  83. SELECT DISTINCT [Payout Agent] HEAD FROM #TEMP_TABLE
  84. SELECT [Payout Agent] HEAD,[Sendng Details],[Receiver Name],[Paid Date],[Send Amount],[Receive Amount] FROM #TEMP_TABLE
  85. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  86. SELECT 'From Date ' head, CONVERT(VARCHAR, @fromDate, 101) value
  87. UNION ALL
  88. SELECT 'To Date ' head, CONVERT(VARCHAR, @toDate, 101) value
  89. UNION ALL
  90. SELECT 'Sending Country ' head,ISNULL(@sCountry,'All')
  91. UNION ALL
  92. SELECT 'Sending Zone ' head,ISNULL(@sZone,'All')
  93. UNION ALL
  94. SELECT 'Sending District ' head,ISNULL(@sDistrict,'All')
  95. UNION ALL
  96. SELECT 'Sending Location ' head,ISNULL((select districtName from api_districtList where districtCode=@sLocation),'All')
  97. UNION ALL
  98. SELECT 'Sending Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  99. UNION ALL
  100. SELECT 'Sending Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  101. UNION ALL
  102. SELECT 'Receiving Country ' head,ISNULL(@rCountry,'All')
  103. UNION ALL
  104. SELECT 'Receiving Zone ' head,ISNULL(@rZone,'All')
  105. UNION ALL
  106. SELECT 'Receiving District ' head,ISNULL(@rDistrict,'All')
  107. UNION ALL
  108. SELECT 'Receiving Location ' head,ISNULL((select districtName from api_districtList where districtCode=@rLocation),'All')
  109. UNION ALL
  110. SELECT 'Receiving Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  111. UNION ALL
  112. SELECT 'Receiving Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  113. SELECT 'PAID TRANSACTION REPORT DETAIL (INTERNATIONAL)' title
  114. END
  115. IF @flag='B'
  116. BEGIN
  117. select dbo.GetAgentNameFromId(isnull(pBranch,pBankBranch)) [Payout Agent]
  118. ,count(*) [Total Count]
  119. ,dbo.ShowDecimal(sum(pAmt)) [Payout Amount]
  120. from remitTran a with(nolock)
  121. LEFT JOIN tranReceivers rec WITH(NOLOCK) ON a.id = rec.tranId
  122. LEFT JOIN tranSenders sen WITH(NOLOCK) ON a.id=sen.tranId
  123. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  124. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  125. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  126. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  127. where tranStatus='Paid'
  128. AND paidDate BETWEEN @fromDate AND @toDate
  129. AND sCountry<>'Nepal'
  130. AND sCountry = ISNULL(@sCountry,sCountry)
  131. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  132. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  133. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  134. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  135. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  136. AND isnull(pCountry,'') = ISNULL(@rCountry,isnull(pCountry,''))
  137. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  138. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  139. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  140. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  141. AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  142. group by pBranch,pBankBranch
  143. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  144. SELECT 'From Date ' head, CONVERT(VARCHAR, @fromDate, 101) value
  145. UNION ALL
  146. SELECT 'To Date ' head, CONVERT(VARCHAR, @toDate, 101) value
  147. UNION ALL
  148. SELECT 'Sending Country ' head,ISNULL(@sCountry,'All')
  149. UNION ALL
  150. SELECT 'Sending Zone ' head,ISNULL(@sZone,'All')
  151. UNION ALL
  152. SELECT 'Sending District ' head,ISNULL(@sDistrict,'All')
  153. UNION ALL
  154. SELECT 'Sending Location ' head,ISNULL((select districtName from api_districtList where districtCode=@sLocation),'All')
  155. UNION ALL
  156. SELECT 'Sending Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  157. UNION ALL
  158. SELECT 'Sending Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  159. UNION ALL
  160. SELECT 'Receiving Country ' head,ISNULL(@rCountry,'All')
  161. UNION ALL
  162. SELECT 'Receiving Zone ' head,ISNULL(@rZone,'All')
  163. UNION ALL
  164. SELECT 'Receiving District ' head,ISNULL(@rDistrict,'All')
  165. UNION ALL
  166. SELECT 'Receiving Location ' head,ISNULL((select districtName from api_districtList where districtCode=@rLocation),'All')
  167. UNION ALL
  168. SELECT 'Receiving Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  169. UNION ALL
  170. SELECT 'Receiving Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  171. SELECT 'PAID TRANSACTION REPORT SUMMARY (INTERNATIONAL)' title
  172. END
  173. IF @flag='C'
  174. BEGIN
  175. IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL
  176. DROP TABLE #tempTable
  177. IF OBJECT_ID('tempdb..#tempTable1') IS NOT NULL
  178. DROP TABLE #tempTable1
  179. CREATE TABLE #tempTable
  180. (
  181. agentId int null,
  182. sendCount int null,
  183. sendAmount money null,
  184. sendHOmargin money null,
  185. sendServiceCharge money null,
  186. sendAgentComm money null,
  187. sendSuperAgentComm money null,
  188. paidCount int null,
  189. paidAmount money null,
  190. paidHOmargin money null,
  191. paidServiceCharge money null,
  192. paidAgentComm money null,
  193. paidSuperAgentComm money null
  194. )
  195. CREATE TABLE #tempTable1
  196. (
  197. agentId int null,
  198. sendCount int null,
  199. sendAmount money null,
  200. sendHOmargin money null,
  201. sendServiceCharge money null,
  202. sendAgentComm money null,
  203. sendSuperAgentComm money null,
  204. paidCount int null,
  205. paidAmount money null,
  206. paidHOmargin money null,
  207. paidServiceCharge money null,
  208. paidAgentComm money null,
  209. paidSuperAgentComm money null
  210. )
  211. insert into #tempTable(agentId,sendCount,sendAmount,sendHOmargin,sendServiceCharge,sendAgentComm,sendSuperAgentComm
  212. ,paidCount,paidAmount,paidHOmargin,paidServiceCharge,paidAgentComm,paidSuperAgentComm)
  213. select sBranch,sendCount,sendAmount,sendHOMargin,sendServiceCharge,sendAgentComm,sendSuperAgentComm,
  214. paidCount,paidAmount,paidHOMargin,paidServiceCharge,paidAgentComm,paidSuperAgentComm
  215. from
  216. (
  217. select sBranch
  218. ,count(*) sendCount
  219. ,SUM(cAmt) sendAmount
  220. ,sum(isnull(serviceCharge,0)-isnull(sAgentComm,0)-isnull(sSuperAgentComm,0)-isnull(pAgentComm,0)-isnull(pSuperAgentComm,0)) sendHOMargin
  221. ,sum(isnull(serviceCharge,0)) sendServiceCharge
  222. ,sum(isnull(sAgentComm,0)) sendAgentComm
  223. ,sum(isnull(sSuperAgentComm,0)) sendSuperAgentComm
  224. from remitTran a with(nolock)
  225. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  226. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  227. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  228. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  229. where tranStatus='Paid'
  230. and paidDate between @fromDate and @toDate
  231. AND sCountry<>'Nepal'
  232. AND sCountry = ISNULL(@sCountry,sCountry)
  233. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  234. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  235. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  236. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  237. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  238. AND isnull(pCountry,'') = ISNULL(@rCountry,isnull(pCountry,''))
  239. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  240. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  241. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  242. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  243. AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  244. group by sBranch,pBankBranch
  245. )x
  246. inner join
  247. (
  248. select pBranch
  249. ,count(*) paidCount
  250. ,SUM(pAmt) paidAmount
  251. ,sum(isnull(serviceCharge,0)-isnull(sAgentComm,0)-isnull(sSuperAgentComm,0)-isnull(pAgentComm,0)-isnull(pSuperAgentComm,0)) paidHOMargin
  252. ,sum(isnull(serviceCharge,0)) paidServiceCharge
  253. ,sum(isnull(pAgentComm,0)) paidAgentComm
  254. ,sum(isnull(pSuperAgentComm,0)) paidSuperAgentComm
  255. from remitTran a with(nolock)
  256. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  257. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  258. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  259. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  260. where tranStatus='Paid'
  261. and paidDate between @fromDate and @toDate
  262. AND sCountry<>'Nepal'
  263. AND sCountry = ISNULL(@sCountry,sCountry)
  264. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  265. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  266. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  267. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  268. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  269. AND isnull(pCountry,'') = ISNULL(@rCountry,isnull(pCountry,''))
  270. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  271. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  272. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  273. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  274. AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  275. group by pBranch
  276. )y on y.pBranch=x.sBranch
  277. insert into #tempTable1(agentId,paidCount,paidAmount,paidHOmargin,paidServiceCharge,paidAgentComm,paidSuperAgentComm)
  278. select pBranch,paidCount,paidAmount,paidHOMargin,paidServiceCharge,paidAgentComm,paidSuperAgentComm
  279. from
  280. (
  281. select pBranch
  282. ,count(*) paidCount
  283. ,SUM(pAmt) paidAmount
  284. ,sum(isnull(serviceCharge,0)-isnull(sAgentComm,0)-isnull(sSuperAgentComm,0)-isnull(pAgentComm,0)-isnull(pSuperAgentComm,0)) paidHOMargin
  285. ,sum(isnull(serviceCharge,0)) paidServiceCharge
  286. ,sum(isnull(pAgentComm,0)) paidAgentComm
  287. ,sum(isnull(pSuperAgentComm,0)) paidSuperAgentComm
  288. from remitTran a with(nolock)
  289. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  290. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  291. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  292. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  293. where tranStatus='Paid'
  294. and paidDate between @fromDate and @toDate
  295. AND sCountry<>'Nepal'
  296. AND sCountry = ISNULL(@sCountry,sCountry)
  297. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  298. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  299. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  300. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  301. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  302. AND isnull(pCountry,'') = ISNULL(@rCountry,isnull(pCountry,''))
  303. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  304. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  305. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  306. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  307. AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  308. group by pBranch
  309. )b
  310. DELETE FROM #tempTable1
  311. FROM #tempTable1 t
  312. INNER JOIN #tempTable ds ON t.agentId = ds.agentId
  313. insert into #tempTable
  314. select * from #tempTable1
  315. delete from #tempTable1
  316. insert into #tempTable1(agentId,sendCount,sendAmount,sendHOmargin,sendServiceCharge,sendAgentComm,sendSuperAgentComm)
  317. select sBranch,sendCount,sendAmount,sendHOMargin,sendServiceCharge,sendAgentComm,sendSuperAgentComm
  318. from
  319. (
  320. select sBranch
  321. ,count(*) sendCount
  322. ,SUM(cAmt) sendAmount
  323. ,sum(isnull(serviceCharge,0)-isnull(sAgentComm,0)-isnull(sSuperAgentComm,0)-isnull(pAgentComm,0)-isnull(pSuperAgentComm,0)) sendHOMargin
  324. ,sum(isnull(serviceCharge,0)) sendServiceCharge
  325. ,sum(isnull(sAgentComm,0)) sendAgentComm
  326. ,sum(isnull(sSuperAgentComm,0)) sendSuperAgentComm
  327. from remitTran a with(nolock)
  328. LEFT JOIN agentMaster B with(nolock) ON a.sBranch=b.agentId
  329. LEFT JOIN agentMaster C with(nolock) ON a.pBranch=C.agentId
  330. LEFT JOIN api_districtList D WITH(NOLOCK) ON D.ROWID=B.agentLocation
  331. LEFT JOIN api_districtList E WITH(NOLOCK) ON E.ROWID=C.agentLocation
  332. where tranStatus='Paid'
  333. AND sCountry<>'Nepal'
  334. and paidDate between @fromDate and @toDate
  335. AND sCountry = ISNULL(@sCountry,sCountry)
  336. AND isnull(B.agentState,'') =ISNULL(@sZone,isnull(B.agentState,''))
  337. AND isnull(B.agentDistrict,'') =ISNULL(@sDistrict,isnull(B.agentDistrict,''))
  338. AND isnull(B.agentLocation,'') =ISNULL(@sLocation,isnull(B.agentLocation,''))
  339. AND isnull(sAgent,'') = ISNULL(@sAgent,isnull(sAgent,''))
  340. AND isnull(sBranch,'') = ISNULL(@sBranch,isnull(sBranch,''))
  341. AND isnull(pCountry,'') = ISNULL(@rCountry,isnull(pCountry,''))
  342. AND isnull(C.agentState,'') =ISNULL(@rZone,isnull(C.agentState,''))
  343. AND isnull(C.agentDistrict,'') =ISNULL(@rDistrict,isnull(C.agentDistrict,''))
  344. AND isnull(C.agentLocation,'') =ISNULL(@rLocation,isnull(C.agentLocation,''))
  345. AND isnull(pAgent,'') = ISNULL(@rAgent,isnull(pAgent,''))
  346. AND isnull(pBranch,'') = ISNULL(@rBranch,isnull(pBranch,''))
  347. group by sBranch
  348. )b
  349. DELETE FROM #tempTable1
  350. FROM #tempTable1 t
  351. INNER JOIN #tempTable ds ON t.agentId = ds.agentId
  352. insert into #tempTable
  353. select * from #tempTable1
  354. select
  355. b.agentName [Agent Name]
  356. ,isnull(sendCount,0) [#Send Count]
  357. ,ISNULL(paidCount,0) [#Paid Count]
  358. ,ISNULL(sendAmount,0) [Send Amount]
  359. ,ISNULL(paidAmount,0) [Paid Amount]
  360. ,ISNULL(sendAmount,0)-ISNULL(paidAmount,0) [Net Sattlement Amount]
  361. ,ISNULL(sendAgentComm,0) [SC]
  362. ,ISNULL(paidAgentComm,0) [RC]
  363. ,ISNULL(sendAgentComm,0)+ISNULL(paidAgentComm,0) [Total Margin]
  364. ,isnull(sendHOmargin,0)+ISNULL(paidHOmargin,0) [HO Margin]
  365. from #tempTable a with(nolock) inner join agentMaster b with(nolock) on a.agentId=b.agentId
  366. order by b.agentName
  367. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  368. SELECT 'From Date ' head, CONVERT(VARCHAR, @fromDate, 101) value
  369. UNION ALL
  370. SELECT 'To Date ' head, CONVERT(VARCHAR, @toDate, 101) value
  371. UNION ALL
  372. SELECT 'Sending Country ' head,ISNULL(@sCountry,'All')
  373. UNION ALL
  374. SELECT 'Sending Zone ' head,ISNULL(@sZone,'All')
  375. UNION ALL
  376. SELECT 'Sending District ' head,ISNULL(@sDistrict,'All')
  377. UNION ALL
  378. SELECT 'Sending Location ' head,ISNULL((select districtName from api_districtList where districtCode=@sLocation),'All')
  379. UNION ALL
  380. SELECT 'Sending Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  381. UNION ALL
  382. SELECT 'Sending Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  383. UNION ALL
  384. SELECT 'Receiving Country ' head,ISNULL(@rCountry,'All')
  385. UNION ALL
  386. SELECT 'Receiving Zone ' head,ISNULL(@rZone,'All')
  387. UNION ALL
  388. SELECT 'Receiving District ' head,ISNULL(@rDistrict,'All')
  389. UNION ALL
  390. SELECT 'Receiving Location ' head,ISNULL((select districtName from api_districtList where districtCode=@rLocation),'All')
  391. UNION ALL
  392. SELECT 'Receiving Agent ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @sAgent),'All')
  393. UNION ALL
  394. SELECT 'Receiving Branch ' head,ISNULL((SELECT agentName FROM agentMaster with(nolock) WHERE agentId = @rAgent),'All')
  395. SELECT 'PAID TRANSACTION REPORT SUMMARY WITH COMMISSION (INTERNATIONAL)' title
  396. END
  397. GO