-- Actualizar tareas existentes en Asurnet
UPDATE A
SET A.Fusion_USR_Id_FC = B.Fusion_USR_Id_FC,
A.Fusion_USR_Id_FM = B.Fusion_USR_Id_FM,
A.Fusion_TAR_UsuarioDestino = B.Fusion_TAR_UsuarioDestino,
A.Fusion_TAR_UsrId_Resolucion = B.Fusion_TAR_UsrId_Resolucion,
A.Fusion_SIN_Id = B.Fusion_SIN_Id,
A.Fusion_OPT_Id = B.Fusion_OPT_Id,
A.Fusion_CLI_Id = B.Fusion_CLI_Id,
A.Fusion_CIA_Id = B.Fusion_CIA_Id,
A.Fusion_POL_Id = B.Fusion_POL_Id,
A.Fusion_REC_Id = B.Fusion_REC_Id
FROM asurnet.dbo.TAREASINC A
INNER JOIN (
     SELECT A.TAR_ID, B.IDTSS AS Fusion_USR_Id_FC, C.IDTSS AS Fusion_USR_Id_FM, D.IDTSS AS Fusion_TAR_UsuarioDestino, E.IDTSS AS Fusion_TAR_UsrId_Resolucion,
     F.IDTSS AS Fusion_SIN_Id, G.IDTSS AS Fusion_CLI_Id, H.IDTSS AS Fusion_CIA_Id, I.IDTSS AS Fusion_REC_Id, A.OPT_Id AS Fusion_OPT_Id, A.POL_Id AS Fusion_POL_Id
     FROM asurnet.dbo.TAREASINC A
     LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.USR_Id_FC = B.IDTras AND B.TableID = 'Usuarios'
     LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FM = C.IDTras AND C.TableID = 'Usuarios'
     LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.TAR_UsuarioDestino = D.IDTras AND D.TableID = 'Usuarios'
     LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.TAR_UsrId_Resolucion = E.IDTras AND E.TableID = 'Usuarios'
     LEFT JOIN tssseg0001.dbo.IDTraspasos F ON A.SIN_Id = F.IDTras AND F.TableID = 'Siniestros'
     LEFT JOIN tssseg0001.dbo.IDTraspasos G ON A.CLI_Id = G.IDTras AND G.TableID = 'Clientes'
     LEFT JOIN tssseg0001.dbo.IDTraspasos H ON A.CIA_Id = H.IDTras AND H.TableID = 'Companias'
     LEFT JOIN tssseg0001.dbo.IDTraspasos I ON A.REC_Id = I.IDTras AND I.TableID = 'Recibos'
) B ON A.TAR_ID = B.TAR_ID

-- Insertar tareas de visual en la tabla de Asurnet
INSERT INTO asurnet.dbo.TAREASINC (
     OBJ_Id, TAR_TipoObj, DTA_Id, TAR_Fecha, TAR_Hora, TAR_Observaciones, TAR_Situacion,
     TAR_UsuarioDestino, TAR_Prioridad, USR_Id_FC, USR_Id_FM, TAR_FC, TAR_FM,
     TAR_PrevisionResolucion, TAR_Resolucion, TAR_PrevisionDuracion, TAR_Duracion,
     TAR_TipoDuracion, TAR_ObserResolucion, TAR_Tipo, TAR_UsrId_Resolucion,
     TAR_PeticionId, SIN_Id, INC_Id, OPT_Id, CLI_Id, CIA_Id, POL_Id, REC_Id,
     TAR_Dependencia_Id, TAR_DiasDif, INA_Id, TAR_FechaAnulacion, TAR_TipoDiferencia,
     TAR_Diferencia, TAR_DestinoMismoUsuario, TAR_TipoIncidencia, GCN_ID, TAR_EntryId,
     Fusion_USR_Id_FC, Fusion_USR_Id_FM, Fusion_TAR_UsuarioDestino,
     Fusion_TAR_UsrId_Resolucion, Fusion_SIN_Id, Fusion_CLI_Id,
     Fusion_CIA_Id, Fusion_REC_Id, Fusion_OPT_Id, Fusion_POL_Id
)
SELECT A.OBJ_Id, A.TAR_TipoObj, A.DTA_Id, A.TAR_Fecha, A.TAR_Hora, A.TAR_Observaciones, A.TAR_Situacion, A.TAR_UsuarioDestino, A.TAR_Prioridad, A.USR_Id_FC,
A.USR_Id_FM, A.TAR_FC, A.TAR_FM, A.TAR_PrevisionResolucion, A.TAR_Resolucion, A.TAR_PrevisionDuracion, A.TAR_Duracion, A.TAR_TipoDuracion, A.TAR_ObserResolucion,
A.TAR_Tipo, A.TAR_UsrId_Resolucion, A.TAR_PeticionId, A.SIN_Id, A.INC_Id, A.OPT_Id, A.CLI_Id, A.CIA_Id, A.POL_Id, A.REC_Id, A.TAR_Dependencia_Id, A.TAR_DiasDif,
A.INA_Id, A.TAR_FechaAnulacion, A.TAR_TipoDiferencia, A.TAR_Diferencia, A.TAR_DestinoMismoUsuario,
AX.X.value('(TAR_TipoIncidencia/text())[1]', 'tinyint') as TAR_TipoIncidencia,
A.GCN_ID, A.TAR_EntryId, B.IDTSS AS Fusion_USR_Id_FC, C.IDTSS AS Fusion_USR_Id_FM, D.IDTSS AS Fusion_TAR_UsuarioDestino, E.IDTSS AS Fusion_TAR_UsrId_Resolucion,
F.IDTSS AS Fusion_SIN_Id, G.IDTSS AS Fusion_CLI_Id, H.IDTSS AS Fusion_CIA_Id, I.IDTSS AS Fusion_REC_Id, A.OPT_Id AS Fusion_OPT_Id, A.POL_Id AS Fusion_POL_Id
FROM vsegbas.dbo.TAREASINC A
INNER JOIN asurnet.dbo.INCIDENCIAS J ON J.INC_Id = A.OBJ_Id
LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.USR_Id_FC = B.IDTras AND B.TableID = 'Usuarios'
LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FM = C.IDTras AND C.TableID = 'Usuarios'
LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.TAR_UsuarioDestino = D.IDTras AND D.TableID = 'Usuarios'
LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.TAR_UsrId_Resolucion = E.IDTras AND E.TableID = 'Usuarios'
LEFT JOIN tssseg0001.dbo.IDTraspasos F ON A.SIN_Id = F.IDTras AND F.TableID = 'Siniestros'
LEFT JOIN tssseg0001.dbo.IDTraspasos G ON A.CLI_Id = G.IDTras AND G.TableID = 'Clientes'
LEFT JOIN tssseg0001.dbo.IDTraspasos H ON A.CIA_Id = H.IDTras AND H.TableID = 'Companias'
LEFT JOIN tssseg0001.dbo.IDTraspasos I ON A.REC_Id = I.IDTras AND I.TableID = 'Recibos'
CROSS APPLY (SELECT A.* for XML PATH(''), TYPE) AS AX(X)
WHERE A.TAR_TipoObj = 44