-- Insertar datos desde las incidencias de Visual a Asurnet
SET IDENTITY_INSERT asurnet.dbo.INCIDENCIAS ON;

-- Insertar incidencias relacionados con clientes
INSERT INTO asurnet.dbo.INCIDENCIAS (
   INC_Id, OBJ_Id, INC_Descripcion, INC_TipoObj, INC_FC, INC_FM, USR_Id_FC, USR_Id_FM, INC_ReferenciaBorrar, AGN_Id, INC_Referencia, INC_Permanente, CDI_Id, INC_CliPublicable,
   INC_GenPublicable, RFR_Id, INC_Cierre, INC_Situacion, INC_Manual, CLI_Id, INC_TipoIncAviso, INC_UsuarioCierre, INC_TextoCierre, INC_IdAvisoPadre, SUP_Id, INC_FechaPrevista, INA_Id,
   Fusion_CLI_Id, Fusion_OBJ_Id, Fusion_USR_Id_FC, Fusion_USR_Id_FM, Fusion_INC_UsuarioCierre
)
SELECT A.INC_Id, A.OBJ_Id, A.INC_Descripcion, A.INC_TipoObj, A.INC_FC, A.INC_FM, A.USR_Id_FC, A.USR_Id_FM, A.INC_ReferenciaBorrar, A.AGN_Id, A.INC_Referencia, A.INC_Permanente, A.CDI_Id, A.INC_CliPublicable,
A.INC_GenPublicable, A.RFR_Id, A.INC_Cierre, A.INC_Situacion, A.INC_Manual, A.CLI_Id, A.INC_TipoIncAviso, A.INC_UsuarioCierre, A.INC_TextoCierre, A.INC_IdAvisoPadre, A.SUP_Id, A.INC_FechaPrevista, A.INA_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 vsegbas.dbo.INCIDENCIAS A
INNER JOIN tssseg0001.dbo.IDTraspasos F ON A.OBJ_Id = F.IDTras 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'
LEFT JOIN asurnet.dbo.INCIDENCIAS G ON G.INC_Id = A.INC_Id
WHERE A.INC_TipoObj = 1
AND G.INC_Id IS NULL;

-- Insertar incidencias relacionados con colaboradores
INSERT INTO asurnet.dbo.INCIDENCIAS (
   INC_Id, OBJ_Id, INC_Descripcion, INC_TipoObj, INC_FC, INC_FM, USR_Id_FC, USR_Id_FM, INC_ReferenciaBorrar, AGN_Id, INC_Referencia, INC_Permanente, CDI_Id, INC_CliPublicable,
   INC_GenPublicable, RFR_Id, INC_Cierre, INC_Situacion, INC_Manual, CLI_Id, INC_TipoIncAviso, INC_UsuarioCierre, INC_TextoCierre, INC_IdAvisoPadre, SUP_Id, INC_FechaPrevista, INA_Id,
   Fusion_CLI_Id, Fusion_OBJ_Id, Fusion_USR_Id_FC, Fusion_USR_Id_FM, Fusion_INC_UsuarioCierre
)
SELECT A.INC_Id, A.OBJ_Id, A.INC_Descripcion, A.INC_TipoObj, A.INC_FC, A.INC_FM, A.USR_Id_FC, A.USR_Id_FM, A.INC_ReferenciaBorrar, A.AGN_Id, A.INC_Referencia, A.INC_Permanente, A.CDI_Id, A.INC_CliPublicable,
A.INC_GenPublicable, A.RFR_Id, A.INC_Cierre, A.INC_Situacion, A.INC_Manual, A.CLI_Id, A.INC_TipoIncAviso, A.INC_UsuarioCierre, A.INC_TextoCierre, A.INC_IdAvisoPadre, A.SUP_Id, A.INC_FechaPrevista, A.INA_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 vsegbas.dbo.INCIDENCIAS A
INNER JOIN tssseg0001.dbo.IDTraspasos F ON A.OBJ_Id = F.IDTras 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'
LEFT JOIN asurnet.dbo.INCIDENCIAS G ON G.INC_Id = A.INC_Id
WHERE A.INC_TipoObj = 2
AND G.INC_Id IS NULL;

