<?php
require_once("../../base/php/startSession.php");
require_once("../../base/php//classDatabase.php");
require_once("../../base/php/Functions.php" );
require_once("../../base/php/Environnement.php");
require_once("../../base/php/WebAppLog.php");
require_once("../../outils/php/httpService_cacheManager_cleanCache.php");
require_once("../../base/php/lib/cti/Log/SettingsChangesLogger.php");


global $database;
global $action;
global $importString;
global $recordNode;

$tableName = @$_POST["tableName"];
if ($tableName == "") {
	$tableName = @$_GET["tableName"];
}

$action = @$_POST["action"];
if ($action == "") {
	$action = @$_GET["action"];
}

$importString = @$_POST["importString"];
if ($importString == "") {
	$importString = @$_GET["importString"];
}


$recordXML = @$_POST["recordNode"];
if ($recordXML == "") {
	$recordXML = @$_GET["recordNode"];
}
$recordNode = @simplexml_load_string($recordXML);

$database = new Database("iCTI");

$settingsLogger = new SettingsChangesLogger;

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

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

switch ($tableName) {
	case"t_finess" :
		$httpString .= setRecords_finess();
		break;
	case"t_specialites_internes_medecin" :
		$httpString .= setRecords_specialites_internes_medecin();
		break;
	case"t_medecins" :
		$httpString .= setRecords_medecins();
		break;
	case"t_tranches_ages" :
		$httpString .= setRecords_tranches_ages();
		break;
	case"t_unites_medicales" :
		$httpString .= setRecords_unites_medicales();
		break;
	case"t_unites_fonctionnelles" :
		$httpString .= setRecords_unites_fonctionnelles();
		break;
	case"t_ghm_dms" :
		$httpString .= setRecords_ghm_dms();
		break;
	case"t_prestations" :
		$httpString .= setRecords_prestations();
		break;
	case"t_lpp" :
		$httpString .= setRecords_lpp();
		break;
	case"t_ucd" :
		$httpString .= setRecords_ucd();
		break;
	case"t_comites_medicaux" :
		$httpString .= setRecords_comites_medicaux();
		break;
	case"t_departements" :
		$httpString .= setRecords_departements();
		break;
	case"t_codes_postaux" :
		$httpString .= setRecords_codes_postaux();
		break;
	case"t_secteurs_geographiques" :
		$httpString .= setRecords_secteurs_geographiques();
		break;
	case"t_modes_entree" :
		$httpString .= setRecords_modes_entree();
		break;
	case"t_modes_sortie" :
		$httpString .= setRecords_modes_sortie();
		break;
	case"t_provenances_entree" :
		$httpString .= setRecords_provenances_entree();
		break;
	case"t_destinations_sortie" :
		$httpString .= setRecords_destinations_sortie();
		break;
	case"t_diagnostics" :
		$httpString .= setRecords_diagnostics();
		break;
	case"t_etude_1" :
		$httpString .= setRecords_etude_1();
		break;
	// déprécié, remplacé par t_etude_6
	case"t_etude_2" :
		$httpString .= setRecords_etude_2();
		break;
	/* deprecated
	case"t_etude_4" :
		$httpString .= setRecords_etude_4();
		break;
	*/
	case"t_etude_5" :
		$httpString .= setRecords_etude_5();
		break;
	case"t_etude_6" :
		$httpString .= setRecords_etude_6();
		break;
	case"t_expert_controle" :
		$httpString .= setRecords_expert_controle();
		break;
	case"t_divers" :
		$httpString .= setRecords_divers();
		break;
	case"t_etude_7" :
		$httpString .= setRecords_etude_7();
		break;
}


erase_cache();

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



