begin tran

--Actualiza la informacion de la remesa
update D
set D.RemMintrans = O.IngresoId, D.EdoRemMin = '1'
FROM 
Trn_TraManifRem D
JOIN dbmuc.dbo.Remesas O on (D.TipRem='RMT' and D.IdCiaRem=substring(O.RemNroRemEmpresa,0,3) and D.Remesa=substring(O.RemNroRemEmpresa,3,len(O.RemNroRemEmpresa)))
where O.IngresoId <> 0

--Actualiza la informacion del cumplido
update D
set D.Nummintrans = O.IngresoId, D.EdoMintrans = '1'
FROM 
Trn_Tramanifiesto D
JOIN dbmuc.dbo.Manifiestos O on (D.TipDoc='MUC' and D.IdCia=substring(O.ManNumero_Empresa,0,3) and D.Manifiesto=substring(O.ManNumero_Empresa,3,len(O.ManNumero_Empresa)))
where O.IngresoId <> 0

--Actualiza el ingresoid en la tabla de anexo
update D
set D.Remmintrans = MR.RemMintrans, D.Manifmintrans = M.Nummintrans
FROM
Trn_Traremanexo D
join Trn_TraManifRem MR ON (MR.TipRem = D.TipDoc and MR.IdCiaRem = D.IdCia and MR.Remesa = D.NumOrden)
join Trn_Tramanifiesto M on (MR.TipDoc=M.TipDoc and MR.Manifiesto = M.Manifiesto and MR.IdCia = M.IdCia)
where MR.RemMinTrans <> 0 or M.Nummintrans <> 0

--Actualiza el ingresoid en el cumplido
update D
set D.NumRadicaMT = O.IngresoId
FROM
Trn_TraCumplido D
join dbmuc.dbo.Cumplidosman O on (D.TipMuc = 'MUC' and D.Manifiesto = substring(O.ManNumero_Empresa,3,len(O.ManNumero_Empresa)) and D.IdCiaMuc = substring(O.ManNumero_Empresa,0,3))
where O.IngresoId <> 0

select * FROM 
Trn_TraManifRem D
JOIN dbmuc.dbo.Remesas O on (D.TipRem='RMT' and D.IdCiaRem=substring(O.RemNroRemEmpresa,0,3) and D.Remesa=substring(O.RemNroRemEmpresa,3,len(O.RemNroRemEmpresa)))
where O.IngresoId <> 0

select * FROM 
Trn_Tramanifiesto D
JOIN dbmuc.dbo.Manifiestos O on (D.TipDoc='MUC' and D.IdCia=substring(O.ManNumero_Empresa,0,3) and D.Manifiesto=substring(O.ManNumero_Empresa,3,len(O.ManNumero_Empresa)))
where O.IngresoId <> 0

SELECT * FROM
Trn_Traremanexo D
join Trn_TraManifRem MR ON (MR.TipRem = D.TipDoc and MR.IdCiaRem = D.IdCia and MR.Remesa = D.NumOrden)
join Trn_Tramanifiesto M on (MR.TipDoc=M.TipDoc and MR.Manifiesto = M.Manifiesto and MR.IdCia = M.IdCia)
where MR.RemMinTrans <> 0 or M.Nummintrans <> 0

SELECT * FROM
Trn_TraCumplido D
join dbmuc.dbo.Cumplidosman O on (D.TipMuc = 'MUC' and D.Manifiesto = substring(O.ManNumero_Empresa,3,len(O.ManNumero_Empresa)) and D.IdCiaMuc = substring(O.ManNumero_Empresa,0,3))
where O.IngresoId <> 0

rollback tran


