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.

934 lines
26 KiB

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