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.

408 lines
31 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_bonusRpt] 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_bonusRpt]
  9. (
  10. @flag VARCHAR(50)
  11. ,@fromDate VARCHAR(50) = NULL
  12. ,@toDate VARCHAR(50) = NULL
  13. ,@mFrom varchar(50) = null
  14. ,@mTo varchar(50) = null
  15. ,@customerId VARCHAR(50) = NULL
  16. ,@branchId VARCHAR(50) = NULL
  17. ,@orderBy VARCHAR(100) = NULL
  18. ,@membershipId VARCHAR(100) = NULL
  19. ,@user VARCHAR(100) = NULL
  20. ,@prizeId VARCHAR(20) = NULL
  21. ,@pageNumber INT = 1
  22. ,@pageSize INT = 50
  23. )
  24. AS
  25. SET NOCOUNT ON
  26. SET XACT_ABORT ON
  27. declare @sql varchar(max)
  28. , @table as varchar(max)
  29. , @oldToDate varchar(50) = @toDate
  30. SET @toDate = @toDate+' 23:59:59'
  31. if @flag = 'bRpt'
  32. begin
  33. declare @subsql varchar(max) = 'select
  34. sen.customerId
  35. ,sen.membershipId
  36. ,earnedBonus = sum(rt.bonusPoint)
  37. from remitTran rt with(nolock)
  38. inner join tranSenders sen with(nolock) on rt.id = sen.tranId
  39. where rt.approvedDateLocal between '''+@fromDate+''' and '''+@toDate+'''
  40. and rt.isBonusUpdated = ''Y'''
  41. if @membershipId is not null
  42. set @subsql = @subsql+ ' and sen.membershipId = '''+@membershipId+''''
  43. set @subsql = @subsql+ 'group by sen.customerId,sen.membershipId'
  44. set @sql = '
  45. select
  46. [S.N.] = ROW_NUMBER() over(order by x.earnedBonus desc)
  47. ,[Membership Id] = ''<a href = "#" onclick="OpenInNewWindow('''''+dbo.FNAGetURL()+'SwiftSystem/Reports/Reports.aspx?reportName=20167200&flag=bRptDrildown&fromDate='+@fromDate+'&toDate='+@oldToDate+'&membershipId=''+ x.membershipId +'''''')">''+ x.membershipId +''</a>''
  48. ,[Customer Name] = cm.firstName+'' ''+isnull(cm.middleName,'''')+'' ''+ isnull(cm.lastName,'''')
  49. ,[Earned Bonus] = x.earnedBonus
  50. ,[Zone] = pZone
  51. ,[District] = pDistrict
  52. ,[VDC/Municipality] = pMunicipality
  53. ,[Mobile] = mobile
  54. ,[Email] = email
  55. ,[Citizenship No] = citizenshipNo
  56. from
  57. (
  58. '+@subsql+'
  59. )x
  60. inner join customerMaster cm with(nolock) on x.customerId = cm.customerId
  61. where x.earnedBonus between '''+@mFrom+''' and '''+@mTo+''''
  62. EXEC(@sql)
  63. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  64. SELECT 'Date Range From ' head, @fromDate +' - '+ @toDate value
  65. UNION ALL
  66. SELECT 'Bonus Range From ' head, @mFrom +' - '+ @mTo value
  67. UNION ALL
  68. SELECT 'Order By' head, @orderBy value
  69. UNION ALL
  70. SELECT 'Membership Id' head, @membershipId value
  71. SELECT 'Customer Bonus Report ' title
  72. end
  73. if @flag = 'bRptDrildown'
  74. begin
  75. select
  76. [S.N.] = row_number() over(order by rt.id)
  77. ,[Tran Id] = rt.id
  78. ,[Control No] = dbo.fnadecryptstring(rt.controlNo)
  79. ,[Sender Name] = senderName
  80. ,[Receiver Name] = receiverName
  81. ,[Confirm Date] = rt.approvedDateLocal
  82. ,[Earned Bonus] = rt.bonusPoint
  83. ,[Coll. Amt] = cAmt
  84. ,[Payout Amt] = pAmt
  85. ,[Service Charge] = serviceCharge
  86. ,[Payout Agent Comm.] = pAgentComm
  87. ,[Sending Agent Comm.] = sAgentComm
  88. from remitTran rt with(nolock)
  89. inner join tranSenders sen with(nolock) on rt.id = sen.tranId
  90. where rt.approvedDateLocal between @fromDate and @toDate and rt.isBonusUpdated = 'Y'
  91. and sen.membershipId = @membershipId
  92. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  93. SELECT 'Date Range From ' head, @fromDate +' - '+ @toDate value
  94. UNION ALL
  95. SELECT 'Order By' head, @orderBy value
  96. UNION ALL
  97. SELECT 'Membership Id' head, @membershipId value
  98. SELECT 'Customer Bonus Transaction Detail Report ' title
  99. end
  100. IF @flag='bonusPoint'
  101. BEGIN
  102. if @orderBy = 'CustomerName'
  103. set @orderBy = '[Sender Name]'
  104. if @orderBy = 'BonusEarned'
  105. set @orderBy = '[Bonus Earned]'
  106. if @orderBy = 'Branch'
  107. set @orderBy = '[Branch Name]'
  108. IF ISNULL(@mFrom, 0) = 0
  109. SET @mFrom = 1
  110. set @table =
  111. 'select
  112. [Sender Name] = ISNULL('' ''+ c.firstName,'''') + ISNULL('' '' + c.middleName, '''') + ISNULL('' '' + c.lastName, '''')
  113. , [Sender Citizenship No] = c.citizenshipNo
  114. , [Sender Mobile/Phone] = c.mobile
  115. , [Bonus Pending] = convert(int,round(isnull(bonusPointPending,0),0))
  116. , [Bonus Earned] = convert(int,round((isnull(bonusPoint,0) + ISNULL(Redeemed, 0)),0))
  117. , [Available Bonus] = convert(int,round((isnull(bonusPoint,0)),0))
  118. FROM customerMaster c with(nolock)
  119. LEFT JOIN agentMaster am with(nolock) on c.agentId = am.agentId
  120. WHERE (isnull(bonusPointPending,0) between '''+@mFrom+''' and '''+@mTo+''' OR ISNULL(bonusPoint, 0) BETWEEN ''' + @mFrom + ''' AND ''' + @mTo + ''')'
  121. if @membershipId is not null
  122. set @table = @table+ ' and c.membershipId = '''+@membershipId+''''
  123. SET @sql = 'SELECT
  124. COUNT(*) AS TXNCOUNT
  125. ,' + CAST(@pageSize AS VARCHAR) + ' PAGESIZE
  126. ,' + CAST(@pageNumber AS VARCHAR) + ' PAGENUMBER
  127. FROM (' + @table + ') x'
  128. PRINT @sql
  129. EXEC (@sql)
  130. SET @sql = '
  131. SELECT
  132. *
  133. FROM (
  134. SELECT
  135. ROW_NUMBER() OVER (ORDER BY '+@orderBy+') AS [S.N],*
  136. FROM (' + @table + ') x
  137. ) AS tmp WHERE tmp.[S.N] BETWEEN ' + CAST(((@pageNumber - 1) * @pageSize + 1) AS VARCHAR) + ' AND ' + CAST(@pageNumber * @pageSize AS VARCHAR)
  138. PRINT @sql
  139. EXEC (@sql)
  140. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  141. SELECT 'Bonus From ' head, @mFrom value
  142. union all
  143. SELECT 'Bonus To ' head, @mTo value
  144. union all
  145. select 'Branch Name' head, case when @branchId is not null then (select agentName from agentMaster with(nolock) where agentId = @branchId)
  146. else 'All' end
  147. union all
  148. select 'Order By' head, @orderBy value
  149. union all
  150. select 'Membership Id' head, @membershipId value
  151. SELECT 'Customer Bonus Point Report ' title
  152. END
  153. /*
  154. if @flag = 'bonusRedeemed'
  155. begin
  156. if @orderBy = 'CustomerName'
  157. set @orderBy = '[Sender Name]'
  158. if @orderBy = 'RedeemedDate'
  159. set @orderBy = '[Redeemed_Date]'
  160. if @orderBy = 'Branch'
  161. set @orderBy = '[Branch Name]'
  162. if @orderBy = 'GiftItem'
  163. set @orderBy = '[Gift Item]'
  164. declare @tbl table (membershipId varchar(50),hoComm money, txnCount int)
  165. IF OBJECT_ID('tempdb..#tbl') IS NOT NULL
  166. DROP TABLE #tbl
  167. CREATE TABLE #tbl
  168. (
  169. membershipId varchar(50),
  170. hoComm money,
  171. txnCount varchar(50)
  172. )
  173. insert into #tbl
  174. select
  175. membershipId = cm.customerCardNo,
  176. hoComm = sum(isnull(serviceCharge,0)-isnull(sAgentComm,0)-isnull(pAgentComm,0)),
  177. txnCount = count('x')
  178. from remitTran rt with(nolock)
  179. inner join tranSenders sen with(nolock) on rt.id = sen.tranId
  180. inner join customerMaster cm with(nolock) on cm.customerCardNo = sen.membershipId
  181. inner join bonusRedeemHistory red with(nolock) on red.customerId = cm.id
  182. where red.approvedDate between @fromDate and @toDate
  183. and cm.customerCardNo = isnull(@membershipId,cm.customerCardNo)
  184. and red.prizeId = isnull(@prizeId, red.prizeId)
  185. group by cm.customerCardNo
  186. set @table =
  187. 'select
  188. [Ref. No] = red.redeemId
  189. , [Membership Id] = c.customerCardNo
  190. , [Agent] = am.agentName
  191. , [Sender Name] = ISNULL('' ''+ c.firstName,'''') + ISNULL('' '' + c.middleName, '''') + ISNULL('' '' + c.lastName, '''')
  192. , [Sender (ID Type/No)] = dbo.FNAGetDataValue(ci.idType)+'' - ''+ cast(ci.idNumber as varchar)
  193. , [Sender Contact] = c.mobile
  194. , [No. of Txn] = ''<a href = "#" onclick="OpenInNewWindow('''''+dbo.FNAGetURL()+'SwiftSystem/Reports/Reports.aspx?reportName=20167200&flag=txn-detail&branchId='+isnull(@branchId,'')+'&giftItem='+isnull(@prizeId,'')+'&from='+@fromDate+'&to='+@oldToDate+'&membershipId=''+ c.customerCardNo +'''''')">''+ txn.txnCount +''</a>''
  195. , [HO <br/>Commission] = txn.hoComm
  196. , [Bonus Point_Total] = cast(ISNULL(red.currentMilage, 0) as int)
  197. , [Bonus Point_Redeemed] = convert(int,round(isnull(red.deductMilage,0),0))
  198. , [Bonus Point_Remaining] = cast(ISNULL(red.currentMilage, 0) - isnull(red.deductMilage,0) as int)
  199. , [Gift Item] = dbo.FNAGetDataValue(red.prizeId)
  200. , [Request_Date] = red.createdDate
  201. , [Request_User] = red.createdBy
  202. , [Approved_Date] = red.approvedDate
  203. , [Approved_By] = red.approvedBy
  204. from customerMaster c with(nolock)
  205. inner join #tbl txn on c.customerCardNo = txn.membershipId
  206. inner join bonusRedeemHistory red with(nolock) on c.id = red.customerId
  207. left join customerIdentity ci with(nolock) on c.id = ci.customerId
  208. left join agentMaster am with(nolock) on red.branchId = am.agentId
  209. where red.approvedDate between '''+@fromDate+''' and '''+@toDate+''''
  210. if @branchId is not null
  211. set @table = @table+ ' and red.branchId = '''+@branchId+''''
  212. if @membershipId is not null
  213. set @table = @table+ ' and c.customerCardNo = '''+@membershipId+''''
  214. if @prizeId is not null
  215. set @table = @table+ ' and red.prizeId = '''+@prizeId+''''
  216. SET @sql = 'SELECT
  217. COUNT(*) AS TXNCOUNT
  218. ,' + CAST(@pageSize AS VARCHAR) + ' PAGESIZE
  219. ,' + CAST(@pageNumber AS VARCHAR) + ' PAGENUMBER
  220. FROM (' + @table + ') x'
  221. PRINT @sql
  222. EXEC (@sql)
  223. SET @sql = '
  224. SELECT
  225. *
  226. FROM (
  227. SELECT
  228. ROW_NUMBER() OVER (ORDER BY '+@orderBy+') AS [S.N],*
  229. FROM (' + @table + ') x
  230. ) AS tmp WHERE tmp.[S.N] BETWEEN ' + CAST(((@pageNumber - 1) * @pageSize + 1) AS VARCHAR) + ' AND ' + CAST(@pageNumber * @pageSize AS VARCHAR)
  231. PRINT @sql
  232. EXEC (@sql)
  233. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  234. select 'Branch Name' head, case when @branchId is not null then (select agentName from agentMaster with(nolock) where agentId = @branchId)
  235. else 'All' end value
  236. union all
  237. SELECT 'From Date' head, @fromDate value
  238. union all
  239. SELECT 'To Date' head, @toDate value
  240. union all
  241. select 'Gift Item' head,case when @prizeId is not null then (select dbo.FNAGetDataValue(@prizeId)) else 'All' end value
  242. union all
  243. select 'Order By' head, @orderBy value
  244. union all
  245. select 'Membership Id' head, @membershipId value
  246. SELECT 'Customer Bonus Redeemed Report ' title
  247. end
  248. if @flag ='txn-detail'
  249. begin
  250. SELECT
  251. [S.N.] = row_number()over(order by rt.id)
  252. ,[Control No] ='<a href = "#" onclick="OpenInNewWindow('''+dbo.FNAGetURL()+'Remit/Transaction/Reports/SearchTransaction.aspx?commentFlag=N&showBankDetail=N&tranId='+CAST(rt.id AS VARCHAR)+''')">'+dbo.FNADecryptString(controlNo)+'</a>'
  253. ,[Sending_Country] = sCountry
  254. ,[Sending_Agent] = sBranchName
  255. ,[Sending_Amt] = tAmt
  256. ,[Sending_Currency] = collCurr
  257. ,[Receiving_Country] = ISNULL(pCountry,'-')
  258. ,[Receiving_Branch] = case when rt.paymentMethod='Bank Deposit' then rt.pBankBranchName else ISNULL(rt.pBranchName,'-') end
  259. ,[Receiving_Amt] = pAmt
  260. ,[Tran Type] = rt.paymentMethod
  261. ,[Sender Name] = sen.firstName + ISNULL(' ' + sen.middleName, '') + ISNULL(' ' + sen.lastName1, '') + ISNULL(' ' + sen.lastName2,'')
  262. ,[Receiver Name] = rec.firstName + ISNULL(' ' + rec.middleName, '') + ISNULL(' ' + rec.lastName1, '') + ISNULL(' ' + rec.lastName2, '')
  263. FROM remitTran rt WITH(NOLOCK)
  264. inner join tranSenders sen WITH(NOLOCK) ON rt.id=sen.tranId
  265. inner join tranReceivers rec WITH(NOLOCK) ON rt.id=rec.tranId
  266. inner join bonusRedeemHistory red with(nolock) on red.customerId = sen.customerId
  267. where sen.membershipId = @membershipId
  268. and red.approvedDate between @fromDate and @toDate
  269. and red.prizeId = isnull(@prizeId,red.prizeId)
  270. and red.branchId = isnull(@branchId,red.branchId)
  271. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  272. select 'Branch Name' head, case when @branchId is not null then (select agentName from agentMaster with(nolock) where agentId = @branchId)
  273. else 'All' end value
  274. union all
  275. SELECT 'From Date' head, @fromDate value
  276. union all
  277. SELECT 'To Date' head, @toDate value
  278. union all
  279. select 'Gift Item' head,case when @prizeId is not null then (select dbo.FNAGetDataValue(@prizeId)) else 'All' end value
  280. union all
  281. select 'Order By' head, @orderBy value
  282. union all
  283. select 'Membership Id' head, @membershipId value
  284. SELECT 'Customer Bonus - TXN Report ' title
  285. end
  286. if @flag = 'red-agent'
  287. begin
  288. if @orderBy = 'CustomerName'
  289. set @orderBy = '[Sender Name]'
  290. if @orderBy = 'RedeemedDate'
  291. set @orderBy = '[Redeemed_Date]'
  292. if @orderBy = 'Branch'
  293. set @orderBy = '[Branch Name]'
  294. if @orderBy = 'GiftItem'
  295. set @orderBy = '[Gift Item]'
  296. set @table =
  297. 'select
  298. [Ref. No] = red.redeemId
  299. , [Membership Id] = c.customerCardNo
  300. , [Agent] = am.agentName
  301. , [Sender Name] = ISNULL('' ''+ c.firstName,'''') + ISNULL('' '' + c.middleName, '''') + ISNULL('' '' + c.lastName, '''')
  302. , [Sender (ID Type/No)] = dbo.FNAGetDataValue(ci.idType)+'' - ''+ cast(ci.idNumber as varchar)
  303. , [Sender Contact] = c.mobile
  304. , [Bonus Point_Total] = cast(ISNULL(red.currentMilage, 0) as int)
  305. , [Bonus Point_Redeemed] = convert(int,round(isnull(red.deductMilage,0),0))
  306. , [Bonus Point_Remaining] = cast(ISNULL(red.currentMilage, 0) - isnull(red.deductMilage,0) as int)
  307. , [Gift Item] = dbo.FNAGetDataValue(red.prizeId)
  308. , [Request_Date] = red.createdDate
  309. , [Request_User] = red.createdBy
  310. , [Approved_Date] = red.approvedDate
  311. , [Approved_By] = red.approvedBy
  312. from customerMaster c with(nolock)
  313. inner join bonusRedeemHistory red with(nolock) on c.id = red.customerId
  314. left join customerIdentity ci with(nolock) on c.id = ci.customerId
  315. left join agentMaster am with(nolock) on red.branchId = am.agentId
  316. where red.approvedDate between '''+@fromDate+''' and '''+@toDate+''''
  317. if @branchId is not null
  318. set @table = @table+ ' and red.branchId = '''+@branchId+''''
  319. if @membershipId is not null
  320. set @table = @table+ ' and c.customerCardNo = '''+@membershipId+''''
  321. if @prizeId is not null
  322. set @table = @table+ ' and red.prizeId = '''+@prizeId+''''
  323. SET @sql = 'SELECT
  324. COUNT(*) AS TXNCOUNT
  325. ,' + CAST(@pageSize AS VARCHAR) + ' PAGESIZE
  326. ,' + CAST(@pageNumber AS VARCHAR) + ' PAGENUMBER
  327. FROM (' + @table + ') x'
  328. PRINT @sql
  329. EXEC (@sql)
  330. SET @sql = '
  331. SELECT
  332. *
  333. FROM (
  334. SELECT
  335. ROW_NUMBER() OVER (ORDER BY '+@orderBy+') AS [S.N],*
  336. FROM (' + @table + ') x
  337. ) AS tmp WHERE tmp.[S.N] BETWEEN ' + CAST(((@pageNumber - 1) * @pageSize + 1) AS VARCHAR) + ' AND ' + CAST(@pageNumber * @pageSize AS VARCHAR)
  338. PRINT @sql
  339. EXEC (@sql)
  340. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  341. select 'Branch Name' head, case when @branchId is not null then (select agentName from agentMaster with(nolock) where agentId = @branchId)
  342. else 'All' end value
  343. union all
  344. SELECT 'From Date' head, @fromDate value
  345. union all
  346. SELECT 'To Date' head, @toDate value
  347. union all
  348. select 'Gift Item' head,case when @prizeId is not null then (select dbo.FNAGetDataValue(@prizeId)) else 'All' end value
  349. union all
  350. select 'Order By' head, @orderBy value
  351. union all
  352. select 'Membership Id' head, @membershipId value
  353. SELECT 'Customer Bonus Redeemed Report ' title
  354. end
  355. */
  356. GO