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.

594 lines
23 KiB

5 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. GO
  2. use FastMoneyPro_Remit;
  3. GO
  4. CREATE OR ALTER PROC PROC_REMIT_INBOUND_TRANSACTION_VALIDATE
  5. (
  6. @Flag VARCHAR(20)
  7. , @UserName VARCHAR(80) = NULL
  8. , @SendingCountryCode VARCHAR(10) = NULL
  9. , @PayoutCountryCode VARCHAR(10) = NULL
  10. , @PayoutTypeCode VARCHAR(10) = NULL
  11. , @PayoutBankCode VARCHAR(10) = NULL
  12. , @PayoutBankBranchCode VARCHAR(10) = NULL
  13. , @PayoutCurrencyCode VARCHAR(10) = NULL
  14. , @SendingCurrencyCode VARCHAR(10) = NULL
  15. , @CollectionAmount MONEY = NULL
  16. , @TransferAmount MONEY = NULL
  17. , @PayoutAmount MONEY = NULL
  18. , @ServiceCharge MONEY = NULL
  19. , @ExchangeRate FLOAT = NULL
  20. , @RemittancePurpose INT = NULL
  21. , @SourceOfFund INT = NULL
  22. , @Relationship INT = NULL
  23. , @Occupation INT = NULL
  24. , @PartnerTransactionId VARCHAR(40) = NULL
  25. , @CalcBy CHAR(1) = NULL
  26. , @AgentId INT = NULL
  27. --SENDER DETAILS
  28. , @SenderFirstName VARCHAR(60) = NULL
  29. , @SenderMiddleName VARCHAR(60) = NULL
  30. , @SenderLastName VARCHAR(40) = NULL
  31. , @SenderIdType INT = NULL
  32. , @SenderIdNo VARCHAR(20) = NULL
  33. , @SenderIdIssuedDate VARCHAR(10) = NULL
  34. , @SenderIdValidDate VARCHAR(10) = NULL
  35. , @SenderMobile VARCHAR(15) = NULL
  36. , @SenderNativeCountryCode VARCHAR(10) = NULL
  37. , @SenderCity VARCHAR(80) = NULL
  38. , @SenderAddress VARCHAR(150) = NULL
  39. , @SenderEmail VARCHAR(150) = NULL
  40. , @SenderGender VARCHAR(10) = NULL
  41. , @SenderDOB VARCHAR(10) = NULL
  42. --RECEIVER DETAILS
  43. , @ReceiverFirstName VARCHAR(60) = NULL
  44. , @ReceiverMiddleName VARCHAR(60) = NULL
  45. , @ReceiverLastName VARCHAR(60) = NULL
  46. , @ReceiverIdType INT = NULL
  47. , @ReceiverIdNo VARCHAR(20) = NULL
  48. , @ReceiverIdIssuedDate VARCHAR(10) = NULL
  49. , @ReceiverIdValidDate VARCHAR(10) = NULL
  50. , @ReceiverMobile VARCHAR(15) = NULL
  51. , @ReceiverCity VARCHAR(80) = NULL
  52. , @ReceiverAddress VARCHAR(150) = NULL
  53. , @ReceiverEmail VARCHAR(150) = NULL
  54. , @ReceiverAccountNo VARCHAR(40) = NULL
  55. , @IpAddress VARCHAR(20) = NULL
  56. )
  57. AS;
  58. SET NOCOUNT ON;
  59. SET XACT_ABORT ON;
  60. BEGIN TRY
  61. BEGIN
  62. DECLARE @ErrorMsg VARCHAR(MAX) = NULL
  63. IF @Flag = 'VALIDATE'
  64. BEGIN
  65. --IF EXISTS(SELECT * FROM TBL_INBOUND_TRANSACTION_VERIFY (NOLOCK) WHERE PartnerTransactionId = @PartnerTransactionId)
  66. --BEGIN
  67. -- EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Duplicate partner transaction id.', @Id = NULL;
  68. -- RETURN;
  69. --END
  70. DECLARE @PCountryId INT, @DeliveryMethodId INT, @PayoutPartner INT, @ExRateCalcByPartner BIT, @ScountryId INT
  71. DECLARE @SSuperAgentId INT, @PAgent INT, @SAgentId INT = @AgentId, @PCountryName VARCHAR(80), @SCountryName VARCHAR(80)
  72. SELECT @PCountryId = countryId, @PCountryName = countryName
  73. FROM COUNTRYMASTER (NOLOCK)
  74. WHERE countryCode = @PayoutCountryCode
  75. IF ISNULL(@PCountryId, 0) = 0
  76. BEGIN
  77. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid PayoutCountryCode, contact Head Office.', @Id = NULL;
  78. RETURN;
  79. END
  80. SELECT @ScountryId = countryId, @SCountryName = countryName
  81. FROM COUNTRYMASTER (NOLOCK)
  82. WHERE countryCode = @SendingCountryCode
  83. IF ISNULL(@ScountryId, 0) = 0
  84. BEGIN
  85. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid SendingCountryCode, contact Head Office.', @Id = NULL;
  86. RETURN;
  87. END
  88. DECLARE @DeliveryMethodName VARCHAR(40)
  89. SELECT @DeliveryMethodId = serviceTypeId, @DeliveryMethodName = typeTitle
  90. FROM SERVICETYPEMASTER (NOLOCK)
  91. WHERE serviceCode = @PayoutTypeCode
  92. IF ISNULL(@DeliveryMethodId, 0) = 0
  93. BEGIN
  94. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid PaymentTypeCode, contact Head Office.', @Id = NULL;
  95. RETURN;
  96. END
  97. SELECT @PayoutPartner = AGENTID
  98. ,@ExRateCalcByPartner = ISNULL(exRateCalByPartner, 0)
  99. FROM TblPartnerwiseCountry(NOLOCK)
  100. WHERE CountryId = @PCountryId
  101. AND IsActive = 1
  102. AND ISNULL(PaymentMethod, @DeliveryMethodId) = @DeliveryMethodId
  103. IF ISNULL(@PayoutPartner, 0) = 0
  104. BEGIN
  105. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Pyout partner not defined for selected Payout Country, contact Head Office.', @Id = NULL;
  106. RETURN;
  107. END
  108. SELECT @PAgent = AGENTID
  109. FROM AGENTMASTER(NOLOCK)
  110. WHERE PARENTID = @PayoutPartner
  111. AND ISNULL(ISSETTLINGAGENT, 'N') = 'Y';
  112. DECLARE @PSuperAgent INT, @PSuperAgentName VARCHAR(150), @PAgentName VARCHAR(150), @PBranch INT, @PBranchName VARCHAR(150)
  113. SELECT @PSuperAgent = sSuperAgent
  114. ,@PSuperAgentName = sSuperAgentName
  115. ,@PAgent = sAgent
  116. ,@PAgentName = sAgentName
  117. ,@PBranch = sBranch
  118. ,@PBranchName = sBranchName
  119. FROM dbo.FNAGetBranchFullDetails(@PAGENT)
  120. DECLARE @PCurrencyCode VARCHAR(10)
  121. SELECT @PCurrencyCode = pCurrency
  122. FROM dbo.exRateTreasury WITH (NOLOCK)
  123. WHERE pCountry = @PCountryId
  124. AND pAgent = @PAgent
  125. IF @PCurrencyCode IS NULL
  126. SELECT @PCurrencyCode = pCurrency
  127. FROM dbo.exRateTreasury WITH (NOLOCK)
  128. WHERE pCountry = @PCountryId
  129. AND pAgent IS NULL
  130. IF @PCurrencyCode IS NULL
  131. BEGIN
  132. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Currency not been defined yet for receiving country, contact Head Office.', @Id = NULL;
  133. RETURN;
  134. END
  135. IF @PCurrencyCode <> @PayoutCurrencyCode
  136. BEGIN
  137. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid PayoutCurrencyCode, contact Head Office.', @Id = NULL;
  138. RETURN;
  139. END
  140. DECLARE @Place INT, @CurrDecimal INT
  141. SELECT @Place = place
  142. ,@CurrDecimal = currDecimal
  143. FROM currencyPayoutRound WITH (NOLOCK)
  144. WHERE ISNULL(isDeleted, 'N') = 'N'
  145. AND currency = @PCurrencyCode
  146. AND ISNULL(tranType, @DeliveryMethodId) = @DeliveryMethodId
  147. SET @currDecimal = ISNULL(@currDecimal, 0)
  148. DECLARE @ExRateCalculated FLOAT, @sCurrCostRate FLOAT, @sCurrHoMargin FLOAT, @sCurrAgentMargin FLOAT
  149. , @pCurrCostRate FLOAT, @pCurrHoMargin FLOAT, @pCurrAgentMargin FLOAT, @agentCrossSettRate FLOAT
  150. SELECT @ExRateCalculated = customerRate
  151. ,@sCurrCostRate = sCurrCostRate
  152. ,@sCurrHoMargin = sCurrHoMargin
  153. ,@sCurrAgentMargin = sCurrAgentMargin
  154. ,@pCurrCostRate = pCurrCostRate
  155. ,@pCurrHoMargin = pCurrHoMargin
  156. ,@pCurrAgentMargin = pCurrAgentMargin
  157. ,@agentCrossSettRate = agentCrossSettRate
  158. FROM dbo.FNAGetExRate(@ScountryId, @SAgentId, NULL, @SendingCurrencyCode, @pCountryId, @pAgent, @PayoutCurrencyCode, @DeliveryMethodId)
  159. IF ISNULL(@ExRateCalculated, 0) = 0
  160. BEGIN
  161. SET @ErrorMsg = 'Exchange rate not defined yet for receiving currency (' + @PCurrencyCode + ')'
  162. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = @ErrorMsg, @Id = NULL;
  163. RETURN;
  164. END
  165. IF ISNULL(@ExRateCalculated, 0) <> ISNULL(@ExchangeRate, -1)
  166. BEGIN
  167. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid exchange rate passed or exchange rate changed in the system, recalculate the amount details and try again!', @Id = NULL;
  168. RETURN;
  169. END
  170. DECLARE @ServiceChargeCalculated MONEY, @TransferAmountCalculated MONEY, @PayoutAmountCalculated MONEY, @CollectionAmountCalculated MONEY
  171. DECLARE @SSuperAgentName VARCHAR(150), @SAgentName VARCHAR(150), @SBranch INT, @SBranchName VARCHAR(150)
  172. SELECT @SSuperAgentId = sSuperAgent
  173. ,@SSuperAgentName = sSuperAgentName
  174. ,@SAgentName = sAgentName
  175. ,@SBranch = sBranch
  176. ,@SBranchName = sBranchName
  177. FROM dbo.FNAGetBranchFullDetails(@SAgentId)
  178. IF ISNULL(@CalcBy, '') = ''
  179. BEGIN
  180. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Calc by can not be empty!', @Id = NULL;
  181. RETURN;
  182. END
  183. IF @CalcBy NOT IN ('C', 'P')
  184. BEGIN
  185. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Calc by can not be other than ''C'' or ''P''!', @Id = NULL;
  186. RETURN;
  187. END
  188. IF @CalcBy = 'C' AND ISNULL(@CollectionAmount, 0) = 0
  189. BEGIN
  190. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Collection amount can not be 0 or empty for Calc By ''C''!', @Id = NULL;
  191. RETURN;
  192. END
  193. IF @CalcBy = 'P' AND ISNULL(@PayoutAmount, 0) = 0
  194. BEGIN
  195. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Payout amount can not be 0 or empty for Calc By ''P''!', @Id = NULL;
  196. RETURN;
  197. END
  198. IF @CalcBy = 'C'
  199. BEGIN
  200. SELECT @ServiceChargeCalculated = amount
  201. FROM [dbo].FNAGetServiceCharge(@ScountryId, @SSuperAgentId, @SAgentId, NULL, @PCountryId, @PSuperAgent, @PAgent, @PBranch, @DeliveryMethodId, @CollectionAmount, @SendingCurrencyCode)
  202. IF ISNULL(@ServiceChargeCalculated, 0) = 0
  203. BEGIN
  204. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Service charge not defined yet for receiving country', @Id = NULL;
  205. RETURN;
  206. END
  207. SET @TransferAmountCalculated = @CollectionAmount - @ServiceChargeCalculated
  208. SET @PayoutAmountCalculated = @TransferAmountCalculated * @ExRateCalculated
  209. SET @PayoutAmountCalculated = FLOOR(@PayoutAmountCalculated)
  210. END
  211. ELSE
  212. BEGIN
  213. SET @TransferAmountCalculated = CEILING(@PayoutAmount / (@ExRateCalculated))
  214. SELECT @ServiceChargeCalculated = amount
  215. FROM [dbo].FNAGetServiceCharge(@ScountryId, @SSuperAgentId, @SAgentId, NULL, @PCountryId, @PSuperAgent, @PAgent, @PBranch, @DeliveryMethodId, @CollectionAmount, @SendingCurrencyCode)
  216. IF ISNULL(@ServiceChargeCalculated, 0) = 0
  217. BEGIN
  218. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Service charge not defined yet for receiving country', @Id = NULL;
  219. RETURN;
  220. END
  221. SET @CollectionAmountCalculated = @TransferAmountCalculated + @ServiceChargeCalculated
  222. SET @CollectionAmountCalculated = CEILING(@CollectionAmountCalculated)
  223. END
  224. IF @CollectionAmountCalculated <= 0 OR @PayoutAmountCalculated <= 0 OR @TransferAmountCalculated <= 0
  225. BEGIN
  226. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid payout/transfer amount/collect amount found!', @Id = NULL;
  227. RETURN;
  228. END
  229. IF @CalcBy = 'C'
  230. BEGIN
  231. IF ISNULL(@PayoutAmount, 0) <> ISNULL(@PayoutAmountCalculated, -1)
  232. BEGIN
  233. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid Payout Amount passed/Amount details changed, recalculate the amount details and try again!', @Id = NULL;
  234. RETURN;
  235. END
  236. IF ISNULL(@TransferAmount, 0) <> ISNULL(@TransferAmountCalculated, -1)
  237. BEGIN
  238. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid Payout Amount passed/Amount details changed, recalculate the amount details and try again!', @Id = NULL;
  239. RETURN;
  240. END
  241. END
  242. ELSE
  243. BEGIN
  244. IF ISNULL(@CollectionAmount, 0) <> ISNULL(@CollectionAmountCalculated, -1)
  245. BEGIN
  246. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid Collection Amount passed/Amount details changed, recalculate the amount details and try again!', @Id = NULL;
  247. RETURN;
  248. END
  249. IF ISNULL(@TransferAmount, 0) <> ISNULL(@TransferAmountCalculated, -1)
  250. BEGIN
  251. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid Payout Amount passed/Amount details changed, recalculate the amount details and try again!', @Id = NULL;
  252. RETURN;
  253. END
  254. END
  255. DECLARE @ErrorCode VARCHAR(10)
  256. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 's-limit'
  257. ,@cAmt = @CollectionAmount
  258. ,@pAmt = @PayoutAmount
  259. ,@sCountryId = @sCountryId
  260. ,@collMode = NULL
  261. ,@deliveryMethod = @DeliveryMethodId
  262. ,@sendingCustType = NULL
  263. ,@pCountryId = @PCountryId
  264. ,@pCurr = @PayoutCurrencyCode
  265. ,@collCurr = @SendingCurrencyCode
  266. ,@pAgent = @PAgent
  267. ,@sAgent = @SAgentId
  268. ,@sBranch = NULL
  269. ,@msg = @ErrorMsg OUT
  270. ,@errorCode = @ErrorCode OUT
  271. IF @ErrorCode <> '0'
  272. BEGIN
  273. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = @ErrorMsg, @Id = NULL;
  274. RETURN;
  275. END
  276. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 'r-limit'
  277. ,@cAmt = @CollectionAmount
  278. ,@pAmt = @PayoutAmount
  279. ,@sCountryId = @sCountryId
  280. ,@collMode = NULL
  281. ,@deliveryMethod = @DeliveryMethodId
  282. ,@sendingCustType = NULL
  283. ,@pCountryId = @PCountryId
  284. ,@pCurr = @PayoutCurrencyCode
  285. ,@collCurr = @SendingCurrencyCode
  286. ,@pAgent = @PAgent
  287. ,@sAgent = @SAgentId
  288. ,@sBranch = NULL
  289. ,@msg = @ErrorMsg OUT
  290. ,@errorCode = @ErrorCode OUT
  291. IF @ErrorCode <> '0'
  292. BEGIN
  293. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = @ErrorMsg, @Id = NULL;
  294. RETURN;
  295. END
  296. DECLARE @PBankId INT, @PBankName VARCHAR(100), @PBankBranchId INT, @PBankBranchName VARCHAR(100)
  297. IF @PayoutTypeCode = 'BD'
  298. BEGIN
  299. IF @PayoutBankCode IS NULL
  300. BEGIN
  301. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Payout Bank can not be null for transaction type Bank Deposit!', @Id = NULL;
  302. RETURN;
  303. END
  304. IF @ReceiverAccountNo IS NULL
  305. BEGIN
  306. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Receiver Account Number can not be null for transaction type Bank Deposit!', @Id = NULL;
  307. RETURN;
  308. END
  309. SELECT @PBankId = BANK_ID, @PBankName = BANK_NAME
  310. FROM API_BANK_LIST (NOLOCK)
  311. WHERE JME_BANK_CODE = @PayoutBankCode
  312. AND API_PARTNER_ID = @PayoutPartner
  313. IF ISNULL(@PBankId, 0) = 0
  314. BEGIN
  315. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid bank details!!', @Id = NULL;
  316. RETURN;
  317. END
  318. SELECT @PBankBranchId = BRANCH_ID, @PBankBranchName = BRANCH_NAME
  319. FROM API_BANK_BRANCH_LIST (NOLOCK)
  320. WHERE BRANCH_ID = @PayoutBankBranchCode
  321. END
  322. --START OFAC Checking
  323. DECLARE @ReceiverFullName VARCHAR(200)
  324. , @SenderFullName VARCHAR(200)
  325. , @OfacReason VARCHAR(MAX)
  326. , @OfacReasonReceiver VARCHAR(MAX)
  327. , @OfacReasonFull VARCHAR(MAX)
  328. , @OfacErrorMessage VARCHAR(200) = ''
  329. SET @ReceiverFullName = @ReceiverFirstName + ISNULL(' ' + @ReceiverMiddleName, '') + @ReceiverLastName
  330. SET @SenderFullName = @SenderFirstName + ISNULL(' ' + @SenderMiddleName, '') + @SenderLastName
  331. EXEC proc_ofacTracker @flag = 't'
  332. ,@name = @SenderFullName
  333. ,@Result = @OfacReason OUTPUT
  334. SET @OfacReasonFull = @OfacReason
  335. IF @SenderFullName = @ReceiverFullName
  336. BEGIN
  337. SET @OfacReasonReceiver = @OfacReason
  338. END
  339. ELSE
  340. BEGIN
  341. EXEC proc_ofacTracker @flag = 't'
  342. ,@name = @ReceiverFullName
  343. ,@Result = @OfacReasonReceiver OUTPUT
  344. SET @OfacReasonFull = @OfacReasonFull + ISNULL(',' + @OfacReasonReceiver, '')
  345. END
  346. IF ISNULL(@OfacReason, '') <> ''
  347. BEGIN
  348. SET @OfacErrorMessage = 'OFAC matched by Sender Name'
  349. END
  350. IF ISNULL(@OfacReasonReceiver, '') <> ''
  351. BEGIN
  352. SET @OfacErrorMessage = 'OFAC matched by Receiver Name'
  353. END
  354. IF ISNULL(@OfacReason, '') <> '' AND ISNULL(@OfacReasonReceiver, '') <> ''
  355. BEGIN
  356. SET @OfacErrorMessage = 'OFAC matched by both Sender Name and Receiver Name'
  357. END
  358. DECLARE @SendingAgentCommission MONEY, @PayoutAgentCommission MONEY
  359. DECLARE @PAgentCommCurrency VARCHAR(5), @sAgentCommCurrency VARCHAR(5)
  360. SELECT @pAgentCommCurrency = DBO.FNAGetPayCommCurrency(@SSuperAgentId, @SAgentId, NULL, @ScountryId, @PSuperAgent, @PBranch, @PCountryId)
  361. SELECT @PayoutAgentCommission = amount
  362. FROM dbo.FNAGetPayComm(@SAgentId, @ScountryId, NULL, NULL, @PCountryId, NULL, @PAgent, @PAgentCommCurrency, @DeliveryMethodId, @CollectionAmount, @PayoutAmount, @ServiceCharge, @TransferAmount, NULL)
  363. DECLARE @SSettlementRate FLOAT, @PSettlementRate FLOAT
  364. SET @SSettlementRate = @SCurrCostRate + @SCurrHoMargin
  365. SET @PSettlementRate = @PCurrCostRate - @PCurrHoMargin
  366. SELECT @SendingAgentCommission = amount, @sAgentCommCurrency = commissionCurrency FROM dbo.FNAGetSendComm(
  367. @ScountryId, @SSuperAgentId, @SAgentId, NULL,
  368. @pCountryId, @pSuperAgent, @pAgent, NULL,
  369. @SendingCurrencyCode, @deliveryMethodId, @CollectionAmount, @PayoutAmount, @ServiceCharge, NULL, NULL,
  370. @SSettlementRate, @PSettlementRate)
  371. DECLARE @ControlNo VARCHAR(20) = '779' + LEFT(CAST(ABS(CHECKSUM(NEWID())) AS VARCHAR(10)) + '0000000000', 9)
  372. IF EXISTS (
  373. SELECT 'X'
  374. FROM controlNoList WITH (NOLOCK)
  375. WHERE controlNo = @ControlNo
  376. )
  377. BEGIN
  378. SET @ControlNo = '779' + LEFT(CAST(ABS(CHECKSUM(NEWID())) AS VARCHAR(10)) + '0000000000', 9)
  379. IF EXISTS (
  380. SELECT 'X'
  381. FROM controlNoList WITH (NOLOCK)
  382. WHERE controlNo = @ControlNo
  383. )
  384. BEGIN
  385. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Technical error occurred. Please try again!', @Id = NULL;
  386. RETURN;
  387. END
  388. END
  389. DECLARE @ControlNoEncrypted VARCHAR(20) = dbo.FNAEncryptString(@ControlNo)
  390. DECLARE @TransactionUniqueId VARCHAR(40) = NEWID()
  391. SELECT @TransactionUniqueId = REPLACE(@TransactionUniqueId, '-', '')
  392. INSERT INTO TBL_INBOUND_TRANSACTION_VERIFY(PartnerTransactionId, TransactionUniqueId, UserName, AgentId, SendingCountryId, PayoutCountryId, PayoutTypeId, PayoutBankCode, PayoutBankBranchCode, PayoutCurrencyCode
  393. , SendingCurrencyCode, CollectionAmount, TransferAmount, PayoutAmount, ServiceCharge, SendingAgentCommission, PayoutAgentCommission, ExchangeRate, RemittancePurpose, SourceOfFund, Relationship
  394. , Occupation, CalcBy, SenderFirstName, SenderMiddleName, SenderLastName, SenderIdType, SenderIdNo, SenderIdIssued, SenderIdValid, SenderMobile, SenderNativeCountryCode, SenderCity, SenderAddress
  395. , SenderEmail, SenderGender, ReceiverFirstName, ReceiverMiddleName, ReceiverLastName, ReceiverIdType, ReceiverIdNo, ReceiverIdIssued, ReceiverIdValid, ReceiverMobile, ReceiverCity, ReceiverAddress
  396. , ReceiverEmail, ReceiverAccountNo, CreatedDate, TransactionStatus, IsOfacHold, OfacMessage)
  397. SELECT @PartnerTransactionId, @TransactionUniqueId, @UserName, @AgentId, @ScountryId, @PCountryId, @DeliveryMethodId, @PayoutBankCode, @PayoutBankBranchCode, @PayoutCurrencyCode
  398. , @SendingCurrencyCode, @CollectionAmount, @TransferAmount, @PayoutAmount, @ServiceCharge, @SendingAgentCommission, @PayoutAgentCommission, @ExchangeRate, @RemittancePurpose, @SourceOfFund, @Relationship
  399. , @Occupation, @CalcBy, @SenderFirstName, @SenderMiddleName, @SenderLastName, @SenderIdType, @SenderIdNo, @SenderIdIssuedDate, @SenderIdValidDate, @SenderMobile, @SenderNativeCountryCode, @SenderCity, @SenderAddress
  400. , @SenderEmail, @SenderGender, @ReceiverFirstName, @ReceiverMiddleName, @ReceiverLastName, @ReceiverIdType, @ReceiverIdNo, @ReceiverIdIssuedDate, @ReceiverIdValidDate, @ReceiverMobile, @ReceiverCity, @ReceiverAddress
  401. , @ReceiverEmail, @ReceiverAccountNo, GETDATE(), 0, CASE WHEN ISNULL(@OfacErrorMessage, '') = '' THEN 1 ELSE 0 END, @OfacErrorMessage
  402. DECLARE @RelationshipName VARCHAR(150), @RemittancePurposeName VARCHAR(150), @SourceOfFundName VARCHAR(150)
  403. SELECT @RelationshipName = detailTitle
  404. FROM staticDataValue (NOLOCK)
  405. WHERE valueId = @Relationship
  406. SELECT @RemittancePurposeName = detailTitle
  407. FROM staticDataValue (NOLOCK)
  408. WHERE valueId = @RemittancePurpose
  409. SELECT @SourceOfFundName = detailTitle
  410. FROM staticDataValue (NOLOCK)
  411. WHERE valueId = @SourceOfFund
  412. INSERT INTO remitTranTemp (controlNo, sCurrCostRate, sCurrHoMargin, sCurrAgentMargin, pCurrCostRate, pCurrHoMargin, pCurrAgentMargin, agentCrossSettRate
  413. , customerRate, serviceCharge, sAgentComm, sAgentCommCurrency, pAgentComm, pAgentCommCurrency, sSuperAgent, sSuperAgentName, sAgent
  414. , sAgentName, sBranch, sBranchName, sCountry, pSuperAgent, pSuperAgentName, pAgent, pAgentName, pBranch, pBranchName
  415. , pCountry, paymentMethod, pBank, pBankName, pBankBranch, pBankBranchName, accountNo, collCurr, tAmt, cAmt
  416. , pAmt, payoutCurr, relWithSender, purposeOfRemit, sourceOfFund, tranStatus, payStatus, createdDate, createdDateLocal, createdBy
  417. , tranType, senderName, receiverName)
  418. SELECT @controlNoEncrypted, @sCurrCostRate, @sCurrHoMargin, @sCurrAgentMargin, @pCurrCostRate, @pCurrHoMargin, @pCurrAgentMargin, @agentCrossSettRate
  419. , @ExRateCalculated, @serviceCharge, @SendingAgentCommission, @sAgentCommCurrency, @PayoutAgentCommission, @PAgentCommCurrency ,@SSuperAgentId, @SSuperAgentName, @SAgentId
  420. , @SAgentName, @SBranch, @SBranchName, @SCountryName, @PSuperAgent, @PSuperAgentName, @PAgent, @PAgentName, @PBranch, @PBranchName
  421. , @PCountryName, @DeliveryMethodName, @PBankId, @PBankName, @PBankBranchId, @PBankBranchName, @ReceiverAccountNo, @SendingCurrencyCode, @TransferAmount, @CollectionAmount
  422. , @PayoutAmount, @PayoutCurrencyCode, @RelationshipName, @RemittancePurposeName, @SourceOfFundName, 'Hold', 'Unpaid', GETDATE(), GETDATE(), @UserName
  423. , 'B', @SenderFullName, @ReceiverFullName
  424. DECLARE @Id BIGINT = @@IDENTITY
  425. INSERT INTO controlNoList (
  426. controlNo
  427. , createdby
  428. )
  429. SELECT @ControlNo
  430. , @UserName
  431. SELECT gender,* FROM tranSenders
  432. DECLARE @SenderNativeCountryName VARCHAR(80), @SenderIdTypeName VARCHAR(150), @OccupationName VARCHAR(150)
  433. SELECT @SenderNativeCountryName = CountryName
  434. FROM COUNTRYMASTER (NOLOCK)
  435. WHERE countryCode = @SenderNativeCountryCode
  436. SELECT @SenderIdTypeName = detailTitle
  437. FROM staticDataValue (NOLOCK)
  438. WHERE valueId = @SenderIdType
  439. SELECT @OccupationName = detailTitle
  440. FROM staticDataValue (NOLOCK)
  441. WHERE valueId = @Occupation
  442. INSERT INTO tranSendersTemp (tranId, customerId, membershipId, firstName, middleName, lastName1, fullName, country, [address], city, email, mobile
  443. , nativeCountry, dob, idType, idNumber, gender, issuedDate, validDate, occupation, ipAddress)
  444. SELECT @Id, 0, NULL, @SenderFirstName, @SenderMiddleName, @SenderLastName, @SenderFullName, @SCountryName, @SenderAddress, @SenderCity, @SenderEmail, @SenderMobile
  445. , @SenderNativeCountryName, @SenderDOB, @SenderIdTypeName, @SenderIdNo, @SenderGender, @SenderIdIssuedDate, @SenderIdValidDate, @OccupationName, @IpAddress
  446. DECLARE @ReceiverIdTypeName VARCHAR(150)
  447. SELECT @ReceiverIdTypeName = detailTitle
  448. FROM staticDataValue (NOLOCK)
  449. WHERE valueId = @ReceiverIdType
  450. INSERT INTO tranReceiversTemp (tranId, customerId, membershipId, firstName, middleName, lastName1, fullName, country, [address], city, email, mobile
  451. , idType, idNumber, issuedDate, validDate, accountNo)
  452. SELECT @id, 0, '', @ReceiverFirstName, @ReceiverMiddleName, @ReceiverLastName, @ReceiverFullName, @PCountryName, @ReceiverAddress, @ReceiverCity, @ReceiverEmail, @ReceiverMobile
  453. , @ReceiverIdType, @ReceiverIdNo, @ReceiverIdIssuedDate, @ReceiverIdValidDate, @ReceiverAccountNo
  454. SELECT * FROM remitTranOfac ORDER BY ROWID DESC
  455. IF (ISNULL(@OfacReasonFull, '') <> '')
  456. BEGIN
  457. CREATE TABLE #temp (sno INT, rmrks NVARCHAR(MAX))
  458. INSERT INTO #temp (sno, rmrks)
  459. EXEC proc_sendPageLoadData @flag = 'ofac'
  460. ,@user = @UserName
  461. ,@blackListIds = @OfacReasonFull
  462. DECLARE @OfacResultJson NVARCHAR(MAX)
  463. SET @OfacResultJson = (SELECT *
  464. FROM #temp
  465. FOR JSON AUTO)
  466. INSERT remitTranOfac (
  467. TranId
  468. ,blackListId
  469. ,reason
  470. ,flag
  471. ,TRACK_BY
  472. ,ofacDetail
  473. )
  474. SELECT @id
  475. ,@OfacReasonFull
  476. ,@OfacErrorMessage
  477. ,dbo.FNAGetOFAC_Flag(@OfacReasonFull)
  478. ,'OFAC'
  479. ,@OfacResultJson
  480. UPDATE remitTranTemp
  481. SET tranStatus = 'OFAC Hold'
  482. WHERE id = @Id
  483. END
  484. SELECT ErrorCode = 100
  485. , ErrorMessage = 'Success'
  486. , IMEControlNo = @ControlNo
  487. , PartnerTransactionId = @PartnerTransactionId
  488. , TranStatus = tranStatus
  489. , ExRate = customerRate
  490. , SendingCommission = sAgentComm
  491. , SendingCommissionCurrency = sAgentCommCurrency
  492. , CollectionAmount = cAmt
  493. , TransferAmount = tAmt
  494. , PayoutAmount = pAmt
  495. FROM remitTranTemp (NOLOCK)
  496. WHERE id = @Id
  497. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Success', @Id = @TransactionUniqueId, @Extra = @PartnerTransactionId, @Extra2 = @OfacErrorMessage;
  498. END
  499. END
  500. END TRY
  501. BEGIN CATCH
  502. IF @@TRANCOUNT>0
  503. ROLLBACK TRANSACTION
  504. SET @ErrorMsg = 'Exception executing SP: ' + ERROR_MESSAGE()
  505. EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = @ErrorMsg, @Id = NULL;
  506. END CATCH