text: ﻿CREATE PROCEDURE [bll].[Documents_Validate__Open]
  	@DefinitionId INT,
  	@Ids [dbo].[IndexedIdList] READONLY,
  	@Top INT = 200,
  	@UserId INT,
  	@IsError BIT OUTPUT
  AS
  BEGIN
  	SET NOCOUNT ON;
  	DECLARE @ValidationErrors [dbo].[ValidationErrorList];
  	DECLARE @Documents DocumentList, @DocumentLineDefinitionEntries DocumentLineDefinitionEntryList,
  			@Lines LineList, @Entries EntryList;
  
      -- Non Null Ids must exist
      INSERT INTO @ValidationErrors([Key], [ErrorName], [Argument0])
  	SELECT DISTINCT TOP (@Top)
  		'[' + CAST([Index] AS NVARCHAR (255)) + ']',
  		N'Error_TheDocumentWithId0WasNotFound',
  		CAST([Id] AS NVARCHAR (255))
      FROM @Ids
      WHERE Id <> 0
  	AND Id NOT IN (SELECT Id from [dbo].[Documents]);
  
  	IF EXISTS(SELECT * FROM @ValidationErrors) GOTO DONE
  
  	-- Cannot unpost it if it is not posted
  	INSERT INTO @ValidationErrors([Key], [ErrorName], [Argument0])
  	SELECT DISTINCT TOP (@Top)
  		'[' + CAST([Index] AS NVARCHAR (255)) + ']',
  		N'Error_DocumentIsNotInState0',
  		N'localize:Document_State_1'
  	FROM @Ids FE
  	JOIN dbo.Documents D ON FE.[Id] = D.[Id]
  	WHERE D.[State] <> 1;	
  
  	-- [C#] cannot open if the document posting date falls in an archived period.
  	INSERT INTO @ValidationErrors([Key], [ErrorName])
  	SELECT DISTINCT TOP (@Top)
  		'[' + CAST([Index] AS NVARCHAR (255)) + '].PostingDate',
  		N'Error_FallsinArchivedPeriod'
  	FROM @Ids FE
  	JOIN dbo.Documents D ON FE.[Id] = D.[Id]
  	WHERE D.[PostingDate] < (SELECT [ArchiveDate] FROM dbo.Settings)
  
  	INSERT INTO @Documents ([Index], [Id], [SerialNumber], [Clearance], [PostingDate], [PostingDateIsCommon], [Memo], [MemoIsCommon],
  		[CenterId], [CenterIsCommon], [AgentId], [AgentIsCommon], [NotedAgentId], [NotedAgentIsCommon],
  		[CurrencyId], [CurrencyIsCommon], [ExternalReference], [ExternalReferenceIsCommon], 
  		[ReferenceSourceId], [ReferenceSourceIsCommon], [InternalReference], [InternalReferenceIsCommon]
  	)
  	SELECT [Id], [Id], [SerialNumber], [Clearance], [PostingDate], [PostingDateIsCommon], [Memo], [MemoIsCommon],
  		[CenterId], [CenterIsCommon], [AgentId], [AgentIsCommon], [NotedAgentId], [NotedAgentIsCommon],
  		[CurrencyId], [CurrencyIsCommon], [ExternalReference], [ExternalReferenceIsCommon],
  		[ReferenceSourceId], [ReferenceSourceIsCommon], [InternalReference], [InternalReferenceIsCommon]
  	FROM dbo.Documents
  	WHERE [Id] IN (SELECT [Id] FROM @Ids)
  
  	INSERT INTO @DocumentLineDefinitionEntries(
  		[Index], [DocumentIndex], [Id], [LineDefinitionId], [EntryIndex], [PostingDate], [PostingDateIsCommon], [Memo], [MemoIsCommon],
  		[CurrencyId], [CurrencyIsCommon], [CenterId], [CenterIsCommon], [AgentId], [AgentIsCommon],
  		[NotedAgentId], [NotedAgentIsCommon], [ResourceId], [ResourceIsCommon], [Quantity], [QuantityIsCommon], [UnitId], [UnitIsCommon],
  		[Time1], [Time1IsCommon], [Time2], [Time2IsCommon], [ExternalReference], [ExternalReferenceIsCommon], [InternalReference],
  		[InternalReferenceIsCommon])
  	SELECT 		[Id], [DocumentId], [Id], [LineDefinitionId], [EntryIndex], [PostingDate], [PostingDateIsCommon], [Memo], [MemoIsCommon],
  		[CurrencyId], [CurrencyIsCommon], [CenterId], [CenterIsCommon], [AgentId], [AgentIsCommon],
  		[NotedAgentId], [NotedAgentIsCommon], [ResourceId], [ResourceIsCommon], [Quantity], [QuantityIsCommon], [UnitId], [UnitIsCommon],
  		[Time1], [Time1IsCommon], [Time2], [Time2IsCommon], [ExternalReference], [ExternalReferenceIsCommon], [InternalReference],
  		[InternalReferenceIsCommon]
  	FROM DocumentLineDefinitionEntries
  	WHERE [DocumentId] IN (SELECT [Id] FROM @Ids)
  	AND [LineDefinitionId]  IN (SELECT [Id] FROM map.LineDefinitions() WHERE [HasWorkflow] = 0);
  
  	-- Verify that workflow-less lines in Events can be in state draft
  	INSERT INTO @Lines(
  			[Index],	[DocumentIndex],[Id],	[DefinitionId], [PostingDate],		[Memo])
  	SELECT	L.[Index],	FE.[Index],	L.[Id], L.[DefinitionId], L.[PostingDate], L.[Memo]
  	FROM dbo.Lines L
  	JOIN @Ids FE ON L.[DocumentId] = FE.[Id]
  	--JOIN map.Documents() D ON FE.[Id] = D.[Id]
  	--WHERE D.[LastLineState] = 4 -- event
  	AND L.[DefinitionId] IN (SELECT [Id] FROM map.LineDefinitions() WHERE [HasWorkflow] = 0);
  	
  	INSERT INTO @Entries (
  	[Index], [LineIndex], [DocumentIndex], [Id],
  	[Direction], [AccountId], [CurrencyId], [AgentId], [NotedAgentId], [ResourceId], [CenterId],
  	[EntryTypeId], [MonetaryValue], [Quantity], [UnitId], [Value], [Time1],
  	[Time2], [ExternalReference], [ReferenceSourceId], [InternalReference], [NotedAgentName],
  	[NotedAmount], [NotedDate])
  	SELECT
  	E.[Index],L.[Index],L.[DocumentIndex],E.[Id],
  	E.[Direction],E.[AccountId],E.[CurrencyId],E.[AgentId],E.[NotedAgentId],E.[ResourceId],E.[CenterId],
  	E.[EntryTypeId], E.[MonetaryValue],E.[Quantity],E.[UnitId],E.[Value],E.[Time1],
  	E.[Time2],E.[ExternalReference],E.[ReferenceSourceId], E.[InternalReference],E.[NotedAgentName],
  	E.[NotedAmount],E.[NotedDate]
  	FROM dbo.Entries E
  	JOIN @Lines L ON E.[LineId] = L.[Id];
  
  	INSERT INTO @ValidationErrors
  	EXEC [bll].[Lines_Validate__State_Data]
  		@Documents = @Documents, 
  		@DocumentLineDefinitionEntries = @DocumentLineDefinitionEntries,
  		@Lines = @Lines, 
  		@Entries = @Entries, 
  		@State = 0, 
  		@Top = @Top, 
  		@IsError = @IsError OUTPUT;
  
  DONE:
  
  	-- Set @IsError
  	SET @IsError = CASE WHEN EXISTS(SELECT 1 FROM @ValidationErrors) THEN 1 ELSE 0 END;
  
  	SELECT TOP (@Top) * FROM @ValidationErrors;
  END;