﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[ProcesOOOEnableStatus]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE PROCEDURE [dbo].[ProcesOOOEnableStatus] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @ID INT DECLARE @OOOUserID INT DECLARE @DelegatedTo INT DECLARE @OOOStartDate DATETIME DECLARE @OOOExpirationDate DATETIME -- Insert statements for procedure here
BEGIN TRY DECLARE ProcessOOOData CURSOR FOR
SELECT
	ID,
	OOOUserID,
	DelegatedTo,
	OOOStartDate,
	OOOExpirationDate
FROM
	OOOTemp
WHERE
	Processed = 0
	AND OOOStartDate <= GETDATE() OPEN ProcessOOOData;

FETCH NEXT
FROM
	ProcessOOOData INTO @ID,
	@OOOUserID,
	@DelegatedTo,
	@OOOStartDate,
	@OOOExpirationDate WHILE @ @FETCH_STATUS = 0 BEGIN --PRINT @OOOStartDate
	EXEC EnableOOOStatus @OOOUserID,
	@DelegatedTo,
	@OOOStartDate,
	@OOOExpirationDate
UPDATE
	OOOTemp
SET
	Processed = 1
WHERE
	ID = @ID FETCH NEXT
FROM
	ProcessOOOData INTO @ID,
	@OOOUserID,
	@DelegatedTo,
	@OOOStartDate,
	@OOOExpirationDate
END CLOSE ProcessOOOData;

DEALLOCATE ProcessOOOData;

insert into
	EventLog (UserID, DocumentType, OperationType, Comments)
VALUES
	(
		'0',
		'ScheduledJob',
		'Job',
		'Process OoO ran successfully'
	)
END TRY BEGIN CATCH CLOSE ProcessOOOData;

DEALLOCATE ProcessOOOData;

DECLARE @ErrorMessage NVARCHAR(4000);

DECLARE @ErrorSeverity INT;

DECLARE @ErrorState INT;

SELECT
	@ErrorMessage = ERROR_MESSAGE(),
	@ErrorSeverity = ERROR_SEVERITY(),
	@ErrorState = ERROR_STATE();

-- Use RAISERROR inside the CATCH block to return 
-- error information about the original error that 
-- caused execution to jump to the CATCH block.
RAISERROR (
	@ErrorMessage,
	-- Message text.
	@ErrorSeverity,
	-- Severity.
	@ErrorState -- State.
);

insert into
	EventLog (UserID, DocumentType, OperationType, Comments)
VALUES
	('0', 'ScheduledJob', 'Job', 'Process OoO failed')
END CATCH
END
GO