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.

523 lines
33 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_validateTransaction] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROCEDURE [dbo].[proc_validateTransaction]
  9. @User VARCHAR(100),
  10. @SenderId INT,
  11. @sIpAddress VARCHAR(20),
  12. @ReceiverId INT = NULL,
  13. @rFirstName VARCHAR(50),
  14. @rMiddleName VARCHAR(50) = NULL,
  15. @rLastName VARCHAR(50),
  16. @rIdType VARCHAR(50) = NULL,
  17. @rIdNo VARCHAR(30) = NULL,
  18. @rIdIssue VARCHAR(10) = NULL,
  19. @rIdExpiry VARCHAR(10) = NULL,
  20. @rDob VARCHAR(10) = NULL,
  21. @rMobileNo VARCHAR(20),
  22. @rNativeCountry VARCHAR(50) = NULL,
  23. @rStateId INT = NULL,
  24. @rDistrictId INT = NULL,
  25. @rAddress VARCHAR(100),
  26. @rCity VARCHAR(50),
  27. @rEmail VARCHAR(50) = NULL,
  28. @rAccountNo VARCHAR(50) = NULL,
  29. @sCountryId INT,
  30. @pCountryId INT,
  31. @deliveryMethodId INT,
  32. @pBankId BIGINT = NULL,
  33. @pBranchId BIGINT = NULL,
  34. @collCurr VARCHAR(3),
  35. @payoutCurr VARCHAR(3),
  36. @collAmt MONEY,
  37. @payoutAmt MONEY,
  38. @transferAmt MONEY,
  39. @exRate DECIMAL(12,9),
  40. @calBy CHAR(1),
  41. @tpExRate DECIMAL(12,9),
  42. @payOutPartnerId BIGINT,
  43. @forexSessionId VARCHAR(40),
  44. @kftcLogId VARCHAR(20) = NULL,
  45. @paymentType VARCHAR(20),
  46. @PurposeOfRemittance VARCHAR(100),
  47. @SourceOfFund VARCHAR(100),
  48. @RelWithSender VARCHAR(100),
  49. @processId VARCHAR(40)
  50. AS
  51. SET NOCOUNT ON;
  52. SET XACT_ABORT ON;
  53. BEGIN TRY
  54. DECLARE @complianceRuleId INT
  55. ,@cAmtUSD MONEY
  56. ,@complienceMessage VARCHAR(1000) = NULL
  57. ,@shortMsg VARCHAR(100) = NULL
  58. ,@complienceErrorCode TINYINT = NULL
  59. ,@compErrorCode INT
  60. ,@customerType INT
  61. IF EXISTS( SELECT 'A' FROM customerMaster(NOLOCK) WHERE email = @user AND customerType = '11048')
  62. BEGIN
  63. SELECT '1' ErrorCode ,'Service is temporary blocked!' Msg ,NULL ID
  64. RETURN
  65. END
  66. IF @paymentType IS NULL
  67. SET @paymentType = 'WALLET'
  68. IF @paymentType = 'autodebit'
  69. BEGIN
  70. DECLARE @DATE DATETIME, @NextDate datetime
  71. SELECT @DATE = CAST(GETDATE() AS DATE)
  72. SELECT @NextDate = DATEADD(DAY,1,@DATE)
  73. SELECT @DATE = @DATE+' 23:20:00',@NextDate = @NextDate+' 00:40:00'
  74. IF GETDATE() BETWEEN @DATE AND @NextDate
  75. BEGIN
  76. SELECT '1' ErrorCode ,'KFTC service is not available between 11:30 PM to 12:30 AM' Msg ,NULL ID
  77. RETURN
  78. END
  79. END
  80. DECLARE @sCurrCostRate FLOAT ,@sCurrHoMargin FLOAT ,@pCurrCostRate FLOAT ,@customerRate FLOAT ,@agentCrossSettRate FLOAT,@serviceCharge MONEY
  81. DECLARE @iServiceCharge MONEY ,@iTAmt MONEY ,@iPAmt MONEY ,@iCAmt MONEY ,@iCustomerRate FLOAT
  82. DECLARE @place INT ,@currDecimal INT;
  83. DECLARE @agentAvlLimit MONEY--,@serviceCharge MONEY
  84. DECLARE @sAgent BIGINT,@sAgentName VARCHAR(100),@sBranch INT,@sBranchName VARCHAR(100),@sSuperAgent INT,@sSuperAgentName VARCHAR(100)
  85. DECLARE @sBirthDate DATE,@sIdIssueDate DATE,@sIdExpiryDate DATE,@senderName VARCHAR(100),@sIdNo VARCHAR(50),@sIdType VARCHAR(50),@sMobile VARCHAR(15),@sOccupation VARCHAR(50)
  86. DECLARE @pAgent BIGINT,@pSuperAgent BIGINT,@pAgentName VARCHAR(100),@receiverName VARCHAR(100)
  87. SELECT @sCountryId = 118,@sBranch = 2080
  88. SELECT @sAgent = sAgent, @sAgentName = sAgentName, @sBranch = sBranch, @sBranchName = sBranchName,
  89. @sSuperAgent = sSuperAgent, @sSuperAgentName = sSuperAgentName
  90. FROM dbo.FNAGetBranchFullDetails(@sBranch)
  91. ----SELECT @pCountry = COUNTRYNAME FROM COUNTRYMASTER (NOLOCK) WHERE COUNTRYID = @pCountryId
  92. SELECT TOP 1 @pAgent = AM.agentId
  93. FROM agentMaster AM(NOLOCK)
  94. WHERE AM.parentId = @payOutPartnerId AND agentType = 2903
  95. AND AM.isSettlingAgent = 'Y' AND AM.isApiPartner = 1
  96. SELECT @pSuperAgent = sSuperAgent,@pAgent = sAgent,@pAgentName = sAgentName FROM dbo.FNAGetBranchFullDetails(@pAgent)
  97. IF @receiverId IS NOT NULL
  98. BEGIN
  99. SELECT @receiverName = ISNULL(firstName,'')+ISNULL(' '+middleName,'')+ISNULL(' '+lastName1,'') +ISNULL(' '+lastName2,'')
  100. ,@rIdNo = idNumber
  101. FROM dbo.receiverInformation(NOLOCK)
  102. WHERE receiverId = @receiverId
  103. END
  104. ELSE
  105. SET @receiverName = ISNULL(@rFirstName,'')+ISNULL(' '+@rMiddleName,'')+ISNULL(' '+@rLastName,'')
  106. IF @rFirstName IS NULL AND @receiverId IS NULL
  107. BEGIN
  108. EXEC proc_errorHandler '1','Receiver name cannot be empty', NULL;
  109. RETURN;
  110. END
  111. IF NOT EXISTS (SELECT '' FROM TblPartnerwiseCountry(NOLOCK)
  112. WHERE AgentId = @payOutPartnerId AND CountryId = @pCountryId
  113. AND ISNULL(PaymentMethod,@deliveryMethodId) = @deliveryMethodId and IsActive = 1
  114. )
  115. BEGIN
  116. EXEC proc_errorHandler '1','Oops, something went wrong.Please perform the transaction again' ,null
  117. RETURN;
  118. END
  119. IF @payOutPartnerId = 585209 AND @pCountryId = 33 AND @deliveryMethodId = 2 AND @rIdNo IS NULL
  120. BEGIN
  121. EXEC proc_errorHandler '1','Receiver id number is missing.', NULL;
  122. RETURN;
  123. END
  124. IF ISNULL(@exRate,0) = 0
  125. BEGIN
  126. EXEC proc_errorHandler '1', 'Transaction cannot be proceed.Exchange Rate not defined', NULL
  127. RETURN
  128. END
  129. IF @pAgent IS NULL
  130. BEGIN
  131. EXEC proc_errorHandler '1','Oops, something went wrong.Please perform the transaction again' ,null
  132. RETURN;
  133. END
  134. IF NOT EXISTS (SELECT 'X' FROM dbo.customerMaster(nolock) WHERE email = @user AND approvedDate IS NOT NULL)
  135. BEGIN
  136. EXEC proc_errorHandler '1','You are not authorized to perform transaction :(', NULL;
  137. RETURN;
  138. END
  139. IF ISNULL(@paymentType,'') NOT IN ('wallet', 'autodebit')
  140. BEGIN
  141. EXEC proc_errorHandler '1','Invalid payment method.Please perform the transaction again!', NULL;
  142. RETURN;
  143. END
  144. SELECT @sIdType = idType,
  145. @sIdNo = idNumber ,
  146. @sMobile = cm.mobile,
  147. @senderName = isnull(fullName, firstname),
  148. @agentAvlLimit = dbo.FNAGetCustomerACBal(email),
  149. @sOccupation = V.detailTitle,
  150. @sIdIssueDate = cm.idIssueDate,
  151. @sIdExpiryDate = cm.idExpiryDate,
  152. @sBirthDate = cm.dob,
  153. @customerType = customerType
  154. FROM customerMaster(NOLOCK) cm
  155. LEFT JOIN staticDataValue V(NOLOCK) ON V.valueId = CM.occupation
  156. WHERE customerId = @senderId AND cm.approvedDate IS NOT NULL
  157. IF ISNULL(@sIdNo,'') = ''
  158. BEGIN
  159. EXEC proc_errorHandler '1','Invalid request found from customer.', NULL;
  160. RETURN
  161. END
  162. --IF @sIdIssueDate IS NULL OR @sIdIssueDate > CAST(GETDATE() AS DATE)
  163. --BEGIN
  164. -- EXEC proc_errorHandler '1','Enter ID Issue Date.Call:15886864', NULL;
  165. -- RETURN
  166. --END
  167. --IF @sBirthDate IS NULL OR @sBirthDate > CAST(GETDATE() AS DATE)
  168. --BEGIN
  169. -- EXEC proc_errorHandler '1','Enter valid DOB.Call:15886864', NULL;
  170. -- RETURN
  171. --END
  172. IF ISNULL(@sOccupation,'')=''
  173. BEGIN
  174. EXEC proc_errorHandler '1','Update Customer Occupation.Call:15886864', NULL;
  175. RETURN
  176. END
  177. /* Easy Remit limit check */
  178. IF @customerType = '11048'
  179. BEGIN
  180. IF ISNULL(@collAmt, 0) > 1000000
  181. BEGIN
  182. EXEC proc_errorHandler '1','You cannot remit more than one million(KRW)!', NULL
  183. RETURN
  184. END
  185. END
  186. IF @paymentType = 'WALLET'
  187. BEGIN
  188. IF ISNULL(@agentAvlLimit, 1) < ISNULL(@collAmt, 0)
  189. BEGIN
  190. EXEC proc_errorHandler '1','You donot have sufficient balance to do the transaction!', NULL;
  191. RETURN;
  192. END;
  193. END
  194. --IF (SELECT COUNT(1) FROM dbo.Split(' ',@rFirstName)) < 2
  195. IF @rFirstName IS NULL
  196. BEGIN
  197. EXEC proc_errorHandler '1', 'Receiver First Name is missing', NULL
  198. RETURN
  199. END
  200. IF @rLastName IS NULL
  201. BEGIN
  202. EXEC proc_errorHandler '1', 'Receiver Last Name is missing', NULL
  203. RETURN
  204. END
  205. IF isnull(@rMobileNo, '') = ''
  206. BEGIN
  207. EXEC proc_errorHandler '1', 'Receiver mobile number is required!', NULL
  208. RETURN
  209. END
  210. IF @rAddress IS NULL
  211. BEGIN
  212. EXEC proc_errorHandler '1','Receiver Address missing', NULL;
  213. RETURN;
  214. END;
  215. IF ISNULL(@deliveryMethodId, '') = ''
  216. BEGIN
  217. EXEC proc_errorHandler '1','Please choose payment mode', NULL;
  218. RETURN;
  219. END;
  220. IF ISNULL(@transferAmt, 0) = 0
  221. BEGIN
  222. EXEC proc_errorHandler '1','Transfer Amount missing', NULL;
  223. RETURN;
  224. END;
  225. IF ISNULL(@exRate, 0) = 0
  226. BEGIN
  227. EXEC proc_errorHandler '1', 'Exchange Rate missing',NULL;
  228. RETURN;
  229. END;
  230. if isnull(@PurposeOfRemittance,'') = ''
  231. BEGIN
  232. EXEC proc_errorHandler '1', 'Purpose of Remittance is required!', NULL
  233. RETURN
  234. END
  235. if isnull(@SourceOfFund,'') = ''
  236. BEGIN
  237. EXEC proc_errorHandler '1', 'Source of Fund is required!', NULL
  238. RETURN
  239. END
  240. IF ISNULL(@collAmt, 0) = 0
  241. BEGIN
  242. EXEC proc_errorHandler '1','Collection Amount is missing. Cannot send transaction',NULL;
  243. RETURN;
  244. END;
  245. IF @deliveryMethodId = 2 -- FOR BANK DEPOSIT
  246. BEGIN
  247. IF NOT EXISTS(SELECT 'A' FROM agentMaster(NOLOCK) WHERE agentId = @pBankId AND agenttype = 2903 AND isActive='Y' AND agentRole = '2')
  248. BEGIN
  249. EXEC proc_errorHandler '1', 'Invalid bank selected', NULL
  250. RETURN
  251. END
  252. IF @pBankId IS NULL
  253. BEGIN
  254. EXEC proc_errorHandler '1', 'Please select bank', NULL
  255. RETURN
  256. END
  257. IF @pAgent IS NULL
  258. BEGIN
  259. EXEC proc_errorHandler '1', 'Please select bank', NULL
  260. RETURN
  261. END
  262. IF @rAccountNo IS NULL
  263. BEGIN
  264. EXEC proc_errorHandler '1', 'Account number cannot be blank', NULL
  265. RETURN
  266. END
  267. END;
  268. SELECT @iServiceCharge = ISNULL(amount, -1)
  269. FROM [dbo].FNAGetServiceCharge(
  270. @sCountryId, @sSuperAgent, @sAgent, @sBranch,
  271. @pCountryId, @pSuperAgent, @pAgent, @pBranchId,
  272. @deliveryMethodId, @collAmt, @collCurr
  273. )
  274. IF @iServiceCharge = -1
  275. BEGIN
  276. EXEC proc_errorHandler '1', 'Transaction cannot be proceed. Service Charge is not defined', NULL
  277. RETURN
  278. END
  279. SELECT
  280. @customerRate = customerRate
  281. ,@sCurrCostRate = sCurrCostRate
  282. ,@sCurrHoMargin = sCurrHoMargin
  283. ,@pCurrCostRate = pCurrCostRate
  284. ,@agentCrossSettRate = agentCrossSettRate
  285. ,@serviceCharge = serviceCharge
  286. FROM exRateCalcHistory(NOLOCK)
  287. WHERE FOREX_SESSION_ID = @forexSessionId
  288. AND [USER_ID] = @User AND isExpired = 0
  289. IF ISNULL(@serviceCharge,0) > @collAmt
  290. BEGIN
  291. SELECT '1' ErrorCode ,'Collection amount should be more than service charge' Msg ,NULL
  292. RETURN;
  293. END;
  294. IF ISNULL(@serviceCharge,0) <> @iServiceCharge
  295. BEGIN
  296. SELECT '1' ErrorCode ,'Please re calculate rate and try again' Msg ,NULL
  297. RETURN;
  298. END;
  299. IF @customerRate IS NULL
  300. BEGIN
  301. EXEC proc_errorHandler 1, 'Transaction cannot be proceed.Please re-calculate the amount again.', NULL
  302. RETURN
  303. END
  304. IF ISNULL(@exRate,0) <> ISNULL(@customerRate,1)
  305. BEGIN
  306. EXEC proc_errorHandler 1, 'Amount detail not match. Please re-calculate the amount again', NULL
  307. RETURN
  308. END
  309. SELECT @iCustomerRate = @exRate, @iTAmt = @collAmt - @iServiceCharge
  310. SELECT @place = place, @currDecimal = currDecimal
  311. FROM currencyPayoutRound WITH(NOLOCK)
  312. WHERE ISNULL(isDeleted, 'N') = 'N'
  313. AND currency = @payoutCurr AND (tranType IS NULL OR tranType = @deliveryMethodId)
  314. SET @currDecimal = ISNULL(@currDecimal, 0)
  315. SET @place = ISNULL(@place, 0)
  316. SET @iPAmt = ROUND(@iTAmt * @iCustomerRate, @currDecimal)
  317. IF @payoutCurr = 'USD'
  318. SET @iPAmt = ROUND(@iPAmt, 2)
  319. ELSE
  320. SET @iPAmt = ROUND(@iPAmt, 0)
  321. IF @payoutCurr IN('MNT','MMK') AND ISNULL(@iPAmt,0) < ISNULL(@payoutAmt,1)+50
  322. BEGIN
  323. SET @payoutAmt = ISNULL(@iPAmt,0)
  324. END
  325. IF ISNULL(@iPAmt,0) <> ISNULL(@payoutAmt,1)
  326. BEGIN
  327. declare @iMsg VARCHAR(500) = 'Amount detail not match. Please re-calculate the amount again.'
  328. --+cast(@iPAmt as varchar(10))+cast(@payoutAmt as varchar(10))
  329. EXEC proc_errorHandler 1, @iMsg, NULL
  330. RETURN
  331. END
  332. SET @cAmtUSD = @transferAmt / (@sCurrCostRate + ISNULL(@sCurrHoMargin, 0))
  333. --Compliance Checking
  334. EXEC [proc_complianceRuleDetail]
  335. @flag = 'sender-limit'
  336. ,@user = @user
  337. ,@sIdType = @sIdType
  338. ,@sIdNo = @sIdNo
  339. ,@cAmt = @transferAmt
  340. ,@cAmtUSD = @cAmtUSD
  341. ,@customerId = @senderId
  342. ,@pCountryId = @pCountryId
  343. ,@deliveryMethod= @deliveryMethodId
  344. ,@message = @complienceMessage OUTPUT
  345. ,@shortMessage = @shortMsg OUTPUT
  346. ,@errCode = @complienceErrorCode OUTPUT
  347. ,@ruleId = @complianceRuleId OUTPUT
  348. IF(@complienceErrorCode <> 0)
  349. BEGIN
  350. IF(@complienceErrorCode = 1)
  351. BEGIN
  352. SET @compErrorCode=101
  353. --SELECT 101 errorCode,@msg msg, @complienceErrorCode id, @complienceMessage compApproveRemark,'compliance' vtype
  354. END
  355. ELSE
  356. BEGIN
  357. SET @compErrorCode=102
  358. INSERT remitTranComplianceTemp(csDetailTranId, matchTranId, agentRefId)
  359. SELECT @complianceRuleId, NULL, @processId
  360. --SELECT 102 errorCode,@msg msg, @complienceErrorCode id, @complienceMessage compApproveRemark,'compliance' vtype
  361. END
  362. INSERT INTO ComplianceLog(senderName, senderCountry, senderIdType, senderIdNumber, senderMobile, receiverName
  363. , receiverCountry,payOutAmt,complianceId,complianceReason,complainceDetailMessage,createdBy,createdDate,logType)
  364. SELECT @senderName, @sCountryId, @sIdType, @sIdNo, @sMobile, @receiverName
  365. , @pCountryId, @collAmt, @complianceRuleId, @shortMsg, @complienceMessage, @user, GETDATE(),'sender-limit'
  366. END
  367. IF @complienceErrorCode = 1
  368. BEGIN
  369. EXEC proc_errorHandler 1, @complienceMessage, NULL
  370. RETURN;
  371. END;
  372. EXEC [proc_complianceRuleDetail]
  373. @flag = 'receiver-limit'
  374. ,@user = @user
  375. ,@sIdType = @sIdType
  376. ,@sIdNo = @sIdNo
  377. ,@receiverName = @receiverName
  378. ,@cAmt = @transferAmt
  379. ,@cAmtUSD = @cAmtUSD
  380. ,@customerId = @senderId
  381. ,@pCountryId = @pCountryId
  382. ,@receiverMobile = @rMobileNo
  383. ,@deliveryMethod = @deliveryMethodId
  384. ,@message = @complienceMessage OUTPUT
  385. ,@shortMessage = @shortMsg OUTPUT
  386. ,@errCode = @complienceErrorCode OUTPUT
  387. ,@ruleId = @complianceRuleId OUTPUT
  388. IF(@complienceErrorCode <> 0)
  389. BEGIN
  390. IF(@complienceErrorCode = 1)
  391. BEGIN
  392. SET @compErrorCode=101
  393. --SELECT 101 errorCode,@msg msg, @complienceErrorCode id, @complienceMessage compApproveRemark,'compliance' vtype
  394. END
  395. ELSE
  396. BEGIN
  397. SET @compErrorCode=102
  398. INSERT remitTranComplianceTemp(csDetailTranId, matchTranId, agentRefId)
  399. SELECT @complianceRuleId, NULL, @processId
  400. --SELECT 102 errorCode,@msg msg, @complienceErrorCode id, @complienceMessage compApproveRemark,'compliance' vtype
  401. END
  402. INSERT INTO ComplianceLog(senderName, senderCountry, senderIdType, senderIdNumber, senderMobile, receiverName
  403. , receiverCountry,payOutAmt,complianceId,complianceReason,complainceDetailMessage,createdBy,createdDate,logType)
  404. SELECT @senderName, @sCountryId, @sIdType, @sIdNo, @sMobile, @receiverName
  405. , @pCountryId, @collAmt, @complianceRuleId, @shortMsg, @complienceMessage, @user, GETDATE(),'receiver-limit'
  406. END
  407. IF @complienceErrorCode = 1
  408. BEGIN
  409. EXEC proc_errorHandler 1, @shortMsg, NULL
  410. RETURN;
  411. END;
  412. --Compliance checking end
  413. DECLARE @remitTranTemp TABLE (
  414. tranId BIGINT,controlNo VARCHAR(20),cAmt MONEY,receiverName VARCHAR(200) ,
  415. receiverIdType VARCHAR(100),receiverIdNumber VARCHAR(50),dot DATETIME
  416. );
  417. INSERT INTO @remitTranTemp( tranId ,controlNo ,cAmt ,receiverName ,receiverIdType ,receiverIdNumber ,dot )
  418. SELECT rt.id ,rt.controlNo ,rt.cAmt ,rt.receiverName ,rec.idType ,rec.idNumber ,rt.createdDateLocal
  419. FROM vwRemitTran rt WITH ( NOLOCK )
  420. INNER JOIN vwTranSenders sen WITH ( NOLOCK ) ON rt.id = sen.tranId
  421. INNER JOIN vwTranReceivers rec WITH ( NOLOCK ) ON rt.id = rec.tranId
  422. WHERE sen.customerId = @senderId
  423. AND ( rt.approvedDate BETWEEN CONVERT(VARCHAR,GETDATE(),101) AND CONVERT(VARCHAR,GETDATE(),101)+ ' 23:59:59'
  424. OR ( approvedBy IS NULL AND cancelApprovedBy IS NULL )
  425. );
  426. IF EXISTS ( SELECT 'X' FROM @remitTranTemp
  427. WHERE cAmt = @collAmt
  428. AND ( receiverName = @rFirstName ) AND DATEDIFF(MI, dot, GETDATE()) <= 2
  429. )
  430. BEGIN
  431. EXEC proc_errorHandler 1,'Similar transaction found', NULL;
  432. RETURN;
  433. END;
  434. IF @user = 'demo.gme@gmeremit.com'
  435. BEGIN
  436. EXEC proc_errorHandler 1,'You can not send money through test GME acocunt :(', NULL;
  437. RETURN;
  438. END
  439. SELECT 0 ErrorCode,'Transaction is valid' Msg, null id
  440. RETURN
  441. END TRY
  442. BEGIN CATCH
  443. IF @@TRANCOUNT<>0
  444. ROLLBACK TRANSACTION
  445. DECLARE @errorMessage VARCHAR(MAX)
  446. SET @errorMessage = ERROR_MESSAGE()
  447. EXEC proc_errorHandler 1, @errorMessage, @user
  448. END CATCH
  449. GO