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

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

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

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

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

$database = new Database("iCTI");

$settingsLogger = new SettingsChangesLogger;

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

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

if ($tableName == "t_sites") {
	$httpString = $httpString . setRecords_sites();
}
if ($tableName == "t_journaux") {
	$httpString = $httpString . setRecords_journaux();
}
if ($tableName == "t_types_compta") {
	$httpString = $httpString . setRecords_types_compta();
}
if ($tableName == "t_classes_comptes") {
	$httpString = $httpString . setRecords_classes_comptes();
}
if ($tableName == "t_comptes") {
	$httpString = $httpString . setRecords_comptes();
}
if ($tableName == "t_comptes_extra") {
	$httpString = $httpString . setRecords_comptes_extra();
}
if ($tableName == "t_partenaires") {
	$httpString = $httpString . setRecords_partenaires();
}
if ($tableName == "t_ecritures_ajustement") {
	$httpString = $httpString . setRecords_ecritures_ajustement();
}
if ($tableName == "t_budget") {
	$httpString = $httpString . setRecords_budget();
}
if ($tableName == "t_divers") {
    $httpString .= setRecords_divers();
}


erase_cache();



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


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


function setRecords_journaux() {

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


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

	}

	if ($action == "copy") {
	
		

	}


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


}

function setRecords_sites() {

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


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

	}

	if ($action == "copy") {
	
		

	}


	if ($action == "delete") {
		
	}
    
    // Chaque site peut être associé à un FINESS (pour essentiels)
    $sqlcmd = "UPDATE compta.t_sites SET
                finess_code = t_finess.code,
                finess_texte = t_finess.texte
            FROM base.t_finess
            WHERE finess_id = t_finess.oid AND
                (
                finess_code IS DISTINCT FROM t_finess.code or
                finess_texte IS DISTINCT FROM t_finess.texte
	)";
	$result  =  $database->exec($sqlcmd);
	
}


function setRecords_types_compta() {

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


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

	}

	if ($action == "copy") {
	
		

	}


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


}


