﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[ProcesOOOExpiration]    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].[ProcesOOOExpiration] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @OOOUserID INT DECLARE @DelegatedTo INT BEGIN TRY DECLARE ProcessData CURSOR FOR
SELECT
	UserID,
	DelegatedTo
FROM
	Users
WHERE
	OOO = 1
	AND CONVERT(VARCHAR(8), OOOExpirationDate, 112) <= CONVERT(VARCHAR(8), GETDATE(), 112) OPEN ProcessData;

FETCH NEXT
FROM
	ProcessData INTO @OOOUserID,
	@DelegatedTo WHILE @ @FETCH_STATUS = 0 BEGIN EXEC ExpireOOOStatus @OOOUserID,
	@DelegatedTo FETCH NEXT
FROM
	ProcessData INTO @OOOUserID,
	@DelegatedTo
END CLOSE ProcessData;

DEALLOCATE ProcessData;

insert into
	EventLog (UserID, DocumentType, OperationType, Comments)
VALUES
	(
		'0',
		'ScheduledJob',
		'Job',
		'Process OoO Expiration was successful'
	)
END TRY BEGIN CATCH CLOSE ProcessData;

DEALLOCATE ProcessData;

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 Expiration failed'
	)
END CATCH
END
GO