-- Insertar incidencias relacionados con pólizas
INSERT INTO asurnet.dbo.INCIDENCIAS (
   INC_Id, OBJ_Id, INC_Descripcion, INC_TipoObj, INC_FC, INC_FM, USR_Id_FC, USR_Id_FM, INC_ReferenciaBorrar, AGN_Id, INC_Referencia, INC_Permanente, CDI_Id, INC_CliPublicable,
   INC_GenPublicable, RFR_Id, INC_Cierre, INC_Situacion, INC_Manual, CLI_Id, INC_TipoIncAviso, INC_UsuarioCierre, INC_TextoCierre, INC_IdAvisoPadre, SUP_Id, INC_FechaPrevista, INA_Id,
   Fusion_CLI_Id, Fusion_OBJ_Id, Fusion_USR_Id_FC, Fusion_USR_Id_FM, Fusion_INC_UsuarioCierre
)
SELECT A.INC_Id, A.OBJ_Id, A.INC_Descripcion, A.INC_TipoObj, A.INC_FC, A.INC_FM, A.USR_Id_FC, A.USR_Id_FM, A.INC_ReferenciaBorrar, A.AGN_Id, A.INC_Referencia, A.INC_Permanente, A.CDI_Id, A.INC_CliPublicable,
A.INC_GenPublicable, A.RFR_Id, A.INC_Cierre, A.INC_Situacion, A.INC_Manual, A.CLI_Id, A.INC_TipoIncAviso, A.INC_UsuarioCierre, A.INC_TextoCierre, A.INC_IdAvisoPadre, A.SUP_Id, A.INC_FechaPrevista, A.INA_Id,
B.IDTSS AS Fusion_CLI_Id, F.PolNumID 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 vsegbas.dbo.INCIDENCIAS A
INNER JOIN tssseg0001.dbo.Polizas F ON A.OBJ_Id = F.PolNumID
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'
LEFT JOIN asurnet.dbo.INCIDENCIAS G ON G.INC_Id = A.INC_Id
WHERE A.INC_TipoObj = 7
AND G.INC_Id IS NULL;

-- Insertar incidencias relacionados con recibos
INSERT INTO asurnet.dbo.INCIDENCIAS (
   INC_Id, OBJ_Id, INC_Descripcion, INC_TipoObj, INC_FC, INC_FM, USR_Id_FC, USR_Id_FM, INC_ReferenciaBorrar, AGN_Id, INC_Referencia, INC_Permanente, CDI_Id, INC_CliPublicable,
   INC_GenPublicable, RFR_Id, INC_Cierre, INC_Situacion, INC_Manual, CLI_Id, INC_TipoIncAviso, INC_UsuarioCierre, INC_TextoCierre, INC_IdAvisoPadre, SUP_Id, INC_FechaPrevista, INA_Id,
   Fusion_CLI_Id, Fusion_OBJ_Id, Fusion_USR_Id_FC, Fusion_USR_Id_FM, Fusion_INC_UsuarioCierre
)
SELECT A.INC_Id, A.OBJ_Id, A.INC_Descripcion, A.INC_TipoObj, A.INC_FC, A.INC_FM, A.USR_Id_FC, A.USR_Id_FM, A.INC_ReferenciaBorrar, A.AGN_Id, A.INC_Referencia, A.INC_Permanente, A.CDI_Id, A.INC_CliPublicable,
A.INC_GenPublicable, A.RFR_Id, A.INC_Cierre, A.INC_Situacion, A.INC_Manual, A.CLI_Id, A.INC_TipoIncAviso, A.INC_UsuarioCierre, A.INC_TextoCierre, A.INC_IdAvisoPadre, A.SUP_Id, A.INC_FechaPrevista, A.INA_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 vsegbas.dbo.INCIDENCIAS A
INNER JOIN tssseg0001.dbo.IDTraspasos F ON A.OBJ_Id = F.IDTras 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'
LEFT JOIN asurnet.dbo.INCIDENCIAS G ON G.INC_Id = A.INC_Id
WHERE A.INC_TipoObj = 10
AND G.INC_Id IS NULL;

