<?php
require_once("../../base/php/startSession.php");
require_once("../../base/php//classDatabase.php");
require_once("../../base/php/Functions.php" );
require_once("../../base/php/WebAppLog.php");
require_once("../../base/php/lib/cti/Log/SettingsChangesLogger.php");
require_once("../../outils/php/httpService_cacheManager_cleanCache.php");
require_once("./expert/ControleExpertManager.class.php");	
require_once("./expert/ControleExpert.class.php");	
require_once("./expert/RegleExpert.class.php");	

global $database;
global $action;
global $propagate;
global $recordNode;

$tableName = getPOST("tableName");
$action = getPOST("action");
$propagate = getPOST("propagate");
$recordXML = getPOST("recordNode");


$recordNode = @simplexml_load_string("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" . utf8_decode($recordXML));

$database = new Database("iCTI");

$settingsLogger = new SettingsChangesLogger;

try {
	$settingsLogger->logParameterChanges('activite', $tableName, $recordNode, $database);
} catch (Exception $e) {
	echo $e->getMessage();
}

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

$settersByTableToUpdate = array(
	't_services_facturation' => 'setRecords_services_facturation',
	't_activites' => 'setRecords_activites',
	't_divers' => 'setRecords_divers',
	't_unites_medicales' => 'setRecords_unites_medicales',
	't_unites_fonctionnelles' => 'setRecords_unites_fonctionnelles',
	't_etages' => 'setRecords_etages',
	't_lits' => 'setRecords_lits',
	't_compte' => 'setRecords_compte',
	't_service_rubrique' => 'setRecords_service_rubrique',
	't_service_rubrique_dotation' => 'setRecords_service_rubrique_dotation',
	't_prestations_c' => 'setRecords_prestations_c',
	't_prestations_h' => 'setRecords_prestations_h',
	't_rubriques_facturation' => 'setRecords_rubriques_facturation',
	't_types_tiers_payant' => 'setRecords_types_tiers_payant',
	't_tiers_payant' => 'setRecords_tiers_payant',
	't_specialites_medecin' => 'setRecords_specialites_internes_medecin',
	't_medecins_administratifs' => 'setRecords_medecins_administratifs',
	't_medecins_traitants_administratifs' => 'setRecords_medecins_traitants_administratifs',
	't_medecins' => 'setRecords_medecins',
	't_sejour' => 'setRecords_sejour',
	't_budget' => 'setRecords_budget',
	't_budget_cle' => 'setRecords_budget_cle',
	't_budget_cle_rule' => 'setRecords_budget_cle_rule',
	't_budget_cle_budget' => 'setRecords_budget_cle_budget',
	't_expert_controle' => 'setRecords_expert_controle',
	't_forme_activite' => 'setRecords_forme_activite',
	't_forme_activite_rule' => 'setRecords_forme_activite_rule',
	't_filiere_soin' => 'setRecords_filiere_soin',
	't_filiere_soin_rule' => 'setRecords_filiere_soin_rule',
	't_pole_rule' => 'setRecords_pole_rule',
	't_compte_produit_analytique_rule' => 'setRecords_compte_produit_analytique_rule',
	't_calcul_encours' => 'setRecords_calcul_encours',
    't_kiwee_params' => 'setRecords_kiwee_params'
);

$httpString = $httpString . $settersByTableToUpdate[$tableName]();

// Effacer le cache après maj parametre
erase_cache();

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


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


function setRecords_services_facturation() {

	global $database;
	global $action;
	global $propagate;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$nb_lits = utf8_decode($recordNode["nb_lits"]);
	$nb_cp = utf8_decode($recordNode["nb_cp"]);
	$type_t2a = utf8_decode($recordNode["type_t2a"]);
	$est_budget_global = utf8_decode($recordNode["est_budget_global"]);
	$est_sans_mouvement = utf8_decode($recordNode["est_sans_mouvement"]);
	if ($est_sans_mouvement != "1") {
		$est_sans_mouvement = "0";
	}
	$est_sans_facturation = utf8_decode($recordNode["est_sans_facturation"]);
	if ($est_sans_facturation != "1" && $est_sans_facturation != "2") {
		$est_sans_facturation = "0";
	}
	$avec_facturation_intermediaire = utf8_decode($recordNode["avec_facturation_intermediaire"]);
	if ($avec_facturation_intermediaire != "1") {
		$avec_facturation_intermediaire = "0";
	}
	$sej_sans_acte_fictif = utf8_decode($recordNode["sej_sans_acte_fictif"]);
	if ($sej_sans_acte_fictif != "1") {
		$sej_sans_acte_fictif = "0";
	}
	$finess_id = utf8_decode($recordNode["finess_id"]) + 0;
	$mode_traitement_id = utf8_decode($recordNode["mode_traitement_id"]) + 0;
	$dmt_id = utf8_decode($recordNode["dmt_id"]) + 0;
	$type_sejour = utf8_decode($recordNode["type_sejour"]);
	$etage_par_defaut_id = ctiFloatVal($recordNode["etage_par_defaut_id"]);
	$etage_force_id = ctiFloatVal($recordNode["etage_force_id"]);
    $activite_par_defaut_id = ctiFloatVal($recordNode["activite_par_defaut_id"]);
	$activite_force_id = ctiFloatVal($recordNode["activite_force_id"]);
	$medecin_par_defaut_id = ctiFloatVal($recordNode["medecin_par_defaut_id"]);
	$medecin_force_id = ctiFloatVal($recordNode["medecin_force_id"]);
	
	$option_jour_ferme = utf8_decode($recordNode["option_jour_ferme"]);
	if ($option_jour_ferme != "1") {
		$option_jour_ferme = "0";
	}
	$prestations_presence = utf8_decode($recordNode["prestations_presence"]);
	$particularites_encours = utf8_decode($recordNode["particularites_encours"]);
    $mode_calcul_journees = utf8_decode($recordNode["mode_calcul_journees"]);

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		
		$prestations_presence = toSQL($prestations_presence);
		$particularites_encours = toSQL($particularites_encours);
        $mode_calcul_journees = toSQL($mode_calcul_journees);
		
		$nb_lits = $nb_lits + 0;
		$nb_cp = $nb_cp + 0;
		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
		
			$sqlcmd = "UPDATE activite.t_services_facturation SET 
					code = '$code', 
					texte = '$texte', 
					texte_court = '$texte_court', 
					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',
					finess_id = $finess_id, 
					mode_traitement_id = $mode_traitement_id, 
					dmt_id = $dmt_id, 
					type_sejour = '$type_sejour',
					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,
					option_jour_ferme = '$option_jour_ferme',
					prestations_presence = '$prestations_presence',
					particularites_encours = '$particularites_encours',
					mode_calcul_journees='$mode_calcul_journees'
				WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
            	
			
			$sqlcmd = "DELETE FROM activite.t_services_facturation_historique WHERE service_facturation_id = $oid; ";
	
			
			foreach ($recordNode->HISTO as $place_node) {
				$date = trim($place_node["date"]);
				if (strlen($date) == 10) {
					$date = substr($date,6,4) . substr($date,3,2) . substr($date,0,2);
					$nb_lits_theoriques = ctiFloatVal($place_node["nb_lits_theoriques"]);
					$nb_lits_ouverts = ctiFloatVal($place_node["nb_lits_ouverts"]);
					$nb_box_ambulatoires_theoriques = ctiFloatVal($place_node["nb_box_ambulatoires_theoriques"]);
					$nb_box_ambulatoires_ouverts = ctiFloatVal($place_node["nb_box_ambulatoires_ouverts"]);
					$nb_box_seances_theoriques = ctiFloatVal($place_node["nb_box_seances_theoriques"]);
					$nb_box_seances_ouverts = ctiFloatVal($place_node["nb_box_seances_ouverts"]);
					$est_ouvert_1 = ctiFloatVal($place_node["est_ouvert_1"]);
					$est_ouvert_2 = ctiFloatVal($place_node["est_ouvert_2"]);
					$est_ouvert_3 = ctiFloatVal($place_node["est_ouvert_3"]);
					$est_ouvert_4 = ctiFloatVal($place_node["est_ouvert_4"]);
					$est_ouvert_5 = ctiFloatVal($place_node["est_ouvert_5"]);
					$est_ouvert_6 = ctiFloatVal($place_node["est_ouvert_6"]);
					$est_ouvert_7 = ctiFloatVal($place_node["est_ouvert_7"]);
					$sqlcmd = $sqlcmd . "INSERT INTO activite.t_services_facturation_historique(
						service_facturation_id, 
						date, 
						nb_lits_theoriques,
						nb_lits_ouverts,
						nb_box_ambulatoires_theoriques,
						nb_box_ambulatoires_ouverts,
						nb_box_seances_theoriques,
						nb_box_seances_ouverts,
						est_ouvert_1,
						est_ouvert_2,
						est_ouvert_3,
						est_ouvert_4,
						est_ouvert_5,
						est_ouvert_6,
						est_ouvert_7) 
						VALUES(
						$oid, 
						'$date', 
						$nb_lits_theoriques,
						$nb_lits_ouverts,
						$nb_box_ambulatoires_theoriques,
						$nb_box_ambulatoires_ouverts,
						$nb_box_seances_theoriques,
						$nb_box_seances_ouverts,
						'$est_ouvert_1',
						'$est_ouvert_2',
						'$est_ouvert_3',
						'$est_ouvert_4',
						'$est_ouvert_5',
						'$est_ouvert_6',
						'$est_ouvert_7'
						); "; 
				}
			}
			
						
						
			$result  =  $database->exec($sqlcmd);
			
			
			if ($propagate != "false") {
				$result = $database->exec("SELECT activite.cti_reorganize_lieux_c(); ");
	
				
				$sqlcmd = "INSERT INTO activite.p_oids (code_table, oid) 
					SELECT DISTINCT 'mode_traitement', t_services_facturation.mode_traitement_id FROM activite.t_lieux, activite.t_services_facturation 
					WHERE 
					service_facturation_id = t_services_facturation.oid AND
					t_services_facturation.mode_traitement_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'mode_traitement' AND oid IS NOT NULL) AND t_services_facturation.mode_traitement_id IS NOT NULL";
				$result  =  $database->exec($sqlcmd);
				
			}
			
			
		}
	}
}



function setRecords_activites() {

	global $database;
	global $action;
	global $propagate;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$etage_par_defaut_id = ctiFloatVal($recordNode["etage_par_defaut_id"]);
	$etage_force_id = ctiFloatVal($recordNode["etage_force_id"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_activites SET 
					code = '$code', 
					texte = '$texte', 
					texte_court = '$texte_court',
					etage_par_defaut_id = $etage_par_defaut_id,
					etage_force_id = $etage_force_id 
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			if ($propagate != "false") {
				$result = $database->exec("SELECT activite.cti_reorganize_lieux_c(); ");
			}
		}
	}
}


function setRecords_divers() {

	global $database;
	global $action;
	global $propagate;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$valeur = utf8_decode($recordNode["valeur"]);
	$description = utf8_decode($recordNode["description"]);
	$valeur2 = utf8_decode($recordNode["valeur2"]);
    $show_info_module = utf8_decode($recordNode["show_info_module"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$valeur = toSQL($valeur);
		$description = toSQL($description);
        $show_info_module = ctiFloatVal($show_info_module);
		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
		
			$sqlcmd = "UPDATE activite.t_divers SET ";
			$sqlcmd = $sqlcmd . "valeur = '$valeur', ";
			$sqlcmd = $sqlcmd . "valeur2 = '$valeur2', ";
			$sqlcmd = $sqlcmd . "show_info_module = ($show_info_module = 1) ";
			$sqlcmd = $sqlcmd . "WHERE code = '$code'";
			$result  =  $database->exec($sqlcmd);
			
			if ($propagate != "false") {
				if ($code == "PLACESREF") {
						$result = $database->exec("SELECT activite.cti_reorganize_lieux_c(); ");
				}
				if ($code == 'OPTADM_DAYOUT') {
					$result = $database->exec("SELECT activite.cti_calcul_journees_mouvements(); ");
					$result = $database->exec("SELECT activite.cti_calcul_hdj_mouvements(); ");
				}
			}
		}	
	}
}



