<?php
require_once("../../base/php/startSession.php");
require_once("../../base/php//classDatabase.php");
require_once( "../../base/php/Functions.php" );

global $database;

$tableName = getPOST("tableName");
$firstCall = getPOST("firstCall");

if ($tableName == "") {
	$tableName = "t_services_facturation";
}

$database = new Database("iCTI");

$httpString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
$httpString .= "\n<HTTP_SERVICE>";

if ($tableName == "t_services_facturation") {
	$httpString .= getRecords_services_facturation();
}
if ($tableName == "t_activites") {
	$httpString .= getRecords_activites();
}
if ($tableName == "t_divers") {
	$httpString .= getRecords_divers();
}
if ($tableName == "t_unites_medicales") {
	$httpString .= getRecords_unites_medicales();
}
if ($tableName == "t_unites_fonctionnelles") {
	$httpString .= getRecords_unites_fonctionnelles();
}
if ($tableName == "t_etages") {
	$httpString .= getRecords_etages();
	$httpString .= getCombo_services();
	$httpString .= getCombo_finess();
}
if ($tableName == "t_lits") {
	$httpString .= getRecords_lits();
	$httpString .= getCombo_etages();
}
if ($tableName == "t_compte") {
	$httpString = $httpString . getRecords_compte();
	$httpString = $httpString . getCombo_compte_tva();
}
if ($tableName == "t_service_rubrique") {
	$httpString = $httpString . getRecords_service_rubrique();
	$httpString = $httpString . getCombo_compte();
	$httpString .= getCombo_prestations();
}
if ($tableName == "t_service_rubrique_dotation") {
	$httpString .= getRecords_service_rubrique_dotation();
	$httpString .= getCombo_services();
    $httpString .= getCombo_rubriques();
    $httpString .= getCombo_prestations();
}
if ($tableName == "t_prestations_c") {
	$httpString .= getRecords_prestations_c();
}
if ($tableName == "t_prestations_h") {
	$httpString .= getRecords_prestations_h();
}
if ($tableName == "t_rubriques_facturation") {
	$httpString .= getRecords_rubriques_facturation();
}
if ($tableName == "t_types_tiers_payant") {
	$httpString .= getRecords_types_tiers_payant();
}
if ($tableName == "t_tiers_payant") {
	$httpString .= getRecords_tiers_payant();
}
if ($tableName == "t_ccam_regroupements") {
	$httpString .= getRecords_ccam_regroupements();
}
if ($tableName == "t_actes") {
	$httpString .= getRecords_actes();
}
if ($tableName == "t_ghs") {
	$httpString .= getRecords_ghs();
}
if ($tableName == "t_specialites_medecin") {
	$httpString .= getRecords_specialites_internes_medecin();
}
if ($tableName == "t_medecins_administratifs") {
	$httpString .= getRecords_medecins_administratifs();
}
if ($tableName == "t_medecins_traitants_administratifs") {
	$httpString .= getRecords_medecins_traitants_administratifs();
}

if ($tableName == "t_medecins") {
	$httpString .= getRecords_medecins();
	$httpString .= getList_medecins_administratifs();
	$httpString .= getCombo_specialites_internes_medecin();
}
if ($tableName == "t_sejour") {
	$httpString .= getRecords_sejour();
	if ($firstCall == "1") {
		$httpString .= getCombo_medecins_administratifs();
		$httpString .= getCombo_services();
		$httpString .= getCombo_etages();
		$httpString .= getCombo_mois_encours();
		$httpString .= getArray_sejours();
	}
}

if ($tableName == "t_budget") {
	$httpString .= getRecords_budget();
	$httpString .= getCombo_indicateurs();
}
if ($tableName == "t_budget_cle") {
	$httpString .= getRecords_budget_cle();
}
if ($tableName == "t_budget_cle_rule") {
	$httpString .= getRecords_budget_cle_rule();
	$httpString .= getCombo_budget_cle();
}
if ($tableName == "t_budget_cle_budget") {
	$httpString .= getRecords_budget_cle_budget();
}
if ($tableName == "t_expert_controle") {
	if ($getOption != "getRefSettings") {
		$httpString .= getRecords_expert_controle();
	}
	else {
		$httpString .= getRecords_expert_controle_refsettings($refPrefix);
	}
}


if ($tableName == "t_forme_activite") {
	$httpString = $httpString . getRecords_forme_activite();
}

if ($tableName == "t_forme_activite_rule") {
	$httpString = $httpString . getRecords_forme_activite_rule();
    $httpString = $httpString . getCombo_forme_activite();
}

if ($tableName == "t_filiere_soin") {
	$httpString = $httpString . getRecords_filiere_soin();
}

if ($tableName == "t_filiere_soin_rule") {
	$httpString = $httpString . getRecords_filiere_soin_rule();
    $httpString = $httpString . getCombo_filiere_soin();
}

if ($tableName == "t_pole_rule") {
	$httpString = $httpString . getRecords_pole_rule();
    $httpString = $httpString . getCombo_pole();
}

if ($tableName == "t_compte_produit_analytique_rule") {
	$httpString = $httpString . getRecords_compte_analytique_rule();
    $httpString = $httpString . getCombo_compte();
}

if ($tableName == "t_calcul_encours") {
	$httpString = $httpString . getRecords_calcul_encours();
}

if ($tableName == "t_kiwee_params") {
	$httpString = $httpString . getRecords_kiwee_params();
}


$httpString .= "\n</HTTP_SERVICE>";

$httpString = compress64($httpString);

echo "<CTICONTENT>$httpString</CTICONTENT>";



function getRecords_services_facturation() {

global $database;
	
	$httpString = "";
	
	$sqlcmd = "SELECT t_services_facturation.oid as ser_id, 
				t_services_facturation.code as ser_code, 
				t_services_facturation.texte as ser_texte, 
				t_services_facturation.texte_court as ser_texte_court, 
				t_services_facturation.code_original as ser_code_original, 
				t_services_facturation.nb_lits as ser_nb_lits, 
				t_services_facturation.nb_cp as ser_nb_cp, 
				t_services_facturation.type_t2a as ser_type_t2a, 
				t_services_facturation.est_budget_global as ser_est_budget_global,
				t_services_facturation.mode_traitement_id as mode_traitement_id, 
				t_modes_traitement.code as mode_traitement_code,
				t_services_facturation.dmt_id as dmt_id,  
				t_dmt.code as dmt_code,
				t_services_facturation.est_sans_mouvement as ser_est_sans_mouvement,
				t_services_facturation.est_sans_facturation as ser_est_sans_facturation,
				t_services_facturation.sej_sans_acte_fictif as sej_sans_acte_fictif,
				t_services_facturation.avec_facturation_intermediaire as ser_avec_facturation_intermediaire,
				t_services_facturation.type_sejour as ser_type_sejour,
				t_services_facturation.etage_par_defaut_id as ser_etage_par_defaut,
				t_services_facturation.etage_force_id as ser_etage_force_id,
				t_services_facturation.finess_id as finess_id, 
				COALESCE(t_finess.code,''::text) as finess_code,
				t_services_facturation.option_jour_ferme,
				t_services_facturation.prestations_presence,
				t_services_facturation.particularites_encours,
                t_services_facturation.mode_calcul_journees,
                t_services_facturation.activite_par_defaut_id as ser_activite_par_defaut,
				t_services_facturation.activite_force_id as ser_activite_force_id,
				t_services_facturation.medecin_par_defaut_id as ser_medecin_par_defaut,
				t_services_facturation.medecin_force_id as ser_medecin_force_id,
				subview.xmlnodes as xmlnodes
			FROM activite.t_services_facturation
				LEFT JOIN base.t_finess ON (finess_id = t_finess.oid)
				LEFT JOIN base.t_modes_traitement ON (mode_traitement_id = t_modes_traitement.oid)
				LEFT JOIN base.t_dmt ON (dmt_id = t_dmt.oid)
				LEFT JOIN
				(
				SELECT
					service_facturation_id,
					base.cti_group_concat_without_sep(xmlnode) AS xmlnodes
				FROM (
					SELECT
						service_facturation_id, 
						'<HISTO ' ||
						'date=\"' || to_char(date,'DD/MM/YYYY') || '\" ' ||
						'nb_lits_theoriques=\"' || nb_lits_theoriques || '\" '  
						'nb_lits_ouverts=\"' || nb_lits_ouverts || '\" '  
						'nb_box_ambulatoires_theoriques=\"' || coalesce(nb_box_ambulatoires_theoriques,0) || '\" '  
						'nb_box_ambulatoires_ouverts=\"' || coalesce(nb_box_ambulatoires_ouverts,0) || '\" '  
						'nb_box_seances_theoriques=\"' || coalesce(nb_box_seances_theoriques,0) || '\" '  
						'nb_box_seances_ouverts=\"' || coalesce(nb_box_seances_ouverts,0) || '\" '  
						'est_ouvert_1=\"' || est_ouvert_1 || '\" '  
						'est_ouvert_2=\"' || est_ouvert_2 || '\" '  
						'est_ouvert_3=\"' || est_ouvert_3 || '\" '  
						'est_ouvert_4=\"' || est_ouvert_4 || '\" '  
						'est_ouvert_5=\"' || est_ouvert_5 || '\" '  
						'est_ouvert_6=\"' || est_ouvert_6 || '\" '  
						'est_ouvert_7=\"' || est_ouvert_7 || '\" '  
						|| '/>'	AS xmlnode
					FROM activite.t_services_facturation_historique
					ORDER BY service_facturation_id, date DESC
				) subview
			GROUP BY service_facturation_id) subview ON subview.service_facturation_id = t_services_facturation.oid
			WHERE t_services_facturation.oid > 0  
				AND t_services_facturation.oid IN (SELECT DISTINCT service_facturation_id FROM activite.t_lieux) 
			ORDER BY t_services_facturation.code ";
	$result = $database->exec($sqlcmd);
	
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecord();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = ctiFloatVal(trim($record['ser_id']));
				$code = toHTML(trim($record['ser_code']));
				$texte = toHTML(trim($record['ser_texte']));
				$texte_court = toHTML(trim($record['ser_texte_court']));
				$code_original = toHTML(trim($record['ser_code_original']));
				$nb_lits = toHTML(trim($record['ser_nb_lits']));
				$nb_cp = toHTML(trim($record['ser_nb_cp']));
				$type_t2a = toHTML(trim($record['ser_type_t2a']));
				
				$est_budget_global = toHTML(trim($record['ser_est_budget_global']));
				$mode_traitement_id = trim($record['mode_traitement_id']) + 0 ;
				$mode_traitement_code = toHTML(trim($record['mode_traitement_code'])) ;
				$dmt_id = trim($record['dmt_id']) + 0 ;
				$dmt_code = toHTML(trim($record['dmt_code'])) ;

				$est_sans_mouvement = toHTML(trim($record['ser_est_sans_mouvement']));
				$est_sans_facturation = toHTML(trim($record['ser_est_sans_facturation']));
				$sej_sans_acte_fictif = toHTML(trim($record['sej_sans_acte_fictif']));
				$avec_facturation_intermediaire = toHTML(trim($record['ser_avec_facturation_intermediaire']));
				$type_sejour = toHTML(trim($record['ser_type_sejour']));
				
				$etage_par_defaut_id = ctiFloatVal(trim($record['ser_etage_par_defaut']));
				$etage_force_id = ctiFloatVal(trim($record['ser_etage_force_id']));
                
                $activite_par_defaut_id = ctiFloatVal(trim($record['ser_activite_par_defaut']));
				$activite_force_id = ctiFloatVal(trim($record['ser_activite_force_id']));
				
				$medecin_par_defaut_id = ctiFloatVal(trim($record['ser_medecin_par_defaut']));
				$medecin_force_id = ctiFloatVal(trim($record['ser_medecin_force_id']));
				
				$finess_id = ctiFloatVal(trim($record['finess_id']));
				$finess_code = trim($record['finess_code']);
				
				$option_jour_ferme = toHTML(trim($record['option_jour_ferme']));
				$prestations_presence = toHTML(trim($record['prestations_presence']));
				$particularites_encours = toHTML(trim($record['particularites_encours']));
                
                $mode_calcul_journees = toHTML(trim($record['mode_calcul_journees']));
				
				$xmlHISTO = trim($record['xmlnodes']);
				
				$mtdmt = "";
				if ($mode_traitement_id > 0) {
					$mtdmt = $mtdmt . $mode_traitement_code;
				}
				if ($dmt_id > 0) {
					$mtdmt = $mtdmt . $dmt_code;
				}
				$mtdmt = trim($mtdmt);
				
				$comment = "";
				if ($type_t2a == "1") {
					$comment = $comment . "PMSI MCO";
				}
				if ($type_t2a == "2") {
					$comment = $comment . "PMSI SSR";
				}
				if ($type_t2a == "3") {
					$comment = $comment . "PMSI PSY";
				}
				if ($type_t2a == "4") {
					$comment = $comment . "PMSI HAD";
				}
				if ($est_budget_global == "1") {
					$comment = $comment . " BG";
				}
				if ($est_sans_mouvement == "1") {
					$comment = $comment . " Sans mouvement";
				}
				if ($est_sans_facturation == "1") {
					$comment = $comment . " Sans facturation";
				}
				if ($sej_sans_acte_fictif == "1") {
					$comment = $comment . " Séjours sans acte sont fictifs";
				}
                if ($mode_calcul_journees == "1") {
					$comment = $comment . " JS non inclus";
				}
                if ($mode_calcul_journees == "2") {
					$comment = $comment . " JS inclus";
				}
				$comment = trim($comment);
				
				$nbref = 0;
				
				$httpString .= 
					"\r\n<RECORD 
						oid=\"$oid\" 
						code=\"$code\" 
						texte=\"$texte\" 
						texte_court=\"$texte_court\" 
						code_original=\"$code_original\" 
						nb_lits=\"$nb_lits\" 
						nb_cp=\"$nb_cp\" 
						type_t2a=\"$type_t2a\" 
						est_budget_global=\"$est_budget_global\" 
						est_sans_mouvement=\"$est_sans_mouvement\" 
						est_sans_facturation=\"$est_sans_facturation\" 
						sej_sans_acte_fictif=\"$sej_sans_acte_fictif\" 
						avec_facturation_intermediaire=\"$avec_facturation_intermediaire\" 
						type_sejour=\"$type_sejour\" 
						mode_traitement_id=\"$mode_traitement_id\" 
						dmt_id=\"$dmt_id\" 
						mtdmt=\"$mtdmt\" 
						etage_par_defaut_id=\"$etage_par_defaut_id\" 
						etage_force_id=\"$etage_force_id\" 
                        activite_par_defaut_id=\"$activite_par_defaut_id\" 
						activite_force_id=\"$activite_force_id\" 
						medecin_par_defaut_id=\"$medecin_par_defaut_id\" 
						medecin_force_id=\"$medecin_force_id\" 
						finess_id=\"$finess_id\" 
						finess=\"$finess_code\" 
						option_jour_ferme=\"$option_jour_ferme\" 
						prestations_presence=\"$prestations_presence\" 
						particularites_encours=\"$particularites_encours\" 
                        mode_calcul_journees=\"$mode_calcul_journees\" 
						comment=\"$comment\" 
						nbref=\"+$nbref\" 
						 >
						$xmlHISTO
					\r\n</RECORD>"; 
			}
		}
	}
	$httpString = $httpString . 
		getCombo_finess() . 
		getCombo_modes_traitement() . 
		getCombo_dmt() . 
		getCombo_etages("t_services_facturation") . 
		getCombo_activites("t_services_facturation") . 
		getCombo_medecins_administratifs("t_services_facturation") . 
		getCombo_unites_medicales() . 
		getCombo_unites_medicales_exceptions();
	
	return $httpString;
}

function getRecords_activites() {

global $database;

	$httpString = "";

	$sqlcmd = "	SELECT 
					oid as act_id, 
					code as act_code, 
					texte as act_texte,
					texte_court as act_texte_court, 
					code_original as act_code_original,
					t_activites.etage_par_defaut_id as eta_default_id,
					t_activites.etage_force_id as est_force_id
				FROM 
					activite.t_activites  
				WHERE 
					oid > 0  
					AND oid IN (SELECT DISTINCT activite_id FROM activite.t_lieux) 
				ORDER BY code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['act_id'];
				$code = toHTML(trim($record['act_code']));
				$texte = toHTML(trim($record['act_texte']));
				$texte_court = toHTML(trim($record['act_texte_court']));
				$code_original = toHTML(trim($record['act_code_original']));
				$etage_par_defaut_id = ctiFloatVal(trim($record['eta_default_id']));
				$etage_force_id = ctiFloatVal(trim($record['est_force_id']));
				$nbref = 0;
				
				$httpString .= "\r\n<RECORD 
						oid=\"$oid\" 
						code=\"$code\" 
						texte=\"$texte\" 
						texte_court=\"$texte_court\" 
						code_original=\"$code_original\" 
						etage_par_defaut_id=\"$etage_par_defaut_id\" 
						etage_force_id=\"$etage_force_id\" 
						nbref=\"+$nbref\" 
						 />";
			}
		}
	}
	
	$httpString .= getCombo_etages();
	
	return $httpString;
}

function getRecords_divers() {

global $database;

	$httpString = "";

	$sqlcmd = "	SELECT 
					1::bigint AS div_id, 
					code as div_code, 
					texte as div_texte, 
					valeur as div_valeur, 
					description as div_desc, 
					valeur2 as div_valeur2,
                    CASE WHEN show_info_module THEN 1 ELSE 0 END AS show_info_module
				FROM activite.t_divers
				ORDER BY code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = 1;
				$code = toHTML(trim($record['div_code']));
				$texte = toHTML(trim($record['div_texte']));
				$valeur = toHTML(trim($record['div_valeur']));
				$description = toHTML(trim($record['div_desc']));
				$valeur2 = toHTML(trim($record['div_valeur2']));
                $show_info_module = toHTML(trim($record['show_info_module']));
				$nbref = 1;
				
				$httpString .= "\r\n<RECORD
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									valeur=\"$valeur\" 
									description=\"$description\" 
									valeur2=\"$valeur2\" 
                                    show_info_module=\"$show_info_module\" 
									nbref=\"+$nbref\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_unites_medicales() {

global $database;
	
	$httpString = "";
	
	$sqlcmd = "SELECT 
					oid as um_id, 
					code as um_code, 
					texte as um_texte, 
					texte_court as um_texte_court, 
					code_original as um_code_original,
					t_unites_medicales.etage_par_defaut_id as umd_etage_par_defaut,
					t_unites_medicales.etage_force_id as umd_etage_force_id,
					subview.xmlnodes as xmlnodes  
				FROM activite.t_unites_medicales 
					LEFT JOIN
						(
						SELECT
							unite_medicale_id,
							base.cti_group_concat_without_sep(xmlnode) AS xmlnodes
						FROM (
							SELECT
								unite_medicale_id, 
								'<HISTO ' ||
								'date=\"' || to_char(date,'DD/MM/YYYY') || '\" ' ||
								'nb_lits_theoriques=\"' || nb_lits_theoriques || '\" '  
								'nb_lits_ouverts=\"' || nb_lits_ouverts || '\" '  
								'nb_box_ambulatoires_theoriques=\"' || coalesce(nb_box_ambulatoires_theoriques,0) || '\" '  
								'nb_box_ambulatoires_ouverts=\"' || coalesce(nb_box_ambulatoires_ouverts,0) || '\" '  
								'nb_box_seances_theoriques=\"' || coalesce(nb_box_seances_theoriques,0) || '\" '  
								'nb_box_seances_ouverts=\"' || coalesce(nb_box_seances_ouverts,0) || '\" ' 
								'est_ouvert_1=\"' || est_ouvert_1 || '\" '  
								'est_ouvert_2=\"' || est_ouvert_2 || '\" '  
								'est_ouvert_3=\"' || est_ouvert_3 || '\" '  
								'est_ouvert_4=\"' || est_ouvert_4 || '\" '  
								'est_ouvert_5=\"' || est_ouvert_5 || '\" '  
								'est_ouvert_6=\"' || est_ouvert_6 || '\" '  
								'est_ouvert_7=\"' || est_ouvert_7 || '\" '  
								|| '/>'	AS xmlnode
							FROM activite.t_unites_medicales_historique
							ORDER BY unite_medicale_id, date DESC
						) subview
					GROUP BY unite_medicale_id) subview ON subview.unite_medicale_id = t_unites_medicales.oid				
				WHERE oid > 0  
				ORDER BY code";
	$result = $database->exec($sqlcmd);
	
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['um_id'];
				$code = toHTML(trim($record['um_code']));
				$texte = toHTML(trim($record['um_texte']));
				$texte_court = toHTML(trim($record['um_texte_court']));
				$code_original = toHTML(trim($record['um_code_original']));
				$etage_par_defaut_id = ctiFloatVal(trim($record['ser_etage_par_defaut']));
				$etage_force_id = ctiFloatVal(trim($record['ser_etage_force_id']));
				$xmlHISTO = trim($record['xmlnodes']);
				$nbref = 0;
				
				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									texte_court=\"$texte_court\" 
									code_original=\"$code_original\" 
									etage_par_defaut_id=\"$etage_par_defaut_id\" 
									etage_force_id=\"$etage_force_id\"
									nbref=\"+$nbref\" >
									>
										$xmlHISTO
									\r\n</RECORD>";
			}
		}
	}
	
	$httpString = $httpString . getCombo_etages("t_services_facturation");
	 
	return $httpString;
}

