-- Reconfiguración Manual FDW Cambyretá
DROP SERVER IF EXISTS srv_703 CASCADE;
CREATE SERVER srv_703 
FOREIGN DATA WRAPPER postgres_fdw 
OPTIONS (host '10.0.12.5', port '5414', dbname 'sigem0703');

CREATE USER MAPPING FOR sigem_user 
SERVER srv_703 
OPTIONS (user 'postgres', password 'x25yvaga2018');

DROP SCHEMA IF EXISTS fdw_703 CASCADE;
CREATE SCHEMA fdw_703;
IMPORT FOREIGN SCHEMA public FROM SERVER srv_703 INTO fdw_703;

-- Crear vista definitiva (REGLA 23)
CREATE OR REPLACE VIEW public.vw_lotes_morosidad_703 AS 
SELECT 
    lot.*, 
    m.inm_ficha, 
    m.inm_ctacatastral, 
    m.trb_total_deuda, 
    m.trb_total_pago, 
    m.ultimo_pago 
FROM public.e703_lotes_activos lot 
LEFT JOIN fdw_703.v_liq_entidad_totalxobjeto m ON lot.snc_cuenta = REPLACE(m.inm_ctacatastral, '-', '');
