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.

886 lines
43 KiB

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