function setRecords_unites_medicales() {

	global $database;
	global $action;
	global $propagate;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$etage_par_defaut_id = ctiFloatVal($recordNode["etage_par_defaut_id"]);
	$etage_force_id = ctiFloatVal($recordNode["etage_force_id"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_unites_medicales SET 
					code = '$code', 
					texte = '$texte', 
					texte_court = '$texte_court',
					etage_par_defaut_id = $etage_par_defaut_id,
					etage_force_id = $etage_force_id 
				WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			$sqlcmd = "DELETE FROM activite.t_unites_medicales_historique WHERE unite_medicale_id = $oid; ";
	
			
			foreach ($recordNode->HISTO as $place_node) {
				$date = trim($place_node["date"]);
				if (strlen($date) == 10) {
					$date = substr($date,6,4) . substr($date,3,2) . substr($date,0,2);
					$nb_lits_theoriques = ctiFloatVal($place_node["nb_lits_theoriques"]);
					$nb_lits_ouverts = ctiFloatVal($place_node["nb_lits_ouverts"]);
					$nb_box_ambulatoires_theoriques = ctiFloatVal($place_node["nb_box_ambulatoires_theoriques"]);
					$nb_box_ambulatoires_ouverts = ctiFloatVal($place_node["nb_box_ambulatoires_ouverts"]);
					$nb_box_seances_theoriques = ctiFloatVal($place_node["nb_box_seances_theoriques"]);
					$nb_box_seances_ouverts = ctiFloatVal($place_node["nb_box_seances_ouverts"]);
					$est_ouvert_1 = ctiFloatVal($place_node["est_ouvert_1"]);
					$est_ouvert_2 = ctiFloatVal($place_node["est_ouvert_2"]);
					$est_ouvert_3 = ctiFloatVal($place_node["est_ouvert_3"]);
					$est_ouvert_4 = ctiFloatVal($place_node["est_ouvert_4"]);
					$est_ouvert_5 = ctiFloatVal($place_node["est_ouvert_5"]);
					$est_ouvert_6 = ctiFloatVal($place_node["est_ouvert_6"]);
					$est_ouvert_7 = ctiFloatVal($place_node["est_ouvert_7"]);
					$sqlcmd = $sqlcmd . "INSERT INTO activite.t_unites_medicales_historique(
						unite_medicale_id, 
						date, 
						nb_lits_theoriques,
						nb_lits_ouverts,
						nb_box_ambulatoires_theoriques,
						nb_box_ambulatoires_ouverts,
						nb_box_seances_theoriques,
						nb_box_seances_ouverts,
						est_ouvert_1,
						est_ouvert_2,
						est_ouvert_3,
						est_ouvert_4,
						est_ouvert_5,
						est_ouvert_6,
						est_ouvert_7) 
						VALUES(
						$oid, 
						'$date', 
						$nb_lits_theoriques,
						$nb_lits_ouverts,
						$nb_box_ambulatoires_theoriques,
						$nb_box_ambulatoires_ouverts,
						$nb_box_seances_theoriques,
						$nb_box_seances_ouverts,
						'$est_ouvert_1',
						'$est_ouvert_2',
						'$est_ouvert_3',
						'$est_ouvert_4',
						'$est_ouvert_5',
						'$est_ouvert_6',
						'$est_ouvert_7'
						); "; 
				}
			}
			
						
						
			$result  =  $database->exec($sqlcmd);
			
			if ($propagate != "false") {
				$result = $database->exec("SELECT activite.cti_reorganize_lieux_c(); ");
			}
		}
	}
}


function setRecords_unites_fonctionnelles() {

	global $database;
	global $action;
	global $propagate;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$est_sans_facturation = utf8_decode($recordNode["est_sans_facturation"]);
	if ($est_sans_facturation != "1" && $est_sans_facturation != "2") {
		$est_sans_facturation = "0";
	}


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_unites_fonctionnelles SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court', ";
			$sqlcmd = $sqlcmd . "est_sans_facturation = '$est_sans_facturation' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			$sqlcmd = "DELETE FROM activite.t_unites_fonctionnelles_historique WHERE unite_fonctionnelle_id = $oid; ";
	
			
			foreach ($recordNode->HISTO as $place_node) {
				$date = trim($place_node["date"]);
				if (strlen($date) == 10) {
					$date = substr($date,6,4) . substr($date,3,2) . substr($date,0,2);
					$nb_lits_theoriques = ctiFloatVal($place_node["nb_lits_theoriques"]);
					$nb_lits_ouverts = ctiFloatVal($place_node["nb_lits_ouverts"]);
					$nb_box_ambulatoires_theoriques = ctiFloatVal($place_node["nb_box_ambulatoires_theoriques"]);
					$nb_box_ambulatoires_ouverts = ctiFloatVal($place_node["nb_box_ambulatoires_ouverts"]);
					$nb_box_seances_theoriques = ctiFloatVal($place_node["nb_box_seances_theoriques"]);
					$nb_box_seances_ouverts = ctiFloatVal($place_node["nb_box_seances_ouverts"]);
					$est_ouvert_1 = ctiFloatVal($place_node["est_ouvert_1"]);
					$est_ouvert_2 = ctiFloatVal($place_node["est_ouvert_2"]);
					$est_ouvert_3 = ctiFloatVal($place_node["est_ouvert_3"]);
					$est_ouvert_4 = ctiFloatVal($place_node["est_ouvert_4"]);
					$est_ouvert_5 = ctiFloatVal($place_node["est_ouvert_5"]);
					$est_ouvert_6 = ctiFloatVal($place_node["est_ouvert_6"]);
					$est_ouvert_7 = ctiFloatVal($place_node["est_ouvert_7"]);
					$sqlcmd = $sqlcmd . "INSERT INTO activite.t_unites_fonctionnelles_historique(
						unite_fonctionnelle_id, 
						date, 
						nb_lits_theoriques,
						nb_lits_ouverts,
						nb_box_ambulatoires_theoriques,
						nb_box_ambulatoires_ouverts,
						nb_box_seances_theoriques,
						nb_box_seances_ouverts,
						est_ouvert_1,
						est_ouvert_2,
						est_ouvert_3,
						est_ouvert_4,
						est_ouvert_5,
						est_ouvert_6,
						est_ouvert_7) 
						VALUES(
						$oid, 
						'$date', 
						$nb_lits_theoriques,
						$nb_lits_ouverts,
						$nb_box_ambulatoires_theoriques,
						$nb_box_ambulatoires_ouverts,
						$nb_box_seances_theoriques,
						$nb_box_seances_ouverts,
						'$est_ouvert_1',
						'$est_ouvert_2',
						'$est_ouvert_3',
						'$est_ouvert_4',
						'$est_ouvert_5',
						'$est_ouvert_6',
						'$est_ouvert_7'
						); "; 
				}
			}
			
						
						
			$result  =  $database->exec($sqlcmd);
			
			
			if ($propagate != "false") {
				$result = $database->exec("SELECT activite.cti_reorganize_lieux_c(); ");
			}
		}
	}	
}


function setRecords_etages() {

	global $database;
	global $action;
	global $propagate;
	global $recordNode;
	
	$etage_id = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$nb_lits = utf8_decode($recordNode["nb_lits"]);
	$nb_cp = utf8_decode($recordNode["nb_cp"]);
	$remplacer_par_valeur_par_defaut = utf8_decode($recordNode["remplacer_par_valeur_par_defaut"]);
    $est_urgence = utf8_decode($recordNode["est_urgence"]);
    $service_force_id = ctiFloatVal($recordNode["service_force_id"]);
	$finess_id = utf8_decode($recordNode["finess_id"]) + 0;
	


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		
		$nb_lits = $nb_lits + 0;
		$nb_cp = $nb_cp + 0;
		
		// creation ou mise à  jour de la liste
		if ($etage_id != -1) {
			$sqlcmd = "UPDATE activite.t_etages SET 
				code = '$code', 
				texte = '$texte', 
				texte_court = '$texte_court', 
				nb_lits = $nb_lits, 
				nb_cp = $nb_cp,
				remplacer_par_valeur_par_defaut = '$remplacer_par_valeur_par_defaut',
				est_urgence = '$est_urgence',
				service_force_id = $service_force_id,
				finess_id = $finess_id
				WHERE oid = $etage_id";
			$result  =  $database->exec($sqlcmd);
			
			$sqlcmd = "";
			
			foreach ($recordNode->LIT as $lit_node) {
				$oid = ctiFloatVal($lit_node["oid"]);
				$chambre_particuliere = trim($lit_node["chambre_particuliere"]);
				if ($chambre_particuliere == "1") {
					$chambre_particuliere = "O";
				}
				else {	
					$chambre_particuliere = "N";
				}
				$sqlcmd = $sqlcmd . "UPDATE activite.t_lits
					SET chambre_particuliere = '$chambre_particuliere'
					WHERE oid = $oid AND oid > 0 AND chambre_particuliere IS DISTINCT FROM '$chambre_particuliere';"; 
			}
			
			$result  =  $database->exec($sqlcmd);
			
			
			
			
			$sqlcmd = "DELETE FROM activite.t_etages_historique WHERE etage_id = $etage_id; ";
	
			
			foreach ($recordNode->HISTO as $place_node) {
				$date = trim($place_node["date"]);
				if (strlen($date) == 10) {
					$date = substr($date,6,4) . substr($date,3,2) . substr($date,0,2);
					$nb_lits_theoriques = ctiFloatVal($place_node["nb_lits_theoriques"]);
					$nb_lits_ouverts = ctiFloatVal($place_node["nb_lits_ouverts"]);
					$nb_lits_cp_theoriques = ctiFloatVal($place_node["nb_lits_cp_theoriques"]);
					$nb_lits_cp_ouverts = ctiFloatVal($place_node["nb_lits_cp_ouverts"]);
					$nb_box_ambulatoires_theoriques = ctiFloatVal($place_node["nb_box_ambulatoires_theoriques"]);
					$nb_box_ambulatoires_ouverts = ctiFloatVal($place_node["nb_box_ambulatoires_ouverts"]);
					$nb_box_seances_theoriques = ctiFloatVal($place_node["nb_box_seances_theoriques"]);
					$nb_box_seances_ouverts = ctiFloatVal($place_node["nb_box_seances_ouverts"]);
					$est_ouvert_1 = ctiFloatVal($place_node["est_ouvert_1"]);
					$est_ouvert_2 = ctiFloatVal($place_node["est_ouvert_2"]);
					$est_ouvert_3 = ctiFloatVal($place_node["est_ouvert_3"]);
					$est_ouvert_4 = ctiFloatVal($place_node["est_ouvert_4"]);
					$est_ouvert_5 = ctiFloatVal($place_node["est_ouvert_5"]);
					$est_ouvert_6 = ctiFloatVal($place_node["est_ouvert_6"]);
					$est_ouvert_7 = ctiFloatVal($place_node["est_ouvert_7"]);
					$lit_force = trim($place_node["lit_force"]);
					$sqlcmd = $sqlcmd . "INSERT INTO activite.t_etages_historique(
						etage_id, 
						date, 
						nb_lits_theoriques,
						nb_lits_ouverts,
						nb_lits_cp_theoriques,
						nb_lits_cp_ouverts,
						nb_box_ambulatoires_theoriques,
						nb_box_ambulatoires_ouverts,
						nb_box_seances_theoriques,
						nb_box_seances_ouverts,
						est_ouvert_1,
						est_ouvert_2,
						est_ouvert_3,
						est_ouvert_4,
						est_ouvert_5,
						est_ouvert_6,
						est_ouvert_7,
						lit_force) 
						VALUES(
						$etage_id, 
						'$date', 
						$nb_lits_theoriques,
						$nb_lits_ouverts,
						$nb_lits_cp_theoriques,
						$nb_lits_cp_ouverts,
						$nb_box_ambulatoires_theoriques,
						$nb_box_ambulatoires_ouverts,
						$nb_box_seances_theoriques,
						$nb_box_seances_ouverts,
						'$est_ouvert_1',
						'$est_ouvert_2',
						'$est_ouvert_3',
						'$est_ouvert_4',
						'$est_ouvert_5',
						'$est_ouvert_6',
						'$est_ouvert_7',
						'$lit_force'
						); "; 
				}
			}
			
						
						
			$result  =  $database->exec($sqlcmd);
			
			
			if ($propagate != "false") {
				$result = $database->exec("SELECT activite.cti_reorganize_lieux_c(); ");
			}
		}
	}	
}