function getRecords_unites_fonctionnelles() {

global $database;

	$httpString = "";
	
	$sqlcmd = "	SELECT 
					oid as uf_id, 
					code as uf_code, 
					texte as uf_texte,
					texte_court as uf_texte_court, 
					code_original as uf_code_original,
					est_sans_facturation as uf_est_sans_facturation,
					subview.xmlnodes as xmlnodes
				FROM activite.t_unites_fonctionnelles 
					LEFT JOIN
						(
						SELECT
							unite_fonctionnelle_id,
							base.cti_group_concat_without_sep(xmlnode) AS xmlnodes
						FROM (
							SELECT
								unite_fonctionnelle_id, 
								'<HISTO ' ||
								'date=\"' || to_char(date,'DD/MM/YYYY') || '\" ' ||
								'nb_lits_theoriques=\"' || nb_lits_theoriques || '\" '  
								'nb_lits_ouverts=\"' || nb_lits_ouverts || '\" '  
								'nb_box_ambulatoires_theoriques=\"' || coalesce(nb_box_ambulatoires_theoriques,0) || '\" '  
								'nb_box_ambulatoires_ouverts=\"' || coalesce(nb_box_ambulatoires_ouverts,0) || '\" '  
								'nb_box_seances_theoriques=\"' || coalesce(nb_box_seances_theoriques,0) || '\" '  
								'nb_box_seances_ouverts=\"' || coalesce(nb_box_seances_ouverts,0) || '\" ' 
								'est_ouvert_1=\"' || est_ouvert_1 || '\" '  
								'est_ouvert_2=\"' || est_ouvert_2 || '\" '  
								'est_ouvert_3=\"' || est_ouvert_3 || '\" '  
								'est_ouvert_4=\"' || est_ouvert_4 || '\" '  
								'est_ouvert_5=\"' || est_ouvert_5 || '\" '  
								'est_ouvert_6=\"' || est_ouvert_6 || '\" '  
								'est_ouvert_7=\"' || est_ouvert_7 || '\" '  
								|| '/>'	AS xmlnode
							FROM activite.t_unites_fonctionnelles_historique
							ORDER BY unite_fonctionnelle_id, date DESC
						) subview
					GROUP BY unite_fonctionnelle_id) subview ON subview.unite_fonctionnelle_id = t_unites_fonctionnelles.oid
				WHERE oid > 0  
				ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['uf_id'];
				$code = toHTML(trim($record['uf_code']));
				$texte = toHTML(trim($record['uf_texte']));
				$texte_court = toHTML(trim($record['uf_texte_court']));
				$code_original = toHTML(trim($record['uf_code_original']));
				$est_sans_facturation = toHTML(trim($record['uf_est_sans_facturation']));
				$xmlHISTO = trim($record['xmlnodes']);
				$nbref = 0;
				
				$httpString .=  
					"\r\n<RECORD 
							oid=\"$oid\" 
							code=\"$code\" 
							texte=\"$texte\" 
							texte_court=\"$texte_court\" 
							code_original=\"$code_original\" 
							est_sans_facturation=\"$est_sans_facturation\" 
							nbref=\"+$nbref\" 
							 >
							$xmlHISTO
						\r\n</RECORD>";
			}
		}
	}
	return $httpString;
}

function getRecords_etages() {

global $database;

	$httpString = "";
	
	$sqlcmd = "	SELECT 
					t_etages.oid as eta_id, 
					t_etages.code as eta_code, 
					t_etages.texte as eta_texte, 
					t_etages.texte_court as eta_texte_court, 
					t_etages.code_original as eta_code_original, 
					t_etages.nb_lits as eta_nb_lits, 
					t_etages.nb_cp as eta_nb_cp, 
                    t_etages.service_force_id,
                    t_etages.est_urgence,
					t_etages.finess_id as finess_id, 
					COALESCE(t_finess.code,''::text) as finess_code,
					t_etages.remplacer_par_valeur_par_defaut,
					ARRAY(SELECT code FROM activite.t_lits WHERE etage_id = t_etages.oid AND code <> '' ORDER BY code ) as lit_codes,
					COALESCE(subview.xmlnodes,'') ||	COALESCE(subview2.xmlnodes,'') as xmlnodes
				FROM activite.t_etages
					LEFT JOIN base.t_finess ON (finess_id = t_finess.oid)
					LEFT JOIN
						(
						SELECT
							etage_id,
							base.cti_group_concat_without_sep(xmlnode) AS xmlnodes
						FROM (
							SELECT
								etage_id, 
								'<HISTO ' ||
								'date=\"' || to_char(date,'DD/MM/YYYY') || '\" ' ||
								'nb_lits_theoriques=\"' || nb_lits_theoriques || '\" '  
								'nb_lits_ouverts=\"' || nb_lits_ouverts || '\" '  
								'nb_lits_cp_theoriques=\"' || nb_lits_cp_theoriques || '\" '  
								'nb_lits_cp_ouverts=\"' || nb_lits_cp_ouverts || '\" '  
								'nb_box_ambulatoires_theoriques=\"' || coalesce(nb_box_ambulatoires_theoriques,0) || '\" '  
								'nb_box_ambulatoires_ouverts=\"' || coalesce(nb_box_ambulatoires_ouverts,0) || '\" '  
								'nb_box_seances_theoriques=\"' || coalesce(nb_box_seances_theoriques,0) || '\" '  
								'nb_box_seances_ouverts=\"' || coalesce(nb_box_seances_ouverts,0) || '\" '
								'est_ouvert_1=\"' || est_ouvert_1 || '\" '  
								'est_ouvert_2=\"' || est_ouvert_2 || '\" '  
								'est_ouvert_3=\"' || est_ouvert_3 || '\" '  
								'est_ouvert_4=\"' || est_ouvert_4 || '\" '  
								'est_ouvert_5=\"' || est_ouvert_5 || '\" '  
								'est_ouvert_6=\"' || est_ouvert_6 || '\" '  
								'est_ouvert_7=\"' || est_ouvert_7 || '\" '  
								'lit_force=\"' || COALESCE(lit_force,'') || '\" ' 
								|| '/>'	AS xmlnode
							FROM activite.t_etages_historique
							ORDER BY etage_id, date DESC
						) subview
						GROUP BY etage_id) subview ON subview.etage_id = t_etages.oid
					LEFT JOIN
						(
						SELECT
							etage_id,
							base.cti_group_concat_without_sep(xmlnode) AS xmlnodes
						FROM (
							SELECT
								etage_id, 
								'<LIT ' ||
								'oid=\"' || t_lits.oid || '\" '  
								'code=\"' || t_lits.code || '\" '  
								'chambre_particuliere=\"' || CASE WHEN coalesce(chambre_particuliere,'N') = 'O' THEN '1' ELSE '0' END || '\" '  
								|| '/>' AS xmlnode
							FROM activite.t_lits
							WHERE code NOT LIKE '%**%'
							ORDER BY etage_id, code
						) subview
						GROUP BY etage_id) subview2 ON subview2.etage_id = t_etages.oid
	
				WHERE t_etages.oid > 0  
				ORDER BY t_etages.code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = ctiFloatVal($record['eta_id']);
				$code = toHTML(trim($record['eta_code']));
				$texte = toHTML(trim($record['eta_texte']));
				$texte_court = toHTML(trim($record['eta_texte_court']));
				$code_original = toHTML(trim($record['eta_code_original']));
				$nb_lits = toHTML(trim($record['eta_nb_lits']));
				$nb_cp = toHTML(trim($record['eta_nb_cp']));
				$lits = toHTML(trim($record['lit_codes']));
				$service_force_id = ctiFloatVal(trim($record['service_force_id']));
				$est_urgence = toHTML(trim($record['est_urgence']));
                $remplacer_par_valeur_par_defaut = toHTML(trim($record['remplacer_par_valeur_par_defaut']));
				$finess_id = ctiFloatVal(trim($record['finess_id']));
				$finess_code = trim($record['finess_code']);
				$xmlHISTO = trim($record['xmlnodes']);
				$nbref = 0;
				
				$lits = str_replace("{", "", $lits);
				$lits = str_replace("}", "", $lits);
				$lits = str_replace(",", ", ", $lits);
				
				$httpString .= 
					"\r\n<RECORD 
							oid=\"$oid\" 
							code=\"$code\" 
							texte=\"$texte\" 
							texte_court=\"$texte_court\" 
							code_original=\"$code_original\" 
							nb_lits=\"$nb_lits\" 
							nb_cp=\"$nb_cp\" 
							lits=\"$lits\" 
							service_force_id=\"$service_force_id\" 
							remplacer_par_valeur_par_defaut=\"$remplacer_par_valeur_par_defaut\" 
                            est_urgence=\"$est_urgence\" 
							finess_id=\"$finess_id\" 
							finess=\"$finess_code\" 
							nbref=\"+$nbref\" 
							>
						$xmlHISTO
						\r\n</RECORD>";
			}
		}
	}
	return $httpString;
}

function getRecords_lits() {

global $database;

	$httpString = "";
	$httpString_tmp = "";
	
	$sqlcmd = "SELECT 
					COALESCE(subview.etage_id,t_etages.oid) as etage_id, 
					COALESCE(subview.etage_code,t_etages.code) as etage_code, 
					COALESCE(subview.etage_texte,t_etages.texte) as etage_texte, 
					t_lits.oid as lit_id, 
					t_lits.code as lit_code, 
					t_lits.texte as lit_texte, 
					t_lits.texte_court as lit_texte_court, 
					t_lits.code_original as lit_code_original,
					CASE WHEN COALESCE(chambre_particuliere,'N') = 'O' THEN '1' ELSE '0' END AS est_chambre_particuliere,
					COALESCE(subview.xmlnodes,'') as xmlnodes
				FROM activite.t_lits
					JOIN activite.t_etages ON etage_id = t_etages.oid
					LEFT JOIN
						(
						SELECT
							lit_id,
							(MAX(ARRAY[date_fin::text, etage_id::text]))[2]::bigint AS etage_id,
							(MAX(ARRAY[date_fin::text, etage_code::text]))[2] AS etage_code,
							(MAX(ARRAY[date_fin::text, etage_texte::text]))[2] AS etage_texte,
							base.cti_group_concat_without_sep(xmlnode) AS xmlnodes
						FROM (
							SELECT
								lit_id, 
								etage_id,
								t_etages.code AS etage_code,
								t_etages.texte AS etage_texte,
								date_debut,
								date_fin,
								'<ETAGE ' ||
								'oid=\"' || t_etages.oid || '\" '  
								'code=\"' || t_etages.code || '\" '  
								'date=\"' || to_char(date_debut,'DD/MM/YYYY') || '\" ' 
								'date_debut=\"' || to_char(date_debut,'DD/MM/YYYY') || '\" ' 
								'date_fin=\"' || to_char(date_fin,'DD/MM/YYYY') || '\" ' 
								|| '/>'::text AS xmlnode
							FROM activite.t_lit_historique
							JOIN activite.t_etages ON etage_id = t_etages.oid
							ORDER BY lit_id, date_debut DESC
						) subview
						GROUP BY lit_id) subview ON subview.lit_id = t_lits.oid
				WHERE t_lits.oid > 0  
				ORDER BY 2,5";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = ctiFloatVal($record['lit_id']);
				$code = toHTML(trim($record['lit_code']));
				$texte = toHTML(trim($record['lit_texte']));
				$texte_court = toHTML(trim($record['lit_texte_court']));
				$code_original = toHTML(trim($record['lit_code_original']));
				$etage_id = ctiFloatVal($record['etage_id']);
				$etage_code = toHTML(trim($record['etage_code']));
				$etage_texte = toHTML(trim($record['etage_texte']));
				$est_chambre_particuliere = toHTML(trim($record['est_chambre_particuliere']));
				$xmlHISTO = trim($record['xmlnodes']);
				$nbref = 0;
				
				$httpString_tmp .= 
					"\r\n<RECORD 
							oid=\"$oid\" 
							code=\"$code\" 
							texte=\"$texte\" 
							texte_court=\"$texte_court\" 
							code_original=\"$code_original\" 
							etage_id=\"$etage_id\" 
							etage_code=\"$etage_code\" 
							etage_texte=\"$etage_texte\" 
							est_chambre_particuliere=\"$est_chambre_particuliere\"
							nbref=\"+$nbref\" 
							>
						$xmlHISTO
						\r\n</RECORD>";
						
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	return $httpString;
}

function getRecords_compte() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT t_compte.oid, t_compte.code, t_compte.texte, t_compte.texte_court, t_compte.code_original, t_compte.compte_tva_id,  t_compte.taux_tva, 
			CASE WHEN t_compte.taux_tva <> 0 THEN COALESCE(t_compte_tva.code::text || ' '::text || t_compte.taux_tva::text || '%'::text,''::text) ELSE ''::text END
			FROM activite.t_compte
			LEFT JOIN activite.t_compte t_compte_tva ON t_compte.compte_tva_id = t_compte_tva.oid			
			WHERE t_compte.oid > 0  
			ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$code = toHTML(trim($record[1]));
				$texte = toHTML(trim($record[2]));
				$texte_court = toHTML(trim($record[3]));
				$code_original = toHTML(trim($record[4]));
				$compte_tva_id = ctiFloatVal(trim($record[5]));
				$taux_tva = ctiFloatVal(trim($record[6]));
				$tva_texte = toHTML(trim($record[7]));
				$nbref = 0;
				
				$httpString = $httpString . "\r\n<RECORD ";
				$httpString = $httpString . "oid=\"$oid\" ";
				$httpString = $httpString . "code=\"$code\" ";
				$httpString = $httpString . "texte=\"$texte\" ";
				$httpString = $httpString . "texte_court=\"$texte_court\" ";
				$httpString = $httpString . "code_original=\"$code_original\" ";
				$httpString = $httpString . "compte_tva_id=\"$compte_tva_id\" ";
				$httpString = $httpString . "taux_tva=\"$taux_tva\" ";
				$httpString = $httpString . "tva_texte=\"$tva_texte\" ";
				$httpString = $httpString . "nbref=\"+$nbref\" ";
				$httpString = $httpString . " />";

			}
		}

	
	}
	
	return $httpString;
	
	

}

function getRecords_service_rubrique() {

global $database;

	$httpString = "";

	$sqlcmd = "DROP TABLE IF EXISTS w_factures_lignes_c;
			CREATE TEMP TABLE w_factures_lignes_c AS
			SELECT
				t_lieux.service_facturation_id,
				p_factures_lignes_c.rubrique_facturation_id,
				count(*) AS nb_ref
			FROM activite.p_factures_lignes_c
			JOIN activite.t_lieux ON lieu_id = t_lieux.oid
			WHERE t_lieux.service_facturation_id <> 0 AND p_factures_lignes_c.rubrique_facturation_id <> 0
			GROUP BY 1,2;

			SELECT
				COALESCE(t_service_rubrique.oid,-1) AS service_rubrique_id,
				t_services_facturation.code || '-' || t_rubriques_facturation.code AS service_rubrique_code,
				t_services_facturation.texte || '-' || t_rubriques_facturation.texte AS service_rubrique_texte,
				t_services_facturation.texte_court || '-' || t_rubriques_facturation.code AS service_rubrique_texte_court,
				t_services_facturation.code || '-' || t_rubriques_facturation.code AS service_rubrique_code_original,
				w_factures_lignes_c.service_facturation_id,
				t_services_facturation.code || '-' || t_services_facturation.texte AS service_facturation_texte,
				w_factures_lignes_c.rubrique_facturation_id,
				t_rubriques_facturation.code || '-' || t_rubriques_facturation.texte AS rubrique_facturation_texte,
				t_service_rubrique.est_compte_force,
				COALESCE(t_compte.oid,0) AS compte_id,
				COALESCE(t_compte.code || '-' || t_compte.texte,'') AS compte_texte,
				COALESCE(t_prestations.oid,0) AS prestation_forcee_id,
				COALESCE(t_prestations.code || '-' || t_prestations.texte,'') AS prestations_texte,
				nb_ref
			FROM w_factures_lignes_c
			JOIN activite.t_services_facturation ON w_factures_lignes_c.service_facturation_id = t_services_facturation.oid
			JOIN activite.t_rubriques_facturation ON w_factures_lignes_c.rubrique_facturation_id = t_rubriques_facturation.oid
			LEFT JOIN activite.t_service_rubrique ON 
				w_factures_lignes_c.service_facturation_id = t_service_rubrique.service_facturation_id AND
				w_factures_lignes_c.rubrique_facturation_id = t_service_rubrique.rubrique_facturation_id
			LEFT JOIN activite.t_compte ON t_compte.oid = t_service_rubrique.compte_id AND t_compte.oid <> 0
			LEFT JOIN activite.t_prestations ON t_prestations.oid = t_service_rubrique.prestation_forcee_id AND t_prestations.oid <> 0
			ORDER BY 2,3";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$code = toHTML(trim($record[1]));
				$texte = toHTML(trim($record[2]));
				$texte_court = "";
				$code_original = "";
				$service_id = $record[5] + 0;
				$service = toHTML(trim($record[6]));
				$rubrique_id = $record[7] + 0;
				$rubrique = toHTML(trim($record[8]));
				$est_compte_force = toHTML(trim($record[9]));
				$compte_id = $record[10] + 0;
				$compte = toHTML(trim($record[11]));
				$prestation_forcee_id = $record[12] + 0;
				$prestation_forcee = toHTML(trim($record[13]));
				$nbref = $record[14] + 0;
				
				if ($est_compte_force == "1") {
					$compte = "$compte (Forcé)";
				}
				
				
				$httpString = $httpString . "\r\n<RECORD ";
				$httpString = $httpString . "oid=\"$oid\" ";
				$httpString = $httpString . "code=\"$code\" ";
				$httpString = $httpString . "texte=\"$texte\" ";
				$httpString = $httpString . "texte_court=\"$texte_court\" ";
				$httpString = $httpString . "code_original=\"$code_original\" ";
				
				$httpString = $httpString . "service=\"$service\" ";
				$httpString = $httpString . "service_id=\"$service_id\" ";
				$httpString = $httpString . "rubrique=\"$rubrique\" ";
				$httpString = $httpString . "rubrique_id=\"$rubrique_id\" ";
				$httpString = $httpString . "est_compte_force=\"$est_compte_force\" ";
				$httpString = $httpString . "compte=\"$compte\" ";
				$httpString = $httpString . "compte_id=\"$compte_id\" ";
				$httpString = $httpString . "prestation_forcee=\"$prestation_forcee\" ";
				$httpString = $httpString . "prestation_forcee_id=\"$prestation_forcee_id\" ";
				
				
				$httpString = $httpString . "nbref=\"+$nbref\" ";
				$httpString = $httpString . " />";

			}
		}

	
	}
	
	return $httpString;
	
	

}



