----------------------------------------------
-- En cas de que el document ja sigui al ECM2
----------------------------------------------

declare @asnum int = 18769
declare @transid int
declare @ecm2absentry int

select @transid=TransId from OJDT where Number=@asnum and YEAR(RefDate)=2021
select @ecm2absentry=AbsEntry from ECM2 where SrcObjType=30 and SrcObjAbs=@transid

print @transid
print @ecm2absentry

--delete from ECM2 where AbsEntry=62901
update ECM2 set ActStatus='O',ActMessage='Cancelado manual',UpdateDate=GETDATE(), UpdateTS=130200, CreateDate=GETDATE(),CreateTS=130100 
where AbsEntry=@ecm2absentry

select * from OJDT where Number=20968 and YEAR(RefDate)=2021
select * from ECM2 where SrcObjAbs=201161

-------------------------------------------------------
-- En el cas de que el document NO estigui al ECM2
-------------------------------------------------------

-- 46=Pagaments OVPM PP
-- 30=Assentaments OJDT AS
declare @objecte int = 30
declare @docentry int = 201151
declare @acttype nvarchar = 'O'

-- 1: Avançar el comptador de l'objecte 1730000000.

declare @comptador int
select @comptador=AutoKey from ONNM where ObjectCode='1730000000'
update ONNM set AutoKey=@comptador+1 where ObjectCode='1730000000'

-- 2: Afegir el registre a la taula d'enviaments electrònics.

insert into ECM2 
(AbsEntry,Code,SrcObjType,SrcObjAbs,ActType,ActStatus,ActMessage,Submits) 
values 
(@comptador,'S',@objecte,@docentry,@acttype,'O','Cancelado manual',1)