<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  Sylvain FATOME
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/


// Vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

/****************************/
/* Gestion des traçabilités */
/****************************/

/**
 * Ajout traçabilité
 * $typrat =
 * 1 : Intervention
 * 2 : Client
 * 3 : Donneur d'ordre
 *
 * @param $typrat
 * @param $coderat
 * @param $codeta
 * @param $titre
 * @param $desc
 * @param bool $open
 */
function add_trace($typrat, $coderat, $codeta, $titre, $desc, $open = TRUE)
{
    global
    $db,
    $_SESSION;

    if ($_SESSION["niv_log"] == 9)
        $code_session = _("Administrateur");
    else
        $code_session = $_SESSION["code_log"];
    $datecourante = date("Y-m-d");
    $heurecourante = date("H:i:s");

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    $rech = "SELECT cons_nivtra FROM `" . TBCONS . "`";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row)
        extract($row);

    // On ne fait rien si pas de traçabilité
    if (intval($cons_nivtra) <> 0) {
        $rech = "INSERT INTO `" . TBTRA . "`";
        $rech .= " (tra_codeutil,tra_date,tra_heure,tra_codeta,tra_titre,tra_desc,tra_typrat,tra_coderat) VALUES";
        $rech .= " ('$code_session','$datecourante','$heurecourante','$codeta','" . $db->escape($titre) . "','" . $db->escape($desc) . "','$typrat','$coderat')";
        $db->query($rech);
    }

    if ($open == TRUE)
        $db->disconnect();
}

/**
 * @param $typ
 * @param $enr_avant
 * @param $enr_apres
 * @param $detailsaisi
 * @param bool $open
 * @return bool
 */