function getRecords_service_rubrique_dotation() {

global $database;

	$httpString = "";

	$sqlcmd = "
			SELECT
                t_service_rubrique_dotation.oid,
                t_service_rubrique_dotation.texte,
                t_service_rubrique_dotation.annee,
                t_service_rubrique_dotation.montant_dotation,
                COALESCE(t_services_facturation_1.code,'') || ' ' || 
                    COALESCE(', '||t_services_facturation_2.code,'') || ' ' || 
                    COALESCE(', '||t_services_facturation_3.code,'') || ' ' || 
                    COALESCE(', '||t_services_facturation_4.code,'') || ' ' || 
                    COALESCE(', '||t_services_facturation_5.code,'') AS service,
                COALESCE(t_rubriques_facturation_1.code) || ' ' || 
                    COALESCE(', '||t_rubriques_facturation_2.code,'') || ' ' || 
                    COALESCE(', '||t_rubriques_facturation_3.code,'') || ' ' || 
                    COALESCE(', '||t_rubriques_facturation_4.code,'') || ' ' || 
                    COALESCE(', '||t_rubriques_facturation_5.code,'') As rubrique,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[1],0) AS service_1_id,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[2],0) AS service_2_id,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[3],0) AS service_3_id,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[4],0) AS service_4_id,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[5],0) AS service_5_id,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[6],0) AS service_6_id,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[7],0) AS service_7_id,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[8],0) AS service_8_id,
                COALESCE(t_service_rubrique_dotation.service_facturation_id_array[9],0) AS service_9_id,

                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[1],0) AS rubrique_1_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[2],0) AS rubrique_2_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[3],0) AS rubrique_3_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[4],0) AS rubrique_4_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[5],0) AS rubrique_5_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[6],0) AS rubrique_6_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[7],0) AS rubrique_7_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[8],0) AS rubrique_8_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[9],0) AS rubrique_9_id,
                COALESCE(t_service_rubrique_dotation.rubrique_facturation_id_array[10],0) AS rubrique_10_id,

                COALESCE(t_to_rubriques_facturation.code) As to_rubrique,
                to_rubrique_facturation_id,
                to_prestation_id
                
			FROM activite.t_service_rubrique_dotation
                LEFT JOIN activite.t_services_facturation t_services_facturation_1 ON t_service_rubrique_dotation.service_facturation_id_array[1] = t_services_facturation_1.oid
                LEFT JOIN activite.t_services_facturation t_services_facturation_2 ON t_service_rubrique_dotation.service_facturation_id_array[2] = t_services_facturation_2.oid
                LEFT JOIN activite.t_services_facturation t_services_facturation_3 ON t_service_rubrique_dotation.service_facturation_id_array[3] = t_services_facturation_3.oid
                LEFT JOIN activite.t_services_facturation t_services_facturation_4 ON t_service_rubrique_dotation.service_facturation_id_array[4] = t_services_facturation_4.oid
                LEFT JOIN activite.t_services_facturation t_services_facturation_5 ON t_service_rubrique_dotation.service_facturation_id_array[5] = t_services_facturation_5.oid
                LEFT JOIN activite.t_rubriques_facturation t_rubriques_facturation_1 ON t_service_rubrique_dotation.rubrique_facturation_id_array[1] = t_rubriques_facturation_1.oid
			    LEFT JOIN activite.t_rubriques_facturation t_rubriques_facturation_2 ON t_service_rubrique_dotation.rubrique_facturation_id_array[2] = t_rubriques_facturation_2.oid
			    LEFT JOIN activite.t_rubriques_facturation t_rubriques_facturation_3 ON t_service_rubrique_dotation.rubrique_facturation_id_array[3] = t_rubriques_facturation_3.oid
			    LEFT JOIN activite.t_rubriques_facturation t_rubriques_facturation_4 ON t_service_rubrique_dotation.rubrique_facturation_id_array[4] = t_rubriques_facturation_4.oid
			    LEFT JOIN activite.t_rubriques_facturation t_rubriques_facturation_5 ON t_service_rubrique_dotation.rubrique_facturation_id_array[5] = t_rubriques_facturation_5.oid
                LEFT JOIN activite.t_rubriques_facturation t_to_rubriques_facturation ON t_service_rubrique_dotation.to_rubrique_facturation_id = t_to_rubriques_facturation.oid AND t_to_rubriques_facturation.oid <> 0
			ORDER BY 2,3";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
				$annee = $record[2] + 0;
				$dotation = $record[3] + 0;
				$service = toHTML(trim($record[4]));
                $rubrique = toHTML(trim($record[5]));
                $service_1_id = $record[6] + 0;
                $service_2_id = $record[7] + 0;
                $service_3_id = $record[8] + 0;
                $service_4_id = $record[9] + 0;
                $service_5_id = $record[10] + 0;
                $service_6_id = $record[11] + 0;
                $service_7_id = $record[12] + 0;
                $service_8_id = $record[13] + 0;
                $service_9_id = $record[14] + 0;
				$rubrique_1_id = $record[15] + 0;
                $rubrique_2_id = $record[16] + 0;
                $rubrique_3_id = $record[17] + 0;
                $rubrique_4_id = $record[18] + 0;
                $rubrique_5_id = $record[19] + 0;
                $rubrique_6_id = $record[20] + 0;
                $rubrique_7_id = $record[21] + 0;
                $rubrique_8_id = $record[22] + 0;
                $rubrique_9_id = $record[23] + 0;
                $rubrique_10_id = $record[24] + 0;
                
                $to_rubrique = toHTML(trim($record[25]));
                $to_rubrique_id = $record[26] + 0;
                $to_prestation_id = $record[27] + 0;
				
				
				$httpString = $httpString . "\r\n<RECORD ";
				$httpString = $httpString . "oid=\"$oid\" ";
				$httpString = $httpString . "texte=\"$texte\" ";
				$httpString = $httpString . "annee=\"$annee\" ";
				$httpString = $httpString . "dotation=\"$dotation\" ";
				
				$httpString = $httpString . "service=\"$service\" ";
                $httpString = $httpString . "rubrique=\"$rubrique\" ";
                
				$httpString = $httpString . "service_1_id=\"$service_1_id\" ";
                $httpString = $httpString . "service_2_id=\"$service_2_id\" ";
                $httpString = $httpString . "service_3_id=\"$service_3_id\" ";
                $httpString = $httpString . "service_4_id=\"$service_4_id\" ";
                $httpString = $httpString . "service_5_id=\"$service_5_id\" ";
                $httpString = $httpString . "service_6_id=\"$service_6_id\" ";
                $httpString = $httpString . "service_7_id=\"$service_7_id\" ";
                $httpString = $httpString . "service_8_id=\"$service_8_id\" ";
                $httpString = $httpString . "service_9_id=\"$service_9_id\" ";
				
				$httpString = $httpString . "rubrique_1_id=\"$rubrique_1_id\" ";
                $httpString = $httpString . "rubrique_2_id=\"$rubrique_2_id\" ";
                $httpString = $httpString . "rubrique_3_id=\"$rubrique_3_id\" ";
                $httpString = $httpString . "rubrique_4_id=\"$rubrique_4_id\" ";
                $httpString = $httpString . "rubrique_5_id=\"$rubrique_5_id\" ";
                $httpString = $httpString . "rubrique_6_id=\"$rubrique_6_id\" ";
                $httpString = $httpString . "rubrique_7_id=\"$rubrique_7_id\" ";
                $httpString = $httpString . "rubrique_8_id=\"$rubrique_8_id\" ";
                $httpString = $httpString . "rubrique_9_id=\"$rubrique_9_id\" ";
                $httpString = $httpString . "rubrique_10_id=\"$rubrique_10_id\" ";
                
                $httpString = $httpString . "to_rubrique=\"$to_rubrique\" ";
                $httpString = $httpString . "to_rubrique_id=\"$to_rubrique_id\" ";
                $httpString = $httpString . "to_prestation_id=\"$to_prestation_id\" ";
                
				$httpString = $httpString . " />";

			}
		}

	
	}
	
	return $httpString;
	
	

}

function getRecords_prestations_c() {

global $database;
	
	$httpString = "";
	
	$sqlcmd = "	SELECT 
					oid as presta_id, 
					code as preste_code, 
					texte as presta_texte, 
					texte_court as presta_texte_court, 
					code_original as presta_code_original, 
					type_valorisation_non_facture, 
					type_ventilation_jour,
					norme as presta_norme
				FROM 
					activite.t_prestations 
				WHERE 
					oid > 0 
					AND oid IN (SELECT oid FROM activite.p_oids WHERE code_table = 'prestation_c') 
				ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record['presta_id'];
				$code = toHTML(trim($record['preste_code']));
				$texte = toHTML(trim($record['presta_texte']));
				$texte_court = toHTML(trim($record['presta_texte_court']));
				$code_original = toHTML(trim($record['presta_code_original']));
				$type_valorisation_non_facture = toHTML(trim($record['type_valorisation_non_facture']));
				$type_ventilation_jour = toHTML(trim($record['type_ventilation_jour']));
				$complement = toHTML(trim($record['presta_norme']));
				$nbref = 0;
				
				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									texte_court=\"$texte_court\" 
									code_original=\"$code_original\" 
									type_valorisation_non_facture=\"$type_valorisation_non_facture\" 
									type_ventilation_jour=\"$type_ventilation_jour\" 
									complement=\"$complement\" 
									nbref=\"+$nbref\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_prestations_h() {

global $database;
	
	$httpString = "";
	
	$sqlcmd = "	SELECT 
					oid as presta_id, 
					code as presta_code, 
					texte as presta_texte, 
					texte_court as presta_texte_court, 
					code_original as presta_code_original, 
					norme as presta_norme
				FROM 
					activite.t_prestations 
				WHERE 
					oid > 0 
					AND oid IN (SELECT oid FROM activite.p_oids WHERE code_table = 'prestation_h') 
				ORDER BY code";
	$result = $database->exec($sqlcmd);
	
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecord();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record['presta_id'];
				$code = toHTML(trim($record['presta_code']));
				$texte = toHTML(trim($record['presta_texte']));
				$texte_court = toHTML(trim($record['presta_texte_court']));
				$code_original = toHTML(trim($record['presta_code_original']));
				$complement = toHTML(trim($record['presta_norme']));
				$nbref = 0;
				
				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									texte_court=\"$texte_court\" 
									code_original=\"$code_original\" 
									complement=\"$complement\" 
									nbref=\"+$nbref\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_rubriques_facturation() {

global $database;
	
	$httpString = "";
	
	$sqlcmd = "	SELECT 
					oid as rub_id, 
					code as rub_code, 
					texte as rub_texte, 
					texte_court as rub_texte_court, 
					code_original as rub_code_original, 
					actes_inclus_public as rub_actes_inclus_public, 
					est_force_etablissement as rub_est_force_etablissement
				FROM 
					activite.t_rubriques_facturation 
				WHERE 
					oid > 0 
					AND oid IN (SELECT oid FROM activite.p_oids WHERE code_table = 'rubrique_c') 
				ORDER BY code";
	$result = $database->exec($sqlcmd);
			
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['rub_id'];
				$code = toHTML(trim($record['rub_code']));
				$texte = toHTML(trim($record['rub_texte']));
				$texte_court = toHTML(trim($record['rub_texte_court']));
				$code_original = toHTML(trim($record['rub_code_original']));
				$actes_inclus_public = toHTML(trim($record['rub_actes_inclus_public']));
				$est_force_etablissement = toHTML(trim($record['rub_est_force_etablissement']));
				$nbref = 0;
				
				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									texte_court=\"$texte_court\" 
									code_original=\"$code_original\" 
									actes_inclus_public=\"$actes_inclus_public\" 
									est_force_etablissement=\"$est_force_etablissement\" 
									nbref=\"+$nbref\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_types_tiers_payant() {

global $database;
	
	$httpString = "";
	
	$sqlcmd = "	SELECT 
					code as tp_id, 
					code as tp_code, 
					texte as tp_texte, 
					texte_court as tp_texte_court 
				FROM 
					activite.t_types_tiers_payant 
				ORDER BY code";
	$result = $database->exec($sqlcmd);
	
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();
			
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record['tp_id'];
				$code = toHTML(trim($record['tp_code']));
				$texte = toHTML(trim($record['tp_texte']));
				$texte_court = toHTML(trim($record['tp_texte_court']));
				$nbref = 0;
				
				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									texte_court=\"$texte_court\" 
									nbref=\"+$nbref\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_tiers_payant() {

global $database;

	$httpString = "";

	$httpString_tmp = "";
	
	$sqlcmd = "	SELECT 
					t_tiers_payant.oid as tp_id, 
					t_tiers_payant.code as tp_code, 
					t_tiers_payant.texte as tp_texte, 
					t_tiers_payant.texte_court as tp_texte_court, 
					t_tiers_payant.code_original as tp_code_original, 
					t_types_tiers_payant.texte_court as type_tp_texte_court, 
					COALESCE(t_tiers_payant.grand_regime_id, 0) as grand_regime_id,
					CASE WHEN t_tiers_payant.grand_regime_id > 0 THEN t_grands_regimes.code || ' '::text || t_grands_regimes.texte_court ELSE '' END as grand_regime_texte
				FROM 
					activite.t_tiers_payant
					JOIN activite.t_types_tiers_payant ON t_tiers_payant.type_tiers_payant = t_types_tiers_payant.code
					LEFT JOIN base.t_grands_regimes ON t_tiers_payant.grand_regime_id = t_grands_regimes.oid
				WHERE 
					t_tiers_payant.oid > 0 AND  
					t_tiers_payant.oid IN (SELECT oid FROM activite.p_oids WHERE code_table = 'tiers_payant') 
				ORDER BY t_tiers_payant.code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['tp_id'];
				$code = toHTML(trim($record['tp_code']));
				$texte = toHTML(trim($record['tp_texte']));
				$texte_court = toHTML(trim($record['tp_texte_court']));
				$code_original = toHTML(trim($record['tp_code_original']));
				$type_tiers_payant = toHTML(trim($record['type_tp_texte_court']));
				$grand_regime_id = $record['grand_regime_id'];
				$grand_regime = toHTML(trim($record['grand_regime_texte']));
				$nbref = 0;
				
				$httpString_tmp .= "\r\n<RECORD 
											oid=\"$oid\" 
											code=\"$code\" 
											texte=\"$texte\" 
											texte_court=\"$texte_court\" 
											code_original=\"$code_original\" 
											type_tiers_payant=\"$type_tiers_payant\" 
											grand_regime_id=\"$grand_regime_id\" 
											grand_regime=\"$grand_regime\" 
											nbref=\"+$nbref\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	
	$httpString .= getCombo_grands_regimes();
	return $httpString;
}

function getRecords_ccam_regroupements() {

global $database;
	
	$httpString = "";
	
	$sqlcmd = "	SELECT 
					oid as ccam_id, 
					code as ccam_code, 
					texte as ccam_texte,
					texte_court as ccam_texte_court 
				FROM base.t_ccam_regroupements 
				WHERE oid > 0 
				ORDER BY code";
	$result = $database->exec($sqlcmd);
			
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecord();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record['ccam_id'];
				$code = toHTML(trim($record['ccam_code']));
				$texte = toHTML(trim($record['ccam_texte']));
				$texte_court = toHTML(trim($record['ccam_texte_court']));
				
				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									texte_court=\"$texte_court\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_actes() {

global $database;
	
	$httpString = "";
	
	$httpString_tmp = "";
	
	$sqlcmd = "	SELECT 
					t_actes.oid as act_id, 
					t_actes.code as act_code, 
					t_actes.texte as act_texte, 
					t_ccam_regroupements_1.code as regroupement_1, 
					t_ccam_regroupements_4.code as regroupement_4 
				FROM 
					base.t_actes, 
					base.t_ccam_regroupements t_ccam_regroupements_1, 
					base.t_ccam_regroupements t_ccam_regroupements_4 
				WHERE
					t_actes.oid > 0 
					AND t_actes.oid IN (SELECT oid FROM activite.p_oids WHERE code_table = 'acte_h') 
					AND ccam_regroupement_id_1 = t_ccam_regroupements_1.oid 
					AND ccam_regroupement_id_4 = t_ccam_regroupements_4.oid 
					AND nomenclature IN ('CCAM', 'NGAP') 
				ORDER BY t_actes.code";
	$result = $database->exec($sqlcmd);
	
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecord();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record['act_id'];
				$code = toHTML(trim($record['act_code']));
				$texte = toHTML(trim($record['act_texte']));
				$regroupement_1 = toHTML(trim($record['regroupement_1']));
				$regroupement_4 = toHTML(trim($record['regroupement_4']));
				$nbref = 0;
				
				$httpString_tmp .= "\r\n<RECORD 
										oid=\"$oid\" 
										code=\"$code\" 
										texte=\"$texte\" 
										regroupement_1=\"$regroupement_1\" 
										regroupement_4=\"$regroupement_4\" 
										nbref=\"+$nbref\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	$httpString .= $httpString_tmp;
	
	return $httpString;
}

function getRecords_ghs() {

global $database;
	
	$httpString = "";
	
	$httpString_tmp = "";
	
	$sqlcmd = "	SELECT 
					ghs_id, 
					ghs_code, 
					ghs_texte
				FROM 
					activite.t_ghs_c 
				WHERE 
					ghs_id > 0 
					AND ghs_id IN (SELECT oid FROM activite.p_oids WHERE code_table = 'ghs') 
				ORDER BY ghs_code";
	$result = $database->exec($sqlcmd);
	
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecord();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record['ghs_id'];
				$code = toHTML(trim($record['ghs_code']));
				$texte = toHTML(trim($record['ghs_texte']));
				
				$httpString_tmp .= "\r\n<RECORD 
										oid=\"$oid\" 
										code=\"$code\" 
										texte=\"$texte\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	
	return $httpString;
}

function getRecords_specialites_internes_medecin() {

global $database;
	
	$httpString = "";
	
	$sqlcmd = "	SELECT 
					oid as spe_id, 
					code as spe_code, 
					texte as spe_texte, 
					texte_court as spe_texte_court, 
					subview.count as nb_ref 
				FROM 
					base.t_specialites_medecin 
					LEFT JOIN 
					(
						SELECT specialite_id, count(*) as count 
						FROM base.t_medecins 
						GROUP BY specialite_id
					) subview ON (t_specialites_medecin.oid = subview.specialite_id) 
				WHERE
					oid > 0 
				ORDER BY code";
	$result = $database->exec($sqlcmd);
	
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecord();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record['spe_id'];
				$code = toHTML(trim($record['spe_code']));
				$texte = toHTML(trim($record['spe_texte']));
				$texte_court = toHTML(trim($record['spe_texte_court']));
				
				$nbref = $record['nb_ref'] + 0;
				
				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									texte_court=\"$texte_court\" 
									nbref=\"+$nbref\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_medecins_administratifs() {

global $database;

	$httpString = "";
	
	$httpString_tmp = "";

	$sqlcmd = "	SELECT 
					t_medecins_administratifs.oid as med_id, 
					t_medecins_administratifs.code as med_code, 
					t_medecins_administratifs.nom as med_nom, 
					t_medecins_administratifs.prenom as med_prenom, 
					t_medecins.numero_ordre as med_numero_ordre, 
					COALESCE(t_specialites_medecin.code || ' '::text || t_specialites_medecin.texte,'') as spe_texte, 
					CASE WHEN t_medecins_administratifs.medecin_id > 0 THEN t_medecins.nom || ' '::text || t_medecins.prenom || ' ('::text || t_medecins.code || ')'::text ELSE ''::text END as med_ref, 
					t_medecins_administratifs.code_original as med_code_original, 
					t_medecins_administratifs.est_medecin_salarie as med_est_medecin_salarie, 
					t_medecins_administratifs.kiwee_sigems_honpatcpt as med_kiwee_sigems_honpatcpt, 
					t_medecins_administratifs.kiwee_sigems_honpatcptdep as med_kiwee_sigems_honpatcptdep, 
					CASE WHEN t_medecins_administratifs.est_medecin_salarie = '1' THEN 'Salarié' ELSE '' END AS complement
				FROM 
					activite.t_medecins_administratifs  
					JOIN base.t_medecins ON medecin_id = t_medecins.oid
					LEFT JOIN base.t_specialites_medecin ON t_medecins.specialite_id = t_specialites_medecin.oid AND t_medecins.specialite_id <> 0
				WHERE 
					t_medecins_administratifs.oid > 0 AND
					t_medecins_administratifs.oid IN (SELECT oid FROM activite.p_oids WHERE code_table = 'medecin_s' OR code_table = 'medecin_x') 
				ORDER BY t_medecins_administratifs.nom, t_medecins_administratifs.prenom";
	$result = $database->exec($sqlcmd);
			
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['med_id'];
				$code = toHTML(trim($record['med_code']));
				$nom = toHTML(trim($record['med_nom']));
				$prenom = toHTML(trim($record['med_prenom']));
				$no_adeli = toHTML(trim($record['med_numero_ordre']));
				$specialite = toHTML(trim($record['spe_texte']));
				$medecin_reference = toHTML(trim($record['med_ref']));
				$code_original = toHTML(trim($record['med_code_original']));
				$est_medecin_salarie = toHTML(trim($record['med_est_medecin_salarie']));
				$kiwee_sigems_honpatcpt = toHTML(trim($record['med_kiwee_sigems_honpatcpt']));
				$kiwee_sigems_honpatcptdep = toHTML(trim($record['med_kiwee_sigems_honpatcptdep']));
				$complement = toHTML(trim($record['complement']));
				$nbref = 0;
				
				$httpString_tmp .= "\r\n<RECORD 
											oid=\"$oid\" 
											code=\"$code\" 
											nom=\"$nom\" 
											prenom=\"$prenom\" 
											no_adeli=\"$no_adeli\" 
											specialite=\"$specialite\" 
											medecin_reference=\"$medecin_reference\" 
											code_original=\"$code_original\" 
											est_medecin_salarie=\"$est_medecin_salarie\" 
											kiwee_sigems_honpatcpt=\"$kiwee_sigems_honpatcpt\" 
											kiwee_sigems_honpatcptdep=\"$kiwee_sigems_honpatcptdep\" 
											complement=\"$complement\" 
											nbref=\"+$nbref\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	$httpString .= $httpString_tmp;
	
	return $httpString;
}