function setRecords_classes_comptes() {

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


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE compta.t_classes_comptes SET ";
			$sqlcmd = $sqlcmd . "texte = '$texte' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			
			$sqlcmd = "UPDATE compta.t_comptes_c ";
			$sqlcmd = $sqlcmd . "SET  ";
			$sqlcmd = $sqlcmd . "classe_1_code = t_classe_1.code, ";
			$sqlcmd = $sqlcmd . "classe_1_texte = t_classe_1.texte, ";
			$sqlcmd = $sqlcmd . "classe_2_code = t_classe_2.code, ";
			$sqlcmd = $sqlcmd . "classe_2_texte = t_classe_2.texte, ";
			$sqlcmd = $sqlcmd . "classe_3_code = t_classe_3.code, ";
			$sqlcmd = $sqlcmd . "classe_3_texte = t_classe_3.texte, ";
			$sqlcmd = $sqlcmd . "classe_4_code = t_classe_4.code, ";
			$sqlcmd = $sqlcmd . "classe_4_texte = t_classe_4.texte, ";
			$sqlcmd = $sqlcmd . "classe_5_code = t_classe_5.code, ";
			$sqlcmd = $sqlcmd . "classe_5_texte = t_classe_5.texte, ";
			$sqlcmd = $sqlcmd . "classe_6_code = t_classe_6.code, ";
			$sqlcmd = $sqlcmd . "classe_6_texte = t_classe_6.texte ";
			$sqlcmd = $sqlcmd . "FROM  ";
			$sqlcmd = $sqlcmd . "compta.t_classes_comptes t_classe_1,  ";
			$sqlcmd = $sqlcmd . "compta.t_classes_comptes t_classe_2,  ";
			$sqlcmd = $sqlcmd . "compta.t_classes_comptes t_classe_3,  ";
			$sqlcmd = $sqlcmd . "compta.t_classes_comptes t_classe_4,  ";
			$sqlcmd = $sqlcmd . "compta.t_classes_comptes t_classe_5,  ";
			$sqlcmd = $sqlcmd . "compta.t_classes_comptes t_classe_6 ";
			$sqlcmd = $sqlcmd . "WHERE  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$sqlcmd = $sqlcmd . "AND t_comptes_c.classe_1_id = t_classe_1.oid ";
			$sqlcmd = $sqlcmd . "AND t_comptes_c.classe_2_id = t_classe_2.oid ";
			$sqlcmd = $sqlcmd . "AND t_comptes_c.classe_3_id = t_classe_3.oid ";
			$sqlcmd = $sqlcmd . "AND t_comptes_c.classe_4_id = t_classe_4.oid ";
			$sqlcmd = $sqlcmd . "AND t_comptes_c.classe_5_id = t_classe_5.oid ";
			$sqlcmd = $sqlcmd . "AND t_comptes_c.classe_6_id = t_classe_6.oid ";
			$result  =  $database->exec($sqlcmd);
			
			
			$sqlcmd = "UPDATE compta.t_comptes_c SET  ";
			$sqlcmd = $sqlcmd . "arbre_classe_1_id = 0,  ";
			$sqlcmd = $sqlcmd . "arbre_classe_1_code = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_1_texte = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_2_id = 0,  ";
			$sqlcmd = $sqlcmd . "arbre_classe_2_code = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_2_texte = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_3_id = 0,  ";
			$sqlcmd = $sqlcmd . "arbre_classe_3_code = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_3_texte = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_4_id = 0,  ";
			$sqlcmd = $sqlcmd . "arbre_classe_4_code = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_4_texte = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_5_id = 0,  ";
			$sqlcmd = $sqlcmd . "arbre_classe_5_code = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_5_texte = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_6_id = 0,  ";
			$sqlcmd = $sqlcmd . "arbre_classe_6_code = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_6_texte = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_7_id = 0,  ";
			$sqlcmd = $sqlcmd . "arbre_classe_7_code = '',  ";
			$sqlcmd = $sqlcmd . "arbre_classe_7_texte = ''; ";
			$sqlcmd = $sqlcmd . "WHERE  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$result  =  $database->exec($sqlcmd);

			
			$sqlcmd = "UPDATE compta.t_comptes_c ";
			$sqlcmd = $sqlcmd . "SET  ";
			$sqlcmd = $sqlcmd . "arbre_classe_1_id = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_1_texte <> '' THEN classe_1_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_2_texte <> '' THEN classe_2_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' THEN classe_3_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' THEN classe_4_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' THEN classe_5_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' THEN classe_6_id  ";
			$sqlcmd = $sqlcmd . "	ELSE oid  ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_1_code = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_1_texte <> '' THEN classe_1_code ";
			$sqlcmd = $sqlcmd . "	WHEN classe_2_texte <> '' THEN classe_2_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' THEN classe_3_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' THEN classe_4_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' THEN classe_5_code ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' THEN classe_6_code ";
			$sqlcmd = $sqlcmd . "	ELSE numero ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_1_texte = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_1_texte <> '' THEN classe_1_texte  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_2_texte <> '' THEN classe_2_texte  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' THEN classe_3_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' THEN classe_4_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' THEN classe_5_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' THEN classe_6_texte ";
			$sqlcmd = $sqlcmd . "	ELSE texte ";
			$sqlcmd = $sqlcmd . "END ";
			$sqlcmd = $sqlcmd . "WHERE  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$result  =  $database->exec($sqlcmd);


			$sqlcmd = "UPDATE compta.t_comptes_c ";
			$sqlcmd = $sqlcmd . "SET  ";
			$sqlcmd = $sqlcmd . "arbre_classe_2_id = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_2_texte <> '' AND arbre_classe_1_code <> classe_2_code THEN classe_2_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' AND arbre_classe_1_code <> classe_3_code THEN classe_3_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code THEN classe_4_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code THEN classe_5_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code THEN classe_6_id  ";
			$sqlcmd = $sqlcmd . "	ELSE oid  ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_2_code = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_2_texte <> '' AND arbre_classe_1_code <> classe_2_code THEN classe_2_code ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' AND arbre_classe_1_code <> classe_3_code THEN classe_3_code ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code THEN classe_4_code ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code THEN classe_5_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code THEN classe_6_code ";
			$sqlcmd = $sqlcmd . "	ELSE numero ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_2_texte = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_2_texte <> '' AND arbre_classe_1_code <> classe_2_code THEN classe_2_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' AND arbre_classe_1_code <> classe_3_code THEN classe_3_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code THEN classe_4_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code THEN classe_5_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code THEN classe_6_texte ";
			$sqlcmd = $sqlcmd . "	ELSE texte ";
			$sqlcmd = $sqlcmd . "END ";
			$sqlcmd = $sqlcmd . "WHERE arbre_classe_1_code <> numero ";
			$sqlcmd = $sqlcmd . "AND  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$result  =  $database->exec($sqlcmd);


			$sqlcmd = "UPDATE compta.t_comptes_c ";
			$sqlcmd = $sqlcmd . "SET  ";
			$sqlcmd = $sqlcmd . "arbre_classe_3_id = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' AND arbre_classe_1_code <> classe_3_code AND arbre_classe_2_code <> classe_3_code THEN classe_3_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code AND arbre_classe_2_code <> classe_4_code THEN classe_4_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code THEN classe_5_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code THEN classe_6_id  ";
			$sqlcmd = $sqlcmd . "	ELSE oid  ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_3_code = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' AND arbre_classe_1_code <> classe_3_code AND arbre_classe_2_code <> classe_3_code THEN classe_3_code ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code AND arbre_classe_2_code <> classe_4_code THEN classe_4_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code THEN classe_5_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code THEN classe_6_code ";
			$sqlcmd = $sqlcmd . "	ELSE numero ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_3_texte = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_3_texte <> '' AND arbre_classe_1_code <> classe_3_code AND arbre_classe_2_code <> classe_3_code THEN classe_3_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code AND arbre_classe_2_code <> classe_4_code THEN classe_4_texte  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code THEN classe_5_texte  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code THEN classe_6_texte ";
			$sqlcmd = $sqlcmd . "	ELSE texte ";
			$sqlcmd = $sqlcmd . "END ";
			$sqlcmd = $sqlcmd . "WHERE arbre_classe_1_code <> numero AND arbre_classe_2_code <> numero ";
			$sqlcmd = $sqlcmd . "AND  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$result  =  $database->exec($sqlcmd);


			$sqlcmd = "UPDATE compta.t_comptes_c ";
			$sqlcmd = $sqlcmd . "SET  ";
			$sqlcmd = $sqlcmd . "arbre_classe_4_id = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code AND arbre_classe_2_code <> classe_4_code AND arbre_classe_3_code <> classe_4_code THEN classe_4_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code AND arbre_classe_3_code <> classe_5_code THEN classe_5_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code THEN classe_6_id  ";
			$sqlcmd = $sqlcmd . "	ELSE oid  ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_4_code = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code AND arbre_classe_2_code <> classe_4_code AND arbre_classe_3_code <> classe_4_code THEN classe_4_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code AND arbre_classe_3_code <> classe_5_code THEN classe_5_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code THEN classe_6_code  ";
			$sqlcmd = $sqlcmd . "	ELSE numero ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_4_texte = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_4_texte <> '' AND arbre_classe_1_code <> classe_4_code AND arbre_classe_2_code <> classe_4_code AND arbre_classe_3_code <> classe_4_code THEN classe_4_texte  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code AND arbre_classe_3_code <> classe_5_code THEN classe_5_texte  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code THEN classe_6_texte  ";
			$sqlcmd = $sqlcmd . "	ELSE texte ";
			$sqlcmd = $sqlcmd . "END ";
			$sqlcmd = $sqlcmd . "WHERE arbre_classe_1_code <> numero AND arbre_classe_2_code <> numero AND arbre_classe_3_code <> numero ";
			$sqlcmd = $sqlcmd . "AND  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$result  =  $database->exec($sqlcmd);


			$sqlcmd = "UPDATE compta.t_comptes_c ";
			$sqlcmd = $sqlcmd . "SET  ";
			$sqlcmd = $sqlcmd . "arbre_classe_5_id = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code AND arbre_classe_3_code <> classe_5_code AND arbre_classe_4_code <> classe_5_code THEN classe_5_id  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code THEN classe_6_id  ";
			$sqlcmd = $sqlcmd . "	ELSE oid  ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_5_code = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code AND arbre_classe_3_code <> classe_5_code AND arbre_classe_4_code <> classe_5_code THEN classe_5_code  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code THEN classe_6_code  ";
			$sqlcmd = $sqlcmd . "	ELSE numero ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_5_texte = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_5_texte <> '' AND arbre_classe_1_code <> classe_5_code AND arbre_classe_2_code <> classe_5_code AND arbre_classe_3_code <> classe_5_code AND arbre_classe_4_code <> classe_5_code THEN classe_5_texte ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code THEN classe_6_texte  ";
			$sqlcmd = $sqlcmd . "	ELSE texte ";
			$sqlcmd = $sqlcmd . "END ";
			$sqlcmd = $sqlcmd . "WHERE arbre_classe_1_code <> numero AND arbre_classe_2_code <> numero AND arbre_classe_3_code <> numero AND arbre_classe_4_code <> numero ";
			$sqlcmd = $sqlcmd . "AND  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$result  =  $database->exec($sqlcmd);

			
			$sqlcmd = "UPDATE compta.t_comptes_c ";
			$sqlcmd = $sqlcmd . "SET  ";
			$sqlcmd = $sqlcmd . "arbre_classe_6_id = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code THEN classe_6_id  ";
			$sqlcmd = $sqlcmd . "	ELSE oid  ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_6_code = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code THEN classe_6_code ";
			$sqlcmd = $sqlcmd . "	ELSE numero ";
			$sqlcmd = $sqlcmd . "END, ";
			$sqlcmd = $sqlcmd . "arbre_classe_6_texte = ";
			$sqlcmd = $sqlcmd . "CASE  ";
			$sqlcmd = $sqlcmd . "	WHEN classe_6_texte <> '' AND arbre_classe_1_code <> classe_6_code AND arbre_classe_2_code <> classe_6_code AND arbre_classe_3_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code AND arbre_classe_4_code <> classe_6_code THEN classe_6_texte ";
			$sqlcmd = $sqlcmd . "	ELSE texte ";
			$sqlcmd = $sqlcmd . "END ";
			$sqlcmd = $sqlcmd . "WHERE arbre_classe_1_code <> numero AND arbre_classe_2_code <> numero AND arbre_classe_3_code <> numero AND arbre_classe_4_code <> numero AND arbre_classe_5_code <> numero ";
			$sqlcmd = $sqlcmd . "AND  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$result  =  $database->exec($sqlcmd);
			


			$sqlcmd = "UPDATE compta.t_comptes_c ";
			$sqlcmd = "SET  ";
			$sqlcmd = "arbre_classe_7_id = oid,  ";
			$sqlcmd = "arbre_classe_7_code = numero, ";
			$sqlcmd = "arbre_classe_7_texte = texte ";
			$sqlcmd = "WHERE arbre_classe_1_code <> numero AND arbre_classe_2_code <> numero AND arbre_classe_3_code <> numero AND arbre_classe_4_code <> numero AND arbre_classe_5_code <> numero AND arbre_classe_6_code <> numero ";
			$sqlcmd = $sqlcmd . "AND  ";
			$sqlcmd = $sqlcmd . "(t_comptes_c.classe_1_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_2_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_3_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_4_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_5_id = $oid ";
			$sqlcmd = $sqlcmd . "OR t_comptes_c.classe_6_id = $oid) ";
			$result  =  $database->exec($sqlcmd);
			
		}
				

	}

	if ($action == "copy") {
	
		

	}


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

}


