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.

996 lines
58 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_riaRemitPayHistory] 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_riaRemitPayHistory] (
  9. @flag VARCHAR(50)
  10. ,@user VARCHAR(50)
  11. ,@rowId BIGINT = NULL
  12. ----------------------------------------------------
  13. --> from ria API
  14. ,@transRefID VARCHAR(100) = NULL
  15. ,@orderFound VARCHAR(100) = NULL
  16. ,@pIN VARCHAR(100) = NULL
  17. ,@orderNo VARCHAR(100) = NULL
  18. ,@seqIDRA VARCHAR(100) = NULL
  19. ,@orderDate DATETIME = NULL
  20. ,@custNameFirst VARCHAR(100) = NULL
  21. ,@custNameLast1 VARCHAR(100) = NULL
  22. ,@custNameLast2 VARCHAR(100) = NULL
  23. ,@custAddress VARCHAR(100) = NULL
  24. ,@custCity VARCHAR(100) = NULL
  25. ,@custState VARCHAR(100) = NULL
  26. ,@custCountry VARCHAR(100) = NULL
  27. ,@custZip VARCHAR(100) = NULL
  28. ,@custTelNo VARCHAR(100) = NULL
  29. ,@beneNameFirst VARCHAR(100) = NULL
  30. ,@beneNameLast1 VARCHAR(100) = NULL
  31. ,@beneNameLast2 VARCHAR(100) = NULL
  32. ,@beneAddress VARCHAR(100) = NULL
  33. ,@beneCity VARCHAR(100) = NULL
  34. ,@beneState VARCHAR(100) = NULL
  35. ,@beneCountry VARCHAR(100) = NULL
  36. ,@beneZip VARCHAR(100) = NULL
  37. ,@beneTelNo VARCHAR(100) = NULL
  38. ,@beneCurrency VARCHAR(50) = NULL
  39. ,@beneAmount MONEY = NULL
  40. ,@responseDateTimeUTC VARCHAR(50) = NULL
  41. ----------------------------------------------------
  42. ,@payConfirmationNo VARCHAR(100) = NULL
  43. ,@apiStatus VARCHAR(100) = NULL
  44. ,@payResponseCode VARCHAR(20) = NULL
  45. ,@payResponseMsg VARCHAR(100) = NULL
  46. ,@recordStatus VARCHAR(50) = NULL
  47. ,@tranPayProcess VARCHAR(20) = NULL
  48. ,@createdDate DATETIME = NULL
  49. ,@createdBy VARCHAR(30) = NULL
  50. ,@paidDate DATETIME = NULL
  51. ,@paidBy VARCHAR(30) = NULL
  52. ,@pBranch INT = NULL
  53. ,@pBranchName VARCHAR(100) = NULL
  54. ,@pAgent INT = NULL
  55. ,@pAgentName VARCHAR(100) = NULL
  56. ,@rIdType VARCHAR(30) = NULL
  57. ,@rIdNumber VARCHAR(30) = NULL
  58. ,@rIdPlaceOfIssue VARCHAR(50) = NULL
  59. ,@rValidDate VARCHAR(20) = NULL
  60. ,@rIssuedDate DATETIME = NULL
  61. ,@rDob DATETIME = NULL
  62. ,@rAddress VARCHAR(100) = NULL
  63. ,@rOccupation VARCHAR(100) = NULL
  64. ,@rContactNo VARCHAR(50) = NULL
  65. ,@rCity VARCHAR(100) = NULL
  66. ,@rNativeCountry VARCHAR(100) = NULL
  67. ,@relationType VARCHAR(50) = NULL
  68. ,@relativeName VARCHAR(100) = NULL
  69. ,@remarks VARCHAR(500) = NULL
  70. ,@approveBy VARCHAR(30) = NULL
  71. ,@approvePwd VARCHAR(100) = NULL
  72. ,@sCountry VARCHAR(100) = NULL
  73. ,@agentName VARCHAR(100) = NULL
  74. ,@provider VARCHAR(100) = NULL
  75. ,@sortBy VARCHAR(50) = NULL
  76. ,@sortOrder VARCHAR(5) = NULL
  77. ,@pageSize INT = NULL
  78. ,@pageNumber INT = NULL
  79. ,@customerId VARCHAR(50) = NULL
  80. ,@membershipId VARCHAR(50) = NULL
  81. ,@rbankName VARCHAR(50) = NULL
  82. ,@rbankBranch VARCHAR(100) = NULL
  83. ,@rcheque VARCHAR(50) = NULL
  84. ,@rAccountNo VARCHAR(50) = NULL
  85. ,@topupMobileNo varchar(50) = null
  86. ,@relationship VARCHAR(100) = NULL
  87. ,@purpose VARCHAR(100) = NULL
  88. )
  89. AS
  90. SET XACT_ABORT ON
  91. BEGIN TRY
  92. DECLARE @pinEnc VARCHAR(100) = dbo.FNAEncryptString(@pIN)
  93. ,@sFullName VARCHAR(200)
  94. ,@rFullName VARCHAR(200)
  95. ,@tranId BIGINT
  96. ,@tranIdTemp BIGINT
  97. ,@pAmt MONEY
  98. ,@cAmt MONEY
  99. ,@txnDate DATETIME
  100. /*
  101. EXEC [dbo].[proc_riaRemitPayHistory] @flag = 'aInfo', @user = 'adbapiuser'
  102. */
  103. IF @flag = 'aInfo'
  104. BEGIN
  105. SELECT TOP 1
  106. 0 ErrorCode, am.agentName, am.agentId
  107. FROM applicationUsers au (NOLOCK)
  108. INNER JOIN agentMaster am (NOLOCK) ON au.agentId = am.agentId
  109. WHERE au.userName = @user
  110. RETURN
  111. END
  112. IF @flag = 'loc'
  113. BEGIN
  114. IF @pBranch IS NULL
  115. BEGIN
  116. SELECT TOP 1 @pBranch = agentId FROM applicationUsers (NOLOCK) WHERE userName = @user
  117. END
  118. SELECT BranchCode = 'IME' + RIGHT('00000' + CAST(ISNULL(@pBranch, '0') AS VARCHAR), 6)
  119. --SELECT BranchCode = 'IME' + RIGHT('00000' + CAST(agentId AS VARCHAR), 6) FROM agentMaster am WITH(NOLOCK) WHERE agentId = @pBranch
  120. RETURN;
  121. END
  122. IF @flag = 's'
  123. BEGIN
  124. DECLARE @table VARCHAR(MAX)
  125. ,@sql_filter VARCHAR(MAX)
  126. ,@select_field_list VARCHAR(MAX)
  127. ,@extra_field_list VARCHAR(MAX)
  128. SET @table = '
  129. (
  130. SELECT
  131. ria.rowId
  132. ,''RIA FINANCIAL SERVICES'' AS provider
  133. ,am.agentName
  134. ,dbo.FNADecryptString(ria.pin) AS xpin
  135. ,customer = ISNULL(ria.CustNameFirst, '''')
  136. ,beneficiary = ISNULL(ria.beneNameFirst, '''')
  137. ,customerAddress = ISNULL(ria.CustAddress, '''')
  138. ,beneficiaryAddress = ISNULL(ria.rAddress, '''')
  139. ,payoutAmount = ria.BeneAmount
  140. ,payoutDate =ria.paidDate
  141. FROM riaRemitPayHistory ria WITH(NOLOCK)
  142. LEFT JOIN agentMaster am WITH(NOLOCK) ON am.agentId = ria.pBranch
  143. WHERE recordStatus NOT IN(''DRAFT'', ''EXPIRED'')
  144. '
  145. IF @pIN IS NOT NULL
  146. BEGIN
  147. SET @table = @table + ' AND pin = ''' + @pinEnc + ''''
  148. select @pBranch = pBranch,@user = createdBy from riaRemitPayHistory with(nolock)
  149. where pin = @pinEnc
  150. if @pBranch is null and @user is not null
  151. begin
  152. select @pBranch = agentId from applicationUsers with(nolock)
  153. where userName = @user
  154. update riaRemitPayHistory set pBranch = @pBranch
  155. where pin = @pinEnc
  156. end
  157. END
  158. ELSE
  159. BEGIN
  160. SET @table = @table + ' AND tranPayProcess IS NULL'
  161. END
  162. IF @agentName IS NOT NULL
  163. SET @table = @table + ' AND am.agentName LIKE ''' + @agentName + '%'''
  164. SET @table = @table + '
  165. ) x '
  166. SET @sql_filter = ''
  167. IF @provider IS NOT NULL
  168. SET @sql_filter = @sql_filter + ' AND provider LIKE ''' + @provider + '%'''
  169. SET @select_field_list ='
  170. rowId
  171. ,provider
  172. ,agentName
  173. ,xpin
  174. ,customer
  175. ,beneficiary
  176. ,customerAddress
  177. ,beneficiaryAddress
  178. ,payoutAmount
  179. ,payoutDate
  180. '
  181. EXEC dbo.proc_paging
  182. @table
  183. ,@sql_filter
  184. ,@select_field_list
  185. ,@extra_field_list
  186. ,@sortBy
  187. ,@sortOrder
  188. ,@pageSize
  189. ,@pageNumber
  190. RETURN
  191. END
  192. IF @flag = 'a'
  193. BEGIN
  194. SELECT TOP 1
  195. rowId
  196. ,[controlNo] = dbo.FNADecryptString(ria.pin)
  197. ,[sCountry] = ria.sCountry
  198. ,[sName] = ria.CustNameFirst
  199. ,[sAddress] = ISNULL(ria.CustAddress,'')
  200. ,sCity = ISNULL(ria.custCity,'')
  201. ,sMobile = ISNULL(ria.custTelNo,'')
  202. ,sAgentName = 'RIA FINANCIAL SERVICES'
  203. ,sAgent = 4869
  204. ,[rCountry] = 'Nepal'
  205. ,[rName] = ria.beneNameFirst
  206. ,[rAddress] = ria.rAddress
  207. ,[rCity] = ria.rCity
  208. ,[rPhone] = ISNULL(ria.rContactNo,'')
  209. ,[rIdType] = ria.rIdType
  210. ,[rIdNumber] = ria.rIdNumber
  211. ,[pAmt] = ria.BeneAmount
  212. ,[pCurr] = ria.BeneCurrency
  213. ,[pBranch] = am.agentName
  214. ,branchId = ria.pBranch
  215. ,[pUser] = ria.createdBy
  216. ,transactionMode = 'Cash Payment'
  217. ,PlaceOfIssue = rIdPlaceOfIssue
  218. ,rRelativeName = relativeName
  219. ,RelationType = relationType
  220. ,rContactNo = rContactNo
  221. FROM riaRemitPayHistory ria WITH(NOLOCK)
  222. left JOIN agentMaster am WITH(NOLOCK) ON ria.pBranch = am.agentId
  223. WHERE recordStatus <> ('DRAFT') AND rowId = @rowId
  224. ORDER BY rowId DESC
  225. RETURN
  226. END
  227. IF @flag = 'i'
  228. BEGIN
  229. IF EXISTS (SELECT 'x' FROM riaRemitPayHistory WITH(NOLOCK) WHERE pin= @pinEnc)
  230. BEGIN
  231. UPDATE riaRemitPayHistory SET
  232. recordStatus = 'EXPIRED'
  233. WHERE pin = @pinEnc AND recordStatus <> 'READYTOPAY'
  234. END
  235. IF @pBranch IS NULL
  236. SELECT @pBranch = agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user
  237. IF @pBranch = '1001'
  238. BEGIN
  239. EXEC [proc_errorHandler] 1, 'Payout branch is missing.', @rowId
  240. RETURN;
  241. END
  242. INSERT INTO riaRemitPayHistory (
  243. transRefID
  244. ,orderFound
  245. ,pin
  246. ,orderNo
  247. ,seqIDRA
  248. ,orderDate
  249. ,custNameFirst
  250. ,custNameLast1
  251. ,custNameLast2
  252. ,custAddress
  253. ,custCity
  254. ,custState
  255. ,custCountry
  256. ,custZip
  257. ,custTelNo
  258. ,beneNameFirst
  259. ,beneNameLast1
  260. ,beneNameLast2
  261. ,beneAddress
  262. ,beneCity
  263. ,beneState
  264. ,beneCountry
  265. ,beneZip
  266. ,beneTelNo
  267. ,beneAmount
  268. ,responseDateTimeUTC
  269. ,remarks
  270. ,apiStatus
  271. ,recordStatus
  272. ,pBranch
  273. ,createdDate
  274. ,createdBy
  275. )
  276. SELECT
  277. @transRefID
  278. ,@orderFound
  279. ,@pinEnc
  280. ,@orderNo
  281. ,@seqIDRA
  282. ,CONVERT(datetime, @orderDate, 102)
  283. ,@custNameFirst
  284. ,@custNameLast1
  285. ,@custNameLast2
  286. ,@custAddress
  287. ,@custCity
  288. ,@custState
  289. ,@custCountry
  290. ,@custZip
  291. ,@custTelNo
  292. ,@beneNameFirst
  293. ,@beneNameLast1
  294. ,@beneNameLast2
  295. ,@beneAddress
  296. ,@beneCity
  297. ,@beneState
  298. ,@beneCountry
  299. ,@beneZip
  300. ,@beneTelNo
  301. ,@beneAmount
  302. ,@responseDateTimeUTC
  303. ,@remarks
  304. ,@apiStatus
  305. ,'DRAFT'
  306. ,@pBranch
  307. ,GETDATE()
  308. ,@user
  309. SET @rowId = SCOPE_IDENTITY()
  310. EXEC [proc_errorHandler] 0, 'Transaction Has Been Saved Successfully', @rowId
  311. RETURN
  312. END
  313. IF @flag = 'readyToPay'
  314. BEGIN
  315. SELECT
  316. @tranId = id
  317. ,@pAmt = pAmt
  318. ,@cAmt = cAmt
  319. ,@sCountry = sCountry
  320. ,@txnDate = createdDateLocal
  321. ,@sFullName = senderName
  322. ,@rFullName = receiverName
  323. FROM remitTran WITH(NOLOCK) WHERE controlNo = @pinEnc
  324. --## Start OFAC / Compliance
  325. DECLARE
  326. @receiverOfacRes VARCHAR(MAX),
  327. @ofacRes VARCHAR(MAX),
  328. @ofacReason VARCHAR(MAX),
  329. @senderOfacRes VARCHAR(MAX)
  330. --## checking for OFAC is already checked or not
  331. IF NOT EXISTS(SELECT 'x' FROM tranPayOfac WITH(NOLOCK) WHERE tranId = @tranId AND provider = 4869)
  332. BEGIN
  333. EXEC proc_ofacTracker @flag = 't', @name = @sFullName, @Result = @senderOfacRes OUTPUT
  334. EXEC proc_ofacTracker @flag = 't', @name = @rFullName, @Result = @receiverOfacRes OUTPUT
  335. IF ISNULL(@senderOfacRes, '') <> ''
  336. BEGIN
  337. SET @ofacReason = 'Matched by sender name'
  338. END
  339. IF ISNULL(@receiverOfacRes, '') <> ''
  340. BEGIN
  341. SET @ofacRes = ISNULL(@senderOfacRes + ',' + @receiverOfacRes, '' + @receiverOfacRes)
  342. SET @ofacReason = 'Matched by receiver name'
  343. END
  344. IF ISNULL(@senderOfacRes, '') <> '' AND ISNULL(@receiverOfacRes, '') <> ''
  345. BEGIN
  346. SET @ofacReason = 'Matched by both sender name and receiver name'
  347. END
  348. IF ISNULL(@ofacRes, '') <> ''
  349. BEGIN
  350. INSERT tranPayOfac(tranId,provider, blackListId, reason, flag, pAmt,controlNo,pBranch,senderName,receiverName,txnDate,createdBy,createdDate,
  351. rMemId,rIdType,rIdNumber,rPlaceOfIssue,rContactNo,rRelationType,rRelativeName)
  352. SELECT @tranId, '4869' ,@ofacRes, @ofacReason, dbo.FNAGetOFAC_Flag(@ofacRes),@pAmt,@pinEnc,@pBranch,@sFullName,@rFullName,@txnDate,@user,GETDATE(),
  353. @membershipId,@rIdType,@rIdNumber,@rIdPlaceOfIssue,@beneTelNo,@relationType,@relativeName
  354. EXEC proc_errorHandler 1, 'Transaction is not in authorized mode, Please contact Head Office.', @pinEnc
  355. RETURN
  356. END
  357. END
  358. --## checking for OFAC is approved or not
  359. IF EXISTS(SELECT 'x' FROM tranPayOfac WITH(NOLOCK)
  360. WHERE tranId = @tranId
  361. AND provider = 4869
  362. AND approvedDate IS NULL)
  363. BEGIN
  364. EXEC proc_errorHandler 1, 'Transaction is not in authorized mode, Please contact Head Office.', @pinEnc
  365. RETURN
  366. END
  367. UPDATE riaRemitPayHistory SET
  368. recordStatus = 'READYTOPAY'
  369. ,pBranch = isnull(@pBranch ,pBranch)
  370. ,rIdType = @rIdType
  371. ,rIdNumber = @rIdNumber
  372. ,rIdPlaceOfIssue = @rIdPlaceOfIssue
  373. ,rValidDate = CONVERT(DATETIME,@rValidDate, 102)
  374. ,rDob = @rDob
  375. ,rAddress = @rAddress
  376. ,rCity = @rCity
  377. ,rOccupation = @rOccupation
  378. ,rContactNo = @rContactNo
  379. ,nativeCountry = @rNativeCountry
  380. ,relationType = @relationType
  381. ,relativeName = @relativeName
  382. ,remarks = @remarks
  383. ,sCountry = @sCountry
  384. ,rBank = @rbankName
  385. ,rBankBranch = @rbankBranch
  386. ,rAccountNo = @rAccountNo
  387. ,rChequeNo = @rcheque
  388. ,membershipId = @membershipId
  389. ,customerId = @customerId
  390. ,relWithSender = @relationship
  391. ,purposeOfRemit = @purpose
  392. ,rIssueDate = @rIssuedDate
  393. WHERE rowId = @rowId
  394. EXEC [proc_errorHandler] 0, 'Ready to pay has been recorded successfully.', @rowId
  395. RETURN
  396. END
  397. IF @flag = 'payError'
  398. BEGIN
  399. UPDATE riaRemitPayHistory SET
  400. recordStatus = 'PAYERROR'
  401. ,payResponseCode = @payResponseCode
  402. ,payResponseMsg = @payResponseMsg
  403. WHERE rowId = @rowId
  404. EXEC [proc_errorHandler] 0, 'Pay error has been recorded successfully.', @rowId
  405. RETURN
  406. END
  407. IF @flag IN ('pay', 'restore')
  408. BEGIN
  409. IF NOT EXISTS(
  410. SELECT 'x' FROM riaRemitPayHistory WITH(NOLOCK)
  411. WHERE recordStatus IN('READYTOPAY', 'PAYERROR', 'PAID')
  412. AND rowid = @rowid )
  413. BEGIN
  414. EXEC proc_errorHandler 1, 'Transaction Not Found!', @rowid
  415. RETURN
  416. END
  417. DECLARE
  418. -- @tranId BIGINT
  419. @pSuperAgent INT
  420. ,@pSuperAgentName VARCHAR(100)
  421. ,@pCountry VARCHAR(100)
  422. ,@pState VARCHAR(100)
  423. ,@pDistrict VARCHAR(100)
  424. ,@pLocation INT
  425. ,@pAgentComm MONEY
  426. ,@pAgentCommCurrency VARCHAR(3)
  427. ,@pSuperAgentComm MONEY
  428. ,@pSuperAgentCommCurrency VARCHAR(3)
  429. ,@sAgent INT
  430. ,@sAgentName VARCHAR(100)
  431. ,@sBranch INT
  432. ,@sBranchName VARCHAR(100)
  433. ,@sSuperAgent INT
  434. ,@sSuperAgentName VARCHAR(100)
  435. ,@sAgentMapCode INT = 33200000 /*Need to change the value as per ria */
  436. ,@sBranchMapCode INT = 33200100 /*Need to change the value as per ria */
  437. ,@bankName VARCHAR(100) = NULL
  438. ,@purposeOfRemit VARCHAR(100) = NULL
  439. ,@pBankBranch VARCHAR(100) = NULL
  440. ,@sAgentSettRate VARCHAR(100) = NULL
  441. ,@agentType INT
  442. ,@payoutMethod VARCHAR(50)
  443. --,@cAmt MONEY
  444. ,@beneIdNo INT
  445. ,@customerRate MONEY
  446. ,@payoutCurr VARCHAR(50)
  447. ,@collCurr VARCHAR(50)
  448. ,@MapCodeIntBranch VARCHAR(50)
  449. ,@MapCodeIntAgent VARCHAR(50)
  450. ,@MapAgentName VARCHAR(50)
  451. ,@companyId INT = 16
  452. ,@controlNo VARCHAR(50)
  453. SELECT
  454. /* Start Ria Date */
  455. @transRefID = ria.transRefID
  456. ,@orderFound = ria.orderFound
  457. ,@pin = ria.pin
  458. ,@orderNo = ria.orderNo
  459. ,@seqIDRA = ria.seqIDRA
  460. ,@orderDate = ria.orderDate
  461. ,@custNameFirst = ria.custNameFirst
  462. ,@custNameLast1 = ria.custNameLast1
  463. ,@custNameLast2 = ria.custNameLast2
  464. ,@custAddress = ria.custAddress
  465. ,@custCity = ria.custCity
  466. ,@custState = ria.custState
  467. ,@custCountry = ria.custCountry
  468. ,@custZip = ria.custZip
  469. ,@custTelNo = ria.custTelNo
  470. ,@beneNameFirst = ria.beneNameFirst
  471. ,@beneNameLast1 = ria.beneNameLast1
  472. ,@beneNameLast2 = ria.beneNameLast2
  473. ,@rFullName = ISNULL(ria.beneNameFirst,'') +ISNULL(' '+ria.beneNameLast1,'') + ISNULL(' '+ria.beneNameLast2,'')
  474. ,@beneAddress = ria.beneAddress
  475. ,@beneCity = ria.beneCity
  476. ,@beneState = ria.beneState
  477. ,@beneCountry = ria.beneCountry
  478. ,@beneZip = ria.beneZip
  479. ,@beneTelNo = ria.beneTelNo
  480. ,@rContactNo = ria.rContactNo
  481. ,@beneAmount = ria.beneAmount
  482. ,@responseDateTimeUTC = ria.responseDateTimeUTC
  483. ------------------------------------------------------
  484. ,@apiStatus = ria.apiStatus
  485. ,@recordStatus = ria.recordStatus
  486. ,@rIdType = ria.rIdType
  487. ,@rIdNumber = ria.rIdNumber
  488. ,@rValidDate = ria.rValidDate
  489. ,@rDob = ria.rDob
  490. ,@rOccupation = ria.rOccupation
  491. ,@rNativeCountry = ria.nativeCountry
  492. ,@pBranch = isnull(@pBranch,ria.pBranch)
  493. ,@rIdPlaceOfIssue = ria.rIdPlaceOfIssue
  494. ,@relationType = ria.relationType
  495. ,@relativeName = ria.relativeName
  496. ,@sCountry = case when ria.custCountry ='Nepal' then 'Australia' else ria.custCountry end
  497. ,@rbankName = rBank
  498. ,@rbankBranch = rBankBranch
  499. ,@rcheque = rAccountNo
  500. ,@rAccountNo = rChequeNo
  501. ,@membershipId = membershipId
  502. ,@customerId = customerId
  503. ,@purpose = purposeOfRemit
  504. ,@relationship = relWithSender
  505. ,@rIssuedDate = rIssueDate
  506. FROM riaRemitPayHistory ria WITH(NOLOCK)
  507. WHERE rowId = @rowId
  508. SELECT
  509. @pAgent = parentId,
  510. @pBranchName = agentName,
  511. @agentType = agentType,
  512. @pCountry = agentCountry,
  513. @pState = agentState,
  514. @pDistrict = agentDistrict,
  515. @pLocation = agentLocation,
  516. @MapCodeIntBranch=mapCodeInt
  517. FROM agentMaster WITH(NOLOCK) WHERE agentId = @pBranch
  518. /*Additional End */--End
  519. --## Check if controlno exist in remittran.
  520. IF EXISTS( SELECT 'x' FROM remitTran WITH(NOLOCK) WHERE controlNo = @pin )
  521. BEGIN
  522. DECLARE @status VARCHAR(100),@msg VARCHAR(100)
  523. SELECT
  524. @agentName = sAgentName
  525. ,@status = payStatus
  526. FROM remitTran WITH(NOLOCK) WHERE controlNo = @pin
  527. SET @msg = 'This transaction belongs to ' + @agentName + ' and is in status: ' + @status
  528. EXEC proc_errorHandler 1,@msg,NULL
  529. RETURN
  530. END
  531. --## Set paying agent details.
  532. SELECT
  533. @pAgent = parentId,
  534. @pBranchName = agentName,
  535. @agentType = agentType,
  536. @pCountry = agentCountry,
  537. @pState = agentState,
  538. @pDistrict = agentDistrict,
  539. @pLocation = agentLocation,
  540. @MapCodeIntBranch=mapCodeInt
  541. FROM agentMaster WITH(NOLOCK) WHERE agentId = @pBranch
  542. IF @agentType = 2903
  543. SET @pAgent = @pBranch
  544. SELECT @sBranch = agentId,
  545. @sAgent = parentId,
  546. @sBranchName = agentName,
  547. @agentType = agentType
  548. FROM agentMaster WITH(NOLOCK)
  549. WHERE mapCodeInt = @sBranchMapCode AND ISNULL(isDeleted, 'N') = 'N'
  550. IF @agentType = 2903
  551. BEGIN
  552. SET @sAgent = @sBranch
  553. END
  554. SELECT @sSuperAgent = parentId,
  555. @sAgentName = agentName
  556. FROM agentMaster WITH(NOLOCK) WHERE agentId = @sAgent
  557. SELECT @sSuperAgentName = agentName
  558. FROM agentMaster WITH(NOLOCK) WHERE agentId = @sSuperAgent
  559. --## 2. Find Payout Agent Details
  560. SELECT @pSuperAgent = parentId,
  561. @pAgentName = agentName
  562. FROM agentMaster WITH(NOLOCK) WHERE agentId = @pAgent
  563. SELECT @pSuperAgentName = agentName
  564. FROM agentMaster WITH(NOLOCK) WHERE agentId = @pSuperAgent
  565. --## 3. Find Commission
  566. DECLARE @sCountryId INT, @deliveryMethodId INT, @pCommCheck MONEY
  567. SELECT @sCountryId = countryId FROM countryMaster WITH(NOLOCK) WHERE countryName = @sCountry
  568. if @sCountryId is null
  569. select @sCountryId = agentCountryId from agentMaster with(nolock) where agentId = @sBranch
  570. SET @payoutMethod = 'Cash Payment'
  571. DECLARE @pCountryId INT = NULL
  572. SELECT @pCountryId = countryId FROM countryMaster WITH(NOLOCK) WHERE countryName = @pCountry
  573. SELECT @deliveryMethodId = serviceTypeId FROM serviceTypeMaster WITH(NOLOCK)
  574. WHERE typeTitle = @payoutMethod AND ISNULL(isDeleted, 'N') = 'N'
  575. SELECT @pSuperAgentComm = 0, @pSuperAgentCommCurrency = 'NPR'
  576. SELECT @pAgentComm = ISNULL(amount, 0),
  577. @pCommCheck = amount,
  578. @pAgentCommCurrency = commissionCurrency,
  579. @pCommCheck = amount
  580. FROM dbo.FNAGetPayComm(@sBranch, @sCountryId, NULL, @pSuperAgent, 151,
  581. @pLocation, @pBranch, 'NPR', @deliveryMethodId, NULL, @beneamount, NULL, NULL, NULL)
  582. IF @pCommCheck IS NULL
  583. BEGIN
  584. EXEC proc_errorHandler 1, 'Payout Commission not defined', NULL
  585. RETURN
  586. END
  587. -------Compliance Check Begin----------
  588. -- Thirdparty txn doesn't have tranid. Hence, temp tranid is created for compliance checking process. Later on this will replace by actual tranId.
  589. SELECT @tranIdTemp = ABS(CAST(CRYPT_GEN_RANDOM(8) AS BIGINT))
  590. IF NOT EXISTS(SELECT 'X' FROM remitTranCompliancePay WITH(NOLOCK) WHERE tranId = @tranIdTemp AND approvedDate IS NOT NULL)
  591. BEGIN
  592. DECLARE @csMasterId INT, @complianceRes VARCHAR(20), @totalRows INT, @count INT, @compFinalRes VARCHAR(20), @result VARCHAR(MAX),@complianceAction CHAR(1),
  593. @compApproveRemark VARCHAR(200)
  594. DECLARE @csMasterRec TABLE (rowId INT IDENTITY(1,1), masterId INT)
  595. INSERT @csMasterRec(masterId)
  596. SELECT masterId FROM dbo.FNAGetComplianceRuleMaster_Pay(@pBranch, @pCountryId, NULL, @pBranch, NULL, NULL, @customerId)
  597. SELECT @totalRows = COUNT(*) FROM @csMasterRec
  598. DECLARE @denyTxn CHAR(1) = 'N'
  599. IF EXISTS(SELECT 'X' FROM @csMasterRec)
  600. BEGIN
  601. DELETE FROM remitTranCompliancePayTemp WHERE tranId = @tranIdTemp
  602. SET @count = 1
  603. WHILE(@count <= @totalRows)
  604. BEGIN
  605. SELECT @csMasterId = masterId FROM @csMasterRec WHERE rowId = @count
  606. EXEC proc_complianceRuleDetail_Pay
  607. @user = @user
  608. ,@tranId = @tranIdTemp
  609. ,@tAmt = @beneamount
  610. ,@customerId = @customerId
  611. ,@receiverId = @rIdNumber
  612. ,@receiverMemId = @membershipId
  613. ,@receiverName = @rFullName
  614. ,@receiverMobile = @rContactNo
  615. ,@receiverAcNo = @rAccountNo
  616. ,@masterId = @csMasterId
  617. ,@paymentMethod = @deliveryMethodId
  618. ,@checkingFor = 'v'
  619. ,@result = @complianceRes OUTPUT
  620. SET @compFinalRes = ISNULL(@compFinalRes, '') + ISNULL(@complianceRes, '')
  621. IF @complianceRes = 'M' AND ISNULL(@complianceAction, '') <> 'C'
  622. SET @complianceAction = 'M'
  623. IF @complianceRes = 'C'
  624. SET @complianceAction = 'C'
  625. SET @count = @count + 1
  626. END
  627. END
  628. IF(ISNULL(@compFinalRes, '') <> '')
  629. BEGIN
  630. IF(@compFinalRes <> '')
  631. BEGIN
  632. IF EXISTS(SELECT 'X' FROM remitTranCompliancePayTemp WITH(NOLOCK) WHERE tranId = @tranIdTemp)
  633. BEGIN
  634. INSERT INTO remitTranCompliancePay(tranId, csDetailTranId, matchTranId)
  635. SELECT @tranIdTemp, csDetailTranId, matchTranId FROM remitTranCompliancePayTemp WITH(NOLOCK) WHERE tranId = @tranIdTemp
  636. --INSERT tranPayCompliance(tranId,provider,controlNo,pBranch,receiverName,rMemId,dob,
  637. --rIdType,rIdNumber,rPlaceOfIssue,rContactNo,rRelationType,rRelativeName,relWithSender,purposeOfRemit,createdBy,createdDate)
  638. --SELECT @tranIdTemp, '4869' , @pin,@pBranch,@rFullName,@membershipId,@rDob,
  639. --@rIdType,@rIdNumber,@rIdPlaceOfIssue,@rContactNo,@relationType,@relativeName,@relationship,@purpose,@user,GETDATE()
  640. DELETE FROM dbo.remitTranCompliancePayTemp WHERE tranId = @tranIdTemp
  641. END
  642. IF ISNULL(@complianceAction, '') <> ''
  643. BEGIN
  644. --UPDATE remitTran SET
  645. -- tranStatus = 'Hold'
  646. --WHERE id = @tranId
  647. UPDATE remitTranCompliancePay SET
  648. approvedRemarks = 'Marked for Compliance'
  649. ,approvedBy = 'system'
  650. ,approvedDate = GETDATE()
  651. WHERE tranId = @tranIdTemp
  652. --UPDATE tranPayCompliance SET
  653. -- approvedRemarks = 'Marked for Compliance'
  654. -- ,approvedBy = 'system'
  655. -- ,approvedDate = GETDATE()
  656. --WHERE tranId = @tranIdTemp
  657. END
  658. END
  659. END
  660. END
  661. -------Compliance Check End----------
  662. BEGIN TRANSACTION
  663. BEGIN
  664. INSERT INTO remitTran (
  665. [controlNo]
  666. ,[senderName]
  667. ,[sCountry]
  668. ,[sSuperAgent]
  669. ,[sSuperAgentName]
  670. ,[paymentMethod]
  671. ,[cAmt]
  672. ,[pAmt]
  673. ,[tAmt]
  674. ,[pAgentComm]
  675. ,[payoutCurr]
  676. ,[pAgent]
  677. ,[pAgentName]
  678. ,[pSuperAgent]
  679. ,[pSuperAgentName]
  680. ,[receiverName]
  681. ,[pCountry]
  682. ,[pBranch]
  683. ,[pBranchName]
  684. ,[pState]
  685. ,[pDistrict]
  686. ,[pLocation]
  687. ,[pbankName]
  688. ,[purposeofRemit]
  689. ,[pMessage]
  690. ,[pBankBranch]
  691. ,[sAgentSettRate]
  692. ,[createdDate]
  693. ,[createdDateLocal]
  694. ,[createdBy]
  695. ,[approvedDate]
  696. ,[approvedDateLocal]
  697. ,[approvedBy]
  698. ,[paidBy]
  699. ,[paidDate]
  700. ,[paidDateLocal]
  701. --,[serviceCharge]
  702. --## hardcoded parameters
  703. ,[tranStatus]
  704. ,[payStatus]
  705. ,[collCurr]
  706. ,[controlNo2]
  707. ,[tranType]
  708. ,[sAgent]
  709. ,[sAgentName]
  710. ,[sBranch]
  711. ,[sBranchName]
  712. ,sCurrCostRate
  713. )
  714. SELECT
  715. @pin
  716. ,@custNameFirst+' '+ISNULL(@custNameLast1,'')+' '+ISNULL(@custNameLast2,'')
  717. ,@sCountry
  718. ,@sSuperAgent
  719. ,@sSuperAgentName
  720. ,'Cash Payment'
  721. ,dbo.FNARemitRoundForNPR(@beneamount)
  722. ,dbo.FNARemitRoundForNPR(@beneamount)
  723. ,dbo.FNARemitRoundForNPR(@beneamount)
  724. --,@exchangeRate
  725. ,@pAgentComm
  726. ,'NPR'--@beneCurrency
  727. ,@pAgent
  728. ,@pAgentName
  729. ,@pSuperAgent
  730. ,@pSuperAgentName
  731. ,@beneNameFirst+' '+ISNULL(@beneNameLast1,'')+' '+ISNULL(@beneNameLast2,'')
  732. ,@pCountry
  733. ,@pBranch
  734. ,@pBranchName
  735. ,@pState
  736. ,@pDistrict
  737. ,@pLocation
  738. ,@bankName
  739. ,@purposeOfRemit
  740. ,@remarks
  741. ,@pBankBranch
  742. ,@SagentsettRate
  743. ,GETDATE()
  744. ,GETDATE()
  745. ,'SWIFT:API'
  746. ,GETDATE()
  747. ,GETDATE()
  748. ,'SWIFT:API'
  749. ,@user
  750. ,dbo.FNAGetDateInNepalTZ()
  751. ,dbo.FNAGetDateInNepalTZ()
  752. --,@pCommission
  753. --## HardCoded Parameters
  754. ,'Paid'
  755. ,'Paid'
  756. ,'NPR'
  757. ,dbo.FNAEncryptString(@transRefID)
  758. ,'I'
  759. ,@sAgent
  760. ,@sAgentName
  761. ,@sBranch
  762. ,@sBranchName
  763. ,'1'
  764. SET @tranId = SCOPE_IDENTITY()
  765. INSERT INTO tranSenders (
  766. tranId
  767. ,firstName
  768. ,lastName1
  769. ,lastName2
  770. ,country
  771. ,[address]
  772. ,homePhone
  773. )
  774. SELECT
  775. @tranId
  776. ,@custNameFirst
  777. ,@custNameLast1
  778. ,@custNameLast2
  779. ,@sCountry
  780. ,@CustAddress
  781. ,@CustTelNo
  782. INSERT INTO tranReceivers (
  783. tranId
  784. ,firstName
  785. ,lastName1
  786. ,lastName2
  787. ,country
  788. ,city
  789. ,[address]
  790. ,mobile
  791. ,homePhone
  792. ,idType
  793. ,idNumber
  794. ,idType2
  795. ,idNumber2
  796. ,dob
  797. ,occupation
  798. ,validDate
  799. ,idPlaceOfIssue
  800. ,relationType
  801. ,relativeName
  802. ,bankName
  803. ,branchName
  804. ,chequeNo
  805. ,accountNo
  806. ,membershipId
  807. ,customerId
  808. ,relWithSender
  809. ,purposeOfRemit
  810. ,issuedDate2
  811. ,validDate2
  812. )
  813. SELECT
  814. @tranId
  815. ,@beneNameFirst
  816. ,@beneNameLast1
  817. ,@beneNameLast2
  818. ,@pCountry
  819. ,@beneAddress
  820. ,@beneCity
  821. ,@beneTelNo
  822. ,@rContactNo
  823. ,@rIdType
  824. ,@rIdNumber
  825. ,@rIdType
  826. ,@rIdNumber
  827. ,@rDob
  828. ,@rOccupation
  829. ,@rValidDate
  830. ,@rIdPlaceOfIssue
  831. ,@relationType
  832. ,@relativeName
  833. ,@rbankName
  834. ,@rbankBranch
  835. ,@rcheque
  836. ,@raccountNo
  837. ,@membershipId
  838. ,@customerId
  839. ,@relationship
  840. ,@purpose
  841. ,@rIssuedDate
  842. ,@rValidDate
  843. UPDATE remitTranCompliancePay SET
  844. tranId = @tranId
  845. WHERE tranId = @tranIdTemp
  846. UPDATE tranPayCompliance SET
  847. tranId = @tranId
  848. WHERE tranId = @tranIdTemp
  849. IF @membershipId IS NOT NULL
  850. BEGIN
  851. UPDATE dbo.customerMaster SET
  852. paidTxn = ISNULL(paidTxn,0)+1,
  853. firstTxnDate = ISNULL(firstTxnDate,GETDATE())
  854. WHERE membershipId = @membershipId
  855. END
  856. ----## >> Updating Data in riaRemitPayHistory table by paid status
  857. UPDATE riaRemitPayHistory SET
  858. recordStatus = 'PAID'
  859. ,tranPayProcess = CASE WHEN @flag = 'Pay' THEN 'REGULAR' ELSE 'RESTORED' END
  860. ,payResponseCode = @payResponseCode
  861. ,payResponseMsg = @payResponseMsg
  862. ,confirmationNo = @payConfirmationNo
  863. ,paidDate = GETDATE()
  864. ,paidBy = @user
  865. WHERE rowId = @rowId
  866. -- ## Limit Update
  867. EXEC Proc_AgentBalanceUpdate @flag = 'p',@tAmt = @beneamount ,@settlingAgent = @pBranch
  868. END
  869. IF @@TRANCOUNT > 0
  870. COMMIT TRANSACTION
  871. SET @msg =
  872. CASE
  873. WHEN @flag = 'restore' THEN 'Transaction has been restored successfully'
  874. ELSE 'Transaction paid successfully'
  875. END
  876. EXEC proc_pushToAc @flag= 'i', @controlNoEncrypted = @pin
  877. --EXEC proc_INFICARE_sendTxn @flag = 'SI',@controlNoEncrypted = @pin
  878. --EXEC proc_INFICARE_payTxn @flag = 'p',@tranIds = @tranId
  879. SET @controlNo = dbo.fnadecryptstring(@pin)
  880. EXEC [proc_errorHandler] 0, @msg, @controlNo
  881. RETURN
  882. END
  883. IF @flag ='byPass'
  884. BEGIN
  885. SELECT @rowId = rowId,@beneAmount=beneAmount FROM riaRemitPayHistory WITH(NOLOCK) WHERE pin = DBO.FNAEncryptString(@pIN) AND recordStatus='PAID'
  886. SELECT CASE WHEN (@rowId IS NOT NULL OR @rowId > 0)THEN '0' ELSE '1' END errorCode
  887. , CASE WHEN (@rowId IS NOT NULL OR @rowId > 0)THEN 'Success' ELSE 'Transaction not found' END msg
  888. , @rowId id, @beneAmount extra
  889. END
  890. END TRY
  891. BEGIN CATCH
  892. IF @@TRANCOUNT > 0
  893. ROLLBACK TRANSACTION
  894. SELECT 1 errorCode, ERROR_MESSAGE() msg, NULL id
  895. END CATCH
  896. GO