function setRecords_finess() {

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

	$oid = $recordNode["oid"];
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$date_debut_coefficient_mco_1 = $recordNode["date_debut_coefficient_mco_1"];
	$coefficient_mco_1 = toSQLnum($recordNode["coefficient_mco_1"]);
	$coefficient_transition_1 = toSQLnum($recordNode["coefficient_transition_1"]);
	$coefficient_ht_1 = toSQLnum($recordNode["coefficient_ht_1"]);
	$coefficient_geo_1 = toSQLnum($recordNode["coefficient_geo_1"]);
	$date_debut_coefficient_mco_2 = $recordNode["date_debut_coefficient_mco_2"];
	$coefficient_mco_2 = toSQLnum($recordNode["coefficient_mco_2"]);
	$coefficient_transition_2 = toSQLnum($recordNode["coefficient_transition_2"]);
	$coefficient_ht_2 = toSQLnum($recordNode["coefficient_ht_2"]);
	$coefficient_geo_2 = toSQLnum($recordNode["coefficient_geo_2"]);
	$date_debut_coefficient_mco_3 = $recordNode["date_debut_coefficient_mco_3"];
	$coefficient_mco_3 = toSQLnum($recordNode["coefficient_mco_3"]);
	$coefficient_transition_3 = toSQLnum($recordNode["coefficient_transition_3"]);
	$coefficient_ht_3 = toSQLnum($recordNode["coefficient_ht_3"]);
	$coefficient_geo_3 = toSQLnum($recordNode["coefficient_geo_3"]);
	$date_debut_coefficient_mco_4 = $recordNode["date_debut_coefficient_mco_4"];
	$coefficient_mco_4 = toSQLnum($recordNode["coefficient_mco_4"]);
	$coefficient_transition_4 = toSQLnum($recordNode["coefficient_transition_4"]);
	$coefficient_ht_4 = toSQLnum($recordNode["coefficient_ht_4"]);
	$coefficient_geo_4 = toSQLnum($recordNode["coefficient_geo_4"]);
	$date_debut_coefficient_mco_5 = $recordNode["date_debut_coefficient_mco_5"];
	$coefficient_mco_5 = toSQLnum($recordNode["coefficient_mco_5"]);
	$coefficient_transition_5 = toSQLnum($recordNode["coefficient_transition_5"]);
	$coefficient_ht_5 = toSQLnum($recordNode["coefficient_ht_5"]);
	$coefficient_geo_5 = toSQLnum($recordNode["coefficient_geo_5"]);
	$date_debut_coefficient_mco_6 = $recordNode["date_debut_coefficient_mco_6"];
	$coefficient_mco_6 = toSQLnum($recordNode["coefficient_mco_6"]);
	$coefficient_transition_6 = toSQLnum($recordNode["coefficient_transition_6"]);
	$coefficient_ht_6 = toSQLnum($recordNode["coefficient_ht_6"]);
	$coefficient_geo_6 = toSQLnum($recordNode["coefficient_geo_6"]);
	$date_debut_coefficient_mco_7 = $recordNode["date_debut_coefficient_mco_7"];
	$coefficient_mco_7 = toSQLnum($recordNode["coefficient_mco_7"]);
	$coefficient_transition_7 = toSQLnum($recordNode["coefficient_transition_7"]);
	$coefficient_ht_7 = toSQLnum($recordNode["coefficient_ht_7"]);
	$coefficient_geo_7 = toSQLnum($recordNode["coefficient_geo_7"]);
	$date_debut_coefficient_mco_8 = $recordNode["date_debut_coefficient_mco_8"];
	$coefficient_mco_8 = toSQLnum($recordNode["coefficient_mco_8"]);
	$coefficient_transition_8 = toSQLnum($recordNode["coefficient_transition_8"]);
	$coefficient_ht_8 = toSQLnum($recordNode["coefficient_ht_8"]);
	$coefficient_geo_8 = toSQLnum($recordNode["coefficient_geo_8"]);
	$date_debut_coefficient_mco_9 = $recordNode["date_debut_coefficient_mco_9"];
	$coefficient_mco_9 = toSQLnum($recordNode["coefficient_mco_9"]);
	$coefficient_transition_9 = toSQLnum($recordNode["coefficient_transition_9"]);
	$coefficient_ht_9 = toSQLnum($recordNode["coefficient_ht_9"]);
	$coefficient_geo_9 = toSQLnum($recordNode["coefficient_geo_9"]);

    $date_debut_coefficient_pacte_resp_1 = $recordNode["date_debut_coefficient_pacte_resp_1"];
    $coefficient_pacte_resp_1 = toSQLnum($recordNode["coefficient_pacte_resp_1"]);
    $date_debut_coefficient_pacte_resp_2 = $recordNode["date_debut_coefficient_pacte_resp_2"];
    $coefficient_pacte_resp_2 = toSQLnum($recordNode["coefficient_pacte_resp_2"]);
    $date_debut_coefficient_pacte_resp_3 = $recordNode["date_debut_coefficient_pacte_resp_3"];
    $coefficient_pacte_resp_3 = toSQLnum($recordNode["coefficient_pacte_resp_3"]);
    $date_debut_coefficient_pacte_resp_4 = $recordNode["date_debut_coefficient_pacte_resp_4"];
    $coefficient_pacte_resp_4 = toSQLnum($recordNode["coefficient_pacte_resp_4"]);
    $date_debut_coefficient_pacte_resp_5 = $recordNode["date_debut_coefficient_pacte_resp_5"];
    $coefficient_pacte_resp_5 = toSQLnum($recordNode["coefficient_pacte_resp_5"]);
    $date_debut_coefficient_pacte_resp_6 = $recordNode["date_debut_coefficient_pacte_resp_6"];
    $coefficient_pacte_resp_6 = toSQLnum($recordNode["coefficient_pacte_resp_6"]);
    $date_debut_coefficient_pacte_resp_7 = $recordNode["date_debut_coefficient_pacte_resp_7"];
    $coefficient_pacte_resp_7 = toSQLnum($recordNode["coefficient_pacte_resp_7"]);
    $date_debut_coefficient_pacte_resp_8 = $recordNode["date_debut_coefficient_pacte_resp_8"];
    $coefficient_pacte_resp_8 = toSQLnum($recordNode["coefficient_pacte_resp_8"]);
    $date_debut_coefficient_pacte_resp_9 = $recordNode["date_debut_coefficient_pacte_resp_9"];
    $coefficient_pacte_resp_9 = toSQLnum($recordNode["coefficient_pacte_resp_9"]);

	$tersante_id = toSQLnum($recordNode["tersante_id"]);
	
	$coefficient_mco_simulation_fg11 = toSQLnum($recordNode["coefficient_mco_simulation_fg11"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
		
			$temp_select = "";
			$temp_select_n = 0;
			if ($date_debut_coefficient_mco_1 != "31/12/2099" && $date_debut_coefficient_mco_1 != "" && $coefficient_mco_1 != 0) {
				$temp_select_n++;
				$temp_select = $temp_select . "(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_1'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_1 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_1 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_1 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_1 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($date_debut_coefficient_mco_2 != "31/12/2099" && $date_debut_coefficient_mco_2 != "" && $coefficient_mco_2 != 0) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . "(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_2'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_2 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_2 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_2 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_2 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($date_debut_coefficient_mco_3 != "31/12/2099" && $date_debut_coefficient_mco_3 != "" && $coefficient_mco_3 != 0) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . "(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_3'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_3 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_3 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_3 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_3 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($date_debut_coefficient_mco_4 != "31/12/2099" && $date_debut_coefficient_mco_4 != "" && $coefficient_mco_4 != 0) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . "(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_4'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_4 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_4 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_4 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_4 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($date_debut_coefficient_mco_5 != "31/12/2099" && $date_debut_coefficient_mco_5 != "" && $coefficient_mco_5 != 0) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . "(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_5'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_5 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_5 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_5 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_5 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($date_debut_coefficient_mco_6 != "31/12/2099" && $date_debut_coefficient_mco_6 != "" && $coefficient_mco_6 != 0) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . "(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_6'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_6 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_6 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_6 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_6 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($date_debut_coefficient_mco_7 != "31/12/2099" && $date_debut_coefficient_mco_7 != "" && $coefficient_mco_7 != 0) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . "(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_7'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_7 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_7 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_7 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_7 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($date_debut_coefficient_mco_8 != "31/12/2099" && $date_debut_coefficient_mco_8 != "" && $coefficient_mco_8 != 0) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . "(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_8'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_8 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_8 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_8 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_8 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($date_debut_coefficient_mco_9 != "31/12/2099" && $date_debut_coefficient_mco_9 != "" && $coefficient_mco_9 != 0) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL(";
				$temp_select = $temp_select . "SELECT '$date_debut_coefficient_mco_9'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_mco_9 AS coefficient_mco, ";
				$temp_select = $temp_select . "$coefficient_transition_9 AS coefficient_transition, ";
				$temp_select = $temp_select . "$coefficient_ht_9 AS coefficient_ht, ";
				$temp_select = $temp_select . "$coefficient_geo_9 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				if ($temp_select_n > 0) {
					$temp_select = $temp_select . " UNION ALL ";
				}
				$temp_select_n++;
				$temp_select = $temp_select . " (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			if ($temp_select_n < 9) {
				$temp_select_n++;
				$temp_select = $temp_select . " UNION ALL (";
				$temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_mco, ";
				$temp_select = $temp_select . "0 AS coefficient_transition, ";
				$temp_select = $temp_select . "0 AS coefficient_ht, ";
				$temp_select = $temp_select . "0 AS coefficient_geo";
				$temp_select = $temp_select . ")";
			}
			$temp_select = $temp_select . " ORDER BY 1";
			
			$sqlcmd = "DROP SEQUENCE IF EXISTS temp_sequence; CREATE TEMP SEQUENCE temp_sequence; ";
			$sqlcmd = $sqlcmd . "SELECT set_config('datestyle', 'ISO, DMY', false); ";
			$sqlcmd = $sqlcmd . "DROP TABLE IF EXISTS temp_table; CREATE TEMP TABLE temp_table AS ";
			$sqlcmd = $sqlcmd . "SELECT nextval('temp_sequence'::regclass) as sequence,  ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco, coefficient_mco, coefficient_transition, coefficient_ht, coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM ($temp_select) subview; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_1 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_1 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_1 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_1 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_1 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 1; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_2 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_2 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_2 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_2 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_2 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 2; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_3 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_3 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_3 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_3 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_3 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 3; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_4 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_4 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_4 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_4 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_4 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 4; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_5 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_5 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_5 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_5 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_5 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 5; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_6 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_6 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_6 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_6 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_6 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 6; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_7 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_7 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_7 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_7 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_7 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 7; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_8 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_8 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_8 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_8 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_8 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 8; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "date_debut_coefficient_mco_9 = temp_table.date_debut_coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_mco_9 = temp_table.coefficient_mco, ";
			$sqlcmd = $sqlcmd . "coefficient_transition_9 = temp_table.coefficient_transition, ";
			$sqlcmd = $sqlcmd . "coefficient_ht_9 = temp_table.coefficient_ht, ";
			$sqlcmd = $sqlcmd . "coefficient_geo_9 = temp_table.coefficient_geo ";
			$sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 9; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
			$sqlcmd = $sqlcmd . "coefficient_mco_simulation_fg11 = $coefficient_mco_simulation_fg11, ";
			$sqlcmd = $sqlcmd . "territoire_sante_id = $tersante_id ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid; ";
			$sqlcmd = $sqlcmd . "UPDATE base.t_finess SET dernier_coefficient_mco_fg10 =  ";
			$sqlcmd = $sqlcmd . "CASE WHEN date_debut_coefficient_mco_9 < '2008-12-31'  THEN coefficient_mco_9 ";
			$sqlcmd = $sqlcmd . "WHEN date_debut_coefficient_mco_8 < '2008-12-31' THEN coefficient_mco_8 ";
			$sqlcmd = $sqlcmd . "WHEN date_debut_coefficient_mco_7 < '2008-12-31' THEN coefficient_mco_7 ";
			$sqlcmd = $sqlcmd . "WHEN date_debut_coefficient_mco_6 < '2008-12-31' THEN coefficient_mco_6 ";
			$sqlcmd = $sqlcmd . "WHEN date_debut_coefficient_mco_5 < '2008-12-31' THEN coefficient_mco_5 ";
			$sqlcmd = $sqlcmd . "WHEN date_debut_coefficient_mco_4 < '2008-12-31' THEN coefficient_mco_4 ";
			$sqlcmd = $sqlcmd . "WHEN date_debut_coefficient_mco_3 < '2008-12-31' THEN coefficient_mco_3 ";
			$sqlcmd = $sqlcmd . "WHEN date_debut_coefficient_mco_2 < '2008-12-31' THEN coefficient_mco_2 ";
			$sqlcmd = $sqlcmd . "WHEN date_debut_coefficient_mco_1 < '2008-12-31' THEN coefficient_mco_1 ";
			$sqlcmd = $sqlcmd . "ELSE coefficient_mco_1 ";
			$sqlcmd = $sqlcmd . "END ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			
            // MAJ coefficient pacte responsabilité et CICE date_debut_coefficient_pacte_resp_8
            $temp_select = "";
            $temp_select_n = 0;


            if ($date_debut_coefficient_pacte_resp_1 != "31/12/2099" && $date_debut_coefficient_pacte_resp_1 != "" && $coefficient_pacte_resp_1 != 0) {
                $temp_select_n++;
                $temp_select = $temp_select . "(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_1'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_1 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($date_debut_coefficient_pacte_resp_2 != "31/12/2099" && $date_debut_coefficient_pacte_resp_2 != "" && $coefficient_pacte_resp_2 != 0) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . "(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_2'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_2 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($date_debut_coefficient_pacte_resp_3 != "31/12/2099" && $date_debut_coefficient_pacte_resp_3 != "" && $coefficient_pacte_resp_3 != 0) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . "(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_3'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_3 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($date_debut_coefficient_pacte_resp_4 != "31/12/2099" && $date_debut_coefficient_pacte_resp_4 != "" && $coefficient_pacte_resp_4 != 0) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . "(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_4'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_4 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($date_debut_coefficient_pacte_resp_5 != "31/12/2099" && $date_debut_coefficient_pacte_resp_5 != "" && $coefficient_pacte_resp_5 != 0) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . "(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_5'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_5 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($date_debut_coefficient_pacte_resp_6 != "31/12/2099" && $date_debut_coefficient_pacte_resp_6 != "" && $coefficient_pacte_resp_6 != 0) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . "(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_6'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_6 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($date_debut_coefficient_pacte_resp_7 != "31/12/2099" && $date_debut_coefficient_pacte_resp_7 != "" && $coefficient_pacte_resp_7 != 0) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . "(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_7'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_7 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($date_debut_coefficient_pacte_resp_8 != "31/12/2099" && $date_debut_coefficient_pacte_resp_8 != "" && $coefficient_pacte_resp_8 != 0) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . "(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_8'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_8 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($date_debut_coefficient_pacte_resp_9 != "31/12/2099" && $date_debut_coefficient_pacte_resp_9 != "" && $coefficient_pacte_resp_9 != 0) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL(";
                $temp_select = $temp_select . "SELECT '$date_debut_coefficient_pacte_resp_9'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "$coefficient_pacte_resp_9 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                if ($temp_select_n > 0) {
                    $temp_select = $temp_select . " UNION ALL ";
                }
                $temp_select_n++;
                $temp_select = $temp_select . " (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            if ($temp_select_n < 9) {
                $temp_select_n++;
                $temp_select = $temp_select . " UNION ALL (";
                $temp_select = $temp_select . "SELECT '2099/12/31'::date AS date_debut_coefficient_pacte_resp, ";
                $temp_select = $temp_select . "0 AS coefficient_pacte_resp ";
                $temp_select = $temp_select . ")";
            }
            $temp_select = $temp_select . " ORDER BY 1";


            $sqlcmd = "DROP SEQUENCE IF EXISTS temp_sequence; CREATE TEMP SEQUENCE temp_sequence; ";
            $sqlcmd = $sqlcmd . "SELECT set_config('datestyle', 'ISO, DMY', false); ";
            $sqlcmd = $sqlcmd . "DROP TABLE IF EXISTS temp_table; CREATE TEMP TABLE temp_table AS ";
            $sqlcmd = $sqlcmd . "SELECT nextval('temp_sequence'::regclass) as sequence,  ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp, coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM ($temp_select) subview; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_1 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_1 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 1; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_2 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_2 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 2; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_3 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_3 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 3; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_4 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_4 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 4; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_5 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_5 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 5; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_6 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_6 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 6; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_7 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_7 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 7; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_8 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_8 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 8; ";
            $sqlcmd = $sqlcmd . "UPDATE base.t_finess SET ";
            $sqlcmd = $sqlcmd . "date_debut_coefficient_pacte_resp_9 = temp_table.date_debut_coefficient_pacte_resp, ";
            $sqlcmd = $sqlcmd . "coefficient_pacte_resp_9 = temp_table.coefficient_pacte_resp ";
            $sqlcmd = $sqlcmd . "FROM temp_table WHERE t_finess.oid = $oid AND temp_table.sequence = 9; ";
            $result  =  $database->exec($sqlcmd);


		}
				

	}

	if ($action == "copy") {
	
		

	}


	if ($action == "delete") {
		
	}
	
	

}

function toSQLnum($val) {
	$num = str_replace(",", ".", $val) + 0;
	
	return $num;
}

function setRecords_specialites_internes_medecin() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$specialite_defaut_code_1 = $recordNode["specialite_defaut_code_1"];
	$specialite_defaut_code_2 = $recordNode["specialite_defaut_code_2"];
	$specialite_defaut_code_3 = $recordNode["specialite_defaut_code_3"];
	$specialite_defaut_code_4 = $recordNode["specialite_defaut_code_4"];
	$specialite_defaut_code_5 = $recordNode["specialite_defaut_code_5"];

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO pmsi.t_specialites_internes_medecin ";
			$sqlcmd = $sqlcmd . "( ";
			$sqlcmd = $sqlcmd . "code, texte, ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_1, ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_2, ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_3, ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_4, ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_5 ";
			$sqlcmd = $sqlcmd . ") ";
			$sqlcmd = $sqlcmd . "VALUES(";
			$sqlcmd = $sqlcmd . "'$code', '$texte', ";
			$sqlcmd = $sqlcmd . "'$specialite_defaut_code_1', ";
			$sqlcmd = $sqlcmd . "'$specialite_defaut_code_2', ";
			$sqlcmd = $sqlcmd . "'$specialite_defaut_code_3', ";
			$sqlcmd = $sqlcmd . "'$specialite_defaut_code_4', ";
			$sqlcmd = $sqlcmd . "'$specialite_defaut_code_5' ";
			$sqlcmd = $sqlcmd . ") ";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
				
			}
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_specialites_internes_medecin SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_1 = '$specialite_defaut_code_1', ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_2 = '$specialite_defaut_code_2', ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_3 = '$specialite_defaut_code_3', ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_4 = '$specialite_defaut_code_4', ";
			$sqlcmd = $sqlcmd . "specialite_defaut_code_5 = '$specialite_defaut_code_5' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$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(code, '999999')) as oid from pmsi.t_specialites_internes_medecin WHERE code LIKE '$fromcode%'");
		if ($result != false) {
			$record = $database->nextRecordInto();
			if ($record != FALSE) {
				$last_oid = $record[0];
			}
		}
		$last_oid++;
		
		$sqlcmd = "INSERT INTO pmsi.t_specialites_internes_medecin(code, texte) ";
		$sqlcmd = $sqlcmd . "SELECT '$fromcode$last_oid', 'Copie de ' || texte ";
		$sqlcmd = $sqlcmd . "FROM pmsi.t_specialites_internes_medecin WHERE oid = $oid";
		$result = $database->exec($sqlcmd);

	}


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

}

function setRecords_medecins() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$code = utf8_decode($recordNode["code"]);
	$nom = utf8_decode($recordNode["nom"]);
	$prenom = utf8_decode($recordNode["prenom"]);
	$specialite_interne_id = $recordNode["specialite_interne_id"] + 0;
	$medecin_reference_id = $recordNode["medecin_reference_id"] + 0;
	$medecins_regroupes_oids = trim($recordNode["medecins_regroupes_oids"]);
	$identifiant_annuaire = utf8_decode($recordNode["identifiant_annuaire"]);
	if ($medecins_regroupes_oids == "") {
		$medecins_regroupes_oids = "-1";
	}

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$nom = toSQL($nom);
		$prenom = toSQL($prenom);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_medecins SET 
					code = '$code', 
					nom = '$nom', 
					prenom = '$prenom', 
					identifiant_annuaire = '$identifiant_annuaire'
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			
			$sqlcmd = "UPDATE base.t_medecins t_medecins_reference
					SET specialite_id = $specialite_interne_id
					FROM pmsi.t_medecins 
					WHERE t_medecins.medecin_id = t_medecins_reference.oid AND 
						t_medecins.oid = $oid AND
						t_medecins_reference.specialite_id IS DISTINCT FROM $specialite_interne_id";
			$result  =  $database->exec($sqlcmd);
			
			
		}
				

	}

	if ($action == "copy") {
	
		

	}


	if ($action == "delete") {
		
	}
	
	update_medecins_c();

}

