<?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_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_tra.inc.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();
$_POST = clean_str($_POST);

$pbm_enr = FALSE;

$titre = @$_POST["titre"];
$td = @$_POST["td"];
$idscli = @$_POST["id"];
$prop = str_majuscules(@$_POST["prop"]);
$codecli = @$_POST["cc"];

// On vérifie que le client existe
$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
$rech = "SELECT cli_prop FROM `" . TBCLI . "` WHERE `cli_code` = '" . $db->escape($codecli) . "'";
$row = $db->get_row($rech, ARRAY_A);
if ($row) {
    extract($row);
    // Si propriétaire NULL : pas de propriétaire autre "qu'agence couvrant le Département" pour le site
    // (Une agence ne peut pas s'approprier un site si il n'y a pas de gestionnaire du compte principal)
    if (trim($cli_prop) == "")
        $prop = "-1";
} else
    $pbm_enr = TRUE;
$db->disconnect();

$code = @$_POST["code"];
$ste = str_majuscules(@$_POST["ste"]);
$rcs = str_majuscules(@$_POST["rcs"]);
$ape = str_majuscules(@$_POST["ape"]);
$tvai = str_majuscules(@$_POST["tvai"]);
$adr1 = str_majuscules(@$_POST["adr1"]);
$adr2 = str_majuscules(@$_POST["adr2"]);
$dep = str_majuscules(@$_POST["dep"]);
$ville = str_majuscules(@$_POST["ville"]);
$pays = str_majuscules(@$_POST["pays"]);
$telf = @$_POST["telf"];
$fax = @$_POST["fax"];
$email = strtolower(@$_POST["email"]);
$rcivil = str_majuscules(@$_POST["rcivil"]);
$rprenom = ucname(@$_POST["rprenom"]);
$rnom = str_majuscules(@$_POST["rnom"]);
$rtelf = @$_POST["rtelf"];
$rfax = @$_POST["rfax"];
$rtelp = @$_POST["rtelp"];
$remail = strtolower(@$_POST["remail"]);
$rfunc = str_majuscules(@$_POST["rfunc"]);
$nota = @$_POST["nota"];
$actif = intval(@$_POST["actif"]);
$ccpta = @$_POST["ccpta"];

$codeuser = @$_SESSION["code_log"];

$retourfiche = TRUE;