function setRecords_lits() {

	global $database;
	global $action;
	global $propagate;
	global $recordNode;
	
	$lit_id = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$est_chambre_particuliere = utf8_decode($recordNode["est_chambre_particuliere"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		if ($est_chambre_particuliere == "1") {
			$chambre_particuliere = "O";
		}
		else {
			$chambre_particuliere = "N";
		}
		
		
		// creation ou mise à  jour de la liste
		if ($lit_id != -1) {
			$sqlcmd = "UPDATE activite.t_lits SET 
				code = '$code', 
				texte = '$texte', 
				texte_court = '$texte_court', 
				chambre_particuliere = '$chambre_particuliere'
				WHERE oid = $lit_id";
			$result  =  $database->exec($sqlcmd);
			
			
			
			// Historique des étages par lit
			$sqlcmd = "DELETE FROM activite.t_lit_historique WHERE lit_id = $lit_id; ";
			$one_historique = FALSE;
			foreach ($recordNode->ETAGE as $etage_node) {
				$date = trim($etage_node["date"]);
				if (strlen($date) == 10) {
					$date = substr($date,6,4) . substr($date,3,2) . substr($date,0,2);
					$etage_id = ctiFloatVal($etage_node["oid"]);
					$sqlcmd = $sqlcmd . "INSERT INTO activite.t_lit_historique(
						lit_id, 
						date_debut, 
						date_fin,
						etage_id) 
						VALUES(
						$lit_id, 
						'$date', 
						'20991231',
						$etage_id
						); "; 
						
					$one_historique = TRUE;
				}
			}
			
			if ($one_historique == TRUE) {
				$sqlcmd = $sqlcmd . "UPDATE activite.t_lits
							SET etage_id = subview.etage_id
							FROM (
								SELECT
									lit_id,
									(MAX(ARRAY[date_fin::text, etage_id::text]))[2]::bigint AS etage_id
								FROM (
									SELECT
										lit_id, 
										etage_id,
										date_debut,
										date_fin
									FROM activite.t_lit_historique
									WHERE lit_id = $lit_id
									ORDER BY lit_id
								) subview
								GROUP BY lit_id
							) subview
							WHERE t_lits.oid = subview.lit_id AND
								t_lits.etage_id IS DISTINCT FROM subview.etage_id;";
	
					$sqlcmd = $sqlcmd . "UPDATE activite.t_lit_historique
							SET date_fin = subview.date_fin
							FROM (
								SELECT
									oid,
									lit_id, 
									date(COALESCE((SELECT MIN(date_debut) - interval '1 day' 
									FROM activite.t_lit_historique t_lit_historique_next
									WHERE 
										t_lit_historique.lit_id = t_lit_historique_next.lit_id AND
										t_lit_historique.date_debut < t_lit_historique_next.date_debut
									),'20991231')) AS date_fin
								FROM activite.t_lit_historique
								WHERE lit_id = $lit_id
							) subview
							WHERE t_lit_historique.oid = subview.oid AND
								t_lit_historique.date_fin IS DISTINCT FROM subview.date_fin;";
	
			}
						
						
			$result  =  $database->exec($sqlcmd);
			
			
			if ($propagate != "false") {
				$result = $database->exec("SELECT activite.cti_reorganize_lieux_c(); ");
			}
		}
	}	
}


function setRecords_compte() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$taux_tva = ctiFloatVal($recordNode["taux_tva"]);
	$compte_tva_id = ctiFloatVal($recordNode["compte_tva_id"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);

		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_compte (code, code_original, texte, texte_court, taux_tva, compte_tva_id) VALUES('$code','$code','$texte','$texte_court', $taux_tva, $compte_tva_id )";
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_compte SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court', ";
			$sqlcmd = $sqlcmd . "taux_tva = $taux_tva, ";
			$sqlcmd = $sqlcmd . "compte_tva_id = $compte_tva_id ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
				

	}
	

}

function setRecords_service_rubrique() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$service_id = ctiFloatVal($recordNode["service_id"]);
	$rubrique_id = ctiFloatVal($recordNode["rubrique_id"]);
	$compte_id = ctiFloatVal($recordNode["compte_id"]);
	$est_compte_force = utf8_decode($recordNode["est_compte_force"]);
	$prestation_forcee_id = ctiFloatVal($recordNode["prestation_forcee_id"]);


	if ($action != "delete" && $action != "copy") {

		$sqlcmd = "
				UPDATE activite.t_service_rubrique SET
					compte_id = $compte_id,
					est_compte_force = '$est_compte_force',
					prestation_forcee_id = $prestation_forcee_id
				WHERE service_facturation_id = $service_id AND
					rubrique_facturation_id = $rubrique_id;
				INSERT INTO activite.t_service_rubrique
					(service_facturation_id, rubrique_facturation_id, compte_id, est_compte_force, prestation_forcee_id)
				SELECT $service_id,$rubrique_id,$compte_id,'$est_compte_force', $prestation_forcee_id
				WHERE $service_id::text || '-' || $rubrique_id::text NOT IN (
					SELECT service_facturation_id::text || '-' || rubrique_facturation_id::text FROM activite.t_service_rubrique
					);";

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

	}
	

}


function setRecords_service_rubrique_dotation() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
    $texte = utf8_decode($recordNode["texte"]);
    $texte = toSQL($texte);
    $annee = ctiFloatVal($recordNode["annee"]);
    $dotation = ctiFloatVal(str_replace(',','.',$recordNode["dotation"]));
    
	$service_1_id = ctiFloatVal($recordNode["service_1_id"]);
	$service_2_id = ctiFloatVal($recordNode["service_2_id"]);
    $service_3_id = ctiFloatVal($recordNode["service_3_id"]);
    $service_4_id = ctiFloatVal($recordNode["service_4_id"]);
    $service_5_id = ctiFloatVal($recordNode["service_5_id"]);
    $service_6_id = ctiFloatVal($recordNode["service_6_id"]);
    $service_7_id = ctiFloatVal($recordNode["service_7_id"]);
    $service_8_id = ctiFloatVal($recordNode["service_8_id"]);
    $service_9_id = ctiFloatVal($recordNode["service_9_id"]);
    $rubrique_1_id = ctiFloatVal($recordNode["rubrique_1_id"]);
	$rubrique_2_id = ctiFloatVal($recordNode["rubrique_2_id"]);
    $rubrique_3_id = ctiFloatVal($recordNode["rubrique_3_id"]);
    $rubrique_4_id = ctiFloatVal($recordNode["rubrique_4_id"]);
    $rubrique_5_id = ctiFloatVal($recordNode["rubrique_5_id"]);
    $rubrique_6_id = ctiFloatVal($recordNode["rubrique_6_id"]);
    $rubrique_7_id = ctiFloatVal($recordNode["rubrique_7_id"]);
    $rubrique_8_id = ctiFloatVal($recordNode["rubrique_8_id"]);
    $rubrique_9_id = ctiFloatVal($recordNode["rubrique_9_id"]);
    $rubrique_10_id = ctiFloatVal($recordNode["rubrique_10_id"]);
    
    $to_rubrique_id = ctiFloatVal($recordNode["to_rubrique_id"]);
    $to_prestation_id = ctiFloatVal($recordNode["to_prestation_id"]);
    
    $service_id_array = "";
    if ($service_1_id != 0) {
         $service_id_array = "$service_1_id";
    }
    if ($service_2_id != 0) {
         if ($service_id_array != "") {
             $service_id_array = "$service_id_array,";
         }
         $service_id_array = "$service_id_array$service_2_id";
    }
    if ($service_3_id != 0) {
         if ($service_id_array != "") {
             $service_id_array = "$service_id_array,";
         }
         $service_id_array = "$service_id_array$service_3_id";
    }
    if ($service_4_id != 0) {
         if ($service_id_array != "") {
             $service_id_array = "$service_id_array,";
         }
         $service_id_array = "$service_id_array$service_4_id";
    }
    if ($service_5_id != 0) {
         if ($service_id_array != "") {
             $service_id_array = "$service_id_array,";
         }
         $service_id_array = "$service_id_array$service_5_id";
    }
    if ($service_6_id != 0) {
         if ($service_id_array != "") {
             $service_id_array = "$service_id_array,";
         }
         $service_id_array = "$service_id_array$service_6_id";
    }
    if ($service_7_id != 0) {
         if ($service_id_array != "") {
             $service_id_array = "$service_id_array,";
         }
         $service_id_array = "$service_id_array$service_7_id";
    }
    if ($service_8_id != 0) {
         if ($service_id_array != "") {
             $service_id_array = "$service_id_array,";
         }
         $service_id_array = "$service_id_array$service_8_id";
    }
    if ($service_9_id != 0) {
         if ($service_id_array != "") {
             $service_id_array = "$service_id_array,";
         }
         $service_id_array = "$service_id_array$service_9_id";
    }
    if ($service_id_array != "") {
        $service_id_array = "ARRAY[$service_id_array]";
    }
    else {
        $service_id_array = "NULL";
    }
    
    $rubrique_id_array = "";
    if ($rubrique_1_id != 0) {
         $rubrique_id_array = "$rubrique_1_id";
    }
    if ($rubrique_2_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_2_id";
    }
    if ($rubrique_3_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_3_id";
    }
    if ($rubrique_4_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_4_id";
    }
    if ($rubrique_5_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_5_id";
    }
    if ($rubrique_6_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_6_id";
    }
    if ($rubrique_7_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_7_id";
    }
    if ($rubrique_8_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_8_id";
    }
    if ($rubrique_9_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_9_id";
    }
    if ($rubrique_10_id != 0) {
         if ($rubrique_id_array != "") {
             $rubrique_id_array = "$rubrique_id_array,";
         }
         $rubrique_id_array = "$rubrique_id_array$rubrique_10_id";
    }
    if ($rubrique_id_array != "") {
        $rubrique_id_array = "ARRAY[$rubrique_id_array]";
    }
    else {
        $rubrique_id_array = "NULL";
    }


	if ($action != "delete" && $action != "copy") {

		$sqlcmd = "
				UPDATE activite.t_service_rubrique_dotation SET
					annee = $annee,
					texte = '$texte',
					montant_dotation = $dotation,
                    service_facturation_id_array = $service_id_array,
                    rubrique_facturation_id_array = $rubrique_id_array,
                    to_rubrique_facturation_id = $to_rubrique_id,
                    to_prestation_id = $to_prestation_id
				WHERE oid = $oid;";

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

	}
}



function setRecords_prestations_c() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$type_valorisation_non_facture = utf8_decode($recordNode["type_valorisation_non_facture"]);
	$type_ventilation_jour = utf8_decode($recordNode["type_ventilation_jour"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		$type_valorisation_non_facture = toSQL($type_valorisation_non_facture);
		$type_ventilation_jour = toSQL($type_ventilation_jour);
		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_prestations SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court', ";
			$sqlcmd = $sqlcmd . "type_valorisation_non_facture = '$type_valorisation_non_facture', ";
			$sqlcmd = $sqlcmd . "type_ventilation_jour = '$type_ventilation_jour' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
	}

}

function setRecords_prestations_h() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);

		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_prestations SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
	}	
}


function setRecords_rubriques_facturation() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$actes_inclus_public = utf8_decode($recordNode["actes_inclus_public"]);
	$est_force_etablissement = utf8_decode($recordNode["est_force_etablissement"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		$actes_inclus_public = toSQL($actes_inclus_public);
		$est_force_etablissement = toSQL($est_force_etablissement);

		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_rubriques_facturation SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court', ";
			$sqlcmd = $sqlcmd . "actes_inclus_public = '$actes_inclus_public', ";
			$sqlcmd = $sqlcmd . "est_force_etablissement = '$est_force_etablissement' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
	}	
}


function setRecords_types_tiers_payant() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_types_tiers_payant SET ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
			$sqlcmd = $sqlcmd . "WHERE code = '$oid'";
			$result  =  $database->exec($sqlcmd);
		}
	}
	
}

function setRecords_tiers_payant() {

	global $database;
	global $action;
	global $propagate;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$grand_regime_id = ctiFloatVal($recordNode["grand_regime_id"]);

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);

		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_tiers_payant SET 
							code = '$code', 
							texte = '$texte', 
							texte_court = '$texte_court',
							grand_regime_id = $grand_regime_id
						WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
	}
		
	
	if ($propagate != "false") {
		$result = $database->exec("SELECT activite.cti_reorganize_tiers_payant(); ");
	}
	

}