function setRecords_tranches_ages() {

	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"]);
	$age_min = $recordNode["age_min"] + 0;
	$age_max = $recordNode["age_max"] + 0;

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO pmsi.t_tranches_ages ";
			$sqlcmd = $sqlcmd . "( ";
			$sqlcmd = $sqlcmd . "code, texte, ";
			$sqlcmd = $sqlcmd . "texte_court, ";
			$sqlcmd = $sqlcmd . "age_min, ";
			$sqlcmd = $sqlcmd . "age_max ";
			$sqlcmd = $sqlcmd . ") ";
			$sqlcmd = $sqlcmd . "VALUES(";
			$sqlcmd = $sqlcmd . "'$code', '$texte', ";
			$sqlcmd = $sqlcmd . "'$texte_court', ";
			$sqlcmd = $sqlcmd . "$age_min, ";
			$sqlcmd = $sqlcmd . "$age_max ";
			$sqlcmd = $sqlcmd . ") ";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
				
			}
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_tranches_ages SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court', ";
			$sqlcmd = $sqlcmd . "age_min = $age_min, ";
			$sqlcmd = $sqlcmd . "age_max = $age_max ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$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(code, '999999')) as oid from pmsi.t_tranches_ages WHERE code LIKE '$fromcode%'");
		if ($result != false) {
			$record = $database->nextRecordInto();
			if ($record != FALSE) {
				$last_oid = $record[0];
			}
		}
		$last_oid++;
		
		$sqlcmd = "INSERT INTO pmsi.t_tranches_ages(code, texte, texte_court) ";
		$sqlcmd = $sqlcmd . "SELECT '$fromcode$last_oid', 'Copie de ' || texte, 'Copie de ' || texte_court ";
		$sqlcmd = $sqlcmd . "FROM pmsi.t_tranches_ages WHERE oid = $oid";
		$result = $database->exec($sqlcmd);

	}


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

}

