<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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) {
    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_date.inc.php');
include_once('../inc/func_fic.inc.php');
include_once('../inc/func_time.inc.php');
include_once('../inc/func_geo.inc.php');
include_once('../inc/func_mail.inc.php');
include_once('../inc/func_txt.inc.php');
include_once('../inc/func_txtcm.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(valsession('code_log'));
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$titre = valpost('titre');

$codecocl = str_majuscules(valpost('codecocl'));
$datesigf = valpost('datesigf');
$datesig = $datesigf;
if ($datesigf <> '') {
    $datesig = datetomysql($datesigf);
}
$signataire = valpost('signataire');

$codeuser = valsession('code_log');

$retourliste = TRUE;

if ($codecocl <> '') {
    // MAJ de la fiche contrat client
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    $rech = 'SELECT * FROM `' . TBCOCL . "` WHERE `cocl_code` =  '" . $db->escape($codecocl) . "'";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        extract($row);
        if (comp_date($cocl_dateenv, $datesig) == TRUE) {
            $upd = 'UPDATE `' . TBCOCL;
            $upd .= "` SET `cocl_datemod`='" . $datea . "', `cocl_codemod`='" . $codeuser . "', `cocl_datesig`='" . $datesig . "',";
            $upd .= "  `cocl_daterec`='" . $datea . "', `cocl_signe`='" . $db->escape($signataire) . "'";
            $upd .= " WHERE `cocl_code` = '" . $db->escape($codecocl) . "'";
            $db->query($upd);

            // Remise à niveau type client
            upd_nivcli($cocl_codecli, FALSE);

            $msg = _('Réception contrat') . ' ' . $codecocl . ' ' . _('effectuée');
        } else {
            $msg = _("Date de réception inférieure à la date d'envoi");
            $retourliste = FALSE;
        }
    } else {
        $msg = _('Contrat inconnu');
        $retourliste = FALSE;
    }
    close_database();
} else {
    $msg = _('Contrat inconnu');
    $retourliste = 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 _('Accusé réception contrat'); ?></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) . "');";}
            ?>
            Focus_first();
        }

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

</head>
<body>

<?php
include_once('../inc/entete.inc.php');
cre_ent_form($titre, '', '', '', FALSE);
if ($retourliste == FALSE) {
    aff_ret_idx_man($td, 'coclrec_maj.php', _('Retour à la saisie'), FALSE);
} else {
    // Envoi d'email vers le client
    envoi_mail('', _('Accusé de réception de votre contrat'), 'con', 'ar', 'xxx', 'cli', $codecocl, '', '', FALSE);
    // Envoi d'email vers l'agence
    envoi_mail('', _('Accusé de réception de contrat'), 'con', 'ar', 'xxx', 'age', $codecocl, '', '', FALSE);
    // Envoi d'email vers le centre de gestion
    envoi_mail('', _('Accusé de réception de contrat'), 'con', 'ar', 'xxx', 'cdg', $codecocl, '', '', TRUE);

    pop_ret_auto($msg, 'ok', 'coclrec_maj.php');
}
include_once('pied.php');
?>
</body>
</html>