<?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");


include_once("../inc/config.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_repl.inc.php");
include_once("../inc/lst.inc.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$repli_ok = TRUE;
$tab_dep = [];

if (isset($_GET['id'])) {
    $id = intval(@$_GET['id']);
    $row = lit_enr_replm($id);
    if (is_array($row)) {
        extract($row);
        $tab_dep = explode('_', $replm_dep);
    } else
        $repli_ok = FALSE;
} else
    $repli_ok = FALSE;

$titre = "EXECUTION REPLICATION";

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("Réplication"); ?></title>

    <?PHP include_once("../inc/header.inc.php"); ?>

</head>

<body>
<?php
include_once("../inc/entete.inc.php");
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<p>&nbsp;</p>
<?PHP
if ($repli_ok == TRUE) {
    ?>
    <table width="100%" border="0">
        <tr>
            <td width="40%">&nbsp;</td>
            <td width="160" class="annot_std_gras" align="center">
                Début du transfert
            </td>
            <td width="40%">&nbsp;</td>
        </tr>
    </table>
    <?PHP
    echo '<p align="center"><span class="annot_std_gras"></span></p>';
    // Connexion à la base principale
    $db1 = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    // Connexion à la base secondaire
    $db2 = open_database(DBUSER, DBPASS, $replm_dbname, DBHOST);

    // Construction chaine de recherche
    $rech = "SELECT * FROM `" . TBCLI . "`";
    // WHERE existe ?
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE (";
    else
        $rech .= " AND (";

    foreach ($tab_dep as $key => $value) {
        $rech .= " " . TBCLI . ".cli_dep='" . substr($value, 0, 2) . "' OR";
    }

    // Suppression du dernier 'OR'
    $rech = substr($rech, 0, strlen($rech) - 3);
    $rech .= ")";

    // Si on est en mode 0 (Principal écrase secondaire) ou 2 (selon date), on balaye la table principale
    // Sinon on balaye la table secondaire
    switch (intval($replm_mode)) {
        // Principale écrase secondaire
        // ou selon date plus récente
        case 0 :
        case 2 :
            $rows = $db1->get_results($rech, ARRAY_A);
            break;
        // Secondaire écrase primaire
        case 1 :
            $rows = $db2->get_results($rech, ARRAY_A);
            break;
    }

    // *** Affichage de la table
    echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";

    echo '<th align="center">';
    echo '<span class="annot_petit_gras3">';
    echo _("Code client");
    echo '</span>';
    echo "</th>\n";
    echo '<th>';
    echo '<span class="annot_petit_gras3">';
    echo _("Société");
    echo '</span>';
    echo "</th>\n";
    echo '<th>';
    echo '<span class="annot_petit_gras3">';
    echo _("Nom client");
    echo '</span>';
    echo "</th>\n";
    echo '<th>';
    echo '<span class="annot_petit_gras3">';
    echo _("Répliqué");
    echo '</span>';
    echo "</th>\n";
    if ($replm_sctact == 1) {
        echo '<th>';
        echo '<span class="annot_petit_gras3">';
        echo _("Contacts répliqués");
        echo '</span>';
        echo "</th>\n";
    }
    if ($replm_ssites == 1) {
        echo '<th>';
        echo '<span class="annot_petit_gras3">';
        echo _("Sites répliqués");
        echo '</span>';
        echo "</th>\n";
    }

    $i = 0;
    foreach ($rows as $row) {
        // création de l'entête de ligne (TR)
        creetrlst($i);

        $tcli_ok = FALSE;

        echo '<td>' . $row["cli_code"];
        echo "</td>\n";
        echo '<td>';
        if ($row["cli_ste"] <> "")
            echo $row["cli_ste"];
        else
            echo "&nbsp;";
        echo "</td>\n";
        echo '<td>';
        echo $row["cli_civilite"] . " " . $row["cli_nom"] . " " . $row["cli_prenom"];
        echo "</td>\n";
        echo '<td align="center">';


        // MISE A JOUR DES FICHES PRINCIPALES //

        $texist = FALSE;
        $tacreer = FALSE;
        $newcode = "";

        // On vérifie si le client existe dans la table de transfert
        $rech2 = "SELECT * FROM `" . TBREPLCLI . "`";
        switch (intval($replm_mode)) {
            // Principale écrase secondaire
            // ou selon date plus récente
            case 0 :
            case 2 :
                $rech2 .= "WHERE replcli_modele=" . $replm_id . " AND replcli_idori=" . $row["cli_id"];
                break;
            // Secondaire écrase primaire
            case 1 :
                $rech2 .= "WHERE replcli_modele=" . $replm_id . " AND replcli_iddest=" . $row["cli_id"];
                break;
        }
        $row2 = $db1->get_row($rech2, ARRAY_A);
        // Si oui, on récupère les éléments
        if ($row2) {
            $texist = TRUE;
            extract($row2);
        } else
            $tacreer = TRUE;

// echo "mode : ".$replm_mode."<br />";
        switch (intval($replm_mode)) {
            // Principale écrase secondaire
            case 0 :
                // On met à jour ?
                if ($texist == TRUE) {
                    $rech3 = "UPDATE `" . TBCLI . "` SET ";
                    foreach ($row as $key => $value) {
                        if ($key <> "cli_id")
                            $rech3 .= " " . TBCLI . "." . $key . "='" . $value . "', ";
                    }
                    // Suppression dernière virgule
                    $rech3 = substr($rech3, 0, strlen($rech3) - 2);
                    $rech3 .= " WHERE `cli_id` = '" . $replcli_iddest . "'";
                }

                // On crée ?
                if ($tacreer == TRUE) {
                    // On génère un nouveau code (on est en création...) dans la base de destination
                    $rech3 = "SELECT * FROM `" . TBCONS . "`";
                    $row3 = $db2->get_row($rech3, ARRAY_A);
                    if ($row3) {
                        $row3 = encode_str($row3);
                        extract($row3);
                        $rcmaj = "";
                        $rcli = clean_str($cons_rcli);
                        $ncli = $cons_ncli;
                        $ncli++;
                        settype($ncli, "string");
                        $format = "%'07s";
                        $nclif = sprintf($format, $ncli);
                        $newcode = $rcli . $nclif;
                        $rcmaj = "UPDATE `" . TBCONS . "` SET `cons_ncli`='" . $ncli . "'  WHERE `cons_id` = '" . $cons_id . "'";
                        $rscmaj = $db2->query($rcmaj);
                    }

                    $rech3 = "INSERT INTO `" . TBCLI . "` (";
                    foreach ($row as $key => $value) {
                        switch ($key) {
                            case "cli_id" :
                                break;
                            default :
                                $rech3 .= $key . ",";
                                break;
                        }
                    }
                    // Suppression dernière virgule
                    $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                    $rech3 .= ") VALUES (";
                    foreach ($row as $key => $value) {
                        switch ($key) {
                            case "cli_id" :
                                break;
                            case "cli_code" :
                                $rech3 .= "'" . $newcode . "',";
                                break;
                            default :
                                $rech3 .= "'" . $value . "',";
                                break;
                        }
                    }
                    // Suppression dernière virgule
                    $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                    $rech3 .= ")";
                }
                $rs3 = $db2->query($rech3);
                break;

            // Secondaire écrase primaire
            case 1 :
                // On met à jour ?
                if ($texist == TRUE) {
                    $rech3 = "UPDATE `" . TBCLI . "` SET ";
                    foreach ($row as $key => $value) {
                        if ($key <> "cli_id")
                            $rech3 .= " " . TBCLI . "." . $key . "='" . $value . "', ";
                    }
                    // Suppression dernière virgule
                    $rech3 = substr($rech3, 0, strlen($rech3) - 2);
                    $rech3 .= " WHERE `cli_id` = '" . $replcli_iddest . "'";
                }

                // On crée ?
                if ($tacreer == TRUE) {
                    // On génère un nouveau code (on est en création...) dans la base principale
                    $rech3 = "SELECT * FROM `" . TBCONS . "`";
                    $row3 = $db1->get_row($rech3, ARRAY_A);
                    if ($row3) {
                        encode_str($row3);
                        extract($row3);
                        $rcmaj = "";
                        $rcli = clean_str($cons_rcli);
                        $ncli = $cons_ncli;
                        $ncli++;
                        settype($ncli, "string");
                        $format = "%'07s";
                        $nclif = sprintf($format, $ncli);
                        $newcode = $rcli . $nclif;
                        $rcmaj = "UPDATE `" . TBCONS . "` SET `cons_ncli`='" . $ncli . "'  WHERE `cons_id` = '" . $cons_id . "'";
                        $rscmaj = $db1->query($rcmaj);
                    }

                    $rech3 = "INSERT INTO `" . TBCLI . "` (";
                    foreach ($row as $key => $value) {
                        switch ($key) {
                            case "cli_id" :
                                break;
                            default :
                                $rech3 .= $key . ",";
                                break;
                        }
                    }
                    // Suppression dernière virgule
                    $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                    $rech3 .= ") VALUES (";
                    foreach ($row as $key => $value) {
                        switch ($key) {
                            case "cli_id" :
                                break;
                            case "cli_code" :
                                $rech3 .= "'" . $newcode . "',";
                                break;
                            default :
                                $rech3 .= "'" . $value . "',";
                                break;
                        }
                    }
                    // Suppression dernière virgule
                    $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                    $rech3 .= ")";
                }

                $rs3 = $db1->query($rech3);
                break;

            // Plus récente écrase plus ancien
            case 2 :
                // On met à jour ?
                if ($texist == TRUE) {
                    // On lit la secondaire
                    $rech4 = "SELECT * FROM `" . TBCLI . "`";
                    $rech4 .= "WHERE cli_id=" . $replcli_iddest;
                    $row4 = $db2->get_row($rech4, ARRAY_A);
                    if ($row4) {
                        // On vérifie la dernière date de modification de la base secondaire par rapport à la dernière
                        // date de modification de la fiche dans la date principale

                        // La fiche principale est plus récente que la fiche secondaire
                        // On met donc à jour la fche secondaire
                        if (comp_date($row4["cli_datemod"], $row["cli_datemod"]) == TRUE) {
                            $rech3 = "UPDATE `" . TBCLI . "` SET ";
                            foreach ($row as $key => $value) {
                                if ($key <> "cli_id")
                                    $rech3 .= " " . TBCLI . "." . $key . "='" . $value . "', ";
                            }
                            // Suppression dernière virgule
                            $rech3 = substr($rech3, 0, strlen($rech3) - 2);
                            $rech3 .= " WHERE `cli_id` = '" . $replcli_iddest . "'";
                            $rs3 = $db2->query($rech3);
                        } // La fiche secondaire est plus récente que la fiche principale
                        // On met donc à jour la fche principale
                        else {
                            // On met à jour
                            $rech3 = "UPDATE `" . TBCLI . "` SET ";
                            foreach ($row4 as $key => $value) {
                                if ($key <> "cli_id")
                                    $rech3 .= " " . TBCLI . "." . $key . "='" . $value . "', ";
                            }
                            // Suppression dernière virgule
                            $rech3 = substr($rech3, 0, strlen($rech3) - 2);
                            $rech3 .= " WHERE `cli_id` = '" . $replcli_idori . "'";
                            $rs3 = $db1->query($rech3);
                        }
                    }
                }

                // On crée ?
                // Si oui, c'est une procédure de création classique
                if ($tacreer == TRUE) {
                    // On génère un nouveau code (on est en création...) dans la base secondaire
                    $rech3 = "SELECT * FROM `" . TBCONS . "`";
                    $row3 = $db2->get_row($rcons, ARRAY_A);
                    if ($row3) {
                        extract($row3);
                        $rcmaj = "";
                        $rcli = clean_str($cons_rcli);
                        $ncli = $cons_ncli;
                        $ncli++;
                        settype($ncli, "string");
                        $format = "%'07s";
                        $nclif = sprintf($format, $ncli);
                        $newcode = $rcli . $nclif;
                        $rcmaj = "UPDATE `" . TBCONS . "` SET `cons_ncli`='" . $ncli . "'  WHERE `cons_id` = '" . $cons_id . "'";
                        $rscmaj = $db2->query($rcmaj);
                    }

                    $rech3 = "INSERT INTO `" . TBCLI . "` (";
                    foreach ($row as $key => $value) {
                        switch ($key) {
                            case "cli_id" :
                                break;
                            default :
                                $rech3 .= $key . ",";
                                break;
                        }
                    }
                    // Suppression dernière virgule
                    $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                    $rech3 .= ") VALUES (";
                    foreach ($row as $key => $value) {
                        switch ($key) {
                            case "cli_id" :
                                break;
                            case "cli_code" :
                                $rech3 .= "'" . $newcode . "',";
                                break;
                            default :
                                $rech3 .= "'" . $value . "',";
                                break;
                        }
                    }
                    // Suppression dernière virgule
                    $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                    $rech3 .= ")";
                    $rs3 = $db2->query($rech3);
                }
                break;
        }
        // On met à jour le fichier de réplication
        if ($tacreer == TRUE) {
            switch (intval($replm_mode)) {
                // Principale écrase secondaire
                case 0 :
                    // Plus récente écrase plus ancien
                case 2 :
                    $rech5 = "INSERT INTO `" . TBREPLCLI . "`";
                    $rech5 .= " (replcli_modele,replcli_idori,replcli_iddest,replcli_lastdate,replcli_lasthour)";
                    $rech5 .= " VALUES";
                    $rech5 .= " ('$replm_id','" . $row["cli_id"] . "'," . mysql_insert_id() . ",'$datea','$heurea')";
                    break;
                // Secondaire écrase primaire
                case 1 :
                    $rech5 = "INSERT INTO `" . TBREPLCLI . "`";
                    $rech5 .= " (replcli_modele,replcli_idori,replcli_iddest,replcli_lastdate,replcli_lasthour)";
                    $rech5 .= " VALUES";
                    $rech5 .= " ('$replm_id','" . mysql_insert_id() . "'," . $row["cli_id"] . ",'$datea','$heurea')";
                    break;
            }
            echo _("Oui (ajouté)");
        }
        if ($texist == TRUE) {
            $rech5 = "UPDATE `" . TBREPLCLI;
            $rech5 .= "` SET `replcli_lastdate`='" . $datea . "', `replcli_lasthour`='" . $heurea . "'";
            $rech5 .= " WHERE `replcli_id` = '" . $replcli_id . "'";
            echo _("Oui (Mis à jour)");
        }
        $rs5 = $db1->query($rech5);

        // FIN MISE A JOUR FICHE PRINCIPALE //

        echo "</td>\n";


        // MISE A JOUR DES FICHES CONTACTS

        if ($replm_sctact == 1) {
            echo '<td align="center">';
            switch (intval($replm_mode)) {
                // Principale écrase secondaire
                case 0 :
                    // On balaye la liste des contacts du client dans la base principale
                    $rech6 = "SELECT * FROM `" . TBCCLI . "` WHERE `ccli_codecli` = '" . $row["cli_code"] . "'";

                    mysql_select_db(DBNAME, $conn1);
                    $rows6 = $db1->get_results($rech6, ARRAY_A);
                    if ($rows6) {
                        foreach ($rows6 as $row6) {
                            $tcexist = FALSE;
                            $tcacreer = FALSE;

                            // On vérifie si le client existe dans la table de transfert des contacts
                            $rech7 = "SELECT * FROM `" . TBREPLCLIC . "`";
                            $rech7 .= "WHERE replclic_modele=" . $replm_id . " AND replclic_idori=" . $row6["ccli_id"];
                            $row7 = $db1->get_row($rech7, ARRAY_A);
                            // Si oui, on récupère les éléments
                            if ($row7) {
                                $tcexist = TRUE;
                                extract($row7);
                            } else
                                $tcacreer = TRUE;

                            // On met à jour ?
                            if ($tcexist == TRUE) {
                                $rech3 = "UPDATE `" . TBCCLI . "` SET ";
                                foreach ($row6 as $key => $value) {
                                    if ($key <> "ccli_id")
                                        $rech3 .= " " . TBCCLI . "." . $key . "='" . $value . "', ";
                                }
                                // Suppression dernière virgule
                                $rech3 = substr($rech3, 0, strlen($rech3) - 2);
                                $rech3 .= " WHERE `ccli_id` = '" . $replclic_iddest . "'";
                            }

                            // On crée ?
                            if ($tcacreer == TRUE) {
                                $rech3 = "INSERT INTO `" . TBCCLI . "` (";
                                foreach ($row6 as $key => $value) {
                                    switch ($key) {
                                        case "ccli_id" :
                                            break;
                                        default :
                                            $rech3 .= $key . ",";
                                            break;
                                    }
                                }
                                // Suppression dernière virgule
                                $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                                $rech3 .= ") VALUES (";
                                foreach ($row6 as $key => $value) {
                                    switch ($key) {
                                        case "ccli_id" :
                                            break;
                                        case "ccli_codecli" :
                                            if ($tacreer == TRUE)
                                                $rech3 .= "'" . $newcode . "',";
                                            else
                                                $rech3 .= "'" . $value . "',";
                                            break;
                                        default :
                                            $rech3 .= "'" . $value . "',";
                                            break;
                                    }
                                }
                                // Suppression dernière virgule
                                $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                                $rech3 .= ")";
                            }
                            $rs3 = $db2->query($rech3);

                            // On met à jour le fichier de réplication contacts
                            if ($tcexist == TRUE) {
                                $rech8 = "UPDATE `" . TBREPLCLIC;
                                $rech8 .= "` SET `replclic_lastdate`='" . $datea . "', `replclic_lasthour`='" . $heurea . "'";
                                $rech8 .= " WHERE `replclic_id` = '" . $replclic_id . "'";
                            }

                            if ($tcacreer == TRUE) {
                                $rech8 = "INSERT INTO `" . TBREPLCLIC . "`";
                                $rech8 .= " (replclic_modele,replclic_idori,replclic_iddest,replclic_lastdate,replclic_lasthour)";
                                $rech8 .= " VALUES";
                                $rech8 .= " ('$replm_id','" . $row6["ccli_id"] . "'," . mysql_insert_id() . ",'$datea','$heurea')";
                            }
                            $rs8 = $db1->query($rech8);
                        }
                        echo _("ok");
                    } else
                        echo _("Aucun contact");
                    break;

                // Secondaire écrase primaire
                case 1 :
                    // On balaye la liste des contacts du client dans la base secondaire
                    $rech6 = "SELECT * FROM `" . TBCCLI . "` WHERE `ccli_codecli` = '" . $row["cli_code"] . "'";
                    $rows6 = $db2->get_results($rech6, ARRAY_A);
                    if ($rows6) {
                        foreach ($rows6 as $row6) {
                            $tcexist = FALSE;
                            $tcacreer = FALSE;

                            // On vérifie si le client existe dans la table de transfert des contacts
                            $rech7 = "SELECT * FROM `" . TBREPLCLIC . "`";
                            $rech7 .= "WHERE replclic_modele=" . $replm_id . " AND replclic_iddest=" . $row6["ccli_id"];
                            mysql_select_db(DBNAME, $conn1);
                            $row7 = $db1->get_row($rech7, ARRAY_A);
                            // Si oui, on récupère les éléments
                            if ($row7) {
                                $tcexist = TRUE;
                                extract($row7);
                            } else
                                $tcacreer = TRUE;

                            // On met à jour ?
                            if ($tcexist == TRUE) {
                                $rech3 = "UPDATE `" . TBCCLI . "` SET ";
                                foreach ($row6 as $key => $value) {
                                    if ($key <> "ccli_id")
                                        $rech3 .= " " . TBCCLI . "." . $key . "='" . $value . "', ";
                                }
                                // Suppression dernière virgule
                                $rech3 = substr($rech3, 0, strlen($rech3) - 2);
                                $rech3 .= " WHERE `ccli_id` = '" . $replclic_iddest . "'";
                            }

                            // On crée ?
                            if ($tcacreer == TRUE) {
                                $rech3 = "INSERT INTO `" . TBCCLI . "` (";
                                foreach ($row6 as $key => $value) {
                                    switch ($key) {
                                        case "ccli_id" :
                                            break;
                                        default :
                                            $rech3 .= $key . ",";
                                            break;
                                    }
                                }
                                // Suppression dernière virgule
                                $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                                $rech3 .= ") VALUES (";
                                foreach ($row6 as $key => $value) {
                                    switch ($key) {
                                        case "ccli_id" :
                                            break;
                                        case "ccli_codecli" :
                                            if ($tacreer == TRUE)
                                                $rech3 .= "'" . $newcode . "',";
                                            else
                                                $rech3 .= "'" . $value . "',";
                                            break;
                                        default :
                                            $rech3 .= "'" . $value . "',";
                                            break;
                                    }
                                }
                                // Suppression dernière virgule
                                $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                                $rech3 .= ")";
                            }
                            $rs3 = $db1->query($rech3);

                            // On met à jour le fichier de réplication contacts
                            if ($tcexist == TRUE) {
                                $rech8 = "UPDATE `" . TBREPLCLIC;
                                $rech8 .= "` SET `replclic_lastdate`='" . $datea . "', `replclic_lasthour`='" . $heurea . "'";
                                $rech8 .= " WHERE `replclic_id` = '" . $replclic_id . "'";
                            }

                            if ($tcacreer == TRUE) {
                                $rech8 = "INSERT INTO `" . TBREPLCLIC . "`";
                                $rech8 .= " (replclic_modele,replclic_idori,replclic_iddest,replclic_lastdate,replclic_lasthour)";
                                $rech8 .= " VALUES";
                                $rech8 .= " ('$replm_id','" . $row6["ccli_id"] . "'," . mysql_insert_id() . ",'$datea','$heurea')";
                            }
                            $rs8 = $db1->query($rech8);
                        }
                        echo _("ok");
                    } else
                        echo _("Aucun contact");
                    break;

                // Plus récente écrase plus ancien
                case 2 :
                    // On balaye la liste des contacts du client dans la base principale
                    $rech6 = "SELECT * FROM `" . TBCCLI . "` WHERE `ccli_codecli` = '" . $row["cli_code"] . "'";
                    $rows6 = $db1->get_results($rech6, ARRAY_A);
                    if ($rows6) {
                        foreach ($rows6 as $row6) {
                            $tcexist = FALSE;
                            $tcacreer = FALSE;

                            // On vérifie si le client existe dans la table de transfert des contacts
                            $rech7 = "SELECT * FROM `" . TBREPLCLIC . "`";
                            $rech7 .= "WHERE replclic_modele=" . $replm_id . " AND replclic_idori=" . $row6["ccli_id"];
                            $row7 = $db1->get_row($rech7, ARRAY_A);
                            // Si oui, on récupère les éléments
                            if ($row7) {
                                $tcexist = TRUE;
                                extract($row7);
                            } else
                                $tcacreer = TRUE;

                            // On met à jour ?
                            if ($tcexist == TRUE) {
                                // On lit la secondaire
                                $rech4 = "SELECT * FROM `" . TBCCLI . "`";
                                $rech4 .= "WHERE ccli_id=" . $replclic_iddest;
                                $row4 = $db2->get_row($rech4, ARRAY_A);
                                if ($row4) {
                                    // On vérifie la dernière date de modification de la base secondaire par rapport à la dernière
                                    // date de modification de la fiche dans la date principale

                                    // La fiche principale est plus récente que la fiche secondaire
                                    // On met donc à jour la fche secondaire
                                    if (comp_date($row4["ccli_datemod"], $row["ccli_datemod"]) == TRUE) {
                                        $rech3 = "UPDATE `" . TBCCLI . "` SET ";
                                        foreach ($row as $key => $value) {
                                            if ($key <> "ccli_id")
                                                $rech3 .= " " . TBCCLI . "." . $key . "='" . $value . "', ";
                                        }
                                        // Suppression dernière virgule
                                        $rech3 = substr($rech3, 0, strlen($rech3) - 2);
                                        $rech3 .= " WHERE `ccli_id` = '" . $replclic_iddest . "'";

                                        $rs3 = $db2->query($rech3);
                                    } // La fiche secondaire est plus récente que la fiche principale
                                    // On met donc à jour la fche principale
                                    else {
                                        // On met à jour
                                        $rech3 = "UPDATE `" . TBCCLI . "` SET ";
                                        foreach ($row4 as $key => $value) {
                                            if ($key <> "ccli_id")
                                                $rech3 .= " " . TBCCLI . "." . $key . "='" . $value . "', ";
                                        }
                                        // Suppression dernière virgule
                                        $rech3 = substr($rech3, 0, strlen($rech3) - 2);
                                        $rech3 .= " WHERE `ccli_id` = '" . $replclic_idori . "'";

                                        $rs3 = $db1->query($rech3);
                                    }
                                }
                            }

                            // On crée ?
                            // Si oui, c'est une procédure de création classique
                            if ($tcacreer == TRUE) {
                                $rech3 = "INSERT INTO `" . TBCCLI . "` (";
                                foreach ($row as $key => $value) {
                                    switch ($key) {
                                        case "ccli_id" :
                                            break;
                                        default :
                                            $rech3 .= $key . ",";
                                            break;
                                    }
                                }
                                // Suppression dernière virgule
                                $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                                $rech3 .= ") VALUES (";
                                foreach ($row as $key => $value) {
                                    switch ($key) {
                                        case "ccli_id" :
                                            break;
                                        case "ccli_codecli" :
                                            if ($tacreer == TRUE)
                                                $rech3 .= "'" . $newcode . "',";
                                            else
                                                $rech3 .= "'" . $value . "',";
                                            break;
                                            break;
                                        default :
                                            $rech3 .= "'" . $value . "',";
                                            break;
                                    }
                                }
                                // Suppression dernière virgule
                                $rech3 = substr($rech3, 0, strlen($rech3) - 1);
                                $rech3 .= ")";

                                $rs3 = $db2->query($rech3);
                            }
                        }
                        echo _("ok");
                    } else
                        echo _("Aucun contact");
                    break;
            }

            echo "</td>\n";
        }

        // FIN MISE A JOUR FICHES CONTACTS

        // Réplication des sites
        if ($replm_ssites == 1) {
            echo '<td align="center">';
            echo '-';
            echo "</td>\n";
        }

        echo "</tr>\n";

        $i++;
    }

    echo "</table>";
    $db1->disconnect();
    $db2->disconnect();
    ?>
    <table width="100%" border="0">
        <tr>
            <td width="40" class="annot_gras_it"><?PHP aff_lnk_lst_rech('repl_lst.php', _("Retour à la liste"), $_SERVER['HTTP_REFERER'], _("Retour à la page précédente")); ?></td>
            <td width="160" class="annot_std_gras" align="center"><?PHP echo _("Fin du transfert"); ?></td>
            <td width="40%">&nbsp;

            </td>
        </tr>
    </table>
<?PHP
} else
    pop_ret_auto(_("Type de réplication incorrect"), 'warn', "index.php");

include_once("pied.php");
?>
</body>
</html>