function setRecords_unites_medicales() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$type_ventilation_ca = utf8_decode($recordNode["type_ventilation_ca"]);
	$type_eclatement = utf8_decode($recordNode["type_eclatement"]);
	$type_sejour = utf8_decode($recordNode["type_sejour"]);
	$type_autorisation = $recordNode["type_autorisation"];
	$date_effet_autorisation = $recordNode["date_effet_autorisation"];
	$nb_lits = $recordNode["nb_lits"];
	$mode_hospitalisation = $recordNode["mode_hospitalisation"];
	$medecin_par_defaut_id = $recordNode["medecin_par_defaut_id"] + 0;
	$um_expedition_id = $recordNode["um_expedition_id"] + 0;
	
	//WebAppLog("debug","debug",$texte);
	
	//return;


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$type_ventilation_ca = toSQL($type_ventilation_ca);
		$type_eclatement = toSQL($type_eclatement);
		$type_sejour = toSQL($type_sejour);
		$type_autorisation = toSQL($type_autorisation);
		$date_effet_autorisation = toSQL($date_effet_autorisation);
		$nb_lits = toSQL($nb_lits);
		$mode_hospitalisation = toSQL($mode_hospitalisation);
		$medecin_par_defaut_id = toSQL($medecin_par_defaut_id);
		$um_expedition_id = toSQL($um_expedition_id);
		
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_unites_medicales SET ";
			$sqlcmd = $sqlcmd . " code = '$code', ";
			$sqlcmd = $sqlcmd . " texte = '$texte', ";
			$sqlcmd = $sqlcmd . " type_ventilation_ca = '$type_ventilation_ca', ";
			$sqlcmd = $sqlcmd . " type_eclatement= '$type_eclatement', ";
			$sqlcmd = $sqlcmd . " type_sejour= '$type_sejour', ";
			$sqlcmd = $sqlcmd . " type_autorisation = '$type_autorisation', ";
			$sqlcmd = $sqlcmd . " date_effet_autorisation = " . ($date_effet_autorisation == "null" ? "null" : "'$date_effet_autorisation'::date") . ",";
			$sqlcmd = $sqlcmd . " nb_lits = " . ($nb_lits == "null" ? "null" : "'$nb_lits'") . ",";
			$sqlcmd = $sqlcmd . " mode_hospitalisation = " . ($mode_hospitalisation == "null" ? "null" : "'$mode_hospitalisation'") . ",";
			$sqlcmd = $sqlcmd . " medecin_par_defaut_id = $medecin_par_defaut_id, ";
			$sqlcmd = $sqlcmd . " um_expedition_id = $um_expedition_id ";
			$sqlcmd = $sqlcmd . " WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
				

	}

	if ($action == "copy") {
	
		

	}


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

}

function setRecords_unites_fonctionnelles() {
	
	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	
	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		}
		else {
			$sqlcmd = "
				UPDATE pmsi.t_unites_fonctionnelles SET
					code = '$code',
					texte = '$texte'
				WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
	}
	
	if ($action == "copy") {
	}
	if ($action == "delete") {
		$sqlcmd = "DELETE FROM pmsi.t_unites_fonctionnelles WHERE oid = $oid";
		$result  =  $database->exec($sqlcmd);
	}
}

function setRecords_ghm_dms() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$dms = utf8_decode($recordNode["dms"]);
	
	//WebAppLog("debug","debug",$dms);
	
	
	
	
	//return;


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_divers SET 
					texte = '$texte' 
					WHERE code = 'GHMDMS$oid'";
			$result  =  $database->exec($sqlcmd);
			
			$sqlcmd = "";
			
			$dms_couples_array = explode(";", $dms);
			foreach($dms_couples_array as $dms_couple) {
				$dms_couple_array = explode("=", $dms_couple);
				$ghm_id = $dms_couple_array[0] + 0;
				$ghm_dms = $dms_couple_array[1] + 0;

				$sqlcmd .= "UPDATE pmsi.t_ghm SET dms_$oid = $ghm_dms WHERE oid = $ghm_id AND dms_$oid <> $ghm_dms;\n";
				if (strlen($sqlcmd) > 6400)  {
					$result = $database->exec($sqlcmd);
					$sqlcmd = "";
				}
			
			}
			
			if (strlen($sqlcmd) > 6400)  {
				$result = $database->exec($sqlcmd);
			}

			
			
		}
				

	}

	if ($action == "copy") {
	
		

	}


	if ($action == "delete") {
		$sqlcmd = "UPDATE pmsi.t_divers SET 
					texte = '' 
					WHERE code = 'GHMDMS$oid'";
		$result  =  $database->exec($sqlcmd);
		
		$sqlcmd = "UPDATE pmsi.t_ghm SET dms_$oid = 0 WHERE dms_$oid <> 0;";
		$result = $database->exec($sqlcmd);

	}
	
	$sqlcmd = "UPDATE pmsi.t_ghm_c SET dms_1 = t_ghm.dms_1, dms_2 = t_ghm.dms_2, dms_3 = t_ghm.dms_3
		FROM pmsi.t_ghm
		WHERE t_ghm.oid = t_ghm_c.oid
		AND (t_ghm_c.dms_1 <> t_ghm.dms_1 OR t_ghm_c.dms_1 IS NULL
			OR t_ghm_c.dms_2 <> t_ghm.dms_2 OR t_ghm_c.dms_2 IS NULL
			OR t_ghm_c.dms_3 <> t_ghm.dms_3 OR t_ghm_c.dms_3 IS NULL
		)";
		$result = $database->exec($sqlcmd);
	

}

