SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE OR ALTER PROCEDURE [dbo].[proc_countryRateMaster] @flag VARCHAR(50) = NULL
	,@user VARCHAR(30) = NULL
	,@depositType VARCHAR(200) = NULL
	,@countryId INT = NULL
	,@actualrate FLOAT = NULL
	,@promRate FLOAT = NULL
	,@countryName VARCHAR(200) = NULL
	,@activeStatus CHAR(1) = NULL
	,@sortBy VARCHAR(50) = NULL
	,@sortOrder VARCHAR(5) = NULL
	,@pageSize INT = NULL
	,@pageNumber INT = NULL
	,@receiverId BIGINT = NULL
AS
SET NOCOUNT ON
SET XACT_ABORT ON

BEGIN TRY
	SET NOCOUNT ON;

	CREATE TABLE #msg (
		errorCode INT
		,msg VARCHAR(100)
		,id INT
		)

	DECLARE @sql VARCHAR(MAX)
		,@oldValue VARCHAR(MAX)
		,@newValue VARCHAR(MAX)
		,@module VARCHAR(10)
		,@tableAlias VARCHAR(100)
		,@logIdentifier VARCHAR(50)
		,@logParamMod VARCHAR(100)
		,@logParamMain VARCHAR(100)
		,@table VARCHAR(MAX)
		,@select_field_list VARCHAR(MAX)
		,@extra_field_list VARCHAR(MAX)
		,@sql_filter VARCHAR(MAX)
		,@logType VARCHAR(10)
		,@oldRate FLOAT
		,@oldPromRate FLOAT

	SELECT @logIdentifier = 'depositType'
		,@logParamMain = 'countryRateMaster'
		,@logParamMod = 'countryRateMasterMod'
		,@module = '20'
		,@tableAlias = ''

	IF @flag = 'i-rate'
	BEGIN
		BEGIN TRANSACTION

		IF NOT EXISTS (
				SELECT 'X'
				FROM dbo.countryWiseExchangeRate
				WHERE countryId = @countryId
					AND depositType = @depositType
				)
		BEGIN
			INSERT INTO countryWiseExchangeRate (
				countryId
				,depositType
				,margin
				,purchaseRate
				,isActive
				,createdBy
				,createdDate
				)
			SELECT @countryId
				,@depositType
				,@actualrate
				,ISNULL(@promRate, '')
				,@activeStatus
				,@user
				,GETDATE()

			IF @@TRANCOUNT > 0
				COMMIT TRANSACTION

			EXEC proc_errorHandler 0
				,'Record has been added successfully.'
				,@depositType
		END
		ELSE IF EXISTS (
				SELECT 'X'
				FROM dbo.countryWiseExchangeRate
				WHERE countryId = @countryId
					AND depositType = @depositType
				)
		BEGIN
			INSERT INTO dbo.countryWiseExchangeRateHistory (
				countryId
				,margin
				,purchaseRate
				,modType
				,createdBy
				,createdDate
				)
			SELECT countryId
				,margin
				,purchaseRate
				,'UPDATE'
				,@user
				,GETDATE()
			FROM dbo.countryWiseExchangeRate
			WHERE countryId = @countryId
				AND depositType = @depositType

			UPDATE countryWiseExchangeRate
			SET depositType = @depositType
				,margin = @actualrate
				,purchaseRate = ISNULL(@promRate, '')
				,isActive = @activeStatus
			WHERE countryId = @countryId
				AND depositType = @depositType

			IF @@TRANCOUNT > 0
				COMMIT TRANSACTION

			EXEC proc_errorHandler 0
				,'Record has been added successfully.'
				,@depositType
		END
	END
	ELSE IF @flag = 'get-list'
	BEGIN
		--SELECT * FROM countryWiseExchangeRate WITH(NOLOCK) WHERE countryId = @countryId
		IF @sortBy IS NULL
			SET @sortBy = 'depositType'

		IF @sortOrder IS NULL
			SET @sortOrder = 'ASC'
		SET @table = '(
				SELECT
					main.countryId
					,main.depositType
					,main.margin
					,main.purchaseRate
					,main.createdBy
					,main.createdDate
					,main.isActive
				FROM countryWiseExchangeRate main WITH(NOLOCK)
					WHERE 1 = 1 AND main.countryId = ' + CAST(@countryId AS VARCHAR) + '
					) x'
		SET @sql_filter = ''
		SET @sql_filter = @sql_filter + ' AND ISNULL(isActive, '''') <> ''N'''

		IF @depositType IS NOT NULL
			SET @sql_filter = @sql_filter + ' AND depositType LIKE ''' + @depositType + '%'''
		SET @select_field_list = '
			countryId
			,depositType
			,margin
			,purchaseRate
			,createdBy
			,createdDate
			,isActive '

		EXEC dbo.proc_paging @table
			,@sql_filter
			,@select_field_list
			,@extra_field_list
			,@sortBy
			,@sortOrder
			,@pageSize
			,@pageNumber
	END
	ELSE IF @flag = 'getById'
	BEGIN
		SELECT *
		FROM dbo.countryWiseExchangeRate
		WHERE countryId = @countryId
			AND depositType = @depositType
	END
	ELSE IF @flag = 'payment-method'
	BEGIN
		SELECT depositType AS detailTitle
			,ISNULL(margin, 0) AS valueId
		FROM countryWiseExchangeRate
		WHERE countryId = @countryId
	END
	ELSE IF @flag = 'getListById'
	BEGIN
		SELECT CR.depositType AS detailTitle
			,CR.margin AS detailDesc
		FROM dbo.countryWiseExchangeRate CR(NOLOCK)
		INNER JOIN countryMaster CM(NOLOCK) ON CM.countryId = CR.countryId
		INNER JOIN receiverInformation RI(NOLOCK) ON RI.country = CM.countryName
		WHERE RI.receiverId = @receiverId
	END
	ELSE IF @flag = 'delete'
	BEGIN
		BEGIN TRANSACTION

		UPDATE countryWiseExchangeRate
		SET isDeleted = 'Y'
			,isActive = 'N'
			,modifiedBy = @user
			,modifiedDate = GETDATE()
		WHERE countryId = @countryId
			AND depositType = @depositType

		IF @@TRANCOUNT > 0
			COMMIT TRANSACTION

		SELECT 0 error_code
			,'Record deleted successfully.' mes
			,@countryId id
	END
END TRY

BEGIN CATCH
	IF @@TRANCOUNT > 0
		ROLLBACK TRANSACTION

	DECLARE @errorMessage VARCHAR(MAX)

	SET @errorMessage = ERROR_MESSAGE()

	EXEC proc_errorHandler 1
		,@errorMessage
		,@depositType
END CATCH