function analyse_trace($typ, $enr_avant, $enr_apres, $detailsaisi, $open = TRUE)
{
    global
    $db,
    $non_oui,
    $niv_cpta,
    $typ_client,
    $prev_client,
    $typ_fact;

    $trace_added = FALSE;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    $rech = "SELECT cons_nivtra FROM `" . TBCONS . "`";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row)
        extract($row);

    // On ne fait rien si pas de traçabilité
    if (intval($cons_nivtra) <> 0) {
        // Données traçabilité
        switch ($typ) {
            // Interventions
            case "int" :
                $titre_tra = _("Modification de l'intervention");

                // Si détail saisi, on ajoute le texte sur "modification"
                if (trim($detailsaisi) <> "") {
                    $desc_tra = trim($detailsaisi);
                    add_trace(1, $enr_avant["int_code"], "MOD", $titre_tra, $desc_tra, FALSE);
                    $trace_added = TRUE;
                }

                // Si mode Synthétique, aucun détail complémentaire
                if (intval($cons_nivtra) > 1) {
                    // On analyse les différences de saisie du formulaire
                    if ((is_array($enr_avant) == TRUE) AND (is_array($enr_apres) == TRUE)) {
                        $tab_diff = array_diff_assoc($enr_avant, $enr_apres);

                        $desc_tra = '';

                        foreach ($tab_diff as $key => $value) {
                            if (intval($cons_nivtra) == 3) {
                                $desc_tra = '';
                                $code_tra = '';
                            }
                            if (intval($cons_nivtra) == 2) {
                                if (trim($desc_tra) <> "")
                                    $desc_tra .= chr(13) . chr(10) . ">>";
                            }
                            switch ($key) {
                                case "int_codecat" :
                                    $desc_tra .= _("Changement de catégorie");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["int_codecat"] . ' '._("en").' ' . $enr_apres["int_codecat"];
                                    $code_tra = "MODCA";
                                    break;
                                case "int_codeext" :
                                    break;
                                case "int_codeman" :
                                    $desc_tra .= _("Changement de code manuel");

                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["int_codeman"] . ' '._("en").' ' . $enr_apres["int_codeman"];
                                    $code_tra = "MODCM";
                                    break;
                                case "int_codeocli" :
                                    break;
                                case "int_codeage" :
                                    break;
                                case "int_codedo" :
                                    break;
                                case "int_codecli" :
                                    break;
                                case "int_ddeur" :
                                    $desc_tra .= _("Changement du demandeur");
                                    $desc_tra .= chr(13) . chr(10);
                                    switch (intval($enr_avant["int_ddeur"])) {
                                        case -1 :
                                            $rech2 = "SELECT cli_civilite, cli_prenom, cli_nom FROM `" . TBCLI . "` WHERE `cli_code` = '" . $enr_avant["int_codecli"] . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_avant = $cli_civilite . " " . $cli_prenom . " " . $cli_nom;
                                            break;
                                        case 0 :
                                            $rech2 = "SELECT scli_rcivil, scli_rprenom, scli_rnom FROM `" . TBSCLI . "` WHERE `scli_id` = '" . intval($enr_avant["int_idsite"]) . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_avant = $scli_rcivil . " " . $scli_rprenom . " " . $scli_rnom;
                                            break;
                                        default :
                                            $rech2 = "SELECT ccli_civilite, ccli_prenom, ccli_nom FROM `" . TBCCLI . "` WHERE `ccli_id` = '" . intval($enr_avant["int_ddeur"]) . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_avant = $ccli_civilite . " " . $ccli_prenom . " " . $ccli_nom;
                                            break;
                                    }
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_apres = chr(13) . chr(10);
                                    switch (intval($enr_apres["int_ddeur"])) {
                                        case -1 :
                                            $rech2 = "SELECT cli_civilite, cli_prenom, cli_nom FROM `" . TBCLI . "` WHERE `cli_code` = '" . $enr_avant["int_codecli"] . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_apres .= $cli_civilite . " " . $cli_prenom . " " . $cli_nom;
                                            break;
                                        case 0 :
                                            $rech2 = "SELECT scli_rcivil, scli_rprenom, scli_rnom FROM `" . TBSCLI . "` WHERE `scli_id` = '" . intval($enr_apres["int_idsite"]) . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_apres .= $scli_rcivil . " " . $scli_rprenom . " " . $scli_rnom;
                                            break;
                                        default :
                                            $rech2 = "SELECT ccli_civilite, ccli_prenom, ccli_nom FROM `" . TBCCLI . "` WHERE `ccli_id` = '" . intval($enr_apres["int_ddeur"]) . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_apres .= $ccli_civilite . " " . $ccli_prenom . " " . $ccli_nom;
                                            break;
                                    }
                                    $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                    $code_tra = "MODDE";
                                    break;
                                case "int_codecocl" :
                                    if (trim($enr_apres["int_codecocl"]) == "") {
                                        $desc_tra .= _("Contrat de maintenance").' ' . $enr_avant["int_codecocl"] . ' ' ._("supprimé");
                                        $code_tra = "SUPC";
                                    } else {
                                        $desc_tra .= _("Contrat de maintenance").' ' . $enr_avant["int_codecocl"] . ' ' ._("supprimé");
                                        $desc_tra .= chr(13) . chr(10);
                                        $desc_tra .= _("Contrat de maintenance").' ' . $enr_apres["int_codecocl"] . ' ' ._("ajouté");
                                        $code_tra = "MODC";
                                    }
                                    break;
                                case "int_codeorig" :
                                    break;
                                case "int_codestatut" :
                                    $desc_tra .= _("Changement de statut");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= _("De"). ' ';
                                    $rech2 = "SELECT statut_desc FROM `" . TBSTA . "` WHERE `statut_code` = '" . $enr_avant["int_codestatut"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $desc_tra .= $satut_desc;
                                    $desc_tra .= ' ' ._("en").' ';
                                    $rech2 = "SELECT statut_desc FROM `" . TBSTA . "` WHERE `statut_code` = '" . $enr_apres["int_codestatut"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $code_tra = "MODST";
                                    break;
                                case "int_codelieu" :
                                    $desc_tra .= _("Changement de lieu d'intervention");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= _("De"). ' ';
                                    $rech2 = "SELECT lieu_desc FROM `" . TBLIEU . "` WHERE `lieu_code` = '" . $enr_avant["int_codelieu"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $desc_tra .= $lieu_desc;
                                    $desc_tra .= ' ' ._("en").' ';
                                    $rech2 = "SELECT lieu_desc FROM `" . TBLIEU . "` WHERE `lieu_code` = '" . $enr_apres["int_codelieu"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $desc_tra .= $lieu_desc;
                                    $code_tra = "MODLI";
                                    break;
                                case "int_urg" :
                                    $desc_tra .= _("Changement de niveau d'urgence");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= _("De").' ';
                                    $rech2 = "SELECT urg_des FROM `" . TBURG . "` WHERE `urg_niveau` = '" . $enr_avant["int_urg"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $desc_tra .= $urg_des;
                                    $desc_tra .= ' ' ._("en").' ';
                                    $rech2 = "SELECT urg_des FROM `" . TBURG . "` WHERE `urg_niveau` = '" . $enr_apres["int_urg"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $desc_tra .= $urg_des;
                                    $code_tra = "MODUR";
                                    break;
                                case "int_datefinp" :
                                    $desc_tra .= _("Changement de fin prévue le"). ' ' . datetolocal($enr_apres["int_datefinp"]);
                                    $code_tra = "INTCL";
                                    break;
                                case "int_planifie" :
                                    break;
                                case "int_publique" :
                                    $desc_tra .= _("Changement de visibilité");
                                    $desc_tra .= chr(13) . chr(10);
                                    if (intval($enr_avant["int_publique"]) == 0)
                                        $desc_tra .= _("Privée");
                                    else
                                        $desc_tra .= _("Publique");
                                    $desc_tra .= ' ' ._("en").' ';
                                    if (intval($enr_apres["int_publique"]) == 0)
                                        $desc_tra .= _("Privée");
                                    else
                                        $desc_tra .= _("Publique");
                                    $code_tra = "MODCM";
                                    break;
                                case "int_mat" :
                                    $desc_tra .= _("Changement de matériel");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["int_mat"];
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= ' '._("en").' ';
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_apres["int_mat"];
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $code_tra = "MODMA";
                                    break;
                                case "int_pbm" :
                                    $desc_tra .= _("Changement de problème déclaré");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["int_pbm"];
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= ' '._("en").' ';
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_apres["int_pbm"];
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $code_tra = "MODPB";
                                    break;
                                case "int_gar" :
                                    $desc_tra .= _("Changement de garantie");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $non_oui[$enr_avant["int_gar"]] . ' '._("en").' ' . $non_oui[$enr_apres["int_gar"]];
                                    $code_tra = "MODGA";
                                    break;
                                case "int_fgar" :
                                    $desc_tra .= _("Changement de référence de facture de garantie");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["int_fgar"] . ' ' ._("en").' ' . $enr_apres["int_fgar"];
                                    $code_tra = "MODFG";
                                    break;
                                case "int_acpte" :
                                    $desc_tra .= _("Changement du montant d'acompte");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["int_acpte"] . ' ' ._("en").' ' . $enr_apres["int_acpte"];
                                    $code_tra = "MODAC";
                                    break;
                                case "int_codetec" :
                                    $desc_tra .= _("Changement de technicien responsable");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= _("De"). ' ';
                                    $rech2 = "SELECT tec_civilite, tec_prenom, tec_nom FROM `" . TBTEC . "` WHERE `tec_code` = '" . $enr_avant["int_codetec"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $desc_tra .= $tec_civilite . " " . $tec_prenom . " " . $tec_nom;
                                    $desc_tra .= ' ' ._("en").' ';
                                    $rech2 = "SELECT tec_civilite, tec_prenom, tec_nom FROM `" . TBTEC . "` WHERE `tec_code` = '" . $enr_apres["int_codetec"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $desc_tra .= $tec_civilite . " " . $tec_prenom . " " . $tec_nom;
                                    $code_tra = "MODTR";
                                    break;
                                case "int_durees" :
                                    break;
                                case "int_fait" :
                                    $desc_tra .= _("Changement de travail effectué");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["int_fait"];
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= ' '._("en").' ';
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_apres["int_fait"];
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= "-----";
                                    $code_tra = "MODTE";
                                    break;
                                case "int_dateprev" :
                                    $desc_tra .= _("Client prévenu le"). ' ' . datetolocal($enr_apres["int_dateprev"]);
                                    $code_tra = "MODCP";
                                    break;
                                case "int_codereg" :
                                    break;
                                case "int_datefin" :
                                    $desc_tra = _("Intervention close le"). ' ' . datetolocal($enr_apres["int_datefin"]);
                                    $code_tra = "INTCL";
                                    break;
                                case "int_pces" :
                                    break;
                                case "int_idsite" :
                                    $desc_tra .= _("Changement de site d'intervention");
                                    $desc_tra .= chr(13) . chr(10);
                                    switch (intval($enr_avant["int_idsite"])) {
                                        case 0 :
                                            $txt_avant = _("Site principal");
                                            break;
                                        default :
                                            $rech2 = "SELECT scli_ste, scli_adr1, scli_adr2, scli_ville FROM `" . TBSCLI . "` WHERE `scli_id` = '" . intval($enr_avant["int_idsite"]) . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_avant = $scli_ste;
                                            $txt_avant .= chr(13) . chr(10);
                                            $txt_avant .= $scli_adr1;
                                            $txt_avant .= chr(13) . chr(10);
                                            if (trim($scli_adr2) <> "") {
                                                $txt_avant .= $scli_adr2;
                                                $txt_avant .= chr(13) . chr(10);
                                            }
                                            $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($scli_ville) . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_avant .= $villes_cp . " ";

                                            $ville = str_majuscules($villes_nom);
                                            // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                            if (stristr($villes_nom, "LYON"))
                                                $ville = "LYON";
                                            if (stristr($villes_nom, "MARSEILLE"))
                                                $ville = "MARSEILLE";
                                            if (stristr($villes_nom, "PARIS"))
                                                $ville = "PARIS";

                                            $txt_avant .= $ville;
                                            break;
                                    }
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_apres = chr(13) . chr(10);
                                    switch (intval($enr_apres["int_idsite"])) {
                                        case 0 :
                                            $txt_apres .= _("Site principal");
                                            break;
                                        default :
                                            $rech2 = "SELECT scli_ste, scli_adr1, scli_adr2, scli_ville FROM `" . TBSCLI . "` WHERE `scli_id` = '" . intval($enr_apres["int_idsite"]) . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_apres = $scli_ste;
                                            $txt_apres .= chr(13) . chr(10);
                                            $txt_apres .= $scli_adr1;
                                            $txt_apres .= chr(13) . chr(10);
                                            if (trim($scli_adr2) <> "") {
                                                $txt_apres .= $scli_adr2;
                                                $txt_apres .= chr(13) . chr(10);
                                            }
                                            $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($scli_ville) . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2)
                                            {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            $txt_apres .= $villes_cp . " ";

                                            $ville = str_majuscules($villes_nom);
                                            // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                            if (stristr($villes_nom, "LYON"))
                                                $ville = "LYON";
                                            if (stristr($villes_nom, "MARSEILLE"))
                                                $ville = "MARSEILLE";
                                            if (stristr($villes_nom, "PARIS"))
                                                $ville = "PARIS";

                                            $txt_apres .= $ville;
                                            break;
                                    }
                                    $desc_tra .= $txt_avant . ' ' .("en").' ' . $txt_apres;
                                    $code_tra = "MODSI";
                                    break;
                                case "int_codeages" :
                                    break;
                                case "int_fact" :
                                    break;
                                case "int_forfait" :
                                    $desc_tra .= _("Changement du forfait");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["int_forfait"] . ' ' ._("en").' ' . $enr_apres["int_forfait"];
                                    $code_tra = "MODFO";
                                    break;
                                case "int_codeadev" :
                                    break;
                            }
                            if ((intval($cons_nivtra) == 3) and ($code_tra <> '')) {
                                add_trace(1, $enr_avant["int_code"], $code_tra, $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                            }
                        }

                        if (intval($cons_nivtra) == 2) {
                            add_trace(1, $enr_avant["int_code"], "MOD", $titre_tra, $desc_tra, FALSE);
                            $trace_added = TRUE;
                        }
                    }
                }
                break;
            // Clients
            case "cli" :
                $titre_tra = _("Modification du client");

                // Si détail saisi, on ajoute le texte sur "modification"
                if (trim($detailsaisi) <> "") {
                    $desc_tra = trim($detailsaisi);
                    add_trace(1, $enr_avant["cli_code"], "MOD", $titre_tra, $desc_tra, FALSE);
                    $trace_added = TRUE;
                }

                // Si mode Synthétique, aucun détail complémentaire
                if (intval($cons_nivtra) > 1) {
                    // On analyse les différences de saisie du formulaire
                    if ((is_array($enr_avant) == TRUE) AND (is_array($enr_apres) == TRUE)) {
                        $tab_diff = array_diff_assoc($enr_avant, $enr_apres);

                        $desc_tra = '';
                        foreach ($tab_diff as $key => $value) {
                            if (intval($cons_nivtra) == 3) {
                                $desc_tra = '';
                                $code_tra = '';
                            }
                            if (intval($cons_nivtra) == 2) {
                                if (trim($desc_tra) <> "")
                                    $desc_tra .= chr(13) . chr(10) . ">>";
                            }

                            switch ($key) {
                                case "cli_cat" :
                                    $desc_tra .= _("Changement de catégorie");
                                    $desc_tra .= chr(13) . chr(10);
                                    $rech2 = "SELECT cat_desc FROM `" . TBCCAT . "` WHERE `cat_code` = '" . $enr_avant["cli_cat"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_avant = $cat_desc;
                                    $rech2 = "SELECT cat_desc FROM `" . TBCCAT . "` WHERE `cat_code` = '" . $enr_apres["cli_cat"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_apres = $cat_desc;
                                    $desc_tra .= $txt_avant . ' '.("en").' ' . $txt_apres;
                                    $code_tra = "MODCA";
                                    break;
                                case "cli_prop" :
                                    $desc_tra .= _("Changement de propriétaire");
                                    $desc_tra .= chr(13) . chr(10);
                                    $rech2 = "SELECT age_nom FROM `" . TBAGE . "` WHERE `age_code` = '" . $enr_avant["cli_prop"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_avant = $age_nom;
                                    $rech2 = "SELECT age_nom FROM `" . TBCCAT . "` WHERE `age_code` = '" . $enr_apres["cli_prop"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_apres = $cat_desc;
                                    $desc_tra .= $txt_avant . ' ' .("en").' ' . $txt_apres;
                                    $code_tra = "MODEPR";
                                    break;
                                case "cli_type" :
                                    $desc_tra .= _("Changement de type");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $typ_client[intval($enr_avant["cli_type"])] . ' ' .("en"). ' ' . $typ_client[intval($enr_apres["cli_type"])];
                                    $code_tra = "MODTY";
                                    break;
                                case "cli_rcs" :
                                case "cli_ape" :
                                case "cli_tvai" :
                                    $desc_tra .= _("Changement de données juridiques");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    $txt_avant .= _("Siret"). ' : ' . trim($enr_avant["cli_rcs"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("APE").'  : ' . trim($enr_avant["cli_ape"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("TVA").' : ' . trim($enr_avant["cli_tvai"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_apres = "";
                                    $txt_apres .= _("Siret").' : ' . trim($enr_apres["cli_rcs"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("APE").' : ' . trim($enr_apres["cli_ape"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("TVA").' : ' . trim($enr_apres["cli_tvai"]);
                                    $desc_tra .= $txt_avant . ' ' .("en").' ' . $txt_apres;
                                    $code_tra = "MODDJ";
                                    break;
                                case "cli_civilite" :
                                case "cli_prenom" :
                                case "cli_nom" :
                                case "cli_ste" :
                                case "cli_adr1" :
                                case "cli_adr2" :
                                case "cli_ville" :
                                case "cli_codepays" :
                                    $desc_tra .= _("Changement de coordonnées");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    if (trim($enr_avant["cli_ste"]) <> "") {
                                        $txt_avant .= $enr_avant["cli_ste"];
                                        $txt_avant .= chr(13) . chr(10);
                                    }
                                    $txt_avant .= $enr_avant["cli_adr1"];
                                    $txt_avant .= chr(13) . chr(10);
                                    if (trim($enr_avant["cli_adr2"]) <> "") {
                                        $txt_avant .= $enr_avant["cli_adr2"];
                                        $txt_avant .= chr(13) . chr(10);
                                    }
                                    $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_avant["cli_ville"]) . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_avant .= $villes_cp . " ";

                                    $ville = str_majuscules($villes_nom);
                                    // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                    if (stristr($villes_nom, "LYON"))
                                        $ville = "LYON";
                                    if (stristr($villes_nom, "MARSEILLE"))
                                        $ville = "MARSEILLE";
                                    if (stristr($villes_nom, "PARIS"))
                                        $ville = "PARIS";
                                    $txt_avant .= $ville;
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= $enr_avant["cli_codepays"];
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= $enr_avant["cli_civilite"] . " " . $enr_avant["cli_prenom"] . " " . $enr_avant["cli_nom"];
                                    $txt_avant .= chr(13) . chr(10);

                                    $txt_apres = chr(13) . chr(10);
                                    if (trim($enr_apres["cli_ste"]) <> "") {
                                        $txt_apres .= $enr_apres["cli_ste"];
                                        $txt_apres .= chr(13) . chr(10);
                                    }
                                    $txt_apres .= $enr_apres["cli_adr1"];
                                    $txt_apres .= chr(13) . chr(10);
                                    if (trim($enr_apres["cli_adr2"]) <> "") {
                                        $txt_apres .= $enr_apres["cli_adr2"];
                                        $txt_apres .= chr(13) . chr(10);
                                    }
                                    $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_apres["cli_ville"]) . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_apres .= $villes_cp . " ";

                                    $ville = str_majuscules($villes_nom);
                                    // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                    if (stristr($villes_nom, "LYON"))
                                        $ville = "LYON";
                                    if (stristr($villes_nom, "MARSEILLE"))
                                        $ville = "MARSEILLE";
                                    if (stristr($villes_nom, "PARIS"))
                                        $ville = "PARIS";
                                    $txt_apres .= $ville;
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= $enr_apres["cli_codepays"];
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= $enr_apres["cli_civilite"] . " " . $enr_apres["cli_prenom"] . " " . $enr_apres["cli_nom"];

                                    $desc_tra .= $txt_avant . ' ' ._("en").' ' . $txt_apres;
                                    $code_tra = "MODAD";
                                    break;
                                case "cli_codeadev" :
                                    $desc_tra .= _("Changement de devise");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["cli_codeadev"] . ' ' ._("en").' ' . $enr_apres["cli_codeadev"];
                                    $code_tra = "MODDV";
                                    break;
                                case "cli_telf" :
                                case "cli_fax" :
                                case "cli_telp" :
                                    $desc_tra .= _("Changement de coordonnées téléphoniques");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    $txt_avant .= _("Fixe").' : ' . trim($enr_avant["cli_telf"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("Fax").' : ' . trim($enr_avant["cli_fax"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("Mob").' : ' . trim($enr_avant["cli_telp"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_apres = "";
                                    $txt_apres .= _("Fixe").' : ' . trim($enr_apres["cli_telf"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("Fax").' : ' . trim($enr_apres["cli_fax"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("Mob").' : ' . trim($enr_apres["cli_telp"]);
                                    $desc_tra .= $txt_avant . ' ' ._("en").' ' . $txt_apres;
                                    $code_tra = "MODTE";
                                    break;
                                case "cli_email" :
                                case "cli_mess" :
                                    $desc_tra .= _("Changement de coordonnées mail/messagerie");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    $txt_avant .= _("Mail").' : ' . trim($enr_avant["cli_email"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("Mess").' : ' . trim($enr_avant["cli_mess"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_apres = "";
                                    $txt_apres .= _("Mail").' : ' . trim($enr_apres["cli_email"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("Mess").' : ' . trim($enr_apres["cli_mess"]);
                                    $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                    $code_tra = "MODEM";
                                    break;
                                case "cli_notaa" :
                                    $desc_tra .= _("Changement de notes administration");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["cli_notaa"] . chr(13) . chr(10) . ' '._("en").' ' . chr(13) . chr(10) . $enr_apres["cli_notaa"];
                                    $code_tra = "MODNA";
                                    break;
                                case "cli_notat" :
                                    $desc_tra .= _("Changement de notes techniciens");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["cli_notat"] . chr(13) . chr(10) . ' '._("en").' ' . chr(13) . chr(10) . $enr_apres["cli_notat"];
                                    $code_tra = "MODNT";
                                    break;
                                case "cli_ccpta" :
                                case "cli_ccptasp" :
                                    $desc_tra .= _("Changement de code comptable");
                                    if ($key == "cli_ccptasp")
                                        $desc_tra .= ' '.("site principal");
                                    $desc_tra .= chr(13) . chr(10);
                                    if ($key == "cli_ccpta")
                                        $desc_tra .= $enr_avant["cli_ccpta"] . ' '._("en").' ' . $enr_apres["cli_ccpta"];
                                    if ($key == "cli_ccptasp")
                                        $desc_tra .= $enr_avant["cli_ccptasp"] . ' '._("en").' ' . $enr_apres["cli_ccptasp"];
                                    $code_tra = "MODCC";
                                    break;
                                case "cli_cpta" :
                                    $desc_tra .= _("Changement de risque comptable");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $niv_cpta[intval($enr_avant["cli_cpta"])] . ' '._("en").' ' . $niv_cpta[intval($enr_apres["cli_cpta"])];
                                    $code_tra = "MODRC";
                                    break;
                                case "cli_prev" :
                                    $desc_tra .= _("Changement de moyen pour prévenir");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $prev_client[intval($enr_avant["cli_prev"])] . ' '._("en").' ' . $prev_client[intval($enr_apres["cli_prev"])];
                                    $code_tra = "MODPC";
                                    break;
                                case "cli_modfact" :
                                    $desc_tra .= _("Changement de mode de facturation");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $typ_fact[intval($enr_avant["cli_modfact"])] . ' '._("en").' ' . $typ_fact[intval($enr_apres["cli_modfact"])];
                                    $code_tra = "MODMF";
                                    break;
                            }
                            if ((intval($cons_nivtra) == 3) and ($code_tra <> '')) {
                                add_trace(1, $enr_avant["cli_code"], $code_tra, $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                            }
                        }

                        if (intval($cons_nivtra) == 2) {
                            add_trace(1, $enr_avant["cli_code"], "MOD", $titre_tra, $desc_tra, FALSE);
                            $trace_added = TRUE;
                        }
                    }
                }
                break;
            // Sites clients
            case "scli" :
                $titre_tra = _("Modification de site");
                $titre_tra .= " (" . $enr_avant["scli_ste"];
                $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_avant["scli_ville"]) . "'";
                $row2 = $db->get_row($rech2, ARRAY_A);
                if ($row2)
                {
                    $row2 = encode_str($row2);
                    extract($row2);
                }
                $titre_tra .= " " . $villes_cp . " ";

                $ville = str_majuscules($villes_nom);
                // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                if (stristr($villes_nom, "LYON"))
                    $ville = "LYON";
                if (stristr($villes_nom, "MARSEILLE"))
                    $ville = "MARSEILLE";
                if (stristr($villes_nom, "PARIS"))
                    $ville = "PARIS";
                $titre_tra .= " " . $ville;
                $titre_tra .= " " . $enr_avant["scli_codepays"];
                $titre_tra .= ")";

                // Si détail saisi, on ajoute le texte sur "modification"
                if (trim($detailsaisi) <> "") {
                    $desc_tra = trim($detailsaisi);
                    add_trace(1, $enr_avant["scli_codecli"], "MOD", $titre_tra, $desc_tra, FALSE);
                    $trace_added = TRUE;
                }

                // Si mode Synthétique, aucun détail complémentaire
                if (intval($cons_nivtra) > 1) {
                    // On analyse les différences de saisie du formulaire
                    if ((is_array($enr_avant) == TRUE) AND (is_array($enr_apres) == TRUE)) {
                        $tab_diff = array_diff_assoc($enr_avant, $enr_apres);
                        $desc_tra = '';
                        foreach ($tab_diff as $key => $value) {
                            if (intval($cons_nivtra) == 3) {
                                $desc_tra = '';
                                $code_tra = '';
                            }
                            if (intval($cons_nivtra) == 2) {
                                if (trim($desc_tra) <> "")
                                    $desc_tra .= chr(13) . chr(10) . ">>";
                            }

                            switch ($key) {
                                case "scli_prop" :
                                    $desc_tra .= _("Changement de propriétaire");
                                    $desc_tra .= chr(13) . chr(10);
                                    $rech2 = "SELECT age_nom FROM `" . TBAGE . "` WHERE `age_code` = '" . $enr_avant["scli_prop"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_avant = $age_nom;
                                    $rech2 = "SELECT age_nom FROM `" . TBAGE . "` WHERE `age_code` = '" . $enr_apres["scli_prop"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_apres = $age_nom;
                                    $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                    $code_tra = "MODPR";
                                    break;
                                case "scli_code" :
                                    $desc_tra .= _("Changement de code");
                                    $desc_tra .= $enr_avant["scli_code"] . ' '._("en").' ' . $enr_apres["scli_code"];
                                    $code_tra = "MODCD";
                                    break;
                                case "scli_rcs" :
                                case "scli_ape" :
                                case "scli_tvai" :
                                    $desc_tra .= _("Changement de données juridiques");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    $txt_avant .= _("Siret").' : ' . trim($enr_avant["scli_rcs"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("APE").' : ' . trim($enr_avant["scli_ape"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("TVA").' : ' . trim($enr_avant["scli_tvai"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_apres = "";
                                    $txt_apres .= _("Siret").' : ' . trim($enr_apres["scli_rcs"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("APE").' : ' . trim($enr_apres["scli_ape"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("TVA").' : ' . trim($enr_apres["scli_tvai"]);
                                    $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                    $code_tra = "MODDJ";
                                    break;
                                case "scli_rcivil" :
                                case "scli_rprenom" :
                                case "scli_rnom" :
                                case "scli_ste" :
                                case "scli_adr1" :
                                case "scli_adr2" :
                                case "scli_ville" :
                                case "scli_codepays" :
                                    $desc_tra .= _("Changement de coordonnées");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    $txt_avant .= $enr_avant["scli_ste"];
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= $enr_avant["scli_adr1"];
                                    $txt_avant .= chr(13) . chr(10);
                                    if (trim($enr_avant["scli_adr2"]) <> "") {
                                        $txt_avant .= $enr_avant["scli_adr2"];
                                        $txt_avant .= chr(13) . chr(10);
                                    }
                                    $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_avant["scli_ville"]) . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_avant .= $villes_cp . " ";

                                    $ville = str_majuscules($villes_nom);
                                    // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                    if (stristr($villes_nom, "LYON"))
                                        $ville = "LYON";
                                    if (stristr($villes_nom, "MARSEILLE"))
                                        $ville = "MARSEILLE";
                                    if (stristr($villes_nom, "PARIS"))
                                        $ville = "PARIS";
                                    $txt_avant .= $ville;
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= $enr_avant["scli_codepays"];
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= $enr_avant["scli_rcivil"] . " " . $enr_avant["scli_rprenom"] . " " . $enr_avant["scli_rnom"];
                                    $txt_avant .= chr(13) . chr(10);

                                    $txt_apres = chr(13) . chr(10);
                                    $txt_apres .= $enr_apres["scli_ste"];
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= $enr_apres["scli_adr1"];
                                    $txt_apres .= chr(13) . chr(10);
                                    if (trim($enr_apres["scli_adr2"]) <> "") {
                                        $txt_apres .= $enr_apres["scli_adr2"];
                                        $txt_apres .= chr(13) . chr(10);
                                    }
                                    $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_apres["scli_ville"]) . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_apres .= $villes_cp . " ";

                                    $ville = str_majuscules($villes_nom);
                                    // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                    if (stristr($villes_nom, "LYON"))
                                        $ville = "LYON";
                                    if (stristr($villes_nom, "MARSEILLE"))
                                        $ville = "MARSEILLE";
                                    if (stristr($villes_nom, "PARIS"))
                                        $ville = "PARIS";
                                    $txt_apres .= $ville;
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= $enr_apres["scli_codepays"];
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= $enr_apres["scli_rcivil"] . " " . $enr_apres["scli_rprenom"] . " " . $enr_apres["scli_rnom"];
                                    $txt_apres .= chr(13) . chr(10);

                                    $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                    $code_tra = "MODAD";
                                    break;
                                case "scli_rtelf" :
                                case "scli_rfax" :
                                case "scli_rtelp" :
                                    $desc_tra .= _("Changement de coordonnées téléphoniques");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    $txt_avant .= _("Fixe").' : ' . trim($enr_avant["scli_rtelf"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("Fax").' : ' . trim($enr_avant["scli_rfax"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_avant .= _("Mob").' : ' . trim($enr_avant["scli_rtelp"]);
                                    $txt_avant .= chr(13) . chr(10);
                                    $txt_apres = "";
                                    $txt_apres .= _("Fixe").' : ' . trim($enr_apres["scli_rtelf"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("Fax").' : ' . trim($enr_apres["scli_rfax"]);
                                    $txt_apres .= chr(13) . chr(10);
                                    $txt_apres .= _("Mob").' : ' . trim($enr_apres["scli_rtelp"]);
                                    $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                    $code_tra = "MODTE";
                                    break;
                                case "scli_email" :
                                    $desc_tra .= _("Changement de coordonnées mail/messagerie");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    $txt_avant .= trim($enr_avant["scli_email"]);
                                    $txt_apres = "";
                                    $txt_apres .= trim($enr_apres["scli_email"]);
                                    $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                    $code_tra = "MODEM";
                                    break;
                                case "scli_nota" :
                                    $desc_tra .= _("Changement de notes");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["scli_nota"] . chr(13) . chr(10) . ' '._("en").' ' . chr(13) . chr(10) . $enr_apres["scli_nota"];
                                    $code_tra = "MODNO";
                                    break;
                                case "scli_ccpta" :
                                    $desc_tra .= _("Changement de code comptable");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $enr_avant["scli_ccpta"] . ' '._("en").' ' . $enr_apres["scli_ccpta"];
                                    $code_tra = "MODDC";
                                    break;
                                case "scli_actif" :
                                    $desc_tra .= _("Changement de mode actif");
                                    $desc_tra .= chr(13) . chr(10);
                                    $desc_tra .= $non_oui[$enr_avant["scli_actif"]] . ' '._("en").' ' . $non_oui[$enr_apres["scli_actif"]];
                                    $code_tra = "MODMC";
                                    break;
                                case "scli_rcodefonc" :
                                    $desc_tra .= _("Changement de fonction contact");
                                    $desc_tra .= chr(13) . chr(10);
                                    $txt_avant = "";
                                    $rech2 = "SELECT fonc_desc FROM `" . TBFONC . "` WHERE `fonc_code` = '" . $enr_avant["scli_rcodefonc"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_avant .= $fonc_desc;
                                    $txt_avant .= chr(13) . chr(10);

                                    $txt_apres = "";
                                    $rech2 = "SELECT fonc_desc FROM `" . TBFONC . "` WHERE `fonc_code` = '" . $enr_apres["scli_rcodefonc"] . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2)
                                    {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_apres .= $fonc_desc;

                                    $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                    $code_tra = "MODFO";
                                    break;
                            }
                            if ((intval($cons_nivtra) == 3) and ($code_tra <> '')) {
                                add_trace(1, $enr_avant["scli_codecli"], $code_tra, $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                            }
                        }

                        if (intval($cons_nivtra) == 2) {
                            add_trace(1, $enr_avant["scli_codecli"], "MOD", $titre_tra, $desc_tra, FALSE);
                            $trace_added = TRUE;
                        }
                    }
                }
                break;
            // Contacts Clients
            case "ccli" :
                $titre_tra = _("Modification de contact");
                $titre_tra .= " (" . $enr_avant["ccli_civilite"] . " " . $enr_avant["ccli_prenom"] . " " . $enr_avant["ccli_nom"] . ")";

                // On analyse les différences de saisie du formulaire
                if ((is_array($enr_avant) == TRUE) AND (is_array($enr_apres) == TRUE)) {
                    $tab_diff = array_diff_assoc($enr_avant, $enr_apres);
                    foreach ($tab_diff as $key => $value) {
                        switch ($key) {
                            case "ccli_codefonc" :
                                $desc_tra = _("Changement de fonction contact");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $rech2 = "SELECT fonc_desc FROM `" . TBFONC . "` WHERE `fonc_code` = '" . $enr_avant["ccli_codefonc"] . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_avant .= $fonc_desc;
                                $txt_avant .= chr(13) . chr(10);

                                $txt_apres = "";
                                $rech2 = "SELECT fonc_desc FROM `" . TBFONC . "` WHERE `fonc_code` = '" . $enr_apres["ccli_codefonc"] . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_apres .= $fonc_desc;

                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(2, $enr_avant["ccli_codecli"], "MODFO", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "ccli_civilite" :
                            case "ccli_prenom" :
                            case "ccli_nom" :
                            case "ccli_adr1" :
                            case "ccli_adr2" :
                            case "ccli_ville" :
                            case "ccli_codepays" :
                                $desc_tra = _("Changement de coordonnées");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= $enr_avant["ccli_civilite"] . " " . $enr_avant["ccli_prenom"] . " " . $enr_avant["ccli_nom"];
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_avant["ccli_adr1"];
                                $txt_avant .= chr(13) . chr(10);
                                if (trim($enr_avant["ccli_adr2"]) <> "") {
                                    $txt_avant .= $enr_avant["ccli_adr2"];
                                    $txt_avant .= chr(13) . chr(10);
                                }
                                $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_avant["ccli_ville"]) . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_avant .= $villes_cp . " ";

                                $ville = str_majuscules($villes_nom);
                                // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                if (stristr($villes_nom, "LYON"))
                                    $ville = "LYON";
                                if (stristr($villes_nom, "MARSEILLE"))
                                    $ville = "MARSEILLE";
                                if (stristr($villes_nom, "PARIS"))
                                    $ville = "PARIS";
                                $txt_avant .= $ville;
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_avant["ccli_codepays"];
                                $txt_avant .= chr(13) . chr(10);

                                $txt_apres = chr(13) . chr(10);
                                $txt_avant .= $enr_apres["ccli_civilite"] . " " . $enr_apres["ccli_prenom"] . " " . $enr_apres["ccli_nom"];
                                $txt_apres .= $enr_apres["ccli_adr1"];
                                $txt_apres .= chr(13) . chr(10);
                                if (trim($enr_apres["ccli_adr2"]) <> "") {
                                    $txt_apres .= $enr_apres["ccli_adr2"];
                                    $txt_apres .= chr(13) . chr(10);
                                }
                                $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_apres["ccli_ville"]) . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_apres .= $villes_cp . " ";

                                $ville = str_majuscules($villes_nom);
                                // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                if (stristr($villes_nom, "LYON"))
                                    $ville = "LYON";
                                if (stristr($villes_nom, "MARSEILLE"))
                                    $ville = "MARSEILLE";
                                if (stristr($villes_nom, "PARIS"))
                                    $ville = "PARIS";
                                $txt_apres .= $ville;
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_apres["ccli_codepays"];

                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(2, $enr_avant["ccli_codecli"], "MODAD", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "ccli_telf" :
                            case "ccli_fax" :
                            case "ccli_telp" :
                                $desc_tra = _("Changement de coordonnées téléphoniques");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= _("Fixe").' : ' . trim($enr_avant["ccli_telf"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Fax").' : ' . trim($enr_avant["ccli_fax"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Mob").' : ' . trim($enr_avant["ccli_telp"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_apres = "";
                                $txt_apres .= _("Fixe").' : ' . trim($enr_apres["ccli_telf"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Fax").' : ' . trim($enr_apres["ccli_fax"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Mob").' : ' . trim($enr_apres["ccli_telp"]);
                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(2, $enr_avant["ccli_codecli"], "MODTE", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "ccli_email" :
                            case "ccli_mess" :
                                $desc_tra = _("Changement de coordonnées mail/messagerie");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= _("Mail").' : ' . trim($enr_avant["ccli_email"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Mess").' : ' . trim($enr_avant["ccli_mess"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_apres = "";
                                $txt_apres .= _("Mail").' : ' . trim($enr_apres["ccli_email"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Mess").' : ' . trim($enr_apres["ccli_mess"]);
                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(2, $enr_avant["ccli_codecli"], "MODEM", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "ccli_nota" :
                                $desc_tra = _("Changement de notes");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= $enr_avant["ccli_nota"] . chr(13) . chr(10) . ' '._("en").' ' . chr(13) . chr(10) . $enr_apres["ccli_nota"];
                                add_trace(2, $enr_avant["ccli_codecli"], "MODNO", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "ccli_maild" :
                            case "ccli_mailc" :
                            case "ccli_mailt" :
                                $desc_tra = _("Changement de type de message");
                                $desc_tra .= chr(13) . chr(10);
                                if ($key == "ccli_maild")
                                    $desc_tra .= _("Mails direction").' : ' . $non_oui[intval($enr_avant["ccli_maild"])] . ' '._("en").' ' . $non_oui[intval($enr_apres["ccli_maild"])];
                                if ($key == "ccli_mailc")
                                    $desc_tra .= _("Mails comptabilité").' : ' . $non_oui[intval($enr_avant["ccli_mailc"])] . ' '._("en").' ' . $non_oui[intval($enr_apres["ccli_mailc"])];
                                if ($key == "ccli_mailt")
                                    $desc_tra .= _("Mails techniques").' : ' . $non_oui[intval($enr_avant["ccli_mailt"])] . ' '._("en").' ' . $non_oui[intval($enr_apres["ccli_mailt"])];
                                add_trace(2, $enr_avant["ccli_codecli"], "MODTM", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                        }
                    }
                }
                break;
            // Donneurs d'ordres
            case "do" :
                $titre_tra = _("Modification du donneur d'ordre");

                // On analyse les différences de saisie du formulaire
                if ((is_array($enr_avant) == TRUE) AND (is_array($enr_apres) == TRUE)) {
                    $tab_diff = array_diff_assoc($enr_avant, $enr_apres);
                    foreach ($tab_diff as $key => $value) {
                        switch ($key) {
                            case "do_rcs" :
                            case "do_ape" :
                            case "do_tvai" :
                            case "do_tcom" :
                                $desc_tra = _("Changement de données juridiques");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= _("Siret").' : ' . trim($enr_avant["do_rcs"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("APE").' : ' . trim($enr_avant["do_ape"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("TVA").' : ' . trim($enr_avant["do_tvai"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= "Trib. Com. : " . trim($enr_avant["do_tcom"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_apres = "";
                                $txt_apres .= _("Siret").' : ' . trim($enr_apres["do_rcs"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("APE").' : ' . trim($enr_apres["do_ape"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("TVA").' : ' . trim($enr_apres["do_tvai"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= "Trib. Com. : " . trim($enr_apres["do_tcom"]);
                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["do_code"], "MODDJ", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_resp_civilite" :
                            case "do_resp_prenom" :
                            case "do_resp_nom" :
                            case "do_nom" :
                            case "do_adr1" :
                            case "do_adr2" :
                            case "do_ville" :
                            case "do_codepays" :
                                $desc_tra = _("Changement de coordonnées");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                if (trim($enr_avant["do_ste"]) <> "") {
                                    $txt_avant .= $enr_avant["do_ste"];
                                    $txt_avant .= chr(13) . chr(10);
                                }
                                $txt_avant .= $enr_avant["do_adr1"];
                                $txt_avant .= chr(13) . chr(10);
                                if (trim($enr_avant["do_adr2"]) <> "") {
                                    $txt_avant .= $enr_avant["do_adr2"];
                                    $txt_avant .= chr(13) . chr(10);
                                }
                                $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_avant["do_ville"]) . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_avant .= $villes_cp . " ";

                                $ville = str_majuscules($villes_nom);
                                // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                if (stristr($villes_nom, "LYON"))
                                    $ville = "LYON";
                                if (stristr($villes_nom, "MARSEILLE"))
                                    $ville = "MARSEILLE";
                                if (stristr($villes_nom, "PARIS"))
                                    $ville = "PARIS";
                                $txt_avant .= $ville;
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_avant["do_codepays"];
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_avant["do_civilite"] . " " . $enr_avant["do_prenom"] . " " . $enr_avant["do_nom"];
                                $txt_avant .= chr(13) . chr(10);

                                $txt_apres = chr(13) . chr(10);
                                if (trim($enr_apres["do_ste"]) <> "") {
                                    $txt_apres .= $enr_apres["do_ste"];
                                    $txt_apres .= chr(13) . chr(10);
                                }
                                $txt_apres .= $enr_apres["do_adr1"];
                                $txt_apres .= chr(13) . chr(10);
                                if (trim($enr_apres["do_adr2"]) <> "") {
                                    $txt_apres .= $enr_apres["do_adr2"];
                                    $txt_apres .= chr(13) . chr(10);
                                }
                                $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_apres["do_ville"]) . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_apres .= $villes_cp . " ";

                                $ville = str_majuscules($villes_nom);
                                // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                if (stristr($villes_nom, "LYON"))
                                    $ville = "LYON";
                                if (stristr($villes_nom, "MARSEILLE"))
                                    $ville = "MARSEILLE";
                                if (stristr($villes_nom, "PARIS"))
                                    $ville = "PARIS";
                                $txt_apres .= $ville;
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_apres["do_codepays"];
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_apres["do_civilite"] . " " . $enr_apres["do_prenom"] . " " . $enr_apres["do_nom"];

                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["do_code"], "MODAD", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_codeadev" :
                                $desc_tra = _("Changement de devise");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= $enr_avant["do_codeadev"] . ' '._("en").' ' . $enr_apres["do_codeadev"];
                                add_trace(3, $enr_avant["do_code"], "MODDV", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_telf" :
                            case "do_fax" :
                            case "do_resp_telf" :
                            case "do_resp_telp" :
                                $desc_tra = _("Changement de coordonnées téléphoniques");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= _("Fixe").' : ' . trim($enr_avant["do_telf"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Fax").' : ' . trim($enr_avant["do_fax"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Fixe Resp.").' : ' . trim($enr_avant["do_resp_telf"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Mob Resp.").' : ' . trim($enr_avant["do_resp_telp"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_apres = "";
                                $txt_apres .= _("Fixe").' : ' . trim($enr_apres["do_telf"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Fax").' : ' . trim($enr_apres["do_fax"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Fixe Resp.").' : ' . trim($enr_apres["do_resp_telf"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Mob Resp.").' : ' . trim($enr_apres["do_resp_telp"]);
                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["do_code"], "MODTE", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_txttelf" :
                            case "do_txtfax" :
                                $desc_tra = _("Changement texte détail");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= _("Détail Tel Fixe").' : ' . trim($enr_avant["do_txttelf"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Détail Fax").' : ' . trim($enr_avant["do_txtfax"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_apres = "";
                                $txt_apres .= _("Détail Tel Fixe").' : ' . trim($enr_apres["do_txttelf"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Détail Fax").' : ' . trim($enr_apres["do_txtfax"]);
                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["do_code"], "MODTD", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_email" :
                            case "do_emaild" :
                            case "do_emailc" :
                            case "do_emailt" :
                                $desc_tra = _("Changement de coordonnées mail/messagerie");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= _("Mail").' : ' . trim($enr_avant["do_email"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Mail Dir.").' : ' . trim($enr_avant["do_emaild"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Mail Cpta").' : ' . trim($enr_avant["do_emailc"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Mail Tech").' : ' . trim($enr_avant["do_emailt"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_apres = "";
                                $txt_apres .= _("Mail").' : ' . trim($enr_apres["do_email"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Mail Dir.").' : ' . trim($enr_apres["do_emaild"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Mail Cpta").' : ' . trim($enr_apres["do_emailc"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Mail Tech").' : ' . trim($enr_apres["do_emailt"]);
                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["do_code"], "MODEM", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_heure" :
                                $desc_tra = _("Changement heures ouverture");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= $enr_avant["do_heure"] . chr(13) . chr(10) . ' '._("en").' ' . chr(13) . chr(10) . $enr_apres["do_heure"];
                                add_trace(3, $enr_avant["do_code"], "MODHO", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_web" :
                                $desc_tra = _("Changement site web");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= $enr_avant["do_web"] . chr(13) . chr(10) . ' '._("en").' ' . chr(13) . chr(10) . $enr_apres["do_web"];
                                add_trace(3, $enr_avant["do_code"], "MODSW", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_nota" :
                                $desc_tra = _("Changement de notes");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= $enr_avant["do_nota"] . chr(13) . chr(10) . ' '._("en").' ' . chr(13) . chr(10) . $enr_apres["do_nota"];
                                add_trace(3, $enr_avant["do_code"], "MODNA", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_fact" :
                                $desc_tra = _("Changement de mode de facturation");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= $non_oui[intval($enr_avant["do_fact"])] . ' '._("en").' ' . $non_oui[intval($enr_apres["do_fact"])];
                                add_trace(3, $enr_avant["do_code"], "MODMF", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "do_ouvert" :
                                $desc_tra = _("Changement mode actif");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= $non_oui[$enr_avant["do_ouvert"]] . ' '._("en").' ' . $non_oui[$enr_apres["do_ouvert"]];
                                add_trace(3, $enr_avant["do_code"], "MODMC", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                        }
                    }
                }
                break;
            // Contacts Donneurs d'ordres
            case "cdo" :
                $titre_tra = _("Modification de contact");
                $titre_tra .= " (" . $enr_avant["cdo_civilite"] . " " . $enr_avant["cdo_prenom"] . " " . $enr_avant["cdo_nom"] . ")";

                // On analyse les différences de saisie du formulaire
                if ((is_array($enr_avant) == TRUE) AND (is_array($enr_apres) == TRUE)) {
                    $tab_diff = array_diff_assoc($enr_avant, $enr_apres);
                    foreach ($tab_diff as $key => $value) {
                        switch ($key) {
                            case "cdo_codefonc" :
                                $desc_tra = _("Changement de fonction contact");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $rech2 = "SELECT fonc_desc FROM `" . TBFONC . "` WHERE `fonc_code` = '" . $enr_avant["cdo_codefonc"] . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_avant .= $fonc_desc;
                                $txt_avant .= chr(13) . chr(10);

                                $txt_apres = "";
                                $rech2 = "SELECT fonc_desc FROM `" . TBFONC . "` WHERE `fonc_code` = '" . $enr_apres["cdo_codefonc"] . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_apres .= $fonc_desc;

                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["cdo_codedo"], "MODFO", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "cdo_civilite" :
                            case "cdo_prenom" :
                            case "cdo_nom" :
                            case "cdo_adr1" :
                            case "cdo_adr2" :
                            case "cdo_ville" :
                            case "cdo_codepays" :
                                $desc_tra = _("Changement de coordonnées");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= $enr_avant["cdo_civilite"] . " " . $enr_avant["cdo_prenom"] . " " . $enr_avant["cdo_nom"];
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_avant["cdo_adr1"];
                                $txt_avant .= chr(13) . chr(10);
                                if (trim($enr_avant["cdo_adr2"]) <> "") {
                                    $txt_avant .= $enr_avant["cdo_adr2"];
                                    $txt_avant .= chr(13) . chr(10);
                                }
                                $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_avant["cdo_ville"]) . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_avant .= $villes_cp . " ";

                                $ville = str_majuscules($villes_nom);
                                // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                if (stristr($villes_nom, "LYON"))
                                    $ville = "LYON";
                                if (stristr($villes_nom, "MARSEILLE"))
                                    $ville = "MARSEILLE";
                                if (stristr($villes_nom, "PARIS"))
                                    $ville = "PARIS";
                                $txt_avant .= $ville;
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_avant["cdo_codepays"];
                                $txt_avant .= chr(13) . chr(10);

                                $txt_apres = chr(13) . chr(10);
                                $txt_avant .= $enr_apres["cdo_civilite"] . " " . $enr_apres["cdo_prenom"] . " " . $enr_apres["cdo_nom"];
                                $txt_apres .= $enr_apres["cdo_adr1"];
                                $txt_apres .= chr(13) . chr(10);
                                if (trim($enr_apres["cdo_adr2"]) <> "") {
                                    $txt_apres .= $enr_apres["cdo_adr2"];
                                    $txt_apres .= chr(13) . chr(10);
                                }
                                $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($enr_apres["cdo_ville"]) . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $txt_apres .= $villes_cp . " ";

                                $ville = str_majuscules($villes_nom);
                                // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
                                if (stristr($villes_nom, "LYON"))
                                    $ville = "LYON";
                                if (stristr($villes_nom, "MARSEILLE"))
                                    $ville = "MARSEILLE";
                                if (stristr($villes_nom, "PARIS"))
                                    $ville = "PARIS";
                                $txt_apres .= $ville;
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= $enr_apres["cdo_codepays"];

                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["cdo_codedo"], "MODAD", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "cdo_telf" :
                            case "cdo_fax" :
                            case "cdo_telp" :
                                $desc_tra = _("Changement de coordonnées téléphoniques");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= _("Fixe").' : ' . trim($enr_avant["cdo_telf"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Fax").' : ' . trim($enr_avant["cdo_fax"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Mob").' : ' . trim($enr_avant["cdo_telp"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_apres = "";
                                $txt_apres .= _("Fixe").' : ' . trim($enr_apres["cdo_telf"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Fax").' : ' . trim($enr_apres["cdo_fax"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Mob").' : ' . trim($enr_apres["cdo_telp"]);
                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["cdo_codedo"], "MODTE", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "cdo_email" :
                            case "cdo_mess" :
                                $desc_tra = _("Changement de coordonnées mail/messagerie");
                                $desc_tra .= chr(13) . chr(10);
                                $txt_avant = "";
                                $txt_avant .= _("Mail").' : ' . trim($enr_avant["cdo_email"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_avant .= _("Mess").' : ' . trim($enr_avant["cdo_mess"]);
                                $txt_avant .= chr(13) . chr(10);
                                $txt_apres = "";
                                $txt_apres .= _("Mail").' : ' . trim($enr_apres["cdo_email"]);
                                $txt_apres .= chr(13) . chr(10);
                                $txt_apres .= _("Mess").' : ' . trim($enr_apres["cdo_mess"]);
                                $desc_tra .= $txt_avant . ' '._("en").' ' . $txt_apres;
                                add_trace(3, $enr_avant["cdo_codedo"], "MODEM", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;

                            case "cdo_nota" :
                                $desc_tra = _("Changement de notes");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= $enr_avant["cdo_nota"] . chr(13) . chr(10) . ' '._("en").' ' . chr(13) . chr(10) . $enr_apres["cdo_nota"];
                                add_trace(3, $enr_avant["cdo_codedo"], "MODNO", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                        }
                    }
                }
                break;
            // Rendez-vous
            case "rdv" :
                $titre_tra = _("Modification de RDV");
                switch (intval($enr_avant["rdv_typrat"])) {
                    // Interventions
                    case 1 :
                        $titre_tra .= ' ('._("Intervention").' ' ;
                        break;
                    default :
                        break;
                }
                $titre_tra .= $enr_avant["rdv_coderat"] . ")";

                // On analyse les différences de saisie du formulaire
                if ((is_array($enr_avant) == TRUE) AND (is_array($enr_apres) == TRUE)) {
                    $tab_diff = array_diff_assoc($enr_avant, $enr_apres);
                    foreach ($tab_diff as $key => $value) {
                        switch ($key) {
                            case "rdv_date" :
                            case "rdv_heure" :
                            case "rdv_dureem" :
                                // On récupère des éléments précédemment stockés dans l'intervention
                                // (Pour la traçabilité)
                                $ex_drdv = "";
                                if ($enr_avant["rdv_date"] <> "0000-00-00")
                                    $ex_drdv = $enr_avant["rdv_date"];
                                $ex_hrdv = "";
                                if ($enr_avant["rdv_hrdv"] <> "00:00:00")
                                    $ex_hrdv = $enr_avant["rdv_heure"];

                                $new_drdv = "";
                                if ($enr_apres["rdv_date"] <> "0000-00-00")
                                    $new_drdv = $enr_apres["rdv_date"];
                                $new_hrdv = "";
                                if ($enr_apres["rdv_hrdv"] <> "00:00:00")
                                    $new_hrdv = $enr_apres["rdv_heure"];

                                // Ajout traçabilité (Si on a changé le RDV)
                                if (($new_drdv <> $ex_drdv) OR ($new_hrdv <> $ex_hrdv)) {
                                    if ($ex_drdv <> "") {
                                        $desc_tra = _("Changement de l'heure de RDV");
                                        $desc_tra .= chr(13) . chr(10);
                                        $desc_tra .= _("De").'  ' . datetolocal($ex_drdv) . " à " . substr($ex_hrdv, 0, 5) . ' '._("en").' ' . datetolocal($new_drdv) . " à " . substr($new_hrdv, 0, 5);
                                    } else {
                                        $desc_tra = _("Ajout de l'heure de RDV");
                                        $desc_tra .= chr(13) . chr(10);
                                        $desc_tra .= datetolocal($new_drdv) . " à " . substr($new_hrdv, 0, 5);
                                    }
                                    add_trace(1, $enr_avant["rdv_coderat"], "MODR", $titre_tra, $desc_tra, FALSE);
                                    $trace_added = TRUE;
                                }
                                break;
                            case "rdv_codelieu" :
                                $desc_tra = _("Changement de lieu d'intervention");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= _("De").'  ';
                                $rech2 = "SELECT lieu_desc FROM `" . TBLIEU . "` WHERE `lieu_code` = '" . $enr_avant["rdv_codelieu"] . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $desc_tra .= $lieu_desc;
                                $desc_tra .= ' '._("en").' ';
                                $rech2 = "SELECT lieu_desc FROM `" . TBLIEU . "` WHERE `lieu_code` = '" . $enr_apres["rdv_codelieu"] . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $desc_tra .= $lieu_desc;
                                add_trace(1, $enr_avant["rdv_coderat"], "MODLI", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                            case "rdv_nivurg" :
                                $desc_tra = _("Changement de niveau d'urgence");
                                $desc_tra .= chr(13) . chr(10);
                                $desc_tra .= _("De").'  ';
                                $rech2 = "SELECT urg_des FROM `" . TBURG . "` WHERE `urg_niveau` = '" . $enr_avant["rdv_nivurg"] . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $desc_tra .= $urg_des;
                                $desc_tra .= ' '._("en").' ';
                                $rech2 = "SELECT urg_des FROM `" . TBURG . "` WHERE `urg_niveau` = '" . $enr_apres["rdv_nivurg"] . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2)
                                {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                }
                                $desc_tra .= $urg_des;
                                add_trace(1, $enr_avant["rdv_coderat"], "MODUR", $titre_tra, $desc_tra, FALSE);
                                $trace_added = TRUE;
                                break;
                        }
                    }
                }
                break;
            // Sous-traitants
            case "st" :
                break;
            default :
                break;
        }
    }

    if ($open == TRUE)
        $db->disconnect();

    return $trace_added;
}

/**
 * Lit le détail mail rattaché
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_trace_mail($id, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBTRAMAIL . "` WHERE tramail_traid = '$id'";
    $row2 = $db->get_row($rech2, ARRAY_A);

    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}
?>