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.

873 lines
42 KiB

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