SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		Azimi
-- Create date: 
-- Description:	
-- select * from Accwarehouse
-- =============================================
alter PROCEDURE [dbo].[DeleteWarehouseAndRelations] 
	@WarehouseID bigint = 0
AS
BEGIN
	SET NOCOUNT ON;
	
	BEGIN TRANSACTION
	
	DECLARE @ID as BIGINT
	SELECT @ID=FormID from AccWarehouse where FromForm = 0 and ID = @WarehouseID
	IF @ID > 0
	BEGIN
		EXEC DeleteFactorAndRelations @FactorID = @ID
	END
	SELECT @ID=OutID from AccWarehouse where ID = @WarehouseID
	IF @ID > 0
	BEGIN
		EXEC DeleteWarehouseAndRelations @WarehouseID = @ID
	END

	Delete AccDocItem where RelationTable=9 and RelationTableID in (select ID from AccWarehouseDetails where WarehouseID = @WarehouseID)
--؟	Delete AccDocItem where RelationTable=10 and RelationTableID = @WarehouseID
	Delete AccWarehouseDetails where WarehouseID = @WarehouseID
	Delete AccWarehouse where ID = @WarehouseID

	COMMIT

END

GO

