
CREATE PROCEDURE [dbo].[API_DocumentAfter]
@SyncDbId int,
 @OrgId int,
 @NewId int,
 @Sch_Id int
AS
BEGIN
	SET NOCOUNT ON;

	/*
declare @SyncDbId int;
declare @OrgId int;
declare @NewId int;
declare @Sch_Id int;
*/

update dbo.ApiDocuments set ErpId = @NewId where Id = @OrgId and ErpId =0;


-- elememmty oznaczanie
update dbo.ApiLines 
set ErpId= poz.ID
from dbo.PozycjeDokHan poz
where
ErpId=0 and
documentId = @OrgId 
and poz.Towar = itemId
and poz.Dokument = @NewId;



-- pobieramy HistId
declare @HistId int;
set @HistId   = (select Max(Id) from dbo.ChangeInfos where SourceTable = 'DokHandlowe');

INSERT INTO [dbo].[ApiSyncItems]
           ([SyncId],[ObjectSaveTS],[SchemaId],[ObjectType],[ObjectId],[Message],[Status],[TS], HistId)
     VALUES
           (@SyncDbId,[dbo].[ApiGetSec](CURRENT_TIMESTAMP),@Sch_Id,0, @OrgId,'',2,CURRENT_TIMESTAMP,@HistId);
           
END