function setRecords_diagnostics() {

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

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

	if ($action == "copy") {
	}

	if ($action == "delete") {
	}
	

}

function setRecords_prestations() {

	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);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.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);
		}
				

	}

	if ($action == "copy") {
	
		
	}


	if ($action == "delete") {

	}
	

}

function setRecords_modes_entree() {

	global $database;
	global $action;
	global $recordNode;
	
	$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);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		
		$sqlcmd = "UPDATE pmsi.t_modes_entree SET ";
		$sqlcmd = $sqlcmd . "texte = '$texte', ";
		$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
		$sqlcmd = $sqlcmd . "WHERE code = $code";
		$result  =  $database->exec($sqlcmd);

	}

	if ($action == "copy") {
	}

	if ($action == "delete") {
	}
	

}

function setRecords_modes_sortie() {

	global $database;
	global $action;
	global $recordNode;
	
	$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);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		
		$sqlcmd = "UPDATE pmsi.t_modes_sortie SET ";
		$sqlcmd = $sqlcmd . "texte = '$texte', ";
		$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
		$sqlcmd = $sqlcmd . "WHERE code = $code";
		$result  =  $database->exec($sqlcmd);

	}

	if ($action == "copy") {
	}

	if ($action == "delete") {
	}
	

}

function setRecords_provenances_entree() {

	global $database;
	global $action;
	global $recordNode;
	
	$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);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		
		$sqlcmd = "UPDATE pmsi.t_provenances_entree SET ";
		$sqlcmd = $sqlcmd . "texte = '$texte', ";
		$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
		$sqlcmd = $sqlcmd . "WHERE code = $code";
		$result  =  $database->exec($sqlcmd);

	}

	if ($action == "copy") {
	}

	if ($action == "delete") {
	}
	

}

function setRecords_destinations_sortie() {

	global $database;
	global $action;
	global $recordNode;
	
	$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);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		
		$sqlcmd = "UPDATE pmsi.t_destinations_sortie SET ";
		$sqlcmd = $sqlcmd . "texte = '$texte', ";
		$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
		$sqlcmd = $sqlcmd . "WHERE code = $code";
		$result  =  $database->exec($sqlcmd);

	}

	if ($action == "copy") {
	}

	if ($action == "delete") {
	}
	

}

function setRecords_lpp() {

	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"]);
	$code_fg11 = utf8_decode($recordNode["code_fg11"]);

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		$code_fg11 = toSQL($code_fg11);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO pmsi.t_lpp ";
			$sqlcmd = $sqlcmd . "( ";
			$sqlcmd = $sqlcmd . "code, texte, ";
			$sqlcmd = $sqlcmd . "texte_court, ";
			$sqlcmd = $sqlcmd . "code_fg11 ";
			$sqlcmd = $sqlcmd . ") ";
			$sqlcmd = $sqlcmd . "VALUES(";
			$sqlcmd = $sqlcmd . "'$code', '$texte', ";
			$sqlcmd = $sqlcmd . "'$texte_court', ";
			$sqlcmd = $sqlcmd . "'$code_fg11' ";
			$sqlcmd = $sqlcmd . ") ";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
				
			}
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_lpp SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court', ";
			$sqlcmd = $sqlcmd . "code_fg11 = '$code_fg11' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$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 pmsi.t_lpp 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 pmsi.t_lpp(code, texte, texte_court) ";
		$sqlcmd = $sqlcmd . "SELECT '$fromcode$last_oid', 'Copie de ' || texte , 'Copie de ' || texte_court ";
		$sqlcmd = $sqlcmd . "FROM pmsi.t_lpp WHERE oid = $oid";
		$result = $database->exec($sqlcmd);

	}


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

}

function setRecords_ucd() {

	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"]);
	$laboratoire_texte = utf8_decode($recordNode["laboratoire_texte"]);

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		$laboratoire_texte = toSQL($laboratoire_texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO pmsi.t_ucd ";
			$sqlcmd = $sqlcmd . "( ";
			$sqlcmd = $sqlcmd . "code, texte, ";
			$sqlcmd = $sqlcmd . "texte_court, ";
			$sqlcmd = $sqlcmd . "laboratoire_texte ";
			$sqlcmd = $sqlcmd . ") ";
			$sqlcmd = $sqlcmd . "VALUES(";
			$sqlcmd = $sqlcmd . "'$code', '$texte', ";
			$sqlcmd = $sqlcmd . "'$texte_court', ";
			$sqlcmd = $sqlcmd . "'$laboratoire_texte' ";
			$sqlcmd = $sqlcmd . ") ";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
				
			}
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_ucd SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court', ";
			$sqlcmd = $sqlcmd . "laboratoire_texte = '$laboratoire_texte' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$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 pmsi.t_ucd 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 pmsi.t_ucd(code, texte, texte_court) ";
		$sqlcmd = $sqlcmd . "SELECT '$fromcode$last_oid', 'Copie de ' || texte , 'Copie de ' || texte_court ";
		$sqlcmd = $sqlcmd . "FROM pmsi.t_ucd WHERE oid = $oid";
		$result = $database->exec($sqlcmd);

	}


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

}

function setRecords_comites_medicaux() {

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

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$texte_court = toSQL($texte_court);
		if ($texte_court == "") {
			$texte_court = $texte;
		}
		$laboratoire_texte = toSQL($laboratoire_texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO pmsi.t_comites_medicaux ";
			$sqlcmd = $sqlcmd . "( ";
			$sqlcmd = $sqlcmd . "code, texte ";
			$sqlcmd = $sqlcmd . ") ";
			$sqlcmd = $sqlcmd . "VALUES(";
			$sqlcmd = $sqlcmd . "'$code', '$texte' ";
			$sqlcmd = $sqlcmd . ") ";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
				
			}
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_comites_medicaux SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$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 pmsi.t_comites_medicaux 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 pmsi.t_comites_medicaux(code, texte) ";
		$sqlcmd = $sqlcmd . "SELECT '$fromcode$last_oid', 'Copie de ' || texte  ";
		$sqlcmd = $sqlcmd . "FROM pmsi.t_comites_medicaux WHERE oid = $oid";
		$result = $database->exec($sqlcmd);

	}


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

}

function setRecords_departements() {

	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);
		$type_autorisation = toSQL($type_autorisation);
		
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE base.t_departements SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
				

	}

	if ($action == "copy") {
	
		

	}


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

}

function setRecords_codes_postaux() {

	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"]);
	$code_geographique_pmsi = utf8_decode($recordNode["code_geo_pmsi"]);
	$population = utf8_decode($recordNode["population"]) + 0;
	$departement_id = utf8_decode($recordNode["departement_id"]);


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

	}

	if ($action == "copy") {
	
		

	}


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

}

