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.

1054 lines
53 KiB

  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 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. --------------------------------------------
  48. BEGIN
  49. DECLARE @scValue MONEY
  50. ,@scAction CHAR(2)
  51. ,@scOffer MONEY
  52. ,@exRateOffer FLOAT
  53. ,@scDiscount MONEY
  54. DECLARE @place INT
  55. ,@currDecimal INT
  56. ,@collMode VARCHAR(30)
  57. ,@sendingCustType VARCHAR(30)
  58. ,@msg VARCHAR(150)
  59. ,@errorCode INT
  60. DECLARE @exRateCalByPartner BIT
  61. ,@pSuperAgent INT
  62. ,@serviceCharge MONEY
  63. ,@serviceChargetmp MONEY
  64. ,@tAmt MONEY
  65. ,@pSuperAgentName VARCHAR(100)
  66. ,@pBranch INT
  67. DECLARE @pAgentName VARCHAR(100)
  68. ,@pBranchName VARCHAR(100)
  69. ,@exRate FLOAT
  70. ,@pCurrHoMargin FLOAT
  71. ,@FOREX_SESSION_ID VARCHAR(40)
  72. DECLARE @tranCount INT = 0
  73. ,@schemeCount INT = 0
  74. ,@schemeAppliedMsg VARCHAR(100) = ''
  75. ,@isFirstTran CHAR(1) = 'N'
  76. ,@createdFrom CHAR(1) = NULL
  77. ,@isEligible CHAR(1) = 'N'
  78. ,@introducer VARCHAR(25) = NULL
  79. --IF @ProcessFor IN ('send')
  80. --BEGIN
  81. IF ISNULL(@user, 'onlinedefault') <> 'onlinedefault' --OR @user is not null
  82. BEGIN
  83. PRINT 'a'
  84. IF @senderId IS NULL OR @senderId=0
  85. BEGIN
  86. SET @senderId = (
  87. SELECT customerId
  88. FROM customermaster(NOLOCK)
  89. WHERE username = @user
  90. )
  91. END
  92. --EXEC PROC_Customer_LoyaltyV2 @flag = 'check-eligible-v2'
  93. -- ,@isEligible = @isEligible OUT
  94. -- ,@referralCode = @introducer
  95. -- ,@tranCount = @trancount OUT
  96. -- ,@schemeCount = @schemeCount OUT
  97. -- ,@customerId = @senderId
  98. -- ,@createdFrom = 'M'
  99. IF NOT EXISTS (
  100. SELECT 1
  101. FROM (
  102. SELECT TOP 1 customerId
  103. FROM TRANSENDERS TS(NOLOCK)
  104. INNER JOIN remittran(NOLOCK) rt ON rt.id = ts.tranId
  105. WHERE customerId = @SenderId
  106. AND rt.tranStatus <> 'CANCEL' --AND rt.tranType='M'
  107. UNION ALL
  108. SELECT TOP 1 customerId
  109. FROM TRANSENDERSTEMP TT(NOLOCK)
  110. INNER JOIN remittrantemp(NOLOCK) rt ON rt.id = tt.tranId
  111. WHERE customerId = @SenderId
  112. AND rt.tranStatus <> 'CANCEL' --AND rt.tranType='M'
  113. ) a
  114. WHERE customerId = @SenderId
  115. )
  116. BEGIN
  117. SET @isFirstTran = 'Y'
  118. END
  119. END
  120. print '@isFirstTran' + '-> ' + @isFirstTran
  121. SELECT @createdFrom = serviceUsedFor
  122. FROM customerMaster(NOLOCK)
  123. WHERE customerId = @senderId
  124. PRINT '@senderId'
  125. PRINT @senderId;
  126. SET @sCountryId = 233
  127. SET @sAgent='394395';
  128. IF ISNULL(@pCountryId, 0) = 0
  129. SELECT @pCountryId = countryId
  130. FROM countryMaster(NOLOCK)
  131. WHERE COUNTRYNAME = @pCountry
  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 and @isFirstTran='N'
  243. BEGIN
  244. SET @exRate = @exRate - @tpExRate
  245. END
  246. ELSE
  247. BEGIN
  248. DECLARE @rate FLOAT, @promRate FLOAT
  249. SELECT @rate = ISNULL(margin,0), @promRate = ISNULL(purchaseRate,0) FROM countryWiseExchangeRate WHERE depositType='ONLINE' and countryId = @pCountryId
  250. PRINT' @rate'
  251. PRINT @rate
  252. PRINT '@promRate'
  253. PRINT @promRate
  254. IF @isFirstTran='Y'
  255. SET @exRate = @exRate - (ISNULL(@rate,0) +ISNULL(@promRate,0))
  256. ELSE
  257. SET @exRate = @exRate - ISNULL(@rate,0)
  258. END
  259. --IF(ISNULL(@discountedFee,0)> 0)
  260. --BEGIN
  261. -- IF dbo.FNA_GET_AVAILABLE_BALANCE_POINTS(@senderId) < ISNULL(@discountedFee, 0)
  262. -- BEGIN
  263. -- SELECT '1' ErrorCode
  264. -- ,'You do not have sufficient Reward Amount!' Msg
  265. -- ,NULL id
  266. -- RETURN
  267. -- END
  268. --END
  269. DECLARE @ValidationResult TABLE (
  270. IS_VALID VARCHAR(50),
  271. ROW_ID INT,
  272. PROMOTIONAL_CODE VARCHAR(50),
  273. PROMOTIONAL_MSG VARCHAR(255),
  274. -- PROMOTION_TYPE INT,
  275. PROMOTION_VALUE MONEY,
  276. MIN_SENDING_AMT MONEY,
  277. START_DT DATETIME,
  278. END_DT DATETIME
  279. )
  280. DECLARE @availableRewardAmt INT, @row_id INT, @promoValue MONEY, @minSendAmt MONEY, @startDate DATETIME, @endDate DATETIME, @errMsg VARCHAR(100), @promoCode VARCHAR(100)
  281. SELECT @availableRewardAmt = CAST(dbo.[FNA_GET_AVAILABLE_BALANCE_POINTS](@senderId) AS INT)
  282. DECLARE @minCollAmt MONEY, @maxRewardAmt MONEY
  283. SELECT @maxRewardAmt = CAST(detailTitle AS MONEY) FROM staticDataValue WHERE typeID = '10200' AND detailDesc = 'MAX_REWARD_AMT'
  284. SELECT @minCollAmt = CAST(detailTitle AS MONEY) FROM staticDataValue WHERE typeID = '10200' AND detailDesc = 'MIN_COLL_AMT_FOR_REWARD'
  285. IF ISNUMERIC(ISNULL(@discountedFee,0)) = 1
  286. BEGIN
  287. SET @isPromoCode = 'N'
  288. IF(ISNULL(@discountedFee, 0) > 0)
  289. BEGIN
  290. IF (@availableRewardAmt < ISNULL(@discountedFee, 0))
  291. BEGIN
  292. SELECT '1' ErrorCode
  293. ,'You do not have sufficient Rewards!'
  294. ,NULL id
  295. RETURN;
  296. END
  297. END
  298. IF(ISNULL(@discountedFee, 0) > ISNULL(@availableRewardAmt,0))
  299. BEGIN
  300. SELECT '1' ErrorCode
  301. ,'Please enter the valid point. The amount cannot be more than available Reward Amount!' Msg
  302. ,NULL id
  303. RETURN
  304. END
  305. DECLARE @discountedFeeMoney MONEY = CAST(@discountedFee AS MONEY)
  306. IF (@discountedFeeMoney > '0.00' AND @cAmt < @minCollAmt AND @discountedFeeMoney > @maxRewardAmt)
  307. BEGIN
  308. SELECT '8' ErrorCode
  309. ,'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
  310. ,NULL id
  311. RETURN;
  312. END
  313. IF (@discountedFeeMoney > '0.00' AND @discountedFeeMoney > @maxRewardAmt)
  314. BEGIN
  315. SELECT '8' ErrorCode
  316. ,'The maximum reward amount per transaction is ' + CAST(@maxRewardAmt AS VARCHAR(20)) + ' Pounds' Msg
  317. ,NULL id
  318. RETURN;
  319. END
  320. IF (@discountedFeeMoney > '0.00' AND @cAmt < @minCollAmt)
  321. BEGIN
  322. SELECT '8' ErrorCode
  323. ,'The minimum amount that can be sent for each transaction is ' + CAST(@minCollAmt AS VARCHAR(20)) + ' Pounds' Msg
  324. ,NULL id
  325. RETURN;
  326. END
  327. END
  328. ELSE
  329. BEGIN
  330. SET @isPromoCode = 'Y'
  331. print 'promo'
  332. INSERT INTO @ValidationResult (IS_VALID, ROW_ID, PROMOTIONAL_CODE, PROMOTIONAL_MSG, PROMOTION_VALUE, MIN_SENDING_AMT, START_DT, END_DT)
  333. EXEC proc_CheckPromoValidity @flag='chk-validity', @countryId = @pCountryId, @pmode = @deliveryMethodId, @promoCode = @discountedFee
  334. IF EXISTS (SELECT 'X' FROM @ValidationResult WHERE IS_VALID = 'VALID')
  335. BEGIN
  336. SELECT @row_id = ROW_ID, @promoValue = PROMOTION_VALUE, @minSendAmt = MIN_SENDING_AMT, @startDate = START_DT, @endDate = END_DT FROM @ValidationResult WHERE IS_VALID = 'VALID'
  337. SET @promoCode = @discountedFee
  338. SET @discountedFee = @promoValue
  339. IF (@cAmt < @minSendAmt)
  340. BEGIN
  341. SELECT '8' ErrorCode
  342. ,'The minimum amount per transaction should be ' + CAST(@minSendAmt AS VARCHAR(20)) + ' Pounds to use Promo Code' Msg
  343. ,NULL id
  344. RETURN;
  345. END
  346. END
  347. ELSE
  348. BEGIN
  349. SELECT @errMsg = PROMOTIONAL_MSG FROM @ValidationResult WHERE IS_VALID = 'INVALID'
  350. SELECT '1' ErrorCode
  351. ,@errMsg Msg
  352. RETURN
  353. END
  354. END
  355. --PRINT '@isFirstTran:' + CAST( @isFirstTran AS VARCHAR);
  356. --PRINT '@isEligible:' + CAST( @isEligible AS VARCHAR);
  357. -- PRINT '@tranCount:' + CAST( @tranCount AS VARCHAR);
  358. -- PRINT '@@schemeCount:' + CAST( @schemeCount AS VARCHAR);
  359. IF @calBy = 'C'
  360. BEGIN
  361. BEGIN
  362. IF ISNULL(@isManualSc, 'N') = 'N'
  363. BEGIN
  364. PRINT '@serviceChargetmp';
  365. SELECT @serviceChargetmp = isnull(amount, 0)
  366. FROM [dbo].FNAGetServiceCharge(@sCountryId, @sSuperAgent, @sAgent, @sBranch, @pCountryId, @pSuperAgent, @pAgent, @pBranch, @deliveryMethodId, @cAmt, @collCurr)
  367. PRINT @serviceChargetmp
  368. IF (@discountedFee < '0.00')
  369. --AND (@serviceChargetmp < @discountedFee)
  370. BEGIN
  371. SELECT '8' ErrorCode
  372. ,'Redeem Amount cannot be less than zero' Msg
  373. ,NULL id
  374. RETURN;
  375. END
  376. ELSE
  377. BEGIN
  378. --SELECT @serviceCharge = @serviceChargetmp - ISNULL(@discountedFee, 0)
  379. SELECT @serviceCharge = @serviceChargetmp
  380. END
  381. END
  382. ELSE
  383. BEGIN
  384. SET @serviceCharge = ISNULL(@manualSc, 0)
  385. END
  386. IF @serviceCharge IS NULL
  387. AND ISNULL(@isManualSc, 'N') = 'N'
  388. BEGIN
  389. SELECT '4' ErrorCode
  390. ,'Service charge not defined yet for receiving country' Msg
  391. ,NULL id
  392. RETURN;
  393. END
  394. END
  395. IF @scAction = 'PD' -- Percent Discount
  396. BEGIN
  397. SET @scOffer = (@scValue / 100) * @serviceCharge
  398. SET @scDiscount = (@scValue / 100) * @serviceCharge
  399. END
  400. ELSE IF @scAction = 'FD' -- Flat Discount
  401. BEGIN
  402. SET @scDiscount = @scValue
  403. END
  404. ELSE IF @scAction = 'FV' -- Fixed Value
  405. BEGIN
  406. SET @scOffer = @scValue
  407. SET @scDiscount = @serviceCharge - @scValue
  408. END
  409. --IF (@discountedFee > @serviceCharge)
  410. -- BEGIN
  411. -- SELECT '8' ErrorCode
  412. -- ,'Redeem Amount cannot be higher than Transfer fee.' Msg
  413. -- ,NULL id
  414. -- RETURN;
  415. -- END
  416. SET @tAmt = @cAmt - @serviceCharge + @scDiscount + ISNULL(@discountedFee, 0)
  417. SET @pAmt = @tAmt * (@exRate + @exRateOffer)
  418. --PRINT @pAmt;
  419. --SET @pAmt = FLOOR(@pAmt)
  420. SET @tAmt=Cast(Round(@tAmt,2,0) as decimal(18,2));
  421. SET @pAmt=Cast(Round(@pAmt,2,0) as decimal(18,2));
  422. --PRINT @pAmt;
  423. END
  424. ELSE
  425. BEGIN
  426. --SET @tAmt = CEILING(@pAmt/(@exRate + @exRateOffer), 0)
  427. --SET @tAmt = CEILING(@pAmt / (@exRate + @exRateOffer))
  428. SET @tAmt = @pAmt / (@exRate + @exRateOffer);
  429. BEGIN
  430. IF ISNULL(@isManualSc, 'N') = 'N'
  431. BEGIN
  432. SELECT @serviceChargetmp = isnull(amount, 0)
  433. FROM [dbo].FNAGetServiceCharge(@sCountryId, @sSuperAgent, @sAgent, @sBranch, @pCountryId, @pSuperAgent, @pAgent, @pBranch, @deliveryMethodId, @tAmt, @collCurr)
  434. IF (@discountedFee < '0.00')
  435. --AND (@serviceChargetmp < @discountedFee)
  436. BEGIN
  437. SELECT '8' ErrorCode
  438. ,'Redeem Amount cannot be less than zero' Msg
  439. ,NULL id
  440. RETURN;
  441. END
  442. ELSE
  443. BEGIN
  444. SELECT @serviceCharge = @serviceChargetmp;
  445. END
  446. END
  447. ELSE
  448. BEGIN
  449. SET @serviceCharge = ISNULL(@manualSc, 0)
  450. END
  451. IF @serviceCharge IS NULL
  452. BEGIN
  453. SELECT '4' ErrorCode
  454. ,'Service charge not defined yet for receiving country' Msg
  455. ,NULL id
  456. RETURN;
  457. END
  458. END
  459. IF @scAction = 'PD'
  460. BEGIN
  461. SET @scOffer = (@scValue / 100) * @serviceCharge
  462. SET @scDiscount = (@scValue / 100) * @serviceCharge
  463. END
  464. ELSE IF @scAction = 'FD'
  465. BEGIN
  466. SET @scDiscount = @scValue
  467. END
  468. ELSE IF @scAction = 'FV'
  469. BEGIN
  470. SET @scOffer = @scValue
  471. SET @scDiscount = @serviceCharge - @scValue
  472. END
  473. PRINT '@discountedFee'
  474. PRINT @discountedFee
  475. PRINT @serviceCharge
  476. --IF (@discountedFee > @serviceCharge)
  477. -- BEGIN
  478. -- SELECT '8' ErrorCode
  479. -- ,'Redeem Amount cannot be higher than Transfer fee.' Msg
  480. -- ,NULL id
  481. -- RETURN;
  482. -- END
  483. SET @cAmt = (@tAmt + @serviceCharge - @scDiscount) - ISNULL(@discountedFee, 0)
  484. SET @tAmt =Cast(Round(@tAmt,2,0) as decimal(18,2));
  485. SET @cAmt =Cast(Round(@cAmt,2,0) as decimal(18,2))
  486. --SET @cAmt = CEILING(@cAmt)
  487. END
  488. --4. Validate Country Sending Limit
  489. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 's-limit'
  490. ,@cAmt = @cAmt
  491. ,@pAmt = @pAmt
  492. ,@sCountryId = @sCountryId
  493. ,@collMode = @collMode
  494. ,@deliveryMethod = @deliveryMethodId
  495. ,@sendingCustType = @sendingCustType
  496. ,@pCountryId = @pCountryId
  497. ,@pCurr = @pCurr
  498. ,@collCurr = @collCurr
  499. ,@pAgent = @pAgent
  500. ,@sAgent = @sAgent
  501. ,@sBranch = @sBranch
  502. ,@msg = @msg OUT
  503. ,@errorCode = @errorCode OUT
  504. IF @errorCode <> '0'
  505. BEGIN
  506. SELECT @errorCode ErrorCode
  507. ,@msg Msg
  508. RETURN;
  509. END
  510. --Validate Country Sending Limit END
  511. --5. Validate Country Receiving Limit
  512. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 'r-limit'
  513. ,@cAmt = @cAmt
  514. ,@pAmt = @pAmt
  515. ,@sCountryId = @sCountryId
  516. ,@collMode = @collMode
  517. ,@deliveryMethod = @deliveryMethodId
  518. ,@sendingCustType = @sendingCustType
  519. ,@pCountryId = @pCountryId
  520. ,@pCurr = @pCurr
  521. ,@collCurr = @collCurr
  522. ,@pAgent = @pAgent
  523. ,@sAgent = @sAgent
  524. ,@sBranch = @sBranch
  525. ,@msg = @msg OUT
  526. ,@errorCode = @errorCode OUT
  527. IF @errorCode <> '0'
  528. BEGIN
  529. SELECT @errorCode ErrorCode
  530. ,@msg Msg
  531. RETURN;
  532. END
  533. IF ISNULL(@ProcessFor, '') = 'send'
  534. BEGIN
  535. SET @FOREX_SESSION_ID = NEWID()
  536. ----## lock ex rate for individual txn
  537. UPDATE exRateCalcHistory
  538. SET isExpired = 1
  539. WHERE CUSTOMER_ID = @senderId
  540. AND isExpired = 0
  541. PRINT @serviceCharge;
  542. INSERT INTO exRateCalcHistory (
  543. CUSTOMER_ID
  544. ,[USER_ID]
  545. ,FOREX_SESSION_ID
  546. ,serviceCharge
  547. ,pAmt
  548. ,customerRate
  549. ,sCurrCostRate
  550. ,sCurrHoMargin
  551. ,sCurrAgentMargin
  552. ,pCurrCostRate
  553. ,pCurrHoMargin
  554. ,pCurrAgentMargin
  555. ,agentCrossSettRate
  556. ,createdDate
  557. ,isExpired
  558. ,tAmt
  559. ,schemeId
  560. ,discountedFee
  561. ,sharingValue
  562. ,customerPremium
  563. )
  564. SELECT @senderId
  565. ,@user
  566. ,@FOREX_SESSION_ID
  567. ,@serviceCharge
  568. ,@pAmt
  569. ,@exRate
  570. ,1
  571. ,0
  572. ,0
  573. ,@pCurrHoMargin + @exRate
  574. ,@pCurrHoMargin
  575. ,0
  576. ,@exRate
  577. ,GETDATE()
  578. ,0
  579. ,@tAmt
  580. ,@schemeId
  581. ,ISNULL(@discountedFee, 0)
  582. ,@schemeCount
  583. ,@tpExRate
  584. END
  585. SET @msg = 'Success'
  586. PRINT @promRate
  587. PRINT @isFirstTran
  588. PRINT '@promRate1'
  589. if(@promRate>0 AND @isFirstTran='Y')
  590. SET @schemeAppliedMsg = 'Enjoy premium rate due to First Transaction!';
  591. SELECT @errorCode ErrorCode
  592. ,@msg Msg
  593. ,scCharge = @serviceCharge
  594. ,exRate = @exRate
  595. ,place = @place
  596. ,pCurr = @pCurr
  597. ,currDecimal = @currDecimal
  598. ,pAmt = @pAmt
  599. ,sAmt = @tAmt
  600. ,place = @place
  601. ,disc = 0.00
  602. ,collAmt = @cAmt
  603. ,exRateOffer = @exRateOffer
  604. ,scOffer = @scDiscount
  605. ,scAction = @scAction
  606. ,scValue = @scValue
  607. ,scDiscount = @scDiscount
  608. ,tpExRate = 0
  609. ,amountLimitPerDay = 0
  610. ,amountLimitPerTran = 0
  611. ,customerTotalSentAmt = 0
  612. ,exRateDisplay = @exRate
  613. ,EXRATEID = @FOREX_SESSION_ID
  614. ,maxAmountLimitPerTran = 0
  615. ,minAmountLimitPerTran = 0
  616. ,PerTxnMinimumAmt = 0
  617. ,schemeAppliedMsg = @schemeAppliedMsg
  618. ,schemeId = @schemeId
  619. ,tpPCurr = @pCurr
  620. ,collCurr = @collCurr
  621. ,ForexSessionId = @FOREX_SESSION_ID
  622. ,discountedFee = ISNULL(@discountedFee, 0)
  623. ,isPromoCode = @isPromoCode
  624. ,promoRowId = @row_id
  625. ,promoCode = @promoCode
  626. END
  627. ELSE IF @FLAG = 'true'
  628. BEGIN
  629. IF ISNULL(@tpExRate, 0) = 0
  630. BEGIN
  631. SELECT '5' ErrorCode
  632. ,'Third Party Exchange rate fetching error for currency (' + @pCurr + ')' Msg
  633. RETURN
  634. END
  635. DECLARE @exRateDonga FLOAT, @exRateBRAC FLOAT
  636. PRINT @sCountryId;
  637. PRINT @sCountryId;
  638. PRINT @sAgent;
  639. PRINT @sBranch;
  640. PRINT @collCurr;
  641. PRINT @pCountryId;
  642. PRINT @pAgent;
  643. PRINT @pCurr;
  644. PRINT @deliveryMethodId;
  645. SELECT @pCurrHoMargin = pCurrHoMargin
  646. FROM dbo.FNAGetExRate(@sCountryId, @sAgent, @sBranch, @collCurr, @pCountryId, @pAgent, @pCurr, @deliveryMethodId)
  647. SELECT @exRate = @tpExRate - ISNULL(@pCurrHoMargin, 0)
  648. IF ISNULL(@exRate, 0) = 0
  649. BEGIN
  650. SELECT '3' ErrorCode
  651. ,'Exchange rate not defined yet for receiving currency (' + @pCurr + ')' Msg
  652. ,NULL id
  653. RETURN
  654. END
  655. IF @user = 'onlinedefault'
  656. AND @pCountryId = 203
  657. BEGIN
  658. SELECT @exRateDonga = dbo.FNAGetCustomerRate(113, 0, 394395, 'JPY', 203, 394133, 'VND', 1)
  659. IF @exRateDonga > @exRate
  660. SET @exRate = @exRateDonga
  661. END
  662. IF @user = 'onlinedefault'
  663. AND @pCountryId = 16
  664. BEGIN
  665. SELECT @exRateBRAC = dbo.FNAGetCustomerRate(113, 0, 394395, 'JPY', 16, 394414, 'BDT', 1)
  666. IF @exRateBRAC > @exRate
  667. SET @exRate = @exRateBRAC
  668. END
  669. IF @calBy = 'C'
  670. BEGIN
  671. IF (
  672. @tranCount = @schemeCount
  673. AND ISNULL(@user, 'onlinedefault') <> 'onlinedefault'
  674. -- AND ISNULL(@ProcessFor, '') = 'send'
  675. AND @isEligible = 'Y'
  676. )
  677. OR (@isFirstTran = 'Y')
  678. BEGIN
  679. SET @serviceCharge = 0
  680. IF @isFirstTran = 'Y'
  681. BEGIN
  682. SET @schemeAppliedMsg = 'Enjoy zero (0) service charge due to First Transaction!';
  683. END
  684. ELSE
  685. SET @schemeAppliedMsg = 'Enjoy zero (0) service charge due to Loyalty Scheme!';
  686. END
  687. ELSE
  688. BEGIN
  689. IF ISNULL(@isManualSc, 'N') = 'N'
  690. BEGIN
  691. SELECT @serviceChargetmp = ISNULL(amount, 0)
  692. FROM [dbo].FNAGetServiceCharge(@sCountryId, @sSuperAgent, @sAgent, @sBranch, @pCountryId, @pSuperAgent, @pAgent, @pBranch, @deliveryMethodId, @cAmt, @collCurr)
  693. --IF (@discountedFee > '0.00')
  694. -- AND (@serviceChargetmp < @discountedFee)
  695. --BEGIN
  696. -- SELECT '8' ErrorCode
  697. -- ,'Redeem points can not be more than service charge' Msg
  698. -- ,NULL id
  699. -- RETURN;
  700. --END
  701. --ELSE
  702. --BEGIN
  703. -- SELECT @serviceCharge = ISNULL(@serviceChargetmp, 0) - ISNULL(@discountedFee, 0)
  704. --END
  705. END
  706. ELSE
  707. BEGIN
  708. SET @serviceCharge = ISNULL(@manualSc, 0)
  709. END
  710. IF @serviceCharge IS NULL
  711. AND ISNULL(@isManualSc, 'N') = 'N'
  712. BEGIN
  713. SELECT '4' ErrorCode
  714. ,'Service charge not defined yet for receiving country' Msg
  715. ,NULL id
  716. RETURN;
  717. END
  718. END
  719. IF @scAction = 'PD' -- Percent Discount
  720. BEGIN
  721. SET @scOffer = (@scValue / 100) * @serviceCharge
  722. SET @scDiscount = (@scValue / 100) * @serviceCharge
  723. END
  724. ELSE IF @scAction = 'FD' -- Flat Discount
  725. BEGIN
  726. SET @scDiscount = @scValue
  727. END
  728. ELSE IF @scAction = 'FV' -- Fixed Value
  729. BEGIN
  730. SET @scOffer = @scValue
  731. SET @scDiscount = @serviceCharge - @scValue
  732. END
  733. SET @tAmt = @cAmt - @serviceCharge + @scDiscount + ISNULL(@discountedFee, 0)
  734. SET @pAmt = @tAmt * (@exRate + @exRateOffer)
  735. --SET @pAmt = FLOOR(@pAmt)
  736. END
  737. ELSE
  738. BEGIN
  739. --SET @tAmt = CEILING(@pAmt / (@exRate + @exRateOffer))
  740. SET @tAmt = @pAmt / (@exRate + @exRateOffer)
  741. IF ISNULL(@isManualSc, 'N') = 'N'
  742. BEGIN
  743. SELECT @serviceChargetmp = isnull(amount, 0)
  744. FROM [dbo].FNAGetServiceCharge(@sCountryId, @sSuperAgent, @sAgent, @sBranch, @pCountryId, @pSuperAgent, @pAgent, @pBranch, @deliveryMethodId, @tAmt, @collCurr)
  745. --IF (@discountedFee > '0.00')
  746. -- AND (@serviceCharge < @discountedFee)
  747. --BEGIN
  748. -- SELECT '8' ErrorCode
  749. -- ,'Redeem points can not be more than service charge' Msg
  750. -- ,NULL id
  751. -- RETURN;
  752. --END
  753. --ELSE
  754. --BEGIN
  755. -- SELECT @serviceCharge = ISNULL(@serviceChargetmp, 0) - ISNULL(@discountedFee, 0)
  756. --END
  757. END
  758. ELSE
  759. BEGIN
  760. SET @serviceCharge = ISNULL(@manualSc, 0)
  761. END
  762. IF @serviceCharge IS NULL
  763. BEGIN
  764. SELECT '4' ErrorCode
  765. ,'Service charge not defined yet for receiving country' Msg
  766. ,NULL id
  767. RETURN;
  768. END
  769. IF @scAction = 'PD'
  770. BEGIN
  771. SET @scOffer = (@scValue / 100) * @serviceCharge
  772. SET @scDiscount = (@scValue / 100) * @serviceCharge
  773. END
  774. ELSE IF @scAction = 'FD'
  775. BEGIN
  776. SET @scDiscount = @scValue
  777. END
  778. ELSE IF @scAction = 'FV'
  779. BEGIN
  780. SET @scOffer = @scValue
  781. SET @scDiscount = @serviceCharge - @scValue
  782. END
  783. SET @cAmt = (@tAmt + @serviceCharge - @scDiscount)+ ISNULL(@discountedFee, 0)
  784. --SET @cAmt = ROUND(@cAmt, @currDecimal)
  785. --New logic for calculating new tAmt and pAmt after adding discount fee
  786. SET @tAmt = @tAmt + ISNULL(@discountedFee, 0)
  787. SET @pAmt = @tAmt * (@exRate + @exRateOffer)
  788. --SET @pAmt = FLOOR(@pAmt)
  789. END
  790. --4. Validate Country Sending Limit
  791. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 's-limit'
  792. ,@cAmt = @cAmt
  793. ,@pAmt = @pAmt
  794. ,@sCountryId = @sCountryId
  795. ,@collMode = @collMode
  796. ,@deliveryMethod = @deliveryMethodId
  797. ,@sendingCustType = @sendingCustType
  798. ,@pCountryId = @pCountryId
  799. ,@pCurr = @pCurr
  800. ,@collCurr = @collCurr
  801. ,@pAgent = @pAgent
  802. ,@sAgent = @sAgent
  803. ,@sBranch = @sBranch
  804. ,@msg = @msg OUT
  805. ,@errorCode = @errorCode OUT
  806. IF @errorCode <> '0'
  807. BEGIN
  808. SELECT @errorCode ErrorCode
  809. ,@msg Msg
  810. RETURN;
  811. END
  812. --Validate Country Sending Limit END
  813. --5. Validate Country Receiving Limit
  814. EXEC PROC_CHECKCOUNTRYLIMIT @flag = 'r-limit'
  815. ,@cAmt = @cAmt
  816. ,@pAmt = @pAmt
  817. ,@sCountryId = @sCountryId
  818. ,@collMode = @collMode
  819. ,@deliveryMethod = @deliveryMethodId
  820. ,@sendingCustType = @sendingCustType
  821. ,@pCountryId = @pCountryId
  822. ,@pCurr = @pCurr
  823. ,@collCurr = @collCurr
  824. ,@pAgent = @pAgent
  825. ,@sAgent = @sAgent
  826. ,@sBranch = @sBranch
  827. ,@msg = @msg OUT
  828. ,@errorCode = @errorCode OUT
  829. IF @errorCode <> '0'
  830. BEGIN
  831. SELECT @errorCode ErrorCode
  832. ,@msg Msg
  833. RETURN;
  834. END
  835. --Validate Country Receiving Limit
  836. IF ISNULL(@ProcessFor, '') = 'send'
  837. BEGIN
  838. SET @FOREX_SESSION_ID = NEWID()
  839. ----## lock ex rate for individual txn
  840. UPDATE exRateCalcHistory
  841. SET isExpired = 1
  842. WHERE CUSTOMER_ID = @senderId
  843. AND isExpired = 0
  844. INSERT INTO exRateCalcHistory (
  845. CUSTOMER_ID
  846. ,[USER_ID]
  847. ,FOREX_SESSION_ID
  848. ,serviceCharge
  849. ,pAmt
  850. ,customerRate
  851. ,sCurrCostRate
  852. ,sCurrHoMargin
  853. ,sCurrAgentMargin
  854. ,pCurrCostRate
  855. ,pCurrHoMargin
  856. ,pCurrAgentMargin
  857. ,agentCrossSettRate
  858. ,createdDate
  859. ,isExpired
  860. ,tAmt
  861. ,schemeId
  862. ,discountedFee
  863. ,sharingValue
  864. )
  865. SELECT @senderId
  866. ,@user
  867. ,@FOREX_SESSION_ID
  868. ,@serviceCharge
  869. ,@pAmt
  870. ,@exRate
  871. ,1
  872. ,0
  873. ,0
  874. ,@pCurrHoMargin + @exRate
  875. ,@pCurrHoMargin
  876. ,0
  877. ,@exRate
  878. ,GETDATE()
  879. ,0
  880. ,@tAmt
  881. ,@schemeId
  882. ,ISNULL(@discountedFee, 0)
  883. ,@schemeCount
  884. END
  885. SET @msg = 'Success'
  886. PRINT @promRate
  887. PRINT @isFirstTran
  888. PRINT '@promRate1'
  889. if(@promRate>0 AND @isFirstTran='Y')
  890. SET @schemeAppliedMsg = 'Enjoy premium rate due to First Transaction!';
  891. SELECT @errorCode ErrorCode
  892. ,@msg Msg
  893. ,scCharge = @serviceCharge
  894. ,exRate = @exRate
  895. ,place = @place
  896. ,pCurr = @pCurr
  897. ,currDecimal = @currDecimal
  898. ,pAmt = @pAmt
  899. ,sAmt = @tAmt
  900. ,place = @place
  901. ,disc = 0.00
  902. ,collAmt = @cAmt
  903. ,exRateOffer = @exRateOffer
  904. ,scOffer = @scDiscount
  905. ,scAction = @scAction
  906. ,scValue = @scValue
  907. ,scDiscount = @scDiscount
  908. ,tpExRate = @tpExRate
  909. ,amountLimitPerDay = 0
  910. ,amountLimitPerTran = 0
  911. ,customerTotalSentAmt = 0
  912. ,exRateDisplay = @exRate
  913. ,EXRATEID = @FOREX_SESSION_ID
  914. ,maxAmountLimitPerTran = 0
  915. ,minAmountLimitPerTran = 0
  916. ,PerTxnMinimumAmt = 0
  917. ,schemeAppliedMsg = @schemeAppliedMsg
  918. ,schemeId = @schemeId
  919. ,tpPCurr = @pCurr
  920. ,collCurr = @collCurr
  921. ,ForexSessionId = @FOREX_SESSION_ID
  922. ,discountedFee = ISNULL(@discountedFee, 0)
  923. END
  924. END