USE [DD_ECM]
GO

/****** Object:  StoredProcedure [dbo].[PRDEX_BUILD_FILE_NAME]    Script Date: 08.12.2025 21:34:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- TSQL Prozedur - Zentrale Logik zur Erstellung der Dateinamen f�r die Schnittstellendatei f�r Schleupen.

-- MK // 04.10.2024
-- 04.10.2024 MK	Implementierung von SQL TRANSACTION
-- 14.10.2020 MK	Initial

CREATE OR ALTER PROCEDURE [dbo].[PRDEX_SET_EXPORT_FILENAME] 
			@JOB_GUID				INT,		-- Mandatory Parameter, to build the file name
			@EXPORT_BOOKING_NUMBER	VARCHAR(100),
			@FILE_NAME				VARCHAR(50) OUTPUT
AS

	PRINT '==============================='
	PRINT 'PROCEDURE  - START [PRCUST_EXPORT_POSTING_DATA_BUILD_FILE_NAME] @ ' + CONVERT(varchar(50),GETDATE(),120)
	PRINT 'PARAMETER1 - @JOB_GUID: ' + CONVERT(varchar(50),@JOB_GUID)
	PRINT '-------------------------------'

BEGIN TRY

	SET XACT_ABORT ON;

	BEGIN TRANSACTION

	SET NOCOUNT ON;

	DECLARE
			@FILE_NAME_SEPARATOR	VARCHAR(1),
			@FILE_BASE_NAME			VARCHAR(50),
			@FILE_RUNNING_NUMBER	BIGINT,
			@FILE_EXTENSION			VARCHAR(4)

	SELECT	@FILE_BASE_NAME			=	[FILE_BASE_NAME],
			@FILE_NAME_SEPARATOR	=	[FILE_NAME_SEPARATOR],
			@FILE_RUNNING_NUMBER	=	[FILE_RUNNING_NUMBER],
			@FILE_EXTENSION			=	[FILE_EXTENSION]
	FROM	[TBCUST_EXPORT_POSTING_DATA_FILE_CONFIG]
	WHERE	[JOB_GUID]				=	@JOB_GUID

	IF (@EXPORT_BOOKING_NUMBER is null) or (LEN(@EXPORT_BOOKING_NUMBER) = 0)
		BEGIN
			SET @EXPORT_BOOKING_NUMBER = 1
		END

	PRINT	'@FILE_BASE_NAME:			'	+ CONVERT(varchar(50),@FILE_BASE_NAME)
	PRINT	'@FILE_EXTENSION:			'	+ CONVERT(varchar(50),@FILE_EXTENSION)
	PRINT	'@FILE_NAME_SEPARATOR:		'	+ CONVERT(varchar(50),@FILE_NAME_SEPARATOR)

	SET		 @FILE_NAME	=	@FILE_BASE_NAME + @FILE_NAME_SEPARATOR + CONVERT(varchar(1000),@EXPORT_BOOKING_NUMBER) + '.' + @FILE_EXTENSION
	PRINT	'@FILE_NAME:   				'	+ CONVERT(varchar(50),@FILE_NAME)
	PRINT	'@EXPORT_BOOKING_NUMBER:	'	+ CONVERT(varchar(100),@EXPORT_BOOKING_NUMBER)

	-- Set running number to next value...
	SET		 @FILE_RUNNING_NUMBER			= @FILE_RUNNING_NUMBER + 1
	PRINT	'@FILE_RUNNING_NUMBER(new):	'	+ CONVERT(varchar(50),@FILE_RUNNING_NUMBER)

	-- ...and update value in TB
	UPDATE	[TBCUST_EXPORT_POSTING_DATA_FILE_CONFIG]
	SET		[FILE_RUNNING_NUMBER]	= @FILE_RUNNING_NUMBER
	WHERE	[JOB_GUID]				= @JOB_GUID
	PRINT	'Set - new - FILE_RUNNING_NUMBER to table!'

	-- Failsafe
	SET @FILE_NAME = ISNULL(@FILE_NAME,'undefined.txt')

	PRINT	'@FILE_NAME:	'	+ CONVERT(varchar(50),@FILE_NAME)

	PRINT '-------------------------------'
	PRINT 'PROCEDURE  - END PRCUST_EXPORT_POSTING_DATA_BUILD_FILE_NAME @ ' + CONVERT(varchar(50),GETDATE(),120)
	PRINT '==============================='
	
	COMMIT TRANSACTION

	--SELECT @FILE_NAME AS [FILE_NAME]
	RETURN

END TRY

BEGIN CATCH

	ROLLBACK TRANSACTION

	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(50),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
	--EXEC [PRCUST_EXPORT_POSTING_DATA_WRITE_LOG] @JOB_GUID, 'File:','Procedure: PRCUST_EXPORT_POSTING_DATA_CREATE_FILE', ERROR_PROCEDURE, ERROR_MESSAGE
	RETURN 1
END CATCH
GO