function setRecords_secteurs_geographiques() {

	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"]);
	$codes = utf8_decode($recordNode["codes"]);
	if ($texte_court == "") {
		$texte_court = $texte;
	}
	if (trim($codes) == "") {
		$codes = 9999999999;
	}
	
	// Codes à zéro
	$sqlcmd = "INSERT INTO pmsi.t_secteurs_geographiques_codes_postaux (secteur_geographique_id, code_postal_id) ";
	$sqlcmd = $sqlcmd . "SELECT 0, oid FROM base.t_codes_postaux WHERE oid NOT IN (SELECT code_postal_id FROM pmsi.t_secteurs_geographiques_codes_postaux)";
	$result = $database->exec($sqlcmd,"toto");
	
	$sqlcmd = "UPDATE pmsi.t_secteurs_geographiques_codes_postaux SET secteur_geographique_id = 0 WHERE secteur_geographique_id NOT IN (SELECT oid FROM pmsi.t_secteurs_geographiques)";
	$result = $database->exec($sqlcmd,"toto");


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO pmsi.t_secteurs_geographiques ";
			$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,"toto");
			if ($result != FALSE) {
				$result = $database->exec("SELECT max(oid) as oid from pmsi.t_secteurs_geographiques WHERE code = '$code'");
				if ($result != false) {
					$record = $database->nextRecordInto();
					if ($record != FALSE) {
						$oid = $record[0];
					}
				}
			}
			else {
			
			}
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_secteurs_geographiques SET ";
			$sqlcmd = $sqlcmd . "code = '$code', ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "texte_court = '$texte_court' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd,"toto");
		}
		
		if ($oid > 0) {
			$sqlcmd = "UPDATE pmsi.t_secteurs_geographiques_codes_postaux SET secteur_geographique_id = 0 WHERE secteur_geographique_id = $oid AND code_postal_id NOT IN ($codes)";
			$result = $database->exec($sqlcmd,"toto");
			
			$sqlcmd = "UPDATE pmsi.t_secteurs_geographiques_codes_postaux SET secteur_geographique_id = $oid WHERE secteur_geographique_id <> $oid AND code_postal_id IN ($codes)";
			$result = $database->exec($sqlcmd,"toto");
		}
				
	}

	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(code, '999999')) as oid from pmsi.t_secteurs_geographiques WHERE code LIKE '$fromcode%'");
		if ($result != false) {
			$record = $database->nextRecordInto();
			if ($record != FALSE) {
				$last_oid = $record[0];
			}
		}
		$last_oid++;
		
		$sqlcmd = "INSERT INTO pmsi.t_secteurs_geographiques(code, texte, texte_court) ";
		$sqlcmd = $sqlcmd . "SELECT '$fromcode$last_oid', 'Copie de ' || texte, '' ";
		$sqlcmd = $sqlcmd . "FROM pmsi.t_secteurs_geographiques WHERE oid = $oid";
		$result = $database->exec($sqlcmd);

	}


	if ($action == "delete") {
	
		$sqlcmd = "UPDATE pmsi.t_secteurs_geographiques_codes_postaux SET secteur_geographique_id = 0 WHERE secteur_geographique_id = $oid";
		$result = $database->exec($sqlcmd,"toto");
		
		$sqlcmd = "DELETE FROM pmsi.t_secteurs_geographiques WHERE oid = $oid";
		$result  =  $database->exec($sqlcmd,"toto");
		
		
	}
	
	
	
}

function setRecords_etude_1() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$taux_cible_2008 = utf8_decode($recordNode["taux_cible_2008"]) + 0;
	$taux_cible_2009 = utf8_decode($recordNode["taux_cible_2009"]) + 0;
	$taux_cible_2010 = utf8_decode($recordNode["taux_cible_2010"]) + 0;
	$taux_cible_2011 = utf8_decode($recordNode["taux_cible_2011"]) + 0;

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

		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_etude_1 SET ";
			$sqlcmd = $sqlcmd . "taux_cible_2008 = $taux_cible_2008, ";
			$sqlcmd = $sqlcmd . "taux_cible_2009 = $taux_cible_2009, ";
			$sqlcmd = $sqlcmd . "taux_cible_2010 = $taux_cible_2010, ";
			$sqlcmd = $sqlcmd . "taux_cible_2011 = $taux_cible_2011 ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
				

	}

	if ($action == "copy") {
	
		
	}


	if ($action == "delete") {

	}
	

}

function setRecords_etude_2() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$seuil_2008 = utf8_decode($recordNode["seuil_2008"]) + 0;
	$seuil_2009 = utf8_decode($recordNode["seuil_2009"]) + 0;
	$seuil_2010 = utf8_decode($recordNode["seuil_2010"]) + 0;
	$seuil_2011 = utf8_decode($recordNode["seuil_2011"]) + 0;

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

		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_etude_2 SET 
				seuil_2008 = $seuil_2008, 
				seuil_2009 = $seuil_2009, 
				seuil_2010 = $seuil_2010, 
				seuil_2011 = $seuil_2011 
				WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
				

	}

	if ($action == "copy") {
	
		
	}


	if ($action == "delete") {

	}
	

}

/* deprecated
function setRecords_etude_4() {
	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$seuil_regional_2012 = utf8_decode($recordNode["seuil_regional_2012"]);
	$seuil_regional_2009 = utf8_decode($recordNode["seuil_regional_2009"]);
	$seuil_regional_2010 = utf8_decode($recordNode["seuil_regional_2010"]);
	$seuil_regional_2011 = utf8_decode($recordNode["seuil_regional_2011"]);
	$seuil_national_2012 = utf8_decode($recordNode["seuil_national_2012"]);
	$seuil_national_2009 = utf8_decode($recordNode["seuil_national_2009"]);
	$seuil_national_2010 = utf8_decode($recordNode["seuil_national_2010"]);
	$seuil_national_2011 = utf8_decode($recordNode["seuil_national_2011"]);
	
	switch($action) {
		case "delete" :
			break;
		case "copy" :
			break;
		default :
			// creation ou mise à  jour de la liste
			if ($oid != -1) {
				$sqlcmd = "
					UPDATE pmsi.t_etude_4 SET 
						seuil_regional_2009 = $seuil_regional_2009, 
						seuil_regional_2010 = $seuil_regional_2010, 
						seuil_regional_2011 = $seuil_regional_2011,
						seuil_regional_2012 = $seuil_regional_2012,
						seuil_national_2009 = $seuil_national_2009, 
						seuil_national_2010 = $seuil_national_2010, 
						seuil_national_2011 = $seuil_national_2011,
						seuil_national_2012 = $seuil_national_2012
					WHERE oid = $oid
				";
				$result  =  $database->exec($sqlcmd);
			}
	}
	
}
*/

function setRecords_etude_5() {

	global $database;
	global $action;
	global $recordNode;
	
	$settings_file = simplexml_load_file(rootDir() . 'settings/settings.XML');
	$finess = getProperty($settings_file, 'ENVIRONMENT', 'FINESS');
	
	$oid = utf8_decode($recordNode["oid"]);
	// $taux_moyen_regional = utf8_decode($recordNode["taux_moyen_regional"]);
	// $taux_moyen_national = utf8_decode($recordNode["taux_moyen_national"]);
	$taux_cible_ets = utf8_decode($recordNode["taux_cible_ets"]);
	$taux_cible_ars = utf8_decode($recordNode["taux_cible_ars"]);
	
	switch($action) {
		case "delete" :
			break;
		case "copy" :
			break;
		default :
			// Mise à jour de la liste
			if ($oid != -1) {
				
				// Initialisation des variables
				$code = -1;
				$liste = "";
				$tersante_id = -1;
				$region_id = -1;
				
				// Récupère le code du geste marqueur, la sequence, le territoire de santé et la région pour un oid donné
				$sqlcmd = "	
					SELECT 
						code, 
						sequence, 
						region_id, 
						tersante_id 
					FROM 
						pmsi.t_etude_5 
					WHERE 
						oid = $oid;
				";
				$result = $database->exec($sqlcmd);
				
				while($row = $database->nextRecord()) {
					$code = $row[0];
					$sequence = $row[1];
					$region_id = $row[2];
					$tersante_id = $row[3];
				}
				
				// Mise à jour des infos du geste marqueur d'une liste donnée et d'un tersante donné
				$sqlcmd = "
					UPDATE pmsi.t_etude_5 SET 
						taux_cible_ets = $taux_cible_ets, 
						taux_cible_ars = $taux_cible_ars 
					WHERE oid = $oid 
				";
				$result  =  $database->exec($sqlcmd);
				
				// Mise à jour des seuils régionaux pour un geste marqueur donné
				if ($code != -1 && $sequence != "" && $region_id != -1) {
					$sqlcmd = "
						UPDATE pmsi.t_etude_5 SET 
							taux_cible_ars = $taux_cible_ars 
						WHERE 
							code = $code 
							AND sequence = $sequence 
							AND region_id = $region_id 
					";
					$result  =  $database->exec($sqlcmd);
				}
			}
	}
	
}

