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.

794 lines
53 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[mobile_proc_validateTransaction] Script Date: 2019-05-29 $ 5:10:36 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROCEDURE [dbo].[mobile_proc_validateTransaction]
  9. (
  10. @flag VARCHAR(50) ,
  11. @user VARCHAR(100) ,
  12. @senderId VARCHAR(50) = NULL ,
  13. @sIpAddress VARCHAR(50) = NULL ,
  14. @benId VARCHAR(50) = NULL ,
  15. @rfName VARCHAR(100) = NULL ,
  16. @sIdType VARCHAR(100) =NULL,
  17. @sIdNo VARCHAR(100) =NULL,
  18. @sMobile VARCHAR(50) = NULL ,
  19. @rIdType VARCHAR(100) = NULL ,
  20. @rIdNo VARCHAR(50) = NULL ,
  21. @rIdValid DATETIME = NULL ,
  22. @rdob DATETIME = NULL ,
  23. @rTel VARCHAR(20) = NULL ,
  24. @rMobile VARCHAR(20) = NULL ,
  25. @rNaCountry VARCHAR(50) = NULL ,
  26. @rcity VARCHAR(100) = NULL ,
  27. @rAdd1 VARCHAR(150) = NULL ,
  28. @rEmail VARCHAR(100) = NULL ,
  29. @raccountNo VARCHAR(50) = NULL ,
  30. @pCountry VARCHAR(50) = NULL -- pay country
  31. ,@pCountryId INT = NULL -- PAY COUNTRY ID
  32. ,@deliveryMethod VARCHAR(50) = NULL -- payment mode
  33. ,@deliveryMethodId INT = NULL -- payment mode ID
  34. ,@pBank INT = NULL ,
  35. @pBankName VARCHAR(100) = NULL ,
  36. @pBankBranch INT = NULL ,
  37. @pBankBranchName VARCHAR(100) = NULL ,
  38. @pAgent INT = NULL ,
  39. @pAgentName VARCHAR(100) = NULL ,
  40. @pBranch INT = NULL ,
  41. @pBranchName VARCHAR(100) = NULL ,
  42. @pBankType CHAR(1) = NULL ,
  43. @pSuperAgent INT = NULL ,
  44. @pCurr VARCHAR(3) = NULL ,
  45. @collCurr VARCHAR(3) = NULL ,
  46. @cAmt MONEY = NULL ,
  47. @pAmt MONEY = NULL ,
  48. @tAmt MONEY = NULL ,
  49. @serviceCharge MONEY = NULL ,
  50. @discount MONEY = NULL ,
  51. @exRate FLOAT = NULL ,
  52. @purpose VARCHAR(150) = NULL ,
  53. @sourceOfFund VARCHAR(150) = NULL ,
  54. @relationship VARCHAR(100) = NULL ,
  55. @occupation VARCHAR(100) = NULL ,
  56. @payMsg VARCHAR(1000) = NULL ,
  57. @controlNo VARCHAR(20) = NULL ,
  58. @sCountryId INT = NULL ,
  59. @sCountry VARCHAR(100) = NULL ,
  60. @sBranch INT = NULL ,
  61. @sBranchName VARCHAR(100) = NULL ,
  62. @sAgent INT = NULL ,
  63. @sAgentName VARCHAR(100) = NULL ,
  64. @sSuperAgent INT = NULL ,
  65. @sSuperAgentName VARCHAR(100) = NULL ,
  66. @settlingAgent INT = NULL ,
  67. @branchMapCode VARCHAR(10) = NULL ,
  68. @agentMapCode VARCHAR(10) = NULL ,
  69. @collMode VARCHAR(50) = NULL ,
  70. @depositMode VARCHAR(50) = NULL -- DEPOSIT MODE CASH OR BANK
  71. ,
  72. @calBy CHAR(1) = NULL ,
  73. @scDiscount MONEY = NULL ,
  74. @cardOnline VARCHAR(50) = NULL ,
  75. @memberShipId VARCHAR(50) = NULL ,
  76. @agentRefId VARCHAR(50) = NULL ,
  77. @couponCode VARCHAR(20) = NULL ,
  78. @schemeId INT = NULL ,
  79. @tranId VARCHAR(50) = NULL ,
  80. @ScOrderNo BIGINT = NULL ,
  81. @unitaryBankAccountNo VARCHAR(50) = NULL ,
  82. @RState VARCHAR(10) = NULL,
  83. @RStateText VARCHAR(150) = NULL,
  84. @RLocation VARCHAR(20) = NULL,
  85. @RLocationText VARCHAR(150) = NULL,
  86. @VoucherXML XML=null,
  87. @tpExRate money = null,
  88. @tpPCurr varchar(10) = null,
  89. @tpRefNo varchar(20) = null,
  90. @tpTranId varchar(20) = null,
  91. @tpAgentId int = null,
  92. @payOutPartner BIGINT = NULL,
  93. @FOREX_SESSION_ID VARCHAR(40) = NULL,
  94. @kftcLogId BIGINT = NULL,
  95. @paymentType VARCHAR(20) = NULL,
  96. @receiverId VARCHAR(20) = NULL
  97. )
  98. AS
  99. SET NOCOUNT ON;
  100. SET XACT_ABORT ON;
  101. SET @receiverId=@benId
  102. DECLARE @complianceRuleId INT, @cAmtUSD MONEY
  103. ,@receiverName VARCHAR(50) = NULL
  104. ,@complienceMessage VARCHAR(1000) =NULL
  105. ,@shortMsg VARCHAR(100) =NULL
  106. ,@complienceErrorCode TINYINT = NULL
  107. ,@compErrorCode INT
  108. IF @paymentType IS NULL
  109. SET @paymentType ='wallet'
  110. BEGIN TRY
  111. BEGIN
  112. DECLARE @sCurrCostRate FLOAT ,
  113. @sCurrHoMargin FLOAT ,
  114. @pCurrCostRate FLOAT ,
  115. @pCurrHoMargin FLOAT ,
  116. @sCurrAgentMargin FLOAT ,
  117. @pCurrAgentMargin FLOAT ,
  118. @sCurrSuperAgentMargin FLOAT ,
  119. @pCurrSuperAgentMargin FLOAT ,
  120. @customerRate FLOAT ,
  121. @sAgentSettRate FLOAT ,
  122. @pDateCostRate FLOAT ,
  123. @agentCrossSettRate FLOAT ,
  124. @treasuryTolerance FLOAT ,
  125. @customerPremium FLOAT ,
  126. @schemePremium FLOAT = 0,
  127. @sharingValue MONEY ,
  128. @sharingType CHAR(1) ,
  129. @sAgentComm MONEY ,
  130. @sAgentCommCurrency VARCHAR(3) ,
  131. @sSuperAgentComm MONEY ,
  132. @sSuperAgentCommCurrency VARCHAR(3) ,
  133. @pAgentComm MONEY ,
  134. @pAgentCommCurrency VARCHAR(3) ,
  135. @pCommissionType CHAR(1) ,
  136. @pSuperAgentComm MONEY ,
  137. @pSuperAgentCommCurrency VARCHAR(3) ,
  138. @pSuperAgentName VARCHAR(100)
  139. --,@pCountryId INT
  140. ,
  141. @pStateId INT
  142. --,@deliveryMethodId INT
  143. ,
  144. @senderName VARCHAR(100) ,
  145. @id INT ,
  146. @customerStatus VARCHAR(20) ,
  147. @idExpiryDate DATETIME,
  148. @limitBal MONEY
  149. DECLARE @rowId INT;
  150. DECLARE @scValue MONEY ,
  151. @exRateOffer MONEY ,
  152. @scAction VARCHAR(5) ,
  153. @AmountLimitPerTran MONEY ,
  154. @AmountLimitPerDay MONEY ,
  155. @todaysTotalSent MONEY ,
  156. @tranMinimum MONEY ,
  157. @tranMaximum MONEY
  158. DECLARE @ad FLOAT;
  159. DECLARE @xAmt MONEY ,
  160. @sendingCustType INT ,
  161. @msg VARCHAR(MAX);
  162. DECLARE @iServiceCharge MONEY ,
  163. @iTAmt MONEY ,
  164. @iPAmt MONEY ,
  165. @iCAmt MONEY ,
  166. @iCustomerRate FLOAT
  167. DECLARE @place INT ,
  168. @currDecimal INT;
  169. DECLARE @controlNoEncrypted VARCHAR(20);
  170. DECLARE @csMasterId INT ,
  171. @count INT ,
  172. @compFinalRes VARCHAR(20);
  173. DECLARE @GMTDate VARCHAR(50);
  174. DECLARE @createdDate DATETIME;
  175. DECLARE @SENDERS_IDENTITY_TYPE VARCHAR(50);
  176. DECLARE @errorCode CHAR(1)= 0 ,@agentAvlLimit MONEY;
  177. declare @senderIdIssueDate date,@senderIdExpiryDate date,@senderBirthDate date
  178. DECLARE @discountType VARCHAR(2) = null, @discountvalue money =null
  179. SET @GMTDate = GETDATE()
  180. SELECT @sAgent = sAgent, @sAgentName = sAgentName, @sBranch = sBranch, @sBranchName = sBranchName,
  181. @sSuperAgent = sSuperAgent, @sSuperAgentName = sSuperAgentName
  182. FROM dbo.FNAGetBranchFullDetails(@sBranch)
  183. IF @pCountryId IS NULL
  184. SELECT @pCountryId = countryId FROM countryMaster (NOLOCK) WHERE countryName = @pCountry
  185. IF @pCountry LIKE '%?%'
  186. SELECT @pCountry = COUNTRYNAME FROM COUNTRYMASTER (NOLOCK) WHERE COUNTRYID = @pCountryId
  187. SELECT @sCountryId = 118,@sCountry = 'South Korea',@sAgentCommCurrency = 'KRW'
  188. SET @pBank = @pAgent
  189. SET @pAgent = null
  190. IF @pCountryId in(151)
  191. BEGIN
  192. SET @pAgent = @payOutPartner
  193. END
  194. ELSE
  195. BEGIN
  196. SELECT TOP 1 @pAgent = AM.agentId
  197. FROM agentMaster AM(NOLOCK)
  198. WHERE AM.parentId = @payOutPartner AND agentType=2903 AND AM.isSettlingAgent = 'Y' AND AM.isApiPartner = 1
  199. END
  200. DECLARE @SWIFTCODE VARCHAR(20)
  201. SELECT @SWIFTCODE = SWIFTCODE FROM AGENTMASTER(NOLOCK) WHERE agentId = @pBank AND agentType='2903' AND isActive='Y' AND agentCountryId IN('16','203','218','174','42')
  202. IF @SWIFTCODE IN('BNGC00000039','BNGB00000060')--dutchBangla
  203. SET @pAgent = '393864'
  204. ELSE IF @SWIFTCODE IN ('BNGC0000002','BNGB00000048')--agrani
  205. SET @pAgent = '404526'
  206. ELSE IF @SWIFTCODE IN('BNGC00000040','BNGB00000049')--islami
  207. SET @pAgent = '566853'
  208. ELSE IF @SWIFTCODE ='VN00000004' ----## IF VCBR AGENT
  209. SET @pAgent = 393229
  210. ELSE IF @SWIFTCODE = 'VN00000021' ----## IF SACOM AGENT
  211. SET @pAgent = 393862
  212. ELSE IF @SWIFTCODE = 'VN00000040' ----## IF Eximbank
  213. SET @pAgent = 601361
  214. ELSE IF @SWIFTCODE = 'PHC0000003' ----## IF CEBUEANA LHUILLIER
  215. SET @pAgent = 601392
  216. ELSE IF @pBank IN(404518) ----## ROCKET Wallet/DUTCH-BANGLA BANK LTD
  217. SELECT @pAgent = 393864
  218. ELSE IF @SWIFTCODE IN ('SLB00000070','SLB00000072') ----## USD TXN FROM XPRESS
  219. SELECT @pAgent = 415208
  220. SELECT @pSuperAgent = sSuperAgent,@pSuperAgentName = sSuperAgentName,
  221. @pAgent = sAgent,@pAgentName = sAgentName ,@pBranch = sBranch,@pBranchName = sBranchName
  222. FROM dbo.FNAGetBranchFullDetails(@pAgent)
  223. IF @receiverId IS NOT NULL
  224. BEGIN
  225. SELECT
  226. @receiverName = ISNULL(RI.firstName,'')+ISNULL(' '+RI.middleName,'')+ISNULL(' '+RI.lastName1,'') +ISNULL(' '+RI.lastName2,'')
  227. FROM dbo.receiverInformation(NOLOCK) AS RI
  228. WHERE RI.receiverId=@receiverId
  229. END
  230. IF @rfName IS NULL AND @receiverId IS NULL
  231. BEGIN
  232. EXEC proc_errorHandler 1,'Receiver name cannot be empty', NULL;
  233. RETURN;
  234. END
  235. IF @SWIFTCODE IS NOT NULL ----## AGRANI BANK LTD/DUTCH BANGLA BANK/Islami Bank Bangladesh Limited/@payOutPartner = mtrade JUST TO CHECK SETUP EXISTS ON TblPartnerwiseCountry OR NOT(EXRATE FROM LOCAL SETUP)
  236. SELECT TOP 1 @payOutPartner = AgentId FROM TblPartnerwiseCountry(NOLOCK) WHERE CountryId = @pCountryId AND ISNULL(PaymentMethod,@deliveryMethodId) = @deliveryMethodId and IsActive = 1
  237. IF NOT EXISTS (SELECT '' FROM TblPartnerwiseCountry(NOLOCK)
  238. WHERE AgentId = @payOutPartner AND CountryId = @pCountryId
  239. AND ISNULL(PaymentMethod,@deliveryMethodId) = @deliveryMethodId and IsActive = 1
  240. )
  241. BEGIN
  242. EXEC proc_errorHandler 1,'Oops, something went wrong.Please perform the transaction again.' ,null
  243. RETURN;
  244. END
  245. IF ISNULL(@tpExRate,0) = 0
  246. BEGIN
  247. EXEC proc_errorHandler 1, 'Transaction cannot be proceed.Partner Exchange Rate not defined', NULL
  248. RETURN
  249. END
  250. IF @pAgent IS NULL
  251. BEGIN
  252. EXEC proc_errorHandler 1,'Oops, something went wrong.Please perform the transaction again' ,null
  253. RETURN;
  254. END
  255. IF NOT EXISTS (SELECT 'X' FROM dbo.customerMaster(nolock) WHERE email = @user AND approvedDate IS NOT NULL)
  256. BEGIN
  257. EXEC proc_errorHandler 1,'You are not authorized to perform transaction :(', NULL;
  258. RETURN;
  259. END
  260. IF ISNULL(@paymentType,'') NOT IN ('wallet', 'autodebit')
  261. BEGIN
  262. EXEC proc_errorHandler 1,'Invalid payment method.Please perform the transaction again!', NULL;
  263. RETURN;
  264. END
  265. SELECT @SENDERS_IDENTITY_TYPE = CASE WHEN idType = '1302'THEN 'P' WHEN idType = '7316' THEN 'N' END ,
  266. @sIdNo = idNumber ,
  267. @senderName = isnull(fullName, firstname),
  268. @agentAvlLimit = dbo.FNAGetCustomerACBal(email),
  269. @senderIdIssueDate = cm.idIssueDate,
  270. @senderIdExpiryDate = cm.idExpiryDate,
  271. @senderBirthDate = cm.dob,
  272. @occupation = V.detailTitle
  273. FROM customerMaster(NOLOCK) cm
  274. LEFT JOIN staticDataValue V(NOLOCK) ON V.valueId = CM.occupation
  275. WHERE customerId = @senderId;
  276. IF @paymentType = 'wallet'
  277. BEGIN
  278. IF ISNULL(@agentAvlLimit, 1) < ISNULL(@cAmt, 0)
  279. BEGIN
  280. EXEC proc_errorHandler 1,'You donot have sufficient balance to do the transaction!', NULL;
  281. RETURN;
  282. END;
  283. END
  284. --ELSE
  285. --BEGIN
  286. -- EXEC proc_errorHandler 1,'Invalid payment method.Please perform the transaction again!', NULL;
  287. -- RETURN;
  288. --END
  289. IF @rfName IS NULL
  290. BEGIN
  291. EXEC proc_errorHandler 1,'Receiver full Name missing', NULL;
  292. RETURN;
  293. END;
  294. --IF (select COUNT(1) from dbo.Split(' ',@rfName))<2 AND @payOutPartner <> 224388
  295. --BEGIN
  296. -- EXEC proc_errorHandler 1, 'Receiver full Name is missing', NULL
  297. -- RETURN
  298. --END
  299. IF isnull(@rMobile, '')=''
  300. BEGIN
  301. EXEC proc_errorHandler 1, 'Receiver mobile number is required!', NULL
  302. RETURN
  303. END
  304. IF @rAdd1 IS NULL
  305. BEGIN
  306. EXEC proc_errorHandler 1,'Receiver Address missing', NULL;
  307. RETURN;
  308. END;
  309. IF ISNULL(@deliveryMethod, '') = ''
  310. BEGIN
  311. EXEC proc_errorHandler 1,'Please choose payment mode', NULL;
  312. RETURN;
  313. END;
  314. IF @serviceCharge IS NULL
  315. BEGIN
  316. EXEC proc_errorHandler 1, 'Service Charge missing',NULL;
  317. RETURN;
  318. END;
  319. IF ISNULL(@tAmt, 0) = 0
  320. BEGIN
  321. EXEC proc_errorHandler 1,'Transfer Amount missing', NULL;
  322. RETURN;
  323. END;
  324. IF ISNULL(@exRate, 0) = 0
  325. BEGIN
  326. EXEC proc_errorHandler 1, 'Exchange Rate missing',NULL;
  327. RETURN;
  328. END;
  329. if isnull(@purpose,'') = ''
  330. BEGIN
  331. EXEC proc_errorHandler 1, 'Purpose of Remittance is required!', NULL
  332. RETURN
  333. END
  334. if isnull(@sourceOfFund,'') = ''
  335. BEGIN
  336. EXEC proc_errorHandler 1, 'Source of Fund is required!', NULL
  337. RETURN
  338. END
  339. IF ISNULL(@cAmt, 0) = 0
  340. BEGIN
  341. EXEC proc_errorHandler 1,'Collection Amount is missing. Cannot send transaction',NULL;
  342. RETURN;
  343. END;
  344. IF @deliveryMethod = 'Bank Deposit'
  345. BEGIN
  346. IF EXISTS(SELECT 'A' FROM AgentBankMapping(NOLOCK) WHERE bankId = @pBank AND @pcountryId = 151)
  347. BEGIN
  348. SELECT @pAgent = bankpartnerId from AgentBankMapping(NOLOCK) WHERE bankId = @pBank
  349. SELECT @pSuperAgent = sSuperAgent,@pSuperAgentName = sSuperAgentName,
  350. @pAgent = sAgent,@pAgentName = sAgentName ,@pBranch = sBranch,@pBranchName = sBranchName
  351. FROM dbo.FNAGetBranchFullDetails(@pAgent)
  352. END
  353. IF NOT EXISTS(SELECT 'A' FROM agentMaster(nolock) where agentId = @pBank and agenttype =2903 and IsIntl = 1)
  354. BEGIN
  355. EXEC proc_errorHandler 1, 'Invalid bank selected', NULL
  356. return
  357. END
  358. IF @pBank IS NULL
  359. BEGIN
  360. EXEC proc_errorHandler 1, 'Please select bank', NULL
  361. RETURN
  362. END
  363. IF @pAgent IS NULL
  364. BEGIN
  365. EXEC proc_errorHandler 1, 'Please select bank', NULL
  366. RETURN
  367. END
  368. IF @raccountNo IS NULL
  369. BEGIN
  370. EXEC proc_errorHandler 1, 'Account number cannot be blank', NULL
  371. RETURN
  372. END
  373. END;
  374. ----## VALIDATION FOR XPRESS MONEY
  375. IF @payOutPartner = '415207'
  376. BEGIN
  377. IF @senderIdIssueDate > CAST(GETDATE() AS DATE)
  378. BEGIN
  379. EXEC proc_errorHandler 1,'Enter ID Issue Date.Call:15886864', NULL;
  380. RETURN
  381. END
  382. IF ISNULL(@senderIdExpiryDate,'') < CAST(GETDATE() AS DATE) AND @sIdType <> 'National ID'
  383. BEGIN
  384. EXEC proc_errorHandler 1,'Update ID Expiry Date.Call:15886864', NULL;
  385. RETURN
  386. END
  387. IF @senderBirthDate > CAST(GETDATE() AS DATE)
  388. BEGIN
  389. EXEC proc_errorHandler 1,'Enter valid DOB.Call:15886864', NULL;
  390. RETURN
  391. END
  392. IF ISNULL(@occupation,'')=''
  393. BEGIN
  394. EXEC proc_errorHandler 1,'Update Customer Occupation.Call:15886864', NULL;
  395. RETURN
  396. END
  397. END
  398. IF @pCountryId = 36 ----## FOR CAMBODIA
  399. BEGIN
  400. SELECT @iServiceCharge = ISNULL(amount, -1)
  401. FROM [dbo].FNAGetServiceCharge(
  402. @sCountryId, @sSuperAgent, @sAgent, @sBranch,
  403. @pCountryId, @pSuperAgent, @pAgent, NULL,
  404. @deliveryMethodId, @pAmt, @collCurr
  405. )
  406. END
  407. --ELSE IF @pCountryId = 42 AND @pCurr = 'USD' and @deliveryMethodId = 2
  408. --BEGIN
  409. -- SELECT @iServiceCharge = amount
  410. -- FROM [dbo].FNAGetServiceCharge(@sCountryId,@sSuperAgent,@sAgent,@sBranch,
  411. -- @pCountryId,@pSuperAgent,@pAgent,NULL,@deliveryMethodId,@pAmt,'USD');
  412. --END
  413. ELSE
  414. BEGIN
  415. SELECT @iServiceCharge = ISNULL(amount, -1)
  416. FROM [dbo].FNAGetServiceCharge(
  417. @sCountryId, @sSuperAgent, @sAgent, @sBranch,
  418. @pCountryId, @pSuperAgent, @pAgent, @pBranch,
  419. @deliveryMethodId, @cAmt, @collCurr
  420. )
  421. END
  422. IF @iServiceCharge = -1
  423. BEGIN
  424. EXEC proc_errorHandler 1, 'Transaction cannot be proceed. Service Charge is not defined', NULL
  425. RETURN
  426. END
  427. IF isnull(@iServiceCharge,0) <> isnull(@serviceCharge,1)
  428. BEGIN
  429. EXEC proc_errorHandler 1, 'Transaction cannot be proceed. Amount detail not match', NULL
  430. RETURN
  431. END
  432. IF ISNULL(@serviceCharge,0) > @cAmt
  433. BEGIN
  434. SELECT '1' ErrorCode ,'COLLECTION AMOUNT SHOULD BE MORE THAN SERVICE CHARGE' Msg ,NULL
  435. RETURN;
  436. END;
  437. SELECT @iServiceCharge = @serviceCharge , @customerRate = @exRate;
  438. SELECT
  439. --@customerRate = round(@tpExRate/sAgentSettRate, 8)
  440. @customerRate = customerRate
  441. ,@sCurrCostRate = sCurrCostRate
  442. ,@sCurrHoMargin = sCurrHoMargin
  443. ,@sCurrAgentMargin = sCurrAgentMargin
  444. ,@pCurrCostRate = @tpExRate
  445. ,@pCurrHoMargin = 0
  446. ,@pCurrAgentMargin = 0
  447. ,@treasuryTolerance = 0
  448. ,@customerPremium = 0
  449. ,@sharingValue = 0
  450. ,@sharingType = 0
  451. FROM dbo.FNAGetExRate(@sCountryId,@sAgent,@sBranch,@collCurr,@pCountryId,@pAgent,@pCurr,@deliveryMethodId)
  452. --select @sCountryId,@sAgent,@sBranch,@collCurr,@pCountryId,@pAgent,@pCurr,@deliveryMethodId
  453. IF @customerRate IS NULL
  454. BEGIN
  455. EXEC proc_errorHandler 1, 'Transaction cannot be proceed. Exchange Rate not defined', NULL
  456. RETURN
  457. END
  458. SELECT
  459. @customerRate = customerRate
  460. ,@sCurrCostRate = sCurrCostRate
  461. ,@sCurrHoMargin = sCurrHoMargin
  462. ,@sCurrAgentMargin = sCurrAgentMargin
  463. ,@pCurrCostRate = pCurrCostRate
  464. ,@pCurrHoMargin = pCurrHoMargin
  465. ,@pCurrAgentMargin = pCurrAgentMargin
  466. ,@agentCrossSettRate = agentCrossSettRate
  467. ,@treasuryTolerance = treasuryTolerance
  468. ,@customerPremium = customerPremium
  469. ,@sharingValue = sharingValue
  470. ,@sharingType = sharingType
  471. ,@serviceCharge = serviceCharge
  472. ,@iPAmt = pAmt
  473. ,@schemeId = schemeId
  474. FROM exRateCalcHistory(NOLOCK)
  475. WHERE FOREX_SESSION_ID = @FOREX_SESSION_ID AND USER_ID = @user
  476. IF @paymentType = 'autodebit'
  477. BEGIN
  478. DECLARE @tranAmt MONEY = NULL
  479. ------------Find Coupon---------------
  480. IF ISNULL(@schemeId,'') <> ''
  481. BEGIN
  482. IF EXISTS (SELECT '1'
  483. FROM CouponIssue(NOLOCK) AS CI
  484. INNER JOIN CouponSetup(NOLOCK) AS CS
  485. ON CI.couponId = CS.rowId
  486. WHERE CI.rowId = @schemeId
  487. AND CI.endDate >= GETDATE()
  488. AND CI.isActive = 'Y'
  489. AND CS.endDate >= GETDATE()
  490. AND CS.isActive = 'Y'
  491. AND CS.couponType = '1'
  492. AND (ISNULL(CS.usageLimit,0) = 0
  493. OR ISNULL(CS.usageLimit,0) > ISNULL(CI.usedCount,0))
  494. )
  495. BEGIN
  496. SELECT TOP(1)
  497. @discountvalue = CS.discountValue,
  498. @discountType = CS.discountType
  499. FROM CouponIssue(NOLOCK) CI
  500. INNER JOIN CouponSetup(NOLOCK) CS
  501. ON CI.couponId = CS.rowId
  502. WHERE CI.isActive = 'Y'
  503. AND CI.endDate >= GETDATE()
  504. AND CS.endDate >= GETDATE()
  505. AND CS.isActive = 'Y'
  506. AND CS.couponType = '1'
  507. AND CI.rowId = @schemeId
  508. AND (ISNULL(CS.usageLimit,0) = 0
  509. OR ISNULL(CS.usageLimit,0) > ISNULL(CI.usedCount,0))
  510. ORDER BY CI.rowId ASC
  511. IF @discountType = '1'
  512. BEGIN
  513. SET @schemePremium = @serviceCharge * (@discountvalue/100)
  514. END
  515. IF @discountType ='2'
  516. BEGIN
  517. SET @schemePremium = @discountvalue
  518. END
  519. END
  520. ELSE
  521. BEGIN
  522. EXEC proc_errorHandler 1, 'Transaction cannot be proceed. Coupon Checked. But, Can Not Find Coupon', NULL
  523. RETURN
  524. END
  525. END
  526. SELECT @tranAmt = tranAmt
  527. FROM KFTC_CUSTOMER_TRANSFER (NOLOCK)
  528. WHERE rowId = @kftcLogId
  529. IF @tranAmt IS NULL
  530. BEGIN
  531. EXEC proc_errorHandler 1,'Invalid auto debit request.Please perform the transaction again!', NULL;
  532. RETURN;
  533. END
  534. IF @tranAmt <> @cAmt - ISNULL(@schemePremium,0)
  535. BEGIN
  536. EXEC proc_errorHandler 1,'Invalid transaction amount. Please contact GME Support!', NULL;
  537. RETURN;
  538. END
  539. END
  540. ------------Exists Coupon Check---------------
  541. IF ISNULL(@schemeId,'') <> ''
  542. BEGIN
  543. IF NOT EXISTS (SELECT '1'
  544. FROM CouponIssue(NOLOCK) AS CI
  545. INNER JOIN CouponSetup(NOLOCK) AS CS
  546. ON CI.couponId = CS.rowId
  547. WHERE CI.rowId = @schemeId
  548. AND CI.endDate >= GETDATE()
  549. AND CI.isActive = 'Y'
  550. AND CS.endDate >= GETDATE()
  551. AND CS.isActive = 'Y'
  552. AND CS.couponType = '1'
  553. AND (ISNULL(CS.usageLimit,0) = 0
  554. OR ISNULL(CS.usageLimit,0) > ISNULL(CI.usedCount,0))
  555. )
  556. BEGIN
  557. EXEC proc_errorHandler 1, 'Transaction cannot be proceed. Coupon Checked. But, Can Not Find Coupon', NULL
  558. RETURN
  559. END
  560. END
  561. IF @customerRate IS NULL
  562. BEGIN
  563. EXEC proc_errorHandler 1, 'Transaction cannot be proceed. Exchange Rate not defined.', NULL
  564. RETURN
  565. END
  566. SET @customerRate = CAST(@customerRate AS DECIMAL(15,10))
  567. IF ISNULL(@exRate,0) <> ISNULL(@customerRate,1)
  568. BEGIN
  569. EXEC proc_errorHandler 1, 'Amount detail not match. Please re-calculate the amount again', NULL
  570. RETURN
  571. END
  572. DECLARE @scDisc MONEY
  573. SELECT @iCustomerRate = @exRate, @iTAmt = @cAmt - @iServiceCharge
  574. SELECT @place = place, @currDecimal = currDecimal
  575. FROM currencyPayoutRound WITH(NOLOCK) WHERE ISNULL(isDeleted, 'N') = 'N'
  576. AND currency = @pCurr AND (tranType IS NULL OR tranType = @deliveryMethodId)
  577. SET @currDecimal = ISNULL(@currDecimal, 0)
  578. SET @place = ISNULL(@place, 0)
  579. --SET @iPAmt = ROUND(@iTAmt * @iCustomerRate, @currDecimal)
  580. IF @pCurr = 'USD'
  581. SET @iPAmt = ROUND(@iPAmt, @currDecimal)
  582. ELSE
  583. SET @iPAmt = ROUND(@iPAmt, 0)
  584. IF @pCurr IN ('VND','IDR','MMK') AND ISNULL(@iPAmt,0) < ISNULL(@pAmt,1)+10
  585. BEGIN
  586. SET @iPAmt = @pAmt
  587. END
  588. IF ISNULL(@iPAmt,0) <> ISNULL(@pAmt,1)
  589. BEGIN
  590. declare @iMsg VARCHAR(500) = 'Amount detail not match. Please re-calculate the amount again.' + CAST(@iPAmt AS VARCHAR) + ' - ' + CAST(@pAmt AS VARCHAR)
  591. EXEC proc_errorHandler 1, @iMsg, NULL
  592. RETURN
  593. END
  594. --OFAC Checking
  595. DECLARE @receiverOfacRes VARCHAR(MAX), @ofacRes VARCHAR(MAX), @ofacReason VARCHAR(200)
  596. EXEC proc_ofacTracker @flag = 't', @name = @senderName, @Result = @ofacRes OUTPUT
  597. EXEC proc_ofacTracker @flag = 't', @name = @receiverName, @Result = @receiverOfacRes OUTPUT
  598. DECLARE @result VARCHAR(MAX)
  599. IF ISNULL(@ofacRes, '') <> ''
  600. BEGIN
  601. SET @ofacReason = 'Matched by sender name'
  602. END
  603. IF ISNULL(@receiverOfacRes, '') <> ''
  604. BEGIN
  605. SET @ofacRes = ISNULL(@ofacRes + ',' + @receiverOfacRes, '' + @receiverOfacRes)
  606. SET @ofacReason = 'Matched by receiver name'
  607. END
  608. IF ISNULL(@ofacRes, '') <> '' AND ISNULL(@receiverOfacRes, '') <> ''
  609. BEGIN
  610. SET @ofacReason = 'Matched by both sender name and receiver name'
  611. END
  612. --Ofac Checking End
  613. SET @cAmtUSD = @cAmt / (@sCurrCostRate + ISNULL(@sCurrHoMargin, 0))
  614. --Compliance Checking
  615. EXEC [proc_complianceRuleDetail]
  616. @flag = 'receiver-limit'
  617. ,@user = @user
  618. ,@sIdType = @sIdType
  619. ,@sIdNo = @sIdNo
  620. ,@receiverName = @rfName
  621. ,@cAmt = @cAmt
  622. ,@cAmtUSD = @cAmtUSD
  623. ,@customerId = @senderId
  624. ,@receiverMobile= @rMobile
  625. ,@pCountryId = @pCountryId
  626. ,@deliveryMethod= @deliveryMethodId
  627. ,@message = @complienceMessage OUTPUT
  628. ,@shortMessage = @shortMsg OUTPUT
  629. ,@errCode = @complienceErrorCode OUTPUT
  630. ,@ruleId = @complianceRuleId OUTPUT
  631. IF(@complienceErrorCode <> 0)
  632. BEGIN
  633. IF(@complienceErrorCode = 1)
  634. BEGIN
  635. SET @compErrorCode=101
  636. --SELECT 101 errorCode,@msg msg, @complienceErrorCode id, @complienceMessage compApproveRemark,'compliance' vtype
  637. END
  638. ELSE
  639. BEGIN
  640. SET @compErrorCode=102
  641. INSERT remitTranComplianceTemp(csDetailTranId, matchTranId, agentRefId)
  642. SELECT @complianceRuleId, NULL, @agentRefId
  643. --SELECT 102 errorCode,@msg msg, @complienceErrorCode id, @complienceMessage compApproveRemark,'compliance' vtype
  644. END
  645. INSERT INTO ComplianceLog(senderName, senderCountry, senderIdType, senderIdNumber, senderMobile, receiverName
  646. , receiverCountry,payOutAmt,complianceId,complianceReason,complainceDetailMessage,createdBy,createdDate,logType)
  647. SELECT @senderName, @sCountry, @sIdType, @sIdNo, @sMobile, @receiverName
  648. , @pCountry, @cAmt, @complianceRuleId, @shortMsg, @complienceMessage, @user, GETDATE(),'online'
  649. END
  650. IF @complienceErrorCode = 1
  651. BEGIN
  652. EXEC proc_errorHandler 1, @complienceMessage, NULL
  653. RETURN;
  654. END;
  655. --Compliance checking end
  656. select @pAgentCommCurrency = 'USD'
  657. select
  658. @pAgentComm = (SELECT amount FROM dbo.[FNAGetPayComm](
  659. @sAgent,@sCountryId,NULL, NULL, @pCountryId
  660. , null, @pAgent, @pAgentCommCurrency,@deliveryMethodId, @cAmt, @pAmt, @serviceCharge, NULL, NULL)
  661. )
  662. DECLARE @agentFxGain MONEY;
  663. DECLARE @remitTranTemp TABLE (
  664. tranId BIGINT,controlNo VARCHAR(20),cAmt MONEY,receiverName VARCHAR(200) ,
  665. receiverIdType VARCHAR(100),receiverIdNumber VARCHAR(50),dot DATETIME
  666. );
  667. DECLARE @moneySendTemp TABLE(
  668. tranNo BIGINT ,refno VARCHAR(20) ,paidAmt MONEY ,receiverName VARCHAR(200) ,
  669. receiverIdDescription VARCHAR(100) ,receiverIdDetail VARCHAR(50)
  670. );
  671. INSERT INTO @remitTranTemp( tranId ,controlNo ,cAmt ,receiverName ,receiverIdType ,receiverIdNumber ,dot )
  672. SELECT rt.id ,rt.controlNo ,rt.cAmt ,rt.receiverName ,rec.idType ,rec.idNumber ,rt.createdDateLocal
  673. FROM vwRemitTran rt WITH ( NOLOCK )
  674. INNER JOIN vwTranSenders sen WITH ( NOLOCK ) ON rt.id = sen.tranId
  675. INNER JOIN vwTranReceivers rec WITH ( NOLOCK ) ON rt.id = rec.tranId
  676. WHERE sen.customerId = @senderId
  677. AND ( rt.approvedDate BETWEEN CONVERT(VARCHAR,GETDATE(),101) AND CONVERT(VARCHAR,GETDATE(),101)+ ' 23:59:59'
  678. OR ( approvedBy IS NULL AND cancelApprovedBy IS NULL )
  679. );
  680. IF EXISTS ( SELECT 'X' FROM @remitTranTemp
  681. WHERE cAmt = @cAmt
  682. AND ( receiverName = @rfName ) AND DATEDIFF(MI, dot, GETDATE()) <= 2
  683. )
  684. BEGIN
  685. EXEC proc_errorHandler 1,'Similar transaction found', NULL;
  686. RETURN;
  687. END;
  688. IF @user = 'demo.gme@gmeremit.com'
  689. BEGIN
  690. EXEC proc_errorHandler 1,'You can not send money through test GME acocunt :(', NULL;
  691. RETURN;
  692. END
  693. SELECT 0 ErrorCode,'Transaction is valid' Msg, @id id, null extra,ROUND(@pAmt/@pCurrCostRate,2) Extra2;
  694. RETURN
  695. END
  696. END TRY
  697. BEGIN CATCH
  698. IF @@TRANCOUNT<>0
  699. ROLLBACK TRANSACTION
  700. DECLARE @errorMessage VARCHAR(MAX)
  701. SET @errorMessage = ERROR_MESSAGE()
  702. EXEC proc_errorHandler 1, @errorMessage, @user
  703. END CATCH