function setRecords_specialites_internes_medecin() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO base.t_specialites_medecin ";
			$sqlcmd = $sqlcmd . "( ";
			$sqlcmd = $sqlcmd . "code, texte, texte_court";
			$sqlcmd = $sqlcmd . ") ";
			$sqlcmd = $sqlcmd . "VALUES(";
			$sqlcmd = $sqlcmd . "'$code', '$texte', '$texte_court'";
			$sqlcmd = $sqlcmd . ") ";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
				
			}
		}
		else {
			
			$sqlcmd = "UPDATE base.t_specialites_medecin SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			
			$sqlcmd = "UPDATE activite.t_medecins_administratifs_c SET ";
			$sqlcmd = $sqlcmd . "adm_code = t_medecins_administratifs.code, ";
			$sqlcmd = $sqlcmd . "adm_nom = t_medecins_administratifs.nom, ";
			$sqlcmd = $sqlcmd . "adm_prenom = t_medecins_administratifs.prenom, ";
			$sqlcmd = $sqlcmd . "adm_nom_prenom = t_medecins_administratifs.nom || ' ' || t_medecins_administratifs.prenom, ";
			$sqlcmd = $sqlcmd . "medecin_id = t_medecins_administratifs.medecin_id, ";
			$sqlcmd = $sqlcmd . "nom = t_medecins.nom, ";
			$sqlcmd = $sqlcmd . "prenom = t_medecins.prenom, ";
			$sqlcmd = $sqlcmd . "nom_prenom = t_medecins.nom || ' ' || t_medecins.prenom, ";
			$sqlcmd = $sqlcmd . "specialite_id = t_medecins.specialite_id, ";
			$sqlcmd = $sqlcmd . "specialite_code = t_specialites_medecin.code, ";
			$sqlcmd = $sqlcmd . "specialite_texte = t_specialites_medecin.texte_court ";
			$sqlcmd = $sqlcmd . "FROM activite.t_medecins_administratifs, base.t_medecins, base.t_specialites_medecin ";
			$sqlcmd = $sqlcmd . "WHERE t_medecins_administratifs_c.oid = t_medecins_administratifs.oid ";
			$sqlcmd = $sqlcmd . "AND t_medecins_administratifs.medecin_id = t_medecins.oid ";
			$sqlcmd = $sqlcmd . "AND t_medecins.specialite_id = t_specialites_medecin.oid  ";
			$sqlcmd = $sqlcmd . "AND t_medecins.specialite_id = $oid ";
			$result  =  $database->exec($sqlcmd);
			
			
		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM base.t_specialites_medecin WHERE oid = $oid";
		$result  =  $database->exec($sqlcmd);
	}


}


function setRecords_medecins_administratifs() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$nom = utf8_decode($recordNode["nom"]);
	$prenom = utf8_decode($recordNode["prenom"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$est_medecin_salarie = utf8_decode($recordNode["est_medecin_salarie"]);
	$kiwee_sigems_honpatcpt = utf8_decode($recordNode["kiwee_sigems_honpatcpt"]);
	$kiwee_sigems_honpatcptdep = utf8_decode($recordNode["kiwee_sigems_honpatcptdep"]);

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$nom = toSQL($nom);
		$prenom = toSQL($prenom);
		$code_original = toSQL($code_original);

		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_medecins_administratifs SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "nom = '$nom', ";
			$sqlcmd = $sqlcmd . "prenom = '$prenom', ";
			$sqlcmd = $sqlcmd . "est_medecin_salarie = '$est_medecin_salarie', ";
			$sqlcmd = $sqlcmd . "kiwee_sigems_honpatcpt = '$kiwee_sigems_honpatcpt', ";
			$sqlcmd = $sqlcmd . "kiwee_sigems_honpatcptdep = '$kiwee_sigems_honpatcptdep' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
	
			$sqlcmd = "UPDATE activite.t_medecins_administratifs_c SET ";
			$sqlcmd = $sqlcmd . "adm_code = t_medecins_administratifs.code, ";
			$sqlcmd = $sqlcmd . "adm_nom = t_medecins_administratifs.nom, ";
			$sqlcmd = $sqlcmd . "adm_prenom = t_medecins_administratifs.prenom, ";
			$sqlcmd = $sqlcmd . "adm_nom_prenom = t_medecins_administratifs.nom || ' ' || t_medecins_administratifs.prenom, ";
			$sqlcmd = $sqlcmd . "medecin_id = t_medecins_administratifs.medecin_id, ";
			$sqlcmd = $sqlcmd . "nom = t_medecins.nom, ";
			$sqlcmd = $sqlcmd . "prenom = t_medecins.prenom, ";
			$sqlcmd = $sqlcmd . "nom_prenom = t_medecins.nom || ' ' || t_medecins.prenom, ";
			$sqlcmd = $sqlcmd . "specialite_id = t_medecins.specialite_id, ";
			$sqlcmd = $sqlcmd . "specialite_code = t_specialites_medecin.code, ";
			$sqlcmd = $sqlcmd . "specialite_texte = t_specialites_medecin.texte_court ";
			$sqlcmd = $sqlcmd . "FROM activite.t_medecins_administratifs, base.t_medecins, base.t_specialites_medecin ";
			$sqlcmd = $sqlcmd . "WHERE t_medecins_administratifs_c.oid = t_medecins_administratifs.oid ";
			$sqlcmd = $sqlcmd . "AND t_medecins_administratifs.medecin_id = t_medecins.oid ";
			$sqlcmd = $sqlcmd . "AND t_medecins.specialite_id = t_specialites_medecin.oid  ";
			$sqlcmd = $sqlcmd . "AND t_medecins_administratifs.oid = $oid ";
			$result  =  $database->exec($sqlcmd);
		}
	}
		$result  =  $database->exec("SELECT activite.cti_reorganize_medecins_administratifs_c(); ");
}


function setRecords_medecins_traitants_administratifs() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$nom = utf8_decode($recordNode["nom"]);
	$prenom = utf8_decode($recordNode["prenom"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$no_rpps = utf8_decode($recordNode["no_rpps"]);
	$code_postal = utf8_decode($recordNode["code_postal"]);
	$ville = str_replace("'","''",utf8_decode($recordNode["ville"]));
	$adresse = str_replace("'","''",utf8_decode($recordNode["adresse"]));

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$nom = toSQL($nom);
		$prenom = toSQL($prenom);
		$code_original = toSQL($code_original);

		
		// creation ou mise à  jour de la liste
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_medecins_traitants_administratifs SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "nom = '$nom', ";
			$sqlcmd = $sqlcmd . "prenom = '$prenom', ";
			$sqlcmd = $sqlcmd . "no_rpps = '$no_rpps', ";
			$sqlcmd = $sqlcmd . "ville = '$ville', ";
			$sqlcmd = $sqlcmd . "adresse = '$adresse' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			$sqlcmd = "UPDATE activite.t_medecins_traitants_administratifs SET ";
			$sqlcmd = $sqlcmd . "code_postal_id = 0";
			$result  =  $database->exec($sqlcmd);
			
			$sqlcmd = "UPDATE activite.t_medecins_traitants_administratifs SET ";
			$sqlcmd = $sqlcmd . "code_postal_id = t_codes_postaux.oid ";
			$sqlcmd = $sqlcmd . "FROM base.t_codes_postaux ";
			$sqlcmd = $sqlcmd . "WHERE t_codes_postaux.code = '$code_postal' AND t_medecins_traitants_administratifs.oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
	}
}

function setRecords_medecins() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$nom = utf8_decode($recordNode["nom"]);
	$prenom = utf8_decode($recordNode["prenom"]);
	$code_original = utf8_decode($recordNode["code_original"]);
	$numero_ordre = utf8_decode($recordNode["numero_ordre"]);
	$specialite_interne_id = utf8_decode($recordNode["specialite_interne_id"]);
	$medecins_regroupes_oids = utf8_decode($recordNode["medecins_regroupes_oids"]);

	$medecins_regroupes_oids = "-1$medecins_regroupes_oids-1";
	
	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$nom = toSQL($nom);
		$prenom = toSQL($prenom);
		$code_original = toSQL($code_original);
		$numero_ordre = toSQL($numero_ordre);
		$specialite_interne_id = toSQL($specialite_interne_id);
		$medecins_regroupes_oids = toSQL($medecins_regroupes_oids);

		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO base.t_medecins ";
			$sqlcmd = $sqlcmd . "( ";
			$sqlcmd = $sqlcmd . "nom, prenom, numero_ordre, specialite_id";
			$sqlcmd = $sqlcmd . ") ";
			$sqlcmd = $sqlcmd . "VALUES(";
			$sqlcmd = $sqlcmd . "'$nom', '$prenom', '$numero_ordre', $specialite_interne_id";
			$sqlcmd = $sqlcmd . ") ";
			$result  =  $database->exec($sqlcmd);
			if ($result !== FALSE) {
				$sqlcmd = "SELECT MAX(oid) ";
				$sqlcmd = $sqlcmd . "FROM base.t_medecins  ";
				$sqlcmd = $sqlcmd . "WHERE nom = '$nom' AND prenom = '$prenom' AND numero_ordre = '$numero_ordre' AND specialite_id = $specialite_interne_id";
				$result = $database->exec($sqlcmd);
				
				if ($result != false) {
					$record = $database->nextRecordInto();
					if ($record != FALSE) {
						$oid = ctiFloatVal($record[0]);
						
						if ($oid > 0) {
							$sqlcmd = "UPDATE activite.t_medecins_administratifs SET ";
							$sqlcmd = $sqlcmd . "medecin_id = $oid ";
							$sqlcmd = $sqlcmd . "WHERE oid IN ($medecins_regroupes_oids)";
							$result  =  $database->exec($sqlcmd);
						}
					}
				}
			}
			
		}
		else {
			
			$sqlcmd = "UPDATE base.t_medecins SET ";
			$sqlcmd = $sqlcmd . "nom = '$nom', ";
			$sqlcmd = $sqlcmd . "prenom = '$prenom', ";
			$sqlcmd = $sqlcmd . "numero_ordre = '$numero_ordre', ";
			$sqlcmd = $sqlcmd . "specialite_id = $specialite_interne_id ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			$sqlcmd = "UPDATE activite.t_medecins_administratifs SET ";
			$sqlcmd = $sqlcmd . "medecin_id = $oid ";
			$sqlcmd = $sqlcmd . "WHERE oid IN ($medecins_regroupes_oids)";
			$result  =  $database->exec($sqlcmd);
			
			$sqlcmd = "UPDATE activite.t_medecins_administratifs SET ";
			$sqlcmd = $sqlcmd . "medecin_id = 0 ";
			$sqlcmd = $sqlcmd . "WHERE medecin_id = $oid";
			$sqlcmd = $sqlcmd . "AND oid NOT IN ($medecins_regroupes_oids)";
			$result  =  $database->exec($sqlcmd);
		}
				

	}
		if ($oid >= 0) {
	
		$sqlcmd = "UPDATE activite.t_medecins_administratifs_c SET ";
		$sqlcmd = $sqlcmd . "adm_code = t_medecins_administratifs.code, ";
		$sqlcmd = $sqlcmd . "adm_nom = t_medecins_administratifs.nom, ";
		$sqlcmd = $sqlcmd . "adm_prenom = t_medecins_administratifs.prenom, ";
		$sqlcmd = $sqlcmd . "adm_nom_prenom = t_medecins_administratifs.nom || ' ' || t_medecins_administratifs.prenom, ";
		$sqlcmd = $sqlcmd . "medecin_id = t_medecins_administratifs.medecin_id, ";
		$sqlcmd = $sqlcmd . "nom = t_medecins.nom, ";
		$sqlcmd = $sqlcmd . "prenom = t_medecins.prenom, ";
		$sqlcmd = $sqlcmd . "nom_prenom = t_medecins.nom || ' ' || t_medecins.prenom, ";
		$sqlcmd = $sqlcmd . "specialite_id = t_medecins.specialite_id, ";
		$sqlcmd = $sqlcmd . "specialite_code = t_specialites_medecin.code, ";
		$sqlcmd = $sqlcmd . "specialite_texte = t_specialites_medecin.texte_court ";
		$sqlcmd = $sqlcmd . "FROM activite.t_medecins_administratifs, base.t_medecins, base.t_specialites_medecin ";
		$sqlcmd = $sqlcmd . "WHERE t_medecins_administratifs_c.oid = t_medecins_administratifs.oid ";
		$sqlcmd = $sqlcmd . "AND t_medecins_administratifs.medecin_id = t_medecins.oid ";
		$sqlcmd = $sqlcmd . "AND t_medecins.specialite_id = t_specialites_medecin.oid  ";
		$sqlcmd = $sqlcmd . "AND (t_medecins_administratifs.medecin_id = $oid OR t_medecins_administratifs.medecin_id = 0) ";
		$result  =  $database->exec($sqlcmd);
		

	}
	
	$result  =  $database->exec("SELECT activite.cti_reorganize_medecins_administratifs_c(); ");
	

}



