<?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  
					RU_CODE_MED,
					max(RU_MED_NOM), 
					max(RU_MED_PRENOM),
					0,
					'',
					0
				FROM prod_baclesse.V_CFB_RSS_RESPONSABILITE
				WHERE RU_CODE_MED IS NOT NULL AND
					RU_CODE_MED <> '' AND
					RU_CODE_MED NOT IN (SELECT code FROM pmsi.t_medecins)
				GROUP BY 1;
				
				INSERT INTO pmsi.t_comites_medicaux(code, texte)
				SELECT CASE WHEN CODE_COMITE IS NOT NULL AND CODE_COMITE <> '' THEN CODE_COMITE ELSE RU_LIB_UF END AS CODE_COMITE, MAX(RU_LIB_UF) 
					FROM prod_baclesse.V_CFB_RSS_RESPONSABILITE 
					WHERE (CODE_COMITE <> '' OR RU_LIB_UF <> '') AND
						CASE WHEN CODE_COMITE IS NOT NULL AND CODE_COMITE <> '' THEN CODE_COMITE ELSE RU_LIB_UF END NOT IN (SELECT code FROM pmsi.t_comites_medicaux)
					GROUP BY 1
					ORDER BY 1;
				
											
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Identités hospitalises">
			<sqlcmd><![CDATA[
		
				DROP TABLE IF EXISTS W_RSS_IDENT;
				CREATE TEMP TABLE W_RSS_IDENT AS
				SELECT V_RSS_IDENT.RSS_ID,
					to_char(HO_NUM,'FM000000000') AS HO_NUM, 
					V_RSS_IDENT.NOM AS IDENT_NOM,
					V_RSS_IDENT.PRENOM AS IDENT_PRENOM,
					DATE(RSS_DDEB) AS RSS_DDEB,
					DATE(RSS_DFIN) AS RSS_DFIN,
					NIP,
					COALESCE(RU_CODE_MED,'') AS CODE_MED,
					CASE WHEN CODE_COMITE IS NOT NULL AND CODE_COMITE <> '' THEN CODE_COMITE ELSE COALESCE(RU_LIB_UF,'') END AS CODE_COMITE
				FROM prod_baclesse.V_RSS_IDENT
					LEFT JOIN prod_baclesse.V_CFB_RSS_RESPONSABILITE ON V_RSS_IDENT.RSS_ID = V_CFB_RSS_RESPONSABILITE.RU_RSS
				;

				CREATE INDEX W_RSS_IDENT_ik_RSS_ID
					ON W_RSS_IDENT
					USING btree
					(RSS_ID);

				CREATE INDEX W_RSS_IDENT_ik_HO_NUM
					ON W_RSS_IDENT
					USING btree
					(HO_NUM);

				CREATE INDEX W_RSS_IDENT_ik_CODE_MED
					ON W_RSS_IDENT
					USING btree
					(CODE_MED);

				CREATE INDEX W_RSS_IDENT_ik_CODE_COMITE
					ON W_RSS_IDENT
					USING btree
					(CODE_COMITE);

				UPDATE [SCHEMA].p_identites SET
					nom = IDENT_NOM, 
					prenom = IDENT_PRENOM
				FROM [SCHEMA].p_rss
					JOIN W_RSS_IDENT ON p_rss.no_rss = W_RSS_IDENT.rss_id AND date_sortie = RSS_DFIN
				WHERE p_identites.rss_id = p_rss.oid AND
					(nom IS DISTINCT FROM IDENT_NOM OR 
					prenom IS DISTINCT FROM IDENT_PRENOM);


				UPDATE [SCHEMA].p_identites SET
					nom = IDENT_NOM, 
					prenom = IDENT_PRENOM
				FROM [SCHEMA].p_rss
					JOIN W_RSS_IDENT ON p_rss.no_sejour_administratif = W_RSS_IDENT.HO_NUM AND date_sortie = RSS_DFIN
				WHERE p_identites.rss_id = p_rss.oid AND
					(nom IS DISTINCT FROM IDENT_NOM OR 
					prenom IS DISTINCT FROM IDENT_PRENOM);


				UPDATE [SCHEMA].p_rss SET
					medecin_rss_id = COALESCE(t_medecins.oid,p_rss.medecin_rss_id),
					comite_medical_id = COALESCE(t_comites_medicaux.oid,p_rss.comite_medical_id),
					no_patient = NIP
				FROM W_RSS_IDENT
					LEFT JOIN pmsi.t_medecins ON CODE_MED = t_medecins.code
					LEFT JOIN pmsi.t_comites_medicaux ON CODE_COMITE = t_comites_medicaux.code
				WHERE no_rss = rss_id AND date_sortie = RSS_DFIN AND
					(medecin_rss_id IS DISTINCT FROM COALESCE(t_medecins.oid,p_rss.medecin_rss_id) OR
					comite_medical_id IS DISTINCT FROM COALESCE(t_comites_medicaux.oid,p_rss.comite_medical_id) OR
					no_patient IS DISTINCT FROM NIP);


				UPDATE [SCHEMA].p_rss SET
					medecin_rss_id = COALESCE(t_medecins.oid,p_rss.medecin_rss_id),
						comite_medical_id = COALESCE(t_comites_medicaux.oid,p_rss.comite_medical_id),
				no_patient = NIP
				FROM W_RSS_IDENT
					LEFT JOIN pmsi.t_medecins ON CODE_MED = t_medecins.code
					LEFT JOIN pmsi.t_comites_medicaux ON CODE_COMITE = t_comites_medicaux.code
				WHERE p_rss.no_sejour_administratif = W_RSS_IDENT.HO_NUM AND date_sortie = RSS_DFIN AND
					(medecin_rss_id IS DISTINCT FROM COALESCE(t_medecins.oid,p_rss.medecin_rss_id) OR
					comite_medical_id IS DISTINCT FROM COALESCE(t_comites_medicaux.oid,p_rss.comite_medical_id) OR
					no_patient IS DISTINCT FROM NIP);
				
		]]></sqlcmd>
		</NODE>
		
			
		<NODE label="Médecins actes hospitalise">
			<sqlcmd><![CDATA[

				DROP TABLE IF EXISTS w_RSS_ACTE;
				CREATE TEMP TABLE w_RSS_ACTE AS
				SELECT V_RSS_IDENT.RSS_ID, INTER_EXE, CODE_ACTE, ACTIVITE, DATE(DAT_ACTE) AS DAT_ACTE
						FROM prod_baclesse.V_RSS_ACTE
						JOIN prod_baclesse.V_RSS_IDENT ON V_RSS_ACTE.RSS_ID = V_RSS_IDENT.RSS_ID
				WHERE CODE_ACTE IS NOT NULL AND
					CODE_ACTE <> '' AND
					DATE(DAT_ACTE) BETWEEN  DATE(RSS_DDEB) AND DATE(RSS_DFIN)
				GROUP BY 1,2,3,4,5
				;

				CREATE INDEX w_RSS_ACTE_RSS_ID
					ON w_RSS_ACTE
					USING btree
					(RSS_ID);

				CREATE INDEX w_RSS_ACTE_CODE_ACTE
					ON w_RSS_ACTE
					USING btree
					(CODE_ACTE);

				CREATE INDEX w_RSS_ACTE_INTER_EXE
					ON w_RSS_ACTE
					USING btree
					(INTER_EXE);


				UPDATE [SCHEMA].p_rss_actes
				SET medecin_id = t_medecins.oid
				FROM w_RSS_ACTE
					JOIN [SCHEMA].p_rss ON w_RSS_ACTE.RSS_ID = p_rss.no_rss
					JOIN pmsi.t_medecins ON INTER_EXE = t_medecins.code
					JOIN pmsi.t_actes ON substring(CODE_ACTE, 1, 7) = t_actes.code
				WHERE p_rss_actes.rss_id = p_rss.oid AND
					p_rss_actes.date_acte = DAT_ACTE AND
					p_rss_actes.activite_ccam = ACTIVITE AND
					p_rss_actes.acte_id = t_actes.oid AND
					p_rss_actes.medecin_id IS DISTINCT FROM t_medecins.oid;

		
			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Identités externes">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_CFB_RESPONS_PAT_EXT;
				CREATE TEMP TABLE w_CFB_RESPONS_PAT_EXT AS
				SELECT to_char(NPATEXT,'FM000000000') AS NPATEXTA, 
					UPPER(SPLIT_PART(IDENT,',',1)) AS IDENT_NOM,
					UPPER(SPLIT_PART(IDENT,',',2)) AS IDENT_PRENOM,
					*
				FROM prod_baclesse.V_CFB_RESPONS_PAT_EXT;

				CREATE INDEX w_cfb_respons_pat_ext_ik_npatext
					ON w_cfb_respons_pat_ext
					USING btree
					(npatexta);

				CREATE INDEX w_cfb_respons_pat_ext_ik_codemedresp
					ON w_cfb_respons_pat_ext
					USING btree
					(codemedresp);

				CREATE INDEX w_cfb_respons_pat_ext_ik_codecomite
					ON w_cfb_respons_pat_ext
					USING btree
					(codecomite);


				UPDATE [SCHEMA].p_identites SET
					nom = IDENT_NOM, 
					prenom = IDENT_PRENOM
				FROM [SCHEMA].p_rss
					JOIN w_CFB_RESPONS_PAT_EXT ON no_sejour_administratif = NPATEXTA
				WHERE p_identites.rss_id = p_rss.oid AND
					(nom IS DISTINCT FROM IDENT_NOM OR 
					prenom IS DISTINCT FROM IDENT_PRENOM);
					
				UPDATE [SCHEMA].p_rss SET
					medecin_rss_id = COALESCE(t_medecins.oid,p_rss.medecin_rss_id),
					comite_medical_id = COALESCE(t_comites_medicaux.oid,p_rss.comite_medical_id),
					no_patient = NIP
				FROM w_CFB_RESPONS_PAT_EXT
					LEFT JOIN pmsi.t_medecins ON CODEMEDRESP = t_medecins.code
					LEFT JOIN pmsi.t_comites_medicaux ON CODECOMITE = t_comites_medicaux.code
				WHERE no_sejour_administratif = NPATEXTA AND
					(medecin_rss_id IS DISTINCT FROM COALESCE(t_medecins.oid,p_rss.medecin_rss_id) OR
					comite_medical_id IS DISTINCT FROM COALESCE(t_comites_medicaux.oid,p_rss.comite_medical_id) OR
					no_patient IS DISTINCT FROM NIP);

				
			]]></sqlcmd>
		</NODE>
			
		<NODE label="Médecins actes externes">
			<sqlcmd><![CDATA[

				DROP TABLE IF EXISTS w_PASSAGE_CDAM;
				CREATE TEMP TABLE w_PASSAGE_CDAM AS
				SELECT to_char(CDAM_HONUM,'FM000000000') AS HONUM, CDAM_INXCODE AS INTER_EXE, DG_OFF AS CODE_ACTE, DG_ACTIVITE AS ACTIVITE, date(CDAM_DATE) as CDAM_DATE
				FROM prod_baclesse.PASSAGE_CDAM 
					JOIN prod_baclesse.DIAGITEM ON  CDAM_DGCODE = DG_CODE  AND DG_TYPE = 'A' 
					LEFT JOIN prod_baclesse.RSYS_RSS ON RSS_HONUM = CDAM_HONUM
				WHERE DG_OFF IS NOT NULL AND  
					rss_id IS NULL ;

				CREATE INDEX w_PASSAGE_CDAM_HONUM
					ON w_PASSAGE_CDAM
					USING btree
					(HONUM);

				CREATE INDEX w_PASSAGE_CDAM_CODE_ACTE
					ON w_PASSAGE_CDAM
					USING btree
					(CODE_ACTE);

				CREATE INDEX w_PASSAGE_CDAM_INTER_EXE
					ON w_PASSAGE_CDAM
					USING btree
					(INTER_EXE);

				UPDATE [SCHEMA].p_rss_actes
				SET medecin_id = t_medecins.oid
				FROM w_PASSAGE_CDAM
					JOIN [SCHEMA].p_rss ON HONUM = no_sejour_administratif
					JOIN pmsi.t_medecins ON INTER_EXE = t_medecins.code
					JOIN pmsi.t_actes ON substring(CODE_ACTE, 1, 7) = t_actes.code
				WHERE p_rss_actes.rss_id = p_rss.oid AND
					p_rss_actes.date_acte = CDAM_DATE AND
					p_rss_actes.acte_id = t_actes.oid AND
					p_rss_actes.medecin_id IS DISTINCT FROM t_medecins.oid;

		
			]]></sqlcmd>
		</NODE>
		
	
	</NODE>
	
	<NODE name="PMSIENCOURS" label="RECUPERATION DES DOSSIERS PMSI EN-COURS HM">
	
	</NODE>
		

</ROOT>













