USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_GetUSDAmountFromCollKRWAmt]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_GetUSDAmountFromCollKRWAmt]
GO
/****** Object:  StoredProcedure [dbo].[proc_GetUSDAmountFromCollKRWAmt]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE proc [dbo].[proc_GetUSDAmountFromCollKRWAmt]
@flag			VARCHAR(30),
@user			VARCHAR(150)	= NULL,
@pCountryId		INT,
@pCurr			VARCHAR(5),
@deliveryMethod VARCHAR(20),
@cAmt			MONEY,
@pAmt			MONEY			= NULL,
@calBy			CHAR(1),
@pCountry		VARCHAR(50)

AS
SET NOCOUNT ON ;

DECLARE @payOutPartner BIGINT
DECLARE @sCountryId INT = 118,@sAgent INT = 2080,@sSuperAgent INT = 1008 ,@sBranch INT ,@collCurr VARCHAR(5) = 'KRW'
DECLARE @scValue MONEY,@exRateOffer MONEY,@scDiscount MONEY ,@AmountLimitPerTran MONEY,@AmountLimitPerDay MONEY

DECLARE @place INT,@currDecimal INT,@exRate FLOAT,@serviceCharge MONEY,@tAmt MONEY

DECLARE @pSuperAgent INT,@pSuperAgentName VARCHAR(200),@pAgent BIGINT,@pAgentName VARCHAR(200),@pBranch INT,@pBranchName VARCHAR(200)

DECLARE @errorCode INT,@msg VARCHAR(MAX)

SELECT @pCountryId = countryId FROM countryMaster(NOLOCK) WHERE countryName = @pCountry

SELECT @payOutPartner = AgentId FROM TblPartnerwiseCountry (NOLOCK)
	WHERE COUNTRYID = @pCountryId
	AND ISNULL(PaymentMethod, @deliveryMethod) = @deliveryMethod 
	AND IsActive = 1

SELECT TOP 1 @pAgent = AM.agentId
	FROM agentMaster AM(NOLOCK) 
	WHERE AM.parentId = @payOutPartner AND agentType=2903 AND AM.isSettlingAgent = 'Y' AND AM.isApiPartner = 1

SELECT @pSuperAgent = sSuperAgent,@pSuperAgentName = sSuperAgentName,
			@pAgent = sAgent,@pAgentName = sAgentName ,@pBranch = sBranch,@pBranchName = sBranchName
	FROM dbo.FNAGetBranchFullDetails(@pAgent)

IF @flag = 'getUSDAmountFromCAmt'
BEGIN 
            IF @pCurr IS NULL
            BEGIN
				SELECT  '1' ErrorCode ,
                    'Currency not been defined yet for receiving country' Msg ,
                    amountLimitPerDay = @AmountLimitPerDay ,
                    customerTotalSentAmt = 0 ,
                    maxAmountLimitPerTran = @AmountLimitPerTran ,
                    PerTxnMinimumAmt = @AmountLimitPerDay;
                RETURN;
			END;
			
			SELECT  @exRate = 
				dbo.FNAGetCustomerRate(@sCountryId,@sAgent,@sBranch,@collCurr,@pCountryId,@pAgent, @pCurr,@deliveryMethod);
            IF @exRate IS NULL
            BEGIN
                SELECT  '1' ErrorCode ,
                    'Exchange rate not defined yet for receiving currency ('+ @pCurr + ')' Msg ,
                    amountLimitPerDay = @AmountLimitPerDay ,
                    customerTotalSentAmt = 0 ,
                    maxAmountLimitPerTran = @AmountLimitPerTran ,
                    PerTxnMinimumAmt = @AmountLimitPerDay;
                RETURN;
            END;

            IF @calBy = 'C'
            BEGIN	
				IF @pCountryId = 36
				BEGIN
					SET @pAmt = @cAmt * @exRate
					SELECT  @serviceCharge = amount
					FROM [dbo].FNAGetServiceCharge(@sCountryId,@sSuperAgent,@sAgent,@sBranch,
					@pCountryId,@pSuperAgent,@pAgent,NULL,@deliveryMethod,@pAmt,@collCurr);
				END
				ELSE IF @pCountryId = 42 AND @pCurr = 'USD' and @deliveryMethod = 2
				BEGIN
					SELECT  @serviceCharge = amount
					FROM [dbo].FNAGetServiceCharge(@sCountryId,@sSuperAgent,@sAgent,@sBranch,
					@pCountryId,@pSuperAgent,@pAgent,NULL,@deliveryMethod,@pAmt,'USD');
				END
				ELSE
				BEGIN
					SELECT  @serviceCharge = amount
					FROM [dbo].FNAGetServiceCharge(@sCountryId,@sSuperAgent,@sAgent,@sBranch,
					@pCountryId,@pSuperAgent,@pAgent,@pBranch,@deliveryMethod,@cAmt,@collCurr);
				END
                IF @serviceCharge IS NULL
                BEGIN
                    SELECT  '1' ErrorCode ,
                        'Service charge not defined yet for receiving country' Msg ,
                        amountLimitPerDay = @AmountLimitPerDay ,
                        customerTotalSentAmt = 0 ,
						maxAmountLimitPerTran = @AmountLimitPerTran ,
						PerTxnMinimumAmt = @AmountLimitPerDay;
                    RETURN;
                END;
		
                SET @tAmt = @cAmt - @serviceCharge;
			END;
			
			DECLARE @complianceRuleId INT, @cAmtUSD MONEY,@sCurrCostRate MONEY,@sCurrHoMargin MONEY,@tAmtUSD MONEY
  
			SELECT 
				@sCurrCostRate			= sCurrCostRate
				,@sCurrHoMargin			= sCurrHoMargin
			FROM dbo.FNAGetExRate(@sCountryId, @sAgent, @sBranch, @collCurr, @pCountryId, @pAgent, @pCurr, @deliveryMethod)

			IF @sCurrCostRate IS NULL
			BEGIN
				SELECT @errorCode = '1', @msg = 'Transaction cannot be proceed. Exchange Rate not defined!'
				RETURN
			END
			
			SET @tAmtUSD = @tAmt / (@sCurrCostRate + ISNULL(@sCurrHoMargin, 0))
			SET @tAmtUSD = ROUND(@tAmtUSD,2)
			SELECT 0 ErrorCode, Msg = @msg,Id = @tAmtUSD
END


GO
