-- Actualizar campos nuevos de la tabla INCIDENCIASCLIENTES de Asurnet de los Clientes
UPDATE A
SET A.Fusion_CLI_Id = B.Fusion_CLI_Id,
A.Fusion_OBJ_Id = B.Fusion_OBJ_Id,
A.Fusion_USR_Id_FC = B.Fusion_USR_Id_FC,
A.Fusion_USR_Id_FM = B.Fusion_USR_Id_FM,
A.Fusion_INC_UsuarioCierre = B.Fusion_INC_UsuarioCierre
FROM asurnet.dbo.INCIDENCIASCLIENTES A
INNER JOIN (
    SELECT A.INC_Id, B.IDTSS AS Fusion_CLI_Id, F.IDTSS AS Fusion_OBJ_Id, C.IDTSS AS Fusion_USR_Id_FC, D.IDTSS AS Fusion_USR_Id_FM, E.IDTSS AS Fusion_INC_UsuarioCierre
    FROM asurnet.dbo.INCIDENCIASCLIENTES A
    INNER JOIN tssseg0001.dbo.IDTraspasos F ON A.OBJ_Id = F.IDTras COLLATE Modern_Spanish_CI_AI AND F.TableID = 'Clientes'
    LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.CLI_Id = B.IDTras AND B.TableID = 'Clientes'
    LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FC = C.IDTras AND C.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.USR_Id_FM = D.IDTras AND D.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.INC_UsuarioCierre = E.IDTras AND E.TableID = 'Usuarios'
    WHERE A.INC_TipoObj = 1
) B ON A.INC_Id = B.INC_Id

-- Actualizar campos nuevos de la tabla INCIDENCIASCLIENTES de Asurnet de los Colaboradores
UPDATE A
SET A.Fusion_CLI_Id = B.Fusion_CLI_Id,
A.Fusion_OBJ_Id = B.Fusion_OBJ_Id,
A.Fusion_USR_Id_FC = B.Fusion_USR_Id_FC,
A.Fusion_USR_Id_FM = B.Fusion_USR_Id_FM,
A.Fusion_INC_UsuarioCierre = B.Fusion_INC_UsuarioCierre
FROM asurnet.dbo.INCIDENCIASCLIENTES A
INNER JOIN (
    SELECT A.INC_Id, B.IDTSS AS Fusion_CLI_Id, F.IDTSS AS Fusion_OBJ_Id, C.IDTSS AS Fusion_USR_Id_FC, D.IDTSS AS Fusion_USR_Id_FM, E.IDTSS AS Fusion_INC_UsuarioCierre
    FROM asurnet.dbo.INCIDENCIASCLIENTES A
    INNER JOIN tssseg0001.dbo.IDTraspasos F ON A.OBJ_Id = F.IDTras COLLATE Modern_Spanish_CI_AI AND F.TableID = 'Colaboradores'
    LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.CLI_Id = B.IDTras AND B.TableID = 'Clientes'
    LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FC = C.IDTras AND C.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.USR_Id_FM = D.IDTras AND D.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.INC_UsuarioCierre = E.IDTras AND E.TableID = 'Usuarios'
    WHERE A.INC_TipoObj = 2
) B ON A.INC_Id = B.INC_Id

-- Actualizar campos nuevos de la tabla INCIDENCIASCLIENTES de Asurnet de las Pólizas
UPDATE A
SET A.Fusion_CLI_Id = B.Fusion_CLI_Id,
A.Fusion_OBJ_Id = B.Fusion_OBJ_Id,
A.Fusion_USR_Id_FC = B.Fusion_USR_Id_FC,
A.Fusion_USR_Id_FM = B.Fusion_USR_Id_FM,
A.Fusion_INC_UsuarioCierre = B.Fusion_INC_UsuarioCierre
FROM asurnet.dbo.INCIDENCIASCLIENTES A
INNER JOIN (
    SELECT A.INC_Id, B.IDTSS AS Fusion_CLI_Id, A.OBJ_Id AS Fusion_OBJ_Id, C.IDTSS AS Fusion_USR_Id_FC, D.IDTSS AS Fusion_USR_Id_FM, E.IDTSS AS Fusion_INC_UsuarioCierre
    FROM asurnet.dbo.INCIDENCIASCLIENTES A
    LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.CLI_Id = B.IDTras AND B.TableID = 'Clientes'
    LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FC = C.IDTras AND C.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.USR_Id_FM = D.IDTras AND D.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.INC_UsuarioCierre = E.IDTras AND E.TableID = 'Usuarios'
    WHERE A.INC_TipoObj = 7
) B ON A.INC_Id = B.INC_Id

-- Actualizar campos nuevos de la tabla INCIDENCIASCLIENTES de Asurnet de los Recibos
UPDATE A
SET A.Fusion_CLI_Id = B.Fusion_CLI_Id,
A.Fusion_OBJ_Id = B.Fusion_OBJ_Id,
A.Fusion_USR_Id_FC = B.Fusion_USR_Id_FC,
A.Fusion_USR_Id_FM = B.Fusion_USR_Id_FM,
A.Fusion_INC_UsuarioCierre = B.Fusion_INC_UsuarioCierre
FROM asurnet.dbo.INCIDENCIASCLIENTES A
INNER JOIN (
    SELECT A.INC_Id, B.IDTSS AS Fusion_CLI_Id, F.IDTSS AS Fusion_OBJ_Id, C.IDTSS AS Fusion_USR_Id_FC, D.IDTSS AS Fusion_USR_Id_FM, E.IDTSS AS Fusion_INC_UsuarioCierre
    FROM asurnet.dbo.INCIDENCIASCLIENTES A
    INNER JOIN tssseg0001.dbo.IDTraspasos F ON A.OBJ_Id = F.IDTras COLLATE Modern_Spanish_CI_AI AND F.TableID = 'Recibos'
    LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.CLI_Id = B.IDTras AND B.TableID = 'Clientes'
    LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FC = C.IDTras AND C.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.USR_Id_FM = D.IDTras AND D.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.INC_UsuarioCierre = E.IDTras AND E.TableID = 'Usuarios'
    WHERE A.INC_TipoObj = 10
) B ON A.INC_Id = B.INC_Id