function getRecords_medecins_traitants_administratifs() {

global $database;

	$httpString = "";
	
	$httpString_tmp = "";

	$sqlcmd = "	SELECT 
					t_medecins_traitants_administratifs.oid as med_id, 
					t_medecins_traitants_administratifs.code as med_code, 
					t_medecins_traitants_administratifs.nom as med_nom, 
					t_medecins_traitants_administratifs.prenom as med_prenom, 
					t_medecins_traitants_administratifs.no_adeli as med_numero_ordre, 
					t_medecins_traitants_administratifs.code_original as med_code_original, 
					t_medecins_traitants_administratifs.no_rpps as no_rpps ,
					COALESCE(t_codes_postaux.code,'') as code_postal,
					t_medecins_traitants_administratifs.ville as ville, 
					t_medecins_traitants_administratifs.adresse as adresse 
				FROM 
					activite.t_medecins_traitants_administratifs  
					LEFT JOIN base.t_codes_postaux ON code_postal_id = t_codes_postaux.oid and code_postal_id != 0
				WHERE 
					t_medecins_traitants_administratifs.oid > 0 AND
					t_medecins_traitants_administratifs.oid IN (SELECT oid FROM activite.p_oids WHERE code_table = 'medecin_t') 
				ORDER BY t_medecins_traitants_administratifs.nom, t_medecins_traitants_administratifs.prenom";
	$result = $database->exec($sqlcmd);
			
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['med_id'];
				$code = toHTML(trim($record['med_code']));
				$nom = toHTML(trim($record['med_nom']));
				$prenom = toHTML(trim($record['med_prenom']));
				$no_adeli = toHTML(trim($record['med_numero_ordre']));
				$medecin_reference = toHTML(trim($record['med_ref']));
				$code_original = toHTML(trim($record['med_code_original']));
				$no_rpps = toHTML(trim($record['no_rpps']));
				$code_postal = toHTML(trim($record['code_postal']));
				$ville = toHTML(trim($record['ville']));
				$adresse = toHTML(trim($record['adresse']));
				$nbref = 0;
				
				$httpString_tmp .= "\r\n<RECORD 
											oid=\"$oid\" 
											code=\"$code\" 
											nom=\"$nom\" 
											prenom=\"$prenom\" 
											no_adeli=\"$no_adeli\" 
											code_original=\"$code_original\" 
											no_rpps=\"$no_rpps\" 
											code_postal=\"$code_postal\" 
											ville=\"$ville\" 
											adresse=\"$adresse\" 
											nbref=\"+$nbref\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	$httpString .= $httpString_tmp;
	
	return $httpString;
}


function getRecords_forme_activite() {

global $database;

	$httpString = "";

	$sqlcmd = "
			SELECT
				t_forme_activite.oid,
				t_forme_activite.code,
				t_forme_activite.texte,
                t_forme_activite.texte_court
			FROM activite.t_forme_activite
            WHERE oid <> 0
			ORDER BY 2,3";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$code = toHTML(trim($record[1]));
				$texte = toHTML(trim($record[2]));
				$texte_court = toHTML(trim($record[3]));
				$code_original = $code ;
				
				
				$httpString = $httpString . "\r\n<RECORD ";
				$httpString = $httpString . "oid=\"$oid\" ";
				$httpString = $httpString . "code=\"$code\" ";
				$httpString = $httpString . "texte=\"$texte\" ";
				$httpString = $httpString . "texte_court=\"$texte_court\" ";
				$httpString = $httpString . "code_original=\"$code_original\" ";
				
				
				$httpString = $httpString . "nbref=\"1\" ";
				$httpString = $httpString . " />";

			}
		}

	
	}
	
	return $httpString;
	
	

}


function getRecords_forme_activite_rule() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT t_forme_activite_rule.oid as rule_id, 
				t_forme_activite_rule.code as rule_code, 
				t_forme_activite_rule.texte as rule_texte, 
				t_forme_activite_rule.priorite as rule_priorite, 
				t_forme_activite_rule.forme_activite_id as forme_activite_id, 
				t_forme_activite.code || ' '::text || t_forme_activite.texte as cle_texte,

				CASE WHEN liste_type_sejour <> '' THEN ' Typ:'::text || liste_type_sejour ELSE ''::text END ||
				CASE WHEN liste_specialite <> '' THEN ' Spé:'::text || liste_specialite ELSE ''::text END ||
				CASE WHEN liste_medecin <> '' THEN ' Med:'::text ||  liste_medecin ELSE ''::text END ||
                CASE WHEN liste_unite_fonctionnelle <> '' THEN ' UF (S):'::text || liste_unite_fonctionnelle ELSE ''::text END ||
                CASE WHEN liste_service <> '' THEN ' Ser (S):'::text || liste_service ELSE ''::text END ||
				CASE WHEN liste_etage <> '' THEN ' Eta (S):'::text || liste_etage ELSE ''::text END ||
				CASE WHEN liste_unite_fonctionnelle_entree <> '' THEN ' UF (E):'::text || liste_unite_fonctionnelle_entree ELSE ''::text END ||
                CASE WHEN liste_service_entree <> '' THEN ' Ser (E):'::text || liste_service_entree ELSE ''::text END ||
				CASE WHEN liste_etage_entree <> '' THEN ' Eta (E):'::text || liste_etage_entree ELSE ''::text END ||
				CASE WHEN liste_acte <> '' THEN ' Act:'::text || liste_acte ELSE ''::text END ||
				CASE WHEN liste_prestation <> '' THEN ' Presta:'::text || liste_prestation ELSE ''::text END ||
                CASE WHEN liste_rubrique <> '' THEN ' Rub:'::text || liste_rubrique ELSE ''::text END ||
				CASE WHEN liste_ghm <> '' THEN ' GHM:'::text || liste_ghm ELSE ''::text END ||
				CASE WHEN liste_sauf_ghm <> '' THEN ' GHMx:'::text || liste_sauf_ghm ELSE ''::text END ||
				CASE WHEN liste_diagnostic_principal <> '' THEN ' DP:'::text || liste_diagnostic_principal ELSE ''::text END ||
				CASE WHEN liste_diagnostic_relie <> '' THEN ' DR:'::text || liste_diagnostic_relie ELSE ''::text END ||
				CASE WHEN liste_diagnostic_associe <> '' THEN ' DA:'::text ||  liste_diagnostic_associe ELSE ''::text END ||
				CASE WHEN liste_finess <> '' THEN ' Ets:'::text || liste_finess ELSE ''::text END as rule_resume,

				liste_type_sejour,
				liste_specialite,
				liste_medecin,
				liste_unite_fonctionnelle,
                liste_etage,
                liste_service,
				liste_unite_fonctionnelle_entree,
                liste_etage_entree,
                liste_service_entree,
				liste_acte,
                liste_prestation,
				liste_rubrique,
				liste_ghm,
				liste_sauf_ghm,
				liste_diagnostic_principal,
				liste_diagnostic_relie,
				liste_diagnostic_associe,
				liste_finess
			FROM activite.t_forme_activite_rule
			LEFT JOIN activite.t_forme_activite ON t_forme_activite_rule.forme_activite_id = t_forme_activite.oid
			WHERE t_forme_activite_rule.oid > 0
			ORDER BY t_forme_activite_rule.priorite, t_forme_activite.code, t_forme_activite_rule.code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['rule_id'];
				$code = toHTML(trim($record['rule_code']));
				$texte = toHTML(trim($record['rule_texte']));
				$priorite = toHTML(trim($record['rule_priorite']));
				$forme_activite_id = toHTML(trim($record['forme_activite_id']));
				$forme_activite_texte = toHTML(trim($record['cle_texte']));
				
				$rule_resume = toHTML(trim($record['rule_resume']));
				
				$liste_type_sejour = toHTML(trim($record['liste_type_sejour']));
                $liste_specialite = toHTML(trim($record['liste_specialite']));
				$liste_medecin = toHTML(trim($record['liste_medecin']));
				$liste_unite_fonctionnelle = toHTML(trim($record['liste_unite_fonctionnelle']));
                $liste_service = toHTML(trim($record['liste_service']));
                $liste_etage = toHTML(trim($record['liste_etage']));
				$liste_unite_fonctionnelle_entree = toHTML(trim($record['liste_unite_fonctionnelle_entree']));
                $liste_service_entree = toHTML(trim($record['liste_service_entree']));
                $liste_etage_entree = toHTML(trim($record['liste_etage_entree']));
				$liste_acte = toHTML(trim($record['liste_acte']));
				$liste_prestation = toHTML(trim($record['liste_prestation']));
                $liste_rubrique = toHTML(trim($record['liste_rubrique']));
				$liste_ghm = toHTML(trim($record['liste_ghm']));
				$liste_sauf_ghm = toHTML(trim($record['liste_sauf_ghm']));
				$liste_diagnostic_principal = toHTML(trim($record['liste_diagnostic_principal']));
				$liste_diagnostic_relie = toHTML(trim($record['liste_diagnostic_relie']));
				$liste_diagnostic_associe = toHTML(trim($record['liste_diagnostic_associe']));
				$liste_finess = toHTML(trim($record['liste_finess']));
				
				$httpString .= "\r\n<RECORD 
										oid=\"$oid\" 
										code=\"$code\" 
										texte=\"$texte\" 
										priorite=\"$priorite\" 
										forme_activite_id=\"$forme_activite_id\" 
										forme_activite_texte=\"$forme_activite_texte\" 
										
										rule_resume=\"$rule_resume\" 
										
										liste_type_sejour=\"$liste_type_sejour\" 
                                        liste_specialite=\"$liste_specialite\" 
										liste_medecin=\"$liste_medecin\" 
										liste_unite_fonctionnelle=\"$liste_unite_fonctionnelle\" 
                                        liste_service=\"$liste_service\" 
                                        liste_etage=\"$liste_etage\" 
										liste_unite_fonctionnelle_entree=\"$liste_unite_fonctionnelle_entree\" 
                                        liste_service_entree=\"$liste_service_entree\" 
                                        liste_etage_entree=\"$liste_etage_entree\" 
										liste_acte=\"$liste_acte\" 
										liste_prestation=\"$liste_prestation\" 
                                        liste_rubrique=\"$liste_rubrique\" 
										liste_ghm=\"$liste_ghm\" 
										liste_sauf_ghm=\"$liste_sauf_ghm\" 
										liste_diagnostic_principal=\"$liste_diagnostic_principal\" 
										liste_diagnostic_relie=\"$liste_diagnostic_relie\" 
										liste_diagnostic_associe=\"$liste_diagnostic_associe\" 
										liste_finess=\"$liste_finess\" />";
			}
		}
	}
	return $httpString;
}



function getRecords_filiere_soin() {

global $database;

	$httpString = "";

	$sqlcmd = "
			SELECT
				t_filiere_soin.oid,
				t_filiere_soin.code,
				t_filiere_soin.texte,
                t_filiere_soin.texte_court
			FROM activite.t_filiere_soin
            WHERE oid <> 0
			ORDER BY 2,3";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$code = toHTML(trim($record[1]));
				$texte = toHTML(trim($record[2]));
				$texte_court = toHTML(trim($record[3]));
				$code_original = $code ;
				
				
				$httpString = $httpString . "\r\n<RECORD ";
				$httpString = $httpString . "oid=\"$oid\" ";
				$httpString = $httpString . "code=\"$code\" ";
				$httpString = $httpString . "texte=\"$texte\" ";
				$httpString = $httpString . "texte_court=\"$texte_court\" ";
				$httpString = $httpString . "code_original=\"$code_original\" ";
				
				
				$httpString = $httpString . "nbref=\"1\" ";
				$httpString = $httpString . " />";

			}
		}

	
	}
	
	return $httpString;
	
	

}


function getRecords_filiere_soin_rule() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT t_filiere_soin_rule.oid as rule_id, 
				t_filiere_soin_rule.code as rule_code, 
				t_filiere_soin_rule.texte as rule_texte, 
				t_filiere_soin_rule.priorite as rule_priorite, 
				t_filiere_soin_rule.filiere_soin_id as filiere_soin_id, 
				t_filiere_soin.code || ' '::text || t_filiere_soin.texte as cle_texte,

				CASE WHEN liste_type_sejour <> '' THEN ' Typ:'::text || liste_type_sejour ELSE ''::text END ||
				CASE WHEN liste_specialite <> '' THEN ' Spé:'::text || liste_specialite ELSE ''::text END ||
				CASE WHEN liste_medecin <> '' THEN ' Med:'::text ||  liste_medecin ELSE ''::text END ||
                CASE WHEN liste_unite_fonctionnelle <> '' THEN ' UF (S):'::text || liste_unite_fonctionnelle ELSE ''::text END ||
                CASE WHEN liste_service <> '' THEN ' Ser (S):'::text || liste_service ELSE ''::text END ||
				CASE WHEN liste_etage <> '' THEN ' Eta (S):'::text || liste_etage ELSE ''::text END ||
				CASE WHEN liste_unite_fonctionnelle_entree <> '' THEN ' UF (E):'::text || liste_unite_fonctionnelle_entree ELSE ''::text END ||
                CASE WHEN liste_service_entree <> '' THEN ' Ser (E):'::text || liste_service_entree ELSE ''::text END ||
				CASE WHEN liste_etage_entree <> '' THEN ' Eta (E):'::text || liste_etage_entree ELSE ''::text END ||
				CASE WHEN liste_acte <> '' THEN ' Act:'::text || liste_acte ELSE ''::text END ||
				CASE WHEN liste_prestation <> '' THEN ' Presta:'::text || liste_prestation ELSE ''::text END ||
                CASE WHEN liste_rubrique <> '' THEN ' Rub:'::text || liste_rubrique ELSE ''::text END ||
				CASE WHEN liste_ghm <> '' THEN ' GHM:'::text || liste_ghm ELSE ''::text END ||
				CASE WHEN liste_sauf_ghm <> '' THEN ' GHMx:'::text || liste_sauf_ghm ELSE ''::text END ||
				CASE WHEN liste_diagnostic_principal <> '' THEN ' DP:'::text || liste_diagnostic_principal ELSE ''::text END ||
				CASE WHEN liste_diagnostic_relie <> '' THEN ' DR:'::text || liste_diagnostic_relie ELSE ''::text END ||
				CASE WHEN liste_diagnostic_associe <> '' THEN ' DA:'::text ||  liste_diagnostic_associe ELSE ''::text END ||
				CASE WHEN liste_finess <> '' THEN ' Ets:'::text || liste_finess ELSE ''::text END as rule_resume,

				liste_type_sejour,
				liste_specialite,
				liste_medecin,
				liste_unite_fonctionnelle,
                liste_etage,
                liste_service,
				liste_unite_fonctionnelle_entree,
                liste_etage_entree,
                liste_service_entree,
				liste_acte,
                liste_prestation,
				liste_rubrique,
				liste_ghm,
				liste_sauf_ghm,
				liste_diagnostic_principal,
				liste_diagnostic_relie,
				liste_diagnostic_associe,
				liste_finess
			FROM activite.t_filiere_soin_rule
			LEFT JOIN activite.t_filiere_soin ON t_filiere_soin_rule.filiere_soin_id = t_filiere_soin.oid
			WHERE t_filiere_soin_rule.oid > 0
			ORDER BY t_filiere_soin_rule.priorite, t_filiere_soin.code, t_filiere_soin_rule.code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['rule_id'];
				$code = toHTML(trim($record['rule_code']));
				$texte = toHTML(trim($record['rule_texte']));
				$priorite = toHTML(trim($record['rule_priorite']));
				$filiere_soin_id = toHTML(trim($record['filiere_soin_id']));
				$filiere_soin_texte = toHTML(trim($record['cle_texte']));
				
				$rule_resume = toHTML(trim($record['rule_resume']));
				
				$liste_type_sejour = toHTML(trim($record['liste_type_sejour']));
                $liste_specialite = toHTML(trim($record['liste_specialite']));
				$liste_medecin = toHTML(trim($record['liste_medecin']));
				$liste_unite_fonctionnelle = toHTML(trim($record['liste_unite_fonctionnelle']));
                $liste_service = toHTML(trim($record['liste_service']));
                $liste_etage = toHTML(trim($record['liste_etage']));
				$liste_unite_fonctionnelle_entree = toHTML(trim($record['liste_unite_fonctionnelle_entree']));
                $liste_service_entree = toHTML(trim($record['liste_service_entree']));
                $liste_etage_entree = toHTML(trim($record['liste_etage_entree']));
				$liste_acte = toHTML(trim($record['liste_acte']));
				$liste_prestation = toHTML(trim($record['liste_prestation']));
                $liste_rubrique = toHTML(trim($record['liste_rubrique']));
				$liste_ghm = toHTML(trim($record['liste_ghm']));
				$liste_sauf_ghm = toHTML(trim($record['liste_sauf_ghm']));
				$liste_diagnostic_principal = toHTML(trim($record['liste_diagnostic_principal']));
				$liste_diagnostic_relie = toHTML(trim($record['liste_diagnostic_relie']));
				$liste_diagnostic_associe = toHTML(trim($record['liste_diagnostic_associe']));
				$liste_finess = toHTML(trim($record['liste_finess']));
				
				$httpString .= "\r\n<RECORD 
										oid=\"$oid\" 
										code=\"$code\" 
										texte=\"$texte\" 
										priorite=\"$priorite\" 
										filiere_soin_id=\"$filiere_soin_id\" 
										filiere_soin_texte=\"$filiere_soin_texte\" 
										
										rule_resume=\"$rule_resume\" 
										
										liste_type_sejour=\"$liste_type_sejour\" 
                                        liste_specialite=\"$liste_specialite\" 
										liste_medecin=\"$liste_medecin\" 
										liste_unite_fonctionnelle=\"$liste_unite_fonctionnelle\" 
                                        liste_service=\"$liste_service\" 
                                        liste_etage=\"$liste_etage\" 
										liste_unite_fonctionnelle_entree=\"$liste_unite_fonctionnelle_entree\" 
                                        liste_service_entree=\"$liste_service_entree\" 
                                        liste_etage_entree=\"$liste_etage_entree\" 
										liste_acte=\"$liste_acte\" 
										liste_prestation=\"$liste_prestation\" 
                                        liste_rubrique=\"$liste_rubrique\" 
										liste_ghm=\"$liste_ghm\" 
										liste_sauf_ghm=\"$liste_sauf_ghm\" 
										liste_diagnostic_principal=\"$liste_diagnostic_principal\" 
										liste_diagnostic_relie=\"$liste_diagnostic_relie\" 
										liste_diagnostic_associe=\"$liste_diagnostic_associe\" 
										liste_finess=\"$liste_finess\" />";
			}
		}
	}
	return $httpString;
}


function getRecords_pole_rule() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT t_pole_rule.oid as rule_id, 
				t_pole_rule.code as rule_code, 
				t_pole_rule.texte as rule_texte, 
				t_pole_rule.priorite as rule_priorite, 
				t_pole_rule.pole_id as pole_id, 
				t_pole.code || ' '::text || t_pole.texte as cle_texte,

				CASE WHEN liste_type_sejour <> '' THEN ' Typ:'::text || liste_type_sejour ELSE ''::text END ||
				CASE WHEN liste_specialite <> '' THEN ' Spé:'::text || liste_specialite ELSE ''::text END ||
				CASE WHEN liste_medecin <> '' THEN ' Med:'::text ||  liste_medecin ELSE ''::text END ||
                CASE WHEN liste_unite_fonctionnelle <> '' THEN ' UF:'::text || liste_unite_fonctionnelle ELSE ''::text END ||
                CASE WHEN liste_service <> '' THEN ' Ser:'::text || liste_service ELSE ''::text END ||
				CASE WHEN liste_etage <> '' THEN ' Eta:'::text || liste_etage ELSE ''::text END ||
				CASE WHEN liste_acte <> '' THEN ' Act:'::text || liste_acte ELSE ''::text END ||
				CASE WHEN liste_prestation <> '' THEN ' Presta:'::text || liste_prestation ELSE ''::text END ||
                CASE WHEN liste_rubrique <> '' THEN ' Rub:'::text || liste_rubrique ELSE ''::text END ||
				CASE WHEN liste_ghm <> '' THEN ' GHM:'::text || liste_ghm ELSE ''::text END ||
				CASE WHEN liste_sauf_ghm <> '' THEN ' GHMx:'::text || liste_sauf_ghm ELSE ''::text END ||
				CASE WHEN liste_diagnostic_principal <> '' THEN ' DP:'::text || liste_diagnostic_principal ELSE ''::text END ||
				CASE WHEN liste_diagnostic_relie <> '' THEN ' DR:'::text || liste_diagnostic_relie ELSE ''::text END ||
				CASE WHEN liste_diagnostic_associe <> '' THEN ' DA:'::text ||  liste_diagnostic_associe ELSE ''::text END ||
				CASE WHEN liste_finess <> '' THEN ' Ets:'::text || liste_finess ELSE ''::text END as rule_resume,

				liste_type_sejour,
				liste_specialite,
				liste_medecin,
				liste_unite_fonctionnelle,
                liste_etage,
                liste_service,
				liste_acte,
                liste_prestation,
				liste_rubrique,
				liste_ghm,
				liste_sauf_ghm,
				liste_diagnostic_principal,
				liste_diagnostic_relie,
				liste_diagnostic_associe,
				liste_finess
			FROM activite.t_pole_rule
			LEFT JOIN base.t_pole ON t_pole_rule.pole_id = t_pole.oid
			WHERE t_pole_rule.oid > 0
			ORDER BY t_pole_rule.priorite, t_pole.code, t_pole_rule.code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['rule_id'];
				$code = toHTML(trim($record['rule_code']));
				$texte = toHTML(trim($record['rule_texte']));
				$priorite = toHTML(trim($record['rule_priorite']));
				$pole_id = toHTML(trim($record['pole_id']));
				$pole_texte = toHTML(trim($record['cle_texte']));
				
				$rule_resume = toHTML(trim($record['rule_resume']));
				
				$liste_type_sejour = toHTML(trim($record['liste_type_sejour']));
                $liste_specialite = toHTML(trim($record['liste_specialite']));
				$liste_medecin = toHTML(trim($record['liste_medecin']));
				$liste_unite_fonctionnelle = toHTML(trim($record['liste_unite_fonctionnelle']));
                $liste_service = toHTML(trim($record['liste_service']));
                $liste_etage = toHTML(trim($record['liste_etage']));
				$liste_acte = toHTML(trim($record['liste_acte']));
				$liste_prestation = toHTML(trim($record['liste_prestation']));
                $liste_rubrique = toHTML(trim($record['liste_rubrique']));
				$liste_ghm = toHTML(trim($record['liste_ghm']));
				$liste_sauf_ghm = toHTML(trim($record['liste_sauf_ghm']));
				$liste_diagnostic_principal = toHTML(trim($record['liste_diagnostic_principal']));
				$liste_diagnostic_relie = toHTML(trim($record['liste_diagnostic_relie']));
				$liste_diagnostic_associe = toHTML(trim($record['liste_diagnostic_associe']));
				$liste_finess = toHTML(trim($record['liste_finess']));
				
				$httpString .= "\r\n<RECORD 
										oid=\"$oid\" 
										code=\"$code\" 
										texte=\"$texte\" 
										priorite=\"$priorite\" 
										pole_id=\"$pole_id\" 
										pole_texte=\"$pole_texte\" 
										
										rule_resume=\"$rule_resume\" 
										
										liste_type_sejour=\"$liste_type_sejour\" 
                                        liste_specialite=\"$liste_specialite\" 
										liste_medecin=\"$liste_medecin\" 
										liste_unite_fonctionnelle=\"$liste_unite_fonctionnelle\" 
                                        liste_service=\"$liste_service\" 
                                        liste_etage=\"$liste_etage\" 
										liste_acte=\"$liste_acte\" 
										liste_prestation=\"$liste_prestation\" 
                                        liste_rubrique=\"$liste_rubrique\" 
										liste_ghm=\"$liste_ghm\" 
										liste_sauf_ghm=\"$liste_sauf_ghm\" 
										liste_diagnostic_principal=\"$liste_diagnostic_principal\" 
										liste_diagnostic_relie=\"$liste_diagnostic_relie\" 
										liste_diagnostic_associe=\"$liste_diagnostic_associe\" 
										liste_finess=\"$liste_finess\" />";
			}
		}
	}
	return $httpString;
}