-- Insertar incidencias relacionados con siniestros
INSERT INTO asurnet.dbo.INCIDENCIAS (
   INC_Id, OBJ_Id, INC_Descripcion, INC_TipoObj, INC_FC, INC_FM, USR_Id_FC, USR_Id_FM, INC_ReferenciaBorrar, AGN_Id, INC_Referencia, INC_Permanente, CDI_Id, INC_CliPublicable,
   INC_GenPublicable, RFR_Id, INC_Cierre, INC_Situacion, INC_Manual, CLI_Id, INC_TipoIncAviso, INC_UsuarioCierre, INC_TextoCierre, INC_IdAvisoPadre, SUP_Id, INC_FechaPrevista, INA_Id,
   Fusion_CLI_Id, Fusion_OBJ_Id, Fusion_USR_Id_FC, Fusion_USR_Id_FM, Fusion_INC_UsuarioCierre
)
SELECT A.INC_Id, A.OBJ_Id, A.INC_Descripcion, A.INC_TipoObj, A.INC_FC, A.INC_FM, A.USR_Id_FC, A.USR_Id_FM, A.INC_ReferenciaBorrar, A.AGN_Id, A.INC_Referencia, A.INC_Permanente, A.CDI_Id, A.INC_CliPublicable,
A.INC_GenPublicable, A.RFR_Id, A.INC_Cierre, A.INC_Situacion, A.INC_Manual, A.CLI_Id, A.INC_TipoIncAviso, A.INC_UsuarioCierre, A.INC_TextoCierre, A.INC_IdAvisoPadre, A.SUP_Id, A.INC_FechaPrevista, A.INA_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 vsegbas.dbo.INCIDENCIAS A
INNER JOIN tssseg0001.dbo.IDTraspasos F ON A.OBJ_Id = F.IDTras 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'
LEFT JOIN asurnet.dbo.INCIDENCIAS G ON G.INC_Id = A.INC_Id
WHERE A.INC_TipoObj = 13
AND G.INC_Id IS NULL;

-- Insertar incidencias relacionados con oportunidades
INSERT INTO asurnet.dbo.INCIDENCIAS (
   INC_Id, OBJ_Id, INC_Descripcion, INC_TipoObj, INC_FC, INC_FM, USR_Id_FC, USR_Id_FM, INC_ReferenciaBorrar, AGN_Id, INC_Referencia, INC_Permanente, CDI_Id, INC_CliPublicable,
   INC_GenPublicable, RFR_Id, INC_Cierre, INC_Situacion, INC_Manual, CLI_Id, INC_TipoIncAviso, INC_UsuarioCierre, INC_TextoCierre, INC_IdAvisoPadre, SUP_Id, INC_FechaPrevista, INA_Id,
   Fusion_CLI_Id, Fusion_OBJ_Id, Fusion_USR_Id_FC, Fusion_USR_Id_FM, Fusion_INC_UsuarioCierre
)
SELECT A.INC_Id, A.OBJ_Id, A.INC_Descripcion, A.INC_TipoObj, A.INC_FC, A.INC_FM, A.USR_Id_FC, A.USR_Id_FM, A.INC_ReferenciaBorrar, A.AGN_Id, A.INC_Referencia, A.INC_Permanente, A.CDI_Id, A.INC_CliPublicable,
A.INC_GenPublicable, A.RFR_Id, A.INC_Cierre, A.INC_Situacion, A.INC_Manual, A.CLI_Id, A.INC_TipoIncAviso, A.INC_UsuarioCierre, A.INC_TextoCierre, A.INC_IdAvisoPadre, A.SUP_Id, A.INC_FechaPrevista, A.INA_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 vsegbas.dbo.INCIDENCIAS 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'
LEFT JOIN asurnet.dbo.INCIDENCIAS G ON G.INC_Id = A.INC_Id
WHERE A.INC_TipoObj = 160
AND G.INC_Id IS NULL;

SET IDENTITY_INSERT asurnet.dbo.INCIDENCIAS OFF;