﻿
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__setBankingtransaction_done]
	@taID varchar(100)
	,@authuser varchar(25)
AS
BEGIN
	SET NOCOUNT ON;

	IF [dbo].[fis_getModuleAuth]('fds_bam', @authuser) < 1
		THROW 60000, N'not authorized', 1;  
	
	INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
		VALUES ('fds__setBankingtransaction_done' ,@authuser , '');
	

	DECLARE @now datetime = GETUTCDATE();
	DECLARE @today date = @now;

	DECLARE @OUT TABLE([taID] varchar(10), [done_manually] varchar(25));
	
	MERGE [dbo].[fds__bankingtransactions_settings] as TARGET
		USING (SELECT TOP(1) * FROM [dbo].[fds__bankingtransactions] as b WHERE b.[taID] = @taID) as SOURCE ON TARGET.[taID] = SOURCE.[taID]
	WHEN MATCHED THEN
		UPDATE SET [done_manually] = FORMAT(@now, 'yyyy-MM-dd HH:mm:ss') + ';' + @authuser
	WHEN NOT MATCHED BY TARGET THEN
		INSERT ([taID],[done_manually]) VALUES (SOURCE.[taID], FORMAT(@now, 'yyyy-MM-dd HH:mm:ss') + ';' + @authuser)
	OUTPUT inserted.[taID], inserted.[done_manually]
		INTO @out;
	;


	--output to confirm as boolean, plus the transaction data needed for the notification message
	SELECT
		CAST( (CASE WHEN ISNULL((SELECT TOP(1) [done_manually] FROM @out), '') <> '' THEN 1 ELSE 0 END) as bit) as [success]
		,b.[ValueDate]
		,b.[Amount]
	FROM (SELECT 1 as [dummy]) as d
	LEFT JOIN [dbo].[fds__bankingtransactions] as b ON b.[taID] = @taID;

END