function getRecords_compte_analytique_rule() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT t_compte_produit_analytique_rule.oid as rule_id, 
				t_compte_produit_analytique_rule.code as rule_code, 
				t_compte_produit_analytique_rule.texte as rule_texte, 
				t_compte_produit_analytique_rule.priorite as rule_priorite, 
				t_compte_produit_analytique_rule.compte_produit_analytique_id as compte_produit_analytique_id, 
				COALESCE(t_compte.code || ' '::text || t_compte.texte,'') as cle_texte,

				CASE WHEN liste_type_sejour <> '' THEN ' Typ:'::text || liste_type_sejour ELSE ''::text END ||
				CASE WHEN liste_specialite <> '' THEN ' Spé:'::text || liste_specialite ELSE ''::text END ||
				CASE WHEN liste_medecin <> '' THEN ' Med:'::text ||  liste_medecin ELSE ''::text END ||
				CASE WHEN liste_ghm <> '' THEN ' GHM:'::text || liste_ghm ELSE ''::text END ||
				CASE WHEN liste_sauf_ghm <> '' THEN ' Sauf GHM:'::text || liste_sauf_ghm ELSE ''::text END ||
				CASE WHEN liste_diagnostic_principal <> '' THEN ' DP:'::text || liste_diagnostic_principal ELSE ''::text END ||
				CASE WHEN liste_diagnostic_relie <> '' THEN ' DR:'::text || liste_diagnostic_relie ELSE ''::text END ||
				CASE WHEN liste_diagnostic_associe <> '' THEN ' DA:'::text ||  liste_diagnostic_associe ELSE ''::text END ||
				CASE WHEN liste_acte <> '' THEN ' Acte:'::text || liste_acte ELSE ''::text END ||
                CASE WHEN liste_unite_fonctionnelle <> '' THEN ' UF:'::text || liste_unite_fonctionnelle ELSE ''::text END ||
                CASE WHEN liste_service <> '' THEN ' Ser:'::text || liste_service ELSE ''::text END ||
				CASE WHEN liste_etage <> '' THEN ' Eta:'::text || liste_etage ELSE ''::text END ||
				CASE WHEN liste_compte_produit <> '' THEN ' Cpt:'::text || liste_compte_produit ELSE ''::text END ||
				CASE WHEN liste_sauf_compte_produit <> '' THEN ' Sauf Cpt:'::text || liste_sauf_compte_produit ELSE ''::text END ||
				CASE WHEN liste_prestation <> '' THEN ' Presta:'::text || liste_prestation ELSE ''::text END ||
				CASE WHEN liste_sauf_prestation <> '' THEN ' Sauf Presta:'::text || liste_sauf_prestation ELSE ''::text END ||
                CASE WHEN liste_rubrique <> '' THEN ' Rub:'::text || liste_rubrique ELSE ''::text END ||
				CASE WHEN liste_finess <> '' THEN ' Ets:'::text || liste_finess ELSE ''::text END as rule_resume,

				liste_type_sejour,
				liste_specialite,
				liste_medecin,
				liste_ghm,
				liste_sauf_ghm,
				liste_diagnostic_principal,
				liste_diagnostic_relie,
				liste_diagnostic_associe,
				liste_acte,
				liste_unite_fonctionnelle,
                liste_etage,
                liste_service,
				liste_compte_produit,
				liste_sauf_compte_produit,
                liste_prestation,
				liste_sauf_prestation,
				liste_rubrique,
				liste_finess
			FROM activite.t_compte_produit_analytique_rule
			LEFT JOIN activite.t_compte ON t_compte_produit_analytique_rule.compte_produit_analytique_id = t_compte.oid AND t_compte.oid > 0
			ORDER BY t_compte_produit_analytique_rule.priorite, t_compte.code, t_compte_produit_analytique_rule.code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['rule_id'];
				$code = toHTML(trim($record['rule_code']));
				$texte = toHTML(trim($record['rule_texte']));
				$priorite = toHTML(trim($record['rule_priorite']));
				$compte_produit_analytique_id = toHTML(trim($record['compte_produit_analytique_id']));
				$compte_produit_analytique_texte = toHTML(trim($record['cle_texte']));
				
				$rule_resume = toHTML(trim($record['rule_resume']));
				
				$liste_type_sejour = toHTML(trim($record['liste_type_sejour']));
                $liste_specialite = toHTML(trim($record['liste_specialite']));
				$liste_medecin = toHTML(trim($record['liste_medecin']));
				$liste_ghm = toHTML(trim($record['liste_ghm']));
				$liste_sauf_ghm = toHTML(trim($record['liste_sauf_ghm']));
				$liste_diagnostic_principal = toHTML(trim($record['liste_diagnostic_principal']));
				$liste_diagnostic_relie = toHTML(trim($record['liste_diagnostic_relie']));
				$liste_diagnostic_associe = toHTML(trim($record['liste_diagnostic_associe']));
				$liste_acte = toHTML(trim($record['liste_acte']));
				$liste_unite_fonctionnelle = toHTML(trim($record['liste_unite_fonctionnelle']));
                $liste_service = toHTML(trim($record['liste_service']));
                $liste_etage = toHTML(trim($record['liste_etage']));
				$liste_compte_produit = toHTML(trim($record['liste_compte_produit']));
				$liste_sauf_compte_produit = toHTML(trim($record['liste_sauf_compte_produit']));
				$liste_prestation = toHTML(trim($record['liste_prestation']));
				$liste_sauf_prestation = toHTML(trim($record['liste_sauf_prestation']));
                $liste_rubrique = toHTML(trim($record['liste_rubrique']));
				$liste_finess = toHTML(trim($record['liste_finess']));
				
				$httpString .= "\r\n<RECORD 
										oid=\"$oid\" 
										code=\"$code\" 
										texte=\"$texte\" 
										priorite=\"$priorite\" 
										compte_produit_analytique_id=\"$compte_produit_analytique_id\" 
										compte_produit_analytique_texte=\"$compte_produit_analytique_texte\" 
										
										rule_resume=\"$rule_resume\" 
										
										liste_type_sejour=\"$liste_type_sejour\" 
                                        liste_specialite=\"$liste_specialite\" 
										liste_medecin=\"$liste_medecin\" 
										liste_ghm=\"$liste_ghm\" 
										liste_sauf_ghm=\"$liste_sauf_ghm\" 
										liste_diagnostic_principal=\"$liste_diagnostic_principal\" 
										liste_diagnostic_relie=\"$liste_diagnostic_relie\" 
										liste_diagnostic_associe=\"$liste_diagnostic_associe\" 
										liste_acte=\"$liste_acte\" 
										liste_unite_fonctionnelle=\"$liste_unite_fonctionnelle\" 
                                        liste_service=\"$liste_service\" 
                                        liste_etage=\"$liste_etage\" 
										liste_compte_produit=\"$liste_compte_produit\" 
										liste_sauf_compte_produit=\"$liste_sauf_compte_produit\" 
										liste_prestation=\"$liste_prestation\" 
										liste_sauf_prestation=\"$liste_sauf_prestation\" 
                                        liste_rubrique=\"$liste_rubrique\" 
										liste_finess=\"$liste_finess\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_calcul_encours() {

global $database;

	$httpString_tmp = "";

	$sqlcmd = "
			SELECT 
                t_calcul_encours.oid,
                CASE 
                WHEN type_ligne LIKE 'PRESTA%' THEN '3'::text
                WHEN type_ligne LIKE 'GHSNG%' THEN '4'::text
                WHEN type_ligne LIKE 'PRESEJ%' THEN '5'::text
                WHEN type_ligne LIKE 'FACTURE' THEN '8'::text
                ELSE '9'::text END AS type_parametre,
				type_ligne,
				t_services_facturation.texte  || '(' || t_services_facturation.code || ')' ||
				CASE WHEN t_unites_fonctionnelles.oid <> 0 AND t_unites_fonctionnelles.texte <> t_services_facturation.texte THEN ' UF ' || t_unites_fonctionnelles.texte ELSE '' END,
				t_prestations.code || ' ' || t_prestations.texte,
				t_rubriques_facturation.code || ' ' || t_rubriques_facturation.texte,
				CASE WHEN coefficient_calcule > 0 THEN to_char(coefficient_calcule,'FM9999990D00') ELSE '' END,
				coefficient_is_force,
				CASE WHEN coefficient_is_force = '1' THEN to_char(coefficient_force,'FM9999990D00') ELSE '' END,
				CASE WHEN prix_unitaire_calcule > 0 THEN to_char(prix_unitaire_calcule,'FM9999990D00') ELSE '' END,
				prix_unitaire_is_force,
				CASE WHEN prix_unitaire_is_force = '1' THEN to_char(prix_unitaire_force,'FM9999990D00') ELSE '' END,
                t_calcul_encours.no_sejour,
                p_patients.nom,
                t_calcul_encours.no_facture,
                ''::text AS code,
                ''::text AS texte,
                ''::text AS valeur,
                ''::text AS description,
                ''::text AS type_valorisation,
                t_calcul_encours.ignorer
			FROM activite.t_calcul_encours
			JOIN activite.t_services_facturation ON lieu_service_id = t_services_facturation.oid
			JOIN activite.t_unites_fonctionnelles ON lieu_unite_fonctionnelle_id = t_unites_fonctionnelles.oid
			JOIN activite.t_prestations ON prestation_id = t_prestations.oid
			JOIN activite.t_rubriques_facturation ON rubrique_facturation_id = t_rubriques_facturation.oid
            LEFT JOIN activite.p_sejours ON t_calcul_encours.no_sejour = p_sejours.no_sejour AND t_calcul_encours.no_sejour <> ''
            LEFT JOIN activite.p_patients ON p_patients.no_patient = p_sejours.no_patient
            WHERE type_ligne LIKE 'PRESTA%' OR
                type_ligne = 'GHSNG' OR
                type_ligne LIKE 'PRESEJ%' OR
                type_ligne = 'FACTURE'
            
            UNION
            
            SELECT 
				99999::bigint AS oid,
				CASE t_divers.code
				WHEN 'ENCOURSNOW' THEN '0'::text
				ELSE '1'::text END AS type_parametre,
				'GEN'::text AS type_ligne,
				CASE t_divers.code
				WHEN 'ENCOURSNOW' THEN 'GEN1'::text
				WHEN 'DATEENCOURS' THEN 'GEN2'::text
                WHEN 'NBSEJMIN_HISTOGHS' THEN 'GEN3'::text
				WHEN 'ENCOURSGHMSANSGHS' THEN 'GEN4'::text
				WHEN 'ENCOURSGHSMBB' THEN 'GEN5'::text
				WHEN 'ENCOURSBORNEHAUTE' THEN 'GEN6'::text
				WHEN 'ENCOURSEXHMOYEN' THEN 'GEN7'::text
				WHEN 'ENCOURSVALOCP' THEN 'GEN8'::text
				ELSE 'GEN9'::text || code END,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				t_divers.code AS code,
				t_divers.texte AS texte,
				CASE 
                    WHEN t_divers.valeur != '' AND t_divers.code = 'ENCOURSNOW' THEN to_char(t_divers.valeur_date,'MM/YYYY') 
                    WHEN t_divers.valeur = '' AND t_divers.code = 'ENCOURSNOW' THEN 'INACTIF' 
                    ELSE t_divers.valeur END,
				t_divers.description,
                ''::text AS type_valorisation,
                '0'::text AS ignorer
			FROM activite.t_divers
			WHERE code IN
				(
				'DATEENCOURS', 
				'ENCOURSBORNEHAUTE', 
				'ENCOURSEXHMOYEN', 
				'ENCOURSGHMSANSGHS', 
				'ENCOURSGHSMBB', 
				'ENCOURSNOW', 
				'ENCOURSVALOCP',
                'NBSEJMIN_HISTOGHS'
				) OR
				code LIKE 'ENCOURS%'
            UNION
            SELECT 
				oid,
				'2'::text type_parametre,
				'GENPRESTA'::text AS type_ligne,
				''::text,
				t_prestations.code || ' ' || t_prestations.texte,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				CASE type_valorisation_non_facture
				WHEN 'PJ' THEN 'Prix de journée'
				WHEN 'CP' THEN 'Chambre particulière'
                WHEN 'ENT' THEN 'Forfait Entrée'
				WHEN '*AVGJOUSER' THEN 'Moyenne par journée de présence service'
				WHEN '*SAISIE' THEN 'Uniquement prestations saisies'
				ELSE '' END,
                type_valorisation_non_facture,
                '0'::text AS ignorer
			FROM activite.t_prestations
			WHERE oid IN (SELECT oid FROM activite.p_oids WHERE code_table = 'prestation_c')
            UNION
            SELECT 
				99999::bigint AS oid,
				'0'::text type_parametre,
				'GEN'::text AS type_ligne,
				'GEN0'::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				''::text,
				'ENCOURSNEXT'::text AS code,
				'Date des prochains encours'::text AS texte,
				CASE WHEN t_divers.valeur = '1' THEN to_char(LEAST(date_vente_shs,date_encours,date_fermeture),'MM/YYYY') ELSE 'INACTIF' END,
				'Date des prochains encours'::text AS description,
				''::text AS type_valorisation,
                '0'::text AS ignorer
            FROM activite.t_divers
            JOIN (
			SELECT date(CASE WHEN EXTRACT('day' FROM now()) <= base.cti_to_number(valeur) THEN date_trunc('month', now()) - interval '1 day' ELSE date_trunc('month', now()) + interval '1 month' - interval '1 day' END) AS date_encours
			FROM activite.t_divers 
			WHERE code = 'DATEENCOURS'
		    ) subview ON 1=1
            LEFT JOIN (
                SELECT valeur::date AS date_fermeture
                FROM activite.t_divers 
                WHERE code = 'DATEFERMETURE' AND
			valeur <> '' AND
			valeur < to_char(now(),'YYYYMMDD')
            ) subview_datefermeture ON 1=1
            JOIN
            (
                SELECT date(date_trunc('month', MAX(date_vente)) + interval '2 month' - interval '1 day') AS date_vente_shs
                FROM activite.p_factures
                JOIN activite.p_factures_lignes_c ON p_factures.no_facture = p_factures_lignes_c.no_facture
                JOIN activite.t_rubriques_facturation ON rubrique_facturation_id = t_rubriques_facturation.oid AND 
                    t_rubriques_facturation.code_original = '111'
                WHERE provider_id = 0 AND code_vente = '1'
            ) subview_2 ON 1=1
            WHERE t_divers.code = 'ENCOURSACTIVE'

			ORDER BY 13,15,2,3,4,5";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
                $type_parametre = toHTML(trim($record[1]));
                $type_ligne = toHTML(trim($record[2]));
				$service = toHTML(trim($record[3]));
				$prestation = toHTML(trim($record[4]));
				$rubrique = toHTML(trim($record[5]));
                $coefficient_calcule = toHTML(trim($record[6]));
                $coefficient_is_force = toHTML(trim($record[7]));
                $coefficient_force = toHTML(trim($record[8]));
                $prix_unitaire_calcule = toHTML(trim($record[9]));
                $prix_unitaire_is_force = toHTML(trim($record[10]));
                $prix_unitaire_force = toHTML(trim($record[11]));
                $sejour = toHTML(trim($record[12]));
                $nom = toHTML(trim($record[13]));
                $facture = toHTML(trim($record[14]));
                $code = toHTML(trim($record[15]));
                $texte = toHTML(trim($record[16]));
                $valeur = toHTML(trim($record[17]));
                $description = toHTML(trim($record[18]));
                $type_valorisation = toHTML(trim($record[19]));
                $ignorer = toHTML(trim($record[20]));
                
				
				$httpString_tmp = $httpString_tmp . "\r\n<RECORD ";
				$httpString_tmp = $httpString_tmp . "oid=\"$oid\" ";
                $httpString_tmp = $httpString_tmp . "type_parametre=\"$type_parametre\" ";
				$httpString_tmp = $httpString_tmp . "type_ligne=\"$type_ligne\" ";
				$httpString_tmp = $httpString_tmp . "service=\"$service\" ";
				$httpString_tmp = $httpString_tmp . "prestation=\"$prestation\" ";
                $httpString_tmp = $httpString_tmp . "rubrique=\"$rubrique\" ";
                $httpString_tmp = $httpString_tmp . "coefficient_calcule=\"$coefficient_calcule\" ";
                $httpString_tmp = $httpString_tmp . "coefficient_is_force=\"$coefficient_is_force\" ";
                $httpString_tmp = $httpString_tmp . "coefficient_force=\"$coefficient_force\" ";
                $httpString_tmp = $httpString_tmp . "prix_unitaire_calcule=\"$prix_unitaire_calcule\" ";
                $httpString_tmp = $httpString_tmp . "prix_unitaire_is_force=\"$prix_unitaire_is_force\" ";
                $httpString_tmp = $httpString_tmp . "prix_unitaire_force=\"$prix_unitaire_force\" ";
                $httpString_tmp = $httpString_tmp . "sejour=\"$sejour\" ";
                $httpString_tmp = $httpString_tmp . "nom=\"$nom\" ";
                $httpString_tmp = $httpString_tmp . "facture=\"$facture\" ";
                $httpString_tmp = $httpString_tmp . "code=\"$code\" ";
                $httpString_tmp = $httpString_tmp . "texte=\"$texte\" ";
                $httpString_tmp = $httpString_tmp . "valeur=\"$valeur\" ";
                $httpString_tmp = $httpString_tmp . "description=\"$description\" ";
                $httpString_tmp = $httpString_tmp . "type_valorisation=\"$type_valorisation\" ";
				$httpString_tmp = $httpString_tmp . "ignorer=\"$ignorer\" ";
				$httpString_tmp = $httpString_tmp . "nbref=\"1\" ";
				$httpString_tmp = $httpString_tmp . " />";
                
                
                if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}

			}
		}
        
        $httpString .= $httpString_tmp;

	
	}
	
	return $httpString;
	
	

}


function getCombo_compte() {

global $database;

	$httpString = "";

	
	$httpString = $httpString . "\r\n<COMBO_CPT>";
	
	$httpString = $httpString . "\r\n<RECORD oid=\"0\" texte=\"\"  />";

	$sqlcmd = "SELECT oid, code || ' ' || texte FROM activite.t_compte WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString = $httpString . "\r\n<RECORD oid=\"$oid\" texte=\"$texte\"  />";

			}
		}

	
	}

	$httpString = $httpString . "\r\n</COMBO_CPT>";
	
	return $httpString;
	
	

}


function getCombo_compte_tva() {

global $database;

	$httpString = "";

	
	$httpString = $httpString . "\r\n<COMBO_CPT>";
	
	$httpString = $httpString . "\r\n<RECORD oid=\"0\" texte=\"Pas de TVA sur ce compte\"  />";

	$sqlcmd = "SELECT oid, code::text || ' '::text || texte::text FROM activite.t_compte WHERE oid > 0 AND code LIKE '445%'::text ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString = $httpString . "\r\n<RECORD oid=\"$oid\" texte=\"$texte\"  />";

			}
		}

	
	}

	$httpString = $httpString . "\r\n</COMBO_CPT>";
	
	return $httpString;
	
	

}


