<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="ACTI000020" label="Activité. Détail séjour" title="= 'Détail séjour ' + SELECT.SEJOUR" database="iCTI" swf="*CTI_view1" softCode="iCTI_activite" helpFile="iCTI_ACTI_ACTI000020.HTML">
	<CACHE cachable="false" />
	<VIEWPROPERTIES>
		<PROPERTY dataLink="VIEWPROPERTIES" nameField="name" valueField="value"/>
	</VIEWPROPERTIES>
	<SELECTIONS label="Paramètres">
		<GROUP>
			<FIELD name="SEJOUR" label="N° Séjour" UI="text" default="" enabled="false"/>
		</GROUP>
        <GROUP>
			<FIELD name="FACTURE" label="N° Facture" UI="text" default="" enabled="false"/>
		</GROUP>
	</SELECTIONS>
	<QUERIES>
		<QUERY label="Récapitulatif">
			<SQL optimizer="V_SEJOURS_X">
				<select><![CDATA[
                                        
					SELECT v_sejours_1.finess,
						v_sejours_1.no_patient, 
						p_patients.nom || CASE WHEN p_patients.nom_naissance <> '' AND p_patients.nom_naissance <> p_patients.nom THEN ' née ' || p_patients.nom_naissance ELSE '' END, 
						p_patients.prenom, 
						code_postal_code || ' ' || code_postal_texte,
						no_sejour,
						CASE WHEN sexe_texte_court = 'M' THEN 'Masculin' 
							WHEN sexe_texte_court = 'F' THEN 'Féminin' 
							ELSE 'Inconnu' END,
		
						CASE WHEN date_entree > date_naissance THEN trunc((date_entree::date - date_naissance::date) / 365.25,0)::text || ' an(s)' ELSE '' END ||' ('||CASE WHEN p_patients.date_naissance = '2099-12-31' THEN 'Date naiss. N/R' ELSE to_char(p_patients.date_naissance, 'DD/MM/YYYY') END||')',
						medecin_sejour_nom_prenom,
						medecin_traitant_nom_prenom,
						to_char(date_entree,'YYYY-MM-DD') || CASE WHEN code_prevu = '1' THEN ' (Prévue)' ELSE ' (' || substr(lpad(v_sejours_1.heure_entree, 6, '0'), 1, 2) || 'h' || substr(lpad(v_sejours_1.heure_entree, 6, '0'), 3, 2) || ')' END,
						CASE WHEN date_sortie = '2099-12-31' THEN ''
							WHEN code_prevu = '1' OR code_sorti <> '1' OR date_sortie >= date(now()) THEN to_char(date_sortie,'YYYY-MM-DD') || ' (Prévue)'
							ELSE to_char(date_sortie,'YYYY-MM-DD') || ' (' || substr(lpad(v_sejours_1.heure_sortie, 6, '0'), 1, 2) || 'h' || substr(lpad(v_sejours_1.heure_sortie, 6, '0'), 3, 2) || ')' END::text,
						v_sejours_1.tiers_payant_1_id,
						CASE WHEN v_sejours_1.tiers_payant_1_id <> 0 THEN t_tiers_payant_1.code || ' ' || t_tiers_payant_1.texte ELSE '' END,
						v_sejours_1.tiers_payant_2_id,
						CASE WHEN v_sejours_1.tiers_payant_2_id <> 0 THEN t_tiers_payant_2.code || ' ' || t_tiers_payant_2.texte ELSE '' END,
						v_sejours_1.tiers_payant_22_id,
						CASE WHEN v_sejours_1.tiers_payant_22_id <> 0 THEN t_tiers_payant_22.code || ' ' || t_tiers_payant_22.texte ELSE '' END,
						COALESCE(matricule_1,''),
						COALESCE(matricule_2,''),
						COALESCE(matricule_22,''),
						nb_factures,
						montant_facture_c,
						COALESCE(montant_facture_c_actes_inclus_dans_sejour,0),
						montant_facture_h,
						montant_facture_c + montant_facture_h,
						montant_comptabilise_c + COALESCE(montant_comptabilise_budget_global_c,0),
						montant_comptabilise_h,
						montant_regle_c,
						montant_regle_h,
						montant_comptabilise_c - montant_regle_c,
						montant_comptabilise_h - montant_regle_h,
						montant_comptabilise_c,
						COALESCE(montant_comptabilise_budget_global_c,0),
						CASE WHEN COALESCE(montant_comptabilise_budget_global_c,0) <> 0 THEN montant_facture_1_c + montant_facture_1_h - COALESCE(montant_comptabilise_budget_global_c,0) ELSE 0 END,
						montant_facture_1_c + montant_facture_1_h,
						CASE WHEN v_sejours_1.est_budget_global = 1 THEN 'BUDGET GLOBAL' ELSE 'HORS BUDGET GLOBAL' END,
						CASE 
							WHEN t_services_facturation.type_t2a = '2' THEN 'SSR'
							WHEN v_sejours_1.ghs_id < 0 AND v_sejours_1.ghs_id < 0 THEN 'Non' 
							ELSE 'MCO' || ' ([VIEW.NO_RSS])' 
							END,
						CASE
							WHEN v_sejours_1.ghm_id > 0 AND v_sejours_1.ghs_id > 0 THEN ghm_code || ' (GHS ' || ghs_code_text || ') ' || ghm_texte
							WHEN v_sejours_1.ghm_id > 0 THEN ghm_code || ' ' || ghm_texte
							WHEN v_sejours_1.ghs_id > 0 THEN ghs_code_text || ' ' || ghs_texte
							WHEN v_sejours_1.gme_id > 0 AND v_sejours_1.gmt_id > 0 THEN gme_code || ' (GMT ' || gmt_code || ') ' || gme_texte
							WHEN v_sejours_1.gme_id > 0 THEN gme_code || ' ' || gme_texte
							ELSE '' END,
						CASE 
							WHEN v_sejours_1.date_groupage IN ('2099-12-31', '0001-01-01') THEN ''
							WHEN v_sejours_1.type_sejour NOT IN ('1','2','5') THEN '' 
							WHEN v_sejours_1.ghs_id <= 0 AND v_sejours_1.gme_id <= 0 THEN '' 
							ELSE to_char(v_sejours_1.date_groupage, 'DD/MM/YYYY') || '    ' || v_sejours_1.delai_groupage::text || ' jour(s)' END,
						CASE WHEN v_sejours_1.date_facture IN ('2099-12-31', '0001-01-01') THEN '' ELSE to_char(v_sejours_1.date_facture, 'DD/MM/YYYY') || '    ' || v_sejours_1.delai_facture::text || ' jour(s)' END,
						CASE WHEN date_facture IN ('2099-12-31','0001-01-01') OR v_sejours_1.date_solde IN ('2099-12-31', '0001-01-01') THEN '' ELSE to_char(v_sejours_1.date_solde, 'DD/MM/YYYY') || '    ' || v_sejours_1.delai_solde::text || ' jour(s)' END,
			            CASE WHEN code_sorti = '1' AND v_sejours_1.date_sortie =  v_sejours_1.date_entree  THEN substr(lpad(lpad(v_sejours_1.heure_sortie, 6, '0')::numeric - lpad(v_sejours_1.heure_entree, 6, '0')::numeric,6,'0'), 1, 2) || ':' || substr(lpad(lpad(v_sejours_1.heure_sortie, 6, '0')::numeric - lpad(v_sejours_1.heure_entree, 6, '0')::numeric,6,'0'), 3, 2) 
			            	ELSE v_sejours_1.duree::text || ' jour(s)'  
			            END,
						CASE
							WHEN v_sejours_1.type_sejour = '9' THEN 'Fictif'
							WHEN code_prevu = '1' THEN 'Prévu'
							WHEN code_sorti <> '1' THEN 'Présent'
							WHEN date_sortie >= date(now()) THEN 'Présent'
							WHEN date_facture NOT IN ('2099-12-31','0001-01-01') AND date_solde NOT IN ('2099-12-31', '0001-01-01') THEN 'Soldé'
							WHEN date_expedition NOT IN ('2099-12-31', '0001-01-01') THEN 'Expédié'
							WHEN date_facture NOT IN ('2099-12-31', '0001-01-01') THEN 'Facturé'
							WHEN nb_non_groupe = 0 and type_t2a = '1' THEN 'Groupé'
							ELSE 'Sorti'
						END,
						v_sejours_1.etat_sejour_code || ' : ' || v_sejours_1.etat_sejour_texte,						
						CASE WHEN COALESCE(v_sejours_parametres_personnalises_1.parametre_personnalise_1_section_id[1],0) > 0 THEN 
							v_sejours_parametres_personnalises_1.parametre_personnalise_1_section_code[1] || ' ' || v_sejours_parametres_personnalises_1.parametre_personnalise_1_section_texte[1]
							ELSE '' END,
						CASE WHEN COALESCE(v_sejours_parametres_personnalises_1.parametre_personnalise_1_id,0) > 0 THEN 
							v_sejours_parametres_personnalises_1.parametre_personnalise_1_code || ' ' || v_sejours_parametres_personnalises_1.parametre_personnalise_1_texte
							ELSE '' END,
						CASE WHEN COALESCE(budget_cle_section_id[1],0) > 0 THEN 
							budget_cle_section_code[1] || ' ' || budget_cle_section_texte[1]
							ELSE '' END,
						CASE WHEN COALESCE(budget_cle_id,0) > 0 THEN 
							budget_cle_code || ' ' || budget_cle_texte
							ELSE '' END,
						
						v_intervention_1.date_debut,
						COALESCE(v_intervention_1.acte_code || ' ' || v_intervention_1.acte_texte,''),
						COALESCE(v_intervention_1.medecin_chirurgien_nom_prenom,''),
						COALESCE(v_intervention_1.medecin_anesthesiste_nom_prenom,''),
						mode_entree_code || ' - ' || mode_entree_texte || ' / ' || provenance_code || ' - ' || provenance_texte,
						mode_sortie_code || ' - ' || mode_sortie_texte || ' / ' || destination_code || ' - ' || destination_texte,
						type_sejour_texte,
                        CASE WHEN v_sejours_1.risque_id = 0 THEN ''::text ELSE risque_code::text || ' - ' || risque_texte END as risque,
                        traitement_epmsi_texte,
                        ' ' || CASE WHEN etablissement_provenance_id > 0 THEN etablissement_provenance_code ||' : ' || etablissement_provenance_texte ELSE '' END,
                        ' ' || CASE WHEN etablissement_destination_id > 0 THEN etablissement_destination_code ||' : ' || etablissement_destination_texte ELSE '' END
                        
					FROM #V_SEJOURS_X#
						LEFT JOIN activite.v_sejours_budget_cle_1 ON v_sejours_1.sejour_id = v_sejours_budget_cle_1.sejour_id
						JOIN activite.t_tiers_payant as t_tiers_payant_1 ON v_sejours_1.tiers_payant_1_id = t_tiers_payant_1.oid
						JOIN activite.t_tiers_payant as t_tiers_payant_2 ON v_sejours_1.tiers_payant_2_id = t_tiers_payant_2.oid
						JOIN activite.t_tiers_payant as t_tiers_payant_22 ON v_sejours_1.tiers_payant_22_id = t_tiers_payant_22.oid
						LEFT JOIN activite.p_patients ON v_sejours_1.no_patient = p_patients.no_patient 
						LEFT JOIN activite.p_sejours_assurance  ON v_sejours_1.sejour_id = p_sejours_assurance.sejour_id
						LEFT JOIN activite.t_services_facturation ON v_sejours_lieu_1.lieu_sortie_service_id = t_services_facturation.oid
						LEFT JOIN (
								SELECT v_intervention_1.sejour_id, v_intervention_1.date_debut, acte_code, acte_texte, medecin_chirurgien_nom_prenom, medecin_anesthesiste_nom_prenom
							FROM activite.v_intervention_1
								JOIN (
									SELECT
										sejour_id, date_debut, heure_debut
									FROM activite.v_intervention_1
									WHERE no_sejour = '[SEJOUR]'
									ORDER BY 1,2
									LIMIT 1) subview ON v_intervention_1.sejour_id = subview.sejour_id AND
												v_intervention_1.date_debut = subview.date_debut AND
												v_intervention_1.heure_debut = subview.heure_debut
									) v_intervention_1 ON v_sejours_1.sejour_id = v_intervention_1.sejour_id
									
					WHERE v_sejours_1.no_sejour = '[SEJOUR]'
					
					
				]]></select>
				<FIELDS>
					<FIELD name="FINESS"/>
					<FIELD name="NPAT"/>
					<FIELD name="NOM" ifHideNames="Non autorisé"/>
					<FIELD name="PREN" ifHideNames="Non autorisé"/>
					<FIELD name="CP"/>
					<FIELD name="NSEJ"/>
					<FIELD name="SEXE"/>
					<FIELD name="AGE"/>
					<FIELD name="MED"/>
					<FIELD name="MEDT"/>
					<FIELD name="DENT"/>
					<FIELD name="DSOR"/>
					<FIELD name="TP1I"/>
					<FIELD name="TP1"/>
					<FIELD name="TP2I"/>
					<FIELD name="TP2"/>
					<FIELD name="TP22I"/>
					<FIELD name="TP22"/>
					<FIELD name="MAT1" ifHideNames="Non autorisé"/>
					<FIELD name="MAT2" ifHideNames="Non autorisé"/>
					<FIELD name="MAT22" ifHideNames="Non autorisé"/>
					<FIELD name="NBFAC"/>
					<FIELD name="MFC"/>
					<FIELD name="MIC"/>
					<FIELD name="MFH"/>
					<FIELD name="MFT"/>
					<FIELD name="MCC"/>
					<FIELD name="MCH"/>
					<FIELD name="MRC"/>
					<FIELD name="MRH"/>
					<FIELD name="MSC"/>
					<FIELD name="MSH"/>
					<FIELD name="MCCHBG"/>
					<FIELD name="MCCBG"/>
					<FIELD name="MECBG"/>
					<FIELD name="MFT1"/>
					<FIELD name="ESTBG"/>
					<FIELD name="PMSI"/>
					<FIELD name="GHM"/>
					<FIELD name="DATEGRP"/>
					<FIELD name="DATEFAC"/>
					<FIELD name="DATESOL"/>
					<FIELD name="DURSEJ"/>
					<FIELD name="ETATSEJ"/>
					<FIELD name="ETATSEJ1"/>
					<FIELD name="PERS1SEC"/>
					<FIELD name="PERS1"/>
					<FIELD name="BUDCLESEC"/>
					<FIELD name="BUDCLE"/>
					<FIELD name="INTDAT"/>
					<FIELD name="INTACT"/>
					<FIELD name="INTCHI"/>
					<FIELD name="INTARE"/>
					<FIELD name="PROVENANCE"/>
					<FIELD name="DESTINATION"/>
					<FIELD name="TYPSEJ"/>
					<FIELD name="RISQUE"/>
					<FIELD name="EPMSI"/>
					<FIELD name="ETAB_PROV"/>
					<FIELD name="ETAB_DEST"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY label="Mouvements séjour">
			<SQL>
				<select><![CDATA[
			 
					DROP TABLE IF EXISTS w_ACTI000020_mvt;
					CREATE TEMP TABLE w_ACTI000020_mvt AS
					SELECT 
						est_mouvement_previsionnel,
						date, 
						heure_debut, 
						heure_fin, 
						lieu_service_id, 
						lieu_unite_fonctionnelle_id, 
						lieu_unite_medicale_id, 
						lieu_etage_id, 
						lieu_lit_id, 
						lieu_service_texte, 
						lieu_unite_fonctionnelle_texte,
						lieu_unite_medicale_texte,
						lieu_etage_texte, 
						CASE 
							WHEN lieu_lit_texte <> '' AND lieu_lit_code = lieu_lit_texte THEN lieu_lit_texte 
							WHEN lieu_lit_texte <> '' AND lieu_lit_code != lieu_lit_texte THEN lieu_lit_code || ' | ' || lieu_lit_texte 
							ELSE lieu_lit_code END AS lieu_lit_texte,
						medecin_mouvement_nom, 
                        lieu_gir_texte,
                        lieu_gir_id,
						nb_jours_f + nb_jours_absence_js_non_inclus AS nb_jours_js_non_inclus,
						nb_jours_f + nb_jours_absence_js_inclus AS nb_jours_js_inclus,
						equivalent_malade_complet,
						0::bigint AS sequence,
						CASE WHEN nb_externes=1 THEN '1' ELSE '0' END::text AS rupture,
						0::bigint AS sequence_rupture,
						COALESCE(lieu_service_texte,'') ||
						COALESCE(lieu_unite_fonctionnelle_texte,'') ||
						COALESCE(lieu_unite_medicale_texte,'') ||
						COALESCE(lieu_etage_texte,'') ||
						COALESCE(lieu_lit_texte,'') ||
						COALESCE(lieu_gir_texte,'') ||
						COALESCE(medecin_mouvement_nom,'') AS cle_rupture,
						type_sejour as type_sejour
					FROM activite.v_mouvements_sejour_2 
						JOIN activite.p_sejours ON v_mouvements_sejour_2.no_sejour = p_sejours.no_sejour
					WHERE v_mouvements_sejour_2.no_sejour = '[SEJOUR]' AND 
							type_sejour <> '5' AND
							type_sejour <> '3' AND
							date <= now() AND
							heure_fin >= heure_debut
					ORDER BY date, heure_debut;


					DROP SEQUENCE IF EXISTS ACTI000020_seq;
					CREATE temp SEQUENCE ACTI000020_seq
						INCREMENT 1
						MINVALUE 1
						MAXVALUE 9223372036854775807
						START 1
						CACHE 1;

					UPDATE w_ACTI000020_mvt SET sequence = nextval('ACTI000020_seq'::regclass); 

						
					UPDATE w_ACTI000020_mvt 
					SET rupture = '1'
					FROM w_ACTI000020_mvt w_ACTI000020_mvt_prev 
					WHERE w_ACTI000020_mvt_prev.sequence = w_ACTI000020_mvt.sequence -1 AND 
						w_ACTI000020_mvt_prev.cle_rupture <> w_ACTI000020_mvt.cle_rupture;
						
						
					UPDATE w_ACTI000020_mvt 
					SET rupture = '1'
					FROM w_ACTI000020_mvt w_ACTI000020_mvt_prev 
					WHERE w_ACTI000020_mvt_prev.sequence = w_ACTI000020_mvt.sequence -1 AND 
						w_ACTI000020_mvt_prev.date <> w_ACTI000020_mvt.date AND 
						w_ACTI000020_mvt_prev.date <> date(w_ACTI000020_mvt.date - interval '1 day');

					UPDATE w_ACTI000020_mvt set sequence_rupture = 
						(SELECT MAX(sequence) 
						FROM w_ACTI000020_mvt w_ACTI000020_mvt_bis 
						WHERE w_ACTI000020_mvt_bis.sequence <= w_ACTI000020_mvt.sequence AND 
						(rupture = '1' OR sequence = 1));
			
			
			
					SELECT 
						'1' || max(date::text||heure_fin::text)::text,
						min(date), 
						max(CASE WHEN heure_fin < 240000 THEN date ELSE null END), 
						(min(array[date::text,to_char(heure_debut,'FM00:00:00')]))[2], 
						(max(array[date::text,to_char(heure_fin,'FM00:00:00')]))[2], 
						CASE WHEN lieu_service_id <> 0 THEN lieu_service_texte ELSE '' END ||
						CASE WHEN lieu_unite_fonctionnelle_id <> 0 AND lieu_unite_fonctionnelle_texte <> lieu_service_texte THEN chr(13) || lieu_unite_fonctionnelle_texte ELSE '' END
						, 
						CASE WHEN lieu_unite_medicale_id <> 0 THEN lieu_unite_medicale_texte ELSE '' END, 
						CASE WHEN lieu_etage_id <> 0 THEN lieu_etage_texte ELSE '' END || chr(13) || 
						CASE WHEN lieu_lit_id <> 0 AND lieu_lit_texte NOT ILIKE '%non renseigné%' THEN lieu_lit_texte ELSE '' END,
						medecin_mouvement_nom || CASE WHEN lieu_gir_id <> 0 THEN chr(13) || lieu_gir_texte ELSE '' END, 
                        sum(nb_jours_js_non_inclus)::text || ' jour(s)'|| ' ' || min(CASE WHEN type_sejour = '2' THEN (array[date::text,to_char(heure_fin - heure_debut,'FM00:00:00')])[2] || ' h' ELSE ' ' END),  
                        sum(nb_jours_js_inclus)::text || ' jour(s)' || ' ' || min(CASE WHEN type_sejour = '2' THEN (array[date::text,to_char(heure_fin - heure_debut,'FM00:00:00')])[2] || ' h' ELSE ' ' END) ,
						CASE WHEN sum(equivalent_malade_complet) <> 0 THEN sum(equivalent_malade_complet) ELSE 0 END,
						sequence_rupture
					FROM w_ACTI000020_mvt
					GROUP BY 6, 7, 8,9, 13
					UNION
					SELECT 
						'2' || date::text,
						date,
						null,
						null,
						null,
						CASE WHEN lieu_service_id <> 0 THEN lieu_service_texte ELSE '' END ||
						CASE WHEN lieu_unite_fonctionnelle_id <> 0 AND lieu_unite_fonctionnelle_texte <> lieu_service_texte THEN chr(13) || lieu_unite_fonctionnelle_texte ELSE '' END
						, 
						CASE WHEN lieu_unite_medicale_id <> 0 THEN lieu_unite_medicale_texte ELSE '' END, 
						CASE WHEN lieu_etage_id <> 0 THEN lieu_etage_texte ELSE '' END || chr(13) || 
						CASE WHEN lieu_lit_id <> 0 AND lieu_lit_texte NOT ILIKE '%non renseigné%' THEN lieu_lit_texte ELSE '' END,
						medecin_mouvement_nom,  
						nb_seances::text || ' séance(s)' , 
						nb_seances::text || ' séance(s)',
						0,
						0 AS sequence_rupture
					FROM activite.v_mouvements_sejour_2 
						JOIN activite.p_sejours ON v_mouvements_sejour_2.no_sejour = p_sejours.no_sejour
					WHERE v_mouvements_sejour_2.no_sejour = '[SEJOUR]' AND
						nb_seances > 0
					UNION
					SELECT 
						'3' || date::text||to_char(heure_debut,'FM000000')::text,
						date,
						date, 
						to_char(heure_debut,'FM00:00:00'), 
						to_char(heure_fin,'FM00:00:00'), 
						CASE WHEN lieu_service_id <> 0 THEN lieu_service_texte ELSE '' END ||
						CASE WHEN lieu_unite_fonctionnelle_id <> 0 AND lieu_unite_fonctionnelle_texte <> lieu_service_texte THEN chr(13) || lieu_unite_fonctionnelle_texte ELSE '' END
						, 
						CASE WHEN lieu_unite_medicale_id <> 0 THEN lieu_unite_medicale_texte ELSE '' END, 
						CASE WHEN lieu_etage_id <> 0 THEN lieu_etage_texte ELSE '' END || chr(13) || 
						CASE WHEN lieu_lit_id <> 0 AND lieu_lit_texte NOT ILIKE '%non renseigné%' THEN lieu_lit_texte ELSE '' END,
						medecin_mouvement_nom,  
						'' , 
						'' , 
						0,
						0 AS sequence_rupture
					FROM activite.v_mouvements_sejour_2 
						JOIN activite.p_sejours ON v_mouvements_sejour_2.no_sejour = p_sejours.no_sejour
					WHERE v_mouvements_sejour_2.no_sejour = '[SEJOUR]' AND
						(nb_externes > 0)
					UNION
					SELECT 
						'9',
						min(date), 
						max(CASE WHEN heure_fin < 240000 THEN date ELSE null END), 
						null,
						null,
						'', 
						'', 
						'',
						'', 
						sum(nb_jours_js_non_inclus)::text || ' jour(s)' , 
						sum(nb_jours_js_inclus)::text || ' jour(s)',
						0,
						0 AS sequence_rupture
					FROM w_ACTI000020_mvt
					UNION
					SELECT 
						'9',
						min(date), 
						max(date), 
						null,
						null,
						'', 
						'', 
						'',
						'', 
						SUM(nb_seances)::text || ' séance(s)' , 
						SUM(nb_seances)::text || ' séance(s)',
						0,
						0 AS sequence_rupture
					FROM activite.v_mouvements_sejour_2 
						JOIN activite.p_sejours ON v_mouvements_sejour_2.no_sejour = p_sejours.no_sejour
					WHERE v_mouvements_sejour_2.no_sejour = '[SEJOUR]' AND
						nb_seances > 0
					HAVING SUM(nb_seances) > 0
					ORDER BY 1
					
				]]></select>
				<FIELDS>
					<FIELD name="LVL"/>
					<FIELD name="DDEB"/>
					<FIELD name="DFIN"/>
					<FIELD name="HDEB"/>
					<FIELD name="HFIN"/>
					<FIELD name="SER"/>
					<FIELD name="ETA"/>
					<FIELD name="LIT"/>
					<FIELD name="MED"/>
					<FIELD name="JOS"/>
					<FIELD name="JIS"/>
					<FIELD name="EMC"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY label="Factures">
			<SQL select="
						SELECT 
								p_factures.date_debut::text || p_factures.date_facture::text || rpad(p_factures.no_facture,20,' '),
								'1',
								p_factures.no_facture, 
								'',
								p_factures.date_debut, 
								p_factures.date_fin, 
								p_factures.type_facture, 
								p_factures.date_facture, 
								CASE WHEN p_factures.date_expedition &lt; '20991231' THEN p_factures.date_expedition ELSE null END, 
								CASE WHEN p_factures.date_vente &lt; '20991231' THEN p_factures.date_vente ELSE null END, 
								p_factures.montant_facture_c, 
								p_factures.montant_facture_h, 
								p_factures.montant_facture_c + p_factures.montant_facture_h,
								p_factures.montant_comptabilise_c + COALESCE(p_factures.montant_comptabilise_budget_global_c,0), 
								p_factures.montant_comptabilise_h, 
								p_factures.montant_regle_c, 
								p_factures.montant_regle_h, 
								p_factures.montant_comptabilise_c - p_factures.montant_regle_c,
								p_factures.montant_comptabilise_h - p_factures.montant_regle_h,
								p_factures.montant_comptabilise_budget_global_c
							FROM activite.p_factures
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							WHERE p_factures.no_sejour = '[SEJOUR]' AND 
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(
									p_factures.montant_facture_c IS DISTINCT FROM 0 OR
									p_factures.montant_facture_h IS DISTINCT FROM 0 OR
									p_factures.montant_comptabilise_c IS DISTINCT FROM 0 OR
									p_factures.montant_comptabilise_h IS DISTINCT FROM 0 OR
									p_factures.montant_regle_c IS DISTINCT FROM 0 OR
									p_factures.montant_regle_h IS DISTINCT FROM 0 OR
									p_factures.montant_comptabilise_budget_global_c IS DISTINCT FROM 0 OR
									p_factures.date_vente &lt;= now()
								)

						UNION ALL

						SELECT 
								p_factures.date_debut::text || p_factures.date_facture::text || rpad(p_factures.no_facture,20,' ') || '1',
								'2',
								'', 
								t_tiers_payant.code || ' ' || t_tiers_payant.texte_court,
								p_factures.date_debut, 
								p_factures.date_fin, 
								p_factures.type_facture, 
								p_factures.date_facture, 
								CASE WHEN p_factures.date_expedition_1 &lt; '20991231' THEN p_factures.date_expedition_1 ELSE null END, 
								CASE WHEN p_factures.date_vente &lt; '20991231' THEN p_factures.date_vente ELSE null END, 
								p_factures.montant_facture_1_c, 
								p_factures.montant_facture_1_h, 
								p_factures.montant_facture_1_c + p_factures.montant_facture_1_h, 
								p_factures.montant_comptabilise_1_c + COALESCE(p_factures.montant_comptabilise_budget_global_c,0), 
								p_factures.montant_comptabilise_1_h, 
								p_factures.montant_regle_1_c, 
								p_factures.montant_regle_1_h, 
								p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c,
								p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h,
								p_factures.montant_comptabilise_budget_global_c
							FROM activite.p_factures 
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							LEFT JOIN activite.t_tiers_payant ON(p_factures.tiers_payant_1_id = t_tiers_payant.oid)
							WHERE p_factures.no_sejour = '[SEJOUR]' AND type_facture &lt;&gt; 'E' AND
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(p_factures.montant_facture_1_c &lt;&gt; 0 OR 
								p_factures.montant_facture_1_h &lt;&gt; 0 OR 
								p_factures.montant_regle_1_h &lt;&gt; 0 OR 
								p_factures.montant_regle_1_h &lt;&gt; 0 OR 
								COALESCE(p_factures.montant_comptabilise_budget_global_c,0) &lt;&gt; 0)


						UNION ALL

						SELECT 
								p_factures.date_debut::text || p_factures.date_facture::text || rpad(p_factures.no_facture,20,' ') || '2',
								'2',
								'', 
								t_tiers_payant.code || ' ' || t_tiers_payant.texte_court,
								p_factures.date_debut, 
								p_factures.date_fin, 
								p_factures.type_facture, 
								p_factures.date_facture, 
								CASE WHEN p_factures.date_expedition_2 &lt; '20991231' THEN p_factures.date_expedition_2 ELSE null END, 
								CASE WHEN p_factures.date_vente &lt; '20991231' THEN p_factures.date_vente ELSE null END, 
								p_factures.montant_facture_2_c, 
								p_factures.montant_facture_2_h, 
								p_factures.montant_facture_2_c + p_factures.montant_facture_2_h, 
								p_factures.montant_comptabilise_2_c, 
								p_factures.montant_comptabilise_2_h, 
								p_factures.montant_regle_2_c, 
								p_factures.montant_regle_2_h, 
								p_factures.montant_comptabilise_2_c - p_factures.montant_regle_2_c,
								p_factures.montant_comptabilise_2_h - p_factures.montant_regle_2_h,
								0
							FROM activite.p_factures 
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							LEFT JOIN activite.t_tiers_payant ON(p_factures.tiers_payant_2_id = t_tiers_payant.oid)
							WHERE p_factures.no_sejour = '[SEJOUR]' AND type_facture &lt;&gt; 'E' AND
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(p_factures.montant_facture_2_c &lt;&gt; 0 OR 
								p_factures.montant_facture_2_h &lt;&gt; 0 OR 
								p_factures.montant_regle_2_h &lt;&gt; 0 OR 
								p_factures.montant_regle_2_h &lt;&gt; 0)


						UNION ALL

						SELECT 
								p_factures.date_debut::text || p_factures.date_facture::text || rpad(p_factures.no_facture,20,' ') || '22',
								'2',
								'', 
								t_tiers_payant.code || ' ' || t_tiers_payant.texte_court,
								p_factures.date_debut, 
								p_factures.date_fin, 
								p_factures.type_facture, 
								p_factures.date_facture, 
								CASE WHEN p_factures.date_expedition_22 &lt; '20991231' THEN p_factures.date_expedition_22 ELSE null END, 
								CASE WHEN p_factures.date_vente &lt; '20991231' THEN p_factures.date_vente ELSE null END, 
								p_factures.montant_facture_22_c, 
								p_factures.montant_facture_22_h, 
								p_factures.montant_facture_22_c + p_factures.montant_facture_22_h, 
								p_factures.montant_comptabilise_22_c, 
								p_factures.montant_comptabilise_22_h, 
								p_factures.montant_regle_22_c, 
								p_factures.montant_regle_22_h, 
								p_factures.montant_comptabilise_22_c - p_factures.montant_regle_22_c,
								p_factures.montant_comptabilise_22_h - p_factures.montant_regle_22_h,
								0
							FROM activite.p_factures 
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							LEFT JOIN activite.t_tiers_payant ON(p_factures.tiers_payant_22_id = t_tiers_payant.oid)
							WHERE p_factures.no_sejour = '[SEJOUR]' AND
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(p_factures.montant_facture_22_c &lt;&gt; 0 OR 
								p_factures.montant_facture_22_h &lt;&gt; 0 OR 
								p_factures.montant_regle_22_h &lt;&gt; 0 OR 
								p_factures.montant_regle_22_h &lt;&gt; 0)


						UNION ALL

						SELECT 
								p_factures.date_debut::text || p_factures.date_facture::text || rpad(p_factures.no_facture,20,' ') || '3',
								'2',
								'', 
								t_tiers_payant.code || ' ' || t_tiers_payant.texte_court,
								p_factures.date_debut, 
								p_factures.date_fin, 
								p_factures.type_facture, 
								p_factures.date_facture, 
								CASE WHEN p_factures.date_expedition_0 &lt; '20991231' THEN p_factures.date_expedition_0 ELSE null END, 
								CASE WHEN p_factures.date_vente &lt; '20991231' THEN p_factures.date_vente ELSE null END, 
								p_factures.montant_facture_0_c, 
								p_factures.montant_facture_0_h, 
								p_factures.montant_facture_0_c + p_factures.montant_facture_0_h, 
								p_factures.montant_comptabilise_0_c, 
								p_factures.montant_comptabilise_0_h, 
								p_factures.montant_regle_0_c, 
								p_factures.montant_regle_0_h, 
								p_factures.montant_comptabilise_0_c - p_factures.montant_regle_0_c,
								p_factures.montant_comptabilise_0_h - p_factures.montant_regle_0_h,
								0
							FROM activite.p_factures 
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							LEFT JOIN activite.t_tiers_payant ON(p_factures.tiers_payant_0_id = t_tiers_payant.oid)
							WHERE p_factures.no_sejour = '[SEJOUR]' AND type_facture &lt;&gt; 'E' AND
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(p_factures.montant_facture_0_c &lt;&gt; 0 OR 
								p_factures.montant_facture_0_h &lt;&gt; 0 OR 
								p_factures.montant_regle_0_h &lt;&gt; 0 OR 
								p_factures.montant_regle_0_h &lt;&gt; 0)

						UNION ALL

						SELECT 
								p_factures.date_debut::text || p_factures.date_facture::text || rpad(p_factures.no_facture,20,' ') || '9',
								'3',
								'', 
								'',
								null, 
								null, 
								'', 
								null, 
								null, 
								null,
								0,
								0,
								0,
								0,
								0,
								0,
								0,
								0,
								0,
								0
							FROM activite.p_factures  
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''							
							WHERE p_factures.no_sejour = '[SEJOUR]' AND type_facture &lt;&gt; 'E' AND
								p_factures.no_facture NOT LIKE '%.DMA'
							
						UNION ALL
							
						SELECT 
								chr(255),
								'1',
								'Total séjour', 
								'',
								null, 
								null, 
								'', 
								null, 
								null, 
								null,
								SUM(p_factures.montant_facture_c), 
								SUM(p_factures.montant_facture_h), 
								SUM(p_factures.montant_facture_c + p_factures.montant_facture_h), 
								SUM(p_factures.montant_comptabilise_c) + SUM(COALESCE(p_factures.montant_comptabilise_budget_global_c,0)), 
								SUM(p_factures.montant_comptabilise_h), 
								SUM(p_factures.montant_regle_c), 
								SUM(p_factures.montant_regle_h), 
								SUM(p_factures.montant_comptabilise_c - p_factures.montant_regle_c),
								SUM(p_factures.montant_comptabilise_h - p_factures.montant_regle_h),
								SUM(p_factures.montant_comptabilise_budget_global_c)
							FROM activite.p_factures
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							WHERE p_factures.no_sejour = '[SEJOUR]' AND type_facture &lt;&gt; 'E' AND
								p_factures.no_facture NOT LIKE '%.DMA'
							
						UNION ALL
							
						SELECT 
								chr(255) || '1',
								'2',
								'', 
								t_tiers_payant.code || ' ' || t_tiers_payant.texte_court,
								null, 
								null, 
								'', 
								null, 
								null, 
								null,
								SUM(p_factures.montant_facture_1_c), 
								SUM(p_factures.montant_facture_1_h), 
								SUM(p_factures.montant_facture_1_c + p_factures.montant_facture_1_h), 
								SUM(p_factures.montant_comptabilise_1_c) + SUM(COALESCE(p_factures.montant_comptabilise_budget_global_c,0)), 
								SUM(p_factures.montant_comptabilise_1_h), 
								SUM(p_factures.montant_regle_1_c), 
								SUM(p_factures.montant_regle_1_h), 
								SUM(p_factures.montant_comptabilise_1_c - p_factures.montant_regle_1_c),
								SUM(p_factures.montant_comptabilise_1_h - p_factures.montant_regle_1_h),
								SUM(p_factures.montant_comptabilise_budget_global_c)
							FROM activite.p_factures 
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							LEFT JOIN activite.t_tiers_payant ON(p_factures.tiers_payant_1_id = t_tiers_payant.oid)    
							WHERE p_factures.no_sejour = '[SEJOUR]' AND type_facture &lt;&gt; 'E' AND
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(p_factures.montant_facture_1_c &lt;&gt; 0 OR 
								p_factures.montant_facture_1_h &lt;&gt; 0 OR 
								p_factures.montant_regle_1_h &lt;&gt; 0 OR 
								p_factures.montant_regle_1_h &lt;&gt; 0 OR 
								COALESCE(p_factures.montant_comptabilise_budget_global_c,0) &lt;&gt; 0)
							GROUP BY 4
						
						UNION ALL
							
						SELECT 
								chr(255) || '2',
								'2',
								'', 
								t_tiers_payant.code || ' ' || t_tiers_payant.texte_court,
								null, 
								null, 
								'', 
								null, 
								null, 
								null,
								SUM(p_factures.montant_facture_2_c), 
								SUM(p_factures.montant_facture_2_h), 
								SUM(p_factures.montant_facture_2_c + p_factures.montant_facture_2_h), 
								SUM(p_factures.montant_comptabilise_2_c), 
								SUM(p_factures.montant_comptabilise_2_h), 
								SUM(p_factures.montant_regle_2_c), 
								SUM(p_factures.montant_regle_2_h), 
								SUM(p_factures.montant_comptabilise_2_c - p_factures.montant_regle_2_c),
								SUM(p_factures.montant_comptabilise_2_h - p_factures.montant_regle_2_h),
								0
							FROM activite.p_factures 
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							LEFT JOIN activite.t_tiers_payant ON(p_factures.tiers_payant_2_id = t_tiers_payant.oid)    
							WHERE p_factures.no_sejour = '[SEJOUR]' AND
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(p_factures.montant_facture_2_c &lt;&gt; 0 OR 
								p_factures.montant_facture_2_h &lt;&gt; 0 OR 
								p_factures.montant_regle_2_h &lt;&gt; 0 OR 
								p_factures.montant_regle_2_h &lt;&gt; 0)
							GROUP BY 4
							
						UNION ALL
							
						SELECT 
								chr(255) || '22',
								'2',
								'', 
								t_tiers_payant.code || ' ' || t_tiers_payant.texte_court,
								null, 
								null, 
								'', 
								null, 
								null, 
								null,
								SUM(p_factures.montant_facture_22_c), 
								SUM(p_factures.montant_facture_22_h), 
								SUM(p_factures.montant_facture_22_c + p_factures.montant_facture_22_h), 
								SUM(p_factures.montant_comptabilise_22_c), 
								SUM(p_factures.montant_comptabilise_22_h), 
								SUM(p_factures.montant_regle_22_c), 
								SUM(p_factures.montant_regle_22_h), 
								SUM(p_factures.montant_comptabilise_22_c - p_factures.montant_regle_22_c),
								SUM(p_factures.montant_comptabilise_22_h - p_factures.montant_regle_22_h),
								0
							FROM activite.p_factures 
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							LEFT JOIN activite.t_tiers_payant ON(p_factures.tiers_payant_22_id = t_tiers_payant.oid)    
							WHERE p_factures.no_sejour = '[SEJOUR]' AND type_facture &lt;&gt; 'E' AND
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(p_factures.montant_facture_22_c &lt;&gt; 0 OR 
								p_factures.montant_facture_22_h &lt;&gt; 0 OR 
								p_factures.montant_regle_22_h &lt;&gt; 0 OR 
								p_factures.montant_regle_22_h &lt;&gt; 0)
							GROUP BY 4
							
						UNION ALL
							
						SELECT 
								chr(255) || '3',
								'2',
								'', 
								t_tiers_payant.code || ' ' || t_tiers_payant.texte_court,
								null, 
								null, 
								'', 
								null, 
								null, 
								null,
								SUM(p_factures.montant_facture_0_c), 
								SUM(p_factures.montant_facture_0_h), 
								SUM(p_factures.montant_facture_0_c + p_factures.montant_facture_0_h), 
								SUM(p_factures.montant_comptabilise_0_c), 
								SUM(p_factures.montant_comptabilise_0_h), 
								SUM(p_factures.montant_regle_0_c), 
								SUM(p_factures.montant_regle_0_h), 
								SUM(p_factures.montant_comptabilise_0_c - p_factures.montant_regle_0_c),
								SUM(p_factures.montant_comptabilise_0_h - p_factures.montant_regle_0_h),
								0
							FROM activite.p_factures 
							JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
							LEFT JOIN activite.t_tiers_payant ON(p_factures.tiers_payant_0_id = t_tiers_payant.oid)    
							WHERE p_factures.no_sejour = '[SEJOUR]' AND type_facture &lt;&gt; 'E' AND
								p_factures.no_facture NOT LIKE '%.DMA' AND
								(p_factures.montant_facture_0_c &lt;&gt; 0 OR 
								p_factures.montant_facture_0_h &lt;&gt; 0 OR 
								p_factures.montant_regle_0_h &lt;&gt; 0 OR 
								p_factures.montant_regle_0_h &lt;&gt; 0)
							GROUP BY 4
						
						UNION ALL
							
						SELECT 
							chr(255) || '81',
							'2',
							'', 
							'',
							null, 
							null, 
							'', 
							null, 
							null, 
							null,
							0, 
							0, 
							0, 
							0, 
							0, 
							0, 
							0, 
							0,
							0,
							0
						FROM activite.p_factures 
						JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
						WHERE p_factures.no_sejour = '[SEJOUR]' AND
							p_factures.no_facture NOT LIKE '%.DMA' AND
							(p_factures.montant_encours_c &lt;&gt; 0 OR p_factures.montant_encours_h &lt;&gt; 0)
						GROUP BY 4

						UNION ALL
							
						SELECT 
							chr(255) || '82',
							'1',
							'Total Valorisé',
							'Séjour non facturé ' || CASE WHEN MAX(motif_non_facturation_id) &gt; 0 THEN '('||base.cti_group_concat(DISTINCT COALESCE(t_motif_non_facturation.texte,''))||')' ELSE '' END,
							MIN(p_factures.date_debut), 
							MAX(p_factures.date_fin), 
							'', 
							null, 
							null, 
							null,
							SUM(p_factures.montant_encours_c), 
							SUM(p_factures.montant_encours_h), 
							SUM(p_factures.montant_encours_c + p_factures.montant_encours_0_h), 
							0, 
							0, 
							0, 
							0, 
							0,
							0,
							0
						FROM activite.p_factures   
						JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
                        LEFT JOIN activite.t_motif_non_facturation ON motif_non_facturation_id = t_motif_non_facturation.oid AND motif_non_facturation_id &lt;&gt; 0
						WHERE p_factures.no_sejour = '[SEJOUR]' AND
							p_factures.no_facture NOT LIKE '%.DMA' AND
							(p_factures.montant_encours_c &lt;&gt; 0 OR p_factures.montant_encours_h &lt;&gt; 0)
						
						UNION ALL
						
						SELECT 
							chr(255) || '83' || p_factures.date_debut::text || rpad(p_factures.no_facture,20,' '),
							'2',
							p_factures.no_facture, 
							'',
							p_factures.date_debut, 
							p_factures.date_fin, 
							'', 
							null, 
							null, 
							null,
							p_factures.montant_encours_c, 
							p_factures.montant_encours_h, 
							p_factures.montant_encours_c + p_factures.montant_encours_h,
							0, 
							0, 
							0, 
							0, 
							0,
							0,
							0
						FROM activite.p_factures    
						JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
						WHERE p_factures.no_sejour = '[SEJOUR]' AND 
							p_factures.no_facture NOT LIKE '%.DMA' AND
							(
								p_factures.montant_encours_c IS DISTINCT FROM 0 OR
								p_factures.montant_encours_h IS DISTINCT FROM 0
							)



						ORDER BY 1">
				<FIELDS>
					<FIELD name="LIG"/>
					<FIELD name="LVL"/>
					<FIELD name="NFAC"/>
					<FIELD name="TP"/>
					<FIELD name="DDEB"/>
					<FIELD name="DFIN"/>
					<FIELD name="TYP"/>
					<FIELD name="DFAC"/>
					<FIELD name="DEXP"/>
					<FIELD name="DVEN"/>
					<FIELD name="MFC"/>
					<FIELD name="MFH"/>
					<FIELD name="MFT"/>
					<FIELD name="MCC"/>
					<FIELD name="MCH"/>
					<FIELD name="MRC"/>
					<FIELD name="MRH"/>
					<FIELD name="MSC"/>
					<FIELD name="MSH"/>
					<FIELD name="MBG"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY label="Lignes factures clinique">
			<SQL>
				<select><![CDATA[
						
						DROP SEQUENCE IF EXISTS w_factures_lignes_c_sequence;
						CREATE TEMP SEQUENCE w_factures_lignes_c_sequence;

						DROP TABLE IF EXISTS w_factures_lignes_c;
						CREATE TEMP TABLE w_factures_lignes_c AS
						SELECT nextval('w_factures_lignes_c_sequence'::regclass) AS sequence,
							0::bigint AS rupture,
							date_facture::text || 
								type_facture::text ||
								no_facture::text ||
								prestation_texte::text ||
								rubrique_facturation_texte::text ||
								compte_produit_texte::text ||
								prix_unitaire::text ||
								coefficient_mco::text ||
								taux_1::text ||
								taux_2::text ||
								taux_0::text
										AS key,
							subview.* FROM
						(
                            SELECT
                                p_factures.date_facture,
								CASE p_factures.type_facture WHEN '0' THEN 9 WHEN 'X' THEN 5 WHEN '1' THEN 6 ELSE '1' END AS type_facture,
								p_factures.no_facture_reference,
								p_factures.no_facture, 
								prestation_code || ' ' || prestation_texte AS prestation_texte, 
								p_factures_lignes_c.rubrique_facturation_id,
                                p_factures_lignes_c.prestation_id,
								p_factures_lignes_c.lpp_id,
								p_factures_lignes_c.ucd_id,
								p_factures_lignes_c.compte_produit_id,
								COALESCE(
									t_lpp_c.code || ' ' || t_lpp_c.texte,
									t_ucd_c.code || ' ' || t_ucd_c.texte,
									rubrique_facturation_code || ' ' || rubrique_facturation_texte) AS rubrique_facturation_texte, 
								compte_produit_code || ' ' ||compte_produit_texte AS compte_produit_texte, 
								prix_unitaire,
								coefficient_mco,
								taux_1,
								taux_2,
								taux_22,
								taux_0,
								p_factures_lignes_c.date_debut, 
								p_factures_lignes_c.date_fin, 
								nb_prestation, 
								nb_prestation * coefficient AS coefficient,
								montant_facture,
								montant_facture_0,
								montant_facture_1, 
								montant_facture_2,
								montant_facture_22,
								COALESCE(montant_encours,0) AS montant_encours
                            FROM activite.p_factures_lignes_c
                                JOIN activite.p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
                                JOIN activite.t_rubrique_facture_c ON rubrique_facture_id = t_rubrique_facture_c.oid  
								LEFT JOIN activite.t_lpp_c ON p_factures_lignes_c.lpp_id = t_lpp_c.oid AND p_factures_lignes_c.lpp_id <> 0
								LEFT JOIN activite.t_ucd_c ON p_factures_lignes_c.ucd_id = t_ucd_c.oid AND p_factures_lignes_c.ucd_id <> 0
							WHERE no_sejour = '[SEJOUR]' AND 
								origine_facturation_id = 1 AND
								prestation_code <> '0ETS' AND
								p_factures_lignes_c.no_facture NOT LIKE '%.DMA'
							ORDER BY 1,2,3,4,5,6,7,8,9,10,11,12
						) subview;



						UPDATE w_factures_lignes_c 
						SET rupture = sequence_min
						FROM (
						SELECT key, MIN(sequence) AS sequence_min FROM w_factures_lignes_c GROUP BY 1
						) subview 
						WHERE w_factures_lignes_c.key = subview.key;



						SELECT 
							no_facture_reference,
							2,
							MAX(date_facture),
							MAX(type_facture),
							MAX(no_facture), 
							MIN(date_debut), 
							MAX(date_fin), 
							SUM(nb_prestation), 
							MAX(prestation_texte), 
							MAX(rubrique_facturation_texte), 
							MAX(compte_produit_texte), 
							MAX(prix_unitaire),
							SUM(coefficient),
							MAX(coefficient_mco),
							MAX(taux_1),
							MAX(taux_2),
							MAX(taux_22),
							MAX(taux_0),
							SUM(montant_facture),
							SUM(montant_facture_0),
							SUM(montant_facture_1), 
							SUM(montant_facture_2), 
							SUM(montant_facture_22), 
							SUM(montant_encours)
						FROM w_factures_lignes_c
						GROUP BY no_facture_reference, rupture

						UNION
						
						SELECT 
							no_facture_reference,
							1,
							MAX(date_facture),
							0,
							no_facture_reference, 
							MIN(date_debut), 
							MAX(date_fin), 
							SUM(nb_prestation), 
							MAX(prestation_texte), 
							MAX(rubrique_facturation_texte), 
							MAX(compte_produit_texte), 
							MAX(prix_unitaire),
							SUM(coefficient),
							MAX(coefficient_mco),
							MAX(taux_1),
							MAX(taux_2),
							MAX(taux_22),
							MAX(taux_0),
							SUM(montant_facture),
							SUM(montant_facture_0),
							SUM(montant_facture_1), 
							SUM(montant_facture_2), 
							SUM(montant_facture_22), 
							SUM(montant_encours)
						FROM w_factures_lignes_c
						GROUP BY no_facture_reference, rubrique_facturation_id, prestation_id, lpp_id, ucd_id
						
						UNION
						
						SELECT 
							no_facture_reference,
							3,
							MAX(date_facture),
							0,
							no_facture_reference, 
							MIN(date_debut), 
							MAX(date_fin), 
							0,
							'', 
							'', 
							MAX(compte_produit_texte), 
							0,
							0,
							0,
							0,
							0,
							0,
							0,
							SUM(montant_facture),
							SUM(montant_facture_0),
							SUM(montant_facture_1), 
							SUM(montant_facture_2), 
							SUM(montant_facture_22), 
							SUM(montant_encours)
						FROM w_factures_lignes_c
						GROUP BY no_facture_reference, compte_produit_id
						
						UNION

						SELECT 
							'TOTAL',
							9,
							MAX(date_facture),
							0,
							'', 
							NULL, 
							NULL, 
							NULL, 
							NULL, 
							NULL, 
							NULL, 
							NULL,
							NULL,
							NULL,
							NULL,
							NULL,
							NULL,
							NULL,
							SUM(montant_facture),
							SUM(montant_facture_0),
							SUM(montant_facture_1), 
							SUM(montant_facture_2), 
							SUM(montant_facture_22), 
							SUM(montant_encours)
						FROM w_factures_lignes_c
						
						
						ORDER BY 1,2,3,4,5,6,7,8,9;
						
						
						
				]]></select>
				<FIELDS>
					<FIELD name="RFAC"/>
					<FIELD name="LVL"/>
					<FIELD name="DFAC"/>
					<FIELD name="SFAC"/>
					<FIELD name="NFAC"/>
					<FIELD name="DDEB"/>
					<FIELD name="DFIN"/>
					<FIELD name="NB"/>
					<FIELD name="PRE"/>
					<FIELD name="RUB"/>
                    <FIELD name="CPT"/>
					<FIELD name="PU"/>
					<FIELD name="COE"/>
					<FIELD name="COEM"/>
					<FIELD name="TAU1"/>
					<FIELD name="TAU2"/>
					<FIELD name="TAU22"/>
					<FIELD name="TAU0"/>
					<FIELD name="MT"/>
					<FIELD name="MT0"/>
					<FIELD name="MT1"/>
					<FIELD name="MT2"/>
					<FIELD name="MT22"/>
					<FIELD name="ENC"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY label="Lignes factures honoraires">
			<SQL select="WITH subview AS (
							SELECT
								p_factures.no_facture_reference,
								p_factures.no_facture, 
								p_factures_lignes_h.date_debut, 
								p_factures_lignes_h.date_fin, 
								t_prestations.code || ' ' || t_prestations.texte AS prestation_texte, 
								t_actes_c.code || ' ' || t_actes_c.texte AS acte_texte, 
								t_medecins_administratifs_c.nom_prenom || ' (' || t_medecins_administratifs_c.adm_code || ')' AS medecin_texte, 
								prix_unitaire,
								nb_prestation,
								coefficient,
								nb_prestation * coefficient AS nb_coef,
								taux_1,
								montant_facture,
								montant_facture_0,
								montant_facture_1, 
								montant_facture_2,
								montant_facture_22,
								montant_depassement ,
								COALESCE(montant_facture_actes_inclus_dans_sejour,0) AS montant_facture_actes_inclus_dans_sejour,
								COALESCE(montant_non_facture,0) AS montant_non_facture,
                                CASE WHEN nb_prestation >= 0 THEN '1' ELSE '-1' END,
                                protocole_id
							FROM activite.p_factures_lignes_h
							JOIN activite.p_factures ON p_factures_lignes_h.no_facture = p_factures.no_facture
							JOIN activite.t_medecins_administratifs_c ON p_factures_lignes_h.medecin_facture_id = t_medecins_administratifs_c.oid
							JOIN activite.t_prestations ON p_factures_lignes_h.prestation_id = t_prestations.oid
							JOIN activite.t_actes_c ON p_factures_lignes_h.acte_id = t_actes_c.oid
							WHERE no_sejour = '[SEJOUR]'
							)

						SELECT 
							no_facture_reference,
							2,
							no_facture, 
							date_debut, 
							date_fin, 
							prestation_texte, 
							acte_texte, 
							medecin_texte,
							prix_unitaire,
							nb_prestation,
							coefficient,
							nb_coef,
							taux_1,
							montant_facture,
							montant_facture_0,
							montant_facture_1, 
							montant_facture_2 + montant_facture_22,
							montant_depassement ,
							COALESCE(montant_facture_actes_inclus_dans_sejour,0),
							montant_non_facture,
                            CASE WHEN nb_prestation >= 0 THEN '1' ELSE '-1' END::bigint
						FROM subview

						UNION ALL

						SELECT 
							no_facture_reference,
							1,
							no_facture_reference, 
							MIN(date_debut), 
							MAX(date_fin), 
							prestation_texte, 
							acte_texte, 
							medecin_texte,
							prix_unitaire,
							SUM(nb_prestation),
							0,
							SUM(nb_prestation * coefficient),
							taux_1,
							SUM(montant_facture),
							SUM(montant_facture_0),
							SUM(montant_facture_1), 
							SUM(montant_facture_2 + montant_facture_22),
							SUM(montant_depassement) ,
							SUM(COALESCE(montant_facture_actes_inclus_dans_sejour,0)),
							SUM(montant_non_facture),
                            base.cti_sum_distinct_on(DISTINCT array[subview.no_facture::text || '|' || protocole_id::text,CASE WHEN nb_prestation >= 0 THEN '1' ELSE '-1' END])::bigint
						FROM subview
						GROUP BY 1,6,7,8,9,13
						
						UNION ALL

						SELECT 
							'',
							9,
							'', 
							NULL, 
							NULL, 
							'',
							'',
							'',
							0,
							SUM(nb_prestation),
							0,
							0,
							0,
							SUM(montant_facture),
							SUM(montant_facture_0),
							SUM(montant_facture_1), 
							SUM(montant_facture_2 + montant_facture_22),
							SUM(montant_depassement) ,
							SUM(COALESCE(montant_facture_actes_inclus_dans_sejour,0)),
							SUM(montant_non_facture),
                            base.cti_sum_distinct_on(DISTINCT array[subview.no_facture::text || '|' || protocole_id::text,CASE WHEN nb_prestation >= 0 THEN '1' ELSE '-1' END])::bigint
						FROM subview

						ORDER BY 1,2,3,4">
				<FIELDS>
					<FIELD name="RFAC"/>
					<FIELD name="LVL"/>
					<FIELD name="NFAC"/>
					<FIELD name="DDEB"/>
					<FIELD name="DFIN"/>
					<FIELD name="PRE"/>
					<FIELD name="ACT"/>
					<FIELD name="EXE"/>
					<FIELD name="PU"/>
					<FIELD name="NB"/>
					<FIELD name="COE"/>
					<FIELD name="NBCOE"/>
					<FIELD name="TAU1"/>
					<FIELD name="MT"/>
					<FIELD name="MT0"/>
					<FIELD name="MT1"/>
					<FIELD name="MT2"/>
					<FIELD name="MDP"/>
					<FIELD name="MTI"/>
					<FIELD name="MTNF"/>
					<FIELD name="PROTO"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY label="Lignes comptabilisées clinique">
			<SQL>
				<select><![CDATA[
				
				
						DROP SEQUENCE IF EXISTS w_factures_lignes_c_sequence;
						CREATE TEMP SEQUENCE w_factures_lignes_c_sequence;

						DROP TABLE IF EXISTS w_factures_lignes_c;
						CREATE TEMP TABLE w_factures_lignes_c AS
						SELECT nextval('w_factures_lignes_c_sequence'::regclass) AS sequence,
							0::bigint AS rupture,
							date_facture::text || 
								type_facture::text ||
								no_facture::text ||
								prestation_texte::text ||
								rubrique_facturation_texte::text ||
								prix_unitaire::text ||
								coefficient_mco::text ||
								taux_1::text ||
								taux_2::text ||
								taux_0::text
										AS key,
							subview.* FROM
						(
                            SELECT 
								p_factures.date_facture,
								CASE p_factures.type_facture WHEN '0' THEN 9 WHEN 'X' THEN 5 WHEN '1' THEN 6 ELSE '1' END AS type_facture,
								p_factures.no_facture_reference,
								p_factures.no_facture, 
								prestation_code || ' ' || prestation_texte AS prestation_texte, 
								p_factures_lignes_c.rubrique_facturation_id,
								rubrique_facturation_code || ' ' || rubrique_facturation_texte AS rubrique_facturation_texte, 
								prix_unitaire,
								coefficient_mco,
								taux_1,
								taux_2,
								taux_22,
								taux_0,
								p_factures_lignes_c.date_debut, 
								p_factures_lignes_c.date_fin, 
								nb_prestation, 
								nb_prestation * coefficient AS coefficient,
								montant_comptabilise + COALESCE(montant_comptabilise_budget_global_1,0) AS montant_comptabilise_total,
								montant_comptabilise,
								montant_comptabilise_0,
								montant_comptabilise_1, 
								montant_comptabilise_2,
								montant_comptabilise_22,
								COALESCE(montant_comptabilise_budget_global_1,0) AS montant_comptabilise_budget_global_1
								
							FROM activite.p_factures_lignes_c
                                JOIN activite.p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
                                JOIN activite.t_rubrique_facture_c ON rubrique_facture_id = t_rubrique_facture_c.oid      
							WHERE no_sejour = '[SEJOUR]' AND 
								(COALESCE(montant_encours,0) = 0) AND
								origine_facturation_id IN (1,2,4) AND
								(montant_comptabilise <> 0 OR COALESCE(montant_comptabilise_budget_global_1,0) <> 0)
						) subview;



						UPDATE w_factures_lignes_c 
						SET rupture = sequence_min
						FROM (
						SELECT key, MIN(sequence) AS sequence_min FROM w_factures_lignes_c GROUP BY 1
						) subview 
						WHERE w_factures_lignes_c.key = subview.key;

						
						SELECT 
							no_facture_reference,
							2,
							MAX(date_facture),
							MAX(type_facture),
							MAX(no_facture), 
							MIN(date_debut), 
							MAX(date_fin), 
							SUM(nb_prestation), 
							MAX(prestation_texte), 
							MAX(rubrique_facturation_texte), 
							MAX(prix_unitaire),
							SUM(coefficient),
							MAX(coefficient_mco),
							MAX(taux_1),
							MAX(taux_2),
							MAX(taux_22),
							MAX(taux_0),
							SUM(montant_comptabilise_total),
							SUM(montant_comptabilise),
							SUM(montant_comptabilise_0),
							SUM(montant_comptabilise_1), 
							SUM(montant_comptabilise_2), 
							SUM(montant_comptabilise_22), 
							SUM(montant_comptabilise_budget_global_1)
						FROM w_factures_lignes_c
						GROUP BY no_facture_reference, rupture

						UNION
						
						SELECT 
							no_facture_reference,
							1,
							MAX(date_facture),
							0,
							no_facture_reference, 
							MIN(date_debut), 
							MAX(date_fin), 
							SUM(nb_prestation), 
							MAX(prestation_texte), 
							MAX(rubrique_facturation_texte), 
							MAX(prix_unitaire),
							SUM(coefficient),
							MAX(coefficient_mco),
							MAX(taux_1),
							MAX(taux_2),
							MAX(taux_22),
							MAX(taux_0),
							SUM(montant_comptabilise_total),
							SUM(montant_comptabilise),
							SUM(montant_comptabilise_0),
							SUM(montant_comptabilise_1), 
							SUM(montant_comptabilise_2), 
							SUM(montant_comptabilise_22), 
							SUM(montant_comptabilise_budget_global_1)
						FROM w_factures_lignes_c
						GROUP BY no_facture_reference, rubrique_facturation_id
						
						UNION

						SELECT 
							'TOTAL',
							9,
							MAX(date_facture),
							0,
							'', 
							NULL, 
							NULL, 
							NULL, 
							NULL, 
							NULL, 
							NULL,
							NULL,
							NULL,
							NULL,
							NULL,
							NULL,
							NULL,
							SUM(montant_comptabilise_total),
							SUM(montant_comptabilise),
							SUM(montant_comptabilise_0),
							SUM(montant_comptabilise_1), 
							SUM(montant_comptabilise_2), 
							SUM(montant_comptabilise_22), 
							SUM(montant_comptabilise_budget_global_1)
						FROM w_factures_lignes_c
						
						
						ORDER BY 1,2,3,4,5,6,7,8,9;

						

				]]></select>
				<FIELDS>
					<FIELD name="RFAC"/>
					<FIELD name="LVL"/>
					<FIELD name="DFAC"/>
					<FIELD name="SFAC"/>
					<FIELD name="NFAC"/>
					<FIELD name="DDEB"/>
					<FIELD name="DFIN"/>
					<FIELD name="NB"/>
					<FIELD name="PRE"/>
					<FIELD name="RUB"/>
					<FIELD name="PU"/>
					<FIELD name="COE"/>
					<FIELD name="COEM"/>
					<FIELD name="TAU1"/>
					<FIELD name="TAU2"/>
					<FIELD name="TAU22"/>
					<FIELD name="TAU0"/>
					<FIELD name="MTT"/>
					<FIELD name="MT"/>
					<FIELD name="MT0"/>
					<FIELD name="MT1"/>
					<FIELD name="MT2"/>
					<FIELD name="MT22"/>
					<FIELD name="MT1BG"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY label="PMSI">
			<SQL enabled="= floatval('[VIEW.NB_RSS]') != 0">
				<select><![CDATA[
				
					SELECT
						p_sejour_pmsi.pmsi_type,
						v_rss_1.rss_id,
						v_rss_1.no_rss,
						v_rss_1.no_rss,
						date_entree,
						date_sortie,
						v_rss_1.en_cours_texte_tres_court,
						diagnostic_principal_code || ' ' || diagnostic_principal_texte,
						acte_principal_code,
						prestation_principale_code,
						ghm_code || ' ' || ghm_texte
					FROM pmsi.v_rss_1
						JOIN pmsi.p_identites ON v_rss_1.rss_id = p_identites.rss_id
						JOIN activite.p_sejour_pmsi ON p_sejour_pmsi.rss_id = v_rss_1.rss_id 
					WHERE pmsi_type <> '' AND 
						p_sejour_pmsi.no_sejour = '[SEJOUR]'

						

				]]></select>
				<FIELDS>
					<FIELD name="PMSITYP"/>
					<FIELD name="PMSIOID"/>
					<FIELD name="PMSIRSS"/>
					<FIELD name="PMSINO"/>
					<FIELD name="DDEB"/>
					<FIELD name="DFIN"/>
					<FIELD name="ENC"/>
					<FIELD name="DGP"/>
					<FIELD name="ACP"/>
					<FIELD name="PRP"/>
					<FIELD name="GHM"/>
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY label="Controles expert">
			<SQL>
				<select><![CDATA[
					SELECT 
						v_expert_sejour_controle_1.date_signalement,
						v_expert_sejour_controle_1.expert_controle_code,
						v_expert_sejour_controle_1.expert_controle_texte,
						v_expert_sejour_controle_1.expert_controle_gravite_texte,
						v_expert_sejour_controle_1.code_justificatif
					FROM activite.v_expert_sejour_controle_1 
					WHERE v_expert_sejour_controle_1.no_sejour = '[SEJOUR]'
					ORDER BY 1 DESC
						
				]]></select>
				<FIELDS>
					<FIELD name="CXDS"/>
					<FIELD name="CXC"/>
					<FIELD name="CXT"/>
					<FIELD name="CXG"/>
					<FIELD name="CXJ"/>
				</FIELDS>
			</SQL>
		</QUERY>
        <QUERY label="Justificatif valorisation">
			<SQL>
				<select><![CDATA[
                    
                    
                    DROP SEQUENCE IF EXISTS w_sequence;
                    CREATE TEMP SEQUENCE w_sequence;

					WITH subview AS (
                        SELECT nextval('w_sequence'::regclass) AS w_sequence,
                            *
						FROM (
						SELECT *
                        FROM activite.v_facture_ligne_valorise_c_1
                        WHERE no_sejour = '[SEJOUR]'
						ORDER BY no_facture, type_ligne_id, prestation_code, date_debut
                        ) subview
					)
                        
                    
                    SELECT no_facture,
                            0,
                            0,
                            1,
                            'Facture ' || no_facture || ' (du ' || to_char(date_debut_facture,'DD/MM/YYYY') || CASE WHEN date_fin_facture < '20991231' THEN  ' au ' || to_char(date_fin_facture,'DD/MM/YYYY') ELSE ' - présent' END || ')',
                            NULL::numeric,
                            NULL::numeric,
                            NULL::numeric,
                            SUM(montant_encours),
                            NULL::date,
                            NULL::date,
                            36
                        FROM subview
                        GROUP BY 1,2,3,4,5
                        UNION ALL
                        SELECT no_facture,
                            type_ligne_id,
                            w_sequence,
                            2,
                            prestation_code || ' ' || prestation_texte || 
								CASE 
									WHEN lpp_id = 0 AND ucd_id = 0 AND rubrique_facturation_code <> prestation_code THEN ' (rubrique ' || rubrique_facturation_code || ' ' || rubrique_facturation_texte || ')' 
									WHEN lpp_id <> 0 THEN ' (' || lpp_code || ' ' || lpp_texte || ')' 
									WHEN ucd_id <> 0 THEN ' (' || ucd_code || ' ' || ucd_texte || ')' 
								ELSE '' END,
                            nb_prestation, 
                            coefficient,
                            prix_unitaire,
                            montant_encours,
                            date_debut,
                            date_fin,
                            18
                        FROM subview
                        UNION ALL
                        SELECT no_facture,
                            type_ligne_id,
                            w_sequence,
                            3,
                            mode_calcul_texte,
                            NULL::numeric,
                            NULL::numeric,
                            NULL::numeric,
                            NULL::numeric,
                            NULL::date,
                            NULL::date,
                            18
                        FROM subview
                        UNION ALL
                        SELECT no_facture,
                            type_ligne_id,
                            w_sequence,
                            4,
                            subview_modes.texte,
                            NULL::numeric,
                            NULL::numeric,
                            NULL::numeric,
                            NULL::numeric,
                            NULL::date,
                            NULL::date,
                            18
                        FROM subview
                        JOIN 
                            (
                                SELECT '01-MSD12'::text AS code, '01-P.U. moyen GHS pour le médecin, le service et la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '02-MST12'::text AS code, '02-P.U. moyen GHS pour le médecin, le service et la tranche de durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '03-MSD12'::text AS code, '03-P.U. moyen GHS pour le médecin, le service et la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '04-MST12'::text AS code, '04-P.U. moyen GHS pour le médecin, le service et la tranche de durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '05-MSD12'::text AS code, '05-P.U. moyen GHS pour la spécialité du médecin, le service et la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION                                                    
                                SELECT '06-MST12'::text AS code, '06-P.U. moyen GHS pour la spécialité du médecin, le service et la tranche de durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION                                                     
                                SELECT '07-MXD12'::text AS code, '07-P.U. moyen GHS pour le médecin, quelque soit le service et la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION                                                    
                                SELECT '08-MXT12'::text AS code, '08-P.U. moyen GHS pour le médecin, quelque soit le service et la tranche de durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION                                                     
                                SELECT '09-MXD18'::text AS code, '09-P.U. moyen GHS pour le médecin, quelque soit le service et la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION                                                    
                                SELECT '10-MXT18'::text AS code, '10-P.U. moyen GHS pour le médecin, quelque soit le service et la tranche de durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION                                                     
                                SELECT '11-MXD12'::text AS code, '11-P.U. moyen GHS pour la spécialité du médecin, quelque soit le service et la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION                                                    
                                SELECT '12-MXT12'::text AS code, '12-P.U. moyen GHS pour la spécialité du médecin, quelque soit le service et la tranche de durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '13-XSD18'::text AS code, '18-P.U. moyen GHS quelque soit le médecin, pour le service et la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '21-MSX12'::text AS code, '21-P.U. moyen GHS pour le médecin, le service et quelque soit la la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '22-MSX18'::text AS code, '22-P.U. moyen GHS pour le médecin, le service et quelque soit la la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '51-MXX12'::text AS code, '51-P.U. moyen GHS pour le médecin quelque soit le service et quelque soit la la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '52-MXX18'::text AS code, '51-P.U. moyen GHS pour le médecin quelque soit le service et quelque soit la la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                                UNION
                                SELECT '61-XSX18'::text AS code, '61-P.U. moyen GHS quelque soit le médecin, pour le service et quelque soit la la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte

                                
                            ) subview_modes ON subview_modes.code < mode_calcul_code
                        WHERE type_ligne_code = 'GHSNG' AND 
                            mode_calcul_code <> '' 

                        ORDER BY 1,2,3,4,5
                   
						
				]]></select>
				<FIELDS>
					<FIELD name="FAC"/>
					<FIELD name="TYP"/>
					<FIELD name="PRE"/>
                    <FIELD name="LVL"/>
					<FIELD name="TXT"/>
					<FIELD name="NB"/>
                    <FIELD name="COE"/>
                    <FIELD name="PU"/>
                    <FIELD name="MT"/>
                    <FIELD name="DDEB"/>
                    <FIELD name="DFIN"/>
                    <FIELD name="ROWHEIGHT"/>
				</FIELDS>
			</SQL>
		</QUERY>
        
        <QUERY label="Justificatif En-cours">
			<SQL>
				<select><![CDATA[
                    
                    
                    DROP SEQUENCE IF EXISTS w_sequence;
                    CREATE TEMP SEQUENCE w_sequence;

                    WITH subview AS (
                        SELECT nextval('w_sequence'::regclass) AS w_sequence,
                            *
						FROM (
						SELECT *
                        FROM activite.v_facture_encours_ligne_valorise_c_1
                        WHERE no_sejour = '[SEJOUR]'
						ORDER BY no_facture, type_ligne_id, prestation_code, date_debut
                        ) subview
					)
                        
                    
                    SELECT 
                        date_encours,
                        no_facture,
                        0,
                        0,
                        1,
                        'Encours de ' || to_char(date_encours,'MM/YYYY') || 
                            CASE WHEN date_calcul < '20991231' THEN ' calculé le ' || to_char(date_calcul,'DD/MM/YYYY') ELSE '' END ||
                            '. Facture ' || no_facture || 
                            ' (du ' || to_char(date_debut_facture,'DD/MM/YYYY') || 
                            CASE WHEN date_fin_facture < '20991231' THEN  ' au ' || to_char(date_fin_facture,'DD/MM/YYYY') ELSE ' - présent' END || ')',
                        NULL::numeric,
                        NULL::numeric,
                        NULL::numeric,
                        SUM(montant_valorise_total),
                        SUM(montant_encours),
                        NULL::date,
                        NULL::date,
                        36
                    FROM subview
                    GROUP BY 1,2,3,4,5,6
                    UNION ALL
                    SELECT 
                        date_encours,
                        no_facture,
                        type_ligne_id,
                        w_sequence,
                        2,
                        prestation_code || ' ' || prestation_texte || 
							CASE 
								WHEN lpp_id = 0 AND ucd_id = 0 AND rubrique_facturation_code <> prestation_code THEN ' (rubrique ' || rubrique_facturation_code || ' ' || rubrique_facturation_texte || ')' 
								WHEN lpp_id <> 0 THEN ' (' || lpp_code || ' ' || lpp_texte || ')' 
								WHEN ucd_id <> 0 THEN ' (' || ucd_code || ' ' || ucd_texte || ')' 
							ELSE '' END,
						nb_prestation, 
                        coefficient,
                        prix_unitaire,
                        montant_valorise_total,
                        montant_encours,
                        date_debut,
                        date_fin,
                        18
                    FROM subview
                    UNION ALL
                    SELECT 
                        date_encours,
                        no_facture,
                        type_ligne_id,
                        w_sequence,
                        3,
                        mode_calcul_texte,
                        NULL::numeric,
                        NULL::numeric,
                        NULL::numeric,
                        NULL::numeric,
                        NULL::numeric,
                        NULL::date,
                        NULL::date,
                        18
                    FROM subview
                    UNION ALL
                    SELECT 
                        date_encours,
                        no_facture,
                        type_ligne_id,
                        w_sequence,
                        4,
                        subview_modes.texte,
                        NULL::numeric,
                        NULL::numeric,
                        NULL::numeric,
                        NULL::numeric,
                        NULL::numeric,
                        NULL::date,
                        NULL::date,
                        18
                    FROM subview
                    JOIN 
                        (
                            SELECT '01-MSD12'::text AS code, '01-P.U. moyen GHS pour le médecin, le service et la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '02-MST12'::text AS code, '02-P.U. moyen GHS pour le médecin, le service et la tranche de durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '03-MSD12'::text AS code, '03-P.U. moyen GHS pour le médecin, le service et la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '04-MST12'::text AS code, '04-P.U. moyen GHS pour le médecin, le service et la tranche de durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '05-MSD12'::text AS code, '05-P.U. moyen GHS pour la spécialité du médecin, le service et la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION                                                    
                            SELECT '06-MST12'::text AS code, '06-P.U. moyen GHS pour la spécialité du médecin, le service et la tranche de durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION                                                     
                            SELECT '07-MXD12'::text AS code, '07-P.U. moyen GHS pour le médecin, quelque soit le service et la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION                                                    
                            SELECT '08-MXT12'::text AS code, '08-P.U. moyen GHS pour le médecin, quelque soit le service et la tranche de durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION                                                     
                            SELECT '09-MXD18'::text AS code, '09-P.U. moyen GHS pour le médecin, quelque soit le service et la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION                                                    
                            SELECT '10-MXT18'::text AS code, '10-P.U. moyen GHS pour le médecin, quelque soit le service et la tranche de durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION                                                     
                            SELECT '11-MXD12'::text AS code, '11-P.U. moyen GHS pour la spécialité du médecin, quelque soit le service et la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION                                                    
                            SELECT '12-MXT12'::text AS code, '12-P.U. moyen GHS pour la spécialité du médecin, quelque soit le service et la tranche de durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '13-XSD18'::text AS code, '18-P.U. moyen GHS quelque soit le médecin, pour le service et la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '21-MSX12'::text AS code, '21-P.U. moyen GHS pour le médecin, le service et quelque soit la la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '22-MSX18'::text AS code, '22-P.U. moyen GHS pour le médecin, le service et quelque soit la la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '51-MXX12'::text AS code, '51-P.U. moyen GHS pour le médecin quelque soit le service et quelque soit la la durée de séjour sur les 12 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '52-MXX18'::text AS code, '51-P.U. moyen GHS pour le médecin quelque soit le service et quelque soit la la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte
                            UNION
                            SELECT '61-XSX18'::text AS code, '61-P.U. moyen GHS quelque soit le médecin, pour le service et quelque soit la la durée de séjour sur les 18 derniers mois NON SIGNIFICATIF'::text AS texte

                            
                        ) subview_modes ON subview_modes.code < mode_calcul_code
                    WHERE type_ligne_code = 'GHSNG' AND 
                        mode_calcul_code <> '' 

                    ORDER BY 1 DESC,2,3,4,5,6
               
						
				]]></select>
				<FIELDS>
                    <FIELD name="DENC"/>
					<FIELD name="FAC"/>
					<FIELD name="TYP"/>
					<FIELD name="PRE"/>
                    <FIELD name="LVL"/>
					<FIELD name="TXT"/>
					<FIELD name="NB"/>
                    <FIELD name="COE"/>
                    <FIELD name="PU"/>
                    <FIELD name="MT"/>
                    <FIELD name="MTE"/>
                    <FIELD name="DDEB"/>
                    <FIELD name="DFIN"/>
                    <FIELD name="ROWHEIGHT"/>
				</FIELDS>
			</SQL>
		</QUERY>
        
        <QUERY label="Solde client">
			<SQL>
				<select><![CDATA[
				
				
					WITH subview AS 
                        (
                            SELECT date(date_trunc('month',date_comptable)) AS date_comptable,
                                p_factures.no_facture,
                                SUM(montant_comptabilise) AS montant_comptabilise,
                                SUM(montant_comptabilise_1) AS montant_comptabilise_1,
                                SUM(montant_comptabilise_2) AS montant_comptabilise_2,
                                SUM(montant_comptabilise_0) AS montant_comptabilise_0,
                                SUM(montant_regle) AS montant_regle,
                                SUM(montant_regle_1) AS montant_regle_1,
                                SUM(montant_regle_2) AS montant_regle_2,
                                SUM(montant_regle_0) AS montant_regle_0,
                                SUM(montant_comptabilise) - SUM(montant_regle) AS montant_solde,
                                SUM(montant_comptabilise_1) - SUM(montant_regle_1) AS montant_solde_1,
                                SUM(montant_comptabilise_2) - SUM(montant_regle_2) AS montant_solde_2,
                                SUM(montant_comptabilise_0) - SUM(montant_regle_0) AS montant_solde_0
                            FROM activite.p_facture_solde_tiers_c
                            JOIN activite.p_factures ON p_facture_solde_tiers_c.no_facture = p_factures.no_facture
                            WHERE no_sejour = '[SEJOUR]' AND date_trunc('month',date_comptable) <= date(now())
                            GROUP BY 1,2
                            )
                        
                    SELECT
                        lvl,
                        rang,
                        date_comptable,
                        lvl_detail,
                        montant_comptabilise,
                        montant_regle,
                        SUM(montant_solde) OVER (PARTITION BY lvl ORDER BY lvl,rang,date_comptable,lvl_detail),
                        SUM(montant_solde_1) OVER (PARTITION BY lvl ORDER BY lvl,rang,date_comptable,lvl_detail),
                        SUM(montant_solde_2) OVER (PARTITION BY lvl ORDER BY lvl,rang,date_comptable,lvl_detail),
                        SUM(montant_solde_0) OVER (PARTITION BY lvl ORDER BY lvl,rang,date_comptable,lvl_detail)
                    FROM  
                    (
                         SELECT NULL::date AS date_comptable,
                            '0'::text AS lvl,
                            0::numeric AS rang,
                            'ETABLISSEMENT'::text AS lvl_detail,
                            0::numeric AS montant_comptabilise,
                            0::numeric  AS montant_regle,
                            0::numeric AS montant_solde,
                            0::numeric AS montant_solde_1,
                            0::numeric AS montant_solde_2,
                            0::numeric AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '1'::text AS lvl,
                            0::numeric AS rang,
                            trim(base.cti_to_french(to_char(date_comptable, 'Month YYYY'))) AS lvl_detail,
                            SUM(montant_comptabilise) AS montant_comptabilise,
                            SUM(montant_regle) AS montant_regle,
                            SUM(montant_solde)  AS montant_solde,
                            SUM(montant_solde_1) AS montant_solde_1,
                            SUM(montant_solde_2) AS montant_solde_2,
                            SUM(montant_solde_0) AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '2'::text AS lvl,
                            1::numeric AS rang,
                            no_facture AS lvl_detail,
                            SUM(montant_comptabilise) AS montant_comptabilise,
                            SUM(montant_regle) AS montant_regle,
                            SUM(montant_solde)  AS montant_solde,
                            SUM(montant_solde_1) AS montant_solde_1,
                            SUM(montant_solde_2) AS montant_solde_2,
                            SUM(montant_solde_0) AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '3'::text AS lvl,
                            2::numeric AS rang,
                            'AMO'::text AS lvl_detail,
                            SUM(montant_comptabilise_1) AS montant_comptabilise,
                            SUM(montant_regle_1) AS montant_regle,
                            SUM(montant_solde_1) AS montant_solde,
                            SUM(montant_solde_1) AS montant_solde_1,
                            0 AS montant_solde_2,
                            0 AS montant_solde_0
                        FROM subview
                        WHERE (montant_comptabilise_1 <> 0 OR montant_regle_1 <> 0)
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '3'::text AS lvl,
                            3::numeric AS rang,
                            'AMC'::text AS lvl_detail,
                            SUM(montant_comptabilise_2) AS montant_comptabilise,
                            SUM(montant_regle_2) AS montant_regle,
                            SUM(montant_solde_2) AS montant_solde,
                            0 AS montant_solde_1,
                            SUM(montant_solde_2) AS montant_solde_2,
                            0 AS montant_solde_0
                        FROM subview
                        WHERE (montant_comptabilise_2 <> 0 OR montant_regle_2 <> 0)
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '3'::text AS lvl,
                            4::numeric AS rang,
                            'PAT'::text AS lvl_detail,
                            SUM(montant_comptabilise_0) AS montant_comptabilise,
                            SUM(montant_regle_0) AS montant_regle,
                            SUM(montant_solde_0) AS montant_solde,
                            0 AS montant_solde_1,
                            0 AS montant_solde_2,
                            SUM(montant_solde_0) AS montant_solde_0
                        FROM subview
                        WHERE (montant_comptabilise_0 <> 0 OR montant_regle_0 <> 0)
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT NULL::date AS date_comptable,
                            '8'::text AS lvl,
                            0::numeric AS rang,
                            'Total Etablissement'::text AS lvl_detail,
                            SUM(montant_comptabilise) AS montant_comptabilise,
                            SUM(montant_regle) AS montant_regle,
                            SUM(montant_solde)  AS montant_solde,
                            SUM(montant_solde_1) AS montant_solde_1,
                            SUM(montant_solde_2) AS montant_solde_2,
                            SUM(montant_solde_0) AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                         UNION
                        SELECT NULL::date AS date_comptable,
                            '9'::text AS lvl,
                            1::numeric AS rang,
                            ''::text AS lvl_detail,
                            NULL::numeric AS montant_comptabilise,
                            NULL::numeric AS montant_regle,
                            NULL::numeric AS montant_solde,
                            NULL::numeric AS montant_solde_1,
                            NULL::numeric AS montant_solde_2,
                            NULL::numeric AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                    ) subview
                    ORDER BY 1,2,3,4
						

				]]></select>
				<FIELDS>
					<FIELD name="LVL"/>
					<FIELD name="RNG"/>
					<FIELD name="DAT"/>
                    <FIELD name="DET"/>
					<FIELD name="MTC"/>
					<FIELD name="MTR"/>
					<FIELD name="MTS"/>
					<FIELD name="MTS1"/>
                    <FIELD name="MTS2"/>
                    <FIELD name="MTS0"/>
				</FIELDS>
			</SQL>
            <SQL>
				<select><![CDATA[
				
				
					WITH subview AS 
                        (
                            SELECT date(date_trunc('month',date_comptable)) AS date_comptable,
                                p_factures.no_facture,
                                SUM(montant_comptabilise) AS montant_comptabilise,
                                SUM(montant_comptabilise_1) AS montant_comptabilise_1,
                                SUM(montant_comptabilise_2) AS montant_comptabilise_2,
                                SUM(montant_comptabilise_0) AS montant_comptabilise_0,
                                SUM(montant_regle) AS montant_regle,
                                SUM(montant_regle_1) AS montant_regle_1,
                                SUM(montant_regle_2) AS montant_regle_2,
                                SUM(montant_regle_0) AS montant_regle_0,
                                SUM(montant_comptabilise) - SUM(montant_regle) AS montant_solde,
                                SUM(montant_comptabilise_1) - SUM(montant_regle_1) AS montant_solde_1,
                                SUM(montant_comptabilise_2) - SUM(montant_regle_2) AS montant_solde_2,
                                SUM(montant_comptabilise_0) - SUM(montant_regle_0) AS montant_solde_0
                            FROM activite.p_factures_soldes_h
                            JOIN activite.p_factures ON p_factures_soldes_h.no_facture = p_factures.no_facture
                            WHERE no_sejour = '[SEJOUR]' AND date_trunc('month',date_comptable) <= date(now())
                            GROUP BY 1,2
                            )
                        
                    SELECT
                        lvl,
                        rang,
                        date_comptable,
                        lvl_detail,
                        montant_comptabilise,
                        montant_regle,
                        SUM(montant_solde) OVER (PARTITION BY lvl ORDER BY lvl,rang,date_comptable,lvl_detail),
                        SUM(montant_solde_1) OVER (PARTITION BY lvl ORDER BY lvl,rang,date_comptable,lvl_detail),
                        SUM(montant_solde_2) OVER (PARTITION BY lvl ORDER BY lvl,rang,date_comptable,lvl_detail),
                        SUM(montant_solde_0) OVER (PARTITION BY lvl ORDER BY lvl,rang,date_comptable,lvl_detail)
                    FROM  
                    (
                        SELECT NULL::date AS date_comptable,
                            '0'::text AS lvl,
                            0::numeric AS rang,
                            'HONORAIRES'::text AS lvl_detail,
                            0::numeric AS montant_comptabilise,
                            0::numeric  AS montant_regle,
                            0::numeric AS montant_solde,
                            0::numeric AS montant_solde_1,
                            0::numeric AS montant_solde_2,
                            0::numeric AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '1'::text AS lvl,
                            0::numeric AS rang,
                            trim(base.cti_to_french(to_char(date_comptable, 'Month YYYY'))) AS lvl_detail,
                            SUM(montant_comptabilise) AS montant_comptabilise,
                            SUM(montant_regle) AS montant_regle,
                            SUM(montant_solde)  AS montant_solde,
                            SUM(montant_solde_1) AS montant_solde_1,
                            SUM(montant_solde_2) AS montant_solde_2,
                            SUM(montant_solde_0) AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '2'::text AS lvl,
                            1::numeric AS rang,
                            no_facture AS lvl_detail,
                            SUM(montant_comptabilise) AS montant_comptabilise,
                            SUM(montant_regle) AS montant_regle,
                            SUM(montant_solde)  AS montant_solde,
                            SUM(montant_solde_1) AS montant_solde_1,
                            SUM(montant_solde_2) AS montant_solde_2,
                            SUM(montant_solde_0) AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '3'::text AS lvl,
                            2::numeric AS rang,
                            'AMO'::text AS lvl_detail,
                            SUM(montant_comptabilise_1) AS montant_comptabilise,
                            SUM(montant_regle_1) AS montant_regle,
                            SUM(montant_solde_1) AS montant_solde,
                            SUM(montant_solde_1) AS montant_solde_1,
                            0 AS montant_solde_2,
                            0 AS montant_solde_0
                        FROM subview
                        WHERE (montant_comptabilise_1 <> 0 OR montant_regle_1 <> 0)
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '3'::text AS lvl,
                            3::numeric AS rang,
                            'AMC'::text AS lvl_detail,
                            SUM(montant_comptabilise_2) AS montant_comptabilise,
                            SUM(montant_regle_2) AS montant_regle,
                            SUM(montant_solde_2) AS montant_solde,
                            0 AS montant_solde_1,
                            SUM(montant_solde_2) AS montant_solde_2,
                            0 AS montant_solde_0
                        FROM subview
                        WHERE (montant_comptabilise_2 <> 0 OR montant_regle_2 <> 0)
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT date_comptable,
                            '3'::text AS lvl,
                            4::numeric AS rang,
                            'PAT'::text AS lvl_detail,
                            SUM(montant_comptabilise_0) AS montant_comptabilise,
                            SUM(montant_regle_0) AS montant_regle,
                            SUM(montant_solde_0) AS montant_solde,
                            0 AS montant_solde_1,
                            0 AS montant_solde_2,
                            SUM(montant_solde_0) AS montant_solde_0
                        FROM subview
                        WHERE (montant_comptabilise_0 <> 0 OR montant_regle_0 <> 0)
                        GROUP BY 1,2,3,4
                        UNION
                        SELECT NULL::date AS date_comptable,
                            '8'::text AS lvl,
                            0::numeric AS rang,
                            'Total Honoraires'::text AS lvl_detail,
                            SUM(montant_comptabilise) AS montant_comptabilise,
                            SUM(montant_regle) AS montant_regle,
                            SUM(montant_solde)  AS montant_solde,
                            SUM(montant_solde_1) AS montant_solde_1,
                            SUM(montant_solde_2) AS montant_solde_2,
                            SUM(montant_solde_0) AS montant_solde_0
                        FROM subview
                        GROUP BY 1,2,3,4
                    ) subview
                    ORDER BY 1,2,3,4
						

				]]></select>
				<FIELDS>
					<FIELD name="LVL"/>
					<FIELD name="RNG"/>
					<FIELD name="DAT"/>
                    <FIELD name="DET"/>
					<FIELD name="MTC"/>
					<FIELD name="MTR"/>
					<FIELD name="MTS"/>
					<FIELD name="MTS1"/>
                    <FIELD name="MTS2"/>
                    <FIELD name="MTS0"/>
				</FIELDS>
			</SQL>
		</QUERY>
		
		<QUERY label="DMA">
            <SQL>
				<select><![CDATA[
					DROP TABLE IF EXISTS w_prestations;
					CREATE TEMP TABLE w_prestations AS
					SELECT t_prestations.oid, 
						t_prestations.code,
						MAX(CASE 
							WHEN t_prestations.code = 'DMASSR' THEN 'DMA'
							WHEN t_listes.code = 'DMAP_MFS' THEN 'MFS'
							WHEN t_listes.code = 'DMAP_MFSM' THEN 'MFSM'
							ELSE '' END) AS type_dma,
                        MAX(type_valorisation_non_facture) AS type_valorisation_non_facture
					FROM activite.t_prestations
					LEFT JOIN activite.t_listes_contenu ON t_listes_contenu.to_id = t_prestations.oid
					LEFT JOIN activite.t_listes ON liste_id = t_listes.oid
					WHERE t_listes.code IN ('DMAP_MFS','DMAP_MFSM') OR t_prestations.code = 'DMASSR' OR t_prestations.code = 'FJ' OR type_valorisation_non_facture = 'PJ'
					GROUP BY 1,2
					;
				
					
					DROP TABLE IF EXISTS w_dma;
					CREATE TEMP TABLE w_dma AS
					SELECT 
						p_factures.no_sejour,
						p_factures_lignes_c.date_debut,
						0::numeric AS sequence,
						MAX(CASE WHEN p_factures_lignes_c.date_fin >= '20170701' THEN '1' ELSE '0' END) AS juillet2017,
						MAX(CASE WHEN p_sejours.code_sorti = '1' AND p_factures_lignes_c.date_debut = p_sejours.date_sortie THEN '1' ELSE '0' END) AS est_jour_sortie,
						MAX(CASE WHEN t_prestations.code IN ('PJ','SNS','FS') OR type_valorisation_non_facture = 'PJ' THEN prix_unitaire ELSE 0 END) AS pj_pu,
                        (MAX(CASE WHEN t_prestations.code IN ('PJ','SNS','FS') OR type_valorisation_non_facture = 'PJ' THEN Array[p_factures.date_facture::text||to_char(nb_prestation,'FM00000'),taux_1::text] ELSE ARRAY['','0'] END))[2]::numeric AS pj_taux,
						SUM(CASE WHEN t_prestations.code IN ('PJ','SNS','FS') OR type_valorisation_non_facture = 'PJ' THEN nb_rubrique ELSE 0 END) AS pj_nb,
						SUM(CASE WHEN t_prestations.code IN ('PJ','SNS','FJ','FS') OR type_valorisation_non_facture = 'PJ' THEN montant_facture ELSE 0 END) AS pj_montant,
						SUM(CASE WHEN type_dma = 'MFS' AND t_prestations.code NOT IN ('PJ','SNS','FJ','FS') AND type_valorisation_non_facture <> 'PJ' THEN montant_facture ELSE 0 END) AS aut_montant,
						SUM(CASE WHEN type_dma = 'MFSM' THEN montant_facture ELSE 0 END) AS dmam_montant,
						MAX(CASE WHEN type_dma = 'MFSM' THEN '1' ELSE '0' END) AS dmam,
						MAX(CASE WHEN type_dma = 'DMA' THEN '1' ELSE '0' END) AS dma,
						MAX(CASE WHEN type_dma = 'DMA' THEN taux_1 ELSE 0 END) AS dma_taux,
						SUM(CASE WHEN type_dma = 'DMA' THEN nb_rubrique ELSE 0 END) AS dma_nb,
						SUM(CASE WHEN type_dma = 'DMA' THEN montant_facture ELSE 0 END) AS dma_montant
					FROM activite.p_factures_lignes_c
					JOIN activite.p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
					JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour
                    JOIN activite.t_lieux On p_sejours.lieu_sortie_id = t_lieux.oid
                    JOIN activite.t_services_facturation ON service_facturation_id = t_services_facturation.oid
					JOIN w_prestations t_prestations ON prestation_id = t_prestations.oid
					WHERE p_factures.no_sejour = '[SEJOUR]' AND
                        t_services_facturation.type_t2a = '2' AND 
						(t_prestations.code <> 'FJ' OR p_factures_lignes_c.date_debut <> p_sejours.date_sortie OR p_sejours.code_sorti <> '1')
					GROUP BY 1,2,3
					ORDER BY 1,2,3
					;

					UPDATE w_dma
					SET pj_pu = COALESCE((SELECT (MAX(ARRAY[w_dma_2.date_debut::text,pj_pu::text]))[2]::numeric FROM w_dma w_dma_2 WHERE w_dma_2.no_sejour = w_dma.no_sejour AND w_dma_2.date_debut < w_dma.date_debut AND w_dma_2.pj_pu <> 0),0)
					WHERE w_dma.pj_pu = 0
					;
                    
                    UPDATE w_dma
					SET pj_taux = COALESCE((SELECT (MAX(ARRAY[w_dma_2.date_debut::text,pj_taux::text]))[2]::numeric FROM w_dma w_dma_2 WHERE w_dma_2.no_sejour = w_dma.no_sejour AND w_dma_2.date_debut < w_dma.date_debut AND w_dma_2.pj_taux <> 0),0)
					WHERE w_dma.pj_taux = 0
					;

					UPDATE w_dma
					SET dma = COALESCE((SELECT (MAX(ARRAY[w_dma_2.date_debut::text,dma::text]))[2]::text FROM w_dma w_dma_2 WHERE w_dma_2.no_sejour = w_dma.no_sejour AND w_dma_2.date_debut < w_dma.date_debut AND w_dma_2.dma = '1'),'0')
					WHERE w_dma.dma = '0'
					;
					
					UPDATE w_dma
					SET dma_taux = COALESCE((SELECT (MAX(ARRAY[w_dma_2.date_debut::text,dma_taux::text]))[2]::numeric FROM w_dma w_dma_2 WHERE w_dma_2.no_sejour = w_dma.no_sejour AND w_dma_2.date_debut < w_dma.date_debut AND w_dma_2.dma_taux <> 0),0)
					WHERE w_dma.dma_taux = 0
					;
					
					UPDATE w_dma
					SET dmam = COALESCE((SELECT (MAX(ARRAY[w_dma_2.date_debut::text,dmam::text]))[2]::text FROM w_dma w_dma_2 WHERE w_dma_2.no_sejour = w_dma.no_sejour AND w_dma_2.date_debut < w_dma.date_debut AND w_dma_2.dmam = '1'),'0')
					WHERE w_dma.dmam = '0' AND est_jour_sortie = '1'
					;
					
						

					UPDATE w_dma
					SET sequence = subview.sequence
					FROM
					(
					SELECT rank() OVER (ORDER BY date_debut) AS sequence, date_debut, date_fin
					FROM
					(
					SELECT 
						rank() OVER w1 AS rank,
						first_value(date_debut) OVER w1 AS date_debut,
						first_value(date_debut) OVER w2 AS date_fin
					FROM w_dma
					
					WINDOW
						w1 as (
						    PARTITION BY pj_pu, pj_taux, dma, dma_taux, dmam, juillet2017
						    ORDER BY date_debut),
						w2 as (
						    PARTITION BY pj_pu, pj_taux, dma, dma_taux, dmam, juillet2017
						    ORDER BY date_debut desc)
					) subview 
					WHERE rank = 1
					ORDER BY 1
					) subview
					WHERE w_dma.date_debut BETWEEN subview.date_debut AND subview.date_fin
						     
					;

					SELECT sequence,
						MIN(date_debut),
						MAx(date_debut),
						MAX(pj_pu),
                        MAX(pj_taux),
						SUM(pj_nb),
						SUM(pj_montant),
						SUM(aut_montant),
						SUM(pj_montant)+SUM(aut_montant),
						SUM(dmam_montant),
						MAX(dma_taux),
						SUM(dma_montant),
						SUM(dma_montant) + SUM(dmam_montant)
					FROM w_dma
					GROUP BY 1
					ORDER BY 1					

				]]></select>
				<FIELDS>
					<FIELD name="SEQ"/>
					<FIELD name="DTDEB"/>
					<FIELD name="DTFIN"/>
                    <FIELD name="PUPJ"/>
                    <FIELD name="TXPJ"/>
					<FIELD name="NBPJ"/>
					<FIELD name="MTPJ"/>
					<FIELD name="MTAUT"/>
					<FIELD name="MTPRE"/>
					<FIELD name="MTMDMA"/>
					<FIELD name="MTDMAX"/>
                    <FIELD name="MTDMA"/>
					<FIELD name="MTEDMA"/>
				</FIELDS>
			</SQL>
		</QUERY>
        
		<QUERY type="comboLink" name="FACTURES" forSelections="false">
			<SQL select="SELECT no_facture FROM activite.p_factures WHERE no_sejour = '[SEJOUR]' ORDER BY no_facture">
				<FIELDS>
					<FIELD name="cod"/>
				</FIELDS>
			</SQL>
		</QUERY>
		
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forSelections="false" forRows="true">
			<SQL>
				<select><![CDATA[
				
						SELECT 
							'QEXPERT',
							count(*)::text
						FROM activite.v_expert_sejour_controle_1 
						WHERE v_expert_sejour_controle_1.no_sejour = '[SEJOUR]'
                        UNION
                        SELECT 
							'QVALO',
							count(*)::text
						FROM activite.p_facture_ligne_valorise_c
                        JOIN activite.p_factures ON p_factures.no_facture = p_facture_ligne_valorise_c.no_facture
						WHERE p_factures.no_sejour = '[SEJOUR]' 
                        UNION
                        SELECT 
							'QENCOURS',
							count(*)::text
						FROM activite.p_factures_encours
						WHERE p_factures_encours.no_sejour = '[SEJOUR]' 
						UNION
						SELECT 'NB_RSS', count(DISTINCT rss_id)::text AS rss_id
						FROM activite.p_sejour_pmsi
						WHERE 1=1
                            AND no_sejour = '[SEJOUR]'
							AND pmsi_type = 'MCO'
                            AND EXISTS (SELECT 1 FROM information_schema.views WHERE table_schema = 'pmsi' AND table_name = 'v_rss_1')                            
						UNION
						SELECT 'NO_RSS', base.cti_group_concat(pmsi_type::text || ' ' || no_rss::text) 
                        FROM activite.p_sejour_pmsi
                        WHERE 1=1
                            AND no_sejour = '[SEJOUR]'
                            AND pmsi_type = 'MCO'
                            AND EXISTS (SELECT 1 FROM information_schema.views WHERE table_schema = 'pmsi' AND table_name = 'v_rss_1')                            
						;
							
				]]></select>
				<FIELDS>
					<FIELD name="name"/>
					<FIELD name="value"/>
				</FIELDS>
			</SQL>
			
			<SQL>
				<select><![CDATA[
						SELECT 'BG', est_budget_global
							FROM activite.p_sejours
							WHERE no_sejour = '[SEJOUR]'
						;
							
				]]></select>
				<FIELDS>
					<FIELD name="name"/>
					<FIELD name="value"/>
				</FIELDS>
			</SQL>
			
			
		</QUERY>
	</QUERIES>
	<PRESENTATION>
		<VIEWLINKS>
			
			<VIEWLINK label="Détail RSS PMSI" shortLabel="Dossier PMSI" view="PMSI000006.XML" rowContext="true"
				  visible="= (TAB == Dossier PMSI MCO6 || (TAB == Expert7 &amp;&amp; VIEW.BG == '1')) &amp;&amp; ROW.PMSIRSS != 0">
				<ARG name="OID" value="ROW.PMSIOID" />
			</VIEWLINK>

		</VIEWLINKS>
		
		<ONGLET label="Séjour">
			<DATAGRID title="" total="false" showHeaders="false" multiLine="true" headerHeight="18" printRatio="1">
				<ROWSTYLE name="fontSize" value="14"/>
				<ROWSTYLE name="backgroundColor" value="0xFFD393" condition="ROW.KEY == 'CHA'"/>
				<ROWSTYLE name="fontWeight" value="bold" condition="ROW.KEY == 'CHA'"/>
				<COLUMN dataField="KEY" visible="false" textAlign="left"/>
				<COLUMN dataField="LABEL" visible="true" width="300" textAlign="left">
					<CELLSTYLE name="paddingLeft" value="30" condition="ROW.KEY == 'PAD'"/>
					<CELLSTYLE name="paddingLeft" value="50" condition="ROW.KEY == 'PAD2'"/>
				</COLUMN>
				<COLUMN dataField="VAL" visible="true" minWidth="400" textAlign="left">
					<CELLSTYLE name="fontWeight" value="bold" condition="ROW.KEY == '1'"/>
					<CELLSTYLE name="fontWeight" value="bold" condition="ROW.KEY == '2'"/>
					<CELLSTYLE name="paddingLeft" value="20" condition="ROW.KEY == 'PAD2'"/>
				</COLUMN>
				<LINE label="Détail Séjour" key="CHA"/>
				<LINE label="FINESS" key="PAD">
					<COLUMN dataField="FINESS" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="N° Séjour" key="PAD">
					<COLUMN dataField="NSEJ" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Nom" key="PAD">
					<COLUMN dataField="NOM" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Prénom" key="PAD">
					<COLUMN dataField="PREN" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Code Postal" key="PAD">
					<COLUMN dataField="CP" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Sexe" key="PAD">
					<COLUMN dataField="SEXE" type="Char" toDataField="VAL" visible="true" textAlign="left" >
						<CELLSTYLE name="cellIndicator" value="femme, 12,left middle , 0xEB76B1" condition="ROW.SEXE == 'Féminin'" /> 
						<CELLSTYLE name="cellIndicator" value="homme, 12,left middle , 0x75a0eb " condition="ROW.SEXE == 'Masculin'" /> 
						<CELLSTYLE name="cellIndicator" value="rectangle, 10, left middle, 0x666666" condition="ROW.SEXE != 'Féminin' &amp;&amp; ROW.SEXE != 'Masculin'" /> 
						<CELLSTYLE name="paddingLeft" value="10"/>
					</COLUMN>
				</LINE>
				<LINE label="Age" key="PAD">
					<COLUMN dataField="AGE" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="N° Patient" key="PAD">
					<COLUMN dataField="NPAT" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Etat du Séjour" key="CHA"/>
