return: text
lang: plpgsql
src: |
  DECLARE	
  	
  BEGIN
  	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_rss_rum_ventil') THEN
  		CREATE TEMP TABLE w_rss_rum_ventil
  		(
  		    rss_id bigint,
  			unite_medicale_id bigint,
  			type_eclatement char(1),
  			unite_medicale_code text,
  			unite_medicale_texte text,
  			unite_medicale_type_autorisation text,
  			medecin_rss_id bigint,
  			nb_rum numeric(5,0),
  			unite_medicale_1_id numeric,
  			unite_medicale_v_id bigint,
  			unite_medicale_v_code text,
  			unite_medicale_v_texte text
  		); 
  	END IF;
  	
  	TRUNCATE w_rss_rum_ventil;
  	
  	INSERT INTO w_rss_rum_ventil
  	SELECT p_rss_rum.rss_id,
  		p_rss_rum.unite_medicale_id,
  		t_unites_medicales.type_eclatement,
  		t_unites_medicales.code AS unite_medicale_code,
  		t_unites_medicales.texte AS unite_medicale_texte,
  		t_unites_medicales.type_autorisation AS unite_medicale_type_autorisation,
  		p_rss.medecin_rss_id, 
  		p_rss.nb_rum,
  		(MIN(Array[p_rss_rum_un.no_rum,p_rss_rum_un.unite_medicale_id]))[2] AS unite_medicale_1_id,
  		0::bigint AS unite_medicale_v_id,
  		''::text AS unite_medicale_v_code,
  		''::text AS unite_medicale_v_texte
  	FROM pmsi.p_rss_rum
  	JOIN pmsi.p_rss ON p_rss.oid = p_rss_rum.rss_id
  	JOIN pmsi.t_unites_medicales ON p_rss_rum.unite_medicale_id = t_unites_medicales.oid
  	LEFT JOIN pmsi.p_rss_rum p_rss_rum_un ON p_rss_rum.rss_id = p_rss_rum_un.rss_id AND p_rss_rum.unite_medicale_id <> p_rss_rum_un.unite_medicale_id
  	LEFT JOIN pmsi.t_unites_medicales t_unites_medicales_un ON p_rss_rum_un.unite_medicale_id = t_unites_medicales_un.oid
  	WHERE 
  		(t_unites_medicales.type_eclatement IN ('1') AND p_rss_rum_un.rss_id IS NOT NULL AND COALESCE(t_unites_medicales_un.type_eclatement,'') = '' OR
  		t_unites_medicales.type_eclatement IN ('1') AND p_rss_rum_un.rss_id IS NULL AND p_rss.nb_rum = 1 OR
  		t_unites_medicales.type_eclatement IN ('2') AND medecin_rss_id <> 0
  		)
  	GROUP BY 1,2,3,4,5,6,7,8;
  	UPDATE w_rss_rum_ventil SET
  		unite_medicale_v_code = unite_medicale_code || '-' || t_unites_medicales.code,
  		unite_medicale_v_texte = unite_medicale_texte || '-' || t_unites_medicales.texte
  	FROM pmsi.t_unites_medicales 
  	WHERE w_rss_rum_ventil.type_eclatement = '1' AND
  		w_rss_rum_ventil.unite_medicale_1_id = t_unites_medicales.oid;
  	UPDATE w_rss_rum_ventil SET
  				unite_medicale_v_code = unite_medicale_code || '-MONO',
  				unite_medicale_v_texte = unite_medicale_texte || '-MonoRUM'
  			WHERE w_rss_rum_ventil.type_eclatement = '1' AND
  				nb_rum = 1;
  	UPDATE w_rss_rum_ventil SET
  		unite_medicale_v_code = replace(unite_medicale_code,'-MONO','') || '-' || replace(t_specialites_medecin.code,'.',''),
  		unite_medicale_v_texte = replace(unite_medicale_texte,'-MonoRUM','') || '-' || t_specialites_medecin.texte
  	FROM pmsi.t_medecins t_medecins_pmsi
  	JOIN base.t_medecins ON t_medecins_pmsi.medecin_id = t_medecins.oid
  	JOIN base.t_specialites_medecin ON t_medecins.specialite_id = t_specialites_medecin.oid
  	WHERE w_rss_rum_ventil.type_eclatement = '2' AND
  		w_rss_rum_ventil.medecin_rss_id = t_medecins_pmsi.oid;	
  		
  	INSERT INTO pmsi.t_unites_medicales (code, texte, type_autorisation)
  	SELECT
  		unite_medicale_v_code,
  		unite_medicale_v_texte,
  		unite_medicale_type_autorisation
  	FROM w_rss_rum_ventil
  	WHERE unite_medicale_v_code NOT IN (SELECT code FROM pmsi.t_unites_medicales)
  	GROUP BY 1,2,3;
  	UPDATE w_rss_rum_ventil SET
  		unite_medicale_v_id = t_unites_medicales.oid
  	FROM pmsi.t_unites_medicales 
  	WHERE w_rss_rum_ventil.unite_medicale_v_code = t_unites_medicales.code;
  	UPDATE pmsi.t_unites_medicales 
  	SET type_autorisation = unite_medicale_type_autorisation
  	FROM 
  		(SELECT unite_medicale_v_id, unite_medicale_type_autorisation
  		FROM w_rss_rum_ventil
  		GROUP BY 1,2
  		) subview
  	WHERE t_unites_medicales.oid = unite_medicale_v_id AND
  		type_autorisation IS DISTINCT FROM unite_medicale_type_autorisation;
  	UPDATE pmsi.p_rss
  			SET unite_medicale_principale_id = unite_medicale_v_id
  			FROM w_rss_rum_ventil
  			WHERE w_rss_rum_ventil.rss_id = p_rss.oid AND
  				w_rss_rum_ventil.unite_medicale_id = p_rss.unite_medicale_principale_id;
  	UPDATE pmsi.p_rss_rum
  	SET unite_medicale_id = unite_medicale_v_id
  	FROM w_rss_rum_ventil
  	WHERE w_rss_rum_ventil.rss_id = p_rss_rum.rss_id AND
  		w_rss_rum_ventil.unite_medicale_id = p_rss_rum.unite_medicale_id;
  	UPDATE pmsi.p_rsf_detail
  	SET unite_medicale_id = unite_medicale_v_id
  	FROM w_rss_rum_ventil
  	WHERE w_rss_rum_ventil.rss_id = p_rsf_detail.rss_id AND
  		w_rss_rum_ventil.unite_medicale_id = p_rsf_detail.unite_medicale_id AND
  		p_rsf_detail.est_ligne_rum = '1';
  	UPDATE pmsi.p_rss_diagnostics
  	SET unite_medicale_id = unite_medicale_v_id
  	FROM w_rss_rum_ventil
  	WHERE w_rss_rum_ventil.rss_id = p_rss_diagnostics.rss_id AND
  		w_rss_rum_ventil.unite_medicale_id = p_rss_diagnostics.unite_medicale_id;
  	UPDATE pmsi.p_rss_actes
  	SET unite_medicale_id = unite_medicale_v_id
  	FROM w_rss_rum_ventil
  	WHERE w_rss_rum_ventil.rss_id = p_rss_actes.rss_id AND
  		w_rss_rum_ventil.unite_medicale_id = p_rss_actes.unite_medicale_id;	
  	INSERT INTO pmsi.p_oids (code_table, oid) 
  	SELECT DISTINCT 'um', unite_medicale_v_id FROM w_rss_rum_ventil WHERE unite_medicale_v_id NOT IN (SELECT oid FROM pmsi.p_oids WHERE code_table = 'um');
  
  	UPDATE  pmsi.t_unites_medicales SET
                    type_autorisation = t_unites_medicales_4.type_autorisation,
                    date_effet_autorisation = t_unites_medicales_4.date_effet_autorisation,
                    mode_hospitalisation = t_unites_medicales_4.mode_hospitalisation,
                    nb_lits = t_unites_medicales_4.nb_lits
                FROM pmsi.t_unites_medicales t_unites_medicales_4 
                WHERE length(t_unites_medicales.code) > 4 AND
                    split_part(t_unites_medicales.code,'-',1) = t_unites_medicales_4.code AND
                    length(t_unites_medicales_4.code) <= 4 AND
                    (
                    t_unites_medicales.type_autorisation IS DISTINCT FROM t_unites_medicales_4.type_autorisation OR
                    t_unites_medicales.date_effet_autorisation IS DISTINCT FROM t_unites_medicales_4.date_effet_autorisation OR
                    t_unites_medicales.mode_hospitalisation IS DISTINCT FROM t_unites_medicales_4.mode_hospitalisation OR
                    t_unites_medicales.nb_lits IS DISTINCT FROM t_unites_medicales_4.nb_lits
                    );
  		
  	RETURN 'OK';
  END;