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

DECLARE @CurrLotTravelerRevisionID INT -- -1 indicates to return the current revision
IF @RevisionNumber <> -1 BEGIN
SELECT
	L.ID,
	WR.SWRNumber,
	WR.Title,
	'Approved' AS Status,
	U.FirstName + ' ' + U.LastName AS Originator,
	U1.FirstName + ' ' + U1.LastName AS LastUpdatedBy,
	L.LotNumber,
	L.WIPPartNumber,
	L.PartDescription,
	L.Process,
	WR.IsITAR,
	L.WorkRequestID AS LTWorkRequestID,
	WR.PurposeOfRequest,
	LA.RevisionNumber AS LotTravCurrentRevision,
	LA.IsCurrentRevision,
	LA.RecordLockIndicator,
	WR.ProcessFlow AS BaseFlow,
	TraverlerLastUpdatedDatetime AS LastUpdate,
	LA.ID AS LotTravRevID,
	U2.FirstName + ' ' + U2.LastName AS TravelerClosedBy,
	TravelerClosedReason,
	TravelerClosedDate,
	TravelerClosed
FROM
	LTLot L
	INNER JOIN LTWorkRequest WR ON WR.ID = L.WorkRequestID
	INNER JOIN LTLotTravelerRevisionAttrib LA ON L.ID = LA.lTLotID
	LEFT JOIN Users U ON WR.OriginatorID = U.UserID
	LEFT JOIN Users U1 ON LA.TravelerLastUpdatedBy = U1.UserID
	LEFT JOIN Users U2 ON L.TravelerClosedBy = U2.UserID
WHERE
	L.ID = @LTLotID
	AND LA.RevisionNumber = @RevisionNumber
END
ELSE BEGIN
SELECT
	L.ID,
	WR.SWRNumber,
	WR.Title,
	'Approved' AS Status,
	U.FirstName + ' ' + U.LastName AS Originator,
	U1.FirstName + ' ' + U1.LastName AS LastUpdatedBy,
	L.LotNumber,
	L.WIPPartNumber,
	L.PartDescription,
	L.Process,
	WR.IsITAR,
	L.WorkRequestID AS LTWorkRequestID,
	WR.PurposeOfRequest,
	LA.RevisionNumber AS LotTravCurrentRevision,
	LA.RecordLockIndicator,
	WR.ProcessFlow AS BaseFlow,
	TraverlerLastUpdatedDatetime AS LastUpdate,
	LA.ID AS LotTravRevID,
	U2.FirstName + ' ' + U2.LastName AS TravelerClosedBy,
	TravelerClosedReason,
	TravelerClosedDate,
	TravelerClosed
FROM
	LTLot L
	INNER JOIN LTWorkRequest WR ON WR.ID = L.WorkRequestID
	INNER JOIN LTLotTravelerRevisionAttrib LA ON L.ID = LA.lTLotID
	AND LA.IsCurrentRevision = 1
	LEFT JOIN Users U ON WR.OriginatorID = U.UserID
	LEFT JOIN Users U1 ON LA.TravelerLastUpdatedBy = U1.UserID
	LEFT JOIN Users U2 ON L.TravelerClosedBy = U2.UserID
WHERE
	L.ID = @LTLotID
END
END
GO