function getCombo_forme_activite() {

global $database;

	$httpString = "";

	
	$httpString = $httpString . "\r\n<COMBO_FAC>";
	
    $sqlcmd = "SELECT oid, code::text || ' '::text || texte::text FROM activite.t_forme_activite WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString = $httpString . "\r\n<RECORD oid=\"$oid\" texte=\"$texte\"  />";

			}
		}

	
	}

	$httpString = $httpString . "\r\n</COMBO_FAC>";
	
	return $httpString;
	
	

}


function getCombo_filiere_soin() {

global $database;

	$httpString = "";

	
	$httpString = $httpString . "\r\n<COMBO_FSO>";
	
    $sqlcmd = "SELECT oid, code::text || ' '::text || texte::text FROM activite.t_filiere_soin ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString = $httpString . "\r\n<RECORD oid=\"$oid\" texte=\"$texte\"  />";

			}
		}

	
	}

	$httpString = $httpString . "\r\n</COMBO_FSO>";
	
	return $httpString;
	
	

}


function getCombo_pole() {

global $database;

	$httpString = "";

	
	$httpString = $httpString . "\r\n<COMBO_POL>";
	
    $sqlcmd = "SELECT oid, code::text || ' '::text || texte::text FROM base.t_pole WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString = $httpString . "\r\n<RECORD oid=\"$oid\" texte=\"$texte\"  />";

			}
		}

	
	}

	$httpString = $httpString . "\r\n</COMBO_POL>";
	
	return $httpString;
	
	

}


function getCombo_specialites_internes_medecin() {

global $database;

	$httpString = "\r\n<COMBO_SPI>";
	
	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM base.t_specialites_medecin WHERE oid >= 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
			
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecordInto();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
				
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}
	$httpString .= "\r\n</COMBO_SPI>";
	
	return $httpString;
}

function getRecords_medecins() {

global $database;

	$httpString = "";
	
	$httpString_tmp = "";

	$sqlcmd = "	SELECT 
					t_medecins.oid as med_id, 
					'CTI' || trim(to_char(t_medecins.oid, '00000')) as med_code, 
					t_medecins.nom as med_nom, 
					t_medecins.prenom as med_prenom, 
					'CTI' || trim(to_char(t_medecins.oid, '00000')) as med_code_original, 
					t_medecins.numero_ordre as med_numero_ordre, 
					t_medecins.specialite_id as spe_id,
					t_specialites_medecin.code || ' ' || t_specialites_medecin.texte_court as spe_texte, 
					ARRAY(SELECT oid FROM activite.t_medecins_administratifs WHERE medecin_id = t_medecins.oid AND oid <> 0 ) as med_regroupes, 
					subview.count as nb_ref
				FROM 
					base.t_medecins  
					LEFT JOIN  
						(	SELECT 
								medecin_id, count(*) as count  
							FROM 
								activite.t_medecins_administratifs  
							GROUP BY medecin_id
						) subview ON (t_medecins.oid = subview.medecin_id) 
					LEFT JOIN 
						base.t_specialites_medecin ON (t_medecins.specialite_id = t_specialites_medecin.oid)  
				WHERE 
					t_medecins.oid > 0  
				ORDER BY t_medecins.nom, t_medecins.prenom";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['med_id'];
				$code = toHTML(trim($record['med_code']));
				$nom = toHTML(trim($record['med_nom']));
				$prenom = toHTML(trim($record['med_prenom']));
				$code_original = toHTML(trim($record['med_code_original']));
				$numero_ordre = toHTML(trim($record['med_numero_ordre']));
				$specialite_interne_id = toHTML(trim($record['spe_id']));
				$specialite_interne_texte = toHTML(trim($record['spe_texte']));
				$medecins_regroupes_oids = toHTML(trim($record['med_regroupes']));
				$nbref = $record['nb_ref'] + 0;
				
				$medecins_regroupes_oids = str_replace("{", "", $medecins_regroupes_oids);
				$medecins_regroupes_oids = str_replace("}", "", $medecins_regroupes_oids);
				if ($medecins_regroupes_oids != "") {
					$medecins_regroupes_oids = "," . trim($medecins_regroupes_oids) . ",";
				}
				else {
					$medecins_regroupes_oids = ",";
				}
				
				$httpString_tmp .= "\r\n<RECORD 
						oid=\"$oid\" 
						code=\"$code\" 
						nom=\"$nom\" 
						prenom=\"$prenom\" 
						code_original=\"$code_original\" 
						numero_ordre=\"$numero_ordre\" 
						specialite_interne_id=\"$specialite_interne_id\" 
						specialite_interne_texte=\"$specialite_interne_texte\" 
						medecins_regroupes_oids=\"$medecins_regroupes_oids\" 
						nbref=\"+$nbref\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	
	return $httpString;
}

function getRecords_sejour() {

global $database;

	$httpString = "";
	
	$sqlcmd = "SELECT 
				t_sejour.oid as sej_id, 
				t_sejour.no_sejour as sej_no_sejour, 
				p_patients.nom || ' ' || p_patients.prenom as pat_nom, 
				t_sejour.est_ignore as sej_est_ignore_code, 
				CASE WHEN t_sejour.est_ignore = '1' THEN 'Ignoré' ELSE '' END as sej_est_ignore_texte,
				t_sejour.medecin_sejour_id as med_sej_id,
				COALESCE(t_medecins_administratifs.nom || ' ' || t_medecins_administratifs.prenom || ' (' || t_medecins_administratifs.code || ')','') as med_sej_nom,
				t_sejour.service_facturation_id,
				COALESCE(t_services_facturation.texte || ' (' || t_services_facturation.code || ')','') as service_facturation_texte,
				t_sejour.etage_id,
				COALESCE(t_etages.texte || ' (' || t_etages.code || ')','') as etage_texte,
				t_sejour.type_sejour,
				COALESCE(t_type_sejour.texte,'') AS type_sejour_texte,
				COALESCE(encours_from_date[1],'2099-12-31') AS encours_1_from_date,
				COALESCE(encours_est_ignore[1],'0') AS encours_1_est_ignore,
				CASE WHEN COALESCE(encours_from_date[1],'2099-12-31') <> '20991231' AND encours_est_ignore[1] = '1'
					THEN '>='||to_char(encours_from_date[1],'MMYYYY') || 
						CASE WHEN encours_est_ignore[1] = '1' THEN ' Ignorer' ELSE '' END
					ELSE '' END AS encours_texte
			FROM activite.t_sejour 
			LEFT JOIN activite.p_sejours ON t_sejour.no_sejour = p_sejours.no_sejour
			LEFT JOIN activite.p_patients ON p_sejours.no_patient = p_patients.no_patient
			LEFT JOIN activite.t_medecins_administratifs ON t_sejour.medecin_sejour_id  = t_medecins_administratifs.oid AND t_sejour.medecin_sejour_id <> 0
			LEFT JOIN activite.t_services_facturation ON t_sejour.service_facturation_id  = t_services_facturation.oid AND t_sejour.service_facturation_id <> 0
			LEFT JOIN activite.t_etages ON t_sejour.etage_id  = t_etages.oid AND t_sejour.etage_id <> 0
			LEFT JOIN activite.t_type_sejour ON t_type_sejour.code = t_sejour.type_sejour AND t_sejour.type_sejour IN ('1','2','3','4','5','6','9')
			ORDER BY t_sejour.no_sejour";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = ctiFloatVal($record['sej_id']);
				$code = toHTML(trim($record['sej_no_sejour']));
				$texte = toHTML(trim($record['pat_nom']));
				$est_ignore = toHTML(trim($record['sej_est_ignore_code']));
				$est_ignore_texte = toHTML(trim($record['sej_est_ignore_texte']));
				$medecin_sejour_id = ctiFloatVal($record['med_sej_id']);
				$medecin_sejour_texte = toHTML(trim($record['med_sej_nom']));
				$service_facturation_id = ctiFloatVal($record['service_facturation_id']);
				$service_facturation_texte = toHTML(trim($record['service_facturation_texte']));
				$etage_id = ctiFloatVal($record['etage_id']);
				$etage_texte = toHTML(trim($record['etage_texte']));
				$type_sejour = ctiFloatVal($record['type_sejour']);
				$type_sejour_texte = toHTML(trim($record['type_sejour_texte']));
				
				$encours_texte = toHTML(trim($record['encours_texte']));
				
				$encours_1_from_date = toHTML(trim($record['encours_1_from_date']));
				$encours_1_est_ignore = toHTML(trim($record['encours_1_est_ignore']));

				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									est_ignore=\"$est_ignore\" 
									est_ignore_texte=\"$est_ignore_texte\" 
									medecin_sejour_id=\"$medecin_sejour_id\" 
									medecin_sejour_texte=\"$medecin_sejour_texte\" 
									service_facturation_id=\"$service_facturation_id\" 
									service_facturation_texte=\"$service_facturation_texte\" 
									etage_id=\"$etage_id\" 
									etage_texte=\"$etage_texte\" 
									type_sejour=\"$type_sejour\" 
									type_sejour_texte=\"$type_sejour_texte\" 
									encours_texte=\"$encours_texte\" 
									encours_1_from_date=\"$encours_1_from_date\" 
									encours_1_est_ignore=\"$encours_1_est_ignore\" 
									/>";
			}
		}
	}
	return $httpString;
}

function getRecords_budget() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT oid, code, texte, 
				to_char(date_debut,'DD/MM/YYYY'), 
				to_char(date_fin,'DD/MM/YYYY'),
				COALESCE(xmlnodes,'')
			FROM activite.t_budget
			LEFT JOIN
				(
					SELECT
						budget_id,
						base.cti_group_concat_without_sep(xmlnode) AS xmlnodes
					FROM (
						SELECT
									budget_id, 
									'<INDICATEUR ' ||
									'oid=\"' || oid || '\" ' ||
									'code=\"' || code || '\" ' ||
									'texte=\"' || texte || '\" ' ||
									'indicateur_id=\"' || indicateur_id || '\" ' ||
									'/>'AS xmlnode
								FROM activite.t_budget_indicateur
						) subview
					GROUP BY 1
				) subview ON t_budget.oid = subview.budget_id
			WHERE oid > 0  
			ORDER BY code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$code = toHTML(trim($record[1]));
				$texte = toHTML(trim($record[2]));
				$date_debut = toHTML(trim($record[3]));
				$date_fin = toHTML(trim($record[4]));
				$XMLindicateurs = $record[5];
				
				$httpString .=  "\r\n<RECORD 
						oid=\"$oid\" 
						code=\"$code\" 
						texte=\"$texte\" 
						date_debut=\"$date_debut\" 
						date_fin=\"$date_fin\" 
				 >
				 $XMLindicateurs
				</RECORD>";
			}
		}
	}
	return $httpString;
}

function getRecords_budget_cle() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT oid, code, texte, texte_court, code_original  
		FROM activite.t_budget_cle
		WHERE oid > 0  
		ORDER BY code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$code = toHTML(trim($record[1]));
				$texte = toHTML(trim($record[2]));
				$texte_court = toHTML(trim($record[3]));
				$code_original = toHTML(trim($record[4]));
				$nbref = 0;
				
				$httpString .= "\r\n<RECORD 
									oid=\"$oid\" 
									code=\"$code\" 
									texte=\"$texte\" 
									texte_court=\"$texte_court\" 
									code_original=\"$code_original\" 
									nbref=\"+$nbref\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_budget_cle_rule() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT t_budget_cle_rule.oid as rule_id, 
				t_budget_cle_rule.code as rule_code, 
				t_budget_cle_rule.texte as rule_texte, 
				t_budget_cle_rule.priorite as rule_priorite, 
				t_budget_cle_rule.budget_cle_id as cle_id, 
				t_budget_cle.code || ' '::text || t_budget_cle.texte as cle_texte,
				
				CASE WHEN liste_specialite <> '' THEN ' Spé:'::text || liste_specialite ELSE ''::text END ||
				CASE WHEN liste_medecin <> '' THEN ' Med:'::text ||  liste_medecin ELSE ''::text END ||
				CASE WHEN liste_service <> '' THEN ' Ser:'::text || liste_service ELSE ''::text END ||
				CASE WHEN liste_acte <> '' THEN ' Act:'::text || liste_acte ELSE ''::text END ||
				CASE WHEN liste_rubrique <> '' THEN ' Rub:'::text || liste_rubrique ELSE ''::text END ||
				CASE WHEN liste_ghm <> '' THEN ' GHM:'::text || liste_ghm ELSE ''::text END ||
				CASE WHEN liste_sauf_ghm <> '' THEN ' GHMx:'::text || liste_sauf_ghm ELSE ''::text END ||
				CASE WHEN liste_diagnostic_principal <> '' THEN ' DP:'::text || liste_diagnostic_principal ELSE ''::text END ||
				CASE WHEN liste_diagnostic_relie <> '' THEN ' DR:'::text || liste_diagnostic_relie ELSE ''::text END ||
				CASE WHEN liste_diagnostic_associe <> '' THEN ' DA:'::text ||  liste_diagnostic_associe ELSE ''::text END ||
				CASE WHEN liste_finess <> '' THEN ' Ets:'::text || liste_finess ELSE ''::text END as rule_resume,
				
				liste_specialite,
				liste_medecin,
				liste_service,
				liste_acte,
				liste_rubrique,
				liste_ghm,
				liste_sauf_ghm,
				liste_diagnostic_principal,
				liste_diagnostic_relie,
				liste_diagnostic_associe,
				liste_finess
			FROM activite.t_budget_cle_rule
			LEFT JOIN activite.t_budget_cle ON t_budget_cle_rule.budget_cle_id = t_budget_cle.oid
			WHERE t_budget_cle_rule.oid > 0  
			ORDER BY t_budget_cle_rule.priorite, t_budget_cle.code, t_budget_cle_rule.code ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecord();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['rule_id'];
				$code = toHTML(trim($record['rule_code']));
				$texte = toHTML(trim($record['rule_texte']));
				$priorite = toHTML(trim($record['rule_priorite']));
				$budget_cle_id = toHTML(trim($record['cle_id']));
				$budget_cle_texte = toHTML(trim($record['cle_texte']));
				
				$rule_resume = toHTML(trim($record['rule_resume']));
				
				$liste_specialite = toHTML(trim($record['liste_specialite']));
				$liste_medecin = toHTML(trim($record['liste_medecin']));
				$liste_service = toHTML(trim($record['liste_service']));
				$liste_acte = toHTML(trim($record['liste_acte']));
				$liste_rubrique = toHTML(trim($record['liste_rubrique']));
				$liste_ghm = toHTML(trim($record['liste_ghm']));
				$liste_sauf_ghm = toHTML(trim($record['liste_sauf_ghm']));
				$liste_diagnostic_principal = toHTML(trim($record['liste_diagnostic_principal']));
				$liste_diagnostic_relie = toHTML(trim($record['liste_diagnostic_relie']));
				$liste_diagnostic_associe = toHTML(trim($record['liste_diagnostic_associe']));
				$liste_finess = toHTML(trim($record['liste_finess']));
				
				$httpString .= "\r\n<RECORD 
										oid=\"$oid\" 
										code=\"$code\" 
										texte=\"$texte\" 
										priorite=\"$priorite\" 
										budget_cle_id=\"$budget_cle_id\" 
										budget_cle_texte=\"$budget_cle_texte\" 
										
										rule_resume=\"$rule_resume\" 
										
										liste_specialite=\"$liste_specialite\" 
										liste_medecin=\"$liste_medecin\" 
										liste_service=\"$liste_service\" 
										liste_acte=\"$liste_acte\" 
										liste_rubrique=\"$liste_rubrique\" 
										liste_ghm=\"$liste_ghm\" 
										liste_sauf_ghm=\"$liste_sauf_ghm\" 
										liste_diagnostic_principal=\"$liste_diagnostic_principal\" 
										liste_diagnostic_relie=\"$liste_diagnostic_relie\" 
										liste_diagnostic_associe=\"$liste_diagnostic_associe\" 
										liste_finess=\"$liste_finess\" />";
			}
		}
	}
	return $httpString;
}

function getRecords_budget_cle_budget() {

global $database;

	$httpString = "";

	$sqlcmd = "SELECT oid, code, texte, 
					to_char(date_debut,'DD/MM/YYYY'), 
					to_char(date_fin,'DD/MM/YYYY'),
					COALESCE(xmlnodes,'')
				FROM activite.t_budget
				LEFT JOIN
					(

				SELECT budget_id,
					base.cti_group_concat_without_sep(
					'<INDICATEUR ' ||
						'budget_indicateur_id=\"' || budget_indicateur_id || '\" ' ||
						'budget_indicateur_texte=\"' || budget_indicateur_texte || '\" ' ||
						'date_01=\"' || date_01 || '\" ' ||
						'date_02=\"' || date_02 || '\" ' ||
						'date_03=\"' || date_03 || '\" ' ||
						'date_04=\"' || date_04 || '\" ' ||
						'date_05=\"' || date_05 || '\" ' ||
						'date_06=\"' || date_06 || '\" ' ||
						'date_07=\"' || date_07 || '\" ' ||
						'date_08=\"' || date_08 || '\" ' ||
						'date_09=\"' || date_09 || '\" ' ||
						'date_10=\"' || date_10 || '\" ' ||
						'date_11=\"' || date_11 || '\" ' ||
						'date_12=\"' || date_12 || '\" ' ||
						'dateh_01=\"' || dateh_01 || '\" ' ||
						'dateh_02=\"' || dateh_02 || '\" ' ||
						'dateh_03=\"' || dateh_03 || '\" ' ||
						'dateh_04=\"' || dateh_04 || '\" ' ||
						'dateh_05=\"' || dateh_05 || '\" ' ||
						'dateh_06=\"' || dateh_06 || '\" ' ||
						'dateh_07=\"' || dateh_07 || '\" ' ||
						'dateh_08=\"' || dateh_08 || '\" ' ||
						'dateh_09=\"' || dateh_09 || '\" ' ||
						'dateh_10=\"' || dateh_10 || '\" ' ||
						'dateh_11=\"' || dateh_11 || '\" ' ||
						'dateh_12=\"' || dateh_12 || '\" ' ||
						'>' || xmlnodes || '</INDICATEUR>' || chr(10)
					 ) AS xmlnodes

				FROM

				(
				SELECT budget_id,
					budget_indicateur_id,
					budget_indicateur_texte,
					date_01, 
					date_02, 
					date_03, 
					date_04, 
					date_05,
					date_06, 
					date_07, 
					date_08, 
					date_09, 
					date_10, 
					date_11, 
					date_12,
					dateh_01, 
					dateh_02, 
					dateh_03, 
					dateh_04, 
					dateh_05,
					dateh_06, 
					dateh_07, 
					dateh_08, 
					dateh_09, 
					dateh_10, 
					dateh_11, 
					dateh_12,
					base.cti_group_concat_without_sep(
					'<CLE ' ||
						'budget_indicateur_id=\"' || budget_indicateur_id || '\" ' ||
						'budget_indicateur_texte=\"' || budget_indicateur_texte || '\" ' ||
						'budget_cle_id=\"' || budget_cle_id || '\" ' ||
						'budget_cle_texte=\"' || budget_cle_texte || '\" ' ||
						'budget_01=\"' || budget_01 || '\" ' ||
						'budget_02=\"' || budget_02 || '\" ' ||
						'budget_03=\"' || budget_03 || '\" ' ||
						'budget_04=\"' || budget_04 || '\" ' ||
						'budget_05=\"' || budget_05 || '\" ' ||
						'budget_06=\"' || budget_06 || '\" ' ||
						'budget_07=\"' || budget_07 || '\" ' ||
						'budget_08=\"' || budget_08 || '\" ' ||
						'budget_09=\"' || budget_09 || '\" ' ||
						'budget_10=\"' || budget_10 || '\" ' ||
						'budget_11=\"' || budget_11 || '\" ' ||
						'budget_12=\"' || budget_12 || '\" ' ||
						'/>' || chr(10)
					) AS xmlnodes
				FROM
					(
						SELECT 
							t_budget.oid AS budget_id,
							t_budget_indicateur.oid AS budget_indicateur_id,
							t_budget_indicateur.code || ' ' || t_budget_indicateur.texte AS budget_indicateur_texte,
							t_budget_cle.oid AS budget_cle_id,
							t_budget_cle.code || ' ' || t_budget_cle.texte AS budget_cle_texte,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 0 AND 10 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_01,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 30 AND 40 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_02,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 59 AND 70 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_03,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 90 AND 100 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_04,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 120 AND 130 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_05,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 150 AND 160 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_06,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 180 AND 190 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_07,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 210 AND 220 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_08,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 240 AND 250 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_09,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 270 AND 280 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_10,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 300 AND 310 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_11,
							SUM(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 330 AND 340 THEN t_budget_cle_budget.budget ELSE 0 END) AS budget_12,

							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 0 AND 10 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_01,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 30 AND 40 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_02,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 59 AND 70 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_03,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 90 AND 100 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_04,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 120 AND 130 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_05,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 150 AND 160 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_06,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 180 AND 190 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_07,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 210 AND 220 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_08,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 240 AND 250 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_09,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 270 AND 280 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_10,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 300 AND 310 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_11,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 330 AND 340 THEN date(date_trunc('month',p_calendrier_mois.date_debut)) ELSE NULL END) AS date_12,

							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 0 AND 10 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_01,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 30 AND 40 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_02,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 59 AND 70 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_03,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 90 AND 100 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_04,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 120 AND 130 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_05,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 150 AND 160 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_06,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 180 AND 190 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_07,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 210 AND 220 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_08,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 240 AND 250 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_09,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 270 AND 280 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_10,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 300 AND 310 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_11,
							MAX(CASE WHEN date(date_trunc('month',p_calendrier_mois.date_fin)) - date(date_trunc('month',t_budget.date_debut)) BETWEEN 330 AND 340 THEN to_char(p_calendrier_mois.date_debut,'MM/YYYY') ELSE NULL END) AS dateh_12

						FROM
							activite.t_budget
							JOIN activite.t_budget_cle ON 1=1
							JOIN activite.t_budget_indicateur ON  t_budget_indicateur.budget_id = t_budget.oid
							JOIN base.p_calendrier_mois ON p_calendrier_mois.date_debut BETWEEN t_budget.date_debut AND t_budget.date_fin
							LEFT JOIN activite.t_budget_cle_budget ON t_budget_cle_budget.budget_id = t_budget.oid AND 
												t_budget_cle_budget.budget_cle_id = t_budget_cle.oid AND
												t_budget_cle_budget.budget_indicateur_id = t_budget_indicateur.oid AND
												date(date_trunc('month',t_budget_cle_budget.date)) = date(date_trunc('month',p_calendrier_mois.date_fin))
						GROUP BY 1,2,3,4,5
						ORDER BY 1,2,5
					) subview
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27
				ORDER BY 1,3
				) subview
				GROUP BY 1
				ORDER BY 1
				)  subview ON t_budget.oid = subview.budget_id
			WHERE oid > 0  
			ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$code = toHTML(trim($record[1]));
				$texte = toHTML(trim($record[2]));
				$date_debut = toHTML(trim($record[3]));
				$date_fin = toHTML(trim($record[4]));
				$XMLindicateurs = $record[5];
				
				$httpString .= "\r\n<RECORD 
										oid=\"$oid\" 
										code=\"$code\" 
										texte=\"$texte\" 
										date_debut=\"$date_debut\" 
										date_fin=\"$date_fin\" 
				 >
				 $XMLindicateurs
				</RECORD>";
			}
		}
	}
	return $httpString;
}

