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.

1077 lines
55 KiB

  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_get_exRate_master] Script Date: 3/26/2024 9:29:34 AM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROC [dbo].[proc_get_exRate_master] (
  9. @flag VARCHAR(20)
  10. ,@user VARCHAR(100) = NULL
  11. ,@sCountryId INT = NULL
  12. ,@sAgent INT = NULL
  13. ,@sSuperAgent INT = NULL
  14. ,@sBranch INT = NULL
  15. ,@senderId BIGINT = NULL
  16. ,@collCurr VARCHAR(5) = NULL
  17. ,@pCountryId INT = NULL
  18. ,@pCountry VARCHAR(50) = NULL
  19. ,@pAgent INT = NULL
  20. ,@pCurr VARCHAR(5) = NULL
  21. ,@deliveryMethodId INT = NULL
  22. ,@cAmt MONEY = NULL
  23. ,@pAmt MONEY = NULL
  24. ,@calBy CHAR(1) = NULL
  25. ,@couponCode VARCHAR(30) = NULL
  26. ,@schemeId VARCHAR(10) = NULL
  27. ,@payOutPartner INT = NULL
  28. ,@paymentType VARCHAR(50) = NULL
  29. ,@cardOnline VARCHAR(10) = NULL
  30. ,@tpExRate FLOAT = NULL
  31. ,@isManualSc CHAR(1) = NULL
  32. ,@manualSc MONEY = NULL
  33. ,@ProcessFor VARCHAR(20) = NULL
  34. ,@discountedFee VARCHAR(50) = NULL
  35. ,@isPromoCode CHAR(1) = NULL
  36. )
  37. AS
  38. SET NOCOUNT ON;
  39. SET XACT_ABORT ON;
  40. -----------------------------------------
  41. --Sep 22 -> #109 ->Change the logic to choose highest Rate in case of Nepal
  42. -- #101 , #361 - Mobile Changes for Multi-Lingual
  43. -- #1526 - Post production fixes for Redeem & Earn
  44. -- #1590 - Customer Loyalty
  45. -- #5968 - sc revised
  46. -- #18600 - change in @flag = 'false' for exRate
  47. -- #27928 added validation for debit card payment
  48. --------------------------------------------
  49. BEGIN
  50. DECLARE @scValue MONEY
  51. ,@scAction CHAR(2)
  52. ,@scOffer MONEY
  53. ,@exRateOffer FLOAT
  54. ,@scDiscount MONEY
  55. DECLARE @place INT
  56. ,@currDecimal INT
  57. ,@collMode VARCHAR(30)
  58. ,@sendingCustType VARCHAR(30)
  59. ,@msg VARCHAR(150)
  60. ,@errorCode INT
  61. DECLARE @exRateCalByPartner BIT
  62. ,@pSuperAgent INT
  63. ,@serviceCharge MONEY
  64. ,@serviceChargetmp MONEY
  65. ,@tAmt MONEY
  66. ,@pSuperAgentName VARCHAR(100)
  67. ,@pBranch INT
  68. DECLARE @pAgentName VARCHAR(100)
  69. ,@pBranchName VARCHAR(100)
  70. ,@exRate FLOAT
  71. ,@pCurrHoMargin FLOAT
  72. ,@FOREX_SESSION_ID VARCHAR(40)
  73. DECLARE @tranCount INT = 0
  74. ,@schemeCount INT = 0
  75. ,@schemeAppliedMsg VARCHAR(100) = ''
  76. ,@isFirstTran CHAR(1) = 'N'
  77. ,@createdFrom CHAR(1) = NULL
  78. ,@isEligible CHAR(1) = 'N'
  79. ,@introducer VARCHAR(25) = NULL
  80. --IF @ProcessFor IN ('send')
  81. --BEGIN
  82. IF ISNULL(@user, 'onlinedefault') <> 'onlinedefault' --OR @user is not null
  83. BEGIN
  84. PRINT 'a'
  85. IF @senderId IS NULL OR @senderId=0
  86. BEGIN
  87. SET @senderId = (
  88. SELECT customerId
  89. FROM customermaster(NOLOCK)
  90. WHERE username = @user
  91. )
  92. END
  93. --EXEC PROC_Customer_LoyaltyV2 @flag = 'check-eligible-v2'
  94. -- ,@isEligible = @isEligible OUT
  95. -- ,@referralCode = @introducer
  96. -- ,@tranCount = @trancount OUT
  97. -- ,@schemeCount = @schemeCount OUT
  98. -- ,@customerId = @senderId
  99. -- ,@createdFrom = 'M'
  100. IF NOT EXISTS (
  101. SELECT 1
  102. FROM (
  103. SELECT TOP 1 customerId
  104. FROM TRANSENDERS TS(NOLOCK)
  105. INNER JOIN remittran(NOLOCK) rt ON rt.id = ts.tranId
  106. WHERE customerId = @SenderId
  107. AND rt.tranStatus <> 'CANCEL' --AND rt.tranType='M'
  108. UNION ALL
  109. SELECT TOP 1 customerId
  110. FROM TRANSENDERSTEMP TT(NOLOCK)
  111. INNER JOIN remittrantemp(NOLOCK) rt ON rt.id = tt.tranId
  112. WHERE customerId = @SenderId
  113. AND rt.tranStatus <> 'CANCEL' --AND rt.tranType='M'
  114. ) a
  115. WHERE customerId = @SenderId
  116. )
  117. BEGIN
  118. SET @isFirstTran = 'Y'
  119. END
  120. END
  121. print '@isFirstTran' + '-> ' + @isFirstTran
  122. SELECT @createdFrom = serviceUsedFor
  123. FROM customerMaster(NOLOCK)
  124. WHERE customerId = @senderId
  125. PRINT '@senderId'
  126. PRINT @senderId;
  127. SET @sCountryId = 233
  128. SET @sAgent='394395';
  129. IF ISNULL(@pCountryId, 0) = 0
  130. SELECT @pCountryId = countryId
  131. FROM countryMaster(NOLOCK)
  132. WHERE COUNTRYNAME = @pCountry
  133. IF @flag = 'False'
  134. AND @ProcessFor IN ('dashboard')
  135. BEGIN
  136. SELECT @payoutPartner = AGENTID
  137. ,@exRateCalByPartner = ISNULL(exRateCalByPartner, 0)
  138. FROM TblPartnerwiseCountry(NOLOCK)
  139. WHERE CountryId = @pCountryId
  140. AND IsActive = 1
  141. AND ISNULL(IsMobileEnabled, 0) = 1
  142. AND ISNULL(PaymentMethod, @deliveryMethodId) = @deliveryMethodId
  143. END
  144. ELSE
  145. BEGIN
  146. SELECT @payoutPartner = AGENTID
  147. ,@exRateCalByPartner = ISNULL(exRateCalByPartner, 0)
  148. FROM TblPartnerwiseCountry(NOLOCK)
  149. WHERE CountryId = @pCountryId
  150. AND IsActive = 1
  151. AND ISNULL(PaymentMethod, @deliveryMethodId) = @deliveryMethodId
  152. END
  153. PRINT '@payoutPartner'
  154. PRINT @payoutPartner;
  155. IF @payoutPartner IS NOT NULL
  156. BEGIN
  157. --GET PAYOUT AGENT DETAILS
  158. SELECT @PAGENT = AGENTID
  159. FROM AGENTMASTER(NOLOCK)
  160. WHERE PARENTID = @payoutPartner
  161. AND ISNULL(ISSETTLINGAGENT, 'N') = 'Y';
  162. SELECT @pSuperAgent = sSuperAgent
  163. ,@pSuperAgentName = sSuperAgentName
  164. ,@pAgent = sAgent
  165. ,@pAgentName = sAgentName
  166. ,@pBranch = sBranch
  167. ,@pBranchName = sBranchName
  168. FROM dbo.FNAGetBranchFullDetails(@PAGENT)
  169. END
  170. ELSE
  171. BEGIN
  172. SELECT '1' ErrorCode
  173. ,'Partner not yet mapped for the selected country!' Msg
  174. ,NULL id
  175. RETURN
  176. END
  177. DECLARE @rowId INT
  178. SELECT @scValue = 0
  179. ,@scOffer = 0
  180. ,@exRateOffer = 0
  181. ,@scDiscount = 0
  182. SELECT @place = place
  183. ,@currDecimal = currDecimal
  184. FROM currencyPayoutRound WITH (NOLOCK)
  185. WHERE ISNULL(isDeleted, 'N') = 'N'
  186. AND currency = @pCurr
  187. AND ISNULL(tranType, @deliveryMethodId) = @deliveryMethodId
  188. SET @currDecimal = ISNULL(@currDecimal, 0)
  189. IF @pCurr IS NULL
  190. BEGIN
  191. SELECT '2' ErrorCode
  192. ,'Currency not been defined yet for receiving country' Msg
  193. ,NULL id
  194. RETURN
  195. END
  196. --pRINT @pAgent;
  197. IF @flag = 'false'
  198. BEGIN
  199. IF @pCountryId = 151
  200. AND @deliveryMethodId = 1
  201. BEGIN
  202. SELECT AgentId
  203. INTO #AgentList
  204. FROM agentMaster(NOLOCK)
  205. WHERE extCode = 'NP ANYWHERE'
  206. SELECT @exRate = customerRate
  207. ,@pCurrHoMargin = pCurrHoMargin
  208. FROM (
  209. SELECT ROW_NUMBER() OVER (
  210. ORDER BY CUSTOMERRATE DESC
  211. ) ROW_NUM
  212. ,CUSTOMERRATE
  213. ,pCurrHoMargin
  214. FROM #AgentList A
  215. CROSS APPLY dbo.FNAGetExRate(@sCountryId, @sAgent, @sBranch, @collCurr, @pCountryId, AgentId, @pCurr, @deliveryMethodId) FN
  216. ) X
  217. WHERE X.ROW_NUM = 1
  218. END
  219. ELSE
  220. BEGIN
  221. PRINT @sCountryId;
  222. PRINT '@sAgent';
  223. PRINT @sAgent;
  224. PRINT '@sBranch';
  225. PRINT @sBranch;
  226. PRINT @collCurr;
  227. PRINT @pCountryId;
  228. PRINT @pCurr;
  229. PRINT @deliveryMethodId;
  230. pRINT '@pAgent';
  231. PRINT @pAgent;
  232. SELECT @exRate = customerRate
  233. ,@pCurrHoMargin = pCurrHoMargin
  234. FROM dbo.FNAGetExRate(@sCountryId, @sAgent, @sBranch, @collCurr, @pCountryId, @pAgent, @pCurr, @deliveryMethodId)
  235. END
  236. IF ISNULL(@exRate, 0) = 0
  237. BEGIN
  238. SELECT '3' ErrorCode
  239. ,'Exchange rate not defined yet for receiving currency (' + @pCurr + ')' Msg
  240. ,NULL id
  241. RETURN
  242. END
  243. IF @tpExRate > 0 and @isFirstTran='N'
  244. BEGIN
  245. SET @exRate = @exRate - @tpExRate
  246. END
  247. ELSE
  248. BEGIN
  249. DECLARE @rate FLOAT, @promRate FLOAT
  250. SELECT @rate = ISNULL(margin,0), @promRate = ISNULL(purchaseRate,0) FROM countryWiseExchangeRate WHERE depositType='ONLINE' and countryId = @pCountryId
  251. PRINT' @rate'
  252. PRINT @rate
  253. PRINT '@promRate'
  254. PRINT @promRate
  255. IF @isFirstTran='Y'
  256. SET @exRate = @exRate - (ISNULL(@rate,0) +ISNULL(@promRate,0))
  257. ELSE
  258. SET @exRate = @exRate - ISNULL(@rate,0)
  259. END
  260. --IF(ISNULL(@discountedFee,0)> 0)
  261. --BEGIN
  262. -- IF dbo.FNA_GET_AVAILABLE_BALANCE_POINTS(@senderId) < ISNULL(@discountedFee, 0)
  263. -- BEGIN
  264. -- SELECT '1' ErrorCode
  265. -- ,'You do not have sufficient Reward Amount!' Msg
  266. -- ,NULL id
  267. -- RETURN
  268. -- END
  269. --END
  270. DECLARE @ValidationResult TABLE (
  271. IS_VALID VARCHAR(50),
  272. ROW_ID INT,
  273. PROMOTIONAL_CODE VARCHAR(50),
  274. PROMOTIONAL_MSG VARCHAR(255),
  275. -- PROMOTION_TYPE INT,
  276. PROMOTION_VALUE MONEY,
  277. MIN_SENDING_AMT MONEY,
  278. START_DT DATETIME,
  279. END_DT DATETIME
  280. )
  281. DECLARE @availableRewardAmt INT, @row_id INT, @promoValue MONEY, @minSendAmt MONEY, @startDate DATETIME, @endDate DATETIME, @errMsg VARCHAR(100), @promoCode VARCHAR(100)
  282. SELECT @availableRewardAmt = CAST(dbo.[FNA_GET_AVAILABLE_BALANCE_POINTS](@senderId) AS INT)
  283. DECLARE @minCollAmt MONEY, @maxRewardAmt MONEY
  284. SELECT @maxRewardAmt = CAST(detailTitle AS MONEY) FROM staticDataValue WHERE typeID = '10200' AND detailDesc = 'MAX_REWARD_AMT'
  285. SELECT @minCollAmt = CAST(detailTitle AS MONEY) FROM staticDataValue WHERE typeID = '10200' AND detailDesc = 'MIN_COLL_AMT_FOR_REWARD'
  286. IF ISNUMERIC(ISNULL(@discountedFee,0)) = 1
  287. BEGIN
  288. SET @isPromoCode = 'N'
  289. IF(ISNULL(@discountedFee, 0) > 0)
  290. BEGIN
  291. IF (@availableRewardAmt < ISNULL(@discountedFee, 0))
  292. BEGIN
  293. SELECT '1' ErrorCode
  294. ,'You do not have sufficient Rewards!'
  295. ,NULL id
  296. RETURN;
  297. END
  298. END
  299. IF(ISNULL(@discountedFee, 0) > ISNULL(@availableRewardAmt,0))
  300. BEGIN
  301. SELECT '1' ErrorCode
  302. ,'Please enter the valid point. The amount cannot be more than available Reward Amount!' Msg
  303. ,NULL id
  304. RETURN
  305. END
  306. DECLARE @discountedFeeMoney MONEY = CAST(@discountedFee AS MONEY)
  307. IF (@discountedFeeMoney > '0.00' AND @cAmt < @minCollAmt AND @discountedFeeMoney > @maxRewardAmt)
  308. BEGIN
  309. SELECT '8' ErrorCode
  310. ,'The maximum reward amount per transaction is ' + CAST(@maxRewardAmt AS VARCHAR(20)) + ' Pounds and the minimum amount that can be sent for each transaction is ' + CAST(@minCollAmt AS VARCHAR(20)) + ' Pounds' Msg
  311. ,NULL id
  312. RETURN;
  313. END
  314. IF (@discountedFeeMoney > '0.00' AND @discountedFeeMoney > @maxRewardAmt)
  315. BEGIN
  316. SELECT '8' ErrorCode
  317. ,'The maximum reward amount per transaction is ' + CAST(@maxRewardAmt AS VARCHAR(20)) + ' Pounds' Msg
  318. ,NULL id
  319. RETURN;
  320. END
  321. IF (@discountedFeeMoney > '0.00' AND @cAmt < @minCollAmt)
  322. BEGIN
  323. SELECT '8' ErrorCode
  324. ,'The minimum amount that can be sent for each transaction is ' + CAST(@minCollAmt AS VARCHAR(20)) + ' Pounds' Msg
  325. ,NULL id
  326. RETURN;
  327. END
  328. END
  329. ELSE
  330. BEGIN
  331. SET @isPromoCode = 'Y'
  332. print 'promo'
  333. PRINT @pCountryId
  334. INSERT INTO @ValidationResult (IS_VALID, ROW_ID, PROMOTIONAL_CODE, PROMOTIONAL_MSG, PROMOTION_VALUE, MIN_SENDING_AMT, START_DT, END_DT)
  335. EXEC proc_CheckPromoValidity @flag='chk-validity', @countryId = @pCountryId, @pmode = @deliveryMethodId, @promoCode = @discountedFee
  336. --select * FROM @ValidationResult;
  337. --RETURN;
  338. IF EXISTS (SELECT 'X' FROM @ValidationResult WHERE IS_VALID = 'VALID')
  339. BEGIN
  340. SELECT @row_id = ROW_ID, @promoValue = PROMOTION_VALUE, @minSendAmt = MIN_SENDING_AMT, @startDate = START_DT, @endDate = END_DT FROM @ValidationResult WHERE IS_VALID = 'VALID'
  341. SET @promoCode = @discountedFee
  342. SET @discountedFee = @promoValue
  343. IF (@cAmt < @minSendAmt)
  344. BEGIN
  345. SELECT '8' ErrorCode
  346. ,'The minimum amount per transaction should be ' + CAST(@minSendAmt AS VARCHAR(20)) + ' Pounds to use Promo Code' Msg
  347. ,NULL id
  348. RETURN;
  349. END
  350. END
  351. ELSE
  352. BEGIN
  353. SELECT @errMsg = PROMOTIONAL_MSG FROM @ValidationResult WHERE IS_VALID = 'INVALID'
  354. SELECT '1' ErrorCode
  355. ,@errMsg Msg
  356. RETURN
  357. END
  358. END
  359. --PRINT '@isFirstTran:' + CAST( @isFirstTran AS VARCHAR);
  360. --PRINT '@isEligible:' + CAST( @isEligible AS VARCHAR);
  361. -- PRINT '@tranCount:' + CAST( @tranCount AS VARCHAR);
  362. -- PRINT '@@schemeCount:' + CAST( @schemeCount AS VARCHAR);
  363. IF @calBy = 'C'
  364. BEGIN
  365. BEGIN
  366. IF ISNULL(@isManualSc, 'N') = 'N'
  367. BEGIN
  368. PRINT '@serviceChargetmp';
  369. SELECT @serviceChargetmp = isnull(amount, 0)
  370. FROM [dbo].FNAGetServiceCharge(@sCountryId, @sSuperAgent, @sAgent, @sBranch, @pCountryId, @pSuperAgent, @pAgent, @pBranch, @deliveryMethodId, @cAmt, @collCurr)
  371. PRINT @serviceChargetmp
  372. IF (@discountedFee < '0.00')
  373. --AND (@serviceChargetmp < @discountedFee)
  374. BEGIN
  375. SELECT '8' ErrorCode
  376. ,'Redeem Amount cannot be less than zero' Msg
  377. ,NULL id
  378. RETURN;
  379. END
  380. ELSE
  381. BEGIN
  382. --SELECT @serviceCharge = @serviceChargetmp - ISNULL(@discountedFee, 0)
  383. SELECT @serviceCharge = @serviceChargetmp
  384. END
  385. END
  386. ELSE
  387. BEGIN
  388. SET @serviceCharge = ISNULL(@manualSc, 0)
  389. END
  390. IF @serviceCharge IS NULL
  391. AND ISNULL(@isManualSc, 'N') = 'N'
  392. BEGIN
  393. SELECT '4' ErrorCode
  394. ,'Service charge not defined yet for receiving country' Msg
  395. ,NULL id
  396. RETURN;
  397. END
  398. END
  399. IF @scAction = 'PD' -- Percent Discount
  400. BEGIN
  401. SET @scOffer = (@scValue / 100) * @serviceCharge
  402. SET @scDiscount = (@scValue / 100) * @serviceCharge
  403. END
  404. ELSE IF @scAction = 'FD' -- Flat Discount
  405. BEGIN
  406. SET @scDiscount = @scValue
  407. END
  408. ELSE IF @scAction = 'FV' -- Fixed Value
  409. BEGIN
  410. SET @scOffer = @scValue
  411. SET @scDiscount = @serviceCharge - @scValue
  412. END
  413. --IF (@discountedFee > @serviceCharge)
  414. -- BEGIN
  415. -- SELECT '8' ErrorCode
  416. -- ,'Redeem Amount cannot be higher than Transfer fee.' Msg
  417. -- ,NULL id
  418. -- RETURN;
  419. -- END
  420. SET @tAmt = @cAmt - @serviceCharge + @scDiscount + ISNULL(@discountedFee, 0)
  421. SET @pAmt = @tAmt * (@exRate + @exRateOffer)
  422. --PRINT @pAmt;
  423. --SET @pAmt = FLOOR(@pAmt)
  424. SET @tAmt=Cast(Round(@tAmt,2,0) as decimal(18,2));
  425. SET @pAmt=Cast(Round(@pAmt,2,0) as decimal(18,2));
  426. --PRINT @pAmt;
  427. END
  428. ELSE
  429. BEGIN
  430. --SET @tAmt = CEILING(@pAmt/(@exRate + @exRateOffer), 0)
  431. --SET @tAmt = CEILING(@pAmt / (@exRate + @exRateOffer))
  432. SET @tAmt = @pAmt / (@exRate + @exRateOffer);
  433. BEGIN
  434. IF ISNULL(@isManualSc, 'N') = 'N'
  435. BEGIN
  436. SELECT @serviceChargetmp = isnull(amount, 0)
  437. FROM [dbo].FNAGetServiceCharge(@sCountryId, @sSuperAgent, @sAgent, @sBranch, @pCountryId, @pSuperAgent, @pAgent, @pBranch, @deliveryMethodId, @tAmt, @collCurr)
  438. IF (@discountedFee < '0.00')
  439. --AND (@serviceChargetmp < @discountedFee)
  440. BEGIN
  441. SELECT '8' ErrorCode
  442. ,'Redeem Amount cannot be less than zero' Msg
  443. ,NULL id
  444. RETURN;
  445. END
  446. ELSE
  447. BEGIN
  448. SELECT @serviceCharge = @serviceChargetmp;
  449. END
  450. END
  451. ELSE
  452. BEGIN
  453. SET @serviceCharge = ISNULL(@manualSc, 0)
  454. END
  455. IF @serviceCharge IS NULL
  456. BEGIN
  457. SELECT '4' ErrorCode
  458. ,'Service charge not defined yet for receiving country' Msg
  459. ,NULL id
  460. RETURN;
  461. END
  462. END
  463. IF @scAction = 'PD'
  464. BEGIN
  465. SET @scOffer = (@scValue / 100) * @serviceCharge
  466. SET @scDiscount = (@scValue / 100) * @serviceCharge
  467. END
  468. ELSE IF @scAction = 'FD'
  469. BEGIN
  470. SET @scDiscount = @scValue
  471. END
  472. ELSE IF @scAction = 'FV'
  473. BEGIN
  474. SET @scOffer = @scValue
  475. SET @scDiscount = @serviceCharge - @scValue
  476. END
  477. PRINT '@discountedFee'
  478. PRINT @discountedFee
  479. PRINT @serviceCharge
  480. --IF (@discountedFee > @serviceCharge)
  481. -- BEGIN
  482. -- SELECT '8' ErrorCode
  483. -- ,'Redeem Amount cannot be higher than Transfer fee.' Msg
  484. -- ,NULL id
  485. -- RETURN;
  486. -- END
  487. SET @cAmt = (@tAmt + @serviceCharge - @scDiscount) - ISNULL(@discountedFee, 0)
  488. SET @tAmt =Cast(Round(@tAmt,2,0) as decimal(18,2));
  489. SET @cAmt =Cast(Round(@cAmt,2,0) as decimal(18,2))
  490. --SET @cAmt = CEILING(@cAmt)
  491. END
  492. --4. Validate Country Sending Limit
  493. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 's-limit'
  494. ,@cAmt = @cAmt
  495. ,@pAmt = @pAmt
  496. ,@sCountryId = @sCountryId
  497. ,@collMode = @collMode
  498. ,@deliveryMethod = @deliveryMethodId
  499. ,@sendingCustType = @sendingCustType
  500. ,@pCountryId = @pCountryId
  501. ,@pCurr = @pCurr
  502. ,@collCurr = @collCurr
  503. ,@pAgent = @pAgent
  504. ,@sAgent = @sAgent
  505. ,@sBranch = @sBranch
  506. ,@msg = @msg OUT
  507. ,@errorCode = @errorCode OUT
  508. IF @errorCode <> '0'
  509. BEGIN
  510. SELECT @errorCode ErrorCode
  511. ,@msg Msg
  512. RETURN;
  513. END
  514. --Validate Country Sending Limit END
  515. --5. Validate Country Receiving Limit
  516. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 'r-limit'
  517. ,@cAmt = @cAmt
  518. ,@pAmt = @pAmt
  519. ,@sCountryId = @sCountryId
  520. ,@collMode = @collMode
  521. ,@deliveryMethod = @deliveryMethodId
  522. ,@sendingCustType = @sendingCustType
  523. ,@pCountryId = @pCountryId
  524. ,@pCurr = @pCurr
  525. ,@collCurr = @collCurr
  526. ,@pAgent = @pAgent
  527. ,@sAgent = @sAgent
  528. ,@sBranch = @sBranch
  529. ,@msg = @msg OUT
  530. ,@errorCode = @errorCode OUT
  531. IF @errorCode <> '0'
  532. BEGIN
  533. SELECT @errorCode ErrorCode
  534. ,@msg Msg
  535. RETURN;
  536. END
  537. DECLARE @maxAmt MONEY
  538. SELECT @maxAmt = maxAmt FROM countryWiseExchangeRate WHERE depositType = 'DEBIT_CARD' AND countryId = @pCountryId
  539. IF (@schemeId = 'DEBIT_CARD' AND @cAmt > @maxAmt)
  540. BEGIN
  541. SELECT '3' ErrorCode
  542. ,'You cannot send more than '+ CAST(@maxAmt AS VARCHAR)+' GBP via Debit Card. Please select Online Banking to complete the transaction or we will automatically redirect to Bank Transfer.' Msg
  543. ,NULL id
  544. RETURN
  545. END
  546. IF ISNULL(@ProcessFor, '') = 'send'
  547. BEGIN
  548. SET @FOREX_SESSION_ID = NEWID()
  549. ----## lock ex rate for individual txn
  550. UPDATE exRateCalcHistory
  551. SET isExpired = 1
  552. WHERE CUSTOMER_ID = @senderId
  553. AND isExpired = 0
  554. PRINT @serviceCharge;
  555. INSERT INTO exRateCalcHistory (
  556. CUSTOMER_ID
  557. ,[USER_ID]
  558. ,FOREX_SESSION_ID
  559. ,serviceCharge
  560. ,pAmt
  561. ,customerRate
  562. ,sCurrCostRate
  563. ,sCurrHoMargin
  564. ,sCurrAgentMargin
  565. ,pCurrCostRate
  566. ,pCurrHoMargin
  567. ,pCurrAgentMargin
  568. ,agentCrossSettRate
  569. ,createdDate
  570. ,isExpired
  571. ,tAmt
  572. ,schemeId
  573. ,discountedFee
  574. ,sharingValue
  575. ,customerPremium
  576. ,promoRowId
  577. )
  578. SELECT @senderId
  579. ,@user
  580. ,@FOREX_SESSION_ID
  581. ,@serviceCharge
  582. ,@pAmt
  583. ,@exRate
  584. ,1
  585. ,0
  586. ,0
  587. ,@pCurrHoMargin + @exRate
  588. ,@pCurrHoMargin
  589. ,0
  590. ,@exRate
  591. ,GETDATE()
  592. ,0
  593. ,@tAmt
  594. ,@schemeId
  595. ,ISNULL(@discountedFee, 0)
  596. ,@schemeCount
  597. ,@tpExRate
  598. ,@row_id
  599. END
  600. SET @msg = 'Success'
  601. PRINT @promRate
  602. PRINT @isFirstTran
  603. PRINT '@promRate1'
  604. if(@promRate>0 AND @isFirstTran='Y')
  605. SET @schemeAppliedMsg = 'Enjoy premium rate due to First Transaction!';
  606. SELECT @errorCode ErrorCode
  607. ,@msg Msg
  608. ,scCharge = @serviceCharge
  609. ,exRate = @exRate
  610. ,place = @place
  611. ,pCurr = @pCurr
  612. ,currDecimal = @currDecimal
  613. ,pAmt = @pAmt
  614. ,sAmt = @tAmt
  615. ,place = @place
  616. ,disc = 0.00
  617. ,collAmt = @cAmt
  618. ,exRateOffer = @exRateOffer
  619. ,scOffer = @scDiscount
  620. ,scAction = @scAction
  621. ,scValue = @scValue
  622. ,scDiscount = @scDiscount
  623. ,tpExRate = 0
  624. ,amountLimitPerDay = 0
  625. ,amountLimitPerTran = 0
  626. ,customerTotalSentAmt = 0
  627. ,exRateDisplay = @exRate
  628. ,EXRATEID = @FOREX_SESSION_ID
  629. ,maxAmountLimitPerTran = 0
  630. ,minAmountLimitPerTran = 0
  631. ,PerTxnMinimumAmt = 0
  632. ,schemeAppliedMsg = @schemeAppliedMsg
  633. ,schemeId = @schemeId
  634. ,tpPCurr = @pCurr
  635. ,collCurr = @collCurr
  636. ,ForexSessionId = @FOREX_SESSION_ID
  637. --,discountedFee = CASE WHEN @isPromoCode = 'N' THEN ISNULL(@discountedFee, 0)
  638. -- WHEN @isPromoCode = 'Y' THEN @promoCode
  639. -- ELSE ISNULL(@discountedFee, 0)
  640. -- END
  641. ,discountedFee=ISNULL(@discountedFee, 0)
  642. ,isPromoCode = @isPromoCode
  643. ,promoRowId = @row_id
  644. ,promoCode = @promoCode
  645. END
  646. ELSE IF @FLAG = 'true'
  647. BEGIN
  648. IF ISNULL(@tpExRate, 0) = 0
  649. BEGIN
  650. SELECT '5' ErrorCode
  651. ,'Third Party Exchange rate fetching error for currency (' + @pCurr + ')' Msg
  652. RETURN
  653. END
  654. DECLARE @exRateDonga FLOAT, @exRateBRAC FLOAT
  655. PRINT @sCountryId;
  656. PRINT @sCountryId;
  657. PRINT @sAgent;
  658. PRINT @sBranch;
  659. PRINT @collCurr;
  660. PRINT @pCountryId;
  661. PRINT @pAgent;
  662. PRINT @pCurr;
  663. PRINT @deliveryMethodId;
  664. SELECT @pCurrHoMargin = pCurrHoMargin
  665. FROM dbo.FNAGetExRate(@sCountryId, @sAgent, @sBranch, @collCurr, @pCountryId, @pAgent, @pCurr, @deliveryMethodId)
  666. SELECT @exRate = @tpExRate - ISNULL(@pCurrHoMargin, 0)
  667. IF ISNULL(@exRate, 0) = 0
  668. BEGIN
  669. SELECT '3' ErrorCode
  670. ,'Exchange rate not defined yet for receiving currency (' + @pCurr + ')' Msg
  671. ,NULL id
  672. RETURN
  673. END
  674. IF @user = 'onlinedefault'
  675. AND @pCountryId = 203
  676. BEGIN
  677. SELECT @exRateDonga = dbo.FNAGetCustomerRate(113, 0, 394395, 'JPY', 203, 394133, 'VND', 1)
  678. IF @exRateDonga > @exRate
  679. SET @exRate = @exRateDonga
  680. END
  681. IF @user = 'onlinedefault'
  682. AND @pCountryId = 16
  683. BEGIN
  684. SELECT @exRateBRAC = dbo.FNAGetCustomerRate(113, 0, 394395, 'JPY', 16, 394414, 'BDT', 1)
  685. IF @exRateBRAC > @exRate
  686. SET @exRate = @exRateBRAC
  687. END
  688. IF @calBy = 'C'
  689. BEGIN
  690. IF (
  691. @tranCount = @schemeCount
  692. AND ISNULL(@user, 'onlinedefault') <> 'onlinedefault'
  693. -- AND ISNULL(@ProcessFor, '') = 'send'
  694. AND @isEligible = 'Y'
  695. )
  696. OR (@isFirstTran = 'Y')
  697. BEGIN
  698. SET @serviceCharge = 0
  699. IF @isFirstTran = 'Y'
  700. BEGIN
  701. SET @schemeAppliedMsg = 'Enjoy zero (0) service charge due to First Transaction!';
  702. END
  703. ELSE
  704. SET @schemeAppliedMsg = 'Enjoy zero (0) service charge due to Loyalty Scheme!';
  705. END
  706. ELSE
  707. BEGIN
  708. IF ISNULL(@isManualSc, 'N') = 'N'
  709. BEGIN
  710. SELECT @serviceChargetmp = ISNULL(amount, 0)
  711. FROM [dbo].FNAGetServiceCharge(@sCountryId, @sSuperAgent, @sAgent, @sBranch, @pCountryId, @pSuperAgent, @pAgent, @pBranch, @deliveryMethodId, @cAmt, @collCurr)
  712. --IF (@discountedFee > '0.00')
  713. -- AND (@serviceChargetmp < @discountedFee)
  714. --BEGIN
  715. -- SELECT '8' ErrorCode
  716. -- ,'Redeem points can not be more than service charge' Msg
  717. -- ,NULL id
  718. -- RETURN;
  719. --END
  720. --ELSE
  721. --BEGIN
  722. -- SELECT @serviceCharge = ISNULL(@serviceChargetmp, 0) - ISNULL(@discountedFee, 0)
  723. --END
  724. END
  725. ELSE
  726. BEGIN
  727. SET @serviceCharge = ISNULL(@manualSc, 0)
  728. END
  729. IF @serviceCharge IS NULL
  730. AND ISNULL(@isManualSc, 'N') = 'N'
  731. BEGIN
  732. SELECT '4' ErrorCode
  733. ,'Service charge not defined yet for receiving country' Msg
  734. ,NULL id
  735. RETURN;
  736. END
  737. END
  738. IF @scAction = 'PD' -- Percent Discount
  739. BEGIN
  740. SET @scOffer = (@scValue / 100) * @serviceCharge
  741. SET @scDiscount = (@scValue / 100) * @serviceCharge
  742. END
  743. ELSE IF @scAction = 'FD' -- Flat Discount
  744. BEGIN
  745. SET @scDiscount = @scValue
  746. END
  747. ELSE IF @scAction = 'FV' -- Fixed Value
  748. BEGIN
  749. SET @scOffer = @scValue
  750. SET @scDiscount = @serviceCharge - @scValue
  751. END
  752. SET @tAmt = @cAmt - @serviceCharge + @scDiscount + ISNULL(@discountedFee, 0)
  753. SET @pAmt = @tAmt * (@exRate + @exRateOffer)
  754. --SET @pAmt = FLOOR(@pAmt)
  755. END
  756. ELSE
  757. BEGIN
  758. --SET @tAmt = CEILING(@pAmt / (@exRate + @exRateOffer))
  759. SET @tAmt = @pAmt / (@exRate + @exRateOffer)
  760. IF ISNULL(@isManualSc, 'N') = 'N'
  761. BEGIN
  762. SELECT @serviceChargetmp = isnull(amount, 0)
  763. FROM [dbo].FNAGetServiceCharge(@sCountryId, @sSuperAgent, @sAgent, @sBranch, @pCountryId, @pSuperAgent, @pAgent, @pBranch, @deliveryMethodId, @tAmt, @collCurr)
  764. --IF (@discountedFee > '0.00')
  765. -- AND (@serviceCharge < @discountedFee)
  766. --BEGIN
  767. -- SELECT '8' ErrorCode
  768. -- ,'Redeem points can not be more than service charge' Msg
  769. -- ,NULL id
  770. -- RETURN;
  771. --END
  772. --ELSE
  773. --BEGIN
  774. -- SELECT @serviceCharge = ISNULL(@serviceChargetmp, 0) - ISNULL(@discountedFee, 0)
  775. --END
  776. END
  777. ELSE
  778. BEGIN
  779. SET @serviceCharge = ISNULL(@manualSc, 0)
  780. END
  781. IF @serviceCharge IS NULL
  782. BEGIN
  783. SELECT '4' ErrorCode
  784. ,'Service charge not defined yet for receiving country' Msg
  785. ,NULL id
  786. RETURN;
  787. END
  788. IF @scAction = 'PD'
  789. BEGIN
  790. SET @scOffer = (@scValue / 100) * @serviceCharge
  791. SET @scDiscount = (@scValue / 100) * @serviceCharge
  792. END
  793. ELSE IF @scAction = 'FD'
  794. BEGIN
  795. SET @scDiscount = @scValue
  796. END
  797. ELSE IF @scAction = 'FV'
  798. BEGIN
  799. SET @scOffer = @scValue
  800. SET @scDiscount = @serviceCharge - @scValue
  801. END
  802. SET @cAmt = (@tAmt + @serviceCharge - @scDiscount)+ ISNULL(@discountedFee, 0)
  803. --SET @cAmt = ROUND(@cAmt, @currDecimal)
  804. --New logic for calculating new tAmt and pAmt after adding discount fee
  805. SET @tAmt = @tAmt + ISNULL(@discountedFee, 0)
  806. SET @pAmt = @tAmt * (@exRate + @exRateOffer)
  807. --SET @pAmt = FLOOR(@pAmt)
  808. END
  809. --4. Validate Country Sending Limit
  810. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 's-limit'
  811. ,@cAmt = @cAmt
  812. ,@pAmt = @pAmt
  813. ,@sCountryId = @sCountryId
  814. ,@collMode = @collMode
  815. ,@deliveryMethod = @deliveryMethodId
  816. ,@sendingCustType = @sendingCustType
  817. ,@pCountryId = @pCountryId
  818. ,@pCurr = @pCurr
  819. ,@collCurr = @collCurr
  820. ,@pAgent = @pAgent
  821. ,@sAgent = @sAgent
  822. ,@sBranch = @sBranch
  823. ,@msg = @msg OUT
  824. ,@errorCode = @errorCode OUT
  825. IF @errorCode <> '0'
  826. BEGIN
  827. SELECT @errorCode ErrorCode
  828. ,@msg Msg
  829. RETURN;
  830. END
  831. --Validate Country Sending Limit END
  832. --5. Validate Country Receiving Limit
  833. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 'r-limit'
  834. ,@cAmt = @cAmt
  835. ,@pAmt = @pAmt
  836. ,@sCountryId = @sCountryId
  837. ,@collMode = @collMode
  838. ,@deliveryMethod = @deliveryMethodId
  839. ,@sendingCustType = @sendingCustType
  840. ,@pCountryId = @pCountryId
  841. ,@pCurr = @pCurr
  842. ,@collCurr = @collCurr
  843. ,@pAgent = @pAgent
  844. ,@sAgent = @sAgent
  845. ,@sBranch = @sBranch
  846. ,@msg = @msg OUT
  847. ,@errorCode = @errorCode OUT
  848. IF @errorCode <> '0'
  849. BEGIN
  850. SELECT @errorCode ErrorCode
  851. ,@msg Msg
  852. RETURN;
  853. END
  854. --Validate Country Receiving Limit
  855. IF ISNULL(@ProcessFor, '') = 'send'
  856. BEGIN
  857. SET @FOREX_SESSION_ID = NEWID()
  858. ----## lock ex rate for individual txn
  859. UPDATE exRateCalcHistory
  860. SET isExpired = 1
  861. WHERE CUSTOMER_ID = @senderId
  862. AND isExpired = 0
  863. INSERT INTO exRateCalcHistory (
  864. CUSTOMER_ID
  865. ,[USER_ID]
  866. ,FOREX_SESSION_ID
  867. ,serviceCharge
  868. ,pAmt
  869. ,customerRate
  870. ,sCurrCostRate
  871. ,sCurrHoMargin
  872. ,sCurrAgentMargin
  873. ,pCurrCostRate
  874. ,pCurrHoMargin
  875. ,pCurrAgentMargin
  876. ,agentCrossSettRate
  877. ,createdDate
  878. ,isExpired
  879. ,tAmt
  880. ,schemeId
  881. ,discountedFee
  882. ,sharingValue
  883. )
  884. SELECT @senderId
  885. ,@user
  886. ,@FOREX_SESSION_ID
  887. ,@serviceCharge
  888. ,@pAmt
  889. ,@exRate
  890. ,1
  891. ,0
  892. ,0
  893. ,@pCurrHoMargin + @exRate
  894. ,@pCurrHoMargin
  895. ,0
  896. ,@exRate
  897. ,GETDATE()
  898. ,0
  899. ,@tAmt
  900. ,@schemeId
  901. ,ISNULL(@discountedFee, 0)
  902. ,@schemeCount
  903. END
  904. SET @msg = 'Success'
  905. PRINT @promRate
  906. PRINT @isFirstTran
  907. PRINT '@promRate1'
  908. if(@promRate>0 AND @isFirstTran='Y')
  909. SET @schemeAppliedMsg = 'Enjoy premium rate due to First Transaction!';
  910. SELECT @errorCode ErrorCode
  911. ,@msg Msg
  912. ,scCharge = @serviceCharge
  913. ,exRate = @exRate
  914. ,place = @place
  915. ,pCurr = @pCurr
  916. ,currDecimal = @currDecimal
  917. ,pAmt = @pAmt
  918. ,sAmt = @tAmt
  919. ,place = @place
  920. ,disc = 0.00
  921. ,collAmt = @cAmt
  922. ,exRateOffer = @exRateOffer
  923. ,scOffer = @scDiscount
  924. ,scAction = @scAction
  925. ,scValue = @scValue
  926. ,scDiscount = @scDiscount
  927. ,tpExRate = @tpExRate
  928. ,amountLimitPerDay = 0
  929. ,amountLimitPerTran = 0
  930. ,customerTotalSentAmt = 0
  931. ,exRateDisplay = @exRate
  932. ,EXRATEID = @FOREX_SESSION_ID
  933. ,maxAmountLimitPerTran = 0
  934. ,minAmountLimitPerTran = 0
  935. ,PerTxnMinimumAmt = 0
  936. ,schemeAppliedMsg = @schemeAppliedMsg
  937. ,schemeId = @schemeId
  938. ,tpPCurr = @pCurr
  939. ,collCurr = @collCurr
  940. ,ForexSessionId = @FOREX_SESSION_ID
  941. ,discountedFee = ISNULL(@discountedFee, 0)
  942. END
  943. END