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.

949 lines
54 KiB

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