-- Estos UPDATE deben realizarse sólo para la correduría Antasur.
-- Sustituir la X (en EmpId = X) por el id de Antasur (204).

UPDATE ai SET ai.Fusion_CLI_ID = p.CliID
FROM TESISSEG.dbo.AS_INCIDENCIAS ai
INNER JOIN TESISSEG.dbo.Polizas p ON p.PolNumID = ai.Fusion_OBJ_ID AND p.EmpID = ai.EmpID
WHERE ai.EmpID = X
	AND ai.INC_TipoObj = 7 -- Pólizas
;

UPDATE ai SET ai.Fusion_CLI_ID = r.CliID
FROM TESISSEG.dbo.AS_INCIDENCIAS ai
INNER JOIN TESISSEG.dbo.Recibos r ON
	r.RecSer = SUBSTRING(ai.Fusion_OBJ_Id, 0, CHARINDEX('/', ai.Fusion_OBJ_Id, 0))
	AND r.RecNumID = SUBSTRING(ai.Fusion_OBJ_Id, CHARINDEX('/', ai.Fusion_OBJ_Id, 0) + 1, LEN(ai.Fusion_OBJ_Id))
	AND r.EmpID = ai.EmpID
WHERE ai.EmpID = X
	AND ai.INC_TipoObj = 10 -- Recibos
;

UPDATE ai SET ai.Fusion_CLI_ID = s.CliID
FROM TESISSEG.dbo.AS_INCIDENCIAS ai
INNER JOIN TESISSEG.dbo.Siniestros s ON
	s.SinSer = SUBSTRING(ai.Fusion_OBJ_Id, 0, CHARINDEX('/', ai.Fusion_OBJ_Id, 0))
	AND s.SinNumID = SUBSTRING(ai.Fusion_OBJ_Id, CHARINDEX('/', ai.Fusion_OBJ_Id, 0) + 1, LEN(ai.Fusion_OBJ_Id))
	AND s.EmpID = ai.EmpID
WHERE ai.EmpID = X
	AND ai.INC_TipoObj = 13 -- Siniestros
;

UPDATE ai SET ai.Fusion_CLI_ID = ai.Fusion_OBJ_Id
FROM TESISSEG.dbo.AS_INCIDENCIAS ai
WHERE ai.EmpID = X
	AND ai.INC_TipoObj = 1 -- Clientes
;
