<?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_geo.inc.php');
include_once('../inc/func_txt.inc.php');
include_once('../inc/fic_droits.inc.php');
include_once('../inc/fic_prm.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'] = 'pdt';

fix_magic_quotes();
$_POST = clean_str($_POST);

$ta = $_GET['ta']; // Source de l'appel m:depuis le menu, i:depuis l'intervention
$td = $_GET['td']; // Récupération type demande
$cc = $_GET['cc']; // Récupération code Client
$ci = $_GET['ci']; // Récupération code Intervention
$cih = (int)$_GET['cih']; // Récupération ID Historique Intervention
$ca = $_GET['ca']; // Récupération code agence

$pbm_pdt = FALSE;

if (($ta <> 'i') AND ($ta <> 'ih') AND ($ta <> 'm')) {
    $pbm_pdt = TRUE;
} else {
    $codeadev = give_dev_codea_defaut($_SESSION['lng'], 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 Articles'); ?></title>

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

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

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

</head>

<body>
<?php
if ($ta == 'm') {
    include_once('../inc/entete.inc.php');
}
cre_ent_form(_('LISTE DES ARTICLES'), '', '', '', FALSE);

if ($pbm_pdt == FALSE) {

    switch ($ta) {
        case 'i' :
        case 'ih' :
            echo '<div align="center">';
            echo '<span class="titre_gras">' . _("Choisissez un article pour l'intervention") . ' ' . $ci . ' ' . _('dans la liste suivante') . '</span><br />';
            echo '<span class="annot_petit_it">' . _('Hors article(s) déjà saisi(s) dans cette intervention') . '</span>';
            echo '</div>';
            echo '<p align="center">';
            echo '<input type="button" class="bton_std" name="Submit" id="Submit" value="' . _('Revenir à la saisie en cours') . '">';
            echo '</p>';
            break;
        default :
            if (valsession('niv_log') >= 5) {
                $url_add = 'pdt_maj.php?td=add&code=';
            }
            break;
    }

    $fic_imp = 'pdt_lsti.php';

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

    switch ($ta) {
        case 'i' :
        case 'ih' :
            $rech = cree_strrechlstrpdti();
            $rech .= crestr_triord($pdtint_titre_col, 'pdt_code', FALSE, $limit);
            break;
        default :
            $rech = cree_strrechlstrpdt();
            $rech .= crestr_triord($pdt_titre_col, 'pdt_code', FALSE, $limit);
            break;
    }

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

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

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

        switch ($ta) {
            case 'i' :
            case 'ih' :
                $rech = modstr_triord($rech, $pdtint_titre_col, 'pdt_code', TRUE, $limit);
                break;
            default :
                $rech = modstr_triord($rech, $pdt_titre_col, 'pdt_code', TRUE, $limit);
                break;
        }

        $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

        switch ($ta) {
            case 'i' :
            case 'ih' :
                titlst($pdtint_titre_col);
                break;
            default :
                titlst($pdt_titre_col);
                break;
        }

        echo '<th>' . _('Cliquer') . '</th>';

        $i = 0;

        foreach ($rows as $row) {
            $row = encode_str($row);
            $affichage_ok = TRUE;

            switch ($ta) {
                case 'i' :
                    $rech2 = 'SELECT COUNT(*) FROM `' . TBPDTI . "` WHERE `pdti_codepdt` = '" . str_escape($row['pdt_code']) . "' AND `pdti_codeint` = '" . $db->escape($ci) . "'";
                    $nbres2 = $db->get_var($rech2);
                    if ($nbres2 > 0) {
                        $affichage_ok = FALSE;
                    }
                    break;
                case 'ih' :
                    $rech2 = 'SELECT * FROM `' . TBPDTIH . "` WHERE `pdtih_codepdt` = '" . str_escape($row['pdt_code']) . "' AND `pdtih_codeint` = '" . $db->escape($ci) . "' AND `pdtih_idhisto` = '" . $cih . "'";
                    $nbres2 = $db->get_var($rech2);
                    if ($nbres2 > 0) {
                        $affichage_ok = FALSE;
                    }
                    break;
                default :
                    break;
            }

            if ($affichage_ok == TRUE) {
                // création de l'entête de ligne (TR)
                echo '<tr>';

                echo '<td>' . $row['pdt_code'] . '</td>';
                echo '<td>';
                echo $row['pdt_fam'];

                // détail famille
                echo '<br />';
                echo '<span class="annot_petit_it">';
                $txt = '(' . formatetxt($row['fam_desc']) . ')';
                echo $txt;
                echo '</span>';

                echo '</td>';
                echo '<td>';
                echo $row['pdt_des'];
                if ($row['pdt_detail'] <> '') {
                    echo '<br />';
                    echo '<span class="annot_petit_it">';
                    echo $row['pdt_detail'];
                    echo '</span>';
                }
                echo '</td>';
                echo '<td>';
                echo '<div align="right">';
                echo full_num_format($row['pdt_puttc'], '', '', TRUE, '', FALSE);
                echo '</div>';
                echo '</td>';
                switch ($ta) {
                    case 'i' :
                    case 'ih' :
                        break;
                    default :
                        echo '<td>';
                        echo '<div align="center">';
                        echo $non_oui[$row['pdt_vendu']];
                        echo '</div>';
                        echo '</td>';
                        break;
                }

                // CREATION lien dynamique sur icône (édition, suppression, etc...)
                echo '<td>';
                echo '<div align="center">';
                switch ($ta) {
                    case 'i' :
                        $lst_int = TRUE;
                        cre_falselnk_img('', 'pdti_edit_' . $row['pdt_id'], 'span', 'center', _('Ajout'), 'lnk', 'pdti_maj.php?td=add&ci=' . $ci . '&ca=' . $row['pdt_code'], '../img/int.png');
                        break;
                    case 'ih' :
                        $lst_int = TRUE;
                        cre_falselnk_img('', 'pdti_edit_' . $row['pdt_id'], 'span', 'center', _('Ajout'), 'lnk', 'pdtih_maj.php?td=add&ci=' . $ci . '&cih=' . $cih . '&ca=' . $row['pdt_code'], '../img/int.png');
                        break;
                    default :
                        if (valsession('niv_log') >= 5) {
                            $lst_maj = TRUE;
                            cre_falselnk_img('', 'pdt_edit_' . $row['pdt_id'], 'span', 'center', _('Edition'), 'lnk', 'pdt_maj.php?td=edit&code=' . $row['pdt_code'], '../img/edit.png');
                            $lst_del = TRUE;
                            cre_falselnk_img('', 'pdt_del_' . $row['pdt_id'], 'span', 'center', _('Suppression'), 'lnk', 'pdt_maj.php?td=del&code=' . $row['pdt_code'], '../img/del.png');
                        }
                        if (CheckNivLog('3-4')) {
                            $lst_voir = TRUE;
                            cre_falselnk_img('', 'pdt_view_' . $row['pdt_id'], 'span', 'center', _('Visualisation'), 'lnk', 'pdt_maj.php?td=view&code=' . $row['pdt_code'], '../img/eye.png');
                        }
                        break;
                }
                echo '</div>';
                echo '</td>';

                echo '</tr>';

                $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');
}

if ($ta == 'm') {
    include_once('pied.php');
}
?>
</body>
</html>