// seuils dégressivité sur la table t_etude_7
function setRecords_etude_7() {

	global $database;
	global $action;
	global $importString;
	global $recordNode;
	
	
	
	switch($action) {
		case "delete" :
			break;
		case "copy" :
			break;
		/* 
			import de fichier excel de seuils de dégressivité 
			première colonne : racine
			deuxieme colonne : taux correction janvier/fevrier
			troisième colonne : taux correction mars/decembre
		*/
		case "import" :
			$search = array('%', ',');
			$replace = array('', '.');
			$string = str_replace($search, $replace, $importString);
			$lignes = explode("\r", $string);
			foreach($lignes as $ligne) {
				$values = explode("\t", $ligne);
				$racine = trim($values[0]);
				$seuil1 = $values[1] + 0;
				$seuil2 = $values[2] + 0;
				if ($racine != "") {
					$sqlcmd = "UPDATE pmsi.t_etude_7 SET
						taux_correction_avant_mars = $seuil1 + 0, 
						taux_correction_apres_mars = $seuil2 + 0 
						WHERE code LIKE '$racine%'
					";
					$result  =  $database->exec($sqlcmd);
				}
			}
			break;
		default :
			$oid = utf8_decode($recordNode["oid"]);
			$taux_correction_avant_mars = utf8_decode($recordNode["taux_correction_avant_mars"]);
			$taux_correction_apres_mars = utf8_decode($recordNode["taux_correction_apres_mars"]);
			if ($oid != -1) {
				
				$sqlcmd = "
					UPDATE pmsi.t_etude_7 SET 
						taux_correction_avant_mars = $taux_correction_avant_mars + 0, 
						taux_correction_apres_mars = $taux_correction_apres_mars + 0 
					WHERE oid = $oid 
				";
				$result  =  $database->exec($sqlcmd);
			}
	}
	
}

function setRecords_etude_6() {

	global $database;
	global $action;
	global $recordNode;
	
	$settings_file = simplexml_load_file(rootDir() . 'settings/settings.XML');
	$finess = getProperty($settings_file, 'ENVIRONMENT', 'FINESS');
	
	$oid = utf8_decode($recordNode["oid"]);
	$seuil_ets = utf8_decode($recordNode["seuil_ets"]);
	$seuil_ars = utf8_decode($recordNode["seuil_ars"]);
	
	switch($action) {
		case "delete" :
			break;
		case "copy" :
			break;
		default :
			// Mise à jour de la liste
			if ($oid != -1) {
				
				// Initialisation des variables
				$code = -1;
				$annee = -1;
				$tersante_id = -1;
				$region_id = -1;
				
				// Récupère le code de l'activité, l'annee du cahier des charges, le territoire de santé et la région pour un oid donné
				$sqlcmd = "	
					SELECT 
						code, 
						annee, 
						region_id, 
						tersante_id 
					FROM 
						pmsi.t_etude_6 
					WHERE 
						oid = $oid;
				";
				$result = $database->exec($sqlcmd);
				
				while($row = $database->nextRecord()) {
					$code = $row[0];
					$annee = $row[1];
					$region_id = $row[2];
					$tersante_id = $row[3];
				}
				
				// Mise à jour des infos du code de l'activité pour une année de cahier des charges donné et un territoire de santé donné
				$sqlcmd = "
					UPDATE pmsi.t_etude_6 SET 
						seuil_ets = $seuil_ets, 
						seuil_ars = $seuil_ars 
					WHERE oid = $oid
				";
				$result  =  $database->exec($sqlcmd);
				
				// Mise à jour des seuils régionaux pour un code donné
				if ($code != -1 && $annee != "" && $region_id != -1) {
					$sqlcmd = "
						UPDATE pmsi.t_etude_6 SET 
							seuil_ars = $seuil_ars  
						WHERE 
							code = $code 
							AND annee = $annee 
							AND region_id = $region_id 
					";
					$result  =  $database->exec($sqlcmd);
				}
			}
	}
	
}

function update_medecins_c() {

	global $database;
	
	$sqlcmd = "SELECT pmsi.cti_reorganize_medecins_c() ";
	$result  =  $database->exec($sqlcmd);

}