function setRecords_comptes() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$numero = toSQL(utf8_decode($recordNode["numero"]));
	$texte = toSQL(utf8_decode($recordNode["texte"]));
	$numero_original = toSQL(utf8_decode($recordNode["numero_original"]));
	$clinique_honoraire = utf8_decode($recordNode["clinique_honoraire"]);
	$banque = utf8_decode($recordNode["banque"]);
	$solde_initial = utf8_decode($recordNode["solde_initial"]);
	$solde_initial = str_replace(",", ".", $solde_initial);
	$solde_initial = $solde_initial + 0;
	$inter_site = utf8_decode($recordNode["inter_site"]);
	
	$collectif = utf8_decode($recordNode["collectif"]);
	$type_compta_extra_id = $recordNode["type_compta_extra_id"] + 0;
	$numero_extra = toSQL(utf8_decode($recordNode["numero_extra"]));
	$texte_extra = toSQL(utf8_decode($recordNode["texte_extra"]));
	if ($collectif != "1") {
		$type_compta_extra_id = 0;
		$numero_extra = "";
		$texte_extra = "";
	}
	else {
		if ($numero_extra == "") {
			$numero_extra = "$numero.*";
		}
		if ($texte_extra == "") {
			$texte_extra = "$texte.*";
		}
	}

	if ($action != "delete" && $action != "copy") {
		$numero = toSQL($numero);
		$texte = toSQL($texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE compta.t_comptes SET 
					texte = '$texte', 
					banque = '$banque', 
					solde_initial = $solde_initial, 
					inter_site = '$inter_site',
					collectif = '$collectif',
					type_compta_extra_id = $type_compta_extra_id, 
					numero_extra = '$numero_extra', 
					texte_extra = '$texte_extra'
					WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
            
            // Mise à jour du compte banque sur les comptes signés
            $sqlcmd = " UPDATE compta.t_comptes SET
                            banque = '$banque'
                        WHERE 1=1
                            AND compte_general_id = $oid
                            AND banque != '$banque';
                      ";
			$result = $database->exec($sqlcmd);
			
			$sqlcmd = "UPDATE compta.t_comptes_c SET ";
			$sqlcmd = $sqlcmd . "texte = '$texte', ";
			$sqlcmd = $sqlcmd . "banque = '$banque' ";
			$sqlcmd = $sqlcmd . "WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
				

	}

	if ($action == "copy") {
	
		

	}


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

}