<!-- 				<LINE label="Etat" key="PAD">
					<COLUMN dataField="ETATSEJ" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE> -->
				<LINE label="Etat du séjour" key="PAD">
					<COLUMN dataField="ETATSEJ1" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Type de séjour CTI" key="PAD">
					<COLUMN dataField="TYPSEJ" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="PMSI" key="PAD">
					<COLUMN dataField="PMSI" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Traitement e-PMSI" key="PAD">
					<COLUMN dataField="EPMSI" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="GHM-GME" key="PAD">
					<COLUMN dataField="GHM" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Date groupage" key="PAD">
					<COLUMN dataField="DATEGRP" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Date facturation" key="PAD">
					<COLUMN dataField="DATEFAC" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Date solde" key="PAD">
					<COLUMN dataField="DATESOL" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Séjour" key="CHA">
				</LINE>
				<LINE label="Date entrée" key="PAD">
					<COLUMN dataField="DENT" type="Date" toDataField="VAL" inputFormat="AAAA-MM-JJ+" outputFormat="DD MMMM YYYY" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Date sortie" key="PAD">
					<COLUMN dataField="DSOR" type="Date" toDataField="VAL" inputFormat="AAAA-MM-JJ+" outputFormat="DD MMMM YYYY" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Durée séjour" key="PAD">
					<COLUMN dataField="DURSEJ" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Mode d'entrée / Provenance" key="PAD">
					<COLUMN dataField="PROVENANCE" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
                <LINE label="Etablissement Provenance" key="PAD">
					<COLUMN dataField="ETAB_PROV" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Mode de sortie / Destination" key="PAD">
					<COLUMN dataField="DESTINATION" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
                <LINE label="Etablissement Destination" key="PAD">
					<COLUMN dataField="ETAB_DEST" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Médecin traitant" key="PAD">
					<COLUMN dataField="MEDT" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>				
                <LINE label="Médecin" key="PAD">
					<COLUMN dataField="MED" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Risque" key="PAD">
					<COLUMN dataField="RISQUE" type="Char" toDataField="VAL" visible="true" textAlign="left" />
				</LINE>
				<LINE label="Intervention" key="CHA">
					<ROWSTYLE name="hide" condition="ROW.INTACT == '' "/>
				</LINE>
				<LINE label="Date" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.INTACT == ''"/>
					<COLUMN dataField="INTDAT" type="Date" toDataField="VAL" inputFormat="AAAA-MM-JJ+" outputFormat="DD MMMM YYYY" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Acte" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.INTACT == ''"/>
					<COLUMN dataField="INTACT" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Chirurgien" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.INTACT == ''"/>
					<COLUMN dataField="INTCHI" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Anesthésiste" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.INTACT == ''"/>
					<COLUMN dataField="INTARE" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>

				
				<LINE label="Classifications établissement" key="CHA">
					<ROWSTYLE name="hide" condition="ROW.PERS1 == '' &amp;&amp; ROW.BUDCLE == '' "/>
				</LINE>
				<LINE label="Paramètre personnalisé" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.PERS1SEC == ''"/>
					<COLUMN dataField="PERS1SEC" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Paramètre personnalisé" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.PERS1 == ''"/>
					<COLUMN dataField="PERS1" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Regroupement budgétaire" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.BUDCLESEC == ''"/>
					<COLUMN dataField="BUDCLESEC" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Clé budgétaire" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.BUDCLE == ''"/>
					<COLUMN dataField="BUDCLE" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="" key="">
				</LINE>
				
				<LINE label="Tiers payant" key="CHA"/>
				<LINE label="Régime Général" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.TP1I == '0'"/>
					<COLUMN dataField="TP1" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Matricule" key="PAD2">
					<ROWSTYLE name="hide" condition="ROW.MAT1 == ''"/>
					<COLUMN dataField="MAT1" type="Char" toDataField="VAL" visibleCondition="ROW.MAT1 != ''" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Régime Complémentaire" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.TP2I == '0'"/>
					<COLUMN dataField="TP2" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Matricule" key="PAD2">
					<ROWSTYLE name="hide" condition="ROW.MAT2 == ''"/>
					<COLUMN dataField="MAT2" type="Char" toDataField="VAL" textAlign="left"/>
				</LINE>
				<LINE label="Régime Complémentaire (2)" key="PAD">
					<ROWSTYLE name="hide" condition="ROW.TP22I == '0'"/>
					<COLUMN dataField="TP22" type="Char" toDataField="VAL" visible="true" textAlign="left"/>
				</LINE>
				<LINE label="Matricule" key="PAD2">
					<ROWSTYLE name="hide" condition="ROW.MAT22 == ''"/>
					<COLUMN dataField="MAT22" type="Char" toDataField="VAL" textAlign="left"/>
				</LINE>
				<LINE label="Facturation" key="CHA">
				</LINE>
				<LINE label="Nombre factures" key="PAD">
					<COLUMN dataField="NBFAC" type="Number" toDataField="VAL" outputFormat="#" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="" key="PAD2">
					<ROWSTYLE name="hide" condition="VIEW.BG != '1'"/>
					<COLUMN dataField="ESTBG" type="Char" toDataField="VAL" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="C.A. Facturé Total" key="PAD">
					<COLUMN dataField="MFT" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="C.A. Facturé Etablissement" key="PAD">
					<COLUMN dataField="MFC" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Dont actes inclus" key="PAD2">
					<ROWSTYLE name="hide" condition="ROW.MIC == 0.00"/>
					<COLUMN dataField="MIC" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="C.A. Facturé Honoraire" key="PAD">
					<COLUMN dataField="MFH" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				
				<LINEx label="C.A. Total (dépassement inclus)" key="PAD">
					<COLUMN dataField="MTDI" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left" calc="true">
						<CALC formula="ROW.MFT + ROW.MIC" condition=""/>
					</COLUMN>
				</LINEx>
				<LINE label="Compte Client" key="CHA"/>
				<LINE label="C.A. Comptabilisé Etablissement" key="PAD">
					<COLUMN dataField="MCC" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Dont Budget Global" key="PAD2">
					<ROWSTYLE name="hide" condition="VIEW.BG != '1'"/>
					<COLUMN dataField="MCCBG" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Dont Hors budget global" key="PAD2">
					<ROWSTYLE name="hide" condition="VIEW.BG != '1'"/>
					<COLUMN dataField="MCCHBG" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Montant réglé Etablissement" key="PAD">
					<COLUMN dataField="MRC" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Solde Etablissement" key="PAD">
					<COLUMN dataField="MSC" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="C.A. Comptabilisé Honoraires" key="PAD">
					<ROWSTYLE name="hide" condition="VIEW.BG == '1'"/>
					<COLUMN dataField="MCH" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Montant réglé Honoraires" key="PAD">
					<ROWSTYLE name="hide" condition="VIEW.BG == '1'"/>
					<COLUMN dataField="MRH" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Solde Honoraires" key="PAD">
					<ROWSTYLE name="hide" condition="VIEW.BG == '1'"/>
					<COLUMN dataField="MSH" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Ecart Budget global" key="CHA">
					<ROWSTYLE name="hide" condition="VIEW.BG != '1' || Number(ROW.MCCBG) == 0"/>
				</LINE>
				<LINE label="C.A. Facturé AMO " key="PAD">
					<ROWSTYLE name="hide" condition="VIEW.BG != '1' || Number(ROW.MCCBG) == 0"/>
					<COLUMN dataField="MFT1" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="C.A. Budget Global" key="PAD">
					<ROWSTYLE name="hide" condition="VIEW.BG != '1' || Number(ROW.MCCBG) == 0"/>
					<COLUMN dataField="MCCBG" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				<LINE label="Ecart Facturé AMO / Budget global" key="PAD">
					<ROWSTYLE name="hide" condition="VIEW.BG != '1' || Number(ROW.MCCBG) == 0"/>
					<COLUMN dataField="MECBG" type="Number" toDataField="VAL" outputFormat="#,00E" visible="true" textAlign="left">
					</COLUMN>
				</LINE>
				
			</DATAGRID>
		</ONGLET>
		<ONGLET label="Mouvements" queryNumber="1">
			<DATAGRID title="" total="true" headerHeight="36" rowHeight="36" totalRowInRows="= ROW.LVL == '9'">
				<COLUMN dataField="LVL" type="Char" visible="false" />
				<COLUMN dataField="DDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date entrée" width="70" textAlign="left"/>
				<COLUMN dataField="HDEB" type="Char" visible="true" headerText="Heure" width="60" textAlign="left"/>
				<COLUMN dataField="DFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date sortie" width="70" textAlign="left"/>
				<COLUMN dataField="HFIN" type="Char" visible="true" headerText="Heure" width="60" textAlign="left"/>
				<COLUMN dataField="JOS" type="Char"  visible="true" headerText="Durée" width="80" textAlign="left" totalFunction="sum"/>
				<COLUMN dataField="EMC" type="Number" visible="true" headerText="EMC" outputFormat="#,00" width="35" textAlign="left" totalFunction="sum"/>
				<COLUMN dataField="SER" type="Char" visible="true" headerText="Service / UF" minWidth="50" textAlign="left"/>
				<COLUMN dataField="ETA" type="Char" visible="true" headerText="UM" minWidth="50" textAlign="left"/>
				<COLUMN dataField="LIT" type="Char" visible="true" headerText="Lit" minWidth="50" textAlign="left"/>
				<COLUMN dataField="MED" type="Char" visible="true" headerText="Médecin" minWidth="80" textAlign="left"/>
			</DATAGRID>
		</ONGLET>
		<ONGLET label="Factures" queryNumber="2">
			<DATAGRID title="" total="false" headerHeight="54" rowHeight="18" sortable="false">
				<ROWSTYLE name="fontSize" value="11" condition="ROW.LVL == '1'"/>
				<ROWSTYLE name="backgroundColor" value="0xFFD393" condition="ROW.LVL == '1'"/>
				<ROWSTYLE name="fontWeight" value="bold" condition="ROW.LVL == '1'"/>
				<COLUMN dataField="LVL" type="Char" visible="false" headerText="" minWidth="100" textAlign="left"/>
				<COLUMN dataField="NFAC" type="Char" visible="true" headerText="N° facture" width="110" textAlign="left"/>
				<COLUMN dataField="DDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date début" width="65" textAlign="left"/>
				<COLUMN dataField="DFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date fin" width="65" textAlign="left"/>
				<COLUMN dataField="DFAC" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date facture" width="65" textAlign="left"/>
				<COLUMN dataField="DEXP" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date expédition" width="65" textAlign="left"/>
				<COLUMN dataField="DVEN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date ventes" width="65" textAlign="left"/>
				<COLUMN dataField="TP" type="Char" visible="true" headerText="Tiers" minWidth="100" textAlign="left"/>
				<COLUMN dataField="MFT" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. Facturé Total" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MFC" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. Facturé Séjour" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MFH" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. Facturé Actes" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MBG" type="Number" fixed="false" outputFormat="#,00E" visibleCondition="VIEW.BG == '1'" headerText="Budget Global" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MCC" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. Compta. Ets" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MRC" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Réglé Ets" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MSC" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Solde Ets" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MCH" type="Number" fixed="false" outputFormat="#,00E" visibleCondition="VIEW.BG != '1'" headerText="Compta. honoraires" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MRH" type="Number" fixed="false" outputFormat="#,00E" visibleCondition="VIEW.BG != '1'" headerText="Réglé honoraires" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
				<COLUMN dataField="MSH" type="Number" fixed="false" outputFormat="#,00E" visibleCondition="VIEW.BG != '1'" headerText="Solde honoraires" width="80" textAlign="right" totalFunction="sum">
				</COLUMN>
			</DATAGRID>
		</ONGLET>
		<ONGLET label="Séjour facturé" queryNumber="3">
			<SELECTIONS>
				<GROUP>
					<FIELD name="LIGNESC_LVL" label="Niveau" UI="combo" showIfNotDefault="true" updateEachTime="true" localSelection="true" default="1">
						<OPTION label="Total par rubrique" data="1"/>
						<OPTION label="Détail des factures" data="2"/>
						<OPTION label="Total par compte" data="3"/>
					</FIELD>
				</GROUP>
			</SELECTIONS>
			<INPUTSELECTION condition="SELECT.LIGNESC_LVL == ROW.LVL || ROW.LVL == '9'"/>
			<DATAGRID title="" total="true" totalRowInRows="= ROW.LVL == '9'" headerHeight="36" rowHeight="18">
				<COLUMN dataField="LVL" type="Char" visible="false" headerText="" minWidth="100" textAlign="left"/>
				<COLUMN dataField="NFAC" type="Char" visible="true" 
					headerTextCalc="true" headerText="if (SELECT.LIGNESC_LVL == '1' || SELECT.LIGNESC_LVL == '3') {return 'N° facture Référence'} else {return 'N° facture'}" 
					width="100" textAlign="left"/>
				<COLUMN dataField="DDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date début" width="55" textAlign="left"/>
				<COLUMN dataField="DFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date fin" width="55" textAlign="left"/>
				<COLUMN dataField="PRE" type="Char" visibleCondition="SELECT.LIGNESC_LVL != '3'" headerText="Prestation" minWidth="100" textAlign="left"/>
				<COLUMN dataField="RUB" type="Char" visibleCondition="SELECT.LIGNESC_LVL != '3'" headerText="Rubrique" minWidth="100" textAlign="left"/>
				<COLUMN dataField="CPT" type="Char" visible="true" headerText="Compte" minWidth="100" textAlign="left"/>
				<COLUMN dataField="PU" type="Number" outputFormat="#,00E" visibleCondition="SELECT.LIGNESC_LVL != '3'" headerText="P.U." width="70" textAlign="right"/>
				<COLUMN dataField="COE" type="Number" outputFormat="#,00" visibleCondition="SELECT.LIGNESC_LVL != '3'" headerText="Coeff." width="50" textAlign="right"/>
				<COLUMN dataField="COEM" type="Number" outputFormat="#,0000" visibleCondition="SELECT.LIGNESC_LVL != '3'" headerText="Coeff. MCO" width="50" textAlign="left"/>
				<COLUMN dataField="MT" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. facturé" width="80" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="TAU1" type="Number" fixed="false" outputFormat="#%" visible="true" headerText="Taux AMO" width="40" textAlign="right"/>
				<COLUMN dataField="MT1" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. AMO" width="80" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="TAU2" type="Number" fixed="false" outputFormat="#%" visible="true" headerText="Taux AMC" width="40" textAlign="right"/>
				<COLUMN dataField="MT2" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. AMC" width="80" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="TAU22" type="Number" fixed="false" outputFormat="#%" visible="true" headerText="Taux AMC2" width="40" textAlign="right"/>
				<COLUMN dataField="MT22" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. AMC2" width="80" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="TAU0" type="Number" fixed="false" outputFormat="#%" visible="true" headerText="Taux pat" width="40" textAlign="right"/>
				<COLUMN dataField="MT0" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. patient" width="80" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="ENC" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. Valorisé@LF@(non facturé)" width="80" textAlign="right" totalFunction="sum"/>
			</DATAGRID>
		</ONGLET>
		<ONGLET label="Actes facturés" queryNumber="4">
			<SELECTIONS>
				<GROUP>
					<FIELD name="LIGNESH_LVL" label="Niveau" UI="combo" updateEachTime="true" localSelection="true" default="1">
						<OPTION label="Total par acte" data="1"/>
						<OPTION label="Détail des actes" data="2"/>
					</FIELD>
				</GROUP>
			</SELECTIONS>
			<INPUTSELECTION condition="SELECT.LIGNESH_LVL == ROW.LVL || ROW.LVL == '9'"/>
			<DATAGRID title="" total="true" totalRowInRows="= ROW.LVL == '9'" headerHeight="36" rowHeight="18" printRatio="0.7">
				<COLUMN dataField="LVL" type="Char" visible="false" headerText="" minWidth="100" textAlign="left"/>
				<COLUMN dataField="NFAC" type="Char" visible="true" 
					headerTextCalc="true" headerText="if (SELECT.LIGNESC_LVL == '1') {return 'N° facture Référence'} else {return 'N° facture'}" 
					width="100" textAlign="left"/>
				<COLUMN dataField="DDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date début" width="55" textAlign="left"/>
				<COLUMN dataField="DFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date fin" width="55" textAlign="left"/>
				<COLUMN dataField="ACT" type="Char" visible="true" headerText="Acte" minWidth="70" textAlign="left"/>
				<COLUMN dataField="EXE" type="Char" visible="true" headerText="Exécutant" minWidth="70" textAlign="left"/>
				<COLUMN dataField="PU" type="Number" outputFormat="#,00E" visible="true" headerText="P.U." width="70" textAlign="right"/>
				<COLUMN dataField="NB" type="Number" outputFormat="#,00" visible="true" headerText="Nb." width="50" textAlign="right"/>
				<COLUMN dataField="PROTO" type="Number" outputFormat="#" visible="true" headerText="Proto." width="50" textAlign="right"/>
				<COLUMN dataField="COE" type="Number" outputFormat="#,00" visible="true" headerText="Coeff." width="50" textAlign="right"/>
				<COLUMN dataField="NBCOE" type="Number" outputFormat="#,00" visible="true" headerText="Nb * Coeff." width="50" textAlign="right"/>
				<COLUMN dataField="MT" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A." width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MDP" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Montant dépassements" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="TAU1" type="Number" fixed="false" outputFormat="#%" visible="true" headerText="Taux SS" width="40" textAlign="right"/>
				<COLUMN dataField="MT1" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. AMO" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MT2" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. AMC" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MT0" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. patient" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MTI" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Actes inclus séjour" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MTNF" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. non facturé" width="70" textAlign="right" totalFunction="sum"/>
			</DATAGRID>
		</ONGLET>
		<ONGLET label="Comptabilisé" queryNumber="5" visibleCondition="VIEW.BG == '1'">
			<SELECTIONS>
				<GROUP>
					<FIELD name="LIGNESCC_LVL" label="Niveau" UI="combo" updateEachTime="true" localSelection="true" default="1">
						<OPTION label="Total par rubrique" data="1"/>
						<OPTION label="Détail des factures" data="2"/>
					</FIELD>
				</GROUP>
			</SELECTIONS>
			<INPUTSELECTION condition="SELECT.LIGNESCC_LVL == ROW.LVL || ROW.LVL == '9'"/>
			<DATAGRID title="" total="true" totalRowInRows="= ROW.LVL == '9'" headerHeight="36" rowHeight="18">
				<COLUMN dataField="LVL" type="Char" visible="false" headerText="" minWidth="100" textAlign="left"/>
				<COLUMN dataField="NFAC" type="Char" visible="true" 
					headerTextCalc="true" headerText="if (SELECT.LIGNESC_LVL == '1') {return 'N° facture Référence'} else {return 'N° facture'}" 
					width="100" textAlign="left"/>
				<COLUMN dataField="DDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date début" width="55" textAlign="left"/>
				<COLUMN dataField="DFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date fin" width="55" textAlign="left"/>
				<COLUMN dataField="PRE" type="Char" visible="true" headerText="Prestation" minWidth="70" textAlign="left"/>
				<COLUMN dataField="RUB" type="Char" visible="true" headerText="Rubrique" minWidth="70" textAlign="left"/>
				<COLUMN dataField="PU" type="Number" outputFormat="#,00E" visible="true" headerText="P.U." width="70" textAlign="right"/>
				<COLUMN dataField="COE" type="Number" outputFormat="#,00" visible="true" headerText="Coeff." width="50" textAlign="right"/>
				<COLUMN dataField="COEM" type="Number" outputFormat="#,0000" visible="true" headerText="Coeff. MCO" width="50" textAlign="left"/>
				<COLUMN dataField="TAU1" type="Number" fixed="false" outputFormat="#%" visible="true" headerText="Taux SS" width="40" textAlign="right"/>
				<COLUMN dataField="MTT" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A." width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MT1BG" type="Number" fixed="false" outputFormat="#,00E" visibleCondition="VIEW.BG == '1'" headerText="C.A. B.G." width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MT" type="Number" fixed="false" outputFormat="#,00E" visibleCondition="VIEW.BG == '1'" headerText="C.A. Hors B.G" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MT1" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Dont AMO" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MT2" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Dont AMC" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MT22" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Dont AMC2" width="70" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MT0" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Dont patient" width="70" textAlign="right" totalFunction="sum"/>
			</DATAGRID>
		</ONGLET>
        <ONGLET label="Solde" queryNumber="10" >
			<SELECTIONS>
				<GROUP>
					<FIELD name="LIGNESSL_LVL" label="Niveau" UI="combo" updateEachTime="true" localSelection="true" default="1">
						<OPTION label="Mois" data="1"/>
						<OPTION label="Factures" data="2"/>
                        <OPTION label="Tiers" data="3"/>
					</FIELD>
				</GROUP>
			</SELECTIONS>
			<INPUTSELECTION condition="SELECT.LIGNESSL_LVL == ROW.LVL || ROW.LVL == '0' || ROW.LVL == '8' || ROW.LVL == '9'"/>
            
			<DATAGRID title="" total="false" headerHeight="36" rowHeight="18" sortable="false">
                <ROWSTYLE name="fontSize" value="12" condition="ROW.LVL == '0'"/>
				<ROWSTYLE name="backgroundColor" value="0xFFD393" condition="ROW.LVL == '0'"/>
				<ROWSTYLE name="fontWeight" value="bold" condition="ROW.LVL == '0'"/>
                <ROWSTYLE name="fontSize" value="12" condition="ROW.LVL == '8'"/>
				<ROWSTYLE name="fontWeight" value="bold" condition="ROW.LVL == '8'"/>
				<COLUMN dataField="LVL" type="Char" visible="false" headerText="" minWidth="100" textAlign="left"/>
                
				<COLUMN dataField="DET" type="Char" visible="true" 
					headerTextCalc="true" headerText="SELECT.LIGNESSL_LVL_LABEL" 
					minWidth="200" textAlign="left"/>
				<COLUMN dataField="DAT" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="MM/YY" 
                    visibleCondition="SELECT.LIGNESSL_LVL != '1'" headerText="Date" width="55" textAlign="left"/>
				<COLUMN dataField="MTC" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Comptabilisé" width="100" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MTR" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Réglé" width="100" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MTS" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Solde" width="100" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MTS1" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Dont AMO" width="100" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MTS2" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Dont AMC" width="100" textAlign="right" totalFunction="sum"/>
				<COLUMN dataField="MTS0" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="Dont patient" width="100" textAlign="right" totalFunction="sum"/>
			</DATAGRID>
		</ONGLET>
		<ONGLET label="Dossier PMSI MCO" queryNumber="6" >
			<DATAGRID title="" total="true" headerHeight="36" rowHeight="18">
				<COLUMN dataField="PMSITYP" type="Char" visible="true" headerText="PMSI" width="70" textAlign="left"/>
				<COLUMN dataField="PMSIOID" type="Char" visible="false" headerText="OID" width="70" textAlign="left"/>
				<COLUMN dataField="PMSIRSS" type="Char" visible="false" headerText="OID" width="70" textAlign="left"/>
				<COLUMN dataField="PMSINO" type="Char" visible="true" headerText="N°" width="70" textAlign="left"/>
				<COLUMN dataField="DDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date début" width="70" textAlign="left"/>
				<COLUMN dataField="DFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date fin" width="70" textAlign="left"/>
				<COLUMN dataField="ENC" type="Char" visible="true" headerText="Expédié" width="70" textAlign="left"/>
				<COLUMN dataField="PRP" type="Char" visible="true" headerText="Type" width="70" textAlign="left"/>
				
				<COLUMNx dataField="DGP" type="Char" visible="true" headerText="Diag. P" minWidth="70" textAlign="left"/>
				<COLUMN dataField="ACP" type="Char" visible="true" headerText="Acte P" width="70" textAlign="left"/>
				
				<COLUMN dataField="GHM" type="Char" visible="true" headerText="GHM" minWidth="70" textAlign="left"/>
			</DATAGRID>
		</ONGLET>
		<ONGLET label="Expert" queryNumber="7" newPage="false" >
			<DATAGRID title="" total="false" headerHeight="36" rowHeight="36">
				<ROWSTYLE name="fontSize" value="12"/>
				<COLUMN dataField="CXDS"
					width="90"
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YY"
					visible="true"
					fixed="false"
					headerText="Date signalement" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text">
				</COLUMN>
				<COLUMN dataField="CXG"
					width="60"
					type="Char"
					visible="true"
					headerText="Gravité" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text"
					totalComplement="">
				</COLUMN>
				<COLUMN dataField="CXC"
					width="120"
					type="Char"
					visible="true"
					outputFormat="#"
					headerText="Code Controle" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text" 
					totalComplement="">
				</COLUMN>
				<COLUMN dataField="CXT"
					minWidth="200"
					type="Char"
					visible="true"
					headerText="Texte Controle" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text"
					totalComplement="">
				</COLUMN>
				<COLUMN dataField="CXJ"
					minWidth="100"
					type="Char"
					visible="true"
					headerText="Justificatif Controle" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text"
					totalComplement="">
				</COLUMN>
			</DATAGRID>
		</ONGLET>
        <ONGLET label="Valorisation" queryNumber="8" newPage="false" >
			<DATAGRID title="" total="false" sortable="false" headerHeight="36" rowHeight="18" variableRowHeight="true">
				<ROWSTYLE name="fontSize" value="12"/>
                <ROWSTYLE name="backgroundColor" value="0xFFD393" condition="ROW.TYP == 0"/>
                <ROWSTYLE name="rowHeight" value="= ROW.ROWHEIGHT.toString()" condition="Number(ROW.ROWHEIGHT) &gt; 0"/>
                <ROWSTYLE name="fontWeight" value="bold" condition="ROW.LVL == 1"/>
                
                <COLUMN dataField="TYP" type="Number" visible="false" />
                <COLUMN dataField="PRE" type="Number" visible="false" />
                <COLUMN dataField="LVL" type="Number" visible="false" />
                <COLUMN dataField="ROWHEIGHT" type="Char" visible="false" />

                    
				<COLUMN dataField="TXT"
					minWidth="400"
					type="Char"
					visible="true"
					fixed="false"
					headerText="Justificatif" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text"
                    treeLevelPad="10">
                    <CELLSTYLE name="paddingLeft" value="0" condition="ROW.LVL == 1"/>
                    <CELLSTYLE name="paddingLeft" value="10" condition="ROW.LVL == 2"/>
                    <CELLSTYLE name="paddingLeft" value="20" condition="ROW.LVL == 3"/>
                    <CELLSTYLE name="paddingLeft" value="50" condition="ROW.LVL == 4"/>
                    <CELLSTYLE name="fontWeight" value="bold" condition="ROW.LVL == 2"/>
				</COLUMN>
                <COLUMN dataField="DDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date début" width="55" textAlign="left"/>
				<COLUMN dataField="DFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date   fin" width="55" textAlign="left"/>
                <COLUMN dataField="PU" type="Number" outputFormat="#,00E" visible="true" headerText="P.U." width="70" textAlign="right"/>
                <COLUMN dataField="NB" type="Number" outputFormat="#,00" visible="true" headerText="Nb." width="50" textAlign="right"/>
				<COLUMN dataField="COE" type="Number" outputFormat="#,00" visible="true" headerText="Coeff." width="50" textAlign="right"/>
				<COLUMN dataField="MT" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. valorisé" width="80" textAlign="right" totalFunction="sum"/>
				
			</DATAGRID>
		</ONGLET>
        <ONGLET label="En-cours" queryNumber="9" newPage="false" >
			<DATAGRID title="" total="false" sortable="false" headerHeight="36" rowHeight="18" variableRowHeight="true">
				<ROWSTYLE name="fontSize" value="12"/>
                <ROWSTYLE name="backgroundColor" value="0xFFD393" condition="ROW.TYP == 0"/>
                <ROWSTYLE name="rowHeight" value="= ROW.ROWHEIGHT.toString()" condition="Number(ROW.ROWHEIGHT) &gt; 0"/>
                <ROWSTYLE name="fontWeight" value="bold" condition="ROW.LVL == 1"/>
                
                <COLUMN dataField="TYP" type="Number" visible="false" />
                <COLUMN dataField="PRE" type="Number" visible="false" />
                <COLUMN dataField="LVL" type="Number" visible="false" />
                <COLUMN dataField="ROWHEIGHT" type="Char" visible="false" />

                    
				<COLUMN dataField="TXT"
					minWidth="400"
					type="Char"
					visible="true"
					fixed="false"
					headerText="Justificatif" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text"
                    treeLevelPad="10">
                    <CELLSTYLE name="paddingLeft" value="0" condition="ROW.LVL == 1"/>
                    <CELLSTYLE name="paddingLeft" value="10" condition="ROW.LVL == 2"/>
                    <CELLSTYLE name="paddingLeft" value="20" condition="ROW.LVL == 3"/>
                    <CELLSTYLE name="paddingLeft" value="50" condition="ROW.LVL == 4"/>
                    <CELLSTYLE name="fontWeight" value="bold" condition="ROW.LVL == 2"/>
				</COLUMN>
                <COLUMN dataField="DDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date début" width="55" textAlign="left"/>
				<COLUMN dataField="DFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date   fin" width="55" textAlign="left"/>
                <COLUMN dataField="PU" type="Number" outputFormat="#,00E" visible="true" headerText="P.U." width="70" textAlign="right"/>
                <COLUMN dataField="NB" type="Number" outputFormat="#,00" visible="true" headerText="Nb." width="50" textAlign="right"/>
				<COLUMN dataField="COE" type="Number" outputFormat="#,00" visible="true" headerText="Coeff." width="50" textAlign="right"/>
				<COLUMN dataField="MT" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. valorisé total" width="90" textAlign="right" totalFunction="sum"/>
                <COLUMN dataField="MTE" type="Number" fixed="false" outputFormat="#,00E" visible="true" headerText="C.A. En-cours du mois" width="90" textAlign="right" totalFunction="sum"/>
				
			</DATAGRID>
		</ONGLET>
		
		<ONGLET label="DMA SSR" queryNumber="11" >
			<DATAGRID title="" total="true" headerHeight="36" rowHeight="18">
			
				<COLUMN dataField="DTDEB" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date début" width="70" textAlign="left"/>
				<COLUMN dataField="DTFIN" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" headerText="Date fin" width="70" textAlign="left"/>
				
				<COLUMN dataField="NBPJ" type="Number" outputFormat="#" visible="true" headerText="Nb Jours" width="70" textAlign="right"/>

				<COLUMN dataField="PUPJ" type="Number" outputFormat="#,00E" visible="true" headerText="P.U. PJ" width="70" textAlign="right"/>
                <COLUMN dataField="TXPJ" type="Number" outputFormat="#%" visible="true" headerText="Taux PJ" width="70" textAlign="right"/>
				<COLUMN dataField="MTPJ" type="Number" outputFormat="#,00E" visible="true" headerText="Montant PJ" width="100" textAlign="right"/>
				<COLUMN dataField="MTAUT" type="Number" outputFormat="#,00E" visible="true" headerText="Montant Autres" width="100" textAlign="right"/>
				<COLUMN dataField="MTPRE" type="Number" outputFormat="#,00E" visible="true" headerText="Montant Total" width="100" textAlign="right"/>
				<COLUMN dataField="MTMDMA" type="Number" outputFormat="#,00E" visible="true" headerText="Montant 10%" width="100" textAlign="right"/>
				
				<COLUMN dataField="MTDMAX" type="Number" outputFormat="#%" visible="true" headerText="Taux DMA" width="100" textAlign="right"/>
				
				
				<COLUMN dataField="MTDMA" type="Number" outputFormat="#,00E" visible="true" headerText="Montant DMA" width="100" textAlign="right"/>
				
				<COLUMN dataField="MTEDMA" type="Number" outputFormat="#,00E" visible="true" headerText="Ecart DMA" width="100" textAlign="right"/>
				
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>