function setRecords_expert_controle() {

	global $database;
	global $action;
	global $recordNode;
	
	$httpString = "";
	
	$mode = floatval($recordNode["mode"]);
	
	// Mise à jour controles unitaires
	if ($mode == 0) {
		$oid = floatval($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_hide = utf8_decode($recordNode["is_hide"]);
		$is_datim = utf8_decode($recordNode["is_datim"]);
		$is_global = utf8_decode($recordNode["is_global"]);
		$gravite_id = floatval($recordNode["gravite_id"]);
		$is_alert = ($recordNode["is_alert"]);
		
		$rule_oid1 = floatval($recordNode["rule_oid1"]);
		$sqlcmd_where1 = utf8_decode($recordNode["sqlcmd_where1"]);
		$sqlcmd_justificatif1 = utf8_decode($recordNode["sqlcmd_justificatif1"]);
		
		$rule_oid2 = floatval($recordNode["rule_oid2"]);
		$sqlcmd_where2 = utf8_decode($recordNode["sqlcmd_where2"]);
		$sqlcmd_justificatif2 = utf8_decode($recordNode["sqlcmd_justificatif2"]);
		
		$rule_oid3 = floatval($recordNode["rule_oid3"]);
		$sqlcmd_where3 = utf8_decode($recordNode["sqlcmd_where3"]);
		$sqlcmd_justificatif3 = utf8_decode($recordNode["sqlcmd_justificatif3"]);
		
		$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_datim = toSQL($is_datim);
		$is_hide = toSQL($is_hide);
		$is_global = toSQL($is_global);
		$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 pmsi.t_expert_controle 
						( 
							code, 
							texte, 
							texte_court, 
							texte_abrege, 
							description, 
							is_cti, 
							is_datim, 
							is_hide, 
							is_global,
							gravite_id,
							alert_exp
						) 
						VALUES(
							'$code', 
							'$texte', 
							'$texte_court', 
							'$texte_abrege',
							'$description', 
							'$is_cti', 
							'$is_datim', 
							'$is_hide', 
							'$is_global',
							$gravite_id,
							'$is_alert'
						) ";
				$result  =  $database->exec($sqlcmd);
				if ($result == FALSE) {
					
				}
				else {
					$result = $database->exec(
						"SELECT max(oid) as oid FROM
						pmsi.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 pmsi.t_expert_controle SET 
							texte = '$texte', 
							texte_court = '$texte_court', 
							texte_abrege = '$texte_abrege',
							description = '$description',
							is_cti = '$is_cti' , 
							is_datim = '$is_datim' , 
							is_hide = '$is_hide' , 
							is_global = '$is_global',
							gravite_id = $gravite_id,
							alert_exp = '$is_alert'
						WHERE oid = $oid;
						UPDATE pmsi.t_expert_controle SET 
							code = '$code'
						WHERE oid = $oid AND is_datim IS DISTINCT FROM '1';
						";
				$result  =  $database->exec($sqlcmd);
			}
			
			// Ajout des règles
			$sqlcmd = "DELETE FROM pmsi.t_expert_controle_rule
				USING 
				(
				SELECT t_expert_controle_rule.oid
				FROM pmsi.t_expert_controle_rule
				LEFT JOIN pmsi.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 pmsi.t_expert_controle_rule (
					controle_id,
					numero,
					sqlcmd_where,
					sqlcmd_justificatif
					)
				SELECT t_expert_controle.oid AS controle_id, 
					1::bigint AS numero,
					'' AS sqlcmd_where,
					'' AS sqlcmd_justificatif
				FROM pmsi.t_expert_controle
				LEFT JOIN pmsi.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
				FROM pmsi.t_expert_controle
				LEFT JOIN pmsi.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
				FROM pmsi.t_expert_controle
				LEFT JOIN pmsi.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 pmsi.t_expert_controle_rule
					SET sqlcmd_where = '$sqlcmd_where1', 
						sqlcmd_justificatif = '$sqlcmd_justificatif1'
					WHERE controle_id = $oid AND
						numero = 1;
					UPDATE pmsi.t_expert_controle_rule
					SET sqlcmd_where = '$sqlcmd_where2', 
						sqlcmd_justificatif = '$sqlcmd_justificatif2'
					WHERE controle_id = $oid AND
						numero = 2;
					UPDATE pmsi.t_expert_controle_rule
					SET sqlcmd_where = '$sqlcmd_where3', 
						sqlcmd_justificatif = '$sqlcmd_justificatif3'
					WHERE controle_id = $oid AND
						numero = 3";
					$result  =  $database->exec($sqlcmd);	

				
			}
					

		}

		if ($action == "copy") {

			$sqlcmd = "DROP TABLE IF EXISTS w_expert_controle;
				CREATE TEMP TABLE w_expert_controle AS
				SELECT 
					t_expert_controle_2.oid,
					nextval('pmsi.s_expert_controle'::regclass) AS to_oid,
					t_expert_controle_2.code || '_' ||
					(GREATEST(MAX(base.cti_to_number(substr(t_expert_controle.code,length(t_expert_controle_2.code)+2,10))),count(*)) + 1)::text AS to_code,
					MAX('Copie de ' || t_expert_controle_2.texte) AS to_texte,
					MAX('Copie de ' || t_expert_controle_2.texte_court) AS to_texte_court
				FROM pmsi.t_expert_controle t_expert_controle_2 
				LEFT JOIN pmsi.t_expert_controle ON 
					(t_expert_controle.code) LIKE t_expert_controle_2.code || '_' || '%'
				WHERE t_expert_controle_2.oid = $oid
				GROUP BY t_expert_controle_2.oid, t_expert_controle_2.code;

				INSERT INTO pmsi.t_expert_controle(oid, code, texte, texte_court) 
				SELECT to_oid, to_code, to_texte, to_texte_court
				FROM w_expert_controle;

				INSERT INTO pmsi.t_expert_controle_rule(controle_id, sqlcmd_where, sqlcmd_justificatif, numero)
				SELECT
					w_expert_controle.to_oid,
					t_expert_controle_rule.sqlcmd_where,
					t_expert_controle_rule.sqlcmd_justificatif,
					t_expert_controle_rule.numero
				FROM pmsi.t_expert_controle_rule
				JOIN w_expert_controle ON w_expert_controle.oid = t_expert_controle_rule.controle_id";
			$result = $database->exec($sqlcmd);

		}
		


		if ($action == "delete") {
			$sqlcmd = "DELETE FROM pmsi.t_expert_controle WHERE oid = $oid AND is_datim <> '1'";
			$result  =  $database->exec($sqlcmd);
		}
		
		if ($action == "purge_history") {
			$sqlcmd = "DELETE FROM pmsi.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"]);
		$EXPERT_DATE_SELECT = utf8_decode($recordNode["EXPERT_DATE_SELECT"]);
		$autorisation_neuro = utf8_decode($recordNode["autorisation_neuro"]);
		$autorisation_cardio = utf8_decode($recordNode["autorisation_cardio"]);
		$autorisation_rea = utf8_decode($recordNode["autorisation_rea"]);
		$autorisation_rep = utf8_decode($recordNode["autorisation_rep"]);
		$autorisation_nn = utf8_decode($recordNode["autorisation_nn"]);
		$service_urgence = utf8_decode($recordNode["service_urgence"]);
		$service_ssr = utf8_decode($recordNode["service_ssr"]);
		$service_sld = utf8_decode($recordNode["service_sld"]);
		$service_psy = utf8_decode($recordNode["service_psy"]);

		if ($action == "save") {

		
			$sqlcmd = "UPDATE pmsi.t_divers
					SET valeur = '$EXPERT_ACTIVE'
					WHERE code = 'EXPERT_ACTIVE' AND
						valeur IS DISTINCT FROM '$EXPERT_ACTIVE'; ";
			$result = $database->exec($sqlcmd);
            
            $sqlcmd = "UPDATE pmsi.t_divers
					SET valeur = '$EXPERT_DATE_SELECT'
					WHERE code = 'EXPERT_DATE_SELECT' AND
						valeur IS DISTINCT FROM '$EXPERT_DATE_SELECT'; ";
			$result = $database->exec($sqlcmd);
			
			$sqlcmd = "UPDATE pmsi.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);
			
			$sqlcmd = "UPDATE base.t_finess SET
						datim_autorisation_neuro = '$autorisation_neuro',
						datim_autorisation_cardio = '$autorisation_cardio',
						datim_autorisation_rea = '$autorisation_rea',
						datim_autorisation_rep = '$autorisation_rep',
						datim_autorisation_nn = '$autorisation_nn',
						datim_service_urgence = '$service_urgence',
						datim_service_ssr = '$service_ssr',
						datim_service_sld = '$service_sld',
						datim_service_psy = '$service_psy'
						WHERE secondaire = '0' AND
							code IN (SELECT finess FROM pmsi.p_rss WHERE date_sortie >= '20130101') AND
							(
								datim_autorisation_neuro IS DISTINCT FROM '$autorisation_neuro' OR
								datim_autorisation_cardio IS DISTINCT FROM '$autorisation_cardio' OR
								datim_autorisation_rea IS DISTINCT FROM '$autorisation_rea' OR
								datim_autorisation_rep IS DISTINCT FROM '$autorisation_rep' OR
								datim_autorisation_nn IS DISTINCT FROM '$autorisation_nn' OR
								datim_service_urgence IS DISTINCT FROM '$service_urgence' OR
								datim_service_ssr IS DISTINCT FROM '$service_ssr' OR
								datim_service_sld IS DISTINCT FROM '$service_sld' OR
								datim_service_psy IS DISTINCT FROM '$service_psy'
							)";
			$result = $database->exec($sqlcmd);
		}
		
	}
	
	
	
	// exécution des règles
	if ($action == "execute_rules") {
		$return_code = "";
		$return_message = "";
		
		$oids = @$_POST["oids"];
		if ($oids == "") {
			$oids = @$_GET["oids"];
		}
		if ("$oids" == "") {
			$oids = "NULL";
		}
		$sqlcmd = "SELECT pmsi.cti_expert_execute_rules($oids,'1')";
		$result  =  $database->exec($sqlcmd);
		if ($result != false) {
			$record = $database->nextRecordInto();
			if ($record != FALSE) {
				$return_message = trim($record[0]);
			}
		}
		if (substr($return_message,0,3) == "OK:") {
			$return_code = "OK";
			$return_message = substr($return_message,3);
		} 
		else {
			if (substr($return_message,0,3) == "KO:") {
				$return_code = "KO";
				$return_message = substr($return_message,3);
			}
			else {
				$return_code = "KO";
				$return_message = "Problème technique lors de l'exécution ($return_messaage)";
			}
		}
		
		$sqlcmd = "VACUUM ANALYSE pmsi.p_expert_rss_controle;";
		$result  =  $database->exec($sqlcmd);
	
		
		$httpString = "<RETURN code=\"$return_code\" message=\"$return_message\" />";
	}
	
	
	// exécution des règles
	if ($action == "create_documentation" || $action == "save" || $action == "delete") {
	
		$reference_template_file = "../references/iCTI_PMSI_ExpertControle.template.HTML";
		$reference_file = "../references/iCTI_PMSI_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 pmsi.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_divers() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = floatval($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$valeur = utf8_decode($recordNode["valeur"]);
	$description = utf8_decode($recordNode["description"]);
    $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 = toSQL($show_info_module);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_divers SET ";
			$sqlcmd = $sqlcmd . "valeur = '$valeur',";
            $sqlcmd = $sqlcmd . "show_info_module = ('$show_info_module' = 1) ";
			$sqlcmd = $sqlcmd . "WHERE code = '$code' ";
			$result  =  $database->exec($sqlcmd);
			
		}
				

	}

	if ($action == "copy") {
	
		

	}


	if ($action == "delete") {
		
	}
	
}

function erase_cache() {

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


}

?>