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.

137 lines
12 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[ws_int_proc_costRateUpdateJP] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. CREATE proc [dbo].[ws_int_proc_costRateUpdateJP]
  9. @accesscode VARCHAR(50)
  10. ,@username VARCHAR(50)
  11. ,@password VARCHAR(50)
  12. ,@AGENT_REFID VARCHAR(50)
  13. ,@cost FLOAT
  14. AS
  15. SET NOCOUNT ON
  16. SET XACT_ABORT ON
  17. BEGIN TRY
  18. DECLARE @errorTable TABLE(AGENT_REFID VARCHAR(150), cost FLOAT)
  19. INSERT INTO @errorTable(AGENT_REFID, cost)
  20. SELECT @AGENT_REFID, @cost
  21. DECLARE @errCode INT, @autMsg VARCHAR(500), @errorCode VARCHAR(10), @errorMsg VARCHAR(MAX)
  22. EXEC ws_int_proc_checkAuthntication @USERNAME,@PASSWORD,@ACCESSCODE,@errCode OUT, @autMsg OUT
  23. IF (@errCode = 1 )
  24. BEGIN --1002
  25. SELECT @errorCode = '102', @errorMsg = ISNULL(@autMsg, 'Authentication Fail')
  26. SELECT @errorCode CODE, @errorMsg MESSAGE, * FROM @errorTable
  27. RETURN
  28. END
  29. CREATE TABLE #defExRateIdTemp(defExRateId INT)
  30. CREATE TABLE #exRateIdTempMain(exRateTreasuryId INT)
  31. CREATE TABLE #exRateIdTempMod(exRateTreasuryId INT)
  32. DECLARE @cCountry INT, @cAgent INT, @defExRateId INT
  33. SELECT @cCountry = 113
  34. SELECT @cAgent = 4846
  35. SELECT @defExRateId = defExRateId FROM defExRate WITH(NOLOCK) WHERE country = @cCountry AND agent = @cAgent AND currency = 'JPY'
  36. DECLARE @cRate FLOAT
  37. SET @cRate = @cost
  38. IF @defExRateId IS NULL
  39. RETURN
  40. BEGIN TRAN
  41. --Currency Rate/ Agent Rate Update------------------------------------------------------------------------------------
  42. UPDATE defExRate SET
  43. cRate = ISNULL(@cost, 0)
  44. ,modifiedBy = @username
  45. ,modifiedDate = GETDATE()
  46. WHERE defExRateId = @defExRateId
  47. --Change Record History-----------------------------------------------------------------------------------------------
  48. INSERT INTO defExRateHistory(
  49. defExRateId
  50. ,setupType
  51. ,currency,country,agent,baseCurrency,tranType,factor
  52. ,cRate,cMargin,cMax,cMin
  53. ,pRate,pMargin,pMax,pMin
  54. ,isEnable,createdBy,createdDate,approvedBy,approvedDate,modType
  55. )
  56. SELECT
  57. @defExRateId
  58. ,main.setupType
  59. ,main.currency,main.country,main.agent,main.baseCurrency,main.tranType,factor
  60. ,cRate,cMargin,cMax,cMin
  61. ,pRate,pMargin,pMax,pMin
  62. ,isEnable,@username,GETDATE(),@username,GETDATE(),'U'
  63. FROM defExRate main WITH(NOLOCK) WHERE defExRateId = @defExRateId
  64. --1. Get All Corridor records affected by receive cost rate change
  65. DELETE FROM #exRateIdTempMain
  66. INSERT INTO #exRateIdTempMain
  67. SELECT exRateTreasuryId FROM exRateTreasury WITH(NOLOCK) WHERE cRateId = @defExRateId
  68. --2. Update Records in Mod Table if data already exist in mod table
  69. IF EXISTS(SELECT 'X' FROM exRateTreasuryMod mode WITH(NOLOCK) INNER JOIN #exRateIdTempMain temp ON mode.exRateTreasuryId = temp.exRateTreasuryId)
  70. BEGIN
  71. DELETE FROM #exRateIdTempMod
  72. INSERT INTO #exRateIdTempMod
  73. SELECT mode.exRateTreasuryId FROM exRateTreasuryMod mode WITH(NOLOCK)
  74. INNER JOIN #exRateIdTempMain temp ON mode.exRateTreasuryId = temp.exRateTreasuryId
  75. UPDATE ert SET
  76. cRate = @cRate
  77. ,cMargin = ert.cMargin
  78. ,maxCrossRate = ROUND(def.pRate/@cRate, dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency))
  79. ,crossRate = ROUND((def.pRate - def.pMargin - pHoMargin)/(@cRate + ert.cMargin + cHoMargin), dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency))
  80. ,customerRate = CASE WHEN ISNULL(toleranceOn, '') IN ('S', 'P', '') THEN ROUND((def.pRate - ert.pMargin - pHoMargin - pAgentMargin)/(@cRate + ert.cMargin + cHoMargin + cAgentMargin), dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency))
  81. WHEN ISNULL(toleranceOn, '') IN ('C') THEN ROUND((def.pRate - def.pMargin - pHoMargin)/(@cRate + ert.cMargin + cHoMargin), dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency)) - ISNULL(agentCrossRateMargin, 0) END
  82. ,createdBy = @username
  83. ,createdDate = GETDATE()
  84. FROM exRateTreasuryMod ert
  85. INNER JOIN defExRate def ON ert.pRateId = def.defExRateId
  86. INNER JOIN #exRateIdTempMod temp ON ert.exRateTreasuryId = temp.exRateTreasuryId
  87. END
  88. --3. Update Record in main table
  89. UPDATE ert SET
  90. cRate = @cRate
  91. ,pMargin = ert.cMargin
  92. ,maxCrossRate = ROUND(def.pRate/@cRate, dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency))
  93. ,crossRate = ROUND((def.pRate - def.pMargin - pHoMargin)/(@cRate + ert.cMargin + cHoMargin), dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency))
  94. --,customerRate = ROUND((@pRate - @pMargin - pHoMargin - pAgentMargin)/(def.cRate + def.cMargin + cHoMargin + cAgentMargin), dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency))
  95. ,customerRate = CASE WHEN ISNULL(toleranceOn, '') IN ('S', 'P', '') THEN ROUND((def.pRate - def.pMargin - pHoMargin - pAgentMargin)/(@cRate + ert.cMargin + cHoMargin + cAgentMargin), dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency))
  96. WHEN ISNULL(toleranceOn, '') IN ('C') THEN ROUND((def.pRate - def.pMargin - pHoMargin)/(@cRate + ert.cMargin + cHoMargin), dbo.FNAGetCrossRateDecimalMask(ert.cCurrency, ert.pCurrency)) - ISNULL(agentCrossRateMargin, 0) END
  97. ,modifiedBy = @username
  98. ,modifiedDate = GETDATE()
  99. FROM exRateTreasury ert
  100. INNER JOIN defExRate def ON ert.pRateId = def.defExRateId
  101. WHERE cRateId = @defExRateId
  102. COMMIT TRAN
  103. SELECT @cRate COST
  104. END TRY
  105. BEGIN CATCH
  106. IF @@TRANCOUNT > 0
  107. ROLLBACK TRAN
  108. SELECT @errorCode = '9001', @errorMsg = 'Technical Error : ' + ERROR_MESSAGE()
  109. SELECT '9001' CODE, 'Technical Error : ' + ERROR_MESSAGE() MESSAGE, * FROM @errorTable
  110. INSERT INTO Logs (errorPage, errorMsg, errorDetails, createdBy, createdDate)
  111. SELECT 'API SP Error','Technical Error : ' + ERROR_MESSAGE() MESSAGE,'ws_int_proc_costRateUpdateJP', @USERNAME, GETDATE()
  112. END CATCH
  113. GO