﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[MRBGetLots]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[MRBGetLots] @MRBNumber INT AS BEGIN -- 2019-04-19 TR: ticket #35349
SET
	NOCOUNT ON;

SELECT
	M.*,
	STUFF(
		(
			SELECT
				DISTINCT ',' + CAST(L.IssueID AS varchar(512))
			FROM
				Lot L
			WHERE
				L.LotNumber = M.LotNumber FOR XML PATH('')
		),
		1,
		1,
		''
	) AS LotDispositionsLinkedToLot,
	STUFF(
		(
			SELECT
				DISTINCT ',' + CAST(m2.MRBNumber AS varchar(512)) + '_' + CAST(ISNULL(m2.DispoType, '') AS varchar(512))
			FROM
				MRBLot m2
			WHERE
				m2.LotNumber = M.LotNumber
				AND m2.MRBNumber <> @MRBNumber FOR XML PATH('')
		),
		1,
		1,
		''
	) AS OtherMRBs
FROM
	MRBLot M
WHERE
	M.MRBNumber = @MRBNumber
ORDER BY
	CASE
		WHEN CHARINDEX('.', M.LotNumber) > 0 THEN LEFT(M.LotNumber, CHARINDEX('.', M.LotNumber))
		ELSE M.LotNumber
	END,
	CASE
		WHEN CHARINDEX('.', M.LotNumber) > 0 THEN CONVERT(
			int,
			SUBSTRING(M.LotNumber, CHARINDEX('.', M.LotNumber) + 1, 99)
		)
		ELSE 0
	END
END
GO