<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	
	<NODE name="PMSICOMPLEMENTS" label="RECUPERATION DES DONNEES COMPLEMENTAIRES PMSI">
		
		<NODE label="Paramètres">
			<sqlcmd><![CDATA[
				
				
				
				-- Spécialités Officielles ------------------------------------
				
				DROP TABLE IF EXISTS w_spec
				;
				
				CREATE TEMP TABLE w_spec AS
				SELECT
					tb_specialite_med.code_spe_med as code,
					tb_specialite_med.libelle_spe_med as texte
				FROM
					prod_cora.tb_specialite_med
				WHERE
					tb_specialite_med.id_spe_med > 0
				;
				
				INSERT INTO pmsi.t_specialites_medecin(code, texte)
				SELECT
					code,
					texte
				FROM
					w_spec
				WHERE
					w_spec.code not in (SELECT DISTINCT code FROM pmsi.t_specialites_medecin)
				;
				
				UPDATE 
					pmsi.t_specialites_medecin
				SET 
					texte = w_spec.texte
				FROM 
					w_spec
				WHERE 
					t_specialites_medecin.code = w_spec.code
					AND t_specialites_medecin.code = t_specialites_medecin.texte
				;

				
				
				-- Spécialités Internes ---------------------------------------
				
				DROP TABLE IF EXISTS w_spec_int
				;
				
				CREATE TEMP TABLE w_spec_int AS
				SELECT
					tb_specialite_med.code_spe_med || '..' as code,
					tb_specialite_med.libelle_spe_med as texte
				FROM
					prod_cora.tb_specialite_med
				WHERE
					tb_specialite_med.id_spe_med > 0
				;
				
				INSERT INTO pmsi.t_specialites_internes_medecin(code, texte)
				SELECT
					code,
					texte
				FROM
					w_spec_int
				WHERE
					w_spec_int.code NOT IN (SELECT DISTINCT code FROM pmsi.t_specialites_internes_medecin)
				;
				
				UPDATE 
					pmsi.t_specialites_internes_medecin
				SET 
					texte = w_spec_int.texte
				FROM
					w_spec_int
				WHERE
					t_specialites_internes_medecin.code = w_spec_int.code
					AND t_specialites_internes_medecin.code = t_specialites_internes_medecin.texte
				;
				
				
				
				-- Médecins ---------------------------------------------------
				
				DROP TABLE IF EXISTS w_medecin
				;
				
				CREATE TEMP TABLE w_medecin AS
				SELECT
					util.code_util as code,
					util.nom_util as nom,
					CASE WHEN prenom_util like '/%' then '' ELSE prenom_util END as prenom,
					COALESCE(t_specialites_medecin.oid, 0) as spe_med,
					COALESCE(util.matricule, '') as no_adeli,
					COALESCE(t_specialites_internes_medecin.oid, 0) as spe_interne_med
					
				FROM
					prod_cora.util
					JOIN prod_cora.tb_specialite_med ON util.id_spe_med = tb_specialite_med.id_spe_med
					LEFT JOIN pmsi.t_specialites_medecin ON t_specialites_medecin.code = tb_specialite_med.code_spe_med
					LEFT JOIN pmsi.t_specialites_internes_medecin ON t_specialites_internes_medecin.code = tb_specialite_med.code_spe_med || '..'
				WHERE util.id_util in
					(
						select distinct id_medecin_responsable from prod_cora.tb_mvt
						union
						select distinct id_medecin_resp_sej from prod_cora.tb_sejour
						union
						select distinct id_medecin from prod_cora.tb_det_acte
					)
				;
				
				INSERT INTO pmsi.t_medecins(code, nom, prenom, specialite_id, no_adeli, specialite_interne_id)
				SELECT
					code,
					nom,
					prenom,
					spe_med,
					no_adeli,
					spe_interne_med
				FROM
					w_medecin
				WHERE 
					w_medecin.code NOT IN (SELECT code FROM pmsi.t_medecins GROUP BY 1)
				;
				
				UPDATE pmsi.t_medecins
				SET
					nom = w_medecin.nom,
					prenom = w_medecin.prenom
				FROM
					w_medecin
				WHERE
					t_medecins.code = w_medecin.code
					AND (
						t_medecins.nom IS DISTINCT FROM w_medecin.nom
						OR t_medecins.prenom IS DISTINCT FROM w_medecin.prenom
					)
				;
				
				SELECT pmsi.cti_reorganize_medecins_c()
				;
				
				
				
				-- Unités médicales -------------------------------------------
				
				DROP TABLE IF EXISTS w_um; 
				CREATE TEMP TABLE w_um AS
				SELECT
					tb_unite_medicale.id_unite_med AS id,
					tb_unite_medicale.code_unite_med AS code,
					MAX(tb_unite_medicale.libelle_unite_med) AS texte,
					MAX(tb_unite.type_autorisation) AS type_aut,
					MAX(tb_unite.nb_lit_autorisation) AS nb_lit
				FROM
					prod_cora.tb_unite_m_unite
					LEFT JOIN prod_cora.tb_unite_medicale ON tb_unite_m_unite.id_unite_med = tb_unite_medicale.id_unite_med
					LEFT JOIN prod_cora.tb_unite ON tb_unite.id_unite = tb_unite_m_unite.id_unite
				GROUP BY 1, 2
				;
				
				INSERT INTO w_um
				SELECT
					tb_unite.id_unite AS id,
					tb_unite.code_unite AS code,
					MAX(tb_unite.libelle_unite) AS texte,
					'' AS type_aut,
					0 AS nb_lit
				FROM
					prod_cora.tb_unite
				WHERE tb_unite.code_unite not in (select code from w_um) AND
					tb_unite.id_unite NOT IN (select id from w_um) AND
					tb_unite.code_unite = ANY (ARRAY(SELECT UF_ENTREE FROM prod_cora.TB_SEJOUR GROUP BY 1)::text[])
				GROUP BY 1, 2
				;
				
				INSERT INTO pmsi.t_unites_medicales(code, texte, type_autorisation)
				SELECT
					code,
					texte,
					type_aut
				FROM 
					w_um
				WHERE code NOT IN (SELECT code FROM pmsi.t_unites_medicales)
				;
				
				UPDATE pmsi.t_unites_medicales
				SET 
					texte = w_um.texte,
					type_autorisation = w_um.type_aut
				FROM 
					w_um
				WHERE
					t_unites_medicales.code = w_um.code
					AND (t_unites_medicales.texte = 'Unité ' || t_unites_medicales.code
						OR t_unites_medicales.texte = t_unites_medicales.code
						OR t_unites_medicales.type_autorisation <> w_um.type_aut)
				;
				
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Identités et Médecin RSS">
			<sqlcmd><![CDATA[
				
				DROP TABLE IF EXISTS w_cora;
				CREATE TEMP TABLE w_cora AS
				SELECT
					tb_synth_rss.no_rss::bigint as no_rss,
					p_rss.oid as id_rss,
					tb_patient.ipp_patient as ipp_patient,
					tb_synth_rss.id_sejour,
					tb_synth_rss.no_sejour,
					tb_patient.nom_usuel_patient as nom_patient,
					tb_patient.prenom_patient as prenom_patient,
					tb_patient.nom_naissance as nom_naissance_patient,
					tb_sejour.uf_entree,
					COALESCE(t_medecins.oid,0) as id_medecin_rss
				FROM
					prod_cora.tb_synth_rss
					JOIN prod_cora.tb_sejour ON tb_synth_rss.id_sejour = tb_sejour.id_sejour
					JOIN prod_cora.tb_patient ON tb_sejour.id_patient = tb_patient.id_patient
					JOIN [SCHEMA].p_rss ON p_rss.no_rss = tb_synth_rss.no_rss AND p_rss.date_sortie = date(tb_synth_rss.date_fin)
					LEFT JOIN prod_cora.util ON tb_sejour.id_medecin_resp_sej = util.id_util
					LEFT JOIN pmsi.t_medecins ON util.code_util = t_medecins.code
				;
				
				
				
				CREATE INDEX w_cora_ix_id_rss
				ON w_cora
				USING btree
				(id_rss);

				CREATE INDEX w_cora_ix_id_sejour
				ON w_cora
				USING btree
				(id_sejour);
				
				
				-- Externes
				INSERT INTO w_cora
				SELECT
					p_rss.no_rss as no_rss,
					p_rss.oid as id_rss,
					tb_patient.ipp_patient as ipp_patient,
					tb_sejour.id_sejour,
					tb_sejour.no_sejour,
					tb_patient.nom_usuel_patient as nom_patient,
					tb_patient.prenom_patient as prenom_patient,
					tb_patient.nom_naissance as nom_naissance_patient,
					tb_sejour.UF_ENTREE,
					COALESCE(t_medecins.oid,0) as id_medecin_rss
				FROM
					prod_cora.tb_sejour 
					JOIN prod_cora.tb_patient ON tb_sejour.id_patient = tb_patient.id_patient
					JOIN [SCHEMA].p_rss ON p_rss.no_sejour_administratif = tb_sejour.no_sejour AND p_rss.date_sortie = date(tb_sejour.date_sortie)
					LEFT JOIN prod_cora.util ON tb_sejour.id_medecin_resp_sej = util.id_util
					LEFT JOIN pmsi.t_medecins ON util.code_util = t_medecins.code
				WHERE p_rss.oid NOT IN (SELECt id_rss FROM w_cora)
				;
				
				INSERT INTO w_cora
				SELECT
					p_rss.no_rss as no_rss,
					p_rss.oid as id_rss,
					tb_patient.ipp_patient as ipp_patient,
					tb_sejour.id_sejour,
					tb_sejour.no_sejour,
					tb_patient.nom_usuel_patient as nom_patient,
					tb_patient.prenom_patient as prenom_patient,
					tb_patient.nom_naissance as nom_naissance_patient,
					tb_sejour.UF_ENTREE,
					COALESCE(t_medecins.oid,0) as id_medecin_rss
				FROM
					prod_cora.tb_sejour 
					JOIN prod_cora.tb_patient ON tb_sejour.id_patient = tb_patient.id_patient
					JOIN [SCHEMA].p_rss ON p_rss.no_sejour_administratif = tb_sejour.no_sejour AND p_rss.date_entree = date(tb_sejour.date_entree)
					LEFT JOIN prod_cora.util ON tb_sejour.id_medecin_resp_sej = util.id_util
					LEFT JOIN pmsi.t_medecins ON util.code_util = t_medecins.code
				WHERE p_rss.oid NOT IN (SELECt id_rss FROM w_cora)
				;

				INSERT INTO w_cora
				SELECT
					p_rss.no_rss as no_rss,
					p_rss.oid as id_rss,
					tb_patient.ipp_patient as ipp_patient,
					tb_sejour.id_sejour,
					tb_sejour.no_sejour,
					tb_patient.nom_usuel_patient as nom_patient,
					tb_patient.prenom_patient as prenom_patient,
					tb_patient.nom_naissance as nom_naissance_patient,
					UF_ENTREE,
					COALESCE(t_medecins.oid,0) as id_medecin_rss
				FROM
					prod_cora.tb_sejour 
					JOIN prod_cora.tb_patient ON tb_sejour.id_patient = tb_patient.id_patient
					JOIN [SCHEMA].p_rss ON p_rss.no_sejour_administratif = tb_sejour.no_sejour AND p_rss.date_sortie = date(tb_sejour.date_entree) AND tb_sejour.date_sortie IS NULL
					LEFT JOIN prod_cora.util ON tb_sejour.id_medecin_resp_sej = util.id_util
					LEFT JOIN pmsi.t_medecins ON util.code_util = t_medecins.code
				WHERE p_rss.oid NOT IN (SELECt id_rss FROM w_cora)
					
				;

				
				
				-- IPP du patient et Médecin Responsable du RSS ---------------
				WITH w_maj as (
                    SELECT  
                        w_cora.id_rss,
                        to_number(w_cora.ipp_patient, '000000000000000')::bigint as patient,
                        w_cora.id_medecin_rss as medecin
                    FROM
                        w_cora
                )
				UPDATE [SCHEMA].p_rss
				SET 
					no_patient = w_maj.patient,
					medecin_rss_id = w_maj.medecin
				FROM
					w_maj
				WHERE
					p_rss.oid = w_maj.id_rss
					AND (	p_rss.no_patient <> w_maj.patient
							OR p_rss.medecin_rss_id <> w_maj.medecin
						)
				;
				
				
				-- Nom / Prénom du patient ------------------------------------
				
				UPDATE [SCHEMA].p_identites
				SET
					nom = w_cora.nom_patient,
					prenom = w_cora.prenom_patient,
					nom_naissance = w_cora.nom_naissance_patient
				FROM
					w_cora
				WHERE
					p_identites.rss_id = w_cora.id_rss
					AND (	p_identites.nom <> w_cora.nom_patient
						OR p_identites.prenom <> w_cora.prenom_patient
						OR p_identites.nom_naissance <> w_cora.nom_naissance_patient
					)
				;
				
				-- Unite medicale des externes
				UPDATE [SCHEMA].p_rss
				SET 
					unite_medicale_principale_id = t_unites_medicales.oid
				FROM
					w_cora
					JOIN pmsi.t_unites_medicales ON UF_ENTREE = t_unites_medicales.code
				WHERE
					p_rss.oid = w_cora.id_rss AND
					nb_rum = 1 AND
					unite_medicale_principale_id = 0
					
				;

				UPDATE [SCHEMA].p_rss_rum
				SET unite_medicale_id = unite_medicale_principale_id
				FROM [SCHEMA].p_rss
				WHERE rss_id = p_rss.oid AND
					unite_medicale_id = 0 AND
					unite_medicale_principale_id <> 0;

			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Médecins actes">
			<sqlcmd><![CDATA[
				
				UPDATE [SCHEMA].p_rss_actes
				SET
					medecin_id = t_medecins.oid
				FROM
					prod_cora.tb_acte
					JOIN w_cora ON tb_acte.id_sejour = w_cora.id_sejour 	
					JOIN prod_cora.tb_det_acte ON tb_acte.id_acte = tb_det_acte.id_acte
					JOIN prod_cora.util ON tb_det_acte.id_medecin = util.id_util
					JOIN pmsi.t_medecins ON t_medecins.code = util.code_util
					JOIN pmsi.t_actes ON t_actes.code = substring(tb_acte.code_acte, 1, 7)
				WHERE
				    p_rss_actes.rss_id = w_cora.id_rss
					AND p_rss_actes.date_acte = tb_acte.date_acte
					AND p_rss_actes.acte_id = t_actes.oid
					AND p_rss_actes.activite_ccam = tb_det_acte.id_activite
					AND p_rss_actes.medecin_id <> t_medecins.oid
				;
				
				-- Je n'ai pas su récupérer les médecins des actes facturés mais qui n'apparaissent pas dans les RSS
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
	<NODE name="PMSIENCOURS" label="RECUPERATION DES DOSSIERS PMSI EN-COURS CORA">
		
		<NODE label="Paramètres">
			<sqlcmd><![CDATA[
				-- select * from information_schema.columns where column_name like '%igs2%' and table_schema = 'prod_cora'
				
				
				-- Spécialités Officielles ------------------------------------
				
				DROP TABLE IF EXISTS w_spec
				;
				
				CREATE TEMP TABLE w_spec AS
				SELECT
					tb_specialite_med.code_spe_med as code,
					tb_specialite_med.libelle_spe_med as texte
				FROM
					prod_cora.tb_specialite_med
				WHERE
					tb_specialite_med.id_spe_med > 0
				;
				
				INSERT INTO pmsi.t_specialites_medecin(code, texte)
				SELECT
					code,
					texte
				FROM
					w_spec
				WHERE
					w_spec.code not in (SELECT DISTINCT code FROM pmsi.t_specialites_medecin)
				;
				
				UPDATE 
					pmsi.t_specialites_medecin
				SET 
					texte = w_spec.texte
				FROM 
					w_spec
				WHERE 
					t_specialites_medecin.code = w_spec.code
					AND t_specialites_medecin.code = t_specialites_medecin.texte
				;

				
				
				-- Spécialités Internes ---------------------------------------
				
				DROP TABLE IF EXISTS w_spec_int
				;
				
				CREATE TEMP TABLE w_spec_int AS
				SELECT
					tb_specialite_med.code_spe_med || '..' as code,
					tb_specialite_med.libelle_spe_med as texte
				FROM
					prod_cora.tb_specialite_med
				WHERE
					tb_specialite_med.id_spe_med > 0
				;
				
				INSERT INTO pmsi.t_specialites_internes_medecin(code, texte)
				SELECT
					code,
					texte
				FROM
					w_spec_int
				WHERE
					w_spec_int.code NOT IN (SELECT DISTINCT code FROM pmsi.t_specialites_internes_medecin)
				;
				
				UPDATE 
					pmsi.t_specialites_internes_medecin
				SET 
					texte = w_spec_int.texte
				FROM
					w_spec_int
				WHERE
					t_specialites_internes_medecin.code = w_spec_int.code
					AND t_specialites_internes_medecin.code = t_specialites_internes_medecin.texte
				;

				
				
				-- Médecins ---------------------------------------------------
				
				DROP TABLE IF EXISTS w_medecin
				;
				
				CREATE TEMP TABLE w_medecin AS
				SELECT
					util.code_util as code,
					util.nom_util as nom,
					CASE WHEN prenom_util like '/%' then '' ELSE prenom_util END as prenom,
					COALESCE(t_specialites_medecin.oid, 0) as spe_med,
					COALESCE(util.matricule, '') as no_adeli,
					COALESCE(t_specialites_internes_medecin.oid, 0) as spe_interne_med
					
				FROM
					prod_cora.util
					JOIN prod_cora.tb_specialite_med ON util.id_spe_med = tb_specialite_med.id_spe_med
					LEFT JOIN pmsi.t_specialites_medecin ON t_specialites_medecin.code = tb_specialite_med.code_spe_med
					LEFT JOIN pmsi.t_specialites_internes_medecin ON t_specialites_internes_medecin.code = tb_specialite_med.code_spe_med || '..'
				WHERE util.id_util in
					(
						select distinct id_medecin_responsable from prod_cora.tb_mvt
						union
						select distinct id_medecin_resp_sej from prod_cora.tb_sejour
						union
						select distinct id_medecin from prod_cora.tb_det_acte
					)
				;
				
				INSERT INTO pmsi.t_medecins(code, nom, prenom, specialite_id, no_adeli, specialite_interne_id)
				SELECT
					code,
					nom,
					prenom,
					spe_med,
					no_adeli,
					spe_interne_med
				FROM
					w_medecin
				WHERE 
					w_medecin.code NOT IN (SELECT code FROM pmsi.t_medecins GROUP BY 1)
				;
				
				UPDATE pmsi.t_medecins
				SET
					nom = w_medecin.nom,
					prenom = w_medecin.prenom
				FROM
					w_medecin
				WHERE
					t_medecins.code = w_medecin.code
					AND (
						t_medecins.nom IS DISTINCT FROM w_medecin.nom
						OR t_medecins.prenom IS DISTINCT FROM w_medecin.prenom
					)
				;
				
				SELECT pmsi.cti_reorganize_medecins_c()
				;
				
				
				
				-- Unités médicales -------------------------------------------
				
				DROP TABLE IF EXISTS w_um; 
				CREATE TEMP TABLE w_um AS
				SELECT
					tb_unite_medicale.id_unite_med AS id,
					tb_unite_medicale.code_unite_med AS code,
					MAX(tb_unite_medicale.libelle_unite_med) AS texte,
					MAX(tb_unite.type_autorisation) AS type_aut,
					MAX(tb_unite.nb_lit_autorisation) AS nb_lit
				FROM
					prod_cora.tb_unite_medicale
					JOIN prod_cora.tb_unite_m_unite ON tb_unite_m_unite.id_unite_med = tb_unite_medicale.id_unite_med
					JOIN prod_cora.tb_unite ON tb_unite.id_unite = tb_unite_m_unite.id_unite
				GROUP BY 1, 2
				;
				
				INSERT INTO w_um
				SELECT
					tb_unite.id_unite AS id,
					tb_unite.code_unite AS code,
					MAX(tb_unite.libelle_unite) AS texte,
					'' AS type_aut,
					0 AS nb_lit
				FROM
					prod_cora.tb_unite
				WHERE tb_unite.code_unite not in (select code from w_um) AND
					tb_unite.id_unite NOT IN (select id from w_um) AND
					tb_unite.code_unite = ANY(ARRAY(SELECT UF_ENTREE FROM prod_cora.TB_SEJOUR GROUP BY 1)::text[])
				GROUP BY 1, 2
				;
				
				INSERT INTO pmsi.t_unites_medicales(code, texte, type_autorisation)
				SELECT
					code,
					texte,
					type_aut
				FROM 
					w_um
				WHERE code NOT IN (SELECT code FROM pmsi.t_unites_medicales)
				;
				
				UPDATE pmsi.t_unites_medicales
				SET 
					texte = w_um.texte,
					type_autorisation = w_um.type_aut
				FROM 
					w_um
				WHERE
					t_unites_medicales.code = w_um.code
					AND (t_unites_medicales.texte = 'Unité ' || t_unites_medicales.code
						OR t_unites_medicales.texte = t_unites_medicales.code
						OR t_unites_medicales.type_autorisation <> w_um.type_aut)
				;
				
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="RSS">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_finess
				;
				CREATE TEMP TABLE w_finess AS
				SELECT (MAX(Array[date_sortie::text,finess]))[2] AS no_finess
				FROM pmsi.p_rss
				WHERE en_cours = '0'
				;
				
				
				
				DROP TABLE IF EXISTS w_TB_SYNTH_RSS_sejour;
				CREATE TEMP TABLE w_TB_SYNTH_RSS_sejour AS
				SELECT ID_SEJOUR, 
					date_fin,
					MAX(NO_RSS) AS NO_RSS
				FROM prod_cora.TB_SYNTH_RSS
				WHERE date_fin  >= '20140101' AND NO_RSS > 0
				GROUP BY 1,2;
				
				CREATE INDEX w_TB_SYNTH_RSS_sejour_i1
				  ON w_TB_SYNTH_RSS_sejour
				  USING btree
				  (NO_RSS);
				  
				  
				CREATE INDEX w_TB_SYNTH_RSS_sejour_i2
				  ON w_TB_SYNTH_RSS_sejour
				  USING btree
				  (ID_SEJOUR);

				DELETE FROM w_TB_SYNTH_RSS_sejour
				USING pmsi.p_rss
				WHERE w_TB_SYNTH_RSS_sejour.no_rss = p_rss.no_rss AND 
					p_rss.etat = '' AND 
					date_trunc('month',w_TB_SYNTH_RSS_sejour.date_fin) = date_trunc('month', p_rss.date_sortie);

				VACUUM FULL ANALYSE w_TB_SYNTH_RSS_sejour;
				
				
				DROP TABLE IF EXISTS w_mvt
				;
				
				CREATE TEMP TABLE w_mvt AS
				SELECT 
					w_TB_SYNTH_RSS_sejour.id_sejour AS id_sejour,
					w_TB_SYNTH_RSS_sejour.NO_RSS,
					SUM(tb_mvt.nb_sea) AS nb_seances,
					max(CASE WHEN length(trim(tb_mvt.igs2)) = 0 THEN 0 ELSE to_number(tb_mvt.igs2, '999') END) AS igs2
				FROM
					w_TB_SYNTH_RSS_sejour
					JOIN prod_cora.tb_mvt ON tb_mvt.id_sejour = w_TB_SYNTH_RSS_sejour.id_sejour
				GROUP BY 1,2
				;


				DROP TABLE IF EXISTS w_med_sej
				; 
				CREATE TEMP TABLE w_med_sej AS 
				SELECT 
					w_TB_SYNTH_RSS_sejour.id_sejour, 
					w_TB_SYNTH_RSS_sejour.NO_RSS,
					MAX(t_medecins.oid) AS medecin_id
				FROM 
					w_TB_SYNTH_RSS_sejour
					LEFT JOIN prod_cora.tb_sejour ON w_TB_SYNTH_RSS_sejour.id_sejour = tb_sejour.id_sejour 
					LEFT JOIN prod_cora.util ON util.id_util = tb_sejour.id_medecin_resp_sej 
					LEFT JOIN pmsi.t_medecins ON t_medecins.code = util.code_util
				GROUP BY 1,2
				;

				DROP TABLE IF EXISTS w_um_rss;
				CREATE TEMP TABLE w_um_rss AS
				SELECT 
					w_TB_SYNTH_RSS_sejour.id_sejour, 
					w_TB_SYNTH_RSS_sejour.NO_RSS,
					MAX(COALESCE(t_unites_medicales.oid , 0)) AS oid
				FROM 
					w_TB_SYNTH_RSS_sejour
					JOIN prod_cora.tb_synth_rss ON tb_synth_rss.ID_SEJOUR = w_TB_SYNTH_RSS_sejour.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_TB_SYNTH_RSS_sejour.NO_RSS
					LEFT JOIN w_um ON w_um.id = TB_SYNTH_RSS.id_um_retenu 
					LEFT JOIN pmsi.t_unites_medicales ON w_um.code = t_unites_medicales.code
				GROUP BY 1,2
				;
				
				
				DROP TABLE IF EXISTS w_rss
				;
				CREATE TEMP TABLE w_rss AS
				SELECT
					base.cti_to_number(tb_patient.ipp_patient) AS no_patient,
					(SELECT no_finess FROM w_finess) AS finess,
					base.cti_to_number(tb_synth_rss.no_rss::text) AS no_rss,
					tb_synth_rss.version AS version_groupage,
					tb_patient.date_nais AS date_naissance,
					CASE WHEN tb_patient.sexe = 'M' THEN 1 ELSE 2 END AS sexe,
					tb_synth_rss.date_debut AS date_entree,
					tb_synth_rss.date_fin AS date_sortie,
					tb_synth_rss.mode_entree AS mode_entree,
					tb_synth_rss.code_provenance AS provenance,
					tb_synth_rss.mode_sortie AS mode_sortie,
					tb_synth_rss.code_destination AS destination,
					substr(tb_patient.code_postal,1,5) AS code_postal,
					tb_patient.poid_naissance AS poids_bebe,
					COALESCE(w_mvt.igs2, 0) AS igs2,
					''::text AS cma,
					substr(tb_synth_rss.code_ghm , 1 , 1) AS groupe_ghm,
					tb_synth_rss.duree_sejour AS duree_sejour,
					1 AS nb_rsa,
					CASE WHEN tb_synth_rss.date_fin::date > tb_patient.date_nais::date THEN trunc((tb_synth_rss.date_debut::date - tb_patient.date_nais::date) / 365.25,0) ELSE 0 END AS age,
					''::text AS supprime,
					now() AS date_import,
					tb_synth_rss.nb_rum AS nb_rum,
					''::text AS secteur,
					tb_synth_rss.no_sejour AS no_sejour_administratif,
					CASE WHEN tb_synth_rss.code_cmd = '28' THEN 1 ELSE 0 END AS nb_seances,
					''::text AS ghm_fg9,
					COALESCE(t_ghm.oid, 0) AS ghm_id,
					w_med_sej.medecin_id AS medecin_rss_id,
					COALESCE(t_ghs.oid, 0) AS ghs_id,
					date_part('year', tb_synth_rss.date_fin) * 100 + date_part('month', tb_synth_rss.date_fin) AS mois_sortie,
					COALESCE(t_diagnostics.oid, 0 ) AS diagnostic_principal_id,
					COALESCE(dr.oid, 0) AS diagnostic_relie_id,
					COALESCE(t_ghm.oid, 0) AS ghm_production_id,
					1 AS no_rum_principal,	--dernier rum
					COALESCE(w_um_rss.oid, 0) AS unite_medicale_principale_id,	
					nextval('pmsi.s_rss'::regclass) AS oid,
					-1 AS import_id,
					''::text AS etat,
					0 AS acte_principal_id,	-- icr le plus lourd
					0 AS code_postal_id,
					0 AS patient_id,
					0 AS prestation_principale_id,
					''::text AS severite_fg11_simulation_code,
					'1'::text AS en_cours,
					CASE WHEN length(tb_synth_rss.code_cmd || tb_synth_rss.code_ghm) = 6 AND tb_synth_rss.code_cmd <> '90' AND tb_synth_rss.num_lot <> 0 THEN 'V' WHEN length(tb_synth_rss.code_cmd || tb_synth_rss.code_ghm) = 6 AND tb_synth_rss.code_cmd <> '90' THEN 'G' ELSE 'S' END AS en_cours_etat, 
					0 AS base_remboursement, 
					0 AS sejour_facture, 
					0 AS honoraires_factures, 
					0 AS t2a_facture, 
					0 AS dmi_facture, 
					0 AS phc_facture, 
					0 AS ca_ghs_theorique, 
					0 AS nb_ghs, 
					0 AS ca_ghs, 
					0 AS nb_exb, 
					0 AS ca_exb, 
					0 AS nb_exh, 
					0 AS ca_exh, 
					0 AS nb_si, 
					0 AS ca_si, 
					0 AS nb_sur, 
					0 AS ca_sur, 
					0 AS nb_rea, 
					0 AS ca_rea, 
					0 AS nb_neonat, 
					0 AS ca_neonat, 
					''::text AS traitement_epmsi, 
					''::text AS code_retour_groupage, 
					0 AS comite_medical_id, 
					'0'::text AS rehosp_meme_ghm, 
					0 AS from_oid,
					0 AS forfait_facture,
					0 AS age_gestationnel,
					'2099-12-31'::date AS date_dernieres_regles,
					''::text AS confirmation_codage,
					''::text AS type_machine_rdth,
					''::text AS type_dosimetrie,
					''::text AS numero_innovation,
					0 AS rss_mere_id,	
					base.cti_to_number(tb_synth_rss.id_rss::text) AS id_rss,
					tb_patient.nom_usuel_patient as nom_patient,
					tb_patient.prenom_patient as prenom_patient,
					tb_patient.nom_naissance as nom_naissance_patient,
					tb_synth_rss.id_sejour AS id_sejour
				FROM 
					prod_cora.tb_synth_rss
					JOIN w_TB_SYNTH_RSS_sejour ON tb_synth_rss.ID_SEJOUR = w_TB_SYNTH_RSS_sejour.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_TB_SYNTH_RSS_sejour.NO_RSS
					JOIN prod_cora.tb_patient ON tb_patient.id_patient = tb_synth_rss.id_patient
					LEFT JOIN pmsi.t_ghm ON t_ghm.code = tb_synth_rss.code_cmd || tb_synth_rss.code_ghm
					LEFT JOIN w_med_sej ON tb_synth_rss.ID_SEJOUR = w_med_sej.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_med_sej.NO_RSS
					LEFT JOIN pmsi.t_ghs ON t_ghs.code = tb_synth_rss.code_ghs
					LEFT JOIN pmsi.t_diagnostics ON t_diagnostics.code = tb_synth_rss.dp_retenu
					LEFT JOIN pmsi.t_diagnostics dr ON dr.code = tb_synth_rss.dr_retenu
					LEFT JOIN w_um_rss ON tb_synth_rss.ID_SEJOUR = w_um_rss.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_um_rss.NO_RSS
					LEFT JOIN w_mvt ON tb_synth_rss.ID_SEJOUR = w_mvt.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_mvt.NO_RSS
				ORDER BY no_rss
				;
				
				
				
				INSERT INTO pmsi.p_rss (
					no_patient,
					finess,
					no_rss,
					version_groupage,
					date_naissance,
					sexe,
					date_entree,
					date_sortie,
					mode_entree,
					provenance,
					mode_sortie,
					destination,
					code_postal,
					poids_bebe,
					igs2,
					cma,
					groupe_ghm,
					duree_sejour,
					nb_rsa,
					age,
					supprime,
					date_import,
					nb_rum,
					secteur,
					no_sejour_administratif,
					nb_seances,
					ghm_fg9,
					ghm_id,
					medecin_rss_id,
					ghs_id,
					mois_sortie,
					diagnostic_principal_id,
					diagnostic_relie_id,
					ghm_production_id,
					no_rum_principal,
					unite_medicale_principale_id,
					oid,
					import_id,
					etat,
					acte_principal_id,
					code_postal_id,
					patient_id,
					prestation_principale_id,
					severite_fg11_simulation_code,
					en_cours,
					en_cours_etat, 
					base_remboursement, 
					sejour_facture, 
					honoraires_factures, 
					t2a_facture, 
					dmi_facture, 
					phc_facture, 
					ca_ghs_theorique, 
					nb_ghs, 
					ca_ghs, 
					nb_exb, 
					ca_exb, 
					nb_exh, 
					ca_exh, 
					nb_si, 
					ca_si, 
					nb_sur, 
					ca_sur, 
					nb_rea, 
					ca_rea, 
					nb_neonat, 
					ca_neonat, 
					traitement_epmsi, 
					code_retour_groupage, 
					comite_medical_id, 
					rehosp_meme_ghm, 
					from_oid,
					forfait_facture,
					age_gestationnel,
					date_dernieres_regles,
					confirmation_codage,
					type_machine_rdth,
					type_dosimetrie,
					numero_innovation,
					rss_mere_id
				)
				SELECT
					w_rss.no_patient,
					w_rss.finess,
					w_rss.no_rss,
					w_rss.version_groupage,
					w_rss.date_naissance,
					w_rss.sexe,
					w_rss.date_entree,
					w_rss.date_sortie,
					w_rss.mode_entree,
					w_rss.provenance,
					w_rss.mode_sortie,
					w_rss.destination,
					w_rss.code_postal,
					w_rss.poids_bebe,
					w_rss.igs2,
					w_rss.cma,
					w_rss.groupe_ghm,
					w_rss.duree_sejour,
					w_rss.nb_rsa,
					w_rss.age,
					w_rss.supprime,
					w_rss.date_import,
					w_rss.nb_rum,
					w_rss.secteur,
					w_rss.no_sejour_administratif,
					w_rss.nb_seances,
					w_rss.ghm_fg9,
					w_rss.ghm_id,
					w_rss.medecin_rss_id,
					w_rss.ghs_id,
					w_rss.mois_sortie,
					w_rss.diagnostic_principal_id,
					w_rss.diagnostic_relie_id,
					w_rss.ghm_production_id,
					w_rss.no_rum_principal,
					w_rss.unite_medicale_principale_id,
					w_rss.oid,
					w_rss.import_id,
					w_rss.etat,
					w_rss.acte_principal_id,
					w_rss.code_postal_id,
					w_rss.patient_id,
					w_rss.prestation_principale_id,
					w_rss.severite_fg11_simulation_code,
					w_rss.en_cours,
					w_rss.en_cours_etat, 
					w_rss.base_remboursement, 
					w_rss.sejour_facture, 
					w_rss.honoraires_factures, 
					w_rss.t2a_facture, 
					w_rss.dmi_facture, 
					w_rss.phc_facture, 
					w_rss.ca_ghs_theorique, 
					w_rss.nb_ghs, 
					w_rss.ca_ghs, 
					w_rss.nb_exb, 
					w_rss.ca_exb, 
					w_rss.nb_exh, 
					w_rss.ca_exh, 
					w_rss.nb_si, 
					w_rss.ca_si, 
					w_rss.nb_sur, 
					w_rss.ca_sur, 
					w_rss.nb_rea, 
					w_rss.ca_rea, 
					w_rss.nb_neonat, 
					w_rss.ca_neonat, 
					w_rss.traitement_epmsi, 
					w_rss.code_retour_groupage, 
					w_rss.comite_medical_id, 
					w_rss.rehosp_meme_ghm, 
					w_rss.from_oid,
					w_rss.forfait_facture,
					w_rss.age_gestationnel,
					w_rss.date_dernieres_regles,
					w_rss.confirmation_codage,
					w_rss.type_machine_rdth,
					w_rss.type_dosimetrie,
					w_rss.numero_innovation,
					w_rss.rss_mere_id
				FROM 
					w_rss
				;
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Identités">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.p_identites(
					finess, 
					rss_id,
					no_rss, 
					nom, 
					prenom, 
					nom_naissance
				)
				SELECT 
					w_rss.finess AS finess, 
					w_rss.oid AS oid, 
					w_rss.no_rss AS no_rss, 
					w_rss.nom_patient AS nom, 
					w_rss.prenom_patient AS prenom, 
					w_rss.nom_naissance_patient AS nom_naissance
				FROM 
					w_rss
				;
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Rum">
			<sqlcmd><![CDATA[

				DROP TABLE IF EXISTS w_um_rum; CREATE TEMP TABLE w_um_rum AS
				SELECT 
					tb_synth_rum.id_rum AS id_rum,
					COALESCE(t_unites_medicales.oid, 0) AS oid,
					COALESCE(t_unites_medicales.code, '?' ) AS code
				FROM 
					w_TB_SYNTH_RSS_sejour
					JOIN prod_cora.TB_SYNTH_RSS ON tb_synth_rss.ID_SEJOUR = w_TB_SYNTH_RSS_sejour.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_TB_SYNTH_RSS_sejour.NO_RSS
					JOIN prod_cora.tb_synth_rum ON TB_SYNTH_RSS.id_rss = tb_synth_rum.id_rss
					LEFT JOIN w_um ON w_um.id = tb_synth_rum.id_unite_medicale
					LEFT JOIN pmsi.t_unites_medicales ON t_unites_medicales.code = w_um.code
				;

				
				
				
				-- Table qui associe un id_rum à un id_diagnostic relié
				-- l'id_rum est celui de la prod CORA, l'id_diagnostic est celui de la table pmsi.t_diagnostics
				
				DROP TABLE IF EXISTS w_um_dr; CREATE TEMP TABLE w_um_dr AS
				SELECT 
					tb_synth_rum.id_rum AS id_rum,
					max(t_diagnostics.oid) AS dr_id
				FROM 
					w_TB_SYNTH_RSS_sejour
					JOIN prod_cora.TB_SYNTH_RSS ON tb_synth_rss.ID_SEJOUR = w_TB_SYNTH_RSS_sejour.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_TB_SYNTH_RSS_sejour.NO_RSS
					JOIN prod_cora.tb_synth_rum ON TB_SYNTH_RSS.id_rss = tb_synth_rum.id_rss
					JOIN prod_cora.tb_mvt_rum ON tb_mvt_rum.id_rum = tb_synth_rum.id_rum
					JOIN prod_cora.tb_mvt ON tb_mvt.id_mvt = tb_mvt_rum.id_mvt
					JOIN prod_cora.tb_diag on tb_diag.id_mvt = tb_mvt.id_mvt
					JOIN pmsi.t_diagnostics ON t_diagnostics.code = tb_diag.code_diag
				WHERE tb_diag.type_diag = 'R'
				GROUP BY 1
				ORDER BY 1
				;

				
				-- Table qui, pour chaque no_rss, donne le nombre de rums et un tableau contnant tous les numéros
				-- de rums
				DROP TABLE IF EXISTS w_tmp; CREATE TEMP TABLE w_tmp AS
				SELECT
					tb_synth_rss.no_rss AS no_rss,
					tb_synth_rss.nb_rum AS nb_rum,
					base.cti_array_accum(tb_synth_rum.date_debut_rum) AS rums
				FROM
					w_TB_SYNTH_RSS_sejour
					JOIN prod_cora.TB_SYNTH_RSS ON tb_synth_rss.ID_SEJOUR = w_TB_SYNTH_RSS_sejour.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_TB_SYNTH_RSS_sejour.NO_RSS
					JOIN prod_cora.tb_synth_rum ON TB_SYNTH_RSS.id_rss = tb_synth_rum.id_rss
				GROUP BY 1,2
				ORDER BY 1
				;
				
				
				-- Table qui donne l'ordre de chaque no_rum dans son RSS correspondant
				-- Utilisation de la fonction base.cti_array_search qui permet de faire une recherche d'une valeur
				-- dans un tableau et en retourne l'id
				
				DROP TABLE IF EXISTS w_ordre_rum; CREATE TEMP TABLE w_ordre_rum AS
				SELECT 
					w_tmp.no_rss,
					tb_synth_rum.id_rum,
					base.cti_array_search(base.cti_array_sort(w_tmp.rums), tb_synth_rum.date_debut_rum) AS ordre,
					tb_synth_rum.dp_mvt_retenu AS rum_principal
				FROM
					w_TB_SYNTH_RSS_sejour
					JOIN prod_cora.TB_SYNTH_RSS ON tb_synth_rss.ID_SEJOUR = w_TB_SYNTH_RSS_sejour.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_TB_SYNTH_RSS_sejour.NO_RSS
					JOIN prod_cora.tb_synth_rum ON TB_SYNTH_RSS.id_rss = tb_synth_rum.id_rss
					JOIN w_tmp ON tb_synth_rss.no_rss = w_tmp.no_rss
				ORDER BY 1,2,3
				;
				
				
				INSERT INTO pmsi.p_rss_rum(
					finess, 
					no_rss, 
					rss_id, 
					no_rum, 
					date_entree, 
					date_sortie, 
					mode_entree, 
					provenance, 
					mode_sortie, 
					destination, 
					duree_sejour, 
					nb_seances,
					unite_medicale, 
					unite_medicale_id, 
					diagnostic_principal_id, 
					diagnostic_relie_id, 
					type_autorisation, 
					igs2, 
					supplement_code,
					type_autorisation_lit_dedie
				)
				SELECT
					w_rss.finess AS finess,
					w_rss.no_rss AS no_rss,
					COALESCE(p_rss.oid, 0) AS rss_id,
					w_ordre_rum.ordre AS no_rum,
					tb_synth_rum.date_debut_rum AS date_entree,
					tb_synth_rum.date_fin_rum AS date_sortie,
					tb_synth_rum.mode_entree AS mode_entree,
					tb_synth_rum.code_provenance AS provenance,
					tb_synth_rum.mode_sortie AS mode_sortie,
					tb_synth_rum.code_destination AS destination,
					tb_synth_rum.duree AS duree_sejour,
					w_mvt.nb_seances AS nb_seances,
					w_um_rum.code AS unite_medicale,
					w_um_rum.oid AS unite_medicale_id,
					COALESCE(t_diagnostics.oid, 0) AS diagnostic_principal_id,
					COALESCE(w_um_dr.dr_id, 0) AS diagnostic_relie_id,
					''::text AS type_autorisation,
					w_mvt.igs2,
					''::text AS supplement_code,
					''::text AS type_autorisation_lit_dedie
				FROM
					w_TB_SYNTH_RSS_sejour
					JOIN prod_cora.TB_SYNTH_RSS ON tb_synth_rss.ID_SEJOUR = w_TB_SYNTH_RSS_sejour.ID_SEJOUR AND tb_synth_rss.NO_RSS = w_TB_SYNTH_RSS_sejour.NO_RSS
					JOIN prod_cora.tb_synth_rum ON TB_SYNTH_RSS.id_rss = tb_synth_rum.id_rss
					LEFT JOIN w_um_dr ON w_um_dr.id_rum = tb_synth_rum.id_rum
					JOIN w_rss ON tb_synth_rss.id_sejour = w_rss.id_sejour AND tb_synth_rss.no_rss = w_rss.no_rss
					JOIN w_mvt ON w_rss.id_sejour = w_mvt.id_sejour
					JOIN w_um_rum ON w_um_rum.id_rum = tb_synth_rum.id_rum
					LEFT JOIN pmsi.t_diagnostics ON tb_synth_rum.diag_p = t_diagnostics.code
					LEFT JOIN w_ordre_rum ON w_ordre_rum.id_rum = tb_synth_rum.id_rum
					LEFT JOIN pmsi.p_rss ON w_rss.no_rss = p_rss.no_rss
				;
				
				-- Mise à jour du numéro de RUM principal
				
				UPDATE pmsi.p_rss
				SET
					no_rum_principal = w_ordre_rum.ordre
				FROM
					w_ordre_rum
				WHERE
					p_rss.en_cours = '1'
					AND p_rss.no_rss = w_ordre_rum.no_rss
					AND w_ordre_rum.rum_principal = 'X'
				;
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Diagnostics">
			<sqlcmd><![CDATA[
			
			-- Certains diags n'ont pas d'um associée (unite_f = ''). On passe alors par le mouvement pour associer l'um
			
			DROP TABLE IF EXISTS w_diag_um; CREATE TEMP TABLE w_diag_um AS
			SELECT
				tb_diag.id_diag AS id_diag,
				tb_diag.unite_f AS um_code,
				t_unites_medicales.code AS code,
				t_unites_medicales.oid AS oid
			FROM
				prod_cora.tb_diag
				LEFT JOIN prod_cora.tb_mvt ON tb_mvt.id_mvt = tb_diag.id_mvt
				JOIN prod_cora.tb_synth_rss ON tb_synth_rss.id_sejour = tb_mvt.id_sejour
				JOIN prod_cora.tb_unite ON tb_unite.code_unite = tb_diag.unite_f
				JOIN prod_cora.tb_unite_m_unite ON tb_unite_m_unite.id_unite = tb_unite.id_unite
				JOIN prod_cora.tb_unite_medicale ON tb_unite_medicale.id_unite_med = tb_unite_m_unite.id_unite_med
				JOIN pmsi.t_unites_medicales ON t_unites_medicales.code = tb_unite_medicale.code_unite_med
			WHERE unite_f <> '' AND tb_synth_rss.date_fin >= '[ANNEE]-01-01'
			UNION
			SELECT
				tb_diag.id_diag AS id_diag,
				tb_mvt.id_unite_m AS um_code,
				t_unites_medicales.code AS code,
				t_unites_medicales.oid AS oid
			FROM
				prod_cora.tb_diag
				LEFT JOIN prod_cora.tb_mvt ON tb_mvt.id_mvt = tb_diag.id_mvt
				JOIN prod_cora.tb_synth_rss ON tb_synth_rss.id_sejour = tb_mvt.id_sejour
				JOIN prod_cora.tb_unite ON tb_unite.code_unite = tb_mvt.id_unite_m
				JOIN prod_cora.tb_unite_m_unite ON tb_unite_m_unite.id_unite = tb_unite.id_unite
				JOIN prod_cora.tb_unite_medicale ON tb_unite_medicale.id_unite_med = tb_unite_m_unite.id_unite_med
				JOIN pmsi.t_unites_medicales ON t_unites_medicales.code = tb_unite_medicale.code_unite_med
			WHERE unite_f = '' AND tb_synth_rss.date_fin >= '[ANNEE]-01-01'
			;
			
			INSERT INTO pmsi.p_rss_diagnostics(
				finess, 
				rss_id, 
				no_rss, 
				no_rum, 
				diagnostic_id, 
				type_diagnostic, 
				type_diagnostic_rss, 
				imprecis, 
				unite_medicale, 
				unite_medicale_id)
			SELECT
				w_rss.finess,
				COALESCE(p_rss.oid, 0) AS rss_id,
				base.cti_to_number(tb_synth_rss.no_rss) AS no_rss,
				COALESCE(w_ordre_rum.ordre, 1) AS no_rum,
				COALESCE(t_diagnostics.oid, 0) AS diagnostic_id,
				CASE
					WHEN tb_diag.type_diag = 'P' THEN 'DP' 
					WHEN tb_diag.type_diag = 'R' THEN 'DR' 
					WHEN tb_diag.type_diag = 'S' THEN 'DA' 
					WHEN tb_diag.type_diag = 'D' THEN 'DD'
					ELSE ''
				END::text AS type_diagnostic,
				CASE
					WHEN tb_diag.type_diag = 'P' AND tb_synth_rum.dp_mvt_retenu = 'X' THEN 'DP'
					ELSE 'DA'
				END::text AS type_diagnostic_rss,
				''::text AS imprecis,
				COALESCE(w_diag_um.code, '?') AS unite_medicale,
				COALESCE(w_diag_um.oid, 0) AS unite_medicale_id
			FROM
				prod_cora.tb_diag
				LEFT JOIN pmsi.t_diagnostics ON t_diagnostics.code = tb_diag.code_diag
				LEFT JOIN w_diag_um ON w_diag_um.id_diag = tb_diag.id_diag
				JOIN prod_cora.tb_mvt ON tb_mvt.id_mvt = tb_diag.id_mvt
				JOIN prod_cora.tb_mvt_rum ON tb_mvt_rum.id_mvt = tb_mvt.id_mvt
				JOIN prod_cora.tb_synth_rum ON tb_synth_rum.id_rum = tb_mvt_rum.id_rum
				JOIN prod_cora.tb_synth_rss ON tb_synth_rss.id_sejour = tb_mvt.id_sejour
				JOIN w_rss ON tb_synth_rss.no_rss = w_rss.no_rss
				JOIN w_ordre_rum ON tb_synth_rum.id_rum = w_ordre_rum.id_rum
				LEFT JOIN pmsi.p_rss ON p_rss.no_rss = tb_synth_rss.no_rss
			;
			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Actes">
			<sqlcmd><![CDATA[
				
				INSERT INTO pmsi.p_rss_actes(
					finess, 
					no_rss,
					rss_id,
					no_rum,
					unite_medicale,
					unite_medicale_id,
					date_acte,
					medecin_id,
					acte_id,
					nombre,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					remboursement_excp_ccam,
					association_nonprevue_ccam
					)
				SELECT
					w_rss.finess AS finess,
					w_rss.no_rss AS no_rss,
					p_rss.oid AS rss_id,
					COALESCE(w_ordre_rum.ordre, 1) AS no_rum,
					COALESCE(t_unites_medicales.code, '?') AS unite_medicale,
					COALESCE(t_unites_medicales.oid, 0) AS unite_medicale_id,
					tb_acte.date_acte AS date_acte,
					COALESCE(t_medecins.oid, 0) AS medecin_id,
					COALESCE(t_actes.oid, 0) AS acte_id,
					tb_acte.quantite AS nombre,
					tb_acte.phase_acte AS phase_ccam,
					tb_det_acte.id_activite AS activite_ccam,
					tb_det_acte.ext_document AS extension_ccam,
					substr(tb_det_acte.modificateur, 1 , 1) AS modificateur_ccam_1,
					substr(tb_det_acte.modificateur, 2 , 1) AS modificateur_ccam_2,
					substr(tb_det_acte.modificateur, 3 , 1) AS modificateur_ccam_3,
					substr(tb_det_acte.modificateur, 4 , 1) AS modificateur_ccam_4,
					''::text AS remboursement_excp_ccam,
					''::text AS association_nonprevue_ccam
				FROM
					prod_cora.tb_det_acte
					JOIN prod_cora.tb_acte ON tb_acte.id_acte = tb_det_acte.id_acte
					JOIN prod_cora.tb_mvt ON tb_mvt.id_mvt = tb_acte.id_mvt
					LEFT JOIN prod_cora.tb_mvt_rum ON tb_mvt_rum.id_mvt = tb_mvt.id_mvt
					LEFT JOIN prod_cora.tb_synth_rum ON tb_synth_rum.id_rum = tb_mvt_rum.id_rum
					LEFT JOIN w_ordre_rum ON tb_synth_rum.id_rum = w_ordre_rum.id_rum
					JOIN w_rss ON w_rss.id_sejour = tb_mvt.id_sejour
					JOIN pmsi.p_rss ON p_rss.no_rss = w_rss.no_rss
					LEFT JOIN pmsi.t_actes ON t_actes.code = substring(tb_acte.code_acte, 1, 7)
					LEFT JOIN prod_cora.util ON util.id_util = tb_det_acte.id_medecin
					LEFT JOIN pmsi.t_medecins ON t_medecins.code = util.code_util
					LEFT JOIN prod_cora.tb_unite On tb_unite.code_unite = tb_mvt.id_unite_m
					LEFT JOIN prod_cora.tb_unite_m_unite ON tb_unite_m_unite.id_unite = tb_unite.id_unite
					LEFT JOIN prod_cora.tb_unite_medicale ON tb_unite_medicale.id_unite_med = tb_unite_m_unite.id_unite_med
					LEFT JOIN pmsi.t_unites_medicales ON t_unites_medicales.code = tb_unite_medicale.code_unite_med
				;
			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Factures">
			<sqlcmd><![CDATA[
				
				INSERT INTO pmsi.p_rsf_total (
					finess,
					no_rss,
					code_pec,
					rang_beneficiaire,
					regime,
					nature_pec,
					justificatif_exo,
					no_facture,
					code_regularisation,
					base_remboursement,
					sejour_facture,
					sejour_remboursable,
					honoraires_factures,
					honoraires_remboursable,
					t2a_facture,
					dmi_facture,
					phc_facture,
					participation_assure_avant_oc,
					sejour_remboursable_2,
					honoraires_remboursable_2,
					montant_dmi_ghs2006,
					nb_si,
					ca_si,
					nb_sur,
					ca_sur,
					nb_rea,
					ca_rea,
					nb_neonat,
					ca_neonat,
					nb_exh,
					ca_exh,
					nb_exb, 
					ca_exb,
					nb_ghs,
					ca_ghs,
					rss_id,
					ca_ghs_theorique,
					taux_remboursement,
					matricule_assure,
					cle_matricule_assure,
					pat_benef_cmu,
					no_facture_sejour_mere
				)
				SELECT
					w_rss.finess AS finess,
					w_rss.no_rss AS no_rss,
					MAX(tb_rsf_fact.code_pec) AS code_pec,
					MAX(tb_rsf_fact.rang_benef) AS rang_beneficiaire,
					MAX(tb_rsf_fact.code_regime) AS regime,
					MAX(tb_rsf_fact.nature_assurance) AS nature_pec,
					MAX(tb_rsf_fact.code_exo) AS justificatif_exo,
					MAX(tb_rsf_fact.no_facture) AS no_facture,
					''::text AS code_regularisation,
					SUM(tb_rsf_fact.mnt_tot_prest_base_remb) AS base_remboursement,
					SUM(tb_rsf_fact.mnt_tot_prest) AS sejour_facture,
					SUM(tb_rsf_fact.mnt_tot_prest_caisse) AS sejour_remboursable,
					SUM(tb_rsf_fact.mnt_tot_hono_fact) AS honoraires_factures,
					SUM(tb_rsf_fact.mnt_tot_hono_caisse) AS honoraires_remboursable,
					0 AS t2a_facture,
					0 AS dmi_facture,
					0 AS phc_facture,
					0 AS participation_assure_avant_oc,
					0 AS sejour_remboursable_2,
					0 AS honoraires_remboursable_2,
					0 AS montant_dmi_ghs2006,
					0 AS nb_si,
					0 AS ca_si,
					0 AS nb_sur,
					0 AS ca_sur,
					0 AS nb_rea,
					0 AS ca_rea,
					0 AS nb_neonat,
					0 AS ca_neonat,
					0 AS nb_exh,
					0 AS ca_exh,
					0 AS nb_exb,
					0 AS ca_exb,
					0 AS nb_ghs,
					0 AS ca_ghs,
					p_rss.oid AS rss_id,
					0 AS ca_ghs_theorique,
					100 AS taux_remboursement,
					MAX(tb_rsf_fact.no_secu) AS matricule_assure,
					MAX(tb_rsf_fact.cle_secu) AS cle_matricule_assure,
					MAX(base.cti_to_number(tb_rsf_fact.patient_cmu)) AS pat_benef_cmu,
					MAX(tb_rsf_fact.no_facture_mere) AS no_facture_sejour_mere
				FROM
					prod_cora.tb_rsf_fact
					JOIN w_rss ON w_rss.id_sejour = tb_rsf_fact.id_sejour
					JOIN pmsi.p_rss ON p_rss.no_rss = w_rss.no_rss
				GROUP BY 
					w_rss.finess,
					w_rss.no_rss,
					p_rss.oid
				;
				
				INSERT INTO pmsi.p_rsf_total( 
					finess, no_rss, code_pec, rang_beneficiaire, regime, nature_pec,  
					justificatif_exo, no_facture, code_regularisation, base_remboursement,  
					sejour_facture, sejour_remboursable, honoraires_factures, honoraires_remboursable, 
					t2a_facture, dmi_facture, phc_facture, participation_assure_avant_oc,  
					sejour_remboursable_2, honoraires_remboursable_2, montant_dmi_ghs2006,  
					nb_si, ca_si, nb_sur, ca_sur, nb_rea, ca_rea, nb_neonat, ca_neonat,  
					nb_exh, ca_exh, nb_exb, ca_exb, nb_ghs, ca_ghs, rss_id, ca_ghs_theorique) 
				SELECT 
					p_rss.finess, p_rss.no_rss, '', '', '', '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 0, 0,  
					0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, p_rss.oid, 0 
				FROM pmsi.p_rss LEFT JOIN pmsi.p_rsf_total ON (p_rsf_total.rss_id = p_rss.oid)
				WHERE en_cours = '1' AND p_rsf_total.rss_id IS NULL
				;
			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Prestations">
			<sqlcmd><![CDATA[
				
				INSERT INTO pmsi.p_rsf_detail(
					finess, 
					rss_id, 
					no_rss, 
					nature, 
					mt, 
					dmt, 
					date_debut, 
					date_fin, 
					nombre, 
					coefficient, 
					prix_unitaire, 
					base_remboursement, 
					taux_remboursement, 
					sejour_facture, 
					sejour_remboursable, 
					compteur, 
					ligne_t2a, 
					pec_fj, 
					coefficient_mco, 
					sejour_remboursable_2, 
					ghs_id, 
					sejour_rembourse_noemie, 
					nature_noemie, 
					prestation_id, 
					type, 
					coefficient_geo, 
					est_ligne_rss, 
					est_ligne_rum, 
					no_rum, 
					unite_medicale_id, 
					base_remboursement_rum,
					montant_supplement_nf
				)
				SELECT
					w_rss.finess AS finess,
					COALESCE(p_rss.oid, 0) AS rss_id,
					w_rss.no_rss AS no_rss,
					''::text AS nature,
					tb_rsf_prestations.mode_traitement AS mt,
					tb_rsf_prestations.code_dmt AS dmt,
					tb_rsf_prestations.date_debut_prest AS date_debut,
					tb_rsf_prestations.date_fin_prest AS date_fin,
					tb_rsf_prestations.quantite AS nombre,
					tb_rsf_prestations.coef AS coefficient,
					tb_rsf_prestations.prix_unit AS prix_unitaire,
					tb_rsf_prestations.mnt_base_remb AS base_remboursement,
					tb_rsf_prestations.taux AS taux_remboursement,
					tb_rsf_prestations.mnt_tot_fact AS sejour_facture,
					tb_rsf_prestations.mnt_caisse AS sejour_remboursable,
					t_prestations.compteur_rsf AS compteur,
					''::text AS ligne_t2a,
					tb_rsf_prestations.code_pec_fj AS pec_fj,
					tb_rsf_prestations.coef_mco AS coefficient_mco,
					0 AS sejour_remboursable_2,
					COALESCE(t_ghs.oid, 0) AS ghs_id,
					tb_rsf_prestations.mnt_noemie AS sejour_remboursable_noemie,
					tb_rsf_prestations.nat_op_noemie AS nature_noemie,
					COALESCE(t_prestations.oid, 0) AS prestation_id,
					'B'::text AS type,
					0 AS coefficient_geo,
					'1'::text AS est_ligne_rss,
					'0'::text AS est_ligne_rum,
					1 AS no_rum, 
					0 AS unite_medicale_id, 
					0 AS base_remboursement_rum,
					0 AS montant_supplement_nf
				FROM
					prod_cora.tb_rsf_prestations
					JOIN prod_cora.tb_rsf_fact ON tb_rsf_fact.id_rsf_fact = tb_rsf_prestations.id_rsf_fact
					JOIN w_rss ON w_rss.id_sejour = tb_rsf_fact.id_sejour
					LEFT JOIN pmsi.t_ghs ON t_ghs.code = tb_rsf_prestations.ghs
					JOIN pmsi.p_rss ON p_rss.no_rss = w_rss.no_rss
					LEFT JOIN pmsi.t_prestations ON t_prestations.code = tb_rsf_prestations.code_prest
				;
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="LPP">
			<sqlcmd><![CDATA[
				
				INSERT INTO pmsi.p_rss_lpp (
					finess,
					no_rss,
					rang,
					date_lpp,
					lpp_id,
					nombre,
					prix_unitaire,
					montant_facture,
					rss_id,
					non_pris_en_compte
				)
				SELECT
					w_rss.finess AS finess,
					w_rss.no_rss AS no_rss,
					''::text AS rang,
					w_rss.date_sortie,
					COALESCE(t_lpp.oid, 0) AS lpp_id,
					tb_rsf_prest_prothese.quantite AS nombre,
					tb_rsf_prest_prothese.tarif_ref AS prix_unitire,
					tb_rsf_prest_prothese.mnt_facture AS montant_facture,
					p_rss.oid AS rss_id,
					'0'::text AS non_pris_en_compte
				FROM
					prod_cora.tb_rsf_prest_prothese
					JOIN prod_cora.tb_rsf_fact ON tb_rsf_prest_prothese.id_rsf_fact = tb_rsf_fact.id_rsf_fact
					JOIN w_rss ON w_rss.id_sejour = tb_rsf_fact.id_sejour
					LEFT JOIN pmsi.t_lpp ON t_lpp.code = tb_rsf_prest_prothese.code_lpp
					JOIN pmsi.p_rss ON p_rss.no_rss = w_rss.no_rss
				;
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="UCD">
			<sqlcmd><![CDATA[
				
				INSERT INTO pmsi.p_rss_ucd (
					finess,
					no_rss,
					rang,
					date_ucd,
					ucd_id,
					nombre,
					coefficient_fractionnement,
					prix_unitaire_achat,
					prix_unitaire_ecart_indemnisable,
					montant_ecart_indemnisable,
					montant_facture,
					rss_id,
					non_pris_en_compte
				)
				SELECT
					w_rss.finess AS finess,
					w_rss.no_rss AS no_rss,
					''::text AS rang,
					w_rss.date_sortie AS date_ucd,
					COALESCE(t_ucd.oid, 0) AS ucd_id,
					tb_rsf_prest_med.quantite AS nombre,
					tb_rsf_prest_med.coef_fractionnement AS coefficient_fractionnement,
					tb_rsf_prest_med.prix_achat_unit AS prix_unitaire_achat,
					tb_rsf_prest_med.mnt_unite_ecart AS prix_unitaire_ecart_indemnisable,
					tb_rsf_prest_med.mnt_tot_ecart AS montant_ecart_indemnisable,
					tb_rsf_prest_med.mnt_facture AS montant_facture,
					COALESCE(p_rss.oid, 0) AS rss_id,
					'0'::text AS non_pris_en_compte
					
				FROM
					prod_cora.tb_rsf_prest_med
					JOIN prod_cora.tb_rsf_fact ON tb_rsf_fact.id_rsf_fact = tb_rsf_prest_med.id_rsf_fact
					JOIN w_rss ON w_rss.id_sejour = tb_rsf_fact.id_sejour
					join pmsi.p_rss ON p_rss.no_rss = w_rss.no_rss
					LEFT JOIN pmsi.t_ucd ON t_ucd.code = tb_rsf_prest_med.code_ucd
				;
			
			]]></sqlcmd>
		</NODE>

	</NODE>
    
    
    <NODE name="PMSIETAT" label="RECUPERATION ETAT DES DOSSIERS">
			
		<NODE label="Initialisation">
			<sqlcmd><![CDATA[
			
				-- Séjours non groupés
				INSERT INTO pmsi.t_ghm(
					code,
					texte,
					mco,
					aso,
					severite,
					cmd_id,
					ghm5_id,
					ligne_oap_id,
					cas_id)
				SELECT
					'99Z99Z' AS code,
					'Séjours sortis non groupés' AS texte,
					'?' AS mco,
					'?' AS aso,
					'Z' AS severite,
					COALESCE((SELECT oid FROM pmsi.t_cmd WHERE code = '99'),0) AS cmd_id,
					COALESCE((SELECT oid FROM pmsi.t_ghm5 WHERE code = '99Z99'),0) AS ghm5_id,
					0 AS ligne_oap_id,
					COALESCE((SELECT oid FROM base.t_cas WHERE code = '?'),0) AS cas_id
				WHERE '99Z99Z' NOT IN (SELECT code FROM pmsi.t_ghm)
				UNION
				SELECT
					'99Z98Z' AS code,
					'GHM indisponible' AS texte,
					'?' AS mco,
					'?' AS aso,
					'Z' AS severite,
					COALESCE((SELECT oid FROM pmsi.t_cmd WHERE code = '99'),0) AS cmd_id,
					COALESCE((SELECT oid FROM pmsi.t_ghm5 WHERE code = '99Z98'),0) AS ghm5_id,
					0 AS ligne_oap_id,
					COALESCE((SELECT oid FROM base.t_cas WHERE code = '?'),0) AS cas_id
				WHERE '99Z98Z' NOT IN (SELECT code FROM pmsi.t_ghm);
					
			
				
				SELECT pmsi.cti_initialize_rss_etat();
				
			]]></sqlcmd>
		</NODE>

		<NODE label="Etat des dossiers">
			<sqlcmd><![CDATA[
			
                UPDATE pmsi.p_rss_etat
                SET date_groupage = date(DATE_FABRICATION)
                FROM prod_cora.tb_synth_rss
                JOIN pmsi.p_rss ON tb_synth_rss.no_rss = p_rss.no_rss AND
                    date(TB_SYNTH_RSS.DATE_DEBUT) = p_rss.date_entree
                WHERE p_rss_etat.rss_id = p_rss.oid AND
                    date_groupage <> date(DATE_FABRICATION)
                ;

                UPDATE pmsi.p_rss_etat
                SET est_facture = '1'
                FROM prod_cora.tb_synth_rss
                JOIN prod_cora.tb_rsf_fact ON tb_synth_rss.ID_SEJOUR = tb_rsf_fact.ID_SEJOUR
                JOIN pmsi.p_rss ON tb_synth_rss.no_rss = p_rss.no_rss AND
                    date(TB_SYNTH_RSS.DATE_DEBUT) = p_rss.date_entree
                WHERE p_rss_etat.rss_id = p_rss.oid AND
                    est_facture <> '1'
                ;
                
                UPDATE pmsi.p_rss
                SET en_cours_etat = 'F'
                FROM pmsi.p_rss_etat
                WHERE rss_id = p_rss.oid AND
                    en_cours = '1' AND
                    est_facture = '1' AND 
                    en_cours_etat <> 'F'
                ;

			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
</ROOT>