function setRecords_sejour() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$est_ignore = utf8_decode($recordNode["est_ignore"]);
	if ($est_ignore != "1") {
		$est_ignore = "0";
	}
	$medecin_sejour_id = utf8_decode($recordNode["medecin_sejour_id"]);
	$medecin_sejour_id = ctiFloatVal($medecin_sejour_id);
	$service_facturation_id = utf8_decode($recordNode["service_facturation_id"]);
	$service_facturation_id = ctiFloatVal($service_facturation_id);
	$etage_id = utf8_decode($recordNode["etage_id"]);
	$etage_id = ctiFloatVal($etage_id);
	$type_sejour = utf8_decode($recordNode["type_sejour"]);
	$type_sejour = ctiFloatVal($type_sejour);
	
	$encours_1_from_date = utf8_decode($recordNode["encours_1_from_date"]);
	$encours_1_est_ignore = utf8_decode($recordNode["encours_1_est_ignore"]);
	if ($encours_1_est_ignore != "1") {
		$encours_1_est_ignore = "0";
	}
	


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);

		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO activite.t_sejour (
					no_sejour, 
					est_ignore, 
					medecin_sejour_id, 
					service_facturation_id, 
					etage_id,
					type_sejour,
					encours_from_date,
					encours_est_ignore
					) 
				VALUES(
					'$code', 
					'$est_ignore', 
					$medecin_sejour_id, 
					$service_facturation_id, 
					$etage_id,
					'$type_sejour',
					ARRAY['$encours_1_from_date'::date],
					ARRAY['$encours_1_est_ignore'::text]
					) ";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
				
			}
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_sejour SET 
						no_sejour = '$code',
						est_ignore = '$est_ignore',
						medecin_sejour_id = $medecin_sejour_id,
						service_facturation_id = $service_facturation_id,
						etage_id = $etage_id,
						type_sejour = '$type_sejour',
						encours_from_date = ARRAY['$encours_1_from_date'::date],
						encours_est_ignore = ARRAY['$encours_1_est_ignore'::text]
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_sejour WHERE oid = $oid";
		$result  =  $database->exec($sqlcmd);
	}
	
	
	// Application à la table des séjours
	$sqlcmd = "UPDATE activite.p_sejours
				SET etat = 'I'
				FROM activite.t_sejour
				WHERE t_sejour.no_sejour = p_sejours.no_sejour AND
					est_ignore = '1' AND
					etat <> 'I';

				UPDATE activite.p_sejours
				SET etat = ''
				WHERE etat = 'I' AND 
					no_sejour NOT IN (SELECT no_sejour FROM activite.t_sejour WHERE est_ignore = '1');
					
				UPDATE activite.p_sejours
				SET medecin_sejour_id = t_sejour.medecin_sejour_id
				FROM activite.t_sejour
				WHERE t_sejour.no_sejour = p_sejours.no_sejour AND
					t_sejour.medecin_sejour_id <> 0 AND
					p_sejours.medecin_sejour_id IS DISTINCT FROM t_sejour.medecin_sejour_id;";
	$result  =  $database->exec($sqlcmd);

}



function setRecords_budget() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$date_debut = utf8_decode($recordNode["date_debut"]);
	$date_fin = utf8_decode($recordNode["date_fin"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$date_debut = toSQL($date_debut);
		$date_fin = toSQL($date_fin);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_budget (code, texte, date_debut, date_fin)
					VALUES('$code', '$texte', to_date('$date_debut','DD/MM/YYYY'), to_date('$date_fin','DD/MM/YYYY'))";
			$result  =  $database->exec($sqlcmd);
		}
		else {
			$sqlcmd = "UPDATE activite.t_budget SET 
						code = '$code', 
						texte = '$texte', 
						date_debut = to_date('$date_debut','DD/MM/YYYY') , 
						date_fin = to_date('$date_fin','DD/MM/YYYY') 
						WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}

		// Modification des indicateurs déjà existant
		
		$indicators = "0";
		foreach ($recordNode->INDICATEUR as $indicateur_node) {
			$budget_indicateur_oid = ctiFloatVal($indicateur_node["oid"]);
			$budget_indicateur_code = trim($indicateur_node["code"]);
			$budget_indicateur_texte = trim($indicateur_node["texte"]);
			$budget_indicateur_indicateur_id = ctiFloatVal($indicateur_node["indicateur_id"]);
			$budget_indicateur_code = toSQL($budget_indicateur_code);
			$budget_indicateur_texte = toSQL($budget_indicateur_texte);
			
			if ($budget_indicateur_oid > 0) {
				$sqlcmd = "UPDATE activite.t_budget_indicateur SET 
						code = '$budget_indicateur_code', 
						texte = '$budget_indicateur_texte', 
						indicateur_id = $budget_indicateur_indicateur_id
						WHERE oid = $budget_indicateur_oid";
				$result  =  $database->exec($sqlcmd);
				$indicators = "$indicators, $budget_indicateur_oid";
			}
		}
		
		// Suppression des indicateurs qui n'existent plus
		$sqlcmd = "DELETE FROM activite.t_budget_indicateur WHERE oid NOT IN ($indicators)";
		$result  =  $database->exec($sqlcmd);
		
		// Ajout des nouveaux indicateurs
		foreach ($recordNode->INDICATEUR as $indicateur_node) {
			$budget_indicateur_oid = ctiFloatVal($indicateur_node["oid"]);
			$budget_indicateur_code = trim($indicateur_node["code"]);
			$budget_indicateur_texte = trim($indicateur_node["texte"]);
			$budget_indicateur_indicateur_id = ctiFloatVal($indicateur_node["indicateur_id"]);
			$budget_indicateur_code = toSQL($budget_indicateur_code);
			$budget_indicateur_texte = toSQL($budget_indicateur_texte);

		
			if ($budget_indicateur_oid <= 0) {
				$sqlcmd = "INSERT INTO activite.t_budget_indicateur (code, texte, budget_id, indicateur_id)
						VALUES('$budget_indicateur_code', '$budget_indicateur_texte', $oid, $budget_indicateur_indicateur_id)";
				$result  =  $database->exec($sqlcmd);
			}
		}		

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_budget WHERE oid = $oid";
		$result  =  $database->exec($sqlcmd);
	}
	

}



function setRecords_expert_controle() {

	global $database;
	global $action;
	global $recordNode;
	
	$httpString = "";
	
	$mode = ctiFloatVal($recordNode["mode"]);
	
	// Mise à jour controles unitaires
	if ($mode == 0) {
		$oid = ctiFloatVal($recordNode["oid"]);
		$code = utf8_decode($recordNode["code"]);
		$texte = utf8_decode($recordNode["texte"]);
		$texte_court = utf8_decode($recordNode["texte_court"]);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		$texte_abrege = utf8_decode($recordNode["texte_abrege"]);
		if ($texte_abrege == "") {
			$texte_abrege = $texte_court;
		}
		$description = $recordNode["description"];
		$is_cti = utf8_decode($recordNode["is_cti"]);
		$is_active = utf8_decode($recordNode["is_active"]);
		$is_hide = utf8_decode($recordNode["is_hide"]);
		$gravite = ctiFloatVal($recordNode["gravite"]);
		$etat_cible = ctiFloatVal($recordNode["etat_cible"]);
		$prestataire_id = ctiFloatVal($recordNode["prestataire_id"]);
		$is_alert = utf8_decode($recordNode["is_alert"]);

		$rule_oid1 = ctiFloatVal($recordNode["rule_oid1"]);
		$sqlcmd_where1 = utf8_decode($recordNode["sqlcmd_where1"]);
		$sqlcmd_justificatif1 = utf8_decode($recordNode["sqlcmd_justificatif1"]);
		$table1 = ctiFloatVal($recordNode["table1"]);
		
		$rule_oid2 = ctiFloatVal($recordNode["rule_oid2"]);
		$sqlcmd_where2 = utf8_decode($recordNode["sqlcmd_where2"]);
		$sqlcmd_justificatif2 = utf8_decode($recordNode["sqlcmd_justificatif2"]);
		$table2 = ctiFloatVal($recordNode["table2"]);
		
		$rule_oid3 = ctiFloatVal($recordNode["rule_oid3"]);
		$sqlcmd_where3 = utf8_decode($recordNode["sqlcmd_where3"]);
		$sqlcmd_justificatif3 = utf8_decode($recordNode["sqlcmd_justificatif3"]);
		$table3 = ctiFloatVal($recordNode["table3"]);
		
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$texte_abrege = toSQL($texte_abrege);
		$description = toSQL($description);
		$is_cti = toSQL($is_cti);
		$is_hide = toSQL($is_hide);
		$is_active = toSQL($is_active);
		$sqlcmd_where1 = toSQL($sqlcmd_where1);
		$sqlcmd_justificatif1 = toSQL($sqlcmd_justificatif1);
		$sqlcmd_where2 = toSQL($sqlcmd_where2);
		$sqlcmd_justificatif2 = toSQL($sqlcmd_justificatif2);
		$sqlcmd_where3 = toSQL($sqlcmd_where3);
		$sqlcmd_justificatif3 = toSQL($sqlcmd_justificatif3);

		if ($action != "delete" && $action != "copy" && $action != "execute_rules" && $action != "purge_history") {

			// creation ou mise à  jour de la liste
			if ($oid == -1) {
			
				$sqlcmd = "INSERT INTO activite.t_expert_controle 
						( 
							code, 
							texte, 
							texte_court, 
							texte_abrege, 
							description, 
							is_cti, 
							is_hide, 
							gravite_id,
							etat_cible,
							prestataire_id,
							is_active, 
							alert_exp
						) 
						VALUES(
							'$code', 
							'$texte', 
							'$texte_court', 
							'$texte_abrege',
							'$description', 
							'$is_cti', 
							'$is_hide', 
							$gravite,
							$etat_cible,
							$prestataire_id,
							'$is_active',
							'$is_alert'
						) ";
				$result  =  $database->exec($sqlcmd);
				if ($result == FALSE) {
					
				}
				else {
					$result = $database->exec(
						"SELECT max(oid) as oid FROM
						activite.t_expert_controle 
						WHERE code = '$code' AND texte = '$texte'"
					);
					if ($result != false) {
						$record = $database->nextRecordInto();
						if ($record != FALSE) {
							$oid = $record[0] + 0;
						}
					}
				}
			}
				
			else {
				
				$sqlcmd = "UPDATE activite.t_expert_controle SET 
							texte = '$texte', 
							texte_court = '$texte_court', 
							texte_abrege = '$texte_abrege',
							description = '$description',
							is_cti = '$is_cti' , 
							is_hide = '$is_hide' , 
							gravite_id = $gravite,
							etat_cible = $etat_cible,
							prestataire_id = $prestataire_id,
							is_active = '$is_active',
							alert_exp = '$is_alert'
						WHERE oid = $oid;
						UPDATE activite.t_expert_controle SET 
							code = '$code'
						WHERE oid = $oid;
						";
				$result  =  $database->exec($sqlcmd);
			}
			
			// Ajout des règles
			$sqlcmd = "DELETE FROM activite.t_expert_controle_rule
				USING 
				(
				SELECT t_expert_controle_rule.oid
				FROM activite.t_expert_controle_rule
				LEFT JOIN activite.t_expert_controle ON t_expert_controle_rule.controle_id = t_expert_controle.oid
				WHERE t_expert_controle.oid IS NULL
				) subview
				WHERE t_expert_controle_rule.oid = subview.oid;

				INSERT INTO activite.t_expert_controle_rule (
					controle_id,
					numero,
					sqlcmd_where,
					sqlcmd_justificatif,
					table_id
					)
				SELECT t_expert_controle.oid AS controle_id, 
					1::bigint AS numero,
					'' AS sqlcmd_where,
					'' AS sqlcmd_justificatif,
					0::bigint as table_id
				FROM activite.t_expert_controle
				LEFT JOIN activite.t_expert_controle_rule ON t_expert_controle_rule.controle_id = t_expert_controle.oid AND t_expert_controle_rule.numero = 1
				WHERE t_expert_controle_rule.oid IS NULL
				UNION
				SELECT t_expert_controle.oid AS controle_id, 
					2::bigint AS numero,
					'' AS sqlcmd_where,
					'' AS sqlcmd_justificatif,
					0::bigint as table_id
				FROM activite.t_expert_controle
				LEFT JOIN activite.t_expert_controle_rule ON t_expert_controle_rule.controle_id = t_expert_controle.oid AND t_expert_controle_rule.numero = 2
				WHERE t_expert_controle_rule.oid IS NULL
				UNION ALL
				SELECT t_expert_controle.oid AS controle_id, 
					3::bigint AS numero,
					'' AS sqlcmd_where,
					'' AS sqlcmd_justificatif,
					0::bigint as table_id
				FROM activite.t_expert_controle
				LEFT JOIN activite.t_expert_controle_rule ON t_expert_controle_rule.controle_id = t_expert_controle.oid AND t_expert_controle_rule.numero = 3
				WHERE t_expert_controle_rule.oid IS NULL;";
			$result  =  $database->exec($sqlcmd);
			
			if ($oid != 0) {

					$sqlcmd = 
					"UPDATE activite.t_expert_controle_rule
					SET sqlcmd_where = '$sqlcmd_where1', 
						sqlcmd_justificatif = '$sqlcmd_justificatif1',
						table_id = $table1
					WHERE controle_id = $oid AND
						numero = 1;
						
					UPDATE activite.t_expert_controle_rule
					SET sqlcmd_where = '$sqlcmd_where2', 
						sqlcmd_justificatif = '$sqlcmd_justificatif2',
						table_id = $table2
					WHERE controle_id = $oid AND
						numero = 2;
						
					UPDATE activite.t_expert_controle_rule
					SET sqlcmd_where = '$sqlcmd_where3', 
						sqlcmd_justificatif = '$sqlcmd_justificatif3',
						table_id = $table3
					WHERE controle_id = $oid AND
						numero = 3";
					$result  =  $database->exec($sqlcmd);	
			}
		}

		if ($action == "copy") {
		
			$pos = stripos($code, "_");
			if ($pos === false) {
				$fromcode = $code . "_";
			}
			else {
				$fromcode = substr($code,0,$pos) . "_";
			}
		
			$last_oid = 1;
			$result = $database->exec("SELECT max(to_number(substr(code,length('$fromcode')+1,10), '999999')) as oid from activite.t_expert_controle WHERE code LIKE '$fromcode%'");
			if ($result != false) {
				$record = $database->nextRecordInto();
				if ($record != FALSE) {
					$last_oid = $record[0] + 0;
				}
			}
			if ($last_oid == 0) {
				$last_oid = 1;
			}
			$last_oid++;
			
			$sqlcmd = "INSERT INTO activite.t_expert_controle(code, texte, texte_court) ";
			$sqlcmd = $sqlcmd . "SELECT '$fromcode$last_oid', 'Copie de ' || texte , 'Copie de ' || texte_court ";
			$sqlcmd = $sqlcmd . "FROM activite.t_expert_controle WHERE oid = $oid";
			$result = $database->exec($sqlcmd);
			
			$oid = 0;
			$result = $database->exec(
				"SELECT max(oid) as oid FROM
				activite.t_expert_controle 
				WHERE code = '$fromcode$last_oid' "
			);
			if ($result != false) {
				$record = $database->nextRecordInto();
				if ($record != FALSE) {
					$oid = $record[0] + 0;
				}
			}
		}

		if ($action == "delete") {
			$sqlcmd = "DELETE FROM activite.t_expert_controle WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			$sqlcmd = "DELETE FROM activite.t_expert_controle_rule WHERE controle_id = $oid";
			$result  =  $database->exec($sqlcmd);
		}
		
		if ($action == "purge_history") {
			$sqlcmd = "DELETE FROM activite.p_expert_rss_controle WHERE controle_id = $oid";
			$result  =  $database->exec($sqlcmd);
		}
	}
	
	
	// Mise à jour paramètres généraux
	if ($mode == 2) {
		$EXPERT_ACTIVE = utf8_decode($recordNode["EXPERT_ACTIVE"]);
		$EXPERT_DATE_DEBUT = utf8_decode($recordNode["EXPERT_DATE_DEBUT"]);

		if ($action == "save") {
		
			$sqlcmd = "UPDATE activite.t_divers
					SET valeur = '$EXPERT_ACTIVE'
					WHERE code = 'EXPERT_ACTIVE' AND
						valeur IS DISTINCT FROM '$EXPERT_ACTIVE'; ";
			$result = $database->exec($sqlcmd);
			
			$sqlcmd = "UPDATE activite.t_divers
					SET valeur_date = to_date('$EXPERT_DATE_DEBUT','DD/MM/YYYY'),
						valeur = '$EXPERT_DATE_DEBUT'
					WHERE code = 'EXPERT_DATE_DEBUT' AND
						valeur_date IS DISTINCT FROM to_date('$EXPERT_DATE_DEBUT','DD/MM/YYYY'); ";
			$result = $database->exec($sqlcmd);
		}
	}
	
	
	
	// exécution des règles
	if ($action == "execute_rules") {
		$return_code = "";
		$return_message = "";
		
		// Liste des oids des controles à exécuter. Ils doivent être dans une chaine de caractères séparés par 
		// un espace, une virgule, un point-virgule, un double-points ou une barre verticale
		$oids = @$_POST["oids"];
		if ($oids == "") {
			$oids = @$_GET["oids"];
		}
		if (strlen($oids) > 0) {
			$oids_to_control = preg_split("/[\s,;:\|]+/", $oids);
		}
		
		$exp_manager = new ControleExpertManager('activite');
		$exp_manager->prestataire = $_SESSION['admProvider'];
		$exp_manager->dictionary = $_SESSION['rootDir'] .'modules/activite/php/expert/dictionary.json';
		if ($exp_manager->run_controls($oids_to_control)) {
			$exp_return_code = 'OK';
			$exp_message = $exp_manager->msg;
		}
		else {
			$exp_return_code = 'KO';
			$exp_message = $exp_manager->error_msg;
		}
		
		$httpString = "<RETURN code=\"$exp_return_code\" message=\"$exp_message\" />";
	}
	
	// exécution des règles
	if ($action == "create_documentation" || $action == "save" || $action == "delete") {
	
		$reference_template_file = "../references/iCTI_ACTI_ExpertControle.template.HTML";
		$reference_file = "../references/iCTI_ACTI_ExpertControle.HTML";
		if (file_exists($reference_template_file)) {
			$reference_template_handle = @fopen($reference_template_file, "r");
			$reference_handle = @fopen($reference_file, "w");
			if ($reference_template_handle) {
				$currentIsControle = false;
				$controleString = "";
				while (!feof($reference_template_handle)) {
					$buffer = fgets($reference_template_handle, 99999);
					if (trim($buffer) == "<CONTROLE_DEFINITION>") {
						$currentIsControle = true;
					}
					else {
						if (trim($buffer) == "</CONTROLE_DEFINITION>") {
						
							$sqlcmd = "SELECT 
										t_expert_controle.oid, 
										t_expert_controle.code, 
										t_expert_controle.texte, 
										t_expert_controle.description,
										t_expert_controle.is_cti
									FROM activite.t_expert_controle  
									WHERE t_expert_controle.oid > 0 AND is_datim <> 1 AND is_global <> 1 AND is_hide <> 1 AND description <> ''
									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[0];
										$code = toHTML(trim($record[1]));
										$texte = toHTML(trim($record[2]));
										$description = trim($record[3]);
										$is_cti = toHTML(trim($record[4]));
										
										if (substr($description,0,3) == "_c_") {
											$description = uncompress64(substr($description, 3));
										}
										$description = str_replace('SIZE="10"','SIZE="3"',$description);
										$description = str_replace('SIZE="12"','SIZE="3"',$description);
										$description = str_replace('SIZE="14"','SIZE="3"',$description);
										
										$controleStringControle = $controleString;
										$controleStringControle = str_replace("[CONTROLE_CODE]",$code,$controleStringControle);
										$controleStringControle = str_replace("[CONTROLE_TEXT]",$texte,$controleStringControle);
										$controleStringControle = str_replace("[CONTROLE_DESCRIPTION]",$description,$controleStringControle);
										
										fwrite($reference_handle,$controleStringControle);
									}
								}
							}
						
							$currentIsControle = false;
						}
						else {
							if ($currentIsControle == false) {
								fwrite($reference_handle,$buffer);
							}
							else {
								$controleString = "$controleString$buffer";
							}
						}
					}
				}
			}
			@fclose($reference_template_handle);
			@fclose($reference_handle);
		}
	}
	
	return $httpString;
}



