return: text
lang: plpgsql
src: |
  DECLARE 
  	_module_pmsimco TEXT;
  	
  BEGIN
  	-- règles ?
  	IF NOT EXISTS (SELECT * FROM activite.t_budget_cle_rule LIMIT 1) THEN
  		return 'Pas de règle';
  	END IF;
  	
  	-- pmsi mco ?
  	IF EXISTS (SELECT * FROM pg_tables WHERE schemaname = 'pmsi' AND tablename = 'p_rss') THEN
  		_module_pmsimco = '1';
  	ELSE
  		_module_pmsimco = '0';
  	END IF;
  	-- Réplication du paramétrage
  	-- INSERT INTO activite.t_budget_cle (oid,code_original,code,texte,texte_court)
  	-- SELECT oid,code_original,code,texte,texte_court
  	-- FROM base.t_budget_cle_activite
  	-- WHERE oid NOT IN (SELECT oid FROM activite.t_budget_cle);
  	-- UPDATE activite.t_budget_cle
  	-- SET code = t_budget_cle_activite.code,
  	-- 	texte = t_budget_cle_activite.texte,
  	-- 	texte_court = t_budget_cle_activite.texte_court
  	-- FROM base.t_budget_cle_activite
  	-- WHERE t_budget_cle_activite.oid = t_budget_cle.oid AND
  	-- 	(
  	-- 	t_budget_cle.code IS DISTINCT FROM t_budget_cle_activite.code OR
  	-- 	t_budget_cle.texte IS DISTINCT FROM t_budget_cle_activite.texte OR
  	-- 	t_budget_cle.texte_court IS DISTINCT FROM t_budget_cle_activite.texte_court
  	-- 	);
  	-- Ménage dans les règles
  	DELETE FROM activite.t_budget_cle_rule
  	WHERE budget_cle_id NOT IN (SELECT oid FROM activite.t_budget_cle)
  	;
  
  	-- Sejours et RSS à Traiter
  	
  	DROP SEQUENCE IF EXISTS w_sejours_rss_sequence;
  	CREATE SEQUENCE w_sejours_rss_sequence;
  	DROP TABLE IF EXISTS w_sejours_rss;
  
  	DROP TABLE IF EXISTS w_sejour_lieu;
  	CREATE TEMP TABLE w_sejour_lieu AS
  	SELECT no_sejour, lieu_sortie_id AS lieu_id
  	FROM activite.p_sejours
  	GROUP BY 1,2
  	;
  
  	INSERT INTO w_sejour_lieu
  	SELECT p_mouvements_sejour.no_sejour, lieu_id
  	FROM activite.p_mouvements_sejour
  	JOIN activite.p_sejours ON p_mouvements_sejour.no_sejour = p_sejours.no_sejour AND
  		lieu_id <> lieu_sortie_id
  	GROUP BY 1,2
  	;
  
  	
  	IF _module_pmsimco = '1' THEN
  		-- si pmsi mco
  		CREATE TEMP TABLE w_sejours_rss AS
  		SELECT
  			nextval('w_sejours_rss_sequence'::regclass) AS oid,
  			COALESCE(t_finess.oid,0) AS finess_id,
  			p_sejours.oid AS sejour_id, 
  			p_sejours.no_sejour AS no_sejour,
  			p_sejours.lieu_sortie_id,
  			w_sejour_lieu.lieu_id, 
  			COALESCE(p_rss.oid,0) AS rss_id,
  			diagnostic_principal_id,
  			diagnostic_relie_id,
  			NULL::bigint[] AS diagnostic_associe_liste_rule_id,
  			NULL::bigint[] AS acte_liste_rule_id,
  			NULL::bigint[] AS rubrique_liste_rule_id,
  			p_rss.ghm_id AS ghm_pmsi_id,
  			p_sejours.ghm_id AS ghm_activite_id,
  			COALESCE(medecin_rss_id,0) AS medecin_rss_id,
  			p_sejours.medecin_sejour_id,
  			COALESCE(t_medecins_pmsi.specialite_id,0) AS specialite_rss_id,
  			COALESCE(t_medecins_base.specialite_id,0) AS specialite_sejour_id,
  			type_sejour,
  			service_facturation_id,
  			unite_fonctionnelle_id AS uf_id,
  			activite_id,
  			0::bigint AS budget_cle_id,
  			0::bigint AS budget_cle_priorite
  		FROM activite.p_sejours
  			JOIN w_sejour_lieu ON p_sejours.no_sejour = w_sejour_lieu.no_sejour
  			JOIN activite.t_lieux ON w_sejour_lieu.lieu_id = t_lieux.oid
  			LEFT JOIN pmsi.p_rss ON no_sejour_administratif = p_sejours.no_sejour AND p_sejours.date_sortie = p_rss.date_sortie AND p_rss.etat = ''
  			LEFT JOIN pmsi.t_medecins t_medecins_pmsi ON medecin_rss_id =  t_medecins_pmsi.oid
  			LEFT JOIN activite.t_medecins_administratifs ON p_sejours.medecin_sejour_id = t_medecins_administratifs.oid
  			LEFT JOIN base.t_medecins t_medecins_base ON t_medecins_administratifs.medecin_id = t_medecins_base.oid
  			LEFT JOIN base.t_finess ON p_rss.finess = t_finess.code;
  		INSERT INTO w_sejours_rss
  		SELECT 
  			nextval('w_sejours_rss_sequence'::regclass) AS oid,
  			COALESCE(t_finess.oid,0) AS finess_id,
  			0 AS sejour_id, 
  			'' AS no_sejour, 
  			0::bigint AS lieu_sortie_id,
  			0::bigint AS lieu_id, 
  			p_rss.oid AS rss_id,
  			diagnostic_principal_id,
  			diagnostic_relie_id,
  			NULL::bigint[] AS diagnostic_associe_liste_rule_id,
  			NULL::bigint[] AS acte_liste_rule_id,
  			NULL::bigint[] AS rubrique_liste_rule_id,
  			p_rss.ghm_id AS ghm_pmsi_id,
  			0 AS ghm_activite_id,
  			medecin_rss_id,
  			0::bigint AS medecin_sejour_id,
  			COALESCE(t_medecins_pmsi.specialite_id,0) AS specialite_rss_id,
  			0::bigint AS specialite_sejour_id,
  			''::text AS type_sejour,
  			0::bigint AS service_facturation_id,
  			0::bigint AS uf_id,
  			0::bigint AS activite_id,
  			0::bigint AS budget_cle_id,
  			0::bigint AS budget_cle_priorite
  		FROM pmsi.p_rss
  			LEFT JOIN pmsi.t_medecins t_medecins_pmsi ON medecin_rss_id =  t_medecins_pmsi.oid
  			LEFT JOIN base.t_finess ON p_rss.finess = t_finess.code
  		WHERE date_sortie >= '20160101' AND p_rss.oid NOT IN (SELECt rss_id FROM w_sejours_rss);
  		UPDATE w_sejours_rss SET
  			medecin_rss_id = t_medecins.oid,
  			specialite_rss_id = t_medecins.specialite_id
  		FROM activite.t_medecins_administratifs_c 
  			JOIN pmsi.t_medecins ON t_medecins_administratifs_c.adm_code = t_medecins.code
  		WHERE medecin_sejour_id = t_medecins_administratifs_c.oid AND
  			t_medecins.oid <> 0 AND
  			(t_medecins.oid IS DISTINCT FROM medecin_rss_id OR
  			specialite_rss_id IS DISTINCT FROM t_medecins.specialite_id)
  			;
  	ELSE
  		-- si pas pmsi mco
  		CREATE TEMP TABLE w_sejours_rss AS
  		SELECT
  			nextval('w_sejours_rss_sequence'::regclass) AS oid,
  			0 AS finess_id,
  			p_sejours.oid AS sejour_id, 
  			p_sejours.no_sejour AS no_sejour, 
  			p_sejours.lieu_sortie_id,
  			w_sejour_lieu.lieu_id, 
  			0 AS rss_id,
  			0::bigint AS diagnostic_principal_id,
  			0::bigint AS diagnostic_relie_id,
  			NULL::bigint[] AS diagnostic_associe_liste_rule_id,
  			NULL::bigint[] AS acte_liste_rule_id,
  			NULL::bigint[] AS rubrique_liste_rule_id,
  			0::bigint AS ghm_pmsi_id,
  			p_sejours.ghm_id AS ghm_activite_id,
  			0 AS medecin_rss_id,
  			medecin_sejour_id,
  			0 AS specialite_rss_id,
  			COALESCE(t_medecins_base.specialite_id,0) AS specialite_sejour_id,
  			type_sejour,
  			service_facturation_id,
  			unite_fonctionnelle_id AS uf_id,
  			activite_id,
  			0::bigint AS budget_cle_id,
  			0::bigint AS budget_cle_priorite
  		FROM activite.p_sejours
  			JOIN w_sejour_lieu ON p_sejours.no_sejour = w_sejour_lieu.no_sejour
  			JOIN activite.t_lieux ON w_sejour_lieu.lieu_id = t_lieux.oid
  			LEFT JOIN activite.t_medecins_administratifs ON p_sejours.medecin_sejour_id = t_medecins_administratifs.oid
  			LEFT JOIN base.t_medecins t_medecins_base ON t_medecins_administratifs.medecin_id = t_medecins_base.oid;
  	END IF;
  	UPDATE w_sejours_rss SET
  		medecin_rss_id = 0,
  		specialite_rss_id = 0
  	WHERE medecin_sejour_id <> 0;
  	UPDATE w_sejours_rss
  	SET finess_id = (SELECT MAX(finess_id) FROM w_sejours_rss WHERE finess_id <> 0)
  	WHERE finess_id = 0 AND (SELECT MAX(finess_id) FROM w_sejours_rss WHERE finess_id <> 0) <> 0;
  	UPDATE w_sejours_rss
  	SET finess_id = (SELECT MAX(oid) FROM base.t_finess)
  	WHERE finess_id = 0;
  	CREATE INDEX w_sejours_rss_i0 ON w_sejours_rss USING btree (oid);
  	CREATE INDEX w_sejours_rss_i1 ON w_sejours_rss USING btree (rss_id);
  	CREATE INDEX w_sejours_rss_i2 ON w_sejours_rss USING btree (sejour_id);
  	CREATE INDEX w_sejours_rss_i3 ON w_sejours_rss USING btree (no_sejour);
  		
  	-- Traduction des règles en oid
  	DROP TABLE IF EXISTS w_budget_cle_rule;
  	CREATE TEMP TABLE w_budget_cle_rule AS
  	SELECT
  		t_budget_cle_rule.oid,
  		budget_cle_id,
  		CASE WHEN priorite <> 0 THEN priorite WHEN liste_diagnostic_principal <> '' THEN 90000 ELSE 999999 END AS priorite,
  		CASE WHEN liste_finess <> '' THEN '1' ELSE '0' END AS a_liste_finess,
  		string_to_array(replace(liste_finess,'*','%'),' ') AS liste_finess,
  		NULL::bigint[] AS liste_finess_id ,
  		CASE WHEN liste_diagnostic_principal <> '' THEN '1' ELSE '0' END AS a_liste_diagnostic_principal,
  		string_to_array(replace(liste_diagnostic_principal,'*','%'),' ') AS liste_diagnostic_principal,
  		NULL::bigint[] AS liste_diagnostic_principal_id ,
  		CASE WHEN liste_diagnostic_relie <> '' THEN '1' ELSE '0' END AS a_liste_diagnostic_relie,
  		string_to_array(replace(liste_diagnostic_relie,'*','%'),' ') AS liste_diagnostic_relie,
  		NULL::bigint[] AS liste_diagnostic_relie_id ,
  		CASE WHEN liste_diagnostic_associe <> '' THEN '1' ELSE '0' END AS a_liste_diagnostic_associe,
  		string_to_array(replace(liste_diagnostic_associe,'*','%'),' ') AS liste_diagnostic_associe,
  		NULL::bigint[] AS liste_diagnostic_associe_id ,
  		CASE WHEN liste_acte <> '' THEN '1' ELSE '0' END AS a_liste_acte,
  		string_to_array(replace(liste_acte,'*','%'),' ') AS liste_acte,
  		NULL::bigint[] AS liste_acte_pmsi_id ,
  		NULL::bigint[] AS liste_acte_base_id ,
  		CASE WHEN liste_ghm <> '' THEN '1' ELSE '0' END AS a_liste_ghm,
  		string_to_array(replace(liste_ghm,'*','%'),' ') AS liste_ghm,
  		NULL::bigint[] AS liste_ghm_pmsi_id,
  		NULL::bigint[] AS liste_ghm_activite_id,
  		liste_ghm AS liste_ghm_origine,
  		CASE WHEN liste_sauf_ghm <> '' THEN '1' ELSE '0' END AS a_liste_sauf_ghm,
  		string_to_array(replace(liste_sauf_ghm,'*','%'),' ') AS liste_sauf_ghm,
  		NULL::bigint[] AS liste_sauf_ghm_pmsi_id ,
  		NULL::bigint[] AS liste_sauf_ghm_activite_id ,
  		liste_sauf_ghm AS liste_sauf_ghm_origine,
  		CASE WHEN liste_type_sejour <> '' THEN '1' ELSE '0' END AS a_liste_type_sejour,
  		string_to_array(replace(liste_type_sejour,'*','%'),' ') AS liste_type_sejour,
  		NULL::text[] AS liste_type_sejour_id ,
  		CASE WHEN liste_service <> '' THEN '1' ELSE '0' END AS a_liste_service,
  		string_to_array(replace(liste_service,'*','%'),' ') AS liste_service,
  		NULL::bigint[] AS liste_service_id ,
  		CASE WHEN liste_uf <> '' THEN '1' ELSE '0' END AS a_liste_uf,
  		string_to_array(replace(liste_uf,'*','%'),' ') AS liste_uf,
  		NULL::bigint[] AS liste_uf_id ,
  		CASE WHEN liste_activite <> '' THEN '1' ELSE '0' END AS a_liste_activite,
  		string_to_array(replace(liste_activite,'*','%'),' ') AS liste_activite,
  		NULL::bigint[] AS liste_activite_id ,
  		CASE WHEN liste_medecin <> '' THEN '1' ELSE '0' END AS a_liste_medecin,
  		string_to_array(CASE WHEN liste_medecin NOT LIKE '%**%' THEN replace(liste_medecin,'*','%') ELSE liste_medecin END,' ') AS liste_medecin,
  		NULL::bigint[] AS liste_medecin_pmsi_id ,
  		NULL::bigint[] AS liste_medecin_activite_id ,
  		CASE WHEN liste_specialite <> '' THEN '1' ELSE '0' END AS a_liste_specialite,
  		string_to_array(replace(liste_specialite,'*','%'),' ') AS liste_specialite,
  		NULL::bigint[] AS liste_specialite_pmsi_id ,
  		NULL::bigint[] AS liste_specialite_activite_id ,
  		CASE WHEN liste_rubrique <> '' THEN '1' ELSE '0' END AS a_liste_rubrique,
  		string_to_array(replace(liste_rubrique,'*','%'),' ') AS liste_rubrique,
  		NULL::bigint[] AS liste_rubrique_id
  	FROM activite.t_budget_cle_rule
  	JOIN activite.t_budget_cle ON budget_cle_id = t_budget_cle.oid 
  	ORDER BY priorite, budget_cle_id;
  	UPDATE w_budget_cle_rule 
  	SET liste_finess_id = (SELECT base.cti_group_array3(oid) FROM base.t_finess WHERE code LIKE ANY (liste_finess))
  	WHERE a_liste_finess = '1';	
  	
  	IF _module_pmsimco = '1' THEN
  		UPDATE w_budget_cle_rule 
  		SET liste_diagnostic_principal_id = (SELECT base.cti_group_array3(oid) FROM pmsi.t_diagnostics WHERE code LIKE ANY (liste_diagnostic_principal))
  		WHERE a_liste_diagnostic_principal = '1';
  		UPDATE w_budget_cle_rule 
  		SET liste_diagnostic_relie_id = (SELECT base.cti_group_array3(oid) FROM pmsi.t_diagnostics WHERE code LIKE ANY (liste_diagnostic_relie))
  		WHERE a_liste_diagnostic_relie = '1';
  		UPDATE w_budget_cle_rule 
  		SET liste_diagnostic_associe_id = (SELECT base.cti_group_array3(oid) FROM pmsi.t_diagnostics WHERE code LIKE ANY (liste_diagnostic_associe))
  		WHERE a_liste_diagnostic_associe = '1';
  		UPDATE w_budget_cle_rule 
  		SET liste_acte_pmsi_id = (SELECT base.cti_group_array3(oid) FROM pmsi.t_actes WHERE code LIKE ANY (liste_acte))
  		WHERE a_liste_acte = '1';
  		DROP TABLE IF EXISTS w_budget_cle_rule_actes_pmsi;
  		CREATE TEMP TABLE w_budget_cle_rule_actes_pmsi AS
  		SELECT t_actes.oid AS rule_acte_id, w_budget_cle_rule.oid AS rule_id
  		FROM pmsi.t_actes
  		JOIN w_budget_cle_rule ON code LIKE ANY (liste_acte);
  		CREATE INDEX w_budget_cle_rule_actes_pmsi_i1 ON w_budget_cle_rule_actes_pmsi USING btree (rule_acte_id);
  	END IF;
  	
  	
  	DROP TABLE IF EXISTS w_budget_cle_rule_actes_base;
  	CREATE TEMP TABLE w_budget_cle_rule_actes_base AS
  	SELECT t_actes.oid AS rule_acte_id, w_budget_cle_rule.oid AS rule_id
  	FROM base.t_actes
  	JOIN w_budget_cle_rule ON code LIKE ANY (liste_acte);
  	
  	CREATE INDEX w_budget_cle_rule_actes_base_i1 ON w_budget_cle_rule_actes_base USING btree (rule_acte_id);
  	DROP TABLE IF EXISTS w_budget_cle_rule_rubriques;
  	CREATE TEMP TABLE w_budget_cle_rule_rubriques AS
  	SELECT t_rubriques_facturation.oid AS rule_rubrique_id, w_budget_cle_rule.oid AS rule_id
  	FROM activite.t_rubriques_facturation
  	JOIN w_budget_cle_rule ON code LIKE ANY (liste_rubrique);
  	
  	CREATE INDEX w_budget_cle_rule_rubriques_i1 ON w_budget_cle_rule_rubriques USING btree (rule_rubrique_id);
  	
  	UPDATE w_budget_cle_rule 
  	SET liste_acte_base_id = (SELECT base.cti_group_array3(oid) FROM base.t_actes WHERE code LIKE ANY (liste_acte))
  	WHERE a_liste_acte = '1'
  	;
  	
  	IF _module_pmsimco = '1' THEN
  		UPDATE w_budget_cle_rule 
  		SET liste_ghm_pmsi_id = (SELECT base.cti_group_array3(oid) FROM pmsi.t_ghm WHERE code LIKE ANY (liste_ghm))
  		WHERE a_liste_ghm = '1';
  	END IF;
  	
  	UPDATE w_budget_cle_rule 
  	SET liste_ghm_activite_id = (SELECT base.cti_group_array3(ghm_id) FROM activite.t_ghm_c WHERE ghm_code LIKE ANY (liste_ghm))
  	WHERE a_liste_ghm = '1'
  	;
  	IF _module_pmsimco = '1' THEN
  		UPDATE w_budget_cle_rule 
  		SET liste_sauf_ghm_pmsi_id = (SELECT base.cti_group_array3(oid) FROM pmsi.t_ghm WHERE code LIKE ANY (liste_sauf_ghm))
  		WHERE a_liste_sauf_ghm = '1';
  	END IF;
  	
  	UPDATE w_budget_cle_rule 
  	SET liste_sauf_ghm_activite_id = (SELECT base.cti_group_array3(ghm_id) FROM activite.t_ghm_c WHERE ghm_code LIKE ANY (liste_sauf_ghm))
  	WHERE a_liste_sauf_ghm = '1'
  	;
  	IF _module_pmsimco = '1' THEN
  		UPDATE w_budget_cle_rule 
  		SET liste_medecin_pmsi_id = (
  			SELECT base.cti_group_array3(t_medecins_pmsi.oid) 
  			FROM pmsi.t_medecins t_medecins_pmsi 
  			JOIN base.t_medecins ON t_medecins_pmsi.medecin_reference_id = t_medecins.oid
  			WHERE t_medecins_pmsi.oid <> 0 AND t_medecins_pmsi.code <> '' AND t_medecins_pmsi.code LIKE ANY (liste_medecin) AND liste_medecin[1] <> '(R)' OR
  				t_medecins.oid <> 0 AND t_medecins.code <> '' AND t_medecins.code LIKE ANY (liste_medecin) AND liste_medecin[1] = '(R)'
  			)
  		WHERE a_liste_medecin = '1';
  	END IF;
  	
  	UPDATE w_budget_cle_rule 
  	SET liste_medecin_activite_id = (
  		SELECT base.cti_group_array3(t_medecins_administratifs.oid) 
  		FROM activite.t_medecins_administratifs 
  		JOIN base.t_medecins ON t_medecins_administratifs.medecin_id = t_medecins.oid
  		WHERE t_medecins_administratifs.oid <> 0 AND t_medecins_administratifs.code <> '' AND t_medecins_administratifs.code LIKE ANY (liste_medecin) AND liste_medecin[1] <> '(R)'  OR
  			t_medecins.oid <> 0 AND t_medecins.code <> '' AND t_medecins.code LIKE ANY (liste_medecin) AND liste_medecin[1] = '(R)' 
  		)
  	WHERE a_liste_medecin = '1'
  	;
  	IF _module_pmsimco = '1' THEN
  		UPDATE w_budget_cle_rule 
  		SET liste_specialite_pmsi_id = (SELECT base.cti_group_array3(oid) FROM pmsi.t_specialites_medecin WHERE code LIKE ANY (liste_specialite))
  		WHERE a_liste_specialite = '1';
  	END IF;
  	
  	UPDATE w_budget_cle_rule 
  	SET liste_specialite_activite_id = (SELECT base.cti_group_array3(oid) FROM base.t_specialites_medecin WHERE code LIKE ANY (liste_specialite))
  	WHERE a_liste_specialite = '1'
  	;
  	UPDATE w_budget_cle_rule 
  	SET liste_type_sejour_id = (SELECT base.cti_group_array3(code) FROM activite.t_type_sejour WHERE code <> '' AND code LIKE ANY (liste_type_sejour))
  	WHERE a_liste_type_sejour = '1'
  	;
  	UPDATE w_budget_cle_rule 
  	SET liste_service_id = (SELECT base.cti_group_array3(oid) FROM activite.t_services_facturation WHERE code LIKE ANY (liste_service))
  	WHERE a_liste_service = '1'
  	;
  	UPDATE w_budget_cle_rule 
  	SET liste_uf_id = (SELECT base.cti_group_array3(oid) FROM activite.t_unites_fonctionnelles WHERE code LIKE ANY (liste_uf))
  	WHERE a_liste_uf = '1'
  	;
  	UPDATE w_budget_cle_rule 
  	SET liste_activite_id = (SELECT base.cti_group_array3(oid) FROM activite.t_activites WHERE code LIKE ANY (liste_activite))
  	WHERE a_liste_activite = '1'
  	;
  	UPDATE w_budget_cle_rule 
  	SET liste_rubrique_id = (SELECT base.cti_group_array3(oid) FROM activite.t_rubriques_facturation WHERE code LIKE ANY (liste_rubrique))
  	WHERE a_liste_rubrique = '1'
  	;
  	-- Association cles aux rubriques
  	
  	DROP TABLE IF EXISTS w_factures_rubriques;
  	CREATE TEMP TABLE w_factures_rubriques AS
  	SELECT
  			no_facture, rule_id
  		FROM activite.p_factures_lignes_c
  		JOIN w_budget_cle_rule_rubriques ON p_factures_lignes_c.rubrique_facturation_id = rule_rubrique_id
  	UNION
  	SELECT
  			no_facture, rule_id
  		FROM activite.p_factures_lignes_non_facturees_c
  		JOIN w_budget_cle_rule_rubriques ON p_factures_lignes_non_facturees_c.rubrique_facturation_id = rule_rubrique_id
  		;
  	CREATE INDEX  w_factures_rubriques_i1 ON  w_factures_rubriques USING btree (no_facture);
  	
  	DROP TABLE IF EXISTS w_budget_cle_rubriques;
  	CREATE TEMP TABLE w_budget_cle_rubriques AS
  	
  	SELECT oid, base.cti_group_array3(rubrique_rule_id) AS rubrique_liste_rule_id
  	FROM
  	(
  		SELECT
  			w_sejours_rss.oid, rule_id AS rubrique_rule_id
  		FROM w_factures_rubriques
  		JOIN activite.p_factures ON w_factures_rubriques.no_facture = p_factures.no_facture
  		JOIN w_sejours_rss ON p_factures.no_sejour = w_sejours_rss.no_sejour
  	) subview
  	GROUP BY 1;
  	ALTER TABLE w_budget_cle_rubriques ADD CONSTRAINT w_budget_cle_rubriques_pkey PRIMARY KEY(oid);
  	UPDATE w_sejours_rss
  	SET rubrique_liste_rule_id = w_budget_cle_rubriques.rubrique_liste_rule_id
  	FROM w_budget_cle_rubriques
  	WHERE w_budget_cle_rubriques.oid = w_sejours_rss.oid;
  	
  	
  	-- Association cles aux actes
  	
  	DROP TABLE IF EXISTS w_factures_actes;
  	CREATE TEMP TABLE w_factures_actes AS
  	SELECT
  			no_facture, rule_id
  		FROM activite.p_factures_lignes_h
  		JOIN w_budget_cle_rule_actes_base ON p_factures_lignes_h.acte_id = rule_acte_id
  		WHERE activite_ccam <> '4'
  	UNION
  	SELECT
  			no_facture, rule_id
  		FROM activite.p_factures_lignes_non_facturees_h
  		JOIN w_budget_cle_rule_actes_base ON p_factures_lignes_non_facturees_h.acte_id = rule_acte_id
  		WHERE activite_ccam <> '4';
  	CREATE INDEX  w_factures_actes_i1 ON  w_factures_actes USING btree (no_facture);
  	
  	DROP TABLE IF EXISTS w_budget_cle_actes;
  	IF _module_pmsimco = '1' THEN
  		CREATE TEMP TABLE w_budget_cle_actes AS
  		SELECT oid, base.cti_group_array3(acte_rule_id) AS acte_liste_rule_id
  		FROM
  		(
  			SELECT
  				w_sejours_rss.oid, rule_id AS acte_rule_id
  			FROM pmsi.p_rss_actes
  			JOIN w_sejours_rss ON p_rss_actes.rss_id = w_sejours_rss.rss_id
  			JOIN w_budget_cle_rule_actes_pmsi ON p_rss_actes.acte_id = rule_acte_id
  			UNION
  			SELECT
  				w_sejours_rss.oid, rule_id AS acte_rule_id
  			FROM w_factures_actes
  			JOIN activite.p_factures ON w_factures_actes.no_facture = p_factures.no_facture
  			JOIN w_sejours_rss ON p_factures.no_sejour = w_sejours_rss.no_sejour
  			
  		) subview
  		GROUP BY 1;
  	ELSE
  		CREATE TEMP TABLE w_budget_cle_actes AS
  		SELECT oid, base.cti_group_array3(acte_rule_id) AS acte_liste_rule_id
  		FROM
  		(
  			SELECT
  				w_sejours_rss.oid, rule_id AS acte_rule_id
  			FROM w_factures_actes
  			JOIN activite.p_factures ON w_factures_actes.no_facture = p_factures.no_facture
  			JOIN w_sejours_rss ON p_factures.no_sejour = w_sejours_rss.no_sejour
  			
  		) subview
  		GROUP BY 1;
  	END IF;
  	ALTER TABLE w_budget_cle_actes ADD CONSTRAINT w_budget_cle_actes_pkey PRIMARY KEY(oid);
  	UPDATE w_sejours_rss
  	SET acte_liste_rule_id = w_budget_cle_actes.acte_liste_rule_id
  	FROM w_budget_cle_actes
  	WHERE w_budget_cle_actes.oid = w_sejours_rss.oid;
  	
  	-- Diagnostics
  	IF _module_pmsimco = '1' THEN
  		DROP TABLE IF EXISTS w_budget_cle_diagnostics;
  		CREATE TEMP TABLE w_budget_cle_diagnostics AS
  		SELECT
  			w_sejours_rss.oid, base.cti_group_array3(w_budget_cle_rule.oid) AS diagnostic_associe_liste_rule_id
  		FROM pmsi.p_rss_diagnostics
  		JOIN w_budget_cle_rule ON type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_id = ANY (liste_diagnostic_associe_id)
  		JOIN w_sejours_rss ON p_rss_diagnostics.rss_id = w_sejours_rss.rss_id
  		WHERE a_liste_diagnostic_associe <> '0'
  		GROUP BY 1;
  		ALTER TABLE w_budget_cle_diagnostics ADD CONSTRAINT w_budget_cle_diagnostics_pkey PRIMARY KEY(oid);
  		UPDATE w_sejours_rss
  		SET diagnostic_associe_liste_rule_id = w_budget_cle_diagnostics.diagnostic_associe_liste_rule_id
  		FROM w_budget_cle_diagnostics
  		WHERE w_budget_cle_diagnostics.oid = w_sejours_rss.oid;
  	END IF;
  	
  	-- Mise à jour des cles venant des données du PMSI
  	DROP TABLE IF EXISTS w_sejours_budget_cle;
  	CREATE TEMP TABLE w_sejours_budget_cle AS
  	SELECT w_sejours_rss.oid, 
  		MIN(priorite)+1 AS budget_cle_priorite,
  		(MIN(ARRAY[priorite,w_budget_cle_rule.budget_cle_id]))[2] AS budget_cle_id
  	FROM w_sejours_rss
  	JOIN w_budget_cle_rule ON 
  		(a_liste_finess = '0' OR finess_id = ANY (liste_finess_id)) AND
  		(a_liste_medecin = '0' OR medecin_rss_id = ANY (liste_medecin_pmsi_id)) AND
  		(a_liste_type_sejour = '0') AND
  		(a_liste_service = '0') AND
  		(a_liste_uf = '0') AND
  		(a_liste_activite = '0') AND
  		(a_liste_specialite = '0' OR specialite_rss_id = ANY (liste_specialite_pmsi_id)) AND
  		(a_liste_diagnostic_principal = '0' OR diagnostic_principal_id = ANY (liste_diagnostic_principal_id)) AND
  		(a_liste_diagnostic_relie = '0' OR diagnostic_relie_id = ANY (liste_diagnostic_relie_id)) AND
  		(a_liste_diagnostic_associe = '0' OR w_budget_cle_rule.oid = ANY(diagnostic_associe_liste_rule_id)) AND
  		(a_liste_acte = '0' OR w_budget_cle_rule.oid = ANY(acte_liste_rule_id)) AND
  		(a_liste_ghm = '0' OR ghm_pmsi_id = ANY (liste_ghm_pmsi_id)) AND
  		(a_liste_sauf_ghm = '0' OR ghm_pmsi_id <> ALL (liste_sauf_ghm_pmsi_id)) AND
  		(a_liste_rubrique = '0')
  	WHERE w_sejours_rss.rss_id <> 0
  	GROUP BY 1
  	ORDER BY 1;
  	UPDATE w_sejours_rss SET
  		budget_cle_id = w_sejours_budget_cle.budget_cle_id,
  		budget_cle_priorite = w_sejours_budget_cle.budget_cle_priorite
  	FROM w_sejours_budget_cle
  	WHERE w_sejours_rss.oid = w_sejours_budget_cle.oid;
  	-- Mise à jour des cles venant des données d'activite
  	DROP TABLE IF EXISTS w_sejours_budget_cle;
  	CREATE TEMP TABLE w_sejours_budget_cle AS
  	SELECT w_sejours_rss.oid, 
  		MIN(priorite) AS budget_cle_priorite,
  		(MIN(ARRAY[priorite,w_budget_cle_rule.budget_cle_id]))[2] AS budget_cle_id
  	FROM w_sejours_rss
  	JOIN w_budget_cle_rule ON 
  		(a_liste_finess = '0' OR finess_id = ANY (liste_finess_id)) AND
  		(a_liste_type_sejour = '0' OR type_sejour = ANY (liste_type_sejour_id)) AND
  		(a_liste_service = '0' OR service_facturation_id = ANY (liste_service_id)) AND
  		(a_liste_uf = '0' OR uf_id = ANY (liste_uf_id)) AND
  		(a_liste_activite = '0' OR activite_id = ANY (liste_activite_id)) AND
  		(a_liste_medecin = '0' OR medecin_sejour_id = ANY (liste_medecin_activite_id)) AND
  		(a_liste_specialite = '0' OR specialite_sejour_id = ANY (liste_specialite_activite_id)) AND
  		(a_liste_diagnostic_principal = '0' OR diagnostic_principal_id = ANY (liste_diagnostic_principal_id)) AND
  		(a_liste_diagnostic_relie = '0' OR diagnostic_relie_id = ANY (liste_diagnostic_relie_id)) AND
  		(a_liste_diagnostic_associe = '0' OR w_budget_cle_rule.oid = ANY(diagnostic_associe_liste_rule_id)) AND
  		(a_liste_acte = '0' OR w_budget_cle_rule.oid = ANY(acte_liste_rule_id)) AND
  		(a_liste_rubrique = '0' OR w_budget_cle_rule.oid = ANY(rubrique_liste_rule_id)) AND
  		(a_liste_ghm = '0' OR ghm_activite_id = ANY (liste_ghm_activite_id)) AND
  		(a_liste_sauf_ghm = '0' OR ghm_activite_id <> ALL (liste_sauf_ghm_activite_id)) 
  	WHERE w_sejours_rss.sejour_id <> 0
  	GROUP BY 1
  	ORDER BY 1;
  	UPDATE w_sejours_rss SET
  		budget_cle_id = w_sejours_budget_cle.budget_cle_id,
  		budget_cle_priorite = w_sejours_budget_cle.budget_cle_priorite
  	FROM w_sejours_budget_cle
  	WHERE w_sejours_rss.oid = w_sejours_budget_cle.oid AND
  		(w_sejours_rss.budget_cle_priorite > w_sejours_budget_cle.budget_cle_priorite OR w_sejours_rss.budget_cle_priorite = 0);
  
  
  	TRUNCATE activite.p_sejours_budget_cle;
  		
  	UPDATE activite.p_sejours_budget_cle
  	SET 
  		budget_cle_id = w_sejours_rss.budget_cle_id
  	FROM w_sejours_rss
  	WHERE p_sejours_budget_cle.sejour_id = w_sejours_rss.sejour_id AND
  		w_sejours_rss.lieu_id = w_sejours_rss.lieu_sortie_id AND
  		p_sejours_budget_cle.sejour_id <> 0 AND
  		(
  			p_sejours_budget_cle.budget_cle_id IS DISTINCT FROM w_sejours_rss.budget_cle_id	
  		);
  
  	
  	INSERT INTO activite.p_sejours_budget_cle (sejour_id, budget_cle_id)
  	SELECT sejour_id, MIN(budget_cle_id)
  	FROM w_sejours_rss
  	WHERE sejour_id <> 0 AND 
  		w_sejours_rss.lieu_id = w_sejours_rss.lieu_sortie_id AND
  		sejour_id NOT IN (SELECT sejour_id FROM activite.p_sejours_budget_cle)
  	GROUP BY 1
  	;
  
  	ANALYSE activite.p_sejours_budget_cle
  	;
  
  	UPDATE activite.p_mouvements_sejour
  	SET budget_cle_id = w_sejours_rss.budget_cle_id
  	FROM w_sejours_rss
  	WHERE p_mouvements_sejour.no_sejour = w_sejours_rss.no_sejour AND
  		p_mouvements_sejour.lieu_id = w_sejours_rss.lieu_id AND
  		p_mouvements_sejour.budget_cle_id IS DISTINCT FROM w_sejours_rss.budget_cle_id
  	;
  
  	ANALYSE activite.p_mouvements_sejour
  	;
  
  
  	UPDATE activite.p_factures_lignes_c
  	SET budget_cle_id = w_sejours_rss.budget_cle_id
  	FROM w_sejours_rss
  	JOIN activite.p_factures ON w_sejours_rss.no_sejour = p_factures.no_sejour
  	WHERE p_factures_lignes_c.no_facture = p_factures.no_facture AND
  		p_factures_lignes_c.lieu_id = w_sejours_rss.lieu_id AND
  		p_factures_lignes_c.budget_cle_id IS DISTINCT FROM w_sejours_rss.budget_cle_id
  	;
  
  	ANALYSE activite.p_factures_lignes_c
  	;
  
  	
          RETURN 'OK' ;
  
  END;