﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[LTGetWorkRequestItemPDF]    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].[LTGetWorkRequestItemPDF] @WorkRequestID INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	-- Get Work Request Item
SELECT
	WR.ID,
	WR.SWRNumber,
	WR.CurrentStep,
	WR.RecordLockIndicator,
	WR.RecordLockedBy,
	WR.RecordLockedDate,
	WR.IsCurrentRevision,
	WR.Revision,
	WR.OriginatorID,
	WR.OriginatedDate,
	WR.SubmitedDate,
	WR.CloseDate,
	WR.Status,
	WR.IsITAR,
	WR.Title,
	WR.LotType,
	WR.PCRBNumber,
	WR.EngLotStartRequired,
	WR.LotTypeConversion,
	WR.QualLot,
	REPLACE(WR.PurposeOfRequest, char(10), '<BR/>') AS PurposeOfRequest,
	WR.PlannedLotStartDate,
	WR.PlannedScheduledCloseDate,
	WR.ChargeDepartment,
	WR.AllocationToUse,
	WR.PredictedCyleTime,
	WR.ChargesForRawWafers,
	WR.DeptChargedForRawWafers,
	WR.DeptOwnerForRawWafers,
	WR.FabCost,
	WR.DeptChargedForFabCost,
	WR.DeptOwnerForFabCost,
	WR.SignOn,
	WR.Verb1,
	WR.EstimatedBinCLoseDate,
	WR.TotalQty,
	WR.StartQty,
	WR.Verb2,
	WR.WIPArea,
	WR.LotStartDate,
	WR.LotNumber,
	WR.WIPPartNumber,
	WR.PartDescription,
	WR.DefaultEPISiliconPartNumber,
	WR.ProcessFlow,
	WR.EmployeeID,
	WR.VerbComment,
	WR.RecordSiliconLotInSPNTapeTagTotrav,
	WR.SiliconLotNoCassette1,
	WR.SiliconOnHand,
	WR.SiliconExpectedDueDate,
	WR.SiliconComments,
	WR.ReticleOnHand,
	WR.ReticleExpectedDueDate,
	WR.ReticleComments,
	WR.SPNTransferOnHand,
	WR.SPNTransferExpectedDueDate,
	WR.SPNTransferComments,
	WR.ProbeCardOnHand,
	WR.ProbeCardExpectedDueDate,
	WR.ProbeCardComments,
	WR.ProbeRecipeOnHand,
	WR.ProbeRecipeExpectedDueDate,
	WR.ProbeRecipeComments,
	WR.ProcessChangeDetailsOnHand,
	WR.ProcessChangeDetailsDueDate,
	WR.LotStartCheckListComments,
	WR.PreviousWorkRequestID,
	WR.LastUpdateBy,
	WR.LastUpdateDate,
	U.FirstName + ' ' + U.LastName AS OriginatorName,
	U1.FirstName + ' ' + U1.LastName AS RecordLockByName
FROM
	LTWorkRequest WR
	LEFT JOIN Users U ON WR.OriginatorID = U.UserID
	LEFT JOIN Users U1 ON WR.RecordLockedBy = U1.UserID
WHERE
	ID = @WorkRequestID -- Get the Affected Departments
SELECT
	SRC.SubRoleCategoryItem AS AffectedDepartments
FROM
	LTAffectedDepartment D
	INNER JOIN SubRoleCategory SRC ON D.DepartmentID = SRC.SubRoleCategoryID
WHERE
	LTWorkRequestID = @WorkRequestID -- Get the Affected Modules
SELECT
	M.ModuleName AS AffectedModules
FROM
	LTAffectedModule LM
	INNER JOIN ECNModule M ON LM.ModuleID = M.ModuleID
WHERE
	LTWorkRequestID = @WorkRequestID -- Get the Materials (EPI)
SELECT
	R.WIPPartNumber,
	R.PartDescription,
	R.DefaultEPISiliconPartNumber,
	M.*
FROM
	LTWorkRequest R
	INNER JOIN LTMaterial M ON R.ID = M.LTWorkRequestID
WHERE
	LTWorkRequestID = @WorkRequestID -- Get the Lot HoldSteps
SELECT
	DISTINCT LH.ID,
	LH.BaseFlow,
	LH.Location,
	LH.OperSequence,
	LH.Operation,
	REPLACE(LH.OperationDescription, char(10), '<BR/>') AS OperationDescription,
	REPLACE(LH.ChangeInstructions, char(10), '<BR/>') AS ChangeInstructions,
	LH.LTWorkRequestID,
	LH.UpdatedBy,
	LH.PreviousHoldStepID,
	FL.FileNames AS FileNames
FROM
	LTHoldStep LH
	LEFT JOIN LTWorkRequestAttachment A ON LH.ID = A.HoldStepID
	LEFT JOIN (
		SELECT
			DISTINCT LH.ID,
			STUFF (
				(
					SELECT
						DISTINCT ',' + CAST(A1.FileName AS varchar(512))
					FROM
						LTWorkRequestAttachment A1
					WHERE
						A1.HoldStepID = LH.ID FOR XML PATH('')
				),
				1,
				1,
				''
			) AS FileNames
		FROM
			LTHoldStep LH
	) AS FL ON LH.ID = FL.ID
WHERE
	LH.LTWorkRequestID = @WorkRequestID
END
GO