<?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/func.inc.php');
include_once('../inc/func_date.inc.php');
include_once('../inc/func_time.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_cocl.inc.php');
include_once('../inc/fic_droits.inc.php');
include_once('../inc/fic_int.inc.php');
include_once('../inc/fic_tac.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('../inc/func_sql.inc.php');
Archivelog(valsession('code_log'));
$_SESSION['typ_filtres'] = 'cocla';

// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$pbm_cocl = FALSE;

$co = '';
// Récupération code Contrat
if (isset($_GET['co'])) {
    $co = $_GET['co'];
} else {
    $pbm_cocl = TRUE;
}

if ($pbm_cocl == FALSE) {
    if ($co <> '') {
        // Récupération des informations contrat client
        $row = lit_enr_cocl($co);
        if (is_array($row)) {
            extract($row);

            // Récupération des informations client
            $row2 = lit_enr_cli($cocl_codecli);
            if (is_array($row2)) {
                extract($row2);
            }

            // Contrôle qu'un client n'essaye pas de "hacker" un contrat
            $tab_cli = [];
            if (CheckNivLog('1')) {
                $tab_cli = give_tab_rcli($_SESSION['code_log'], TRUE);
            }
            if (exist_in_array($cocl_codecli, $tab_cli) == FALSE) {
                $pbm_cocl = TRUE;
            }
        }

        // Contrôle qu'une agence n'essaye pas de "hacker" un contrat
        if (CheckNivLog('3-4')) {
            $pbm_cocl = TRUE;
        }
    }
}
?>
    <!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 _('Liste Annexes Contrats Clients'); ?></title>

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

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

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

    </head>

    <body>

<?php
include_once('../inc/entete.inc.php');

if ($pbm_cocl == FALSE) {
    $titre_lst = '';
    switch (valsession('niv_log')) {
        case 1 :
            if (trim($co) <> '') {
                $titre_lst = _('VOS ANNEXES AU CONTRAT') . ' ' . $co;
            } else {
                $titre_lst = _('ANNEXES DE VOS CONTRATS');
            }
            $tab_cli = give_tab_rcli($_SESSION['code_log'], TRUE);
            cre_ent_form($titre_lst, '', '', '', FALSE);
            break;
        default :
            if (trim($co) <> '') {
                $titre_lst = _('ANNEXES AU CONTRAT') . ' ' . $co;
            } else {
                $titre_lst = _('ANNEXES DE CONTRATS');
            }
            cre_ent_form($titre_lst, '', '', '', FALSE);
            if (CheckNivLog('3-4')) {
                echo '<p align="center" class="annot_std_it">(' . _('Gérés par votre agence') . ')</p>';
            }
            break;
    }

    switch (valsession('niv_log')) {
        case 1 :
            $fic_imp = 'cocla_lsti.php';
            break;
        case 3 :
        case 4 :
        case 5 :
        case 9 :
            $fic_imp = 'cocla_lsti.php';
            break;
    }

    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');

    $rech = cree_strrechlstcocla($co);
    switch (valsession('niv_log')) {
        case 1 :
            break;
        case 3 :
        case 4 :
            if (!stristr($rech, 'WHERE ')) {
                $rech .= ' WHERE ' . TBCOCL . ".cocl_codeage='" . valsession('code_age') . "'";
            } else {
                $rech .= ' AND ' . TBCOCL . ".cocl_codeage='" . valsession('code_age') . "'";
            }
            break;
        default :
            break;
    }

    $rech .= crestr_triord($cocla_titre_col, 'cocla_code', FALSE, $limit);

    $rows = $db->get_results($rech, ARRAY_A);

    close_database();

    if ($rows) {
        include_once('formr.inc.php');
        $numrows = count($rows);

        $rech = modstr_triord($rech, $cocla_titre_col, 'cocla_code', TRUE, $limit);

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');

        $rows = $db->get_results($rech, ARRAY_A);

        // *** Pagination de pied
        pagine_lst($numrows, $limit);


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

        // création entête
        titlst($cocla_titre_col);
        echo '<th>' . _('Cliquer') . '</th>';

        $i = 0;

        foreach ($rows as $row) {
            $row = encode_str($row);
            // Affichage résumé Annexe Contrat Client

            // création de l'entête de ligne (TR)
            echo '<tr>';

            $echeance_ok = TRUE;
            echo '<td>' . $row['cocla_code'] . ' (' . _('contrat') . ' ' . $row['cocla_codecocl'] . ')';

            // CREATION d'icône si signé
            if (datetolocal($row['cocla_datesig']) <> '') {
                $lst_sig = TRUE;
                echo ' <a href="#" class="popa"><img src="../img/ok.png" alt="' . _('Signée le') . ' ' . datetolocal($row['cocla_datesig']) . '" border="0" align="absmiddle"><span>' . _('Signé le') . ' ' . datetolocal($row['cocla_datesig']) . '</span></a>';
            } else {
                cre_falselnk_img('', 'sig_' . $row['cocla_id'], 'span', 'center', _('Réceptionner'), 'lnk', 'coclreca_maj.php?coa=' . $row['cocla_id'], '../img/coclsig.png');
            }

            echo '</td>';

            // création
            echo '<td>';
            echo '<div align="center">';
            echo datetolocal($row['cocla_datecrea']);
            echo '</div>';
            echo '</td>';
            // relancé le
            echo '<td>';
            echo '<div align="center">';
            if (datetolocal($row['cocla_daterel']) == '') {
                echo '&nbsp;';
            } else {
                echo datetolocal($row['cocla_daterel']);
            }
            echo '</div>';
            echo '</td>';

            // CREATION lien dynamique sur icône (édition, suppression, etc...)
            echo '<td align="center">';

            if (valsession('niv_log') >= 4) // Edition, cloture, et annexes uniquement si >=agence
            {
                if (datetolocal($row['cocla_datesig']) == '') {
                    if (url_ok('cocla_maj.php?td=edit&co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'], FALSE) == TRUE) {
                        $lst_maj = TRUE;
                        cre_falselnk_img('', 'cocla_edit_' . $row['cocla_id'], 'span', 'center', _('Edition'), 'lnk', 'cocla_maj.php?td=edit&co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'], '../img/edit.png');
                    }
                } else {
                    if (url_ok('cocla_maj.php?td=view&co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'], FALSE) == TRUE) {
                        $lst_voir = TRUE;
                        cre_falselnk_img('', 'cocla_view_' . $row['cocla_id'], 'span', 'center', _('Visualisation'), 'lnk', 'cocla_maj.php?td=view&co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'], '../img/eye.png');
                    }
                }
            }

            if (valsession('niv_log') <= 3) // Voir si <=Technicien
            {
                if (url_ok('cocla_maj.php?td=view&co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'], FALSE) == TRUE) {
                    $lst_voir = TRUE;
                    cre_falselnk_img('', 'cocla_view_' . $row['cocla_id'], 'span', 'center', _('Visualisation'), 'lnk', 'cocla_maj.php?td=view&co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'], '../img/eye.png');
                }
            }

            $urlimp = 'cocla_pdf.php?co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'];
            if (url_ok($urlimp, FALSE) == TRUE) {
                $lst_imp = TRUE;
                cre_falselnk_img('', 'cocla_prn_' . $row['cocla_id'], 'span', 'center', _('Imprimer'), 'lnkpop', $urlimp, '../img/print.png');
            }

            $urlmail = 'cocla_pdf.php?co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'] . '&timp=1&tact=cop';
            if (url_ok($urlmail, FALSE) == TRUE) {
                $lst_mail = TRUE;
                if (valsession('niv_log') <> 1) {
                    $title = _('Renvoyer une copie par email au client');
                } else {
                    $title = _('Obtenir une copie par email');
                }
                cre_falselnk_img('', 'cocla_mail_' . $row['cocla_id'], 'span', 'center', $title, 'lnkpop', $urlmail, '../img/mail.png', 1, 1);
            }

            if (valsession('niv_log') >= 4) // Suppression uniquement si >=agence et si pas signé
            {
                if (datetolocal($row['cocla_datesig']) == '') {
                    if (url_ok('cocla_maj.php?td=del&co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'], FALSE) == TRUE) {
                        $lst_del = TRUE;
                        cre_falselnk_img('', 'cocla_del_' . $row['cocla_id'], 'span', 'center', _('Suppression'), 'lnk', 'cocla_maj.php?td=del&co=' . $row['cocla_codecocl'] . '&coa=' . $row['cocla_code'], '../img/del.png');
                    }
                }
            }

            echo '</td>';

            echo '</tr>';
            // Fin affichage résumé Contrat Client

            $i++;
        }
        echo '</table>';
        // *** Fin affichage de la table

        close_database();

        // *** Pagination de pied
        pagine_lst($numrows, $limit);

        $taille_piedla = '';
        include_once('piedla.inc.php');
    } else {
        retpp();
    }
} else {
    pop_ret_auto(_('Problème de paramètres'), 'warn', 'index.php');
}
include_once('pied.php');
?>