

--Get the itmes
SELECT * FROm tbItem
WHERe ItemID in
(625 --should be true
	,685 -- should be true
	,686 -- should be true
	,703) -- should stay false
	
--get the store
SELECT * FROM tbEntity

--check the introduction and discontinue dates
SELECT * 
FROM tbVendorEntityItem
WHERE
EntityID = 271	AND
ItemID IN (625
	,685
	,686
	,703)

--check the inuse status annd update it if necessary
SELECT * FROM tbEntityItem
--UPDATE tbEntityItem SET INUSE=0
WHERE
EntityID = 271	AND
ItemID IN (625 -- should be true
	,685 -- should be true
	,686 -- should be true
	,703) -- should stay false


--get all items that are inactive that should be active
DECLARE @EntityID AS BIGINT = 271
DECLARE @ConfigurationEnum AS INT = 84506
DECLARE @DiscontinueDate AS DATETIME = DATEADD(DAY, - dbo.[fn_GetConfigurationLong]( @ConfigurationEnum, @EntityID), GETDATE())
DECLARE @CurrentDate AS DATETIME = GETDATE()

;
WITH VendorITEM_CTE (EntityID, ItemID,IntroductionDate, DiscontinueDate)
AS
(
	SELECT vei.EntityID, vei.ItemID,MAX(ISNULL(vei.IntroductionDate, GETDATE())), MAX(ISNULL(vei.DiscontinueDate, GETDATE())) [DiscontinueDate] 
		FROM tbVendorEntityItem  vei WITH (NOLOCK)
	GROUP BY vei.EntityID, vei.ItemID
)

SELECT *
FROM tbEntityItem ei WITH (NOLOCK)
	INNER JOIN VendorITEM_CTE vei ON ei.EntityID = vei.EntityID AND ei.ItemID = vei.ItemID 
		WHERE ei.EntityID=@EntityID 
			AND ei.InUse=0 
			AND vei.IntroductionDate < @CurrentDate
			AND vei.DiscontinueDate > @DiscontinueDate
			
			
		

			*/