<?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/fic_age.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_time.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/func_txtcm.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../classes/phpmailer/class.smtp.php");
include_once("../inc/mail.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);

$titre = @$_POST["titre"];
$td = @$_POST["td"];

$codecli = str_majuscules(@$_POST["codecli"]);
$codecocl = str_majuscules(@$_POST["code"]);
$code = str_majuscules(@$_POST["codea"]);
$datecrea = datetomysql(@$_POST["datecreaf"]);
$sitep = 0;
if (strtolower(@$_POST["sitep"]) == "sitep")
    $sitep = 1;
$rlp_choisis = $_SESSION["rlp_choisis"];
$matos = @$_POST["matos"];
$pu = floatval(@$_POST["pu"]);
$tva = @$_POST["tva"];
$datesig = '';
if (@$_POST["datesigf"] <> "")
    $datesig = datetomysql(@$_POST["datesigf"]);
$signe = @$_POST["signe"];

$tab_scli = [];
$tab_scli = explode('-', $_SESSION["sclia_choisis"]);

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

$enr_ok = TRUE;
$pcli = FALSE;
$retourliste = TRUE;
$enrimp = FALSE;

if (@$_POST["nbutton"] == "enrimp") {
    $enrimp = TRUE;
    $retourliste = FALSE;
}

switch ($td) {
    // Ajout de la fiche Annexe Contrat Client
    case "add" :
        if ((count($tab_scli) == 0) AND ($sitep == 0)) {
            $enr_ok = FALSE;
            $msg = _("Il faut impérativement choisir au moins un site pour cette annexe");
            $retourliste = FALSE;
            $enrimp = FALSE;
        } else {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "SELECT COUNT(*) FROM `" . TBCOCLA . "` WHERE `cocla_code` = '" . $db->escape($code) . "' AND `cocla_codecocl` = '$codecocl'";
            $nbres = $db->get_var($rech);
            if ($nbres == 0) {
                $rech = "INSERT INTO `" . TBCOCLA . "`";
                $rech .= " (cocla_datecrea,cocla_codecrea,cocla_datemod,cocla_codemod,cocla_dateenv,";
                $rech .= "cocla_datesig,cocla_daterec,cocla_daterel,cocla_nivrel,cocla_codecocl,cocla_code,";
                $rech .= "cocla_sitep,cocla_signe,cocla_matos,cocla_pvht,cocla_coderlp)";
                $rech .= " VALUES";
                $rech .= " ('$datecrea','$codeuser','$datea','$codeuser','$datea','',";
                $rech .= " '','','','$codecocl','" . $db->escape($code) . "',";
                $rech .= "'$sitep','','" . $db->escape($matos) . "','$pu','$rlp_choisis')";
                $db->query($rech);
                $id = $db->insert_id;

                // Balayage et activation des sites sélectionnés
                foreach ($tab_scli as $key => $value) {
                    if (intval($value) <> 0) {
                        $rech = "INSERT INTO `" . TBSCLIA . "`";
                        $rech .= " (sclia_datecrea,sclia_datemod,sclia_idscli,sclia_idcocla,sclia_codecocl)";
                        $rech .= " VALUES";
                        $rech .= " ('$datecrea','$datea','$value','$id','$codecocl')";
                        $db->query($rech);
                    }
                }

                $db->disconnect();

                $msg = _("Annexe Contrat"). ' ' . $codecocl . ' - ' . $code . ' ' ._("créée");
                /*
                                if ($retourliste == TRUE)
                                {
                                    // Envoi d'email vers le client
                                    if ($prev==0)
                                        envoi_mail("","création de votre contrat ".$codeco,"con","cre","xxx","cli",$codeco,"","",FALSE);
                                    else
                                    {
                                        if ($prev<>4) // Le niveau 4 est "Ne pas prévenir"
                                        {
                                            $pcli=TRUE;
                                            $txtcorps=cre_corps_txtc("con","cre","xxx","cli",$codeco,"","");
                                            $_SESSION["txt"] = $txtcorps;
                                        }
                                        // Envoi d'email vers le webmaster
                                        envoi_mailwm("","création d'un contrat","con","cre","xxx","cli",$codeco,"","");
                                    }
                                    // Envoi d'email vers l'agence
                                    if (@$_SESSION["niv_log"] <> 4)
                                        envoi_mail("","création de contrat","con","cre","xxx","age",$codeco,"","",FALSE);
                                    // Envoi d'email vers le centre de gestion
                                    envoi_mail("","création de contrat","con","cre","xxx","cdg",$codeco,"","",TRUE);
                                }
                */
            } else {
                $msg = _("Ce code existe déjà");
                $retourliste = FALSE;
                $enrimp = FALSE;
                $enrmail = FALSE;
            }
        }
        break;

    // MAJ de la fiche Annexe Contrat Client
    case "edit" :
        if ((count($tab_scli) == 0) AND ($sitep == 0)) {
            $enr_ok = FALSE;
            $msg = _("Il faut impérativement choisir au moins un site pour cette annexe");
            $retourliste = FALSE;
            $enrimp = FALSE;
        } else {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "UPDATE `" . TBCOCLA;
            $rech .= "` SET `cocla_datemod`='" . $datea . "', `cocla_codemod`='" . $codeuser . "',";
            $rech .= " `cocla_signe`='" . $db->escape($signe) . "',";
            $rech .= " `cocla_sitep`='" . $sitep . "', `cocla_matos`='" . $db->escape($matos) . "', `cocla_pvht`='" . $pu . "',";
            $rech .= " `cocla_coderlp`='" . $rlp_choisis . "'";
            $rech .= " WHERE `cocla_code` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // Mise à jour des sites sélectionnés

            // On récupère l'ID
            $rech2 = "SELECT cocla_id FROM `" . TBCOCLA . "` WHERE `cocla_code` = '" . $db->escape($code) . "' AND `cocla_codecocl` = '" . $db->escape($codecocl) . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                extract($row2);
                // On vide d'abord tout
                $rech = "DELETE FROM `" . TBSCLIA . "` WHERE `sclia_idcocla`= '" . $cocla_id . "' AND `sclia_codecocl`= '" . $db->escape($codecocl) . "'";
                $db->query($rech);

                foreach ($tab_scli as $key => $value) {
                    if (intval($value) <> 0) {
                        $rech = "INSERT INTO `" . TBSCLIA . "`";
                        $rech .= " (sclia_datecrea,sclia_datemod,sclia_idscli,sclia_idcocla,sclia_codecocl)";
                        $rech .= " VALUES";
                        $rech .= " ('$datecrea','$datea','$value','$cocla_id','" . $db->escape($codecocl) . "')";
                        $db->query($rech);
                    }
                }
            }

            $db->disconnect();
            /*
                        if ($retourliste == TRUE)
                        {
                            if ($datefin <> "") // Si date de clôture, on prévient le client
                            {
                                // Envoi d'email vers le client
                                if ($prev==0)
                                    envoi_mail("","Clôture de votre contrat ".$code,"con","clo","xxx","cli",$code,"","",FALSE);
                                else
                                {
                                    if ($prev<>4) // Le niveau 4 est "Ne pas prévenir"
                                    {
                                        $pcli=TRUE;
                                        $txtcorps=cre_corps_txtc("con","cli","xxx","cli",$code,"","");
                                        $_SESSION["txt"] = $txtcorps;
                                    }
                                    envoi_mailwm("","Clôture du contrat ".$code,"con","clo","xxx","cli",$code,"","");
                                }
                                // Envoi d'email vers l'agence
                                if (@$_SESSION["niv_log"] <> 4)
                                    envoi_mail("","Clôture du contrat ".$code,"con","clo","xxx","age",$code,"","",FALSE);
                                // Envoi d'email vers le centre de gestion
                                envoi_mail("","Clôture du contrat ".$code,"con","clo","xxx","cdg",$code,"","",TRUE);
                            }
                            else // Si pas de date de clôture, on le prévient simplement de la modification
                            {
                                // Envoi d'email vers le client
                                if ($prev==0)
                                    envoi_mail("","Modification de votre contrat ".$code,"con","mod","xxx","cli",$code,"","",FALSE);
                                else
                                {
                                    if ($prev<>4) // Le niveau 4 est "Ne pas prévenir"
                                    {
                                        $pcli=TRUE;
                                        $txtcorps=cre_corps_txtc("con","cli","xxx","cli",$code,"","");
                                        $_SESSION["txt"] = $txtcorps;
                                    }
                                    // Envoi d'email vers le webmaster
                                    envoi_mailwm("","Modification du contrat ".$code,"con","mod","xxx","cli",$code,"","");
                                }
                                // Envoi d'email vers l'agence
                                if (@$_SESSION["niv_log"] <> 4)
                                    envoi_mail("","Modification du contrat ".$code,"con","mod","xxx","age",$code,"","",FALSE);
                                // Envoi d'email vers le centre de gestion
                                envoi_mail("","Modification du contrat ".$code,"con","mod","xxx","cdg",$code,"","",FALSE);
                            }
                        }
            */
            $msg = _("Annexe contrat"). ' ' . $codecocl . ' - ' . $code . ' ' ._("modifiée");
        }
        break;

    // Suppression de la fiche Annexe Contrat Client
    case "del" :
        if (trim($datesig) == "") {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

            // On récupère l'ID
            $rech2 = "SELECT cocla_id FROM `" . TBCOCLA . "` WHERE `cocla_code` = '" . $db->escape($code) . "' AND `cocla_codecocl` = '" . $db->escape($codecocl) . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                extract($row2);
                // On supprime l'annexe contrat
                $rech = "DELETE FROM `" . TBCOCLA . "` WHERE `cocla_id` = '$cocla_id'";
                $db->query($rech);

                // On vide tout
                $rech = "DELETE FROM `" . TBSCLIA . "` WHERE `sclia_idcocla`= '" . $cocla_id . "' AND `sclia_codecocl`= '" . $db->escape($codecocl) . "'";
                $db->query($rech);
            }

            $db->disconnect();

            $msg = _("Annexe contrat"). ' ' . $codecocl . ' - ' . $code . ' ' ;_("supprimée");
        } else {
            $msg = "Impossible de supprimer une annexe contrat signée.";
            $retourliste = FALSE;
            $enrimp = 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 _("Fiche Annexe Contrat Client"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            <?PHP
            if ($retourliste == FALSE)
                echo "warn_txt('" . str_escape($msg) . "');";
            if ($pcli == TRUE)
               echo 'fen_prev_cli('.$prev.');';
            ?>
            init_new_fen('imprim','impression','click');
            Focus_first();
        }

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

</head>
<body>

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

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

<p align="center">&nbsp;</p>
<?PHP
if ($retourliste == FALSE) {
    if ($enrimp == TRUE) {
        $urlimp = "cocla_pdf.php?code=" . $code . "&timp=0";
        if (url_ok($urlimp) == TRUE) {
            echo '<div id="imprim" align="center" title="' . $urlimp . '" class="falselnk">';
            echo '<img src="../img/print.png" title="' . _("Imprimer") . '" border="0" align="absmiddle"> ' . _("Imprimer");
            echo '</div>';
        }
    }

    if (nb_cocla_cocl($codecocl, TRUE) > 0)
        aff_ret_idx_man($td, "cocla_lst.php?co=" . $codecocl, _("Retour à la liste"), TRUE);
    else
        aff_ret_idx_man($td, "cocl_lst.php?ord=DES&tri=Code", _("Retour à la liste"), TRUE);
} else {
    if (nb_cocla_cocl($codecocl, TRUE) > 0)
        pop_ret_auto($msg, 'ok', "cocla_lst.php?co=" . $codecocl);
    else
        pop_ret_auto($msg, 'ok', "cocl_lst.php?ord=DES&tri=Code");
}
include_once("pied.php");
?>
</body>
</html>