if ($pbm_enr == FALSE) {
    switch ($td) {
        // Ajout de la fiche Site
        case "add" :
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

            // données traçabilité
            $titre_tra = _("Création du site client");
            $desc_tra_default = $ste;
            $desc_tra_default .= chr(13) . chr(10);
            $desc_tra_default .= $adr1;
            $desc_tra_default .= chr(13) . chr(10);
            if (trim($adr2) <> "") {
                $desc_tra_default .= $adr2;
                $desc_tra_default .= chr(13) . chr(10);
            }
            $rech2 = "SELECT villes_cp, villes_nom FROM `" . TBVILLES . "` WHERE `villes_id` = '" . intval($ville) . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                $row2 = encode_str($row2);
                extract($row2);
            }
            $desc_tra_default .= $villes_cp . " ";
            $nville = "";
            $nville = str_majuscules($villes_nom);
            // Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
            if (stristr($villes_nom, "LYON"))
                $nville = "LYON";
            if (stristr($villes_nom, "MARSEILLE"))
                $nville = "MARSEILLE";
            if (stristr($villes_nom, "PARIS"))
                $nville = "PARIS";

            $desc_tra_default .= $nville;

            switch (@$_SESSION["niv_log"]) {
                case 1 :
                case 6 :
                case 7 :
                    $rech = "INSERT INTO `" . TBSCLI . "`";
                    $rech .= " (scli_datecrea,scli_codecrea,scli_datemod,scli_codemod,scli_codecli,scli_code,scli_ste,scli_rcs,scli_ape,scli_tvai,scli_adr1,scli_adr2,scli_dep,scli_ville,scli_codepays,scli_telf,scli_fax,scli_email,scli_rcivil,scli_rprenom,scli_rnom,scli_rtelf,scli_rfax,scli_rtelp,scli_remail,scli_rcodefonc,scli_nota,scli_actif,scli_ccpta) VALUES";
                    $rech .= " ('$datea','$codeuser','$datea','$codeuser','$codecli','" . $db->escape($code) . "','" . $db->escape($ste) . "','" . $db->escape($rcs) . "','" . $db->escape($ape) . "','" . $db->escape($tvai) . "','" . $db->escape($adr1) . "','" . $db->escape($adr2) . "','$dep','$ville','$pays','$telf','$fax','$email','$rcivil','" . $db->escape($rprenom) . "','" . $db->escape($rnom) . "','$rtelf','$rfax','$rtelp','$remail','$rfunc','" . $db->escape($nota) . "','$actif','" . $db->escape($ccpta) . "')";
                    break;
                default :
                    $rech = "INSERT INTO `" . TBSCLI . "`";
                    $rech .= " (scli_datecrea,scli_codecrea,scli_datemod,scli_codemod,scli_prop,scli_codecli,scli_code,scli_ste,scli_rcs,scli_ape,scli_tvai,scli_adr1,scli_adr2,scli_dep,scli_ville,scli_codepays,scli_telf,scli_fax,scli_email,scli_rcivil,scli_rprenom,scli_rnom,scli_rtelf,scli_rfax,scli_rtelp,scli_remail,scli_rcodefonc,scli_nota,scli_actif,scli_ccpta) VALUES";
                    $rech .= " ('$datea','$codeuser','$datea','$codeuser','$prop','$codecli','" . $db->escape($code) . "','" . $db->escape($ste) . "','" . $db->escape($rcs) . "','" . $db->escape($ape) . "','" . $db->escape($tvai) . "','" . $db->escape($adr1) . "','" . $db->escape($adr2) . "','$dep','$ville','$pays','$telf','$fax','$email','$rcivil','" . $db->escape($rprenom) . "','" . $db->escape($rnom) . "','$rtelf','$rfax','$rtelp','$remail','$rfunc','" . $db->escape($nota) . "','$actif','" . $db->escape($ccpta) . "')";
                    break;
            }
            $db->query($rech);

            // Ajout traçabilité
            $desc_tra = $desc_tra_default;
            add_trace(2, $codecli, "CRE", $titre_tra, $desc_tra, FALSE);

            $db->disconnect();

            $msg = _("Fiche ajoutée");
            break;

        // MAJ de la fiche Site
        case "edit" :
            $existescliintouv = FALSE;

            // Si on ferme le site, il faut vérifier que rien n'est en cours
            if ($actif == 0) {
                // Vérifie si utilisé dans une fiche intervention en cours
                if (exist_scli_in_int_ouv($idscli) == TRUE) {
                    $existescliintouv = TRUE;
                    $msg = _("Ce site client est déjà utilisé dans une fiche intervention encore ouverte") . '<br />' . _("Vous ne pouvez donc pas le fermer");
                    $retourfiche = FALSE;
                }
            }

            if ($existescliintouv == FALSE) {
                // MAJ de la fiche Site
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

                // On récupère l'enregistrement précédent (traçabilité)
                $rech = "SELECT * FROM `" . TBSCLI . "` WHERE scli_id = '$idscli'";
                $row2 = $db->get_row($rech, ARRAY_A);
                $ex_enrscli = $row2;

                switch (@$_SESSION["niv_log"]) {
                    case 1 :
                    case 6 :
                    case 7 :
                        $rech = "UPDATE `" . TBSCLI . "` SET `scli_datemod`='" . $datea . "', `scli_codemod`='" . $codeuser . "'";
                        $rech .= ", `scli_codecli`='" . $codecli . "', `scli_code`='" . $db->escape($code) . "', `scli_ste`='" . $db->escape($ste) . "', `scli_rcs`='" . $db->escape($rcs) . "', `scli_ape`='" . $db->escape($ape) . "', `scli_tvai`='" . $db->escape($tvai) . "'";
                        $rech .= ", `scli_adr1`='" . $db->escape($adr1) . "', `scli_adr2`='" . $db->escape($adr2) . "', `scli_dep`='" . $dep . "', `scli_ville`='" . $ville . "'";
                        $rech .= ", `scli_codepays`='" . $pays . "', `scli_telf`='" . $telf . "', `scli_fax`='" . $fax . "', `scli_email`='" . $email . "'";
                        $rech .= ", `scli_rcivil`='" . $rcivil . "', `scli_rprenom`='" . $db->escape($rprenom) . "', `scli_rnom`='" . $db->escape($rnom) . "', `scli_rtelf`='" . $rtelf . "'";
                        $rech .= ", `scli_rfax`='" . $rfax . "', `scli_rtelp`='" . $rtelp . "', `scli_remail`='" . $remail . "', `scli_rcodefonc`='" . $rfunc . "'";
                        $rech .= ", `scli_nota`='" . $db->escape($nota) . "', `scli_actif`='" . $actif . "', `scli_ccpta`='" . $db->escape($ccpta) . " WHERE `scli_id` = '" . $idscli . "'";
                        break;
                    default :
                        $rech = "UPDATE `" . TBSCLI . "` SET `scli_datemod`='" . $datea . "', `scli_codemod`='" . $codeuser . "', `scli_prop`='" . $prop . "'";
                        $rech .= ", `scli_codecli`='" . $codecli . "', `scli_code`='" . $db->escape($code) . "', `scli_ste`='" . $db->escape($ste) . "', `scli_rcs`='" . $db->escape($rcs) . "', `scli_ape`='" . $db->escape($ape) . "', `scli_tvai`='" . $db->escape($tvai) . "'";
                        $rech .= ", `scli_adr1`='" . $db->escape($adr1) . "', `scli_adr2`='" . $db->escape($adr2) . "', `scli_dep`='" . $dep . "', `scli_ville`='" . $ville . "'";
                        $rech .= ", `scli_codepays`='" . $pays . "', `scli_telf`='" . $telf . "', `scli_fax`='" . $fax . "', `scli_email`='" . $email . "'";
                        $rech .= ", `scli_rcivil`='" . $rcivil . "', `scli_rprenom`='" . $db->escape($rprenom) . "', `scli_rnom`='" . $db->escape($rnom) . "', `scli_rtelf`='" . $rtelf . "'";
                        $rech .= ", `scli_rfax`='" . $rfax . "', `scli_rtelp`='" . $rtelp . "', `scli_remail`='" . $remail . "', `scli_rcodefonc`='" . $rfunc . "'";
                        $rech .= ", `scli_nota`='" . $db->escape($nota) . "', `scli_actif`='" . $actif . "', `scli_ccpta`='" . $db->escape($ccpta) . "' WHERE `scli_id` = '" . $idscli . "'";
                        break;
                }
                $db->query($rech);

                // On récupère l'enregistrement actuel (traçabilité)
                $rech = "SELECT * FROM `" . TBSCLI . "` WHERE scli_id = '$idscli'";
                $row2 = $db->get_row($rech, ARRAY_A);
                $nouv_enrscli = $row2;

                // On compare les éléments d'enregistrement pour traçabilité
                $trace_added = analyse_trace("scli", $ex_enrscli, $nouv_enrscli, '', FALSE);

                $db->disconnect();

                $msg = _("Fiche modifiée");
            }
            break;

        // Suppression de la fiche Site
        case "del" :
            $existescliint = FALSE;
            $existesclicocl = FALSE;
            $msg = "";

            // Vérifie si utilisé dans une fiche intervention

            if (exist_scli_in_int($idscli) == TRUE) {
                $existescliint = TRUE;
                $msg = _("Ce site client est déjà utilisé dans une fiche intervention") . '<br />' . _("Vous ne pouvez donc pas le supprimer");
                $retourfiche = FALSE;
            }

            // Vérifie si utilisé dans un contrat signé
            if (exist_scli_in_cocl_signe($idscli) == TRUE) {
                $existesclicocl = TRUE;
                if ($msg <> "")
                    $msg .= "<br /><br />";
                $msg = _("Ce site client est déjà utilisé dans un contrat signé") . '<br />' . _("Vous ne pouvez donc pas le supprimer");
                $retourfiche = FALSE;
            }

            if (($existescliint == FALSE) AND ($existesclicocl == FALSE)) {
                // SUPPRESSION de la fiche Site
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

                $rech = "DELETE FROM `" . TBSCLI . "` WHERE `scli_id` = '$idscli'";
                $db->query($rech);
                $db->disconnect();;

                $msg = _("Fiche supprimée");
            }
            break;

        default :
            break;
    }
} else {
    $msg = _("Client principal inexistant");
    $retourfiche = FALSE;
}
?>
<!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 _("Sites Client"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            Focus_first();
        }

        $(document).ready(function () {
            init_all();
        });
        //-->
    </script>

</head>
<body>

<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<p align="center">&nbsp;</p>
<?PHP
if ($retourfiche == FALSE) {
    exec_func_js("warn_txt('" . str_escape($msg) . "')");
    pop_ret_auto('', '', "scli_maj.php?cc=" . $code);
} else
    pop_ret_auto($msg, 'ok', "scli_maj.php?cc=" . $code);
?>
</body>
</html>
