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.
300 lines
19 KiB
300 lines
19 KiB
USE [FastMoneyPro_Remit]
|
|
GO
|
|
/****** Object: StoredProcedure [dbo].[PROC_REMIT_INBOUND_EXRATE] Script Date: 4/17/2024 11:56:39 AM ******/
|
|
SET ANSI_NULLS ON
|
|
GO
|
|
SET QUOTED_IDENTIFIER ON
|
|
GO
|
|
|
|
|
|
ALTER PROC [dbo].[PROC_REMIT_INBOUND_EXRATE]
|
|
(
|
|
@Flag VARCHAR(20)
|
|
, @SendingCountryCode VARCHAR(10) = NULL
|
|
, @UserName VARCHAR(80) = NULL
|
|
, @SendingCurrencyCode VARCHAR(10) = NULL
|
|
, @PayoutCountryCode VARCHAR(10) = NULL
|
|
, @PayoutCurrencyCode VARCHAR(10) = NULL
|
|
, @CollectionAmount MONEY = NULL
|
|
, @PayoutAmount MONEY = NULL
|
|
, @PaymentTypeCode VARCHAR(20) = NULL
|
|
, @AgentId INT = NULL
|
|
, @CalcBy CHAR(1) = NULL
|
|
)
|
|
AS
|
|
;
|
|
SET NOCOUNT ON;
|
|
SET XACT_ABORT ON;
|
|
BEGIN TRY
|
|
BEGIN
|
|
DECLARE @ErrorMsg VARCHAR(MAX) = NULL
|
|
IF @Flag = 'EXRATE'
|
|
BEGIN
|
|
DECLARE @PCountryId INT, @DeliveryMethodId INT, @PayoutPartner INT, @ExRateCalcByPartner BIT, @ScountryId INT
|
|
DECLARE @SSuperAgentId INT, @SAgentId INT = @AgentId, @PAgent INT
|
|
|
|
SELECT @PCountryId = countryId
|
|
FROM COUNTRYMASTER (NOLOCK)
|
|
WHERE countryCode = @PayoutCountryCode
|
|
|
|
IF ISNULL(@PCountryId, 0) = 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid PayoutCountryCode, contact Head Office.', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
SELECT @ScountryId = countryId
|
|
FROM COUNTRYMASTER (NOLOCK)
|
|
WHERE countryCode = @SendingCountryCode
|
|
|
|
IF ISNULL(@ScountryId, 0) = 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid SendingCountryCode, contact Head Office.', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
SELECT @DeliveryMethodId = serviceTypeId
|
|
FROM SERVICETYPEMASTER (NOLOCK)
|
|
WHERE serviceCode = @PaymentTypeCode
|
|
|
|
IF ISNULL(@DeliveryMethodId, 0) = 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid PaymentTypeCode, contact Head Office.', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
SELECT @PayoutPartner = AGENTID
|
|
,@ExRateCalcByPartner = ISNULL(exRateCalByPartner, 0)
|
|
FROM TblPartnerwiseCountry(NOLOCK)
|
|
WHERE CountryId = @PCountryId
|
|
AND IsActive = 1
|
|
AND ISNULL(PaymentMethod, @DeliveryMethodId) = @DeliveryMethodId
|
|
|
|
IF ISNULL(@PayoutPartner, 0) = 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Pyout partner not defined for selected Payout Country, contact Head Office.', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
PRINT '@@PayoutPartner'
|
|
PRINT @PayoutPartner
|
|
|
|
SELECT @PAgent = AGENTID
|
|
FROM AGENTMASTER(NOLOCK)
|
|
WHERE parentid = @PayoutPartner
|
|
AND ISNULL(ISSETTLINGAGENT, 'N') = 'Y';
|
|
PRINT '@PAgent'
|
|
PRINT @PAgent
|
|
|
|
DECLARE @PSuperAgent INT, @PSuperAgentName VARCHAR(150), @PAgentName VARCHAR(150), @PBranch INT, @PBranchName VARCHAR(150)
|
|
SELECT @PSuperAgent = sSuperAgent
|
|
,@PSuperAgentName = sSuperAgentName
|
|
,@PAgent = sAgent
|
|
,@PAgentName = sAgentName
|
|
,@PBranch = sBranch
|
|
,@PBranchName = sBranchName
|
|
FROM dbo.FNAGetBranchFullDetails(@PAGENT)
|
|
|
|
DECLARE @PCurrencyCode VARCHAR(10)
|
|
SELECT @PCurrencyCode = pCurrency
|
|
FROM dbo.exRateTreasury WITH (NOLOCK)
|
|
WHERE pCountry = @PCountryId
|
|
AND pAgent = @PAgent
|
|
|
|
IF @PCurrencyCode IS NULL
|
|
SELECT @PCurrencyCode = pCurrency
|
|
FROM dbo.exRateTreasury WITH (NOLOCK)
|
|
WHERE pCountry = @PCountryId
|
|
AND pAgent IS NULL
|
|
|
|
IF @PCurrencyCode IS NULL
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Currency not been defined yet for receiving country, contact Head Office.', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
IF @PCurrencyCode <> @PayoutCurrencyCode
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid PayoutCurrencyCode, contact Head Office.', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
DECLARE @Place INT, @CurrDecimal INT
|
|
SELECT @Place = place
|
|
,@CurrDecimal = currDecimal
|
|
FROM currencyPayoutRound WITH (NOLOCK)
|
|
WHERE ISNULL(isDeleted, 'N') = 'N'
|
|
AND currency = @PCurrencyCode
|
|
AND ISNULL(tranType, @DeliveryMethodId) = @DeliveryMethodId
|
|
|
|
SET @currDecimal = ISNULL(@currDecimal, 0)
|
|
|
|
PRINT @ScountryId
|
|
PRINT @SAgentId
|
|
PRINT @SendingCurrencyCode
|
|
PRINT @pCountryId
|
|
PRINT @pAgent
|
|
PRINT @PayoutCurrencyCode
|
|
PRINT @DeliveryMethodId
|
|
|
|
DECLARE @ExRate FLOAT
|
|
SELECT @ExRate = dbo.FNAGetCustomerRate(@ScountryId, @SAgentId, NULL, @SendingCurrencyCode, @pCountryId, @pAgent, @PayoutCurrencyCode, @DeliveryMethodId)
|
|
|
|
IF ISNULL(@ExRate, 0) = 0
|
|
BEGIN
|
|
SET @ErrorMsg = 'Exchange rate not defined yet for receiving currency (' + @PCurrencyCode + ')'
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = @ErrorMsg, @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
DECLARE @ServiceCharge MONEY, @SSuperAgent INT, @TransferAmount MONEY,@SendingCurrCodeSC varchar(3)
|
|
SELECT @SSuperAgent = parentId
|
|
FROM agentMaster (NOLOCK)
|
|
WHERE agentId = @SAgentId
|
|
PRINT '@SSuperAgent'
|
|
PRINT @SSuperAgent;
|
|
|
|
PRINT '@@PSuperAgent'
|
|
PRINT @PSuperAgent;
|
|
PRINT '@PAgent'
|
|
PRINT @PAgent;
|
|
IF ISNULL(@CalcBy, '') = ''
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Calc by can not be empty!', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
IF @CalcBy NOT IN ('C', 'P')
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Calc by can not be other than ''C'' or ''P''!', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
IF @CalcBy = 'C' AND ISNULL(@CollectionAmount, 0) = 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Collection amount can not be 0 or empty for Calc By ''C''!', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
IF @CalcBy = 'P' AND ISNULL(@PayoutAmount, 0) = 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Payout amount can not be 0 or empty for Calc By ''P''!', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
SELECT TOP 1 @SendingCurrCodeSC= currency
|
|
FROM sendTranLimit WITH ( NOLOCK )
|
|
WHERE countryId = @ScountryId order by stlid desc
|
|
|
|
IF @CalcBy = 'C'
|
|
BEGIN
|
|
SELECT @ServiceCharge = amount
|
|
FROM [dbo].FNAGetServiceCharge(@ScountryId, @SSuperAgent, @SAgentId, NULL, @PCountryId, @PSuperAgent, @PAgent, @PBranch, @DeliveryMethodId, @CollectionAmount,ISNULL( @SendingCurrCodeSC,@SendingCurrencyCode))
|
|
|
|
IF ISNULL(@ServiceCharge, 0) = 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Service charge not defined yet for receiving country', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
SET @TransferAmount = @CollectionAmount - @ServiceCharge
|
|
SET @PayoutAmount = @TransferAmount * @ExRate
|
|
SET @PayoutAmount = FLOOR(@PayoutAmount)
|
|
END
|
|
ELSE
|
|
BEGIN
|
|
SET @TransferAmount = CEILING(@PayoutAmount / (@ExRate))
|
|
|
|
PRINT '@CollectionAmount'
|
|
PRINT @CollectionAmount
|
|
|
|
PRINT '@@SendingCurrCodeSC'
|
|
PRINT @SendingCurrCodeSC
|
|
|
|
SELECT @ServiceCharge = amount
|
|
FROM [dbo].FNAGetServiceCharge(@ScountryId, @SSuperAgent, @SAgentId, NULL, @PCountryId, @PSuperAgent, @PAgent, @PBranch, @DeliveryMethodId, @PayoutAmount, ISNULL( @SendingCurrCodeSC,@SendingCurrencyCode))
|
|
|
|
IF ISNULL(@ServiceCharge, 0) = 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Service charge not defined yet for receiving country', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
SET @CollectionAmount = @TransferAmount + @ServiceCharge
|
|
SET @CollectionAmount = CEILING(@CollectionAmount)
|
|
END
|
|
|
|
IF @CollectionAmount <= 0 OR @PayoutAmount <= 0 OR @TransferAmount <= 0
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = 'Invalid payout/transfer amount/collect amount found!', @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
|
|
PRINT '@PayoutAmount'
|
|
PRINT @PayoutAmount
|
|
|
|
DECLARE @ErrorCode VARCHAR(10)
|
|
--EXEC PROC_CHECKCOUNTRYLIMIT @flag = 's-limit'
|
|
-- ,@cAmt = @CollectionAmount
|
|
-- ,@pAmt = @PayoutAmount
|
|
-- ,@sCountryId = @sCountryId
|
|
-- ,@collMode = NULL
|
|
-- ,@deliveryMethod = @DeliveryMethodId
|
|
-- ,@sendingCustType = NULL
|
|
-- ,@pCountryId = @PCountryId
|
|
-- ,@pCurr = @PayoutCurrencyCode
|
|
-- ,@collCurr =@SendingCurrCodeSC
|
|
-- ,@pAgent = @PAgent
|
|
-- ,@sAgent = @SAgentId
|
|
-- ,@sBranch = NULL
|
|
-- ,@msg = @ErrorMsg OUT
|
|
-- ,@errorCode = @ErrorCode OUT
|
|
|
|
--IF @ErrorCode <> '0'
|
|
--BEGIN
|
|
-- EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = @ErrorMsg, @Id = NULL;
|
|
-- RETURN;
|
|
--END
|
|
|
|
EXEC PROC_CHECKCOUNTRYLIMIT @flag = 'r-limit'
|
|
,@cAmt = @CollectionAmount
|
|
,@pAmt = @PayoutAmount
|
|
,@sCountryId = @sCountryId
|
|
,@collMode = NULL
|
|
,@deliveryMethod = @DeliveryMethodId
|
|
,@sendingCustType = NULL
|
|
,@pCountryId = @PCountryId
|
|
,@pCurr = @PayoutCurrencyCode
|
|
,@collCurr = @SendingCurrCodeSC
|
|
,@pAgent = @PAgent
|
|
,@sAgent = @SAgentId
|
|
,@sBranch = NULL
|
|
,@msg = @ErrorMsg OUT
|
|
,@errorCode = @ErrorCode OUT
|
|
|
|
IF @ErrorCode <> '0'
|
|
BEGIN
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = @ErrorMsg, @Id = NULL;
|
|
RETURN;
|
|
END
|
|
|
|
SELECT ErrorCode = 100
|
|
, ErrorMessage = 'Success'
|
|
, Id = NULL
|
|
, Extra = NULL
|
|
, ServiceCharge = @ServiceCharge
|
|
, ExRate = @ExRate
|
|
, Place = @Place
|
|
, PayoutCurrencyCode = @PayoutCurrencyCode
|
|
, CurrDecimal = @CurrDecimal
|
|
, CollectionAmount = @CollectionAmount
|
|
, TransferAmount = @TransferAmount
|
|
, PayoutAmount = @PayoutAmount
|
|
END
|
|
END
|
|
END TRY
|
|
BEGIN CATCH
|
|
IF @@TRANCOUNT>0
|
|
ROLLBACK TRANSACTION
|
|
|
|
SET @ErrorMsg = 'Exception executing SP: ' + ERROR_MESSAGE()
|
|
EXEC SW_PROC_ERROR_HANDLER @ErrorCode = 101, @ErrorMessage = @ErrorMsg, @Id = NULL;
|
|
END CATCH
|