-- Actualizar campos nuevos de la tabla INCIDENCIASCLIENTES de Asurnet de los Siniestros
UPDATE A
SET A.Fusion_CLI_Id = B.Fusion_CLI_Id,
A.Fusion_OBJ_Id = B.Fusion_OBJ_Id,
A.Fusion_USR_Id_FC = B.Fusion_USR_Id_FC,
A.Fusion_USR_Id_FM = B.Fusion_USR_Id_FM,
A.Fusion_INC_UsuarioCierre = B.Fusion_INC_UsuarioCierre
FROM asurnet.dbo.INCIDENCIASCLIENTES A
INNER JOIN (
    SELECT A.INC_Id, B.IDTSS AS Fusion_CLI_Id, F.IDTSS AS Fusion_OBJ_Id, C.IDTSS AS Fusion_USR_Id_FC, D.IDTSS AS Fusion_USR_Id_FM, E.IDTSS AS Fusion_INC_UsuarioCierre
    FROM asurnet.dbo.INCIDENCIASCLIENTES A
    INNER JOIN tssseg0001.dbo.IDTraspasos F ON A.OBJ_Id = F.IDTras COLLATE Modern_Spanish_CI_AI AND F.TableID = 'Siniestros'
    LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.CLI_Id = B.IDTras AND B.TableID = 'Clientes'
    LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FC = C.IDTras AND C.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.USR_Id_FM = D.IDTras AND D.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.INC_UsuarioCierre = E.IDTras AND E.TableID = 'Usuarios'
    WHERE A.INC_TipoObj = 13
) B ON A.INC_Id = B.INC_Id

-- Actualizar campos nuevos de la tabla INCIDENCIASCLIENTES de Asurnet de las Oportunidades
UPDATE A
SET A.Fusion_CLI_Id = B.Fusion_CLI_Id,
A.Fusion_OBJ_Id = B.Fusion_OBJ_Id,
A.Fusion_USR_Id_FC = B.Fusion_USR_Id_FC,
A.Fusion_USR_Id_FM = B.Fusion_USR_Id_FM,
A.Fusion_INC_UsuarioCierre = B.Fusion_INC_UsuarioCierre
FROM asurnet.dbo.INCIDENCIASCLIENTES A
INNER JOIN (
    SELECT A.INC_Id, B.IDTSS AS Fusion_CLI_Id, A.OBJ_Id AS Fusion_OBJ_Id, C.IDTSS AS Fusion_USR_Id_FC, D.IDTSS AS Fusion_USR_Id_FM, E.IDTSS AS Fusion_INC_UsuarioCierre
    FROM asurnet.dbo.INCIDENCIASCLIENTES A
    LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.CLI_Id = B.IDTras AND B.TableID = 'Clientes'
    LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FC = C.IDTras AND C.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.USR_Id_FM = D.IDTras AND D.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.INC_UsuarioCierre = E.IDTras AND E.TableID = 'Usuarios'
    WHERE A.INC_TipoObj = 160
) B ON A.INC_Id = B.INC_Id

-- Actualizar campos nuevos de la tabla INCIDENCIASCLIENTES de Asurnet de los cambios de mediación
UPDATE A
SET A.Fusion_CLI_Id = B.Fusion_CLI_Id,
A.Fusion_OBJ_Id = B.Fusion_OBJ_Id,
A.Fusion_USR_Id_FC = B.Fusion_USR_Id_FC,
A.Fusion_USR_Id_FM = B.Fusion_USR_Id_FM,
A.Fusion_INC_UsuarioCierre = B.Fusion_INC_UsuarioCierre
FROM asurnet.dbo.INCIDENCIASCLIENTES A
INNER JOIN (
    SELECT A.INC_Id, B.IDTSS AS Fusion_CLI_Id, A.OBJ_Id AS Fusion_OBJ_Id, C.IDTSS AS Fusion_USR_Id_FC, D.IDTSS AS Fusion_USR_Id_FM, E.IDTSS AS Fusion_INC_UsuarioCierre
    FROM asurnet.dbo.INCIDENCIASCLIENTES A
    LEFT JOIN tssseg0001.dbo.IDTraspasos B ON A.CLI_Id = B.IDTras AND B.TableID = 'Clientes'
    LEFT JOIN tssseg0001.dbo.IDTraspasos C ON A.USR_Id_FC = C.IDTras AND C.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos D ON A.USR_Id_FM = D.IDTras AND D.TableID = 'Usuarios'
    LEFT JOIN tssseg0001.dbo.IDTraspasos E ON A.INC_UsuarioCierre = E.IDTras AND E.TableID = 'Usuarios'
    WHERE A.INC_TipoObj = 1000
) B ON A.INC_Id = B.INC_Id

-- Actualizar tareas de clientes 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.TAREASINCCLIENTES 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.TAREASINCCLIENTES 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


