<?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_int.inc.php');
include_once('../inc/fic_st.inc.php');
include_once('../inc/fic_ope.inc.php');
include_once('../inc/fic_prm.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_fic.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(valsession('code_log'));
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$coderech = $_GET['code']; // Récupération code Contrat

$pbm_cocl = FALSE;

if ($coderech == '') {
    $pbm_cocl = TRUE;
}

// Niveau =2 interdit
if (CheckNivLog('2')) {
    $pbm_cocl = TRUE;
}

ob_end_clean();
if ($pbm_cocl == FALSE) {
    // Récupération des informations contrat client
    $row = lit_enr_cocl($coderech);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
        unset($row);
        // Contrôle qu'une agence n'essaye pas de "hacker" un contrat
        if (CheckNivLog('3-4') AND ($cocl_codeage <> valsession('code_age'))) {
            $pbm_cocl = TRUE;
        }

        if ($pbm_cocl == FALSE) {
            $co_cgv = '';
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            $rech2 = 'SELECT co_cgv FROM `' . TBCO . "` WHERE `co_code` = '" . $db->escape($cocl_codeco) . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            close_database();
            if ($row2) {
                $row2 = encode_str($row2);
                extract($row2);
            }

            include_once('../classes/tcpdf/config/tcpdf_config.php');
            include_once('../classes/tcpdf/tcpdf.php');
            include_once('../inc/tcpdf_ext.inc.php');

            $pdf = new PDF_SMI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
            $pdf->setFontLng('fr');
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('SMI');
            $pdf->SetTitle(_('Contrat N°') . $cocl_code);
            $pdf->SetSubject(_('Contrat N°') . $cocl_code);
            $pdf->settgen();
            $pdf->settheader('cocl');

            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

            //set margins
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
            $pdf->setHeaderMargin(PDF_MARGIN_HEADER);
            $pdf->setFooterMargin(PDF_MARGIN_FOOTER);

            //set auto page breaks
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

            $txt_texte = formatetxt($co_cgv);
            $txt_cocl = parse_txtcorps('age', $cocl_codeage, $txt_texte, '');
            $txt_cocl = parse_txtcorps('cdg', '', $txt_cocl, '');
            $txt_cocl = parse_txtcorps('cli', $cocl_codecli, $txt_cocl, '');
            $txt_cocl = parse_txtcorps('con', $cocl_code, $txt_cocl, '');

            $pdf->AddPage();
            $pdf->writeHTML($txt_cocl, true, false, true);

            $url_pdf = $url_gfc . '/pj/' . _('Contrat') . '_' . $cocl_code . '.pdf';
            $typgen = (int)$pdf->tgen;
            $tact = strtolower($pdf->tact);

            switch ($typgen) {
                // Sortie vers fichier stocké et envoi email
                case 1 :
                    $pdf->Output($url_pdf, 'F');
                    $sujet = '';
                    $type_ope = '';
                    switch ($tact) {
                        case 'add' :
                            $sujet = _('Création de votre contrat') . ' ' . $cocl_code;
                            $type_ope = 'cre';
                            break;
                        case 'edit' :
                            if (datetolocal($cocl_datefin) <> '') {
                                $sujet = _('Clôture de votre contrat') . ' ' . $cocl_code;
                                $type_ope = 'clo';
                            } else {
                                $sujet = _('Modification de votre contrat') . ' ' . $cocl_code;
                                $type_ope = 'mod';
                            }
                            break;
                        case 'cop' :
                            $sujet = _('Copie du contrat') . ' ' . $cocl_code;
                            $type_ope = 'cop';
                            break;
                    }

                    envoi_mail('', $sujet, 'con', $type_ope, 'xxx', 'cli', $cocl_code, '', '', TRUE, $url_pdf);
                    break;
                // Sortie classique vers navigateur
                default :
                    //Détermination d'un nom de fichier temporaire dans le répertoire courant
                    $pdf->Output($url_pdf, 'FI');
                    break;
            }
            killfile($url_pdf);
        }
    } else {
        $pbm_cocl = TRUE;
    }

}

if ($pbm_cocl == TRUE) {
    close_fen_auto('', '');
} else {
    if ($typgen == 1) {
        close_fen_auto('', '');
    }
}
