﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[LTCreateHoldStepsRevision]    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].[LTCreateHoldStepsRevision] -- Add the parameters for the stored procedure here
	@NewWorkRequestID INT,
	@LTWorkRequestID INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @ID INT DECLARE @BaseFlow VARCHAR(50) DECLARE @Location VARCHAR(50) DECLARE @OperSequence VARCHAR(10) DECLARE @Operation VARCHAR(50) DECLARE @OperationDescription VARCHAR(100) DECLARE @ChangeInstructions VARCHAR(1000) DECLARE @UpdatedBy INT DECLARE @tempLTHoldStepID INT DECLARE CurHoldSteps CURSOR FOR
SELECT
	ID,
	BaseFlow,
	Location,
	OperSequence,
	Operation,
	OperationDescription,
	ChangeInstructions,
	UpdatedBy
FROM
	LTHoldStep WITH(NOLOCK)
WHERE
	LTWorkRequestID = @LTWorkRequestID OPEN CurHoldSteps;

FETCH NEXT
FROM
	CurHoldSteps INTO @ID,
	@BaseFlow,
	@Location,
	@OperSequence,
	@Operation,
	@OperationDescription,
	@ChangeInstructions,
	@UpdatedBy WHILE @ @FETCH_STATUS = 0 BEGIN
INSERT INTO
	LTHoldStep (
		BaseFlow,
		Location,
		OperSequence,
		Operation,
		OperationDescription,
		ChangeInstructions,
		LTWorkRequestID,
		UpdatedBy,
		PreviousHoldStepID
	)
VALUES
	(
		@BaseFlow,
		@Location,
		@OperSequence,
		@Operation,
		@OperationDescription,
		@ChangeInstructions,
		@NewWorkRequestID,
		@UpdatedBy,
		@ID
	)
SET
	@tempLTHoldStepID = SCOPE_IDENTITY()
INSERT INTO
	LTWorkRequestAttachment (
		FileGUID,
		FileName,
		DocType,
		Comments,
		WorkRequestID,
		HoldStepID,
		UploadDateTime,
		UploadedByID,
		PreviousWorkRequestAttachmentID
	)
SELECT
	FileGUID,
	FileName,
	DocType,
	Comments,
	@NewWorkRequestID,
	@tempLTHoldStepID,
	UploadDateTime,
	UploadedByID,
	ID
FROM
	LTWorkRequestAttachment
WHERE
	HoldStepID = @ID FETCH NEXT
FROM
	CurHoldSteps INTO @ID,
	@BaseFlow,
	@Location,
	@OperSequence,
	@Operation,
	@OperationDescription,
	@ChangeInstructions,
	@UpdatedBy
END CLOSE CurHoldSteps;

DEALLOCATE CurHoldSteps;

INSERT INTO
	LTWorkRequestAttachment (
		FileGUID,
		FileName,
		DocType,
		Comments,
		WorkRequestID,
		HoldStepID,
		UploadDateTime,
		UploadedByID,
		PreviousWorkRequestAttachmentID
	)
SELECT
	FileGUID,
	FileName,
	DocType,
	Comments,
	@NewWorkRequestID,
	@tempLTHoldStepID,
	UploadDateTime,
	UploadedByID,
	ID
FROM
	LTWorkRequestAttachment
WHERE
	HoldStepID IS NULL
	AND WorkRequestID = @LTWorkRequestID
END
GO