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.

290 lines
9.0 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_transactionUtility] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_transactionUtility]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_transactionUtility] 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 PROC [dbo].[proc_transactionUtility] (
  12. @flag VARCHAR(50)
  13. ,@user VARCHAR(50) = NULL
  14. ,@controlNo VARCHAR(100) = NULL
  15. ,@agentId VARCHAR(100) = NULL
  16. ,@requestXML VARCHAR(MAX) = NULL
  17. ,@responseXML VARCHAR(MAX) = NULL
  18. ,@id VARCHAR(50) = NULL
  19. ,@msg VARCHAR(200) = NULL
  20. ,@holdTranId VARCHAR(50) = NULL
  21. ,@createdDate VARCHAR(50) = NULL
  22. ,@count INT = NULL
  23. ,@pass VARCHAR(50) = NULL
  24. ,@ragent VARCHAR(100) = NULL
  25. ,@agentCode VARCHAR(50) = NULL
  26. ,@sortBy VARCHAR(50) = NULL
  27. ,@sortOrder VARCHAR(5) = NULL
  28. ,@pageSize INT = NULL
  29. ,@pageNumber INT = NULL
  30. ,@bankType CHAR(1) = NULL
  31. ,@fromDate VARCHAR(50) = NULL
  32. ,@toDate VARCHAR(50) = NULL
  33. ,@rCountry VARCHAR(50) = NULL
  34. ,@bankCode VARCHAR(max) = NULL
  35. ,@accountNo VARCHAR(100) = NULL
  36. ,@accountName VARCHAR(100) = NULL
  37. ,@responseCode VARCHAR(100) = NULL
  38. ,@bankName VARCHAR(100) = NULL
  39. ,@superAgentId VARCHAR(50) = NULL
  40. ,@targetCountries VARCHAR(MAX) = NULL
  41. )
  42. AS
  43. /*
  44. */
  45. SET XACT_ABORT ON
  46. SET NOCOUNT ON
  47. BEGIN TRY
  48. --Get list of agent and date for Sync Status in Bulk (Called by Schedular)
  49. DECLARE
  50. @table VARCHAR(MAX)
  51. ,@sql VARCHAR(MAX)
  52. ,@select_field_list VARCHAR(MAX)
  53. ,@riaAgentId INT = 56778
  54. --Reprocess List
  55. IF @flag = 'rp'
  56. BEGIN
  57. IF @sortBy IS NULL
  58. SET @sortBy = 'id'
  59. IF @sortOrder IS NULL
  60. SET @sortOrder = 'ASC'
  61. SET @table = '(
  62. SELECT DISTINCT
  63. trn.id
  64. ,ISNULL(trn.holdTranId,trn.id) holdTranId
  65. ,controlNo = dbo.FNADecryptString(trn.controlNo)
  66. ,sAgent = ams.agentName
  67. ,sBranchName
  68. ,sCountry = trn.sCountry
  69. ,rAgent = amr.agentName
  70. ,rCountry = trn.pCountry
  71. ,sender = sen.firstName + ISNULL( '' '' + sen.middleName, '''') + ISNULL( '' '' + sen.lastName1, '''') + ISNULL( '' '' + sen.lastName2, '''')
  72. ,receiver = rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
  73. ,accountNo = trn.accountNo
  74. ,amt = CAST(trn.cAmt AS DECIMAL(18, 2))
  75. ,createdDate = CONVERT(VARCHAR, trn.createdDate, 101)
  76. ,createdBy = trn.createdBy
  77. ,collMode = trn.collMode
  78. ,ScOrderNo = ''''
  79. FROM remitTran trn WITH(NOLOCK)
  80. LEFT JOIN agentMaster ams WITH(NOLOCK) ON trn.sAgent = ams.agentId
  81. LEFT JOIN agentMaster amr WITH(NOLOCK) ON trn.pAgent = amr.agentId
  82. INNER JOIN tranSenders sen WITH(NOLOCK) ON trn.id = sen.tranId
  83. INNER JOIN tranReceivers rec WITH(NOLOCK) ON trn.id = rec.tranId
  84. WHERE trn.approvedBy IS NOT NULL AND trn.payStatus =''Unpaid''
  85. AND tranStatus = ''payment'' AND trn.pAgent = 1056 AND pCountry=''Nepal''
  86. '
  87. IF @id IS NOT NULL
  88. SET @table = @table + ' AND trn.id = ''' + @id + ''''
  89. IF @createdDate IS NOT NULL
  90. SET @table = @table + ' AND TRN.createdDate between '''+@createdDate+''' and '''+@createdDate+' 23:59:59'''
  91. IF @holdTranId IS NOT NULL
  92. SET @table = @table + ' AND TRN.holdTranId = '''+@holdTranId+''''
  93. IF @controlNo IS NOT NULL
  94. SET @table = @table + ' AND TRN.controlNo = dbo.FNAEncryptString('''+@controlNo+''')'
  95. IF @ragent IS NOT NULL
  96. SET @table = @table + ' AND amr.agentName = '''+@ragent+''''
  97. if @rCountry IS NOT NULL
  98. SET @table = @table + ' AND trn.pCountry = '''+@rCountry+''''
  99. if @superAgentId IS NOT NULL
  100. SET @table = @table + ' AND trn.pSuperAgent = '''+@superAgentId+''''
  101. IF @fromDate IS NOT NULL and @toDate IS NOT NULL
  102. SET @table = @table + ' AND trn.createdDate BETWEEN ''' + @fromDate +''' AND '''+ @toDate +' 23:59:59'''
  103. SET @table = @table + ' ) x '
  104. SET @select_field_list ='
  105. id
  106. ,holdTranId
  107. ,controlNo
  108. ,sAgent
  109. ,sBranchName
  110. ,sCountry
  111. ,rAgent
  112. ,collMode
  113. ,rCountry
  114. ,ScOrderNo
  115. ,sender
  116. ,receiver
  117. ,accountNo
  118. ,amt
  119. ,createdDate
  120. ,createdBy
  121. '
  122. PRINT @table
  123. EXEC dbo.proc_paging
  124. @table
  125. ,null
  126. ,@select_field_list
  127. ,null
  128. ,@sortBy
  129. ,@sortOrder
  130. ,@pageSize
  131. ,@pageNumber
  132. RETURN
  133. END
  134. DECLARE @controlNoEnc VARCHAR(100) = dbo.FNAencryptString(@controlNo)
  135. IF @flag IN ('a')
  136. BEGIN
  137. SELECT
  138. controlNo = dbo.FnaDecryptString(controlNo)
  139. ,benefName = tr.firstName + ISNULL(' ' + tr.middleName,'') + ISNULL(' ' + tr.lastName1,'')
  140. ,benefAdddress = tr.address
  141. ,benefTel = tr.homePhone
  142. ,benefMobile = tr.mobile
  143. ,benefIdType = tr.idType
  144. ,benefAccIdNo = tr.idNumber
  145. ,senderName = ts.firstName + ISNULL(' ' + ts.middleName,'') + ISNULL(' ' + ts.lastName1,'')
  146. ,senderAddress = ts.address
  147. ,senderTel = ts.homePhone
  148. ,senderMobile = ts.mobile
  149. ,senderIdType = ts.idType
  150. ,senderIdNo = ts.idNumber
  151. ,purpose = rt.purposeOfRemit
  152. ,remitType = rt.paymentMethod
  153. ,rCurrency =rt.payoutCurr
  154. ,localAmount = rt.tAmt
  155. ,amount = rt.pAmt
  156. ,serviceCharge = rt.serviceCharge
  157. ,rCommission = rt.pAgentComm
  158. ,exchangeRate = rt.customerRate
  159. ,refNo = rt.id
  160. ,remarks = rt.pMessage
  161. ,[source] = rt.sourceOfFund
  162. ,tranStatus = rt.tranStatus
  163. ,payStatus = rt.payStatus
  164. ,paidBy = rt.paidBy
  165. ,paidDate = rt.paidDate
  166. ,approvedDate = CONVERT(VARCHAR, rt.approvedDate, 101)
  167. FROM remitTran rt
  168. INNER JOIN tranSenders ts WITH(NOLOCK) ON rt.id = ts.tranId
  169. INNER JOIN tranReceivers tr WITH(NOLOCK) ON rt.id = tr.tranId
  170. WHERE rt.controlNo = @controlNoEnc
  171. END
  172. IF @flag IN ('gbl-a')
  173. BEGIN
  174. SELECT
  175. controlNo = dbo.FnaDecryptString(controlNo)
  176. ,benefName = tr.firstName + ISNULL(' ' + tr.middleName,'') + ISNULL(' ' + tr.lastName1,'')
  177. --,benefAdddress = tr.address
  178. --,benefTel = tr.homePhone
  179. --,benefMobile = tr.mobile
  180. --,benefIdType = tr.idType
  181. --,benefAccIdNo = tr.idNumber
  182. ,senderName = ts.firstName + ISNULL(' ' + ts.middleName,'') + ISNULL(' ' + ts.lastName1,'')
  183. --,senderAddress = ts.address
  184. --,senderTel = ts.homePhone
  185. --,senderMobile = ts.mobile
  186. --,senderIdType = ts.idType
  187. --,senderIdNo = ts.idNumber
  188. --,purpose = rt.purposeOfRemit
  189. ,remitType = rt.paymentMethod
  190. --,rCurrency =rt.payoutCurr
  191. ,localAmount = rt.tAmt
  192. ,amount = rt.pAmt
  193. ,serviceCharge = rt.serviceCharge
  194. --,rCommission = rt.pAgentComm
  195. ,exchangeRate = rt.customerRate
  196. --,refNo = rt.id
  197. --,remarks = rt.pMessage
  198. --,[source] = rt.sourceOfFund
  199. ,tranStatus = rt.tranStatus
  200. ,payStatus = rt.payStatus
  201. --,paidBy = rt.paidBy
  202. --,paidDate = rt.paidDate
  203. ,approvedDate = CONVERT(VARCHAR, rt.approvedDate, 101)
  204. FROM remitTran rt
  205. INNER JOIN tranSenders ts WITH(NOLOCK) ON rt.id = ts.tranId
  206. INNER JOIN tranReceivers tr WITH(NOLOCK) ON rt.id = tr.tranId
  207. WHERE rt.controlNo = @controlNoEnc
  208. END
  209. IF @flag = 'rpid'
  210. BEGIN
  211. --Check for authentication
  212. IF dbo.FNACheckWsAuthentication(@user, @pass, @agentCode) <> 0
  213. BEGIN
  214. SELECT id, holdTranId FROM remitTran WITH(NOLOCK) WHERE 1 = 2
  215. RETURN
  216. END
  217. UPDATE dbo.jobRunLog SET lastExecutionDate = GETDATE() WHERE jobName = 'Reprocess transaction'
  218. IF ISNULL(@count, 0) < 1 SET @count = 1
  219. --Get the list of transaction ids to process
  220. IF OBJECT_ID('tempdb..#tempTran') IS NOT NULL
  221. DROP TABLE #tempTran
  222. CREATE TABLE #tempTran(id BIGINT)
  223. INSERT INTO #tempTran(id)
  224. SELECT DISTINCT id FROM (
  225. SELECT TOP (50)
  226. trn.id
  227. FROM remitTran trn WITH(READPAST)
  228. INNER JOIN serviceTypeMaster stm WITH(NOLOCK) ON trn.paymentMethod = stm.typeTitle
  229. WHERE trn.approvedBy IS NOT NULL
  230. AND trn.payStatus ='Unpaid'
  231. AND trn.tranStatus = 'Payment'
  232. AND ISNULL(trn.lockStatus, 'unlocked') <> 'locked'
  233. ORDER BY trn.id ASC
  234. ) x
  235. --Lock the transactions to make it unavailable for another process call
  236. UPDATE remitTran SET
  237. lockStatus = 'locked'
  238. ,lockedBy = 'system'
  239. ,lockedDate = GETDATE()
  240. FROM remitTran rt
  241. INNER JOIN #tempTran t ON rt.id = t.id
  242. DECLARE @currentDate DATETIME = GETDATE()
  243. DECLARE @txnCount INT = 0
  244. SELECT @txnCount = COUNT('X') FROM #tempTran
  245. IF @txnCount > 0
  246. BEGIN
  247. --Log with the number of transactions processed in a single batch run
  248. INSERT INTO reprocessScheduleRunLog(createdDate, txnCount, processName)
  249. SELECT @currentDate, txnCount = COUNT('X'), 'Process third party transaction' FROM #tempTran
  250. END
  251. --Return the list of transaction ids to be processed
  252. SELECT *, processId = SCOPE_IDENTITY() FROM #tempTran
  253. --dispose temp table
  254. IF OBJECT_ID('tempdb..#tempTran') IS NOT NULL
  255. DROP TABLE #tempTran
  256. RETURN
  257. END
  258. END TRY
  259. BEGIN CATCH
  260. IF @@TRANCOUNT > 0
  261. ROLLBACK TRANSACTION
  262. SELECT 1 errorCode, ERROR_MESSAGE() msg, NULL id
  263. SELECT ERROR_LINE()
  264. END CATCH
  265. GO