function setRecords_budget_cle() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);
	$code_original = utf8_decode($recordNode["code_original"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		$code_original = toSQL($code_original);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_budget_cle (code, texte, texte_court)
					VALUES('$code', '$texte', '$texte_court')"; 
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_budget_cle SET 
					code = '$code', 
					texte = '$texte', 
					texte_court = '$texte_court' 
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "UPDATE activite.p_sejours_budget_cle SET budget_cle_id = 0 WHERE budget_cle_id = $oid; 
                DELETE FROM activite.t_budget_cle_rule  WHERE budget_cle_id = $oid; 
                DELETE FROM activite.t_budget_cle WHERE oid = $oid";
		$result  =  $database->exec($sqlcmd);
	}
	

}



function setRecords_budget_cle_rule() {

	global $database;
	global $action;
	global $recordNode;
	
	// Action spécifique : executer les règles
	if ($action == "BUR_execute_rules") {
		$sqlcmd = "SELECT * FROM activite.cti_budget_execute_rules()";
		$result  =  $database->exec($sqlcmd);
		return;
	}
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$priorite = utf8_decode($recordNode["priorite"]);
	$budget_cle_id = utf8_decode($recordNode["budget_cle_id"]);
	
	$liste_specialite = utf8_decode($recordNode["liste_specialite"]);
	$liste_medecin = utf8_decode($recordNode["liste_medecin"]);
	$liste_service = utf8_decode($recordNode["liste_service"]);
	$liste_acte = utf8_decode($recordNode["liste_acte"]);
	$liste_rubrique = utf8_decode($recordNode["liste_rubrique"]);
	$liste_ghm = utf8_decode($recordNode["liste_ghm"]);
	$liste_sauf_ghm = utf8_decode($recordNode["liste_sauf_ghm"]);
	$liste_diagnostic_principal = utf8_decode($recordNode["liste_diagnostic_principal"]);
	$liste_diagnostic_relie = utf8_decode($recordNode["liste_diagnostic_relie"]);
	$liste_diagnostic_associe = utf8_decode($recordNode["liste_diagnostic_associe"]);
	$liste_finess = utf8_decode($recordNode["liste_finess"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$priorite = ctiFloatVal($priorite);
		$budget_cle_id = ctiFloatVal($budget_cle_id);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_budget_cle_rule (code, texte, priorite, budget_cle_id,
							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
						)
					VALUES('$code', '$texte', $priorite, $budget_cle_id,
							activite.cti_budget_translate_list('$liste_specialite'),
							activite.cti_budget_translate_list('$liste_medecin'),
							activite.cti_budget_translate_list('$liste_service'),
							activite.cti_budget_translate_list('$liste_acte'),
							activite.cti_budget_translate_list('$liste_rubrique'),
							activite.cti_budget_translate_list('$liste_ghm'),
							activite.cti_budget_translate_list('$liste_sauf_ghm'),
							activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							activite.cti_budget_translate_list('$liste_finess')
					)"; 
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_budget_cle_rule SET 
							code = '$code', 
							texte = '$texte', 
							priorite = $priorite,
							budget_cle_id = $budget_cle_id,
							liste_specialite = activite.cti_budget_translate_list('$liste_specialite'),
							liste_medecin = activite.cti_budget_translate_list('$liste_medecin'),
							liste_service = activite.cti_budget_translate_list('$liste_service'),
							liste_acte = activite.cti_budget_translate_list('$liste_acte'),
							liste_rubrique = activite.cti_budget_translate_list('$liste_rubrique'),
							liste_ghm = activite.cti_budget_translate_list('$liste_ghm'),
							liste_sauf_ghm = activite.cti_budget_translate_list('$liste_sauf_ghm'),
							liste_diagnostic_principal = activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							liste_diagnostic_relie = activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							liste_diagnostic_associe = activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							liste_finess= activite.cti_budget_translate_list('$liste_finess')
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_budget_cle_rule WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
	}
	

}




function setRecords_budget_cle_budget() {

	global $database;
	global $action;
	global $recordNode;
	
	$budget_id = ctiFloatVal($recordNode["oid"]);

	// initialisation budget
	$sqlcmd = "UPDATE activite.t_budget_cle_budget
				SET indicateur_id = t_budget_indicateur.indicateur_id
				FROM activite.t_budget_indicateur
				WHERE t_budget_indicateur.oid = t_budget_cle_budget.budget_indicateur_id AND
					t_budget_cle_budget.indicateur_id IS DISTINCT FROM t_budget_indicateur.indicateur_id;


				INSERT INTO activite.t_budget_cle_budget (
					budget_id,
					budget_cle_id,
					date,
					budget_indicateur_id,
					indicateur_id,
					budget
				)
				SELECT 
					t_budget.oid AS budget_id,
					t_budget_cle.oid AS budget_cle_id,
					date(date_trunc('month',p_calendrier_mois.date_fin)) AS date,
					t_budget_indicateur.oid AS budget_indicateur_id,
					t_budget_indicateur.indicateur_id AS indicateur_id,
					0::numeric AS budget
				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))
				WHERE t_budget_cle_budget.budget_id IS NULL;";
	$result  =  $database->exec($sqlcmd);
	
	// Modification des valeurs de budget
	
	
	foreach ($recordNode->INDICATEUR as $indicateur_node) {
		$budget_indicateur_id = ctiFloatVal($indicateur_node["budget_indicateur_id"]);
		foreach ($indicateur_node->CLE as $cle_node) {
			$budget_cle_id = ctiFloatVal($cle_node["budget_cle_id"]);
			$sqlcmd = "";
			for ($m = 1; $m <= 12; $m++) {
				if ($m < 10) {
					$mm = "0$m";
				}
				else {
					$mm = "$m";
				}
				$budget_date = $indicateur_node["date_$mm"];
				$budget_value = str_replace(',','.',$cle_node["budget_$mm"]);
				$budget_value = ctiFloatVal($budget_value);
				
				$sqlcmd = "$sqlcmd UPDATE activite.t_budget_cle_budget
					SET budget = $budget_value 
					WHERE budget_id = $budget_id AND
						budget_cle_id = $budget_cle_id AND
						budget_indicateur_id = $budget_indicateur_id AND
						date = '$budget_date' AND
						budget IS DISTINCT FROM $budget_value; ";
				
			}
			$result  =  $database->exec($sqlcmd);
			
		}
	}
		

}



