﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[LTGetLotTravelerHoldSteps]    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].[LTGetLotTravelerHoldSteps] -- Add the parameters for the stored procedure here
	@LTLotID INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SELECT
	DISTINCT LH.*,
	FL.FileNames,
	WR.SWRNumber,
	U.FirstName + ' ' + U.LastName AS SignedOffByName
FROM
	LtLot L
	INNER JOIN LTLotTravelerRevisionAttrib LA ON L.ID = LA.lTLotID
	AND IsCurrentRevision = 1
	INNER JOIN LTWorkRequest WR ON L.WorkRequestID = WR.ID
	INNER JOIN LTLotTravelerHoldSteps LH
	LEFT JOIN Users U ON LH.SignedOffBy = U.UserID ON LA.ID = LH.LotTravelerRevisionID
	LEFT JOIN LTLotTravAttachment A ON LH.ID = A.LTLotTravHoldStepID
	LEFT JOIN (
		SELECT
			DISTINCT LH.ID,
			STUFF (
				(
					SELECT
						DISTINCT ',' + CAST(A1.FileName AS varchar(512)) + '~' + CAST(A1.FileGuid AS varchar(512))
					FROM
						LTLotTravAttachment A1
					WHERE
						A1.LTLotTravHoldStepID = LH.ID FOR XML PATH('')
				),
				1,
				1,
				''
			) AS FileNames
		FROM
			LTLotTravelerHoldSteps LH
	) AS FL ON LH.ID = FL.ID
WHERE
	LA.LTLotID = @LTLotID
ORDER BY
	Location,
	OperSequence
END
GO