function getList_medecins_administratifs() {

global $database;

	$httpString_tmp = "";
	
	$httpString = "\r\n<LIST_MDA>";

	$sqlcmd = "	SELECT
					t_medecins_administratifs.oid, 
					t_medecins_administratifs.nom || ' ' || t_medecins_administratifs.prenom || ' (' || t_medecins_administratifs.code || ')', 
					t_medecins.nom || ' ' || t_medecins.prenom || ' (' || CASE WHEN t_medecins.oid <> 0 THEN 'CTI' || trim(to_char(t_medecins.oid, '00000')) ELSE 'Non associé' END || ')' 
				FROM
					activite.t_medecins_administratifs 
					LEFT JOIN base.t_medecins ON (t_medecins_administratifs.medecin_id = t_medecins.oid) 
				WHERE 
					t_medecins_administratifs.oid > 0 
				ORDER BY t_medecins_administratifs.nom, t_medecins_administratifs.prenom ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = toHTML(trim($record[0]));
				$nom_adm = toHTML(trim($record[1]));
				$nom_ref = toHTML(trim($record[2]));
			
				$httpString_tmp .= "\r\n<RECORD 
										oid=\"$oid\" 
										nom_adm=\"$nom_adm\" 
										nom_ref=\"$nom_ref\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;

	$httpString .= "\r\n</LIST_MDA>";
	
	return $httpString;
}


function getList_medecins_traitants_administratifs() {

global $database;

	$httpString_tmp = "";
	
	$httpString = "\r\n<LIST_MET>";

	$sqlcmd = "	SELECT
					t_medecins_traitants_administratifs.oid, 
					t_medecins_traitants_administratifs.nom || ' ' || t_medecins_traitants_administratifs.prenom || ' (' || t_medecins_traitants_administratifs.code || ')', 
					t_medecins.nom || ' ' || t_medecins.prenom || ' (' || CASE WHEN t_medecins.oid <> 0 THEN 'CTI' || trim(to_char(t_medecins.oid, '00000')) ELSE 'Non associé' END || ')' 
				FROM
					activite.t_medecins_traitants_administratifs 
					LEFT JOIN base.t_medecins ON (t_medecins_traitants_administratifs.medecin_id = t_medecins.oid) 
				WHERE 
					t_medecins_traitants_administratifs.oid > 0 
				ORDER BY t_medecins_traitants_administratifs.nom, t_medecins_traitants_administratifs.prenom ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = toHTML(trim($record[0]));
				$nom_adm = toHTML(trim($record[1]));
				$nom_ref = toHTML(trim($record[2]));
			
				$httpString_tmp .= "\r\n<RECORD 
										oid=\"$oid\" 
										nom_adm=\"$nom_adm\" 
										nom_ref=\"$nom_ref\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;

	$httpString .= "\r\n</LIST_MET>";
	
	return $httpString;
}

function getCombo_modes_traitement() {

global $database;

	$httpString = "\r\n<COMBO_MTR>";
	
	$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Non renseigné\" />";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM base.t_modes_traitement WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}

	$httpString .= "\r\n</COMBO_MTR>";
	
	return $httpString;
}

function getCombo_finess() {

global $database;

	$httpString = "\r\n<COMBO_FINESS>";
	
	$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Défaut\" />";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM base.t_finess WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}

	$httpString .= "\r\n</COMBO_FINESS>";
	
	return $httpString;
}

function getCombo_dmt() {

global $database;

	$httpString = "\r\n<COMBO_DMT>";
	
	$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Non renseignée\" />";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM base.t_dmt WHERE oid > 0 ORDER BY code";
	
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}
	
	$httpString .= "\r\n</COMBO_DMT>";
	
	return $httpString;
}

function getCombo_unites_medicales() {

global $database;

	$httpString = "\r\n<COMBO_UMD>";
	
	$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Non renseignée\" />";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM activite.t_unites_medicales WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
		
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}
	
	$httpString .= "\r\n</COMBO_UMD>";
	
	return $httpString;
}

function getCombo_unites_medicales_exceptions() {

global $database;

	$httpString = "\r\n<COMBO_UMX>";
	
	$httpString .= "\r\n<RECORD oid=\"0\" type=\"\" texte=\"\" />";

	$sqlcmd = "	
			SELECT 
					oid, 
					'A'::text, 
					'Si activité = '::text || code || ' '::text || texte
				FROM 
					activite.t_activites  
				WHERE 
					oid > 0  
					AND oid IN (SELECT DISTINCT activite_id FROM activite.t_lieux) 
		UNION ALL
			SELECT 
				oid, 
				'E'::text, 
				'Si étage = '::text || code || ' '::text || texte
			FROM 
				activite.t_etages  
			WHERE 
				oid > 0  
				AND oid IN (SELECT DISTINCT t_lits.etage_id FROM activite.t_lieux, activite.t_lits WHERE t_lieux.lit_id = t_lits.oid) 
		ORDER BY 2";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$type = toHTML(trim($record[1]));
				$texte = toHTML(trim($record[2]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" type=\"$type\" texte=\"$texte\" />";
			}
		}
	}
	$httpString .= "\r\n</COMBO_UMX>";
	
	return $httpString;
}

function getCombo_etages($tableName="") {

global $database;

	if ($tableName == "t_services_facturation") {
		$httpString_tmp = "\r\n<RECORD oid=\"0\" texte=\"Ignoré\" />";
	}
	else {
		$httpString_tmp = "\r\n<RECORD oid=\"0\" texte=\"Non renseigné\" />";
	}
	
	$httpString = "\r\n<COMBO_ETAGE>";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM activite.t_etages WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;
				
				$code = ctiFloatVal(trim($record[0]));
				$texte = toHTML(trim($record[1]));
			
				$httpString_tmp .= "\r\n<RECORD oid=\"$code\" texte=\"$texte\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</COMBO_ETAGE>";
	
	return $httpString;
}


function getCombo_activites($tableName="") {

global $database;

	if ($tableName == "t_services_facturation") {
		$httpString_tmp = "\r\n<RECORD oid=\"0\" texte=\"Ignoré\" />";
	}
	else {
		$httpString_tmp = "\r\n<RECORD oid=\"0\" texte=\"Non renseigné\" />";
	}
	
	$httpString = "\r\n<COMBO_ACTIVITE>";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM activite.t_activites WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;
				
				$code = ctiFloatVal(trim($record[0]));
				$texte = toHTML(trim($record[1]));
			
				$httpString_tmp .= "\r\n<RECORD oid=\"$code\" texte=\"$texte\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</COMBO_ACTIVITE>";
	
	return $httpString;
}



function getCombo_services($tableName="") {

global $database;


	$httpString_tmp = "\r\n<RECORD oid=\"0\" texte=\"\" />";

	
	$httpString = "\r\n<COMBO_SERVICE>";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM activite.t_services_facturation WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;
				
				$code = ctiFloatVal(trim($record[0]));
				$texte = toHTML(trim($record[1]));
			
				$httpString_tmp .= "\r\n<RECORD oid=\"$code\" texte=\"$texte\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</COMBO_SERVICE>";
	
	return $httpString;
}


function getCombo_rubriques($tableName="") {

global $database;


	$httpString_tmp = "\r\n<RECORD oid=\"0\" texte=\"\" />";

	
	$httpString = "\r\n<COMBO_RUBRIQUE>";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM activite.t_rubriques_facturation WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;
				
				$code = ctiFloatVal(trim($record[0]));
				$texte = toHTML(trim($record[1]));
			
				$httpString_tmp .= "\r\n<RECORD oid=\"$code\" texte=\"$texte\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</COMBO_RUBRIQUE>";
	
	return $httpString;
}



function getCombo_prestations($tableName="") {

global $database;


	$httpString_tmp = "\r\n<RECORD oid=\"0\" texte=\"\" />";

	
	$httpString = "\r\n<COMBO_PRESTATION>";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM activite.t_prestations WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;
				
				$code = ctiFloatVal(trim($record[0]));
				$texte = toHTML(trim($record[1]));
			
				$httpString_tmp .= "\r\n<RECORD oid=\"$code\" texte=\"$texte\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</COMBO_PRESTATION>";
	
	return $httpString;
}

function getCombo_mois() {

global $database;

	$httpString_tmp = "";
	
	$httpString = "\r\n<COMBO_MOIS>";

	$sqlcmd = "	SELECT 
					mois, 
					texte 
				FROM 
					base.p_calendrier_mois 
				WHERE 
					exercice_comptable IN (SELECT DISTINCT exercice_comptable FROM activite.p_mois_comptables) 
				ORDER BY mois DESC ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$code = toHTML(trim($record[0]));
				$texte = toHTML(trim($record[1]));
			
				$httpString_tmp .= "\r\n<RECORD oid=\"$code\" texte=\"$texte\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</COMBO_MOIS>";
	
	return $httpString;
}


function getCombo_mois_encours() {

global $database;

	$httpString_tmp = "";
	
	$httpString = "\r\n<COMBO_MOIS>";

	$sqlcmd = "SELECT MIN(p_calendrier_mois.date_debut) AS date_encours, p_calendrier_mois.texte, p_calendrier_mois.mois
				FROM activite.p_factures_encours
				JOIN base.p_calendrier_mois ON date_encours BETWEEN p_calendrier_mois.date_debut AND p_calendrier_mois.date_fin
				GROUP BY 2,3
				UNION
				SELECT '20991231'::date, 'Pas de forçage encours', 209912
				ORDER BY 3 DESC";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$code = toHTML(trim($record[0]));
				$texte = toHTML(trim($record[1]));
			
				$httpString_tmp .= "\r\n<RECORD oid=\"$code\" texte=\"$texte\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</COMBO_MOIS>";
	
	return $httpString;
}

function getCombo_comptes() {

global $database;

	$httpString_tmp = "";
	
	$httpString = "\r\n<COMBO_COMPTE>";

	$sqlcmd = "
					(
						SELECT 
							numero, 
							texte, 
							CASE WHEN clinique_honoraire = 'C' THEN '1'::text ELSE '0'::text END, 
							CASE WHEN clinique_honoraire = 'H' THEN '1'::text ELSE '0'::text END, 
							'0'::text 
						FROM 
							activite.t_comptes 
						WHERE t_comptes.compte_general_id = t_comptes.oid
					)
				UNION ALL 
					(
						SELECT 
							numero, 
							texte, 
							CASE WHEN clinique_honoraire = 'C' THEN '1'::text ELSE '0'::text END, 
							CASE WHEN clinique_honoraire = 'H' THEN '1'::text ELSE '0'::text END, 
							'1'::text 
						FROM 
							activite.t_comptes 
						WHERE
							t_comptes.compte_general_id <> t_comptes.oid
					) 
				ORDER BY 1, 5 ";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;

				$numero = toHTML(trim($record[0]));
				$texte = toHTML(trim($record[1]));
				$clinique = toHTML(trim($record[2]));
				$honoraire = toHTML(trim($record[3]));
			
				$httpString_tmp .= "\r\n<RECORD
										oid=\"$numero\" 
										texte=\"$texte\" 
										c=\"$clinique\" 
										h=\"$honoraire\" />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</COMBO_COMPTE>";
	
	return $httpString;
}

function getCombo_grands_regimes() {

global $database;

	$httpString = "\r\n<COMBO_GRG>";
	
	$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Non renseigné\" />";

	$sqlcmd = "SELECT oid, code || ' '::text || texte FROM base.t_grands_regimes WHERE oid > 0 ORDER BY code";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}
	$httpString .= "\r\n</COMBO_GRG>";
	
	return $httpString;
}

function getCombo_indicateurs() {

global $database;

	$httpString = "\r\n<COMBO_INDICATEUR>";
	
	$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Non renseignée\" />";

	$sqlcmd = "	SELECT 
					t_indicateurs.oid, 
					CASE 
						WHEN indicateur_reference_id = 0 THEN t_indicateurs.texte
						WHEN dimension_date = '1' THEN t_indicateurs.texte || ' (date '::text || dimensions_date_texte[1] || ')'::text
						WHEN dimension_date = '2' THEN t_indicateurs.texte || ' (date '::text || dimensions_date_texte[2] || ')'::text
						WHEN dimension_date = '3' THEN t_indicateurs.texte || ' (date '::text || dimensions_date_texte[3] || ')'::text
						WHEN dimension_date = '4' THEN t_indicateurs.texte || ' (date '::text || dimensions_date_texte[4] || ')'::text
						WHEN dimension_date = '5' THEN t_indicateurs.texte || ' (date '::text || dimensions_date_texte[5] || ')'::text
						ELSE t_indicateurs.texte END AS texte
				FROM 
					activite.t_indicateurs 
					LEFT JOIN activite.v_rapports_tables_indicateurs_1 ON t_indicateurs.table_name = v_rapports_tables_indicateurs_1.code
				WHERE 
					t_indicateurs.oid > 0 AND 
					t_indicateurs.table_name <> '*CALC' AND
					(indicateur_reference_id <> 0 OR COALESCE(dimension_date_2,'') = '')
				ORDER BY 2";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}
	$httpString .= "\r\n</COMBO_INDICATEUR>";
	
	return $httpString;
}

function getCombo_medecins_administratifs($tableName="") {

global $database;

	$httpString = "\r\n<COMBO_MDA>";
	
	if ($tableName == "t_services_facturation") {
		$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Ignoré\" />";
	}
	else {
		$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Pas de forçage. Médecin issu de la gestion administrative\" />";
	}

	$sqlcmd = "	SELECT
					oid, 
					nom || ' '::text || prenom || ' ('::text || code || ')'::text
				FROM 
					activite.t_medecins_administratifs
				WHERE 
					oid > 0 
				ORDER BY 2";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}
	$httpString .= "\r\n</COMBO_MDA>";
	
	return $httpString;
}

function getArray_sejours() {

global $database;

	$httpString = "\r\n<ARRAY_SEJ>";
	
	$httpString_tmp = "";
	
	$sqlcmd = "	SELECT 
					p_sejours.no_sejour, 
					p_patients.nom || ' '::text || p_patients.prenom
				FROM 
					activite.p_sejours 
					LEFT JOIN activite.p_patients ON p_sejours.no_patient = p_patients.no_patient
				ORDER BY no_sejour";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$code = toHTML(trim($record[0]));
				$texte = toHTML(trim($record[1]));
			
				$httpString_tmp .= "<RECORD code=\"$code\" texte=\"$texte\"/>";
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	$httpString .= $httpString_tmp;
	$httpString .= "\r\n</ARRAY_SEJ>";
	
	return $httpString;
}


function getCombo_budget_cle() {

global $database;
	
	$httpString = "\r\n<COMBO_BUDGET_CLE>";
	
	$httpString .= "\r\n<RECORD oid=\"0\" texte=\"Non renseignée\" />";

	$sqlcmd = "SELECT 
				oid, 
				code || ' '::text || texte
			FROM activite.t_budget_cle
			WHERE oid > 0
			ORDER BY 2";
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();

			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record[0];
				$texte = toHTML(trim($record[1]));
			
				$httpString .= "\r\n<RECORD oid=\"$oid\" texte=\"$texte\" />";
			}
		}
	}
	$httpString .= "\r\n</COMBO_BUDGET_CLE>";
	
	return $httpString;
}