function setRecords_forme_activite() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_forme_activite (code, texte, texte_court)
					VALUES('$code', '$texte', '$texte_court')"; 
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_forme_activite SET 
					code = '$code', 
					texte = '$texte', 
					texte_court = '$texte_court' 
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_forme_activite WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
	}
	

}




function setRecords_forme_activite_rule() {

	global $database;
	global $action;
	global $recordNode;
	
	// Action spécifique : executer les règles
	if ($action == "FAR_execute_rules") {
		$sqlcmd = "SELECT activite.cti_reorganize_forme_activite();";
		$result  =  $database->exec($sqlcmd);
		return;
	}
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$priorite = utf8_decode($recordNode["priorite"]);
	$forme_activite_id = utf8_decode($recordNode["forme_activite_id"]);
	
    $liste_type_sejour = utf8_decode($recordNode["liste_type_sejour"]);
	$liste_specialite = utf8_decode($recordNode["liste_specialite"]);
	$liste_medecin = utf8_decode($recordNode["liste_medecin"]);
	$liste_unite_fonctionnelle = utf8_decode($recordNode["liste_unite_fonctionnelle"]);
    $liste_service = utf8_decode($recordNode["liste_service"]);
    $liste_etage = utf8_decode($recordNode["liste_etage"]);
	$liste_unite_fonctionnelle_entree = utf8_decode($recordNode["liste_unite_fonctionnelle_entree"]);
    $liste_service_entree = utf8_decode($recordNode["liste_service_entree"]);
    $liste_etage_entree = utf8_decode($recordNode["liste_etage_entree"]);
	$liste_acte = utf8_decode($recordNode["liste_acte"]);
	$liste_rubrique = utf8_decode($recordNode["liste_rubrique"]);
    $liste_prestation = utf8_decode($recordNode["liste_prestation"]);
	$liste_ghm = utf8_decode($recordNode["liste_ghm"]);
	$liste_sauf_ghm = utf8_decode($recordNode["liste_sauf_ghm"]);
	$liste_diagnostic_principal = utf8_decode($recordNode["liste_diagnostic_principal"]);
	$liste_diagnostic_relie = utf8_decode($recordNode["liste_diagnostic_relie"]);
	$liste_diagnostic_associe = utf8_decode($recordNode["liste_diagnostic_associe"]);
	$liste_finess = utf8_decode($recordNode["liste_finess"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$priorite = ctiFloatVal($priorite);
		$forme_activite_id = ctiFloatVal($forme_activite_id);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_forme_activite_rule (code, texte, priorite, forme_activite_id,
							liste_type_sejour,
                            liste_specialite,
							liste_medecin,
							liste_unite_fonctionnelle,
                            liste_service,
                            liste_etage,
							liste_unite_fonctionnelle_entree,
                            liste_service_entree,
                            liste_etage_entree,
							liste_acte,
							liste_rubrique,
                            liste_prestation,
							liste_ghm,
							liste_sauf_ghm,
							liste_diagnostic_principal,
							liste_diagnostic_relie,
							liste_diagnostic_associe,
							liste_finess
						)
					VALUES('$code', '$texte', $priorite, $forme_activite_id,
                            activite.cti_budget_translate_list('$liste_type_sejour'),
							activite.cti_budget_translate_list('$liste_specialite'),
							activite.cti_budget_translate_list('$liste_medecin'),
							activite.cti_budget_translate_list('$liste_unite_fonctionnelle'),
                            activite.cti_budget_translate_list('$liste_service'),
                            activite.cti_budget_translate_list('$liste_etage'),
							activite.cti_budget_translate_list('$liste_unite_fonctionnelle_entree'),
                            activite.cti_budget_translate_list('$liste_service_entree'),
                            activite.cti_budget_translate_list('$liste_etage_entree'),
							activite.cti_budget_translate_list('$liste_acte'),
							activite.cti_budget_translate_list('$liste_rubrique'),
                            activite.cti_budget_translate_list('$liste_prestation'),
							activite.cti_budget_translate_list('$liste_ghm'),
							activite.cti_budget_translate_list('$liste_sauf_ghm'),
							activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							activite.cti_budget_translate_list('$liste_finess')
					)"; 
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_forme_activite_rule SET 
							code = '$code', 
							texte = '$texte', 
							priorite = $priorite,
							forme_activite_id = $forme_activite_id,
							liste_type_sejour = activite.cti_budget_translate_list('$liste_type_sejour'),
                            liste_specialite = activite.cti_budget_translate_list('$liste_specialite'),
							liste_medecin = activite.cti_budget_translate_list('$liste_medecin'),
							liste_unite_fonctionnelle = activite.cti_budget_translate_list('$liste_unite_fonctionnelle'),
                            liste_service = activite.cti_budget_translate_list('$liste_service'),
                            liste_etage = activite.cti_budget_translate_list('$liste_etage'),
							liste_unite_fonctionnelle_entree = activite.cti_budget_translate_list('$liste_unite_fonctionnelle_entree'),
                            liste_service_entree = activite.cti_budget_translate_list('$liste_service_entree'),
                            liste_etage_entree = activite.cti_budget_translate_list('$liste_etage_entree'),
							liste_acte = activite.cti_budget_translate_list('$liste_acte'),
							liste_rubrique = activite.cti_budget_translate_list('$liste_rubrique'),
                            liste_prestation = activite.cti_budget_translate_list('$liste_prestation'),
							liste_ghm = activite.cti_budget_translate_list('$liste_ghm'),
							liste_sauf_ghm = activite.cti_budget_translate_list('$liste_sauf_ghm'),
							liste_diagnostic_principal = activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							liste_diagnostic_relie = activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							liste_diagnostic_associe = activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							liste_finess= activite.cti_budget_translate_list('$liste_finess')
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_forme_activite_rule WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
	}
	

}




function setRecords_filiere_soin() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$texte_court = utf8_decode($recordNode["texte_court"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_filiere_soin (code, texte, texte_court)
					VALUES('$code', '$texte', '$texte_court')"; 
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_filiere_soin SET 
					code = '$code', 
					texte = '$texte', 
					texte_court = '$texte_court' 
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_filiere_soin WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
	}
	

}




function setRecords_filiere_soin_rule() {

	global $database;
	global $action;
	global $recordNode;
	
	// Action spécifique : executer les règles
	if ($action == "FRU_execute_rules") {
		$sqlcmd = "SELECT activite.cti_filiere_soin_execute_rule()";
		$result  =  $database->exec($sqlcmd);
		return;
	}
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$priorite = utf8_decode($recordNode["priorite"]);
	$filiere_soin_id = utf8_decode($recordNode["filiere_soin_id"]);
	
    $liste_type_sejour = utf8_decode($recordNode["liste_type_sejour"]);
	$liste_specialite = utf8_decode($recordNode["liste_specialite"]);
	$liste_medecin = utf8_decode($recordNode["liste_medecin"]);
	$liste_unite_fonctionnelle = utf8_decode($recordNode["liste_unite_fonctionnelle"]);
    $liste_service = utf8_decode($recordNode["liste_service"]);
    $liste_etage = utf8_decode($recordNode["liste_etage"]);
	$liste_unite_fonctionnelle_entree = utf8_decode($recordNode["liste_unite_fonctionnelle_entree"]);
    $liste_service_entree = utf8_decode($recordNode["liste_service_entree"]);
    $liste_etage_entree = utf8_decode($recordNode["liste_etage_entree"]);
	$liste_acte = utf8_decode($recordNode["liste_acte"]);
	$liste_rubrique = utf8_decode($recordNode["liste_rubrique"]);
    $liste_prestation = utf8_decode($recordNode["liste_prestation"]);
	$liste_ghm = utf8_decode($recordNode["liste_ghm"]);
	$liste_sauf_ghm = utf8_decode($recordNode["liste_sauf_ghm"]);
	$liste_diagnostic_principal = utf8_decode($recordNode["liste_diagnostic_principal"]);
	$liste_diagnostic_relie = utf8_decode($recordNode["liste_diagnostic_relie"]);
	$liste_diagnostic_associe = utf8_decode($recordNode["liste_diagnostic_associe"]);
	$liste_finess = utf8_decode($recordNode["liste_finess"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$priorite = ctiFloatVal($priorite);
		$filiere_soin_id = ctiFloatVal($filiere_soin_id);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_filiere_soin_rule (code, texte, priorite, filiere_soin_id,
							liste_type_sejour,
                            liste_specialite,
							liste_medecin,
							liste_unite_fonctionnelle,
                            liste_service,
                            liste_etage,
							liste_unite_fonctionnelle_entree,
                            liste_service_entree,
                            liste_etage_entree,
							liste_acte,
							liste_rubrique,
                            liste_prestation,
							liste_ghm,
							liste_sauf_ghm,
							liste_diagnostic_principal,
							liste_diagnostic_relie,
							liste_diagnostic_associe,
							liste_finess
						)
					VALUES('$code', '$texte', $priorite, $filiere_soin_id,
                            activite.cti_budget_translate_list('$liste_type_sejour'),
							activite.cti_budget_translate_list('$liste_specialite'),
							activite.cti_budget_translate_list('$liste_medecin'),
							activite.cti_budget_translate_list('$liste_unite_fonctionnelle'),
                            activite.cti_budget_translate_list('$liste_service'),
                            activite.cti_budget_translate_list('$liste_etage'),
							activite.cti_budget_translate_list('$liste_unite_fonctionnelle_entree'),
                            activite.cti_budget_translate_list('$liste_service_entree'),
                            activite.cti_budget_translate_list('$liste_etage_entree'),
							activite.cti_budget_translate_list('$liste_acte'),
							activite.cti_budget_translate_list('$liste_rubrique'),
                            activite.cti_budget_translate_list('$liste_prestation'),
							activite.cti_budget_translate_list('$liste_ghm'),
							activite.cti_budget_translate_list('$liste_sauf_ghm'),
							activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							activite.cti_budget_translate_list('$liste_finess')
					)"; 
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_filiere_soin_rule SET 
							code = '$code', 
							texte = '$texte', 
							priorite = $priorite,
							filiere_soin_id = $filiere_soin_id,
							liste_type_sejour = activite.cti_budget_translate_list('$liste_type_sejour'),
                            liste_specialite = activite.cti_budget_translate_list('$liste_specialite'),
							liste_medecin = activite.cti_budget_translate_list('$liste_medecin'),
							liste_unite_fonctionnelle = activite.cti_budget_translate_list('$liste_unite_fonctionnelle'),
                            liste_service = activite.cti_budget_translate_list('$liste_service'),
                            liste_etage = activite.cti_budget_translate_list('$liste_etage'),
							liste_unite_fonctionnelle_entree = activite.cti_budget_translate_list('$liste_unite_fonctionnelle_entree'),
                            liste_service_entree = activite.cti_budget_translate_list('$liste_service_entree'),
                            liste_etage_entree = activite.cti_budget_translate_list('$liste_etage_entree'),
							liste_acte = activite.cti_budget_translate_list('$liste_acte'),
							liste_rubrique = activite.cti_budget_translate_list('$liste_rubrique'),
                            liste_prestation = activite.cti_budget_translate_list('$liste_prestation'),
							liste_ghm = activite.cti_budget_translate_list('$liste_ghm'),
							liste_sauf_ghm = activite.cti_budget_translate_list('$liste_sauf_ghm'),
							liste_diagnostic_principal = activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							liste_diagnostic_relie = activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							liste_diagnostic_associe = activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							liste_finess= activite.cti_budget_translate_list('$liste_finess')
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_filiere_soin_rule WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
	}
	

}



function setRecords_pole_rule() {

	global $database;
	global $action;
	global $recordNode;
	
	// Action spécifique : executer les règles
	if ($action == "POR_execute_rules") {
		$sqlcmd = "SELECT activite.cti_reorganize_pole();";
		$result  =  $database->exec($sqlcmd);
		return;
	}
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$priorite = utf8_decode($recordNode["priorite"]);
	$pole_id = utf8_decode($recordNode["pole_id"]);
	
    $liste_type_sejour = utf8_decode($recordNode["liste_type_sejour"]);
	$liste_specialite = utf8_decode($recordNode["liste_specialite"]);
	$liste_medecin = utf8_decode($recordNode["liste_medecin"]);
	$liste_unite_fonctionnelle = utf8_decode($recordNode["liste_unite_fonctionnelle"]);
    $liste_service = utf8_decode($recordNode["liste_service"]);
    $liste_etage = utf8_decode($recordNode["liste_etage"]);
	$liste_acte = utf8_decode($recordNode["liste_acte"]);
	$liste_rubrique = utf8_decode($recordNode["liste_rubrique"]);
    $liste_prestation = utf8_decode($recordNode["liste_prestation"]);
	$liste_ghm = utf8_decode($recordNode["liste_ghm"]);
	$liste_sauf_ghm = utf8_decode($recordNode["liste_sauf_ghm"]);
	$liste_diagnostic_principal = utf8_decode($recordNode["liste_diagnostic_principal"]);
	$liste_diagnostic_relie = utf8_decode($recordNode["liste_diagnostic_relie"]);
	$liste_diagnostic_associe = utf8_decode($recordNode["liste_diagnostic_associe"]);
	$liste_finess = utf8_decode($recordNode["liste_finess"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$priorite = ctiFloatVal($priorite);
		$pole_id = ctiFloatVal($pole_id);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_pole_rule (code, texte, priorite, pole_id,
							liste_type_sejour,
                            liste_specialite,
							liste_medecin,
							liste_unite_fonctionnelle,
                            liste_service,
                            liste_etage,
							liste_acte,
							liste_rubrique,
                            liste_prestation,
							liste_ghm,
							liste_sauf_ghm,
							liste_diagnostic_principal,
							liste_diagnostic_relie,
							liste_diagnostic_associe,
							liste_finess
						)
					VALUES('$code', '$texte', $priorite, $pole_id,
                            activite.cti_budget_translate_list('$liste_type_sejour'),
							activite.cti_budget_translate_list('$liste_specialite'),
							activite.cti_budget_translate_list('$liste_medecin'),
							activite.cti_budget_translate_list('$liste_unite_fonctionnelle'),
                            activite.cti_budget_translate_list('$liste_service'),
                            activite.cti_budget_translate_list('$liste_etage'),
							activite.cti_budget_translate_list('$liste_acte'),
							activite.cti_budget_translate_list('$liste_rubrique'),
                            activite.cti_budget_translate_list('$liste_prestation'),
							activite.cti_budget_translate_list('$liste_ghm'),
							activite.cti_budget_translate_list('$liste_sauf_ghm'),
							activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							activite.cti_budget_translate_list('$liste_finess')
					)"; 
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_pole_rule SET 
							code = '$code', 
							texte = '$texte', 
							priorite = $priorite,
							pole_id = $pole_id,
							liste_type_sejour = activite.cti_budget_translate_list('$liste_type_sejour'),
                            liste_specialite = activite.cti_budget_translate_list('$liste_specialite'),
							liste_medecin = activite.cti_budget_translate_list('$liste_medecin'),
							liste_unite_fonctionnelle = activite.cti_budget_translate_list('$liste_unite_fonctionnelle'),
                            liste_service = activite.cti_budget_translate_list('$liste_service'),
                            liste_etage = activite.cti_budget_translate_list('$liste_etage'),
							liste_acte = activite.cti_budget_translate_list('$liste_acte'),
							liste_rubrique = activite.cti_budget_translate_list('$liste_rubrique'),
                            liste_prestation = activite.cti_budget_translate_list('$liste_prestation'),
							liste_ghm = activite.cti_budget_translate_list('$liste_ghm'),
							liste_sauf_ghm = activite.cti_budget_translate_list('$liste_sauf_ghm'),
							liste_diagnostic_principal = activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							liste_diagnostic_relie = activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							liste_diagnostic_associe = activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							liste_finess= activite.cti_budget_translate_list('$liste_finess')
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_pole_rule WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
	}
	

}

function setRecords_compte_produit_analytique_rule() {

	global $database;
	global $action;
	global $recordNode;
	
	// Action spécifique : executer les règles
	if ($action == "CAR_execute_rules") {
		$sqlcmd = "SELECT activite.cti_reorganize_compte_analytique();";
		$result  =  $database->exec($sqlcmd);
		return;
	}
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$priorite = utf8_decode($recordNode["priorite"]);
	$compte_produit_analytique_id = utf8_decode($recordNode["compte_produit_analytique_id"]);
	
    $liste_type_sejour = utf8_decode($recordNode["liste_type_sejour"]);
	$liste_specialite = utf8_decode($recordNode["liste_specialite"]);
	$liste_medecin = utf8_decode($recordNode["liste_medecin"]);
	$liste_ghm = utf8_decode($recordNode["liste_ghm"]);
	$liste_sauf_ghm = utf8_decode($recordNode["liste_sauf_ghm"]);
	$liste_diagnostic_principal = utf8_decode($recordNode["liste_diagnostic_principal"]);
	$liste_diagnostic_relie = utf8_decode($recordNode["liste_diagnostic_relie"]);
	$liste_diagnostic_associe = utf8_decode($recordNode["liste_diagnostic_associe"]);
	$liste_acte = utf8_decode($recordNode["liste_acte"]);
	$liste_unite_fonctionnelle = utf8_decode($recordNode["liste_unite_fonctionnelle"]);
    $liste_service = utf8_decode($recordNode["liste_service"]);
    $liste_etage = utf8_decode($recordNode["liste_etage"]);
	$liste_rubrique = utf8_decode($recordNode["liste_rubrique"]);
	$liste_compte_produit = utf8_decode($recordNode["liste_compte_produit"]);
	$liste_sauf_compte_produit = utf8_decode($recordNode["liste_sauf_compte_produit"]);
    $liste_prestation = utf8_decode($recordNode["liste_prestation"]);
	$liste_sauf_prestation = utf8_decode($recordNode["liste_sauf_prestation"]);
	
	$liste_finess = utf8_decode($recordNode["liste_finess"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$priorite = ctiFloatVal($priorite);
		$compte_produit_analytique_id = ctiFloatVal($compte_produit_analytique_id);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO activite.t_compte_produit_analytique_rule (code, texte, priorite, compte_produit_analytique_id,
							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_service,
                            liste_etage,
							liste_rubrique,
							liste_compte_produit,
							liste_sauf_compte_produit,
                            liste_prestation,
							liste_sauf_prestation,
							liste_finess
						)
					VALUES('$code', '$texte', $priorite, $compte_produit_analytique_id,
                            activite.cti_budget_translate_list('$liste_type_sejour'),
							activite.cti_budget_translate_list('$liste_specialite'),
							activite.cti_budget_translate_list('$liste_medecin'),
							activite.cti_budget_translate_list('$liste_ghm'),
							activite.cti_budget_translate_list('$liste_sauf_ghm'),
							activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							activite.cti_budget_translate_list('$liste_acte'),
							activite.cti_budget_translate_list('$liste_unite_fonctionnelle'),
                            activite.cti_budget_translate_list('$liste_service'),
                            activite.cti_budget_translate_list('$liste_etage'),
							activite.cti_budget_translate_list('$liste_rubrique'),
							activite.cti_budget_translate_list('$liste_compte_produit'),
							activite.cti_budget_translate_list('$liste_sauf_compte_produit'),
                            activite.cti_budget_translate_list('$liste_prestation'),
							activite.cti_budget_translate_list('$liste_sauf_prestation'),
							activite.cti_budget_translate_list('$liste_finess')
					)"; 
			$result  =  $database->exec($sqlcmd);
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_compte_produit_analytique_rule SET 
							code = '$code', 
							texte = '$texte', 
							priorite = $priorite,
							compte_produit_analytique_id = $compte_produit_analytique_id,
							liste_type_sejour = activite.cti_budget_translate_list('$liste_type_sejour'),
                            liste_specialite = activite.cti_budget_translate_list('$liste_specialite'),
							liste_medecin = activite.cti_budget_translate_list('$liste_medecin'),
							liste_ghm = activite.cti_budget_translate_list('$liste_ghm'),
							liste_sauf_ghm = activite.cti_budget_translate_list('$liste_sauf_ghm'),
							liste_diagnostic_principal = activite.cti_budget_translate_list('$liste_diagnostic_principal'),
							liste_diagnostic_relie = activite.cti_budget_translate_list('$liste_diagnostic_relie'),
							liste_diagnostic_associe = activite.cti_budget_translate_list('$liste_diagnostic_associe'),
							liste_acte = activite.cti_budget_translate_list('$liste_acte'),
							liste_unite_fonctionnelle = activite.cti_budget_translate_list('$liste_unite_fonctionnelle'),
                            liste_service = activite.cti_budget_translate_list('$liste_service'),
                            liste_etage = activite.cti_budget_translate_list('$liste_etage'),
							liste_rubrique = activite.cti_budget_translate_list('$liste_rubrique'),
							liste_compte_produit = activite.cti_budget_translate_list('$liste_compte_produit'),
							liste_sauf_compte_produit = activite.cti_budget_translate_list('$liste_sauf_compte_produit'),
                            liste_prestation = activite.cti_budget_translate_list('$liste_prestation'),
							liste_sauf_prestation = activite.cti_budget_translate_list('$liste_sauf_prestation'),
							liste_finess= activite.cti_budget_translate_list('$liste_finess')
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);

		}
				

	}

	if ($action == "delete") {
		$sqlcmd = "DELETE FROM activite.t_compte_produit_analytique_rule WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
	}
	

}



function setRecords_calcul_encours() {

	global $database;
	global $action;
	global $recordNode;
	
    $type_ligne = trim($recordNode["type_ligne"]);
    
	$oid = ctiFloatVal($recordNode["oid"]);
	$coefficient_is_force = toSQL($recordNode["coefficient_is_force"]);
    $coefficient_force = toSQL($recordNode["coefficient_force"]);
    $coefficient_force = str_replace(",",".",$coefficient_force);
    $coefficient_force = ctiFloatVal($coefficient_force);
    $prix_unitaire_is_force = toSQL($recordNode["prix_unitaire_is_force"]);
    $prix_unitaire_force = toSQL($recordNode["prix_unitaire_force"]);
    $prix_unitaire_force = str_replace(",",".",$prix_unitaire_force);
    $prix_unitaire_force = ctiFloatVal($prix_unitaire_force);
    $type_valorisation = toSQL($recordNode["type_valorisation"]);
    $ignorer = toSQL($recordNode["ignorer"]);
    
    $code = toSQL($recordNode["code"]);
    $valeur = toSQL($recordNode["valeur"]);


	if ($action != "delete" && $action != "copy" && $action != "execute") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		
		// creation ou mise à  jour 
		if ($oid != -1) {
			if ($type_ligne != "GEN" && $type_ligne != "GENPRESTA") {
				$sqlcmd = "
					UPDATE activite.t_calcul_encours SET 
						coefficient_is_force = '$coefficient_is_force',
						coefficient_force = $coefficient_force,
						prix_unitaire_is_force = '$prix_unitaire_is_force',
						prix_unitaire_force = $prix_unitaire_force,
						ignorer = '$ignorer'
					WHERE oid = $oid ;
					
					";
			}
			if ($type_ligne == "GENPRESTA") {
				$sqlcmd = "
					UPDATE activite.t_prestations SET 
						type_valorisation_non_facture = '$type_valorisation'
					WHERE oid = $oid ;
					
					";
			}
			if ($type_ligne == "GEN") {
				$sqlcmd = "
					UPDATE activite.t_divers SET 
						valeur = '$valeur'
					WHERE code = '$code' ;
					
					";
			}
			
			$result  =  $database->exec($sqlcmd);
			
		}
	}
    
    
	if ($action == "execute") {
		$submitCmd = "..\import_activite\iCTI_calcul_encours.exe";
        $return = exec($submitCmd);
	}
}

function setRecords_kiwee_params() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = ctiFloatVal($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$valeur = utf8_decode($recordNode["valeur"]);
	$description = utf8_decode($recordNode["description"]);
	
	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$valeur = $valeur;
		$description = toSQL($description);

        // Mise à jour de données du paramètre
		if ($oid != -1) {
			$sqlcmd = "UPDATE activite.t_kiwee_params SET 
							code = '$code', 
							texte = '$texte', 
							valeur = '$valeur'::boolean,
							description = '$description'
						WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
	}
}


function erase_cache() {

	global $database;
	
	// Ancienne version
	if ($database->cticache == "true") {
		$sqlcmd = "SELECT cti_cache_erase as result from cache.cti_cache_erase('iCTI_activite')";
		$result = $database->exec($sqlcmd);
	}
	
	// Nouvelle version
	cleanAllCache("iCTI_activite");


}

/**
 * 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;
}

    
?>