<?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
						C_PRAT,
						NOM_PRATICIEN, 
						''::text AS PRENOM_PRATICIEN, 
						COALESCE(t_specialites_medecin.oid,0),
						COALESCE(NUMID,''),
						COALESCE(t_specialites_internes_medecin.oid,0)
					FROM prod_qsp.DPRATICIEN
						LEFT JOIN pmsi.t_specialites_medecin ON (t_specialites_medecin.code = trim(to_char(SPECIALITE,'00')))
						LEFT JOIN pmsi.t_specialites_internes_medecin ON (t_specialites_internes_medecin.code = trim(to_char(SPECIALITE,'00')) || '..')
						LEFT JOIN pmsi.t_medecins ON (C_PRAT = t_medecins.code)
					WHERE C_PRAT IN (SELECT C_PRAT FROM prod_qsp.PMSI_RUM) AND t_medecins.code IS NULL
				;
				
				INSERT INTO pmsi.t_medecins(code, nom, prenom, specialite_id, no_adeli, specialite_interne_id)
					SELECT
						C_PRAT,
						NOM_PRATICIEN, 
						''::text AS PRENOM_PRATICIEN, 
						COALESCE(t_specialites_medecin.oid,0),
						COALESCE(NUMID,''),
						COALESCE(t_specialites_internes_medecin.oid,0)
					FROM prod_qsp.DPRATICIEN
						LEFT JOIN pmsi.t_specialites_medecin ON (t_specialites_medecin.code = trim(to_char(SPECIALITE,'00')))
						LEFT JOIN pmsi.t_specialites_internes_medecin ON (t_specialites_internes_medecin.code = trim(to_char(SPECIALITE,'00')) || '..')
						LEFT JOIN pmsi.t_medecins ON (C_PRAT = t_medecins.code)
					WHERE C_PRAT IN (SELECT PRAT_EXEC FROM prod_qsp.DOSSIER_CCAM) AND t_medecins.code IS NULL
				;
				
				INSERT INTO pmsi.t_medecins(code, nom, prenom, specialite_id, no_adeli, specialite_interne_id)
					SELECT
						C_PRAT,
						NOM_PRATICIEN, 
						''::text AS PRENOM_PRATICIEN, 
						COALESCE(t_specialites_medecin.oid,0),
						COALESCE(NUMID,''),
						COALESCE(t_specialites_internes_medecin.oid,0)
					FROM prod_qsp.DPRATICIEN
						LEFT JOIN pmsi.t_specialites_medecin ON (t_specialites_medecin.code = trim(to_char(SPECIALITE,'00')))
						LEFT JOIN pmsi.t_specialites_internes_medecin ON (t_specialites_internes_medecin.code = trim(to_char(SPECIALITE,'00')) || '..')
						LEFT JOIN pmsi.t_medecins ON (C_PRAT = t_medecins.code)
					WHERE C_PRAT IN (SELECT MEDSUIV FROM prod_qsp.DOSSIER) AND t_medecins.code IS NULL
				;
				
				
				UPDATE pmsi.t_unites_medicales
				SET texte = libelle
				FROM prod_qsp.pmsi_service
				WHERE t_unites_medicales.code = pmsi_service.c_serv AND
					t_unites_medicales.code = t_unites_medicales.texte;
								
			]]></sqlcmd>
		</NODE>
		<NODE label="Identités">
			<sqlcmd><![CDATA[
                    
				UPDATE [SCHEMA].p_identites
				SET nom = NOM_PAT,
					nom_naissance = COALESCE(PATIENT.MAR_PAT,''),
					prenom = PRE_PAT
				FROM [SCHEMA].p_rss
					JOIN prod_qsp.PMSI_RESULT ON PMSI_RESULT.N_RSS = p_rss.no_rss
					JOIN prod_qsp.DOSSIER ON DOSSIER.NDOSSIER  = PMSI_RESULT.NDOSSIER
					JOIN prod_qsp.PATIENT ON PATIENT.NPATIENT = DOSSIER.NPATIENT
				WHERE 1=1
					AND p_identites.rss_id = p_rss.oid
					AND date_part('month',DOSSIER.DATE_SORTIE) = date_part('month', p_rss.date_sortie)
					AND n_rss > 0
					AND PMSI_RESULT.N_CLINI = 1
					AND p_rss.import_id = [IMPORT_ID]
					AND (1!=1
						OR p_identites.nom IS DISTINCT FROM NOM_PAT
						OR p_identites.nom_naissance IS DISTINCT FROM COALESCE(PATIENT.MAR_PAT,'')
						OR p_identites.prenom IS DISTINCT FROM PRE_PAT
						)
				;
				
				-- Spécial externes
				UPDATE [SCHEMA].p_rss
				SET no_sejour_administratif = N_HOSPI
				FROM [SCHEMA].p_rsf_total
				JOIN prod_qsp.FACTURE ON FACTURE.NUM_FACT = p_rsf_total.no_facture
				WHERE import_id = [IMPORT_ID] AND
					p_rss.ghm_id = 0 AND
					p_rss.oid = p_rsf_total.rss_id AND
					no_sejour_administratif IS DISTINCT FROM N_HOSPI;
					
				UPDATE [SCHEMA].p_identites
				SET nom = NOM_PAT,
					nom_naissance = COALESCE(PATIENT.MAR_PAT,''),
					prenom = PRE_PAT
				FROM [SCHEMA].p_rss
					JOIN prod_qsp.DOSSIER ON DOSSIER.NDOSSIER  = p_rss.no_sejour_administratif
					JOIN prod_qsp.PATIENT ON PATIENT.NPATIENT = DOSSIER.NPATIENT
				WHERE 1=1
					AND p_identites.rss_id = p_rss.oid
					AND date_part('month',DOSSIER.DATE_SORTIE) = date_part('month', p_rss.date_sortie)
					AND p_rss.ghm_id = 0
					AND p_rss.import_id = [IMPORT_ID]
					AND (1!=1
						OR p_identites.nom IS DISTINCT FROM NOM_PAT
						OR p_identites.nom_naissance IS DISTINCT FROM COALESCE(PATIENT.MAR_PAT,'')
						OR p_identites.prenom IS DISTINCT FROM PRE_PAT
						)
				;	

				UPDATE [SCHEMA].p_rss
				SET no_patient = base.cti_to_number(DOSSIER.NPATIENT)
				FROM prod_qsp.DOSSIER
				WHERE 1=1
                    AND DOSSIER.NDOSSIER = p_rss.no_sejour_administratif
                    AND date_trunc('month',DOSSIER.DATE_SORTIE) = date_trunc('month', p_rss.date_sortie)
					AND no_patient IS DISTINCT FROM base.cti_to_number(DOSSIER.NPATIENT);
										
			]]></sqlcmd>
		</NODE>
		<NODE label="Médecins RSS">
			<sqlcmd><![CDATA[

				UPDATE [SCHEMA].p_rss
				SET medecin_rss_id = COALESCE(t_medecins.oid,0)
				FROM prod_qsp.PMSI_RESULT
					JOIN prod_qsp.PMSI_RUM ON PMSI_RUM.NDOSSIER = PMSI_RESULT.NDOSSIER
					JOIN prod_qsp.DOSSIER ON DOSSIER.NDOSSIER  = PMSI_RESULT.NDOSSIER
					JOIN pmsi.t_medecins ON t_medecins.code = PMSI_RUM.c_prat
				WHERE 1=1
					AND PMSI_RESULT.N_RSS = p_rss.no_rss
					AND date_part('month',DOSSIER.DATE_SORTIE) = date_part('month', p_rss.date_sortie)
					AND n_rss > 0
					AND PMSI_RESULT.N_CLINI = 1
					AND p_rss.import_id = [IMPORT_ID]
					AND medecin_rss_id IS DISTINCT FROM COALESCE(t_medecins.oid,0)
				;
				
				UPDATE [SCHEMA].p_rss
				SET medecin_rss_id = COALESCE(t_medecins.oid,0)
				FROM prod_qsp.DOSSIER 
					JOIN pmsi.t_medecins ON t_medecins.code = DOSSIER.MEDSUIV
				WHERE 1=1
					AND DOSSIER.NDOSSIER =p_rss.no_sejour_administratif
					AND date_part('month',DOSSIER.DATE_SORTIE) = date_part('month', p_rss.date_sortie)
					AND ghm_id > 0
					AND p_rss.import_id = [IMPORT_ID]
					AND medecin_rss_id = 0
				;
				
				-- Spécial externes
				UPDATE [SCHEMA].p_rss
				SET medecin_rss_id = COALESCE(t_medecins.oid,0)
				FROM prod_qsp.DOSSIER 
					JOIN pmsi.t_medecins ON t_medecins.code = DOSSIER.MEDSUIV
				WHERE 1=1
					AND DOSSIER.NDOSSIER =p_rss.no_sejour_administratif
					AND date_part('month',DOSSIER.DATE_SORTIE) = date_part('month', p_rss.date_sortie)
					AND ghm_id = 0
					AND p_rss.import_id = [IMPORT_ID]
					AND medecin_rss_id IS DISTINCT FROM COALESCE(t_medecins.oid,0)
				;
										
			]]></sqlcmd>
		</NODE>
		<NODE label="Médecins actes">
			<sqlcmd><![CDATA[
				
				DROP TABLE IF EXISTS w_PMSI_ACTE;
				CREATE TEMP TABLE w_PMSI_ACTE AS
				SELECT PMSI_RUM.NDOSSIER, DOSSIER.DATE_SORTIE, N_ACTE, CODE_ACTIVITE, PMSI_ACTE.DATE_ACTE, PRAT_EXEC, CODE_CCAM_DESCR
				FROM prod_qsp.PMSI_RUM
				JOIN prod_qsp.DOSSIER ON PMSI_RUM.NDOSSIER = DOSSIER.NDOSSIER
				JOIN prod_qsp.PMSI_ACTE ON PMSI_RUM.N_UNIQUE = PMSI_ACTE.N_UNIQUE
				JOIN prod_qsp.DOSSIER_CCAM ON PMSI_ACTE.N_UNIQUE_CCAM = DOSSIER_CCAM.N_UNIQUE_CCAM
				WHERE PMSI_ACTE.N_UNIQUE_CCAM <> 0;

				CREATE INDEX w_PMSI_ACTE_i1
				ON w_PMSI_ACTE
				USING btree
				(NDOSSIER);

				UPDATE [SCHEMA].p_rss_actes
				SET medecin_id = t_medecins.oid 
				FROM w_PMSI_ACTE
				JOIN [SCHEMA].p_rss ON no_sejour_administratif = ndossier AND date_part('month',w_PMSI_ACTE.DATE_SORTIE) = date_part('month', p_rss.date_sortie)
				JOIN pmsi.t_medecins ON PRAT_EXEC = t_medecins.code
				JOIN pmsi.t_actes ON w_PMSI_ACTE.N_ACTE || COALESCE(w_PMSI_ACTE.CODE_CCAM_DESCR, '') = t_actes.code
				WHERE p_rss_actes.rss_id = p_rss.oid AND
					(
					p_rss_actes.acte_id = t_actes.oid OR
					p_rss_actes.acte_ccam_descriptif_id = t_actes.oid
					) AND
					date(p_rss_actes.date_acte) = date(w_PMSI_ACTE.DATE_ACTE) AND
					p_rss_actes.activite_ccam = w_PMSI_ACTE.CODE_ACTIVITE AND
					p_rss_actes.medecin_id IS DISTINCT FROM t_medecins.oid;

				UPDATE [SCHEMA].p_rss_actes
				SET medecin_id = t_medecins.oid 
				FROM w_PMSI_ACTE
				JOIN [SCHEMA].p_rss ON no_sejour_administratif = ndossier AND date_part('month',w_PMSI_ACTE.DATE_SORTIE) = date_part('month', p_rss.date_sortie)
				JOIN pmsi.t_medecins ON PRAT_EXEC = t_medecins.code
				JOIN pmsi.t_actes ON w_PMSI_ACTE.N_ACTE || COALESCE(w_PMSI_ACTE.CODE_CCAM_DESCR, '') = t_actes.code
				WHERE p_rss_actes.rss_id = p_rss.oid AND
					(
					p_rss_actes.acte_id = t_actes.oid OR
					p_rss_actes.acte_ccam_descriptif_id = t_actes.oid
					) AND
					p_rss_actes.activite_ccam = w_PMSI_ACTE.CODE_ACTIVITE AND
					p_rss_actes.medecin_id = 0;
				
										
			]]></sqlcmd>
		</NODE>
	</NODE>
	<NODE name="PMSIENCOURS" label="RECUPERATION DES DOSSIERS PMSI EN-COURS QSP">
		<NODE label="Paramètres">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.t_medecins(code, nom, prenom, specialite_id, no_adeli, specialite_interne_id)
				SELECT
					C_PRAT,
					NOM_PRATICIEN, 
					''::text AS PRENOM_PRATICIEN, 
					COALESCE(t_specialites_medecin.oid,0),
					COALESCE(NUMID,''),
					COALESCE(t_specialites_internes_medecin.oid,0)
				FROM prod_qsp.DPRATICIEN
					LEFT JOIN pmsi.t_specialites_medecin ON (t_specialites_medecin.code = trim(to_char(SPECIALITE,'00')))
					LEFT JOIN pmsi.t_specialites_internes_medecin ON (t_specialites_internes_medecin.code = trim(to_char(SPECIALITE,'00')) || '..')
					LEFT JOIN pmsi.t_medecins ON (C_PRAT = t_medecins.code)
				WHERE C_PRAT IN (SELECT C_PRAT FROM prod_qsp.PMSI_RUM) AND t_medecins.code IS NULL;
										
			]]></sqlcmd>
		</NODE>
		<NODE label="RSS">
			<sqlcmd><![CDATA[

				DROP TABLE IF EXISTS w_finess; 
				CREATE TEMP TABLE w_finess AS
				SELECT MAX(code) AS code FROM base.t_finess WHERE secondaire <> 1;

				DROP TABLE IF EXISTS w_PMSI_RESULT;
				CREATE TEMP TABLE w_PMSI_RESULT AS
				SELECT 
					(SELECT code FROM w_finess) AS finess, 
					PMSI_RESULT.*, 
					DOSSIER.DATE_SORTIE, 
					DOSSIER.DATE_ENTREE, 
					DOSSIER.NPATIENT, 
					PATIENT.CPO_PAT, 
					PATIENT.SEXE, 
					PATIENT.NOM_PAT, 
					PATIENT.PRE_PAT, 
					COALESCE(PATIENT.MAR_PAT,'') AS MAR_PAT, 
					base.date(NAI_PAT) AS NAI_PAT, 
					nextval('pmsi.s_rss'::regclass) AS rss_id
				FROM prod_qsp.PMSI_RESULT
					JOIN prod_qsp.DOSSIER ON PMSI_RESULT.NDOSSIER = DOSSIER.NDOSSIER 
					JOIN prod_qsp.PATIENT ON DOSSIER.NPATIENT = PATIENT.NPATIENT
					JOIN prod_qsp.CLINIQUE ON DOSSIER.ID_CLI = CLINIQUE.FINESS 
					LEFT JOIN pmsi.p_rss ON PMSI_RESULT.N_RSS = p_rss.no_rss AND p_rss.etat = '' AND date_part('month',DOSSIER.DATE_SORTIE) = date_part('month', p_rss.date_sortie)
				WHERE DOSSIER.DATE_SORTIE BETWEEN '[ANNEE]0101' AND ('[ANNEE]1231'::date + '10 years'::interval)::date
					AND n_rss > 0
					AND p_rss.no_rss IS NULL
					AND DOSSIER.PMSI_GERE = 'O'
				ORDER BY PMSI_RESULT.N_RSS;

                -- Evite d'insérer des RSS alors que le séjour est déjà en base (premier prestataire ou import ePMSI)
                DELETE       
                FROM  w_PMSI_RESULT
                WHERE ndossier IN 
                (
                    SELECT 
                        no_sejour_administratif 
                    FROM pmsi.p_rss 
                    WHERE 1=1
                    AND etat <> 'S'
                    GROUP BY no_sejour_administratif
                 );
	
				DROP TABLE IF EXISTS w_PMSI_ACTIVERUM;
				CREATE TEMP TABLE w_PMSI_ACTIVERUM AS
				SELECT w_PMSI_RESULT.NDOSSIER, w_PMSI_RESULT.N_SEQ, N_RUM, MAX(N_UNIQUE) AS N_UNIQUE
				FROM prod_qsp.PMSI_RUM
					JOIN w_PMSI_RESULT ON w_PMSI_RESULT.NDOSSIER = PMSI_RUM.NDOSSIER AND w_PMSI_RESULT.N_SEQ = PMSI_RUM.N_SEQ
				GROUP BY 1,2,3;
				
				DROP TABLE IF EXISTS W_PMSI_RSS;
				CREATE TEMP TABLE W_PMSI_RSS AS
				SELECT 
					w_PMSI_RESULT.finess,
					w_PMSI_RESULT.rss_id,
					w_PMSI_RESULT.N_RSS, 
					w_PMSI_RESULT.NDOSSIER, 
					w_PMSI_RESULT.N_SEQ,
					'1' AS facture_ok,
					w_PMSI_RESULT.DATE_ENTREE,
					w_PMSI_RESULT.DATE_SORTIE,
					w_PMSI_RESULT.CPO_PAT,
					w_PMSI_RESULT.SEXE,
					w_PMSI_RESULT.NAI_PAT,
					w_PMSI_RESULT.NPATIENT, 
					CASE WHEN w_PMSI_RESULT.MAR_PAT <> '' THEN w_PMSI_RESULT.MAR_PAT ELSE w_PMSI_RESULT.NOM_PAT END AS NOM_PAT_NAI , 
					CASE WHEN w_PMSI_RESULT.MAR_PAT <> '' THEN w_PMSI_RESULT.NOM_PAT ELSE w_PMSI_RESULT.NOM_PAT END AS NOM_PAT, 
					PRE_PAT, 
					TRIM(COALESCE(w_PMSI_RESULT.CMD,'')) AS CMD,
					TRIM(COALESCE(w_PMSI_RESULT.GHM,'')) AS GHM,
					COALESCE(w_PMSI_RESULT.GHS,0) AS GHS,
					w_PMSI_RESULT.VALIDER,
					to_number(substr(MIN(to_char(w_PMSI_ACTIVERUM.N_RUM,'00000') || to_char(w_PMSI_ACTIVERUM.N_UNIQUE, '0000000000')),8,10),'0000000000') AS N_UNIQUE_RUM1,
					to_number(substr(MAX(to_char(w_PMSI_ACTIVERUM.N_RUM,'00000') || to_char(w_PMSI_ACTIVERUM.N_UNIQUE, '0000000000')),8,10),'0000000000') AS N_UNIQUE_RUM9,
                    (MIN(ARRAY[PMSI_RUM.N_RUM::text, substr(PMSI_RUM.MODE_ENT, 1,1)::text]))[2] as mode_entree,
                    (MIN(ARRAY[PMSI_RUM.N_RUM::text, substr(PMSI_RUM.MODE_ENT, 2,1)::text]))[2] as provenance,
                    (MAX(ARRAY[PMSI_RUM.N_RUM::text, substr(PMSI_RUM.MODE_SOR, 1,1)::text]))[2] as mode_sortie,
                    (MAX(ARRAY[PMSI_RUM.N_RUM::text, substr(PMSI_RUM.MODE_SOR, 2,1)::text]))[2] as destination,
					trim(substr(MIN(to_char(PMSI_RUM.N_RUM,'00000') || PMSI_RUM.DIAG_PRI),7,10)) AS DIAG_PRI,
					trim(substr(MIN(to_char(PMSI_RUM.N_RUM,'00000') || PMSI_RUM.DIAG_REL),7,10)) AS DIAG_REL,
					trim(substr(MIN(to_char(PMSI_RUM.N_RUM,'00000') || PMSI_RUM.C_PRAT),7,10)) AS C_PRAT,
					trim(substr(MIN(to_char(PMSI_RUM.N_RUM,'00000') || CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END),7,10)) AS UM,
					SUM(SEANCE) AS SEANCE,
					count(*) AS nb_rum,
					MAX(POIDS) AS POIDS
					
					FROM w_PMSI_RESULT 
					JOIN w_PMSI_ACTIVERUM ON w_PMSI_RESULT.NDOSSIER = w_PMSI_ACTIVERUM.NDOSSIER AND w_PMSI_RESULT.N_SEQ = w_PMSI_ACTIVERUM.N_SEQ
					JOIN prod_qsp.PMSI_RUM ON w_PMSI_ACTIVERUM.N_UNIQUE = PMSI_RUM.N_UNIQUE
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
                ;

                CREATE TEMP TABLE w_PMSI_RUM_CPRAT
                AS
                    SELECT 
                        NDOSSIER,
                        trim(substr(MIN(to_char(PMSI_RUM.N_RUM,'00000') || PMSI_RUM.C_PRAT),7,10)) AS CPRAT
                    FROM prod_qsp.PMSI_RUM
                    WHERE 1=1
                        AND COALESCE(C_PRAT, '')::text <> ''
                    GROUP BY 1
                ;

                UPDATE W_PMSI_RSS
                    SET C_PRAT = w_PMSI_RUM_CPRAT.CPRAT
                FROM w_PMSI_RUM_CPRAT
                WHERE 1=1 
                    AND trim(W_PMSI_RSS.C_PRAT) = ''
                    AND w_PMSI_RUM_CPRAT.NDOSSIER = W_PMSI_RSS.NDOSSIER
                ;
		
				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, 
						from_oid
						)
				SELECT 
						w_PMSI_RSS.finess AS finess, 
						w_PMSI_RSS.rss_id AS oid, 
						w_PMSI_RSS.N_RSS AS no_rss, 
						w_PMSI_RSS.NPATIENT AS no_patient, 
						'' AS version_groupage, 
						w_PMSI_RSS.NAI_PAT AS date_naissance, 
						w_PMSI_RSS.SEXE AS sexe, 
						w_PMSI_RSS.DATE_ENTREE AS date_entree, 
						w_PMSI_RSS.DATE_SORTIE AS date_sortie, 
						w_PMSI_RSS.mode_entree AS mode_entree, 
						w_PMSI_RSS.provenance AS provenance, 
						w_PMSI_RSS.mode_sortie AS mode_sortie, 
						w_PMSI_RSS.destination AS destination, 
						w_PMSI_RSS.CPO_PAT AS code_postal, 
						w_PMSI_RSS.POIDS AS poids_bebe, 
						0 AS igs2, 
						'' AS cma, 
						'' AS groupe_ghm, 
						CASE WHEN w_PMSI_RSS.DATE_SORTIE > w_PMSI_RSS.DATE_ENTREE THEN date(w_PMSI_RSS.DATE_SORTIE) - date(w_PMSI_RSS.DATE_ENTREE) ELSE 0 END AS duree_sejour, 
						CASE WHEN w_PMSI_RSS.CMD = '28' THEN w_PMSI_RSS.SEANCE ELSE 1 END AS nb_rsa, 
						CASE WHEN w_PMSI_RSS.DATE_ENTREE > w_PMSI_RSS.NAI_PAT THEN trunc((date(w_PMSI_RSS.DATE_ENTREE) - date(w_PMSI_RSS.NAI_PAT)) / 365.25,0) ELSE 0 END AS age, 
						'' AS supprime, 
						now() AS date_import, 
						w_PMSI_RSS.nb_rum AS nb_rum, 
						'' AS secteur, 
						w_PMSI_RSS.NDOSSIER AS no_sejour_administratif, 
						CASE WHEN w_PMSI_RSS.CMD = '28' THEN w_PMSI_RSS.SEANCE ELSE 1 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', w_PMSI_RSS.DATE_SORTIE) * 100 + date_part('month', w_PMSI_RSS.DATE_SORTIE) AS mois_sortie, 
						COALESCE(t_diagnostics.oid, 0) AS diagnostic_principal_id, 
						COALESCE(t_diagnostics_relies.oid, 0) AS diagnostic_relie_id, 
						COALESCE(t_ghm.oid, 0) AS ghm_production_id, 
						1 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(w_PMSI_RSS.GHM) = 6 AND w_PMSI_RSS.CMD <> '90' AND w_PMSI_RSS.VALIDER = 1 THEN 'V' WHEN length(w_PMSI_RSS.GHM) = 6 AND w_PMSI_RSS.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, 
						'' AS traitement_epmsi, 
						'' AS code_retour_groupage, 
						0 AS comite_medical_id, 
						'0' AS rehosp_meme_ghm, 
						0 AS from_oid
				FROM w_PMSI_RSS 
					LEFT JOIN pmsi.t_ghm ON (w_PMSI_RSS.GHM = t_ghm.code)
					LEFT JOIN pmsi.t_ghs ON (w_PMSI_RSS.GHS = t_ghs.code)
					LEFT JOIN pmsi.t_diagnostics ON (w_PMSI_RSS.DIAG_PRI = t_diagnostics.code)
					LEFT JOIN pmsi.t_diagnostics t_diagnostics_relies ON (w_PMSI_RSS.DIAG_REL = t_diagnostics_relies.code)
					LEFT JOIN pmsi.t_unites_medicales ON (w_PMSI_RSS.UM = t_unites_medicales.code)
					LEFT JOIN pmsi.t_medecins ON (w_PMSI_RSS.C_PRAT = t_medecins.code);
										
										
			]]></sqlcmd>
		</NODE>
		<NODE label="Identités">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.p_identites(
						finess, 
						rss_id,
						no_rss, 
						nom, 
						prenom, 
						nom_naissance)
				SELECT 
						w_PMSI_RSS.finess AS finess, 
						w_PMSI_RSS.rss_id AS oid, 
						w_PMSI_RSS.N_RSS AS no_rss, 
						NOM_PAT AS nom, 
						PRE_PAT AS prenom, 
						NOM_PAT_NAI AS nom_naissance
				FROM w_PMSI_RSS ;
			
				UPDATE pmsi.p_rss
				SET no_patient = base.cti_to_number(DOSSIER.NPATIENT)
				FROM prod_qsp.DOSSIER
				WHERE 1=1
                    AND DOSSIER.NDOSSIER = p_rss.no_sejour_administratif
                    AND date_trunc('month',DOSSIER.DATE_SORTIE) = date_trunc('month', p_rss.date_sortie)
					AND no_patient IS DISTINCT FROM base.cti_to_number(DOSSIER.NPATIENT);
										
			]]></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
					w_PMSI_RSS.finess  AS finess, 
					w_PMSI_RSS.rss_id AS rss_id, 
					w_PMSI_RSS.N_RSS AS no_rss, 
					PMSI_RUM.N_RUM AS no_rum, 
					PMSI_RUM.DATE_ENTREE AS date_entree, 
					PMSI_RUM.DATE_SORTIE AS date_sortie, 
					SUBSTR(LPAD(PMSI_RUM.MODE_ENT, 2, '8'),1,1) AS mode_entree, 
					SUBSTR(LPAD(PMSI_RUM.MODE_ENT, 2, '8'),2,1) AS provenance, 
					SUBSTR(LPAD(PMSI_RUM.MODE_SOR, 2, '8'),1,1) AS mode_sortie, 
					SUBSTR(LPAD(PMSI_RUM.MODE_SOR, 2, '8'),2,1) AS destination, 
					CASE WHEN PMSI_RUM.DATE_SORTIE > PMSI_RUM.DATE_ENTREE AND date(PMSI_RUM.DATE_SORTIE) - date(PMSI_RUM.DATE_ENTREE) <= 99999 THEN date(PMSI_RUM.DATE_SORTIE) - date(PMSI_RUM.DATE_ENTREE) ELSE 0 END AS duree_sejour, 
					CASE WHEN w_PMSI_RSS.CMD = '28' THEN PMSI_RUM.SEANCE ELSE 0 END AS nb_seances, 
					trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) AS unite_medicale, 
					COALESCE(t_diagnostics.oid,0) AS diagnostic_principal_id, 
					COALESCE(t_diagnostics_relies.oid,0) AS diagnostic_relie_id, 
					COALESCE(t_unites_medicales.oid,0) AS unite_medicale_id, 
					'' AS type_autorisation, 
					PMSI_RUM.IGS AS igs2, 
					'' AS type_autorisation_lit_dedie, 
					'' AS supplement_code
				FROM w_PMSI_RSS 
					JOIN w_PMSI_ACTIVERUM ON w_PMSI_RSS.NDOSSIER = w_PMSI_ACTIVERUM.NDOSSIER AND w_PMSI_RSS.N_SEQ = w_PMSI_ACTIVERUM.N_SEQ
					JOIN prod_qsp.PMSI_RUM ON w_PMSI_ACTIVERUM.N_UNIQUE = PMSI_RUM.N_UNIQUE
					LEFT JOIN pmsi.t_unites_medicales ON trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) = t_unites_medicales.code
					LEFT JOIN pmsi.t_diagnostics ON (PMSI_RUM.DIAG_PRI = t_diagnostics.code)
					LEFT JOIN pmsi.t_diagnostics t_diagnostics_relies ON (PMSI_RUM.DIAG_REL = t_diagnostics_relies.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_PMSI_RSS.finess  AS finess, 
					w_PMSI_RSS.rss_id AS rss_id, 
					w_PMSI_RSS.N_RSS AS no_rss, 
					PMSI_RUM.N_RUM AS no_rum, 
					COALESCE(t_diagnostics.oid,0) AS diagnostic_id, 
					'DP' AS type_diagnostic, 
					'DP' AS type_diagnostic_rss, 
					'' AS imprecis, 
					trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) AS unite_medicale, 
					COALESCE(t_unites_medicales.oid,0) AS unite_medicale_id
				FROM w_PMSI_RSS 
					JOIN w_PMSI_ACTIVERUM ON w_PMSI_RSS.NDOSSIER = w_PMSI_ACTIVERUM.NDOSSIER AND w_PMSI_RSS.N_SEQ = w_PMSI_ACTIVERUM.N_SEQ
					JOIN prod_qsp.PMSI_RUM ON w_PMSI_ACTIVERUM.N_UNIQUE = PMSI_RUM.N_UNIQUE
					LEFT JOIN pmsi.t_unites_medicales ON trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) = t_unites_medicales.code
					LEFT JOIN pmsi.t_diagnostics ON (PMSI_RUM.DIAG_PRI = t_diagnostics.code)
				WHERE COALESCE(PMSI_RUM.DIAG_PRI,'') <> '';
	
				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_PMSI_RSS.finess  AS finess, 
					w_PMSI_RSS.rss_id AS rss_id, 
					w_PMSI_RSS.N_RSS AS no_rss, 
					PMSI_RUM.N_RUM AS no_rum, 
					COALESCE(t_diagnostics.oid,0) AS diagnostic_id, 
					'DR' AS type_diagnostic, 
					'DR' AS type_diagnostic_rss, 
					'' AS imprecis, 
					trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) AS unite_medicale, 
					COALESCE(t_unites_medicales.oid,0) AS unite_medicale_id
				FROM w_PMSI_RSS 
					JOIN w_PMSI_ACTIVERUM ON w_PMSI_RSS.NDOSSIER = w_PMSI_ACTIVERUM.NDOSSIER AND w_PMSI_RSS.N_SEQ = w_PMSI_ACTIVERUM.N_SEQ
					JOIN prod_qsp.PMSI_RUM ON w_PMSI_ACTIVERUM.N_UNIQUE = PMSI_RUM.N_UNIQUE
					LEFT JOIN pmsi.t_unites_medicales ON trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) = t_unites_medicales.code
					LEFT JOIN pmsi.t_diagnostics ON (PMSI_RUM.DIAG_REL = t_diagnostics.code)
				WHERE COALESCE(PMSI_RUM.DIAG_REL,'') <> '';
	
				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_PMSI_RSS.finess  AS finess, 
					w_PMSI_RSS.rss_id AS rss_id, 
					w_PMSI_RSS.N_RSS AS no_rss, 
					PMSI_RUM.N_RUM AS no_rum, 
					COALESCE(t_diagnostics.oid,0) AS diagnostic_id, 
					CASE WHEN PMSI_DIAG.D_DOC = 1 THEN 'DD' ELSE 'DA' END AS type_diagnostic, 
					CASE WHEN PMSI_DIAG.D_DOC = 1 THEN 'DD' ELSE 'DA' END AS type_diagnostic_rss, 
					'' AS imprecis, 
					trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) AS unite_medicale, 
					COALESCE(t_unites_medicales.oid,0) AS unite_medicale_id
				FROM w_PMSI_RSS 
					JOIN w_PMSI_ACTIVERUM ON w_PMSI_RSS.NDOSSIER = w_PMSI_ACTIVERUM.NDOSSIER AND w_PMSI_RSS.N_SEQ = w_PMSI_ACTIVERUM.N_SEQ
					JOIN prod_qsp.PMSI_RUM ON w_PMSI_ACTIVERUM.N_UNIQUE = PMSI_RUM.N_UNIQUE
					JOIN prod_qsp.PMSI_DIAG ON PMSI_RUM.N_UNIQUE = PMSI_DIAG.N_UNIQUE
					LEFT JOIN pmsi.t_unites_medicales ON trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) = t_unites_medicales.code
					LEFT JOIN pmsi.t_diagnostics ON (PMSI_DIAG.N_DIAG = t_diagnostics.code)
				WHERE PMSI_DIAG.D_DOC IN (0, 1);
											
										
			]]></sqlcmd>
		</NODE>
		<NODE label="Actes">
			<sqlcmd><![CDATA[

				INSERT INTO pmsi.t_medecins(code, nom, prenom, specialite_id, no_adeli, specialite_interne_id)
				SELECT
					C_PRAT,
					NOM_PRATICIEN, 
					''::text AS PRENOM_PRATICIEN, 
					COALESCE(t_specialites_medecin.oid,0),
					COALESCE(NUMID,''),
					COALESCE(t_specialites_internes_medecin.oid,0)
				FROM prod_qsp.DPRATICIEN
					LEFT JOIN pmsi.t_specialites_medecin ON (t_specialites_medecin.code = trim(to_char(SPECIALITE,'00')))
					LEFT JOIN pmsi.t_specialites_internes_medecin ON (t_specialites_internes_medecin.code = trim(to_char(SPECIALITE,'00')) || '..')
					LEFT JOIN pmsi.t_medecins ON (C_PRAT = t_medecins.code)
				WHERE C_PRAT IN (
						SELECT
							PRAT_EXEC
						FROM w_PMSI_RSS 
							JOIN w_PMSI_ACTIVERUM ON w_PMSI_RSS.NDOSSIER = w_PMSI_ACTIVERUM.NDOSSIER AND w_PMSI_RSS.N_SEQ = w_PMSI_ACTIVERUM.N_SEQ
							JOIN prod_qsp.PMSI_RUM ON w_PMSI_ACTIVERUM.N_UNIQUE = PMSI_RUM.N_UNIQUE
							JOIN prod_qsp.PMSI_ACTE ON PMSI_RUM.N_UNIQUE = PMSI_ACTE.N_UNIQUE
							JOIN prod_qsp.DOSSIER_CCAM ON PMSI_ACTE.N_UNIQUE_CCAM = DOSSIER_CCAM.N_UNIQUE_CCAM
							LEFT JOIN pmsi.t_unites_medicales ON trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) = t_unites_medicales.code
							LEFT JOIN pmsi.t_actes ON PMSI_ACTE.N_ACTE || COALESCE(PMSI_ACTE.CODE_CCAM_DESCR, '') = t_actes.code
							LEFT JOIN pmsi.t_medecins ON (DOSSIER_CCAM.PRAT_EXEC = t_medecins.code)
						WHERE PMSI_ACTE.N_UNIQUE_CCAM <> 0 AND t_medecins.oid IS NULL
						)
				AND t_medecins.oid IS NULL;
				
				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_PMSI_RSS.finess  AS finess, 
					w_PMSI_RSS.rss_id AS rss_id, 
					w_PMSI_RSS.N_RSS AS no_rss, 
					PMSI_RUM.N_RUM AS no_rum,  
					trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) AS unite_medicale, 
					COALESCE(t_unites_medicales.oid,0) AS unite_medicale_id,
					PMSI_ACTE.DATE_ACTE AS date_acte, 
					COALESCE(t_medecins.oid,0) AS medecin_id,
					COALESCE(t_actes.oid,0) AS acte_id, 
					PMSI_ACTE.NB_ACTE AS nombre, 
					PMSI_ACTE.N_PHASE AS phase_ccam, 
					PMSI_ACTE.CODE_ACTIVITE AS activite_ccam, 
					PMSI_ACTE.EXT_DOC AS extension_ccam, 
					substr(PMSI_ACTE.MODIF,1,1) AS modificateur_ccam_1, 
					substr(PMSI_ACTE.MODIF,2,1) AS modificateur_ccam_2, 
					substr(PMSI_ACTE.MODIF,3,1) AS modificateur_ccam_3, 
					substr(PMSI_ACTE.MODIF,4,1) AS modificateur_ccam_4, 
					PMSI_ACTE.REMB_EXCEPT AS remboursement_excp_ccam, 
					PMSI_ACTE.ASSOC_NP AS association_nonprevue_ccam
				FROM w_PMSI_RSS 
					JOIN w_PMSI_ACTIVERUM ON w_PMSI_RSS.NDOSSIER = w_PMSI_ACTIVERUM.NDOSSIER AND w_PMSI_RSS.N_SEQ = w_PMSI_ACTIVERUM.N_SEQ
					JOIN prod_qsp.PMSI_RUM ON w_PMSI_ACTIVERUM.N_UNIQUE = PMSI_RUM.N_UNIQUE
					JOIN prod_qsp.PMSI_ACTE ON PMSI_RUM.N_UNIQUE = PMSI_ACTE.N_UNIQUE
					JOIN prod_qsp.DOSSIER_CCAM ON PMSI_ACTE.N_UNIQUE_CCAM = DOSSIER_CCAM.N_UNIQUE_CCAM
					LEFT JOIN pmsi.t_unites_medicales ON trim(CASE WHEN COALESCE(PMSI_RUM.UM,'') <> '' THEN SUBSTR(PMSI_RUM.UM,1,4) ELSE SUBSTR(PMSI_RUM.C_SERV,1,4) END) = t_unites_medicales.code
					LEFT JOIN pmsi.t_actes ON PMSI_ACTE.N_ACTE || COALESCE(PMSI_ACTE.CODE_CCAM_DESCR, '') = t_actes.code
					LEFT JOIN pmsi.t_medecins ON (DOSSIER_CCAM.PRAT_EXEC = t_medecins.code)
				WHERE PMSI_ACTE.N_UNIQUE_CCAM <> 0 ;
										
										
			]]></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, 
					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) 
				SELECT 
					w_PMSI_RSS.finess  AS finess, 
					w_PMSI_RSS.rss_id AS rss_id, 
					w_PMSI_RSS.N_RSS AS no_rss, 
					PMSI_FACTURE.TP_PEC AS code_pec, 
					trim(to_char(PMSI_FACTURE.BEN_PAT,'000')) AS rang_beneficiaire, 
					trim(to_char(PMSI_FACTURE.GRAND_REG,'00')) AS regime, 
					trim(to_char(PMSI_FACTURE.NAT_ASS,'00')) AS nature_pec,  
					'' AS justificatif_exo, 
					'' AS no_facture, 
					'' AS code_regularisation, 
					PMSI_FACTURE.BASE_SEJ AS base_remboursement,  
					PMSI_FACTURE.TOTAL_SEJ AS sejour_facture, 
					PMSI_FACTURE.REMB_SEJ AS sejour_remboursable, 
					PMSI_FACTURE.TOTAL_HON AS honoraires_factures, 
					PMSI_FACTURE.REMB_HON 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
				FROM w_PMSI_RSS
					JOIN prod_qsp.PMSI_FACTURE ON w_PMSI_RSS.NDOSSIER = PMSI_FACTURE.NDOSSIER
				WHERE N_SEQ = 0;
				
				INSERT INTO pmsi.p_rsf_total( 
					finess, 
					rss_id, 
					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, 
					ca_ghs_theorique) 
				SELECT 
					p_rss.finess, 
					p_rss.oid AS rss_id,
					p_rss.no_rss, 
					'' AS code_pec, 
					'' AS rang_beneficiaire, 
					'' AS regime, 
					'' AS nature_pec,  
					'' AS justificatif_exo, 
					'' AS no_facture, 
					'' AS code_regularisation, 
					0 AS base_remboursement,  
					0 AS sejour_facture, 
					0 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
				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_PMSI_RSS.finess  AS finess, 
					w_PMSI_RSS.rss_id AS rss_id, 
					w_PMSI_RSS.N_RSS AS no_rss, 
					'' AS nature, 
					trim(to_char(PMSI_FACT_LIGNE.MD_TRAIT,'00')) AS mt, 
					trim(to_char(PMSI_FACT_LIGNE.DISCIPLINE,'000')) AS dmt, 
					PMSI_FACT_LIGNE.DATE_DEB AS date_debut, 
					PMSI_FACT_LIGNE.DATE_FIN AS date_fin, 
					PMSI_FACT_LIGNE.QTE AS nombre, 
					PMSI_FACT_LIGNE.COEF AS coefficient, 
					PMSI_FACT_LIGNE.PU AS prix_unitaire, 
					PMSI_FACT_LIGNE.MT_BASE AS base_remboursement, 
					PMSI_FACT_LIGNE.TAUX AS taux_remboursement, 
					PMSI_FACT_LIGNE.MT_BASE AS sejour_facture, 
					PMSI_FACT_LIGNE.MT_REMB AS sejour_remboursable, 
					0 AS compteur, 
					'' AS ligne_t2a, 
					'' AS pec_fj, 
					1 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, 
					1 AS no_rum, 
					0 AS unite_medicale_id, 
					0 AS base_remboursement_rum
				FROM w_PMSI_RSS
					JOIN prod_qsp.PMSI_FACTURE ON w_PMSI_RSS.NDOSSIER = PMSI_FACTURE.NDOSSIER
					JOIN prod_qsp.PMSI_FACT_LIGNE ON w_PMSI_RSS.NDOSSIER = PMSI_FACT_LIGNE.NDOSSIER
					JOIN pmsi.t_prestations ON (PMSI_FACT_LIGNE.ACTEB2 = t_prestations.code)
					LEFT JOIN pmsi.t_ghs ON (w_PMSI_RSS.GHS = t_ghs.code)
				WHERE w_PMSI_RSS.N_SEQ = 0 AND PMSI_FACT_LIGNE.TYP = 3;
										
			]]></sqlcmd>
		</NODE>
		<!-- <NODE label="LPP">
			<sqlcmd><![CDATA[
			]]></sqlcmd>
		</NODE>
		<NODE label="UCD">
			<sqlcmd><![CDATA[
			]]></sqlcmd>
		</NODE> -->
		<NODE label="Etat des dossiers">
			<sqlcmd><![CDATA[
				
				SELECT pmsi.cti_initialize_rss_etat();
			
		]]></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,PMSI_RESULT.NDOSSIER,
						date(MAX(COALESCE(CASE WHEN p_rss.en_cours = '0' OR p_rss.en_cours_etat IN ('F', 'G', 'V') THEN PMSI_RESULT.DATE_SAIS ELSE NULL END,'20991231'))) AS date_groupage
					FROM prod_qsp.PMSI_RESULT 
						JOIN prod_qsp.PMSI_RUM ON PMSI_RESULT.NDOSSIER = PMSI_RUM.NDOSSIER AND PMSI_RESULT.N_SEQ = PMSI_RUM.N_SEQ
						JOIN prod_qsp.DOSSIER ON
							 PMSI_RESULT.NDOSSIER = DOSSIER.NDOSSIER AND PMSI_RESULT.N_CLINI = DOSSIER.N_CLINI
						JOIN pmsi.p_rss ON PMSI_RESULT.NDOSSIER = no_sejour_administratif AND 
							date_trunc('month',DOSSIER.DATE_SORTIE) = date_trunc('month', p_rss.date_sortie)
						JOIN pmsi.p_rss_etat ON p_rss_etat.rss_id = p_rss.oid	
					WHERE PMSI_RESULT.N_SEQ = 0 
					GROUP BY 1,2
					HAVING date(MAX(COALESCE(CASE WHEN p_rss.en_cours = '0' OR p_rss.en_cours_etat IN ('F', 'G', 'V') THEN PMSI_RESULT.DATE_SAIS ELSE NULL END,'20991231'))) IS DISTINCT FROm
						MAX(p_rss_etat.date_groupage)
			) subview
			WHERE p_rss_etat.rss_id = subview.rss_id;
			
			-- date facture
			DROP TABLE IF EXISTS w_FACTURE_QSP;
			CREATE TEMP TABLE w_FACTURE_QSP AS
			SELECT 
				N_HOSPI,
				MAX(DATE_SORTIE) AS DATE_SORTIE,
				MAX(date(FACTURE.DATE_SYS)) AS date_facture
			FROM prod_qsp.FACTURE 
			JOIN prod_qsp.DOSSIER ON N_HOSPI = NDOSSIER
			WHERE TIERS = 1
			GROUP BY 1;
			
			UPDATE pmsi.p_rss_etat
			SET date_facture = subview.date_facture
			FROM
				(
				SELECT p_rss.oid AS rss_id, COALESCE(w_FACTURE_QSP.date_facture,'20991231') AS date_facture
				FROM pmsi.p_rss
				JOIN pmsi.p_rss_etat ON p_rss_etat.rss_id = p_rss.oid
				LEFT JOIN w_FACTURE_QSP ON no_sejour_administratif = N_HOSPI AND 
							date_trunc('month',w_FACTURE_QSP.DATE_SORTIE) = date_trunc('month', p_rss.date_sortie)
				WHERE p_rss_etat.date_facture IS DISTINCT FROM COALESCE(w_FACTURE_QSP.date_facture,'20991231')
			) 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>
