DELETE FROM tssseg0001.dbo.CliGes
-- Traspasar incidencias de clientes a gestiones de clientes de Tesis
INSERT INTO tssseg0001.dbo.CliGes (
    CliId, CliGesNum, CliGesRes, CliGesDes, CliGesAltFec, CliGesUltFec, CliGesStatus, INC_Id
)
SELECT A.Fusion_OBJ_Id AS CliId,
ROW_NUMBER() OVER (PARTITION BY Fusion_OBJ_Id ORDER BY Fusion_OBJ_Id) AS CliGesNum,
SUBSTRING(A.INC_Descripcion,1,150) AS CliGesRes, SUBSTRING(A.INC_Descripcion,1,16) AS CliGesDes, A.INC_FC AS CliGesAltFec, A.INC_FM AS CliGesUltFec,
CASE A.INC_Situacion WHEN 1 THEN 'P' ELSE 'F' END AS CliGesStatus, A.INC_Id
FROM asurnet.dbo.INCIDENCIAS A
WHERE A.INC_TipoObj = 1
DELETE FROM tssseg0001.dbo.PolGes
-- Traspasar incidencias de pólizas a gestiones de pólizas de Tesis
INSERT INTO tssseg0001.dbo.PolGes (
    PolNumID, PolGesNum, PolGesRes, PolGesDes, PolGesAltFec, PolGesUltFec, PolGesStatus, INC_Id
)
SELECT A.Fusion_OBJ_Id AS PolNumID,
ROW_NUMBER() OVER (PARTITION BY Fusion_OBJ_Id ORDER BY Fusion_OBJ_Id) AS PolGesNum,
SUBSTRING(A.INC_Descripcion,1,150) AS PolGesRes, SUBSTRING(A.INC_Descripcion,1,16) AS PolGesDes, A.INC_FC AS PolGesAltFec, A.INC_FM AS PolGesUltFec,
CASE A.INC_Situacion WHEN 1 THEN 'P' ELSE 'F' END AS PolGesStatus, A.INC_Id
FROM asurnet.dbo.INCIDENCIAS A
WHERE A.INC_TipoObj = 7
DELETE FROM tssseg0001.dbo.RecGes
-- Traspasar incidencias de recibos a gestiones de recibos de Tesis
INSERT INTO tssseg0001.dbo.RecGes (
    RecSer, RecNumID, RecGesNum, RecGesRes, RecGesDes, RecGesAltFec, RecGesUltFec, RecGesStatus, INC_Id
)
SELECT SUBSTRING(A.Fusion_OBJ_Id, 1, CHARINDEX('/', A.Fusion_OBJ_Id) - 1) AS RecSer,
SUBSTRING(A.Fusion_OBJ_Id, CHARINDEX('/', A.Fusion_OBJ_Id) + 1, LEN(A.Fusion_OBJ_Id) - CHARINDEX('/', A.Fusion_OBJ_Id)) AS RecNumID,
ROW_NUMBER() OVER (PARTITION BY Fusion_OBJ_Id ORDER BY Fusion_OBJ_Id) AS RecGesNum,
SUBSTRING(A.INC_Descripcion,1,150) AS RecGesRes, SUBSTRING(A.INC_Descripcion,1,16) AS RecGesDes, A.INC_FC AS RecGesAltFec, A.INC_FM AS RecGesUltFec,
CASE A.INC_Situacion WHEN 1 THEN 'P' ELSE 'F' END AS RecGesStatus, A.INC_Id
FROM asurnet.dbo.INCIDENCIAS A
WHERE A.INC_TipoObj = 10
DELETE FROM tssseg0001.dbo.SinGes
-- Traspasar incidencias de siniestros a gestiones de siniestros de Tesis
INSERT INTO tssseg0001.dbo.SinGes (
    SinSer, SinNumID, SinGesNum, SinGesRes, SinGesDes, SinGesAltFec, SinGesUltFec, SinGesStatus, INC_Id, SinGesFec 
)
SELECT SUBSTRING(A.Fusion_OBJ_Id, 1, CHARINDEX('/', A.Fusion_OBJ_Id) - 1) AS SinSer,
SUBSTRING(A.Fusion_OBJ_Id, CHARINDEX('/', A.Fusion_OBJ_Id) + 1, LEN(A.Fusion_OBJ_Id) - CHARINDEX('/', A.Fusion_OBJ_Id)) AS SinNumID,
ROW_NUMBER() OVER (PARTITION BY Fusion_OBJ_Id ORDER BY Fusion_OBJ_Id) AS SinGesNum,
SUBSTRING(A.INC_Descripcion,1,150) AS SinGesRes, SUBSTRING(A.INC_Descripcion,1,16) AS SinGesDes, A.INC_FC AS SinGesAltFec, A.INC_FM AS SinGesUltFec,
CASE A.INC_Situacion WHEN 1 THEN 'P' ELSE 'F' END AS SinGesStatus, A.INC_Id, '1753-01-01' AS SinGesFec
FROM asurnet.dbo.INCIDENCIAS A
WHERE A.INC_TipoObj = 13