<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	

	<NODE name="PMSICOMPLEMENTS" label="RECUPERATION DES DONNEES COMPLEMENTAIRES PMSI">
		
		<NODE label="Paramètres">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.t_medecins(code, nom, prenom, specialite_id, no_adeli, specialite_interne_id)
				SELECT   
                    MEDN_CODE, 
                    MAX(replace(MEDN_NOM, '''', ' ')) AS NOM, 
                    MAX(replace(MEDN_PRENOM, '''', ' ')) AS PRENOM, 
                    MAX(COALESCE(t_specialites_medecin.oid,0)),
                    MAX(MEDN_CODEADELI),
                    MAX(COALESCE(t_specialites_internes_medecin.oid,0))
				FROM prod_csis.sst_medecin_medn 
				LEFT JOIN prod_csis.SST_MEDETAB_METB ON METB_ID_MEDN = MEDN_ID
				LEFT JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON METB_ID_ETAB = ETAB_ID 
				LEFT JOIN prod_csis.SNO_SPECIALITESS_SPSS ON  MEDN_ID_SPSS = SPSS_ID
				LEFT JOIN pmsi.t_specialites_medecin ON (t_specialites_medecin.code = SPSS_CODE)
				LEFT JOIN pmsi.t_specialites_internes_medecin ON (t_specialites_internes_medecin.code = SPSS_CODE || '..')
				WHERE ETAB_FINESS =  '[FINESS]' AND
						(MEDN_TRAITANT <> '1' 
							OR
							MEDN_ID IN (
								SELECT ENVR_ID_MEDN
								FROM prod_csis.SCA_ENVELOPPERUM_ENVR
								JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM ON ENVR_ID_DADM = DADM_ID
								JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID
								WHERE ETAB_FINESS = '[FINESS]')
							OR
							MEDN_ID IN (
								SELECT RECG_ID1_MEDN
								FROM prod_csis.MPM_RECUEIL_RECG
								JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM ON RECG_ID_DADM = DADM_ID
								JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID
								WHERE ETAB_FINESS = '[FINESS]'
								)

								) AND
				MEDN_CODE <> '' AND
				MEDN_CODE NOT IN (SELECT code FROM pmsi.t_medecins) 
                GROUP BY 1
				ORDER BY 1
                ;

					
										
			]]></sqlcmd>
		</NODE>

		<NODE label="Identités">
			<sqlcmd><![CDATA[
			
				-- N° séjour pour externes
				UPDATE [SCHEMA].p_rss
				SET no_sejour_administratif = DADM_NDA
				FROM prod_csis.mpm_facture_recf 
				JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM  ON RECF_ID_DADM = DADM_ID
                JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
				WHERE '0' || no_sejour_administratif = RECF_NUMFACTALIAS AND
					ghm_id = 0 AND
					(p_rss.date_sortie = date(dadm_datefin) OR p_rss.date_entree = date(dadm_datedebut)) AND
					no_sejour_administratif IS DISTINCT FROM DADM_NDA;
					
					

				-- Nom prénom
				UPDATE [SCHEMA].p_identites
				SET nom = PATI_NOMUSAGE,
					prenom = PATI_PRENOMS,
					nom_naissance = CASE WHEN PATI_NOMFAMILLE <> PATI_NOMMARITAL THEN PATI_NOMFAMILLE ELSE '' END
				FROM prod_csis.SID_DOSSIERADMINISTRATIF_DADM 
                    JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
					JOIN prod_csis.SID_PATIENT_PATI ON DADM_ID_PATI = PATI_ID
					JOIN [SCHEMA].p_rss ON no_sejour_administratif = DADM_NDA
				WHERE p_identites.rss_id = p_rss.oid AND
					p_rss.en_cours LIKE '0' AND
					(p_rss.date_sortie = date(dadm_datefin) OR p_rss.date_entree = date(dadm_datedebut)) AND
					(nom IS DISTINCT FROM PATI_NOMUSAGE OR
					prenom IS DISTINCT FROM PATI_PRENOMS OR
					nom_naissance IS DISTINCT FROM CASE WHEN PATI_NOMFAMILLE <> PATI_NOMMARITAL THEN PATI_NOMFAMILLE ELSE '' END);
					
				UPDATE [SCHEMA].p_identites
				SET nom = PATI_NOMUSAGE,
					prenom = PATI_PRENOMS,
					nom_naissance = CASE WHEN PATI_NOMFAMILLE <> PATI_NOMMARITAL THEN PATI_NOMFAMILLE ELSE '' END
				FROM prod_csis.SID_DOSSIERADMINISTRATIF_DADM 
                    JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
					JOIN prod_csis.SID_PATIENT_PATI ON DADM_ID_PATI = PATI_ID
					JOIN [SCHEMA].p_rss ON no_sejour_administratif = DADM_NDA
				WHERE p_identites.rss_id = p_rss.oid AND
					p_rss.en_cours LIKE '0' AND
					(p_rss.date_sortie = date(dadm_datefin) OR p_rss.date_entree = date(dadm_datedebut)) AND
					(nom IS DISTINCT FROM PATI_NOMUSAGE OR
					prenom IS DISTINCT FROM PATI_PRENOMS OR
					nom_naissance IS DISTINCT FROM CASE WHEN PATI_NOMFAMILLE <> PATI_NOMMARITAL THEN PATI_NOMFAMILLE ELSE '' END);
					
				UPDATE [SCHEMA].p_rss
				SET no_patient = to_number(PATI_IPP, '000000000000000')::bigint
				FROM prod_csis.SID_DOSSIERADMINISTRATIF_DADM 
                    JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
					JOIN prod_csis.SID_PATIENT_PATI ON DADM_ID_PATI = PATI_ID
				WHERE no_sejour_administratif = DADM_NDA AND
					p_rss.en_cours LIKE '0' AND
					no_patient IS DISTINCT FROM to_number(PATI_IPP, '000000000000000')::bigint;
					
					
				
					
										
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Médecins RSS">
			<sqlcmd><![CDATA[

				UPDATE [SCHEMA].p_rss
				SET medecin_rss_id = t_medecins.oid
				FROM prod_csis.SCA_RESUMESS_RESS 
					JOIN prod_csis.SCA_ENVELOPPERUM_ENVR ON RESS_ID_ENVR = ENVR_ID
					JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM ON DADM_ID = ENVR_ID_DADM
                    JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
					JOIN prod_csis.SST_MEDECIN_MEDN ON ENVR_ID_MEDN = MEDN_ID
					JOIN pmsi.t_medecins ON MEDN_CODE = t_medecins.code
				WHERE no_sejour_administratif = DADM_NDA AND 
					(p_rss.date_sortie = date(dadm_datefin) OR p_rss.date_entree = date(dadm_datedebut)) AND
					medecin_rss_id is distinct from t_medecins.oid AND
					p_rss.en_cours LIKE '0' AND
					ghm_id <> 0;


				DROP TABLE IF EXISTS w_SID_DOSSIERADMINISTRATIF_DADM;
				CREATE TEMP TABLE w_SID_DOSSIERADMINISTRATIF_DADM AS
				SELECT
					DADM_NDA, DADM_DATEDEBUT, DADM_DATEFIN,
					COALESCE((MAX(CASE WHEN TMVT_CODE = 'R' AND SMVT_CODE NOT IN ('A') AND MVMT_CHPNUM1 <> 0 THEN array[MVMT_DATEHEURE::text,MVMT_CHPNUM1::text] ELSE NULL END))[2],'0') AS DADM_ID_MEDN
				FROM prod_csis.SID_DOSSIERADMINISTRATIF_DADM
                    JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
					JOIN prod_csis.SMV_MOUVEMENT_MVMT ON MVMT_ID_DADM = dadm_id  
					JOIN prod_csis.SMV_TYPEMOUVEMENT_TMVT ON mvmt_id_tmvt = tmvt_id
					JOIN prod_csis.SMV_STATUTMOUVEMENT_SMVT ON mvmt_id_Smvt = smvt_id
				GROUP BY 1,2,3;


				UPDATE [SCHEMA].p_rss
				SET medecin_rss_id = t_medecins.oid
				FROM	w_SID_DOSSIERADMINISTRATIF_DADM
					JOIN prod_csis.SST_MEDECIN_MEDN ON DADM_ID_MEDN = MEDN_ID
					JOIN pmsi.t_medecins ON MEDN_CODE = t_medecins.code
				WHERE no_sejour_administratif = DADM_NDA AND 
					(p_rss.date_sortie = date(dadm_datefin) OR p_rss.date_entree = date(dadm_datedebut)) AND
					medecin_rss_id is distinct from t_medecins.oid AND
					p_rss.en_cours LIKE '0' AND
					(ghm_id = 0 OR medecin_rss_id = 0);
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Médecins actes">
			<sqlcmd><![CDATA[

				DROP TABLE IF EXISTS w_dadm_actes;
				CREATE TEMP TABLE w_dadm_actes AS
				SELECT 
					DADM_NDA,
					date(DADM_DATEDEBUT) AS DADM_DATEDEBUT,
					date(DADM_DATEFIN) AS DADM_DATEFIN,
					date(RECC_DATEACTE) AS RECC_DATEACTE,
					COALESCE(RECC_CODECCAM,'') AS RECC_CODECCAM,
					RECC_CODEACTIVITE,
					t_medecins.oid AS medecin_id
				FROM prod_csis.MPM_RECUEILTIERS_RECT
					JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM ON DADM_ID = RECT_ID_DADM
                    JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
					JOIN prod_csis.MPM_RECUEIL_RECG ON RECT_ID_RECG = RECG_ID
					JOIN prod_csis.MPM_RECUEILCCAM_RECC ON (RECC_ID_RECG = RECG_ID)
					JOIN prod_csis.SST_MEDECIN_MEDN ON COALESCE(RECC_ID_MEDN,RECG_ID1_MEDN) = MEDN_ID
					JOIN pmsi.t_medecins ON MEDN_CODE = t_medecins.code
				WHERE RECG_TYPELIGNE = 4;
				
				
				UPDATE [SCHEMA].p_rss_actes
				SET medecin_id = w_dadm_actes.medecin_id
				FROM w_dadm_actes
				JOIN [SCHEMA].p_rss ON no_sejour_administratif = DADM_NDA,
				pmsi.t_actes
				WHERE acte_id = t_actes.oid AND 
					p_rss_actes.rss_id = p_rss.oid AND
					p_rss.en_cours LIKE '0' AND
					(p_rss.date_sortie = dadm_datefin OR p_rss.date_entree = dadm_datedebut) AND
					t_actes.code = substring(RECC_CODECCAM, 1, 7) AND 
					activite_ccam = RECC_CODEACTIVITE AND 
					date_acte = RECC_DATEACTE AND
					p_rss_actes.medecin_id <> w_dadm_actes.medecin_id; 
										
										
			]]></sqlcmd>
		</NODE>
	</NODE>

	
	<NODE name="PMSIENCOURS" label="RECUPERATION DES DOSSIERS PMSI EN-COURS CSIS">
		
		<NODE label="Paramètres">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.t_medecins(code, nom, prenom, specialite_id, no_adeli, specialite_interne_id)
				SELECT   
                    MEDN_CODE, 
                    MAX(replace(MEDN_NOM, '''', ' ')) AS NOM, 
                    MAX(replace(MEDN_PRENOM, '''', ' ')) AS PRENOM, 
                    MAX(COALESCE(t_specialites_medecin.oid,0)),
                    MAX(MEDN_CODEADELI),
                    MAX(COALESCE(t_specialites_internes_medecin.oid,0))
				FROM prod_csis.sst_medecin_medn 
				LEFT JOIN prod_csis.SST_MEDETAB_METB ON METB_ID_MEDN = MEDN_ID
				LEFT JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON METB_ID_ETAB = ETAB_ID 
				LEFT JOIN prod_csis.SNO_SPECIALITESS_SPSS ON  MEDN_ID_SPSS = SPSS_ID
				LEFT JOIN pmsi.t_specialites_medecin ON (t_specialites_medecin.code = SPSS_CODE)
				LEFT JOIN pmsi.t_specialites_internes_medecin ON (t_specialites_internes_medecin.code = SPSS_CODE || '..')
				WHERE ETAB_FINESS =  '[FINESS]' AND
						(MEDN_TRAITANT <> '1' 
							OR
							MEDN_ID IN (
								SELECT ENVR_ID_MEDN
								FROM prod_csis.SCA_ENVELOPPERUM_ENVR
								JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM ON ENVR_ID_DADM = DADM_ID
								JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID
								WHERE ETAB_FINESS = '[FINESS]')
							OR
							MEDN_ID IN (
								SELECT RECG_ID1_MEDN
								FROM prod_csis.MPM_RECUEIL_RECG
								JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM ON RECG_ID_DADM = DADM_ID
								JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID
								WHERE ETAB_FINESS = '[FINESS]'
								)

								) AND
				MEDN_CODE <> '' AND
				MEDN_CODE NOT IN (SELECT code FROM pmsi.t_medecins) 
                GROUP BY 1
				ORDER BY 1
                ;
			]]></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 finess
				FROM pmsi.p_rss;
				
				
				DROP TABLE IF EXISTS w_SCA_ENVELOPPERUM_ENVR;
				CREATE TEMP TABLE w_SCA_ENVELOPPERUM_ENVR AS
				SELECT 
					ENVR_ID, 
					0::numeric AS no_rum,
					ENVR_ID_DADM,
					ENVR_ID1_MVMT,
					ENVR_DATEDEBUTRUM,
					ENVR_DATEFINRUM,
					ENVR_ID_MEDN,
					ENVR_MODEENT,
					CASE WHEN ENVR_PROVENANCE <> '0' THEN ENVR_PROVENANCE ELSE '' END AS ENVR_PROVENANCE,
					ENVR_MODESOR,
					CASE WHEN ENVR_DESTINATION <> '0' THEN ENVR_DESTINATION ELSE '' END AS ENVR_DESTINATION,
					ENVR_IGS2,
					ENVR_SA,
					MAX(CASE WHEN TDIA_CODE = 'DP' THEN DSDG_CODE ELSE '' END) AS DSDG_CODE_DP,
					MAX(CASE WHEN TDIA_CODE = 'DR' THEN DSDG_CODE ELSE '' END) AS DSDG_CODE_DR
				FROM prod_csis.SCA_ENVELOPPERUM_ENVR
					LEFT JOIN prod_csis.SCA_DIAGNOSTICDANSRUM_DGRM ON DGRM_ID_ENVR = ENVR_ID
					LEFT JOIN prod_csis.SNO_TYPEDIAGNOSTICS_TDIA ON DGRM_ID_TDIA = TDIA_ID
					LEFT JOIN prod_csis.SNO_DESCRDIAGNOSTIC_DSDG ON DGRM_ID_DSDG = DSDG_ID
				WHERE ENVR_DATEFINRUM >= '[ANNEE]0101'
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13;

				ALTER TABLE w_SCA_ENVELOPPERUM_ENVR
				ADD CONSTRAINT w_SCA_ENVELOPPERUM_ENVR_PK PRIMARY KEY(ENVR_ID);

				CREATE INDEX w_SCA_ENVELOPPERUM_ENVR_i1 ON w_SCA_ENVELOPPERUM_ENVR USING btree (ENVR_ID_DADM);


				SELECT base.cti_execute(
				'UPDATE w_SCA_ENVELOPPERUM_ENVR
				SET no_rum = subview.last_norum + 1
				FROM (
					SELECT 
						ENVR_ID_DADM,
						MAX(no_rum) AS last_norum,
						(MIN(CASE WHEN no_rum = 0 THEN array[ENVR_DATEDEBUTRUM::text,ENVR_ID::text] ELSE NULL END))[2]::numeric AS ENVR_ID
					FROM w_SCA_ENVELOPPERUM_ENVR
					GROUP BY 1) subview
				WHERE w_SCA_ENVELOPPERUM_ENVR.ENVR_ID_DADM = subview.ENVR_ID_DADM AND w_SCA_ENVELOPPERUM_ENVR.ENVR_ID = subview.ENVR_ID',50);
				

				DROP TABLE IF EXISTS w_SCA_ENVELOPPERUM_ENVR_RSS;
				CREATE TEMP TABLE w_SCA_ENVELOPPERUM_ENVR_RSS AS
				SELECT 
					ENVR_ID_DADM AS RSS_ENVR_ID_DADM,
					count(*) AS nb_rum,
					(min(array[ENVR_DATEDEBUTRUM::text,ENVR_MODEENT::text]))[2] AS RSS_ENVR_MODEENT,
					(min(array[ENVR_DATEDEBUTRUM::text,ENVR_PROVENANCE::text]))[2] AS RSS_ENVR_PROVENANCE,
					(max(array[ENVR_DATEFINRUM::text,ENVR_MODESOR::text]))[2] AS RSS_ENVR_MODESOR,
					(max(array[ENVR_DATEFINRUM::text,ENVR_DESTINATION::text]))[2] AS RSS_ENVR_DESTINATION
				FROM w_SCA_ENVELOPPERUM_ENVR
				GROUP BY 1;

				ALTER TABLE w_SCA_ENVELOPPERUM_ENVR_RSS
				ADD CONSTRAINT w_SCA_ENVELOPPERUM_ENVR_RSS_PK PRIMARY KEY(RSS_ENVR_ID_DADM);




				DROP TABLE IF EXISTS w_SCA_RESUMESS_RESS;
				CREATE TEMP TABLE w_SCA_RESUMESS_RESS AS 
				SELECT 
					w_finess.finess,
					nextval('pmsi.s_rss'::regclass) AS rss_id,
					RESS_IDENTIFIANTRSS::numeric AS no_rss,
					SCA_RESUMESS_RESS.*,
					SID_DOSSIERADMINISTRATIF_DADM.*,
					w_SCA_ENVELOPPERUM_ENVR_RSS.*,
					w_SCA_ENVELOPPERUM_ENVR.*,
					COALESCE(MEDN_CODE,'') AS MEDN_CODE,
					substr(NTMD_CODE,1,4) AS NTMD_CODE,
					SID_PATIENT_PATI.*
				FROM w_finess,
					prod_csis.SCA_RESUMESS_RESS
					JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM ON DADM_ID = RESS_ID_DADM AND DADM_DATEDEBUT >= '[ANNEE]0101'
					JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID 
					JOIN w_SCA_ENVELOPPERUM_ENVR ON RESS_ID_ENVR = ENVR_ID 
					JOIN prod_csis.SMV_MOUVEMENT_MVMT ON MVMT_ID = ENVR_ID1_MVMT
					JOIN prod_csis.SST_UNITEMED_NTMD ON MVMT_CHPNUM1 = NTMD_ID
					JOIN prod_csis.SID_PATIENT_PATI ON DADM_ID_PATI = PATI_ID
					LEFT JOIN w_SCA_ENVELOPPERUM_ENVR_RSS ON RESS_ID_DADM = RSS_ENVR_ID_DADM
					LEFT JOIN prod_csis.SST_MEDECIN_MEDN ON ENVR_ID_MEDN = MEDN_ID
					LEFT JOIN pmsi.p_rss ON DADM_NDA = p_rss.no_sejour_administratif AND p_rss.etat = '' AND date_trunc('month',date_sortie) = date_trunc('month', DADM_DATEFIN)
				WHERE ETAB_FINESS =  '[FINESS]' AND
					p_rss.oid IS NULL AND RESS_IDENTIFIANTRSS <> ''
				ORDER BY DADM_DATEFIN;



				INSERT INTO pmsi.p_rss (
					finess, 
					oid, 
					no_rss, 
					no_patient, 
					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, 
					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,
					age_gestationnel,
					date_dernieres_regles,
					from_oid
					)
				SELECT 
					finess, 
					rss_id AS oid, 
					no_rss, 
					to_number(PATI_IPP, '000000000000000')::bigint AS no_patient, 
					'' AS version_groupage, 
					date(PATI_DATENAISSANCE) AS date_naissance,
					PATI_CODESEXE::text AS sexe,
					date(DADM_DATEDEBUT) AS date_entree, 
					date(DADM_DATEFIN) AS date_sortie, 
					COALESCE(RSS_ENVR_MODEENT,'8') AS mode_entree, 
					COALESCE(RSS_ENVR_PROVENANCE,'') AS provenance, 
					COALESCE(RSS_ENVR_MODESOR,'8') AS mode_sortie, 
					COALESCE(RSS_ENVR_DESTINATION,'') AS destination, 
					'' AS code_postal, 
					0 AS poids_bebe, 
					0 AS igs2, 
					'' AS cma, 
					substr(RESS_CODEGHM , 3 , 1) AS groupe_ghm, 
					CASE WHEN DADM_DATEFIN > DADM_DATEDEBUT THEN date(DADM_DATEFIN) - date(DADM_DATEDEBUT) ELSE 0 END AS duree_sejour, 
					CASE WHEN substr(RESS_CODEGHM,1,2) = '28' THEN 1 ELSE 1 END AS nb_rsa, 
					CASE WHEN DADM_DATEDEBUT > PATI_DATENAISSANCE THEN trunc((date(DADM_DATEDEBUT) - date(PATI_DATENAISSANCE)) / 365.25,0) ELSE 0 END AS age, 
					'' AS supprime, 
					now() AS date_import, 
					nb_rum, 
					'' AS secteur, 
					DADM_NDA AS no_sejour_administratif, 
					CASE WHEN substr(RESS_CODEGHM,1,2) = '28' THEN 1 ELSE 0 END AS nb_seances, 
					'' AS ghm_fg9, 
					COALESCE(t_ghm.oid, 0) AS ghm_id, 
					COALESCE(t_medecins.oid, 0) AS medecin_rss_id, 
					COALESCE(t_ghs.oid, 0) AS ghs_id, 
					date_part('year', DADM_DATEFIN) * 100 + date_part('month', DADM_DATEFIN) AS mois_sortie, 
					COALESCE(t_diagnostics_dp.oid, 0) AS diagnostic_principal_id, 
					COALESCE(t_diagnostics_dr.oid, 0) AS diagnostic_relie_id, 
					COALESCE(t_ghm.oid, 0) AS ghm_production_id, 
					no_rum AS no_rum_principal, 
					COALESCE(t_unites_medicales.oid, 0) AS unite_medicale_principale_id, 
					-1 AS import_id, 
					'' AS etat, 
					0 AS acte_principal_id, 
					0 AS code_postal_id, 
					0 AS patient_id, 
					0 AS prestation_principale_id, 
					'' AS severite_fg11_simulation_code, 
					'1' AS en_cours, 
					CASE WHEN length(RESS_CODEGHM) = 6 AND substr(RESS_CODEGHM,1,2) <> '90' AND RESS_ETATGRP = 'V' THEN 'V' WHEN length(RESS_CODEGHM) = 6 AND substr(RESS_CODEGHM,1,2) <> '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, 
					'' AS traitement_epmsi, 
					'' AS code_retour_groupage, 
					0 AS comite_medical_id, 
					'0' AS rehosp_meme_ghm,
					CASE WHEN ENVR_SA > 99 THEN 99 ELSE ENVR_SA END as age_gestationnel,
					DADM_DATEDERNREGLE as date_dernieres_regles,
					0 AS from_oid
				FROM w_SCA_RESUMESS_RESS
					LEFT JOIN pmsi.t_ghm ON (RESS_CODEGHM = t_ghm.code)
					LEFT JOIN pmsi.t_ghs ON (RESS_NUMERO_GGHS = t_ghs.code)
					LEFT JOIN pmsi.t_medecins ON MEDN_CODE = t_medecins.code
					LEFT JOIN pmsi.t_unites_medicales ON NTMD_CODE = t_unites_medicales.code
					LEFT JOIN pmsi.t_diagnostics t_diagnostics_dp ON DSDG_CODE_DP = t_diagnostics_dp.code
					LEFT JOIN pmsi.t_diagnostics t_diagnostics_dr ON DSDG_CODE_DR = t_diagnostics_dr.code;
										
				INSERT INTO base.t_etablissements(code, texte, texte_court)
				SELECT 
					ETBS_FINESS,
					ETBS_RAISONSOCIALE,
					ETBS_RAISONSOCIALE
				FROM prod_csis.SNO_ETABLISSEMENT_ETBS 
				WHERE ETBS_FINESS NOT IN (SELECT code FROM base.t_etablissements)
				GROUP BY 1,2,3
				;

				/**********************************************************************************
				 *            	 ETABLISSEMENT PROVENANCE ET DESTINATION (TRANSFERT)              *
				 **********************************************************************************/

				-- Insertion des établissement de provenance et destination dans p_rss_transfert
				-- Traitement en commun réalisé dans import_rss_rsf_encours.php (generation_rum_post_traitements())
				-- Grâce à la table w_TRANSFERTS
				
				DROP TABLE IF EXISTS temp.w_TRANSFERTS;
				CREATE TABLE temp.w_TRANSFERTS AS 
				SELECT 
					p_rss.oid AS rss_id,
					import_id,
					MAX(COALESCE(provenance.oid,0)) AS prov_id,
					MAX(COALESCE(destination.oid,0)) AS dest_id
				FROM
					pmsi.p_rss
					JOIN prod_csis.SID_DOSSIERADMINISTRATIF_DADM ON DADM_NDA = no_sejour_administratif
					JOIN prod_csis.SMV_DOSSIERMVT_DMVT ON DMVT_ID_DADM = DADM_ID
					LEFT JOIN prod_csis.SNO_SERVICEETABLISSEMENT_SVET SER_PROV ON DMVT_ID_SVET_PROV = SER_PROV.SVET_ID
					LEFT JOIN prod_csis.SNO_SERVICEETABLISSEMENT_SVET SER_DEST ON DMVT_ID_SVET_DEST = SER_DEST.SVET_ID
					LEFT JOIN prod_csis.SNO_ETABLISSEMENT_ETBS PROV ON PROV.ETBS_ID = SER_PROV.SVET_ID_ETBS
					LEFT JOIN prod_csis.SNO_ETABLISSEMENT_ETBS DEST ON DEST.ETBS_ID = SER_DEST.SVET_ID_ETBS
					LEFT JOIN base.t_etablissements provenance ON PROV.ETBS_FINESS = provenance.code
					LEFT JOIN base.t_etablissements destination ON DEST.ETBS_FINESS = destination.code
					WHERE 1=1 AND 
						(DMVT_ID_SVET_PROV != 0 OR DMVT_ID_SVET_DEST != 0)
				GROUP BY 1,2
				;
				
					   					
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Identités">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.p_identites(
						finess, 
						rss_id,
						no_rss, 
						nom, 
						prenom, 
						nom_naissance)
				SELECT 
						finess, 
						rss_id, 
						no_rss, 
						PATI_NOMUSAGE AS nom, 
						PATI_PRENOMS AS prenom, 
						CASE WHEN PATI_NOMFAMILLE <> PATI_NOMMARITAL THEN PATI_NOMFAMILLE ELSE '' END AS nom_naissance
				FROM w_SCA_RESUMESS_RESS;
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Rum">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.p_rss_rum(
					finess, 
					rss_id, 
					no_rss, 
					no_rum, 
					date_entree, 
					date_sortie, 
					mode_entree, 
					provenance, 
					mode_sortie, 
					destination, 
					duree_sejour, 
					nb_seances, 
					unite_medicale, 
					diagnostic_principal_id, 
					diagnostic_relie_id, 
					unite_medicale_id, 
					type_autorisation, 
					igs2, 
					type_autorisation_lit_dedie, 
					supplement_code)
				SELECT
					finess, 
					rss_id , 
					no_rss, 
					w_SCA_ENVELOPPERUM_ENVR.no_rum, 
					date(w_SCA_ENVELOPPERUM_ENVR.ENVR_DATEDEBUTRUM) AS date_entree, 
					date(w_SCA_ENVELOPPERUM_ENVR.ENVR_DATEFINRUM) AS date_sortie, 
					w_SCA_ENVELOPPERUM_ENVR.ENVR_MODEENT AS mode_entree, 
					w_SCA_ENVELOPPERUM_ENVR.ENVR_PROVENANCE AS provenance, 
					w_SCA_ENVELOPPERUM_ENVR.ENVR_MODESOR AS mode_sortie, 
					w_SCA_ENVELOPPERUM_ENVR.ENVR_DESTINATION AS destination, 
					CASE WHEN w_SCA_ENVELOPPERUM_ENVR.ENVR_DATEFINRUM > w_SCA_ENVELOPPERUM_ENVR.ENVR_DATEDEBUTRUM THEN date(w_SCA_ENVELOPPERUM_ENVR.ENVR_DATEFINRUM) - date(w_SCA_ENVELOPPERUM_ENVR.ENVR_DATEDEBUTRUM) ELSE 0 END AS duree_sejour, 
					CASE WHEN substr(RESS_CODEGHM,1,2) = '28' THEN 1 ELSE 0 END AS nb_seances, 
					substr(NTMD_CODE,1,4) AS unite_medicale, 
					COALESCE(t_diagnostics_dp.oid, 0) AS diagnostic_principal_id, 
					COALESCE(t_diagnostics_dr.oid, 0) AS diagnostic_relie_id, 
					COALESCE(t_unites_medicales.oid,0) AS unite_medicale_id, 
					'' AS type_autorisation, 
					w_SCA_ENVELOPPERUM_ENVR.ENVR_IGS2 AS igs2, 
					'' AS type_autorisation_lit_dedie, 
					'' AS supplement_code
				FROM w_SCA_ENVELOPPERUM_ENVR
					JOIN w_SCA_RESUMESS_RESS ON w_SCA_ENVELOPPERUM_ENVR.ENVR_ID_DADM = RESS_ID_DADM
					LEFT JOIN pmsi.t_unites_medicales ON (NTMD_CODE = t_unites_medicales.code)
					LEFT JOIN pmsi.t_diagnostics t_diagnostics_dp ON w_SCA_ENVELOPPERUM_ENVR.DSDG_CODE_DP = t_diagnostics_dp.code
					LEFT JOIN pmsi.t_diagnostics t_diagnostics_dr ON w_SCA_ENVELOPPERUM_ENVR.DSDG_CODE_DR = t_diagnostics_dr.code;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Diagnostics">
			<sqlcmd><![CDATA[

				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_SCA_RESUMESS_RESS.finess, 
					w_SCA_RESUMESS_RESS.rss_id, 
					w_SCA_RESUMESS_RESS.no_rss, 
					w_SCA_ENVELOPPERUM_ENVR.no_rum, 
					COALESCE(t_diagnostics.oid,0) AS diagnostic_id, 
					CASE WHEN TDIA_CODE <> 'DAS' THEN TDIA_CODE ELSE 'DA' END AS type_diagnostic, 
					CASE WHEN TDIA_CODE <> 'DAS' THEN TDIA_CODE ELSE 'DA' END AS type_diagnostic_rss, 
					'' AS imprecis, 
					unite_medicale, 
					unite_medicale_id
				FROM prod_csis.SCA_DIAGNOSTICDANSRUM_DGRM 
					JOIN w_SCA_ENVELOPPERUM_ENVR ON DGRM_ID_ENVR = ENVR_ID
					JOIN w_SCA_RESUMESS_RESS ON w_SCA_ENVELOPPERUM_ENVR.ENVR_ID_DADM = RESS_ID_DADM
					JOIN prod_csis.SNO_TYPEDIAGNOSTICS_TDIA ON DGRM_ID_TDIA = TDIA_ID
					JOIN prod_csis.SNO_DESCRDIAGNOSTIC_DSDG ON DGRM_ID_DSDG = DSDG_ID
					JOIN pmsi.t_diagnostics ON (DSDG_CODE = t_diagnostics.code)
					JOIN pmsi.p_rss_rum ON (w_SCA_RESUMESS_RESS.rss_id = p_rss_rum.rss_id AND w_SCA_ENVELOPPERUM_ENVR.no_rum = p_rss_rum.no_rum)
				WHERE TDIA_CODE IN ('DP', 'DR', 'DAS');
											
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Actes">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.p_rss_actes(
					finess, 
					rss_id, 
					no_rss, 
					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_SCA_RESUMESS_RESS.finess, 
					w_SCA_RESUMESS_RESS.rss_id, 
					w_SCA_RESUMESS_RESS.no_rss, 
					w_SCA_ENVELOPPERUM_ENVR.no_rum, 
					p_rss_rum.unite_medicale AS unite_medicale, 
					p_rss_rum.unite_medicale_id AS unite_medicale_id, 
					date(RECC_DATEACTE) AS date_acte, 
					COALESCE(t_medecins.oid,0) AS medecin_id,
					COALESCE(t_actes.oid,0) AS acte_id, 
					1::numeric AS nombre, 
					RECC_CODEPHASE AS phase_ccam, 
					RECC_CODEACTIVITE AS activite_ccam, 
					RECC_CODEEXTENSIONDOC AS extension_ccam, 
					RECC_CODEMODIFICATEUR1 AS modificateur_ccam_1, 
					RECC_CODEMODIFICATEUR2 AS modificateur_ccam_2, 
					RECC_CODEMODIFICATEUR3 AS modificateur_ccam_3, 
					RECC_CODEMODIFICATEUR4 AS modificateur_ccam_4, 
					RECC_CODEREMBEXCEP AS remboursement_excp_ccam, 
					RECC_CODEASSOCIATION AS association_nonprevue_ccam
				FROM prod_csis.mpm_recueilccam_recc
					JOIN w_SCA_ENVELOPPERUM_ENVR ON RECC_ID_DADM = w_SCA_ENVELOPPERUM_ENVR.ENVR_ID_DADM AND RECC_DATEACTE BETWEEN w_SCA_ENVELOPPERUM_ENVR.ENVR_DATEDEBUTRUM AND w_SCA_ENVELOPPERUM_ENVR.ENVR_DATEFINRUM
					JOIN w_SCA_RESUMESS_RESS ON w_SCA_ENVELOPPERUM_ENVR.ENVR_ID_DADM = RESS_ID_DADM
					JOIN pmsi.p_rss_rum ON (w_SCA_RESUMESS_RESS.rss_id = p_rss_rum.rss_id AND w_SCA_ENVELOPPERUM_ENVR.no_rum = p_rss_rum.no_rum)
					JOIN pmsi.t_actes ON substring(RECC_CODECCAM, 1, 7) = t_actes.code
					LEFT JOIN prod_csis.SST_MEDECIN_MEDN ON RECC_ID_MEDN = MEDN_ID
					LEFT JOIN pmsi.t_medecins ON SST_MEDECIN_MEDN.MEDN_CODE = t_medecins.code;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Factures">
			<sqlcmd><![CDATA[
	
				INSERT INTO pmsi.p_rsf_total(
						finess, 
						rss_id, 
						no_rss, 
						code_pec, 
						rang_beneficiaire, 
						regime, 
						nature_pec, 
						justificatif_exo, 
						no_facture, 
						code_regularisation, 
						matricule_assure, 
						cle_matricule_assure, 
						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, 
						ca_ghs_theorique, 
						taux_remboursement)
				SELECT 
						w_SCA_RESUMESS_RESS.finess, 
						w_SCA_RESUMESS_RESS.rss_id, 
						w_SCA_RESUMESS_RESS.no_rss,  
						'1' AS code_pec, 
						'' AS rang_beneficiaire, 
						'' AS regime, 
						'' AS nature_pec, 
						'' AS justificatif_exo, 
						MAX(RECF_NUMFACTALIAS) AS no_facture, 
						'1' AS code_regularisation, 
						'' AS matricule_assure, 
						'' AS cle_matricule_assure, 
						SUM(round(RECG_QUANTITE * CASE WHEN RECG_COEFFICIENT > 0 THEN RECG_COEFFICIENT ELSE 1 END * RECG_PUFACTURE * CASE WHEN RECG_COEFFICIENTMCO > 0 THEN RECG_COEFFICIENTMCO ELSE 1 END,2)) AS base_remboursement, 
						SUM(RECT_MONTANT) AS sejour_facture, 
						SUM(RECT_MONTANT) AS sejour_remboursable, 
						0 AS honoraires_factures, 
						0 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, 
						0 AS ca_ghs_theorique, 
						0 AS taux_remboursement
				FROM prod_csis.MPM_RECUEILTIERS_RECT
					JOIN w_SCA_RESUMESS_RESS ON RECT_ID_DADM = RESS_ID_DADM
					JOIN prod_csis.MPM_FACTURE_RECF ON RECT_ID_RECF = RECF_ID 
					JOIN prod_csis.MPM_RECUEIL_RECG ON RECT_ID_RECG = RECG_ID
					JOIN prod_csis.SNO_ETATRECUEIL_ETRC SNO_ETATRECUEIL_ETRC_RECT ON RECT_ID_ETRC = SNO_ETATRECUEIL_ETRC_RECT.ETRC_ID 
					JOIN prod_csis.SNO_ETATRECUEIL_ETRC SNO_ETATRECUEIL_ETRC_RECG ON RECG_ID_ETRC = SNO_ETATRECUEIL_ETRC_RECG.ETRC_ID 
					JOIN prod_csis.MPE_ASSURANCE_ASSU ON RECT_ID_ASSU = ASSU_ID
					JOIN pmsi.t_prestations ON RECG_PRESTATION = t_prestations.code
					LEFT JOIN pmsi.t_ghs ON RECG_GHS = t_ghs.code
				WHERE ASSU_TYPE = 'O' AND RECG_TYPELIGNE = 3 AND RECT_MONTANT <> 0 AND 
					SNO_ETATRECUEIL_ETRC_RECT.ETRC_CODE <> 'A' AND
					RECF_NATUREOPERATION <> '5'
				GROUP BY w_SCA_RESUMESS_RESS.finess, 
					w_SCA_RESUMESS_RESS.rss_id, 
					w_SCA_RESUMESS_RESS.no_rss;
					
					
					
				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)
				SELECT 
						w_SCA_RESUMESS_RESS.finess, 
						w_SCA_RESUMESS_RESS.rss_id, 
						w_SCA_RESUMESS_RESS.no_rss,
						'' AS nature, 
						'' AS mt, 
						'' AS dmt, 
						date(RECG_DATEDEBUT) AS date_debut, 
						date(COALESCE(RECG_DATEFIN,RECG_DATEDEBUT)) AS date_fin, 
						RECG_QUANTITE AS nombre, 
						CASE WHEN RECG_COEFFICIENT > 0 THEN RECG_COEFFICIENT ELSE 1 END AS coefficient, 
						RECG_PUFACTURE AS prix_unitaire, 
						round(RECG_QUANTITE * CASE WHEN RECG_COEFFICIENT > 0 THEN RECG_COEFFICIENT ELSE 1 END * RECG_PUFACTURE * CASE WHEN RECG_COEFFICIENTMCO > 0 THEN RECG_COEFFICIENTMCO ELSE 1 END,2) AS base_remboursement, 
						COALESCE(RECT_TAUXREEL,RECT_TAUXPEC) AS taux_remboursement, 
						RECT_MONTANT AS sejour_facture, 
						RECT_MONTANT AS sejour_remboursable, 
						0 AS compteur, 
						'' AS ligne_t2a, 
						'' AS pec_fj, 
						CASE WHEN RECG_COEFFICIENTMCO > 0 THEN RECG_COEFFICIENTMCO ELSE 1 END AS coefficient_mco, 
						0 AS sejour_remboursable_2, 
						COALESCE(t_ghs.oid, 0) AS ghs_id,
						0 AS sejour_rembourse_noemie, 
						'' AS nature_noemie, 
						COALESCE(t_prestations.oid, 0) AS prestation_id, 
						'B' AS type, 
						0 AS coefficient_geo, 
						'1' AS est_ligne_rss, 
						'0' AS est_ligne_rum, 
						0 AS no_rum, 
						0 AS unite_medicale_id, 
						0 AS base_remboursement_rum
				FROM prod_csis.MPM_RECUEILTIERS_RECT
					JOIN w_SCA_RESUMESS_RESS ON RECT_ID_DADM = RESS_ID_DADM
					JOIN prod_csis.MPM_FACTURE_RECF ON RECT_ID_RECF = RECF_ID 
					JOIN prod_csis.MPM_RECUEIL_RECG ON RECT_ID_RECG = RECG_ID
					JOIN prod_csis.SNO_ETATRECUEIL_ETRC SNO_ETATRECUEIL_ETRC_RECT ON RECT_ID_ETRC = SNO_ETATRECUEIL_ETRC_RECT.ETRC_ID 
					JOIN prod_csis.SNO_ETATRECUEIL_ETRC SNO_ETATRECUEIL_ETRC_RECG ON RECG_ID_ETRC = SNO_ETATRECUEIL_ETRC_RECG.ETRC_ID 
					JOIN prod_csis.MPE_ASSURANCE_ASSU ON RECT_ID_ASSU = ASSU_ID
					JOIN pmsi.t_prestations ON RECG_PRESTATION = t_prestations.code
					LEFT JOIN pmsi.t_ghs ON RECG_GHS = t_ghs.code
				WHERE ASSU_TYPE = 'O' AND RECG_TYPELIGNE = 3 AND RECT_MONTANT <> 0 AND 
					SNO_ETATRECUEIL_ETRC_RECT.ETRC_CODE <> 'A' AND
					RECF_NATUREOPERATION <> '5';
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="LPP">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.p_rss_lpp(
					finess, 
					rss_id, 
					no_rss, 
					rang, 
					date_lpp, 
					lpp_id, 
					nombre, 
					prix_unitaire, 
					montant_facture, 
					non_pris_en_compte)
				SELECT
					w_SCA_RESUMESS_RESS.finess, 
					w_SCA_RESUMESS_RESS.rss_id, 
					w_SCA_RESUMESS_RESS.no_rss, 
					0 AS rang, 
					date(COALESCE(RECD_DATEACTE, RECG_DATEDEBUT)) AS date_lpp, 
					COALESCE(t_lpp.oid, 0) AS lpp_id, 
					RECD_QUANTITE AS nombre, 
					RECD_PUFACTURE AS prix_unitaire, 
					RECD_MONTANTFACTURE AS montant_facture, 
					'0' AS non_pris_en_compte
				FROM prod_csis.MPM_RECUEILDETAIL_RECD
					JOIN w_SCA_RESUMESS_RESS ON RECD_ID_DADM = RESS_ID_DADM
					JOIN prod_csis.MPM_RECUEIL_RECG ON RECD_ID_RECG = RECG_ID
					JOIN pmsi.t_lpp ON (RECD_CODEAFFINE = t_lpp.code)
				WHERE RECD_CODEAFFINE <> '' AND RECD_NATUREDETAIL = 'lpp';	
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="UCD">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.p_rss_ucd(
					finess, 
					rss_id,
					no_rss, 
					rang, 
					date_ucd, 
					ucd_id, 
					nombre, 
					coefficient_fractionnement, 
					prix_unitaire_achat, 
					prix_unitaire_ecart_indemnisable, 
					montant_ecart_indemnisable, 
					montant_facture, 
					non_pris_en_compte)
				SELECT
					w_SCA_RESUMESS_RESS.finess, 
					w_SCA_RESUMESS_RESS.rss_id, 
					w_SCA_RESUMESS_RESS.no_rss, 
					0 AS rang, 
					date(COALESCE(RECD_DATEACTE, RECG_DATEDEBUT)) AS date_ucd, 
					COALESCE(t_ucd.oid,0) AS ucd_id, 
					RECD_QUANTITE AS nombre, 
					COALESCE(RECD_COEFFRAC,0) AS coefficient_fractionnement, 
					COALESCE(RECD_PUACHAT, RECD_PUFACTURE) AS prix_unitaire_achat, 
					COALESCE(RECD_MONTANTECART,0) / RECD_QUANTITE AS prix_unitaire_ecart_indemnisable, 
					COALESCE(RECD_MONTANTECART,0) AS montant_ecart_indemnisable, 
					RECD_MONTANTFACTURE AS montant_facture, 
					'0' AS non_pris_en_compte
				FROM prod_csis.MPM_RECUEILDETAIL_RECD
					JOIN w_SCA_RESUMESS_RESS ON RECD_ID_DADM = RESS_ID_DADM
					JOIN prod_csis.MPM_RECUEIL_RECG ON RECD_ID_RECG = RECG_ID
					LEFT JOIN pmsi.t_ucd ON (RECD_CODEAFFINE = t_ucd.code)
				WHERE RECD_CODEAFFINE <> '' AND RECD_NATUREDETAIL = 'ph. UCD';
										
			]]></sqlcmd>
		</NODE>
		
		
		
		<NODE label="Compléments identifications Externes">
			<sqlcmd><![CDATA[
			
					
				
				
				
										
			]]></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[
			
				-- date groupage
				UPDATE pmsi.p_rss_etat
				SET date_groupage = subview.date_groupage
				FROM
					(
						SELECT p_rss.oid AS rss_id,
							MAX(date(COALESCE(RESS_DATEDERNGRP,'20991231'))) AS date_groupage
						FROM pmsi.p_rss
						JOIN pmsi.p_rss_etat ON p_rss_etat.rss_id = p_rss.oid
						LEFT JOIN 
                            (
                            prod_csis.SID_DOSSIERADMINISTRATIF_DADM
                                JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
                            ) ON no_sejour_administratif = DADM_NDA AND
							date_trunc('month',SID_DOSSIERADMINISTRATIF_DADM.DADM_DATEFIN) = date_trunc('month', p_rss.date_sortie)
						LEFT JOIN prod_csis.sca_resumess_ress ON (SID_DOSSIERADMINISTRATIF_DADM.dadm_id = ress_id_dadm)
						GROUP BY 1
						HAVING MAX(date(COALESCE(RESS_DATEDERNGRP,'20991231'))) IS DISTINCT FROM MAX(p_rss_etat.date_groupage)
				) subview
				WHERE p_rss_etat.rss_id = subview.rss_id;
				
				-- Date facture
				UPDATE pmsi.p_rss_etat
				SET date_facture = subview.date_facture,
					est_facture = CASE WHEN subview.date_facture < '20991231' THEN '1' ELSE '0' END
				FROM
					(
						SELECT p_rss.oid AS rss_id,
							MAX(date(COALESCE(RECF_DATEFACTDEB,'20991231'))) AS date_facture
						FROM pmsi.p_rss
						JOIN pmsi.p_rss_etat ON p_rss_etat.rss_id = p_rss.oid
						LEFT JOIN (
                            prod_csis.SID_DOSSIERADMINISTRATIF_DADM
                                JOIN prod_csis.SST_ETABLISSEMENT_ETAB ON DADM_ID_ETAB = ETAB_ID AND ETAB_FINESS = '[FINESS]'
                            ) ON no_sejour_administratif = DADM_NDA AND
							date_trunc('month',SID_DOSSIERADMINISTRATIF_DADM.DADM_DATEFIN) = date_trunc('month', p_rss.date_sortie)
						LEFT JOIN prod_csis.MPM_FACTURE_RECF ON (SID_DOSSIERADMINISTRATIF_DADM.dadm_id = RECF_id_dadm)
						GROUP BY 1
						HAVING MAX(date(COALESCE(RECF_DATEFACTDEB,'20991231'))) IS DISTINCT FROM MAX(p_rss_etat.date_facture)
				) subview
				WHERE p_rss_etat.rss_id = subview.rss_id;

			
				UPDATE pmsi.p_rss_etat
				SET est_facture = CASE WHEN date_facture < '20991231' THEN '1' ELSE '0' END
				FROM pmsi.p_rss
				WHERE rss_id = p_rss.oid AND
					en_cours = '1' AND
					est_facture IS DISTINCT FROM CASE WHEN date_facture < '20991231' THEN '1' ELSE '0' END;

				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>













