/****** Object:  StoredProcedure [dbo].[FindRelatedObject]    Script Date: 1/6/2025 11:17:46 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[FindRelatedObject] 
	(
	@ItemID as bigint,
	@Result as int OUTPUT,
	@ResultID as bigint OUTPUT
	)
AS
	SET NOCOUNT ON
	
--	SELECT @Result=RelationTable, @ResultID=RelationTableID FROM dbo.AccDocItem WITH(NOLOCK) WHERE ID=@ItemID
--	IF @Result>0 and @ResultID > 0  return

/*
	SET @Result=0
	SELECT TOP 1 @ResultID=ID FROM dbo.AccSellFactor WITH(NOLOCK) WHERE DocItemID IN 
		(SELECT ID FROM dbo.AccDocItem WITH(NOLOCK) WHERE HeaderID in (Select HeaderID From dbo.AccDocItem WITH(NOLOCK) where ID=@ItemID)
					      AND RelationID in (Select RelationID From dbo.AccDocItem WITH(NOLOCK) where ID=@ItemID))
	IF @ResultID > 0  BEGIN
		SET @Result=1
		return
	END
*/	

	SELECT ID,RelationTableID into #TEMP FROM dbo.AccDocItem WITH(NOLOCK) WHERE HeaderID in (Select HeaderID From dbo.AccDocItem WITH(NOLOCK) where ID=@ItemID)
					AND RelationID in (Select RelationID From dbo.AccDocItem WITH(NOLOCK) where ID=@ItemID)
					AND RelationTableID in (Select RelationTableID From dbo.AccDocItem WITH(NOLOCK) where ID=@ItemID)
	
	SELECT TOP 1 @ResultID=ID FROM dbo.AccSellFactorTrucks WITH(NOLOCK) WHERE DocItemID IN (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=2
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.AccSellFactor WITH(NOLOCK) WHERE DocItemID IN (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=21
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.AccWarehouseDetails WITH(NOLOCK) WHERE DocItemID IN (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=9
		return
	END
		
	
	SELECT TOP 1 @ResultID=ID FROM dbo.AccChequeIn WITH(NOLOCK) WHERE DocItemID IN  (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=3
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.AccChequeIn WITH(NOLOCK) WHERE CashDocItemID IN  (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=4
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.AccChequeIn WITH(NOLOCK) WHERE ReturnDocItemID IN  (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=5
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.AccChequeOut WITH(NOLOCK) WHERE DocItemID IN  (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=6
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.AccChequeOutItemInfo WITH(NOLOCK) WHERE CashDocItemID IN (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=7
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.AccChequeOutItemInfo WITH(NOLOCK) WHERE ReturnDocItemID IN (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=8
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.AccFactorDetail WITH(NOLOCK) WHERE ID in (Select FactorID from AccFactorDetail WITH(NOLOCK) WHERE DocItemID IN (Select ID from #TEMP))
	IF @ResultID > 0  BEGIN
		SET @Result=12
		return
	END

	SELECT TOP 1 @ResultID=ID FROM dbo.Chicken WITH(NOLOCK) WHERE DocItemID IN (Select ID from #TEMP)
	IF @ResultID > 0  BEGIN
		SET @Result=18
		return
	END

	DECLARE @FD as NVARCHAR(5)
	SELECT top 1 @FD=FormData FROM AccDocItem WITH(NOLOCK) WHERE ID in (Select ID from #TEMP)
	SELECT @FD 
	IF len(@FD) > 0  BEGIN --it is Cash1 or Cash2
		SET @ResultID=@ItemID
		SET @Result=19
		return
	END

	DROP TABLE #TEMP

	RETURN