return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  	_module_pmsimco TEXT;
  	_with_rubrique TEXT;
  	_with_prestation TEXT;
  	_with_acte TEXT;
  BEGIN
  
  		RAISE NOTICE '%' , 'Initialisation poles';  
  		-- Contenu table
  		INSERT INTO base.t_pole (oid, code, texte, texte_court)
  		SELECT 0, '**', 'Non renseigné', 'Non renseigné'
  		WHERE 0 NOT IN (SELECT oid FROM base.t_pole);
  
  		IF (SELECT count(*) FROM base.t_pole WHERE oid > 0) = 0 THEN
  			RETURN 'OK. Aucun pole';
  		END IF;
  
  		-- replication dans activite
  		TRUNCATE activite.t_pole;
  		INSERT INTO activite.t_pole (oid, code, texte, texte_court, section_id, section_code, section_texte)
  		SELECT oid, code, texte, texte_court, section_id, section_code, section_texte
  		FROM base.t_pole
  		;
  
  
  		RAISE NOTICE '%' , 'Preparation mouvemennts'; 
  		
  		DROP TABLE IF EXISTS w_sejour_lieu;
  		
  		CREATE TEMP TABLE w_sejour_lieu AS
  		SELECT p_mouvements_sejour.no_sejour, p_mouvements_sejour.lieu_id
  		FROM activite.p_mouvements_sejour
  		GROUP BY 1,2
  		;
  
  		ANALYSE w_sejour_lieu
  		;
  
  		CREATE INDEX i_w_sejour_lieu_no_sejour
  		  ON w_sejour_lieu
  		  USING btree
  		  (no_sejour COLLATE pg_catalog."default");
  
  		CREATE INDEX i_w_sejour_lieu_lieu_id
  		  ON w_sejour_lieu
  		  USING btree
  		  (lieu_id);
  
  		INSERT INTO w_sejour_lieu (no_sejour, lieu_id)
  		SELECT p_sejours.no_sejour, p_sejours.lieu_sortie_id
  		FROM activite.p_sejours
  		LEFT JOIN w_sejour_lieu ON
  			p_sejours.no_sejour = w_sejour_lieu.no_sejour AND
  			p_sejours.lieu_sortie_id = w_sejour_lieu.lieu_id
  		WHERE w_sejour_lieu.lieu_id IS NULL 
  		GROUP BY 1,2
  		;
  
  		INSERT INTO w_sejour_lieu (no_sejour, lieu_id)
  		SELECT p_factures.no_sejour, p_factures_lignes_c.lieu_id
  		FROM activite.p_factures_lignes_c
  		JOIN activite.p_factures ON p_factures.no_facture = p_factures_lignes_c.no_facture
  		LEFT JOIN w_sejour_lieu ON
  			p_factures.no_sejour = w_sejour_lieu.no_sejour AND
  			p_factures_lignes_c.lieu_id = w_sejour_lieu.lieu_id
  		WHERE w_sejour_lieu.lieu_id IS NULL 
  		GROUP BY 1,2
  		;
  
  
  
  
  		INSERT INTO w_sejour_lieu (no_sejour, lieu_id)
  		SELECT p_factures.no_sejour, p_factures_lignes_h.lieu_id
  		FROM activite.p_factures_lignes_h
  		JOIN activite.p_factures ON p_factures.no_facture = p_factures_lignes_h.no_facture
  		LEFT JOIN w_sejour_lieu ON
  			p_factures.no_sejour = w_sejour_lieu.no_sejour AND
  			p_factures_lignes_h.lieu_id = w_sejour_lieu.lieu_id
  		WHERE w_sejour_lieu.lieu_id IS NULL 
  		GROUP BY 1,2
  		;	
  
  		
  		RAISE NOTICE '%' , 'Application des regles'; 
  
  		-- pmsi mco ?
  		_module_pmsimco = '0';
  		IF EXISTS (SELECT * FROM pg_tables WHERE schemaname = 'pmsi' AND tablename = 'p_rss') THEN
  			IF (SELECT count(*)
  				FROM activite.t_pole_rule
  				WHERE liste_ghm <> '' OR 
  					COALESCE(liste_sauf_ghm,'') <> '' OR
  					COALESCE(liste_diagnostic_principal,'') <> '' OR
  					COALESCE(liste_diagnostic_relie,'') <> '' OR
  					COALESCE(liste_diagnostic_associe,'') <> '' OR
  					COALESCE(liste_acte,'') <> '') > 1 THEN
  				_module_pmsimco = '1';
  				RAISE NOTICE '%' , 'Application des regles. Avec PMSI MCO'; 
  			END IF;
  		END IF;
  
  		-- Rubriques ?
  		_with_rubrique = '0';
  		IF (SELECT count(*)
  			FROM activite.t_pole_rule
  			WHERE COALESCE(liste_rubrique,'') <> ''
  			) > 1 THEN
  			_with_rubrique = '1';
  		END IF;
  		_with_prestation = '0';
  		IF (SELECT count(*)
  			FROM activite.t_pole_rule
  			WHERE COALESCE(liste_prestation,'') <> '' OR
  				COALESCE(liste_sauf_prestation,'') <> ''
  			) > 1 THEN
  			_with_prestation = '1';
  		END IF;
  		_with_acte = '0';
  		IF (SELECT count(*)
  			FROM activite.t_pole_rule
  			WHERE COALESCE(liste_acte,'') <> ''
  			) > 1 THEN
  			_with_acte = '1';
  		END IF;
  	 
  		-- Sejours et RSS à Traiter
  
  		RAISE NOTICE '%' , 'Application des regles. Identification des sejours'; 
  		DROP SEQUENCE IF EXISTS w_sejours_rss_sequence;
  		CREATE SEQUENCE w_sejours_rss_sequence;
  		DROP TABLE IF EXISTS w_sejours_rss;
  		
  		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, 
  				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 prestation_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,
  				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,
  				p_sejours.type_sejour,
  				w_sejour_lieu.lieu_id,
  				t_lieux_c.unite_fonctionnelle_id,
  				t_lieux_c.service_facturation_id,
  				t_lieux_c.etage_id,
  				t_lieux_c.activite_id,
  				0::bigint AS pole_id,
  				0::bigint AS pole_priorite
  			FROM activite.p_sejours
  				JOIN w_sejour_lieu ON p_sejours.no_sejour = w_sejour_lieu.no_sejour
  				JOIN activite.t_lieux_c ON w_sejour_lieu.lieu_id = t_lieux_c.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 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_sejours.finess = t_finess.code;
  			
  			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,
  				COALESCE(t_finess.oid,0) AS finess_id,
  				p_sejours.oid AS sejour_id, 
  				p_sejours.no_sejour AS no_sejour, 
  				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 prestation_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,
  				p_sejours.type_sejour,
  				w_sejour_lieu.lieu_id,
  				t_lieux_c.unite_fonctionnelle_id,
  				t_lieux_c.service_facturation_id,
  				t_lieux_c.etage_id,
  				t_lieux_c.activite_id,
  				0::bigint AS pole_id,
  				0::bigint AS pole_priorite
  			FROM activite.p_sejours
  				JOIN w_sejour_lieu ON p_sejours.no_sejour = w_sejour_lieu.no_sejour
  				JOIN activite.t_lieux_c ON w_sejour_lieu.lieu_id = t_lieux_c.oid
  				LEFT JOIN base.t_finess ON p_sejours.finess = t_finess.code
  				LEFT JOIN activite.t_medecins_administratifs ON 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;
  		-- GHM sur services non MCO
  		-- Verrouillé pour l'instant (on verra par la suite)
  		-- UPDATE w_sejours_rss SET
  		-- 	ghm_pmsi_id = 0,
  		-- 	ghm_activite_id = 0
  		-- FROM activite.t_services_facturation
  		-- WHERE service_facturation_id = t_services_facturation .oid AND
  		-- 	type_t2a <> '1' AND
  		-- 	service_facturation_id <> 0 AND
  		-- 	(ghm_pmsi_id > 0 OR
  		-- 	ghm_activite_id > 0)
  		-- ;
  
  		--
  		UPDATE w_sejours_rss SET
  			medecin_rss_id = 0,
  			specialite_rss_id = 0
  		WHERE medecin_sejour_id <> 0 AND
  			(medecin_rss_id <> 0 OR specialite_rss_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
  		RAISE NOTICE '%' , 'Application des regles. Traduction en oid'; 
  		DROP TABLE IF EXISTS w_pole_rule;
  		CREATE TEMP TABLE w_pole_rule AS
  		SELECT
  			t_pole_rule.oid,
  			pole_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(translate(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(translate(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(translate(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(translate(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(translate(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(translate(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(translate(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(translate(liste_type_sejour,'*,','% '),' ') AS liste_type_sejour,
  			NULL::text[] AS liste_type_sejour_id ,
  			CASE WHEN liste_unite_fonctionnelle <> '' THEN '1' ELSE '0' END AS a_liste_unite_fonctionnelle,
  			string_to_array(translate(liste_unite_fonctionnelle,'*,','% '),' ') AS liste_unite_fonctionnelle,
  			NULL::bigint[] AS liste_unite_fonctionnelle_id ,
  			CASE WHEN liste_service <> '' THEN '1' ELSE '0' END AS a_liste_service,
  			string_to_array(translate(liste_service,'*,','% '),' ') AS liste_service,
  			NULL::bigint[] AS liste_service_id ,
  			CASE WHEN liste_etage <> '' THEN '1' ELSE '0' END AS a_liste_etage,
  			string_to_array(translate(liste_etage,'*,','% '),' ') AS liste_etage,
  			NULL::bigint[] AS liste_etage_id ,
  			CASE WHEN liste_activite <> '' THEN '1' ELSE '0' END AS a_liste_activite,
  			string_to_array(translate(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 translate(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(translate(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(translate(liste_rubrique,'*,','% '),' ') AS liste_rubrique,
  			NULL::bigint[] AS liste_rubrique_id,
  			CASE WHEN liste_prestation <> '' THEN '1' ELSE '0' END AS a_liste_prestation,
  			string_to_array(translate(liste_prestation,'*,','% '),' ') AS liste_prestation,
  			NULL::bigint[] AS liste_prestation_id
  		FROM activite.t_pole_rule
  		JOIN activite.t_pole ON pole_id = t_pole.oid 
  		ORDER BY priorite, pole_id;
  
  
  		
  		UPDATE w_pole_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_pole_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_pole_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_pole_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_pole_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_pole_rule_actes_pmsi;
  			CREATE TEMP TABLE w_pole_rule_actes_pmsi AS
  			SELECT t_actes.oid AS rule_acte_id, w_pole_rule.oid AS rule_id
  			FROM pmsi.t_actes
  			JOIN w_pole_rule ON code LIKE ANY (liste_acte);
  			CREATE INDEX w_pole_rule_actes_pmsi_i1 ON w_pole_rule_actes_pmsi USING btree (rule_acte_id);
  		END IF;
  		
  		
  		DROP TABLE IF EXISTS w_pole_rule_actes_base;
  		CREATE TEMP TABLE w_pole_rule_actes_base AS
  		SELECT t_actes.oid AS rule_acte_id, w_pole_rule.oid AS rule_id
  		FROM base.t_actes
  		JOIN w_pole_rule ON code LIKE ANY (liste_acte);
  		CREATE INDEX w_pole_rule_actes_base_i1 ON w_pole_rule_actes_base USING btree (rule_acte_id);
  		
  		DROP TABLE IF EXISTS w_pole_rule_rubriques;
  		CREATE TEMP TABLE w_pole_rule_rubriques AS
  		SELECT t_rubriques_facturation.oid AS rule_rubrique_id, w_pole_rule.oid AS rule_id
  		FROM activite.t_rubriques_facturation
  		JOIN w_pole_rule ON code LIKE ANY (liste_rubrique);
  		CREATE INDEX w_pole_rule_rubriques_i1 ON w_pole_rule_rubriques USING btree (rule_rubrique_id);
  
  		DROP TABLE IF EXISTS w_pole_rule_prestations;
  		CREATE TEMP TABLE w_pole_rule_prestations AS
  		SELECT t_prestations.oid AS rule_prestation_id, w_pole_rule.oid AS rule_id
  		FROM activite.t_prestations
  		JOIN w_pole_rule ON code LIKE ANY (liste_prestation);
  		CREATE INDEX w_pole_rule_prestations_i1 ON w_pole_rule_prestations USING btree (rule_prestation_id);
  		
  		UPDATE w_pole_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'
  		;
  		UPDATE w_pole_rule 
  		SET liste_acte_base_id = 
  			array_cat(liste_acte_base_id,
  			(SELECT base.cti_group_array3(t_actes.oid) 
  			FROM base.t_actes
  			JOIN activite.t_listes_contenu ON to_id = t_actes.oid 
  			JOIN activite.t_listes ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE '[LISTE:'||t_listes.code||']' LIKE ANY (liste_acte))
  			)
  		WHERE a_liste_acte = '1' AND 
  			array_to_string(liste_acte,' ') LIKE '%[LISTE:%'
  		;
  		
  		
  		IF _module_pmsimco = '1' THEN
  			UPDATE w_pole_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';
  			UPDATE w_pole_rule 
  			SET liste_ghm_pmsi_id = 
  				array_cat(liste_ghm_pmsi_id,
  				(SELECT base.cti_group_array3(t_ghm.oid) FROM pmsi.t_ghm JOIN base.t_cas ON cas_id = t_cas.oid WHERE '[CAS:'||t_cas.code||']' LIKE ANY (liste_ghm))
  				)
  			WHERE a_liste_ghm = '1' AND 
  				liste_ghm_origine LIKE '%[CAS:%'
  			;
  		END IF;
  		
  		UPDATE w_pole_rule 
  		SET liste_ghm_activite_id = (SELECT base.cti_group_array3(oid) FROM base.t_ghm WHERE code LIKE ANY (liste_ghm))
  		WHERE a_liste_ghm = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_ghm_activite_id = 
  			array_cat(liste_ghm_activite_id,
  			(SELECT base.cti_group_array3(t_ghm.oid) FROM base.t_ghm JOIN base.t_cas ON cas_id = t_cas.oid WHERE '[CAS:'||t_cas.code||']' LIKE ANY (liste_ghm))
  			)
  		WHERE a_liste_ghm = '1' AND 
  			liste_ghm_origine LIKE '%[CAS:%'
  		;
  		UPDATE w_pole_rule 
  		SET liste_acte_base_id = 
  			array_cat(liste_acte_base_id,
  			(SELECT base.cti_group_array3(t_actes.oid) 
  			FROM base.t_actes
  			JOIN activite.t_listes_contenu ON to_id = t_actes.oid 
  			JOIN activite.t_listes ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE '[LISTE:'||t_listes.code||']' LIKE ANY (liste_acte))
  			)
  		WHERE a_liste_acte = '1' AND 
  			array_to_string(liste_acte,' ') LIKE '%[LISTE:%'
  		;
  
  		
  		IF _module_pmsimco = '1' THEN
  			UPDATE w_pole_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_pole_rule 
  		SET liste_sauf_ghm_activite_id = (SELECT base.cti_group_array3(oid) FROM base.t_ghm WHERE code LIKE ANY (liste_sauf_ghm))
  		WHERE a_liste_sauf_ghm = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_sauf_ghm_activite_id = 
  			array_cat(liste_sauf_ghm_activite_id,
  			(SELECT base.cti_group_array3(t_ghm.oid) 
  			FROM base.t_ghm 
  			JOIN activite.t_listes_contenu ON to_id = t_ghm.oid 
  			JOIN activite.t_listes ON t_listes_contenu.liste_id = t_listes.oid
  			WHERE '[LISTE:'||t_listes.code||']' LIKE ANY (liste_sauf_ghm))
  			)
  		WHERE a_liste_sauf_ghm = '1' AND 
  			liste_sauf_ghm_origine LIKE '%[LISTE:%'
  		;
  		IF _module_pmsimco = '1' THEN
  			UPDATE w_pole_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_pole_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_pole_rule 
  			SET liste_specialite_pmsi_id = (SELECT base.cti_group_array3(oid) FROM pmsi.t_specialites_medecin WHERE oid <> 0 AND code LIKE ANY (liste_specialite))
  			WHERE a_liste_specialite = '1';
  		END IF;
  		
  		UPDATE w_pole_rule 
  		SET liste_specialite_activite_id = (SELECT base.cti_group_array3(oid) FROM base.t_specialites_medecin WHERE oid <> 0 AND code LIKE ANY (liste_specialite))
  		WHERE a_liste_specialite = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_type_sejour_id = (SELECT base.cti_group_array3(code) FROM activite.t_type_sejour WHERE oid <> 0 AND code <> '' AND code LIKE ANY (liste_type_sejour))
  		WHERE a_liste_type_sejour = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_unite_fonctionnelle_id = (SELECT base.cti_group_array3(oid) FROM activite.t_unites_fonctionnelles WHERE oid <> 0 AND code LIKE ANY (liste_unite_fonctionnelle))
  		WHERE a_liste_unite_fonctionnelle = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_service_id = (SELECT base.cti_group_array3(oid) FROM activite.t_services_facturation WHERE oid <> 0 AND code LIKE ANY (liste_service))
  		WHERE a_liste_service = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_etage_id = (SELECT base.cti_group_array3(oid) FROM activite.t_etages WHERE oid <> 0 AND code LIKE ANY (liste_etage))
  		WHERE a_liste_etage = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_activite_id = (SELECT base.cti_group_array3(oid) FROM activite.t_activites WHERE oid <> 0 AND code LIKE ANY (liste_activite))
  		WHERE a_liste_activite = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_rubrique_id = (SELECT base.cti_group_array3(oid) FROM activite.t_rubriques_facturation WHERE oid <> 0 AND code LIKE ANY (liste_rubrique))
  		WHERE a_liste_rubrique = '1'
  		;
  		UPDATE w_pole_rule 
  		SET liste_prestation_id = (SELECT base.cti_group_array3(oid) FROM activite.t_prestations WHERE oid <> 0 AND code LIKE ANY (liste_prestation))
  		WHERE a_liste_prestation = '1'
  		;
  
  		
  		-- Association cles aux rubriques
  		RAISE NOTICE '%' , 'Application des regles. Traitement des rubriques'; 
  		DROP TABLE IF EXISTS w_factures_rubriques;
  		IF _with_rubrique = '1' THEN
  			CREATE TEMP TABLE w_factures_rubriques AS
  			SELECT
  				p_factures.no_sejour, rule_id
  			FROM activite.p_factures_lignes_c
  			JOIN activite.p_factures ON p_factures.no_facture = p_factures_lignes_c.no_facture
  			JOIN w_pole_rule_rubriques ON p_factures_lignes_c.prestation_id = rule_rubrique_id
  			WHERE p_factures_lignes_c.montant_facture <> 0
  			GROUP BY 1,2
  			HAVING SUM(p_factures_lignes_c.montant_facture) > 0
  			UNION
  			SELECT
  				p_factures.no_sejour, rule_id
  			FROM activite.p_factures_lignes_non_facturees_c
  			JOIN activite.p_factures ON p_factures.no_facture = p_factures_lignes_non_facturees_c.no_facture
  			JOIN w_pole_rule_rubriques ON p_factures_lignes_non_facturees_c.prestation_id = rule_rubrique_id
  			;
  			CREATE INDEX  w_factures_rubriques_i1 ON  w_factures_rubriques USING btree (no_sejour);
  		ELSE 
  			CREATE TEMP TABLE w_factures_rubriques (no_sejour text, rule_id bigint);
  		END IF;
  		
  		DROP TABLE IF EXISTS w_pole_rubriques;
  		CREATE TEMP TABLE w_pole_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 w_sejours_rss ON w_factures_rubriques.no_sejour = w_sejours_rss.no_sejour
  		) subview
  		GROUP BY 1;
  		ALTER TABLE w_pole_rubriques ADD CONSTRAINT w_pole_rubriques_pkey PRIMARY KEY(oid);
  		UPDATE w_sejours_rss
  		SET rubrique_liste_rule_id = w_pole_rubriques.rubrique_liste_rule_id
  		FROM w_pole_rubriques
  		WHERE w_pole_rubriques.oid = w_sejours_rss.oid;
  
  
  
  		-- Association cles aux prestations
  		RAISE NOTICE '%' , 'Application des regles. Traitement des prestations'; 
  		DROP TABLE IF EXISTS w_factures_prestations;
  		IF _with_prestation = '1' THEN
  			CREATE TEMP TABLE w_factures_prestations AS
  			SELECT
  				p_factures.no_sejour, rule_id
  			FROM activite.p_factures_lignes_c
  			JOIN activite.p_factures ON p_factures.no_facture = p_factures_lignes_c.no_facture
  			JOIN w_pole_rule_prestations ON p_factures_lignes_c.prestation_id = rule_prestation_id
  			WHERE p_factures_lignes_c.montant_facture <> 0
  			GROUP BY 1,2
  			HAVING SUM(p_factures_lignes_c.montant_facture) > 0
  			UNION
  			SELECT
  				p_factures.no_sejour, rule_id
  			FROM activite.p_factures_lignes_non_facturees_c
  			JOIN activite.p_factures ON p_factures.no_facture = p_factures_lignes_non_facturees_c.no_facture
  			JOIN w_pole_rule_prestations ON p_factures_lignes_non_facturees_c.prestation_id = rule_prestation_id
  			GROUP BY 1,2
  			;
  		ELSE
  			CREATE TEMP TABLE w_factures_prestations (no_sejour text, rule_id bigint);
  		END IF;
  		CREATE INDEX  w_factures_prestations_i1 ON  w_factures_prestations USING btree (no_sejour);
  		
  		DROP TABLE IF EXISTS w_pole_prestations;
  		CREATE TEMP TABLE w_pole_prestations AS
  		
  		SELECT oid, base.cti_group_array3(prestation_rule_id) AS prestation_liste_rule_id
  		FROM
  		(
  			SELECT
  				w_sejours_rss.oid, rule_id AS prestation_rule_id
  			FROM w_factures_prestations
  			JOIN w_sejours_rss ON w_factures_prestations.no_sejour = w_sejours_rss.no_sejour
  		) subview
  		GROUP BY 1;
  		ALTER TABLE w_pole_prestations ADD CONSTRAINT w_pole_prestations_pkey PRIMARY KEY(oid);
  		UPDATE w_sejours_rss
  		SET prestation_liste_rule_id = w_pole_prestations.prestation_liste_rule_id
  		FROM w_pole_prestations
  		WHERE w_pole_prestations.oid = w_sejours_rss.oid;
  		
  		
  		-- Association cles aux actes
  		RAISE NOTICE '%' , 'Application des regles. Traitement des actes'; 
  		DROP TABLE IF EXISTS w_factures_actes;
  		IF _with_acte = '1' THEN
  			CREATE TEMP TABLE w_factures_actes AS
  			SELECT
  					no_facture, rule_id
  				FROM activite.p_factures_lignes_h
  				JOIN w_pole_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_pole_rule_actes_base ON p_factures_lignes_non_facturees_h.acte_id = rule_acte_id
  				WHERE activite_ccam <> '4';
  		ELSE
  			CREATE TEMP TABLE w_factures_actes (no_facture text, rule_id bigint);
  		END IF;
  		CREATE INDEX  w_factures_actes_i1 ON  w_factures_actes USING btree (no_facture);
  		
  		DROP TABLE IF EXISTS w_pole_actes;
  		IF _module_pmsimco = '1' THEN
  			CREATE TEMP TABLE w_pole_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_pole_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_pole_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_pole_actes ADD CONSTRAINT w_pole_actes_pkey PRIMARY KEY(oid);
  		UPDATE w_sejours_rss
  		SET acte_liste_rule_id = w_pole_actes.acte_liste_rule_id
  		FROM w_pole_actes
  		WHERE w_pole_actes.oid = w_sejours_rss.oid;
  		
  		-- Diagnostics
  		IF _module_pmsimco = '1' THEN
  			RAISE NOTICE '%' , 'Application des regles. Traitement des diagnostics'; 
  			DROP TABLE IF EXISTS w_pole_diagnostics;
  			CREATE TEMP TABLE w_pole_diagnostics AS
  			SELECT
  				w_sejours_rss.oid, base.cti_group_array3(w_pole_rule.oid) AS diagnostic_associe_liste_rule_id
  			FROM pmsi.p_rss_diagnostics
  			JOIN w_pole_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_pole_diagnostics ADD CONSTRAINT w_pole_diagnostics_pkey PRIMARY KEY(oid);
  			UPDATE w_sejours_rss
  			SET diagnostic_associe_liste_rule_id = w_pole_diagnostics.diagnostic_associe_liste_rule_id
  			FROM w_pole_diagnostics
  			WHERE w_pole_diagnostics.oid = w_sejours_rss.oid;
  		END IF;
  		
  		-- Mise à jour des cles venant des données du PMSI
  		RAISE NOTICE '%' , 'Application des regles. Attribution des poles venant du pmsi'; 
  		DROP TABLE IF EXISTS w_sejours_pole;
  		CREATE TEMP TABLE w_sejours_pole AS
  		SELECT w_sejours_rss.oid, 
  			MIN(priorite)+1 AS pole_priorite,
  			(MIN(ARRAY[priorite,w_pole_rule.pole_id]))[2] AS pole_id
  		FROM w_sejours_rss
  		JOIN w_pole_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_unite_fonctionnelle = '0') AND
  			(a_liste_service = '0') AND
  			(a_liste_etage = '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_pole_rule.oid = ANY(diagnostic_associe_liste_rule_id)) AND
  			(a_liste_acte = '0' OR w_pole_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_prestation = '0') AND
  			(a_liste_rubrique = '0')
  		WHERE w_sejours_rss.rss_id <> 0
  		GROUP BY 1
  		ORDER BY 1;
  		UPDATE w_sejours_rss SET
  			pole_id = w_sejours_pole.pole_id,
  			pole_priorite = w_sejours_pole.pole_priorite
  		FROM w_sejours_pole
  		WHERE w_sejours_rss.oid = w_sejours_pole.oid;
  		-- Mise à jour des cles venant des données d'activite
  		RAISE NOTICE '%' , 'Application des regles. Attribution des poles venant de la GAP'; 
  		DROP TABLE IF EXISTS w_sejours_pole;
  		CREATE TEMP TABLE w_sejours_pole AS
  		SELECT w_sejours_rss.oid, 
  			MIN(priorite) AS pole_priorite,
  			(MIN(ARRAY[priorite,w_pole_rule.pole_id]))[2] AS pole_id
  		FROM w_sejours_rss
  		JOIN w_pole_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_unite_fonctionnelle = '0' OR unite_fonctionnelle_id = ANY (liste_unite_fonctionnelle_id)) AND
  			(a_liste_service = '0' OR service_facturation_id = ANY (liste_service_id)) AND
  			(a_liste_etage = '0' OR etage_id = ANY (liste_etage_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_pole_rule.oid = ANY(diagnostic_associe_liste_rule_id)) AND
  			(a_liste_acte = '0' OR w_pole_rule.oid = ANY(acte_liste_rule_id)) AND
  			(a_liste_rubrique = '0' OR w_pole_rule.oid = ANY(rubrique_liste_rule_id)) AND
  			(a_liste_prestation = '0' OR w_pole_rule.oid = ANY(prestation_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
  			pole_id = w_sejours_pole.pole_id,
  			pole_priorite = w_sejours_pole.pole_priorite
  		FROM w_sejours_pole
  		WHERE w_sejours_rss.oid = w_sejours_pole.oid AND
  			(w_sejours_rss.pole_priorite > w_sejours_pole.pole_priorite OR w_sejours_rss.pole_priorite = 0);
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles'; 
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. p_sejour_lieu'; 
  		
  		TRUNCATE activite.p_sejour_lieu;
  	
  		INSERT INTO activite.p_sejour_lieu (no_sejour, sejour_id, lieu_id, pole_id)
  		SELECT no_sejour, sejour_id, lieu_id, MAX(pole_id) AS pole_id
  		FROM w_sejours_rss
  		GROUP BY 1,2,3
  		;
  
  		ANALYSE activite.p_sejour_lieu
  		;
  
  		-- Réplication dans tables de prod
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. p_mouvements_sejour'; 
  		
  		DROP TABLE IF EXISTS w_mouvement_pole;
  		CREATE TEMP TABLE w_mouvement_pole AS
  		SELECT p_mouvements_sejour.*, COALESCE(p_sejour_lieu.pole_id,0) AS new_pole_id
  		FROM activite.p_mouvements_sejour
  		LEFT JOIN activite.p_sejour_lieu ON 
  			p_mouvements_sejour.no_sejour = p_sejour_lieu.no_sejour AND
  			p_mouvements_sejour.lieu_id = p_sejour_lieu.lieu_id
  		;
  		UPDATE w_mouvement_pole
  		SET pole_id = new_pole_id 
  		WHERE pole_id IS DISTINCT FROM new_pole_id 
  		;
  		ALTER TABLE w_mouvement_pole DROP COLUMN new_pole_id
  		;
  		TRUNCATE activite.p_mouvements_sejour
  		;
  		INSERT INTO activite.p_mouvements_sejour SELECT * FROM w_mouvement_pole
  		;
  		ANALYSE activite.p_mouvements_sejour
  		;
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. p_factures_lignes_c'; 
  		
  		DROP TABLE IF EXISTS w_ligne_pole_c;
  		CREATE TEMP TABLE w_ligne_pole_c AS
  		SELECT p_factures_lignes_c.*, COALESCE(p_sejour_lieu.pole_id,0) AS new_pole_id
  		FROM activite.p_factures_lignes_c
  		JOIN activite.p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
  		LEFT JOIN activite.p_sejour_lieu ON 
  			p_factures.no_sejour = p_sejour_lieu.no_sejour AND
  			p_factures_lignes_c.lieu_id = p_sejour_lieu.lieu_id
  		;
  		UPDATE w_ligne_pole_c
  		SET pole_id = new_pole_id 
  		WHERE pole_id IS DISTINCT FROM new_pole_id 
  		;
  		ALTER TABLE w_ligne_pole_c DROP COLUMN new_pole_id
  		;
  		TRUNCATE activite.p_factures_lignes_c
  		;
  		INSERT INTO activite.p_factures_lignes_c SELECT * FROM w_ligne_pole_c
  		;
  		ANALYSE activite.p_factures_lignes_c
  		;
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. p_factures_encours_lignes_c'; 
  		
  		DROP TABLE IF EXISTS w_encours_ligne_pole_c;
  		CREATE TEMP TABLE w_encours_ligne_pole_c AS
  		SELECT p_factures_encours_lignes_c.*, COALESCE(p_sejour_lieu.pole_id,0) AS new_pole_id
  		FROM activite.p_factures_encours_lignes_c
  		JOIN activite.p_factures_encours ON p_factures_encours_lignes_c.no_facture = p_factures_encours.no_facture
  		LEFT JOIN activite.p_sejour_lieu ON 
  			p_factures_encours.no_sejour = p_sejour_lieu.no_sejour AND
  			p_factures_encours_lignes_c.lieu_id = p_sejour_lieu.lieu_id
  		;
  		UPDATE w_encours_ligne_pole_c
  		SET pole_id = new_pole_id 
  		WHERE pole_id IS DISTINCT FROM new_pole_id 
  		;
  		ALTER TABLE w_encours_ligne_pole_c DROP COLUMN new_pole_id
  		;
  		TRUNCATE activite.p_factures_encours_lignes_c
  		;
  		INSERT INTO activite.p_factures_encours_lignes_c SELECT * FROM w_encours_ligne_pole_c
  		;
  		ANALYSE activite.p_factures_encours_lignes_c
  		;
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. p_factures_lignes_h'; 
  		
  		DROP TABLE IF EXISTS w_ligne_pole_h;
  		CREATE TEMP TABLE w_ligne_pole_h AS
  		SELECT p_factures_lignes_h.*, COALESCE(p_sejour_lieu.pole_id,0) AS new_pole_id
  		FROM activite.p_factures_lignes_h
  		JOIN activite.p_factures ON p_factures_lignes_h.no_facture = p_factures.no_facture
  		LEFT JOIN activite.p_sejour_lieu ON 
  			p_factures.no_sejour = p_sejour_lieu.no_sejour AND
  			p_factures_lignes_h.lieu_id = p_sejour_lieu.lieu_id
  		;
  		UPDATE w_ligne_pole_h
  		SET pole_id = new_pole_id 
  		WHERE pole_id IS DISTINCT FROM new_pole_id 
  		;
  		ALTER TABLE w_ligne_pole_h DROP COLUMN new_pole_id
  		;
  		TRUNCATE activite.p_factures_lignes_h
  		;
  		INSERT INTO activite.p_factures_lignes_h SELECT * FROM w_ligne_pole_h
  		;
  		ANALYSE activite.p_factures_lignes_h
  		;
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. p_factures_reference'; 
  		
  		DROP TABLE IF EXISTS w_facture_pole;
  		CREATE TEMP TABLE w_facture_pole AS
  		SELECT no_facture_reference, 
  			(MAX(ARRAY[p_mouvements_sejour.date::text, to_char(p_mouvements_sejour.heure_debut,'FM000000'), p_mouvements_sejour.pole_id::text]))[3]::bigint AS pole_sortie_id
  		FROM activite.p_factures_reference
  		JOIN activite.p_mouvements_sejour ON
  			p_mouvements_sejour.no_sejour = p_factures_reference.no_sejour AND
  			p_mouvements_sejour.date BETWEEN p_factures_reference.date_debut_facture AND date_fin_facture
  		GROUP BY 1
  		;
  
  		UPDATE activite.p_factures_reference
  		SET pole_sortie_id = w_facture_pole.pole_sortie_id
  		FROM w_facture_pole
  		WHERE p_factures_reference.no_facture_reference = w_facture_pole.no_facture_reference AND
  			p_factures_reference.pole_sortie_id IS DISTINCT FROM w_facture_pole.pole_sortie_id
  		;
  
  		ANALYSE activite.p_factures_reference
  		;
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. p_factures_reference'; 
  
  		UPDATE activite.p_factures_reference_tiers
  		SET pole_sortie_id = w_facture_pole.pole_sortie_id
  		FROM w_facture_pole
  		WHERE p_factures_reference_tiers.no_facture_reference = w_facture_pole.no_facture_reference AND
  			p_factures_reference_tiers.pole_sortie_id IS DISTINCT FROM w_facture_pole.pole_sortie_id
  		;
  
  		ANALYSE activite.p_factures_reference_tiers
  		;
  		
  
  		-- Pole par defaut pour taux d'occupation
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. t_etages_historique'; 
  		
  		UPDATE activite.t_etages_historique
  		SET pole_occupation_id = subview.pole_occupation_id
  		FROM
  		(
  		SELECT etage_id, date_debut, (MAX(ARRAY[nb, pole_id]))[2] AS pole_occupation_id
  		FROM
  		(
  		SELECT t_lieux_c.etage_id, t_etages_historique.date AS date_debut, t_etages_historique.date_fin, p_mouvements_sejour.pole_id, count(*) AS nb
  		FROM activite.p_mouvements_sejour
  		JOIN activite.t_lieux_c ON p_mouvements_sejour.lieu_id = t_lieux_c.oid
  		JOIN activite.t_etages_historique ON t_lieux_c.etage_id = t_etages_historique.etage_id AND p_mouvements_sejour.date BETWEEN t_etages_historique.date AND t_etages_historique.date_fin
  		WHERE t_lieux_c.etage_id <> 0
  		GROUP BY 1,2,3,4
  		ORDER BY 1,2
  		) subview
  		GROUP BY 1,2
  		) subview
  		WHERE t_etages_historique.etage_id = subview.etage_id AND
  			t_etages_historique.date = subview.date_debut AND
  			t_etages_historique.pole_occupation_id IS DISTINCT FROM subview.pole_occupation_id
  		;
  		
  		
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. t_services_facturation_historique';
  		 
  		UPDATE activite.t_services_facturation_historique
  		SET pole_occupation_id = subview.pole_occupation_id
  		FROM
  		(
  		SELECT service_facturation_id, date_debut, (MAX(ARRAY[nb, pole_id]))[2] AS pole_occupation_id
  		FROM
  		(
  		SELECT t_lieux_c.service_facturation_id, t_services_facturation_historique.date AS date_debut, t_services_facturation_historique.date_fin, p_mouvements_sejour.pole_id, count(*) AS nb
  		FROM activite.p_mouvements_sejour
  		JOIN activite.t_lieux_c ON p_mouvements_sejour.lieu_id = t_lieux_c.oid
  		JOIN activite.t_services_facturation_historique ON t_lieux_c.service_facturation_id = t_services_facturation_historique.service_facturation_id AND p_mouvements_sejour.date BETWEEN t_services_facturation_historique.date AND t_services_facturation_historique.date_fin
  		WHERE t_lieux_c.service_facturation_id <> 0
  		GROUP BY 1,2,3,4
  		ORDER BY 1,2
  		) subview
  		GROUP BY 1,2
  		) subview
  		WHERE t_services_facturation_historique.service_facturation_id = subview.service_facturation_id AND
  			t_services_facturation_historique.date = subview.date_debut AND
  			t_services_facturation_historique.pole_occupation_id IS DISTINCT FROM subview.pole_occupation_id
  		;
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. t_unites_fonctionnelles_historique';
  		
  		UPDATE activite.t_unites_fonctionnelles_historique
  		SET pole_occupation_id = subview.pole_occupation_id
  		FROM
  		(
  		SELECT unite_fonctionnelle_id, date_debut, (MAX(ARRAY[nb, pole_id]))[2] AS pole_occupation_id
  		FROM
  		(
  		SELECT t_lieux_c.unite_fonctionnelle_id, t_unites_fonctionnelles_historique.date AS date_debut, t_unites_fonctionnelles_historique.date_fin, p_mouvements_sejour.pole_id, count(*) AS nb
  		FROM activite.p_mouvements_sejour
  		JOIN activite.t_lieux_c ON p_mouvements_sejour.lieu_id = t_lieux_c.oid
  		JOIN activite.t_unites_fonctionnelles_historique ON t_lieux_c.unite_fonctionnelle_id = t_unites_fonctionnelles_historique.unite_fonctionnelle_id AND p_mouvements_sejour.date BETWEEN t_unites_fonctionnelles_historique.date AND t_unites_fonctionnelles_historique.date_fin
  		WHERE t_lieux_c.unite_fonctionnelle_id <> 0
  		GROUP BY 1,2,3,4
  		ORDER BY 1,2
  		) subview
  		GROUP BY 1,2
  		) subview
  		WHERE t_unites_fonctionnelles_historique.unite_fonctionnelle_id = subview.unite_fonctionnelle_id AND
  			t_unites_fonctionnelles_historique.date = subview.date_debut AND
  			t_unites_fonctionnelles_historique.pole_occupation_id IS DISTINCT FROM subview.pole_occupation_id
  		;
  
  		RAISE NOTICE '%' , 'Application des regles. Validation des poles. t_places';
  		
  		UPDATE activite.t_places
  		SET pole_id = pole_occupation_id
  		FROM activite.t_places_parents
  		JOIN activite.t_etages_historique ON t_places_parents.oid = t_etages_historique.etage_id
  		WHERE t_places.parent_id = t_places_parents.oid AND
  			parent_type = 'ETA' AND
  			t_places.date BETWEEN t_etages_historique.date AND t_etages_historique.date_fin
  			;
  
  
  		ANALYSE activite.t_places
  		;
  			
  		-- 
  		
  		INSERT INTO activite.p_oids (code_table, oid) 
                  SELECT 'pole', pole_id 
                  FROM activite.p_sejour_lieu
                  WHERE pole_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'pole')
                  GROUP BY 2;
  
  
                  -- Replication Pole dans PMSI MCO
                  IF EXISTS (SELECT * FROM pg_tables WHERE schemaname = 'pmsi' AND tablename = 'p_rss_rum') THEN
  
  			TRUNCATE pmsi.t_pole;
  			INSERT INTO pmsi.t_pole (oid, code, texte, texte_court, section_id, section_code, section_texte)
  			SELECT oid, code, texte, texte_court, section_id, section_code, section_texte
  			FROM base.t_pole
  			;
  			
  			RAISE NOTICE '%' , 'Application des regles PMSI MCO. Validation des poles. p_rss_rum';
  			
  			DROP TABLE IF EXISTS w_rss_rum_pole;
  			CREATE TEMP TABLE w_rss_rum_pole AS
  			SELECT rss_id, no_rum, unite_medicale_id, (MAX(ARRAY[nb, pole_id]))[2] AS pole_id, COUNT(DISTINCT nb) AS nb_nb, count(*) AS nb
  			FROM
  			(
  			SELECT p_rss_rum.rss_id, p_rss_rum.no_rum, unite_medicale_id, p_mouvements_sejour.pole_id, COUNT(DISTINCT(p_mouvements_sejour.date)) AS nb
  			FROM pmsi.p_rss_rum
  			JOIN activite.p_sejour_pmsi ON pmsi.p_rss_rum.rss_id = p_sejour_pmsi.rss_id
  			JOIN activite.p_mouvements_sejour ON 
  				p_mouvements_sejour.sejour_id = p_sejour_pmsi.sejour_id AND
  				p_mouvements_sejour.date BETWEEN p_rss_rum.date_entree AND p_rss_rum.date_sortie
  			GROUP BY 1,2,3,4
  			) subview
  			GROUP BY 1,2,3
  			;
  
  
  
  			UPDATE pmsi.p_rss_rum
  			SET pole_id = w_rss_rum_pole.pole_id
  			FROM w_rss_rum_pole
  			WHERE w_rss_rum_pole.rss_id = p_rss_rum.rss_id AND
  				w_rss_rum_pole.no_rum = p_rss_rum.no_rum AND
  				p_rss_rum.pole_id IS DISTINCT FROM w_rss_rum_pole.pole_id AND
  				nb = nb_nb
  			;
  
  			UPDATE pmsi.p_rss_rum
  			SET pole_id = 0
  			WHERE pole_id <> 0 AND
  				rss_id::text||'-'||no_rum::text NOT IN (SELECT rss_id::text||'-'||no_rum::text FROM w_rss_rum_pole WHERE nb = nb_nb)
  			;
  
  			UPDATE pmsi.p_rss_rum
  			SET pole_id = subview.pole_id
  			FROM
  			(
  			SELECT unite_medicale_id, (MAX(ARRAY[nb, pole_id]))[2] AS pole_id
  			FROM 
  			(
  			SELECT unite_medicale_id, pole_id, count(*) AS nb
  			FROM w_rss_rum_pole
  			WHERE pole_id <> 0 AND nb = nb_nb
  			GROUP BY 1,2
  			) subview
  			GROUP BY 1
  			) subview
  			WHERE p_rss_rum.pole_id = 0 AND
  				p_rss_rum.unite_medicale_id = subview.unite_medicale_id
  			;
  
  			ANALYSE pmsi.p_rss_rum
  			;
  
  			RAISE NOTICE '%' , 'Application des regles PMSI MCO. Validation des poles. p_rss_actes';
  			
  			UPDATE pmsi.p_rss_actes
  			SET pole_id = p_rss_rum.pole_id
  			FROM pmsi.p_rss_rum
  			WHERE p_rss_rum.rss_id = p_rss_actes.rss_id AND
  				p_rss_rum.no_rum = p_rss_actes.no_rum AND
  				p_rss_actes.pole_id IS DISTINCT FROM p_rss_rum.pole_id
  			;
  
  			RAISE NOTICE '%' , 'Application des regles PMSI MCO. Validation des poles. p_rss_diagnostics';
  			
  			UPDATE pmsi.p_rss_diagnostics
  			SET pole_id = p_rss_rum.pole_id
  			FROM pmsi.p_rss_rum
  			WHERE p_rss_rum.rss_id = p_rss_diagnostics.rss_id AND
  				p_rss_rum.no_rum = p_rss_diagnostics.no_rum AND
  				p_rss_diagnostics.pole_id IS DISTINCT FROM p_rss_rum.pole_id
  			;
  
  			RAISE NOTICE '%' , 'Application des regles PMSI MCO. Validation des poles. p_rss';
  			
  			UPDATE pmsi.p_rss
  			SET pole_id = subview.pole_id
  			FROM
  			(
  			SELECT p_rss_rum.rss_id, MAX(p_rss_rum.pole_id) AS pole_id
  			FROM pmsi.p_rss_rum
  			JOIN pmsi.p_rss ON p_rss_rum.rss_id = p_rss.oid AND
  				p_rss_rum.unite_medicale_id = p_rss.unite_medicale_principale_id
  			GROUP BY 1
  			HAVING MAX(p_rss.pole_id) IS DISTINCT FROM MAX(p_rss_rum.pole_id)
  			) subview
  			WHERE p_rss.oid = subview.rss_id
  			;
  
  			
  			INSERT INTO pmsi.p_oids (code_table, oid) 
  			SELECT 'pole', pole_id 
  			FROM pmsi.p_rss_rum
  			WHERE pole_id NOT IN (SELECT oid FROM pmsi.p_oids WHERE code_table = 'pole')
  			GROUP BY 2;
                  
  		END IF;
  
  		IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema = 'pmsissr' AND table_name = 'p_ssr' AND column_name = 'pole_id') THEN
  
  			UPDATE pmsissr.p_ssr
  			SET pole_id = subview.pole_id
  			FROM
  				      (
  				      SELECT p_ssr.oid, (MAX(ARRAY[p_mouvements_sejour.date::text,p_mouvements_sejour.pole_id::text]))[2]::bigint AS pole_id
  				      FROM activite.p_mouvements_sejour
  				      JOIN activite.p_sejour_pmsi ON p_mouvements_sejour.no_sejour = p_sejour_pmsi.no_sejour
  				      JOIN pmsissr.p_ssr ON p_ssr.oid = ANY(ssr_id_array)
  				      GROUP BY 1
  				      ) subview
  			WHERE p_ssr.oid = subview.oid
  			;
  
  		END IF;
  
  
  	RETURN 'OK';
  END;