function getRecords_expert_controle() {

global $database;
	
	$httpString = "";
	$httpString_tmp = "";
	
	// Activation module EXPERT
	$sqlcmd = "
	INSERT INTO activite.t_divers (code, texte, valeur, description)
	SELECT
		'EXPERT_ACTIVE',
		'Module EXPERT activé',
		'0',
		'1=Activé'
	WHERE 'EXPERT_ACTIVE' NOT IN (SELECT code FROM activite.t_divers);
	INSERT INTO activite.t_divers (code, texte, valeur, description, valeur_date)
	SELECT
		'EXPERT_DATE_DEBUT',
		'Date séjours à contrôler',
		'20130101',
		'Date de début des séjours à contrôler',
		'20130101'
	WHERE 'EXPERT_DATE_DEBUT' NOT IN (SELECT code FROM activite.t_divers);";
	$result = $database->exec($sqlcmd);
	
	// Correction données antérieures
	$sqlcmd = "UPDATE activite.t_expert_controle_rule 
		SET numero = 1 
		WHERE numero NOT IN (1,2,3) OR numero IS NULL;
		
		UPDATE activite.t_expert_controle
		SET gravite_id = 5
		WHERE gravite_id NOT IN (0,5,9) OR gravite_id IS NULL;
		
		UPDATE activite.t_expert_controle
		SET etat_cible = 0
		WHERE etat_cible NOT IN (0,1,2) OR etat_cible IS NULL;";
	$result = $database->exec($sqlcmd);
	
	// Paramètres généraux
	$EXPERT_ACTIVE = "0";
	$EXPERT_DATE_DEBUT = "2013-01-01";
	
	$sqlcmd = "SELECT code, valeur, to_char(valeur_date,'DD/MM/YYYY')
			FROM activite.t_divers
			WHERE code LIKE 'EXPERT%'";
	$result = $database->exec($sqlcmd);
	if ($result != false) {
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecordInto();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				switch(trim($record[0])) {
					case "EXPERT_ACTIVE" :
						$EXPERT_ACTIVE = trim($record[1]);
						break;
					case "EXPERT_DATE_DEBUT" :
						$EXPERT_DATE_DEBUT = trim($record[2]);
						break;
				}
			}
		}
	}
	
	// Chargement des Prestataires activité
	$sqlcmd = "
			(SELECT 
				-1 as oid, 
				'Tous'::text as texte, 
				0 as tri
			UNION
			SELECT 
				oid, 
				texte, 
				1 as tri 
			FROM base.t_prestataires
			WHERE 'activite' =  ANY (modules) 
			)
			ORDER BY tri,texte";
	$result = $database->exec($sqlcmd);
	
	if ($result !== false) {	
	
		while ($row = pg_fetch_array($result)) {
		
			$oid = $row['oid'];
			$texte = $row['texte'];
			
			$httpString .= "<PRESTATAIRE value=\"$oid\" label=\"$texte\" />" . PHP_EOL; 
		}
	}
	
	$httpString_tmp .= "<GENERAL_SETTINGS 
						EXPERT_ACTIVE=\"$EXPERT_ACTIVE\" 
						EXPERT_DATE_DEBUT=\"$EXPERT_DATE_DEBUT\" 
						/>";
	
	// Liste des controles
	$sqlcmd = "SELECT 
				t_expert_controle.oid as controle_id, 
				t_expert_controle.code as controle_code, 
				t_expert_controle.texte as controle_texte, 
				COALESCE(t_expert_controle.texte_court,t_expert_controle.texte) as controle_texte_court, 
				COALESCE(t_expert_controle.texte_court,t_expert_controle.texte,t_expert_controle.texte_abrege) as controle_texte_abrege, 
				t_expert_controle.description as controle_description,
				t_expert_controle.is_cti as controle_is_cti,
				t_expert_controle.is_hide as controle_is_hide,
				t_expert_controle.gravite_id as controle_gravite,
				COALESCE(t_expert_controle.etat_cible,0) as controle_etat_cible,
				t_expert_controle.last_execution_timestamp as controle_last_execution_timestamp,
				t_expert_controle.last_execution_ok as controle_last_execution_ok,
				t_expert_controle.prestataire_id,
				t_expert_controle.is_active as controle_is_active,
				COALESCE(t_prestataires.texte, 'Tous') as prestataire_texte,
				COALESCE(t_expert_controle_rule_1.oid,0) as rule1_oid,
				COALESCE(t_expert_controle_rule_1.sqlcmd_where) as rule1_sqlcmd_where,
				COALESCE(t_expert_controle_rule_1.sqlcmd_justificatif) as rule1_sqlcmd_justificatif,
				COALESCE(t_expert_controle_rule_1.table_id,0) as rule1_table_id,
				COALESCE(t_expert_controle_rule_2.oid,0) as rule2_oid,
				COALESCE(t_expert_controle_rule_2.sqlcmd_where) as rule2_sqlcmd_where,
				COALESCE(t_expert_controle_rule_2.sqlcmd_justificatif) as rule2_sqlcmd_justificatif,
				COALESCE(t_expert_controle_rule_2.table_id,0) as rule2_table_id,
				COALESCE(t_expert_controle_rule_3.oid,0) as rule3_oid,
				COALESCE(t_expert_controle_rule_3.sqlcmd_where) as rule3_sqlcmd_where,
				COALESCE(t_expert_controle_rule_3.sqlcmd_justificatif) as rule3_sqlcmd_justificatif,
				COALESCE(t_expert_controle_rule_3.table_id,0) as rule3_table_id,
				COALESCE(subview.nbref, 0) as nb_ref,
				COALESCE(t_expert_controle.alert_exp,'0') as is_alert
			FROM activite.t_expert_controle  
				LEFT JOIN base.t_prestataires ON t_prestataires.oid = t_expert_controle.prestataire_id
				LEFT JOIN activite.t_expert_controle_rule t_expert_controle_rule_1 ON t_expert_controle_rule_1.controle_id = t_expert_controle.oid AND t_expert_controle_rule_1.numero = 1
				LEFT JOIN activite.t_expert_controle_rule t_expert_controle_rule_2 ON t_expert_controle_rule_2.controle_id = t_expert_controle.oid AND t_expert_controle_rule_2.numero = 2
				LEFT JOIN activite.t_expert_controle_rule t_expert_controle_rule_3 ON t_expert_controle_rule_3.controle_id = t_expert_controle.oid AND t_expert_controle_rule_3.numero = 3
				LEFT JOIN (SELECT controle_id, count(*) AS nbref FROM activite.p_expert_sejour_controle GROUP BY 1) subview ON t_expert_controle.oid = subview.controle_id
			WHERE t_expert_controle.oid > 0 AND is_global <> 1
			ORDER BY t_expert_controle.code";
	$result = $database->exec($sqlcmd);
	
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordAssoc();
			
			if ($record != FALSE) {
				$ok = TRUE;
				
				$oid = $record['controle_id'];
				$code = toHTML(trim($record['controle_code']));
				$texte = toHTML(trim($record['controle_texte']));
				$texte_court = toHTML(trim($record['controle_texte_court']));
				$texte_abrege = toHTML(trim($record['controle_texte_abrege']));
				$description = toHTML(trim($record['controle_description']));
				$is_cti = toHTML(trim($record['controle_is_cti']));
				$is_active = toHTML(trim($record['controle_is_active']));
				$is_hide = toHTML(trim($record['controle_is_hide']));
				$etat_cible = ctiFloatVal(trim($record['controle_etat_cible']));
				$gravite = ctiFloatVal(trim($record['controle_gravite']));
				$rule_oid1 = ctiFloatVal(trim($record['rule1_oid']));
				$sqlcmd_where1 = toHTML(trim($record['rule1_sqlcmd_where']));
				$sqlcmd_justificatif1 = toHTML(trim($record['rule1_sqlcmd_justificatif']));
				$table1 = ctiFloatVal(trim($record['rule1_table_id']));
				$rule_oid2 = ctiFloatVal(trim($record['rule2_oid']));
				$sqlcmd_where2 = toHTML(trim($record['rule2_sqlcmd_where']));
				$sqlcmd_justificatif2 = toHTML(trim($record['rule2_sqlcmd_justificatif']));
				$table2 = ctiFloatVal(trim($record['rule2_table_id']));
				$rule_oid3 = ctiFloatVal(trim($record['rule3_oid']));
				$sqlcmd_where3 = toHTML(trim($record['rule3_sqlcmd_where']));
				$sqlcmd_justificatif3 = toHTML(trim($record['rule3_sqlcmd_justificatif']));
				$table3 = ctiFloatVal(trim($record['rule3_table_id']));
				$nbref = ctiFloatVal(trim($record['nb_ref']));
				$last_execution_timestamp = trim($record['controle_last_execution_timestamp']);
				$last_execution_ok = ctiFloatVal(trim($record['controle_last_execution_ok']));
				$prestataire_id = ctiFloatVal(trim($record['prestataire_id']));
				$prestataire_texte = toHTML(trim($record['prestataire_texte']));
				$is_alert = toHTML(trim($record['is_alert']));
				
				if ($last_execution_timestamp == "") {
					$last_execution_timestamp = "jamais exécuté";
					$last_execution_ok = "";
				}
				else {
					if ($last_execution_ok == "1") {
						$last_execution_ok = "OK";
					}
					if ($last_execution_ok == "0") {
						$last_execution_ok = "KO";
					}
				}
				
				$complement = "";
				if ($is_cti == "1") {
					$complement = "$complement CTI";
				}
				else {
					$is_cti = "0";
				}
				if ($is_hide == "1") {
					$complement = "$complement Masqué";
				}
				else {
					$is_hide = "0";
				}
				if ($is_alert == "1") {
					$complement = "$complement Alerte";
				}
				else {
					$is_alert = "0";
				}
				if ($gravite == 0) {
					$gravite_texte = "INFO";
				}
				else {
					if ($gravite == 9) {
						$gravite_texte = "ERREUR";
					}
					else {
						$gravite_texte = "ATT";
					}
				}
				switch ($etat_cible) {
					case 1 :
						$etat_cible_texte = "Présents";
						break;
					case 2 :
						$etat_cible_texte = "Non facturés";
						break;
					default :
						$etat_cible_texte = "Tous";
						break;
				}
				$is_active_texte = $is_active == "1" ? "Actif" : "Inactif";
				
				$md5_def = refmd5("$code$texte$texte_court$texte_abrege$description$is_cti$etat_cible$prestataire_id");
				$md5_sql = refmd5("$code$sqlcmd_where1$sqlcmd_justificatif1$table1$sqlcmd_where2$sqlcmd_justificatif2$table2$sqlcmd_where3$sqlcmd_justificatif3$table3");
				
				$httpString_tmp .= "
				<RECORD 
				selected=\"0\" 
				oid=\"$oid\" 
				code=\"$code\" 
				texte=\"$texte\" 
				texte_court=\"$texte_court\" 
				texte_abrege=\"$texte_abrege\" 
				description=\"$description\" 
				is_cti=\"$is_cti\" 
				is_hide=\"$is_hide\" 
				is_active=\"$is_active\" 
				is_active_texte=\"$is_active_texte\" 
				etat_cible=\"$etat_cible\" 
				etat_cible_texte=\"$etat_cible_texte\" 
				gravite=\"$gravite\" 
				gravite_texte=\"$gravite_texte\" 
				complement=\"$complement\" 
				rule_oid1=\"$rule_oid1\" 
				sqlcmd_where1=\"$sqlcmd_where1\" 
				sqlcmd_justificatif1=\"$sqlcmd_justificatif1\" 
				table1=\"$table1\" 
				rule_oid2=\"$rule_oid2\" 
				sqlcmd_where2=\"$sqlcmd_where2\" 
				sqlcmd_justificatif2=\"$sqlcmd_justificatif2\" 
				table2=\"$table2\" 
				rule_oid3=\"$rule_oid3\" 
				sqlcmd_where3=\"$sqlcmd_where3\" 
				sqlcmd_justificatif3=\"$sqlcmd_justificatif3\" 
				table3=\"$table3\" 
				last_execution_timestamp=\"$last_execution_timestamp\" 
				last_execution_ok=\"$last_execution_ok\" 
				prestataire_id=\"$prestataire_id\" 
				prestataire_texte=\"$prestataire_texte\" 
				nbref=\"$nbref\" 
				md5_def=\"$md5_def\"
				md5_sql=\"$md5_sql\"
				is_alert=\"$is_alert\"
				 />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	
	// Référence
	$httpString .= getRefSettings("t_expert_controle", $httpString);
	
	return $httpString;
}

function getRecords_expert_controle_refsettings($refPrefix) {

global $database;
	
	$httpString = "";
	$httpString_tmp = "";
	
	// Liste des controles
	$sqlcmd = "SELECT 
				t_expert_controle.oid AS controle_id,
				t_expert_controle.code AS controle_code,
				t_expert_controle.texte AS controle_texte,
				COALESCE(t_expert_controle.texte_court,t_expert_controle.texte) AS controle_texte_court,
				COALESCE(t_expert_controle.texte_court,t_expert_controle.texte,t_expert_controle.texte_abrege) AS controle_texte_abrege,
				t_expert_controle.description AS controle_description,
				t_expert_controle.is_cti AS controle_is_cti,
				t_expert_controle.is_hide AS controle_is_hide,
				t_expert_controle.gravite_id AS controle_gravite,
				COALESCE(t_expert_controle.etat_cible,0) AS controle_etat_cible,
				t_expert_controle.last_execution_timestamp AS controle_last_execution_timestamp,
				t_expert_controle.last_execution_ok AS controle_last_execution_ok,
				t_expert_controle.prestataire_id,
				COALESCE(t_prestataires.texte, 'Tous') as prestataire_texte,
				COALESCE(t_expert_controle_rule_1.oid,0) AS rule1_oid,
				COALESCE(t_expert_controle_rule_1.sqlcmd_where) AS rule1_sqlcmd_where,
				COALESCE(t_expert_controle_rule_1.sqlcmd_justificatif) AS rule1_sqlcmd_justificatif,
				COALESCE(t_expert_controle_rule_1.table_id,0) AS rule1_table_id,
				COALESCE(t_expert_controle_rule_2.oid,0) AS rule2_oid,
				COALESCE(t_expert_controle_rule_2.sqlcmd_where) AS rule2_sqlcmd_where,
				COALESCE(t_expert_controle_rule_2.sqlcmd_justificatif) AS rule2_sqlcmd_justificatif,
				COALESCE(t_expert_controle_rule_2.table_id,0) AS rule2_table_id,
				COALESCE(t_expert_controle_rule_3.oid,0) AS rule3_oid,
				COALESCE(t_expert_controle_rule_3.sqlcmd_where) AS rule3_sqlcmd_where,
				COALESCE(t_expert_controle_rule_3.sqlcmd_justificatif) AS rule3_sqlcmd_justificatif,
				COALESCE(t_expert_controle_rule_3.table_id,0) AS rule3_table_id,
                t_expert_controle.alert_exp as is_alert
			FROM activite.t_expert_controle  
				LEFT JOIN base.t_prestataires ON t_prestataires.oid = t_expert_controle.prestataire_id
				LEFT JOIN activite.t_expert_controle_rule t_expert_controle_rule_1 ON t_expert_controle_rule_1.controle_id = t_expert_controle.oid AND t_expert_controle_rule_1.numero = 1
				LEFT JOIN activite.t_expert_controle_rule t_expert_controle_rule_2 ON t_expert_controle_rule_2.controle_id = t_expert_controle.oid AND t_expert_controle_rule_2.numero = 2
				LEFT JOIN activite.t_expert_controle_rule t_expert_controle_rule_3 ON t_expert_controle_rule_3.controle_id = t_expert_controle.oid AND t_expert_controle_rule_3.numero = 3
			WHERE t_expert_controle.oid > 0 AND is_global <> 1 AND t_expert_controle.code ILIKE '$refPrefix%'
			ORDER BY t_expert_controle.code";
	$result = $database->exec($sqlcmd);
			
	if ($result != false) {
		
		// lignes 
		$ok = TRUE;
		
		while ($ok == TRUE) {
			$ok = FALSE;
			
			$record = $database->nextRecordInto();
			
			if ($record != FALSE) {
				$ok = TRUE;

				$oid = $record['controle_id'];
				$code = toHTML(trim($record['controle_code']));
				$texte = toHTML(trim($record['controle_texte']));
				$texte_court = toHTML(trim($record['controle_texte_court']));
				$texte_abrege = toHTML(trim($record['controle_texte_abrege']));
				$description = toHTML(trim($record['controle_description']));
				$is_cti = toHTML(trim($record['controle_is_cti']));
				$is_hide = toHTML(trim($record['controle_is_hide']));
				$gravite = ctiFloatVal(trim($record['controle_gravite']));
				$etat_cible = ctiFloatVal(trim($record['controle_etat_cible']));
				$last_execution_timestamp = toHTML(trim($record['controle_last_execution_timestamp']));
				$last_execution_ok = toHTML(trim($record['controle_last_execution_ok']));
				$rule_oid1 = ctiFloatVal(trim($record['rule1_oid']));
				$sqlcmd_where1 = toHTML(trim($record['rule1_sqlcmd_where']));
				$sqlcmd_justificatif1 = toHTML(trim($record['rule1_sqlcmd_justificatif']));
				$table1 = ctiFloatVal(trim($record['rule1_table_id']));
				$rule_oid2 = ctiFloatVal(trim($record['rule2_oid']));
				$sqlcmd_where2 = toHTML(trim($record['rule2_sqlcmd_where']));
				$sqlcmd_justificatif2 = toHTML(trim($record['rule2_sqlcmd_justificatif']));
				$table2 = ctiFloatVal(trim($record['rule2_table_id']));
				$rule_oid3 = ctiFloatVal(trim($record['rule3_oid']));
				$sqlcmd_where3 = toHTML(trim($record['rule3_sqlcmd_where']));
				$sqlcmd_justificatif3 = toHTML(trim($record['rule3_sqlcmd_justificatif']));
				$table3 = ctiFloatVal(trim($record['rule3_table_id']));
				$prestataire_id = ctiFloatVal(trim($record['prestataire_id']));
				$prestataire_texte = toHTML(trim($record['prestataire_texte']));
				$is_alert = toHTML(trim($record['is_alert']));
				
				if ($last_execution_timestamp == "") {
					$last_execution_ok = "";
				}
				else {
					if ($last_execution_ok == "1") {
						$last_execution_ok = "OK";
					}
					if ($last_execution_ok == "0") {
						$last_execution_ok = "KO";
					}
				}
				
				$complement = "";
				if ($is_cti == "1") {
					$complement = "$complement CTI";
				}
				else {
					$is_cti = "0";
				}
				if ($is_hide == "1") {
					$complement = "$complement Masqué";
				}
				else {
					$is_hide = "0";
				}
				if ($gravite == 0) {
					$gravite_texte = "INFO";
				}
				else {
					if ($gravite == 9) {
						$gravite_texte = "ERREUR";
					}
					else {
						$gravite_texte = "ATT";
					}
				}
				switch ($etat_cible) {
					case 1 :
						$etat_cible_texte = "Présents";
						break;
					case 2 :
						$etat_cible_texte = "Non facturés";
						break;
					default :
						$etat_cible_texte = "Tous";
						break;
				}
				$complement = trim($complement);
				
				$md5_def = refmd5("$code$texte$texte_court$texte_abrege$description$is_cti$etat_cible$prestataire_id");
				$md5_sql = refmd5("$code$sqlcmd_where1$sqlcmd_justificatif1$table1$sqlcmd_where2$sqlcmd_justificatif2$table2$sqlcmd_where3$sqlcmd_justificatif3$table3");
				
				$httpString_tmp .= "
				<RECORD 
				selected=\"0\" 
				oid=\"$oid\" 
				code=\"$code\" 
				texte=\"$texte\" 
				texte_court=\"$texte_court\" 
				texte_abrege=\"$texte_abrege\" 
				is_cti=\"$is_cti\" 
				is_hide=\"$is_hide\" 
				gravite=\"$gravite\" 
				etat_cible=\"$etat_cible\" 
				etat_cible_texte=\"$etat_cible_texte\" 
				gravite_texte=\"$gravite_texte\" 
				complement=\"$complement\"
				prestataire_id=\"$prestataire_id\"
				prestataire_texte=\"$prestataire_texte\" 
				md5_def=\"$md5_def\"
				md5_sql=\"$md5_sql\"
				is_alert=\"$is_alert\"
				 />";
				
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	
	$httpString .= $httpString_tmp;
	return $httpString;
}

function getRecords_kiwee_params() {

    global $database;
    
    $httpString = "";

    $sqlcmd = "	SELECT 
                    oid AS kiwee_params_oid, 
                    code AS kiwee_params_code, 
                    texte AS kiwee_params_texte, 
                    valeur::int AS kiwee_params_valeur, 
                    description AS kiwee_params_desc
                FROM activite.t_kiwee_params
                ORDER BY code ";

    $result = $database->exec($sqlcmd);

    if ($result != false) {
        // lignes 
        $ok = TRUE;

        while ($ok == TRUE) {
            $ok = FALSE;
        
            $record = $database->nextRecord();

            if ($record != FALSE) {
                $ok = TRUE;

                $oid = toHTML(trim($record['kiwee_params_oid']));
                $code = toHTML(trim($record['kiwee_params_code']));
                $texte = toHTML(trim($record['kiwee_params_texte']));
                $valeur = $record['kiwee_params_valeur'];
                $description = toHTML(trim($record['kiwee_params_desc']));
                
                $httpString .= "\r\n<RECORD
                                    oid=\"$oid\" 
                                    code=\"$code\" 
                                    texte=\"$texte\" 
                                    valeur=\"$valeur\" 
                                    description=\"$description\" />";
            }
        }
    }

    return $httpString;
}


// recherche des listes de réference
function getRefSettings($tableName, $httpString_base) {

	global $getOption;
	
	$httpString = "";
	
	$SETTINGS_file = rootDir() . "settings/settings.XML";
	$SETTINGS_xml = simplexml_load_file($SETTINGS_file);
	$REFSETTINGS_URL = getProperty($SETTINGS_xml, "REFSETTINGS", 'URL');
	$REFSETTINGS_PREFIX = getProperty($SETTINGS_xml, "REFSETTINGS", 'PREFIX');

	if ($REFSETTINGS_URL != "" && extension_loaded('curl')) {
			
		// Recherche des indicateurs de l'environnement de référence
		$REFSETTINGS_table_url = "$REFSETTINGS_URL/modules/activite/php/ACTI_getTableRecords.php";
		
		// Recherche http pour indicateurs
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $REFSETTINGS_table_url);
		curl_setopt($ch, CURLOPT_POST, 2);
		curl_setopt($ch, CURLOPT_POSTFIELDS, "tableName=$tableName&getOption=getRefSettings&refPrefix=$REFSETTINGS_PREFIX");
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		$REFSETTINGS_data = @curl_exec($ch);
		curl_close($ch);
		
		if ($REFSETTINGS_data) {
		
			if (substr($REFSETTINGS_data, 0, 12) == "<CTICONTENT>") {
				$REFSETTINGS_data = substr($REFSETTINGS_data, 12, strlen($REFSETTINGS_data) - 25);
			}
			
			$REFSETTINGS_data = uncompress($REFSETTINGS_data);
			$REFSETTINGS_data = str_replace("<RECORD ","<REFRECORD ",$REFSETTINGS_data);
			$REFSETTINGS_data = str_replace("</RECORD>","</REFRECORD>",$REFSETTINGS_data);
			$REFSETTINGS_xml = simplexml_load_string($REFSETTINGS_data);
			if ($REFSETTINGS_xml) {

				foreach ($REFSETTINGS_xml->REFRECORD as $recordNode) {
					$md5_def = trim($recordNode["md5_def"]);
					$md5_sql = trim($recordNode["md5_sql"]);
					$recordNode["ok"] = "";
					if (stripos($httpString_base, $md5_def) === false) {
						$recordNode["ok"] = "Définition";
					}
					if (stripos($httpString_base, $md5_sql) === false) {
						$recordNode["ok"] = "Règles";
					}
					//if ($recordNode["ok"] != "") {
						$httpString .= $recordNode->asXML();
					//}
				}
			}
		}
	}
	return $httpString;
}

function refmd5($string) {
	$string = str_replace("\r","",$string);
	$string = str_replace("\n","",$string);
	$string = md5(trim($string));
	return $string;
}


/**
 * Convertie une chaîne de caractère en double à la manière de ctiFloatVal().
 * Si la chaîne de caractère dépasse la limite de taille que ctiFloatVal() est capable de traiter,
 * la fonction retourne la chaîne de caractère sans conversion.
 */
function ctiFloatVal($stringValue) {
	$ret = 0;
	if (strlen($stringValue) <= 14) {
		$ret = floatval($stringValue);
	} else {
		if (preg_match("/[^0-9]/", $stringValue) === 0) {
			$ret = $stringValue;
		}
	}
    return $ret;
}    
?>