function setRecords_ecritures_ajustement() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$mois_comptable = utf8_decode($recordNode["mois_comptable"]);
	$clinique_honoraire = utf8_decode($recordNode["clinique_honoraire"]);
	$journal_code = utf8_decode($recordNode["journal_code"]);
	$section_code = utf8_decode($recordNode["section_code"]);
	$compte_numero = utf8_decode($recordNode["compte_numero"]);
	$texte = utf8_decode($recordNode["texte"]);
	$report_automatique_mois_suivant = utf8_decode($recordNode["report_automatique_mois_suivant"]);
	$est_balance = utf8_decode($recordNode["est_balance"]);
	$est_import_data = utf8_decode($recordNode["est_import_data"]);
	$import_data = utf8_decode($recordNode["import_data"]);
	
	$montant = str_replace("," , "." , $recordNode["montant_debit"]);
	$montant_debit = floatval($montant);
	$montant = str_replace("," , "." , $recordNode["montant_credit"]);
	$montant_credit = floatval($montant);
	
	$mois_ventile[1] = utf8_decode($recordNode["mois_ventile_1"]);
	$mois_ventile[2] = utf8_decode($recordNode["mois_ventile_2"]);
	$mois_ventile[3] = utf8_decode($recordNode["mois_ventile_3"]);
	$mois_ventile[4] = utf8_decode($recordNode["mois_ventile_4"]);
	$mois_ventile[5] = utf8_decode($recordNode["mois_ventile_5"]);
	$mois_ventile[6] = utf8_decode($recordNode["mois_ventile_6"]);
	$mois_ventile[7] = utf8_decode($recordNode["mois_ventile_7"]);
	$mois_ventile[8] = utf8_decode($recordNode["mois_ventile_8"]);
	$mois_ventile[9] = utf8_decode($recordNode["mois_ventile_9"]);
	$mois_ventile[10]  = utf8_decode($recordNode["mois_ventile_10"]);
	$mois_ventile[11]  = utf8_decode($recordNode["mois_ventile_11"]);
	$mois_ventile[12]  = utf8_decode($recordNode["mois_ventile_12"]);
	
	$montant_ventile[1] = floatval(str_replace("," , "." , $recordNode["montant_ventile_1"]));
	$montant_ventile[2] = floatval(str_replace("," , "." , $recordNode["montant_ventile_2"]));
	$montant_ventile[3] = floatval(str_replace("," , "." , $recordNode["montant_ventile_3"]));
	$montant_ventile[4] = floatval(str_replace("," , "." , $recordNode["montant_ventile_4"]));
	$montant_ventile[5] = floatval(str_replace("," , "." , $recordNode["montant_ventile_5"]));
	$montant_ventile[6] = floatval(str_replace("," , "." , $recordNode["montant_ventile_6"]));
	$montant_ventile[7] = floatval(str_replace("," , "." , $recordNode["montant_ventile_7"]));
	$montant_ventile[8] = floatval(str_replace("," , "." , $recordNode["montant_ventile_8"]));
	$montant_ventile[9] = floatval(str_replace("," , "." , $recordNode["montant_ventile_9"]));
	$montant_ventile[10] = floatval(str_replace("," , "." , $recordNode["montant_ventile_10"]));
	$montant_ventile[11] = floatval(str_replace("," , "." , $recordNode["montant_ventile_11"]));
	$montant_ventile[12] = floatval(str_replace("," , "." , $recordNode["montant_ventile_12"]));
	
	

	
	if ($clinique_honoraire != "H") {
		$clinique_honoraire != "C";
	}
	$journal_code = toSQL($journal_code);
	$section_code = toSQL($section_code);
	$compte_numero = toSQL($compte_numero);
	$texte = toSQL($texte);
	$import_data = toSQL($import_data);
	
	
	$date_ecriture = date("Y-m-d");

	if ($action != "delete" && $action != "copy") {
		
		$ecriture_maitre_id = 0;
		
		// creation ou mise à  jour de l'écriture
		if ($oid == -1) {
			
			$sqlcmd = "INSERT INTO compta.t_ecritures_ajustement( 
				date_ecriture, 
				mois_comptable, 
				clinique_honoraire, 
				journal_code, 
				section_analytique_code, 
				compte_numero, 
				texte, 
				report_automatique_mois_suivant,
				est_balance,
				est_import_data,
				import_data,
				montant_debit, 
				montant_credit,
				ecriture_maitre_id) 
			VALUES(
				'$date_ecriture', 
				$mois_comptable, 
				'$clinique_honoraire', 
				'$journal_code', 
				'$section_code', 
				'$compte_numero', 
				'$texte', 
				'$report_automatique_mois_suivant',
				'$est_balance',
				'$est_import_data',
				'$import_data',
				$montant_debit, 
				$montant_credit,
				$ecriture_maitre_id
			) ";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
					
			}
			else {
				$result  =  $database->exec("SELECT max(oid) FROM compta.t_ecritures_ajustement ");
				if ($result != false) {
					$record = $database->nextRecordInto();
					if ($record != FALSE) {
						$ecriture_maitre_id = $record[0];
					}
				}
			}
		}
		else {
			$sqlcmd = "UPDATE compta.t_ecritures_ajustement SET 
						date_ecriture='$date_ecriture',  
						mois_comptable=$mois_comptable,  
						clinique_honoraire='$clinique_honoraire',  
						journal_code='$journal_code', 
						section_analytique_code='$section_code', 
						compte_numero='$compte_numero',  
						texte='$texte', 
						report_automatique_mois_suivant='$report_automatique_mois_suivant',
						est_balance='$est_balance',
						est_import_data='$est_import_data',  
						import_data='$import_data',  
						montant_debit=$montant_debit,  
						montant_credit=$montant_credit,
						ecriture_maitre_id=$ecriture_maitre_id
					WHERE oid = $oid";
				
			$result  =  $database->exec($sqlcmd);	
			
			$ecriture_maitre_id = $oid;

		}
		
		// Ecritures ventilees
		$sqlcmd = "DELETE FROM compta.t_ecritures_ajustement WHERE ecriture_maitre_id = $ecriture_maitre_id;";
		
		for ($c = 1; $c <= 12; $c++) {
		if ($montant_ventile[$c]<> 0 ) {
			if ($montant_debit <> 0 ) {
				$montant_ventile_debit = 0;
				$montant_ventile_credit = $montant_ventile[$c];
			}
			else {
				$montant_ventile_debit = $montant_ventile[$c];
				$montant_ventile_credit = 0;
			}
			$mois_comptable_ventile = $mois_ventile[$c];
			$sqlcmd = $sqlcmd .
				"INSERT INTO compta.t_ecritures_ajustement( 
					date_ecriture, 
					mois_comptable, 
					clinique_honoraire, 
					journal_code, 
					section_analytique_code,
					compte_numero, 
					texte, 
					montant_debit, 
					montant_credit,
					ecriture_maitre_id) 
				VALUES(
					'$date_ecriture', 
					$mois_comptable_ventile, 
					'$clinique_honoraire', 
					'$journal_code', 
					'$section_code', 
					'$compte_numero', 
					'$texte', 
					$montant_ventile_debit, 
					$montant_ventile_credit,
					$ecriture_maitre_id
				); ";
		}
		}
		
		
		
		$result  =  $database->exec($sqlcmd);	
				

	}

	if ($action == "copy") {
	
		$sqlcmd = "INSERT INTO compta.t_ecritures_ajustement( ";
		$sqlcmd = $sqlcmd . "date_ecriture, mois_comptable, clinique_honoraire, journal_code, compte_numero, texte, montant_debit, montant_credit) ";
		$sqlcmd = $sqlcmd . "SELECT '$date_ecriture', mois_comptable, clinique_honoraire, journal_code, compte_numero, 'Copie de ' || texte, montant_debit, montant_credit ";
		$sqlcmd = $sqlcmd . "FROM compta.t_ecritures_ajustement ";
		$sqlcmd = $sqlcmd . "WHERE oid = $oid";
		
		$result  =  $database->exec($sqlcmd);	

	}


	if ($action == "delete") {
		$sqlcmd = "DELETE FROM compta.t_ecritures_ajustement WHERE oid = $oid";
		$result  =  $database->exec($sqlcmd);
	}
	
	// Menage des liens usages
	$sqlcmd = "DELETE FROM compta.t_ecritures_ajustement
		WHERE COALESCE(ecriture_maitre_id,0) <> 0 AND
		ecriture_maitre_id NOT IN (SELECT oid FROM compta.t_ecritures_ajustement) ";
	$result  =  $database->exec($sqlcmd);
	
	// Mise à jour historique
	$sqlcmd = "SELECT compta.cti_gen_ajustements(0) ";
	$result  =  $database->exec($sqlcmd);
	
}


function setRecords_comptes_extra() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$numero = utf8_decode($recordNode["numero"]);
	$texte = utf8_decode($recordNode["texte"]);
	$numero_original = utf8_decode($recordNode["numero_original"]);
	$clinique_honoraire = utf8_decode($recordNode["clinique_honoraire"]);
	$banque = utf8_decode($recordNode["banque"]);
	$inter_site = utf8_decode($recordNode["inter_site"]);


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

	}

	if ($action == "copy") {
	
		

	}


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


}

function setRecords_partenaires() {

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


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

	}

	if ($action == "copy") {
	
		

	}


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

}



function setRecords_budget() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = $recordNode["oid"];
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$site_id = floatval($recordNode["site_id"]);
	$exercice_comptable = floatval($recordNode["exercice_comptable"]);
	$is_actif = utf8_decode($recordNode["is_actif"]);
	$import_data = utf8_decode($recordNode["import_data"]);


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		$import_data = toSQL($import_data);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
			$sqlcmd = "INSERT INTO compta.t_budget (
						code,
						texte,
						site_id,
						is_actif,
						exercice_comptable,
						import_data
					)
					VALUES( 
						'$code', 
						'$texte', 
						$site_id, 
						'$is_actif', 
						$exercice_comptable, 
						'$import_data' 
					)";
				$result  =  $database->exec($sqlcmd);
				if ($result == FALSE) {
					
				}
				else {
					$result  =  $database->exec("SELECT max(oid) FROM compta.t_budget WHERE code = '$code' ");
					if ($result != false) {
						$record = $database->nextRecordInto();
						if ($record != FALSE) {
							$oid = $record[0];
						}
					}
				}
		}
		else {
			
			$sqlcmd = "UPDATE compta.t_budget SET 
				code = '$code', 
				texte = '$texte', 
				site_id = $site_id, 
				is_actif = '$is_actif', 
				exercice_comptable = $exercice_comptable, 
				import_data = '$import_data' 
				WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
		
		$sqlcmd = "SELECT compta.cti_gen_budget($oid)";
		$result  =  $database->exec($sqlcmd);
				

	}

	if ($action == "copy") {
	
		

	}


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

function setRecords_divers() {

    global $database;
    global $action;
    global $recordNode;
    
    $oid = $recordNode["oid"];
    $code = utf8_decode($recordNode["code"]);
    $texte = utf8_decode($recordNode["texte"]);
    $valeur = floatval($recordNode["valeur"]);
    $valeur_date = utf8_decode($recordNode["valeur_date"]);
    $description = utf8_decode($recordNode["description"]);
    $show_info_module = utf8_decode($recordNode["show_info_module"]);

    /*

$sqlcmd = "
                SELECT
                    code,
                    texte,
                    valeur,
                    valeur_date,
                    description,
                    CASE WHEN show_info_module THEN 1 ELSE 0 END AS show_info_module
                FROM $schema.$tableName
                ORDER BY code

    */


    if ($action != "delete" && $action != "copy") {
        $code = toSQL($code);
        $texte = toSQL($texte);
        $import_data = toSQL($import_data);
        
        // creation ou mise à  jour de la liste
        $sqlcmd = "
            UPDATE compta.t_divers SET
                texte = '$texte',
                valeur = '$valeur',
                valeur_date = nullif('$valeur_date', '')::date,
                description = '$description',
                show_info_module = CASE WHEN $show_info_module = 1 THEN true ELSE false END
            WHERE code = '$code'";
        $result  =  $database->exec($sqlcmd);
        
        if ($code == "MOIS_DEBUT") {
            // Màj de base.p_calendrier_mois.
            $sqlcmd = "
                UPDATE base.p_calendrier_mois SET
                    exercice_comptable = to_char(date_debut - '$valeur month'::interval + '1 month'::interval, 'YYYY')::numeric,
                    exercice_mois_comptable_1 = (to_char(date_debut - '$valeur month'::interval + '1 month'::interval, 'YYYY')||ltrim(to_char($valeur, '00')))::numeric
                ;
            ";
            $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_compta')";
		$result = $database->exec($sqlcmd);
	}
	
	// Effacer cache nouvelle version
	cleanAllCache("iCTI_compta");


}

    
?>