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.

977 lines
48 KiB

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