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

/****************************************/
/* Fonctions pour les accès aux retours */
/* et fichiers rattachés                */
/****************************************/

// Produits à retourner

/**
 * Nombre d'enregistrements produits à retourner
 *
 * @param bool $open
 * @return null
 */
function nb_retp($open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETP . '`';
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Lecture d'un enregistrement produit à retourner
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */

function lit_enr_retp($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBRETP . "` WHERE `retp_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérifie existence NS dans une fiche produit à retourner
 *
 * @param $ns
 * @param bool $open
 * @return bool
 */
function exist_ns_retp($ns, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETP . "` WHERE `retp_ns` = '" . $db->escape($ns) . "'";
    $nbres = $db->get_var($rech2);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}


/**
 * Vérifie existence intervention dans une fiche produit à retourner
 *
 * @param $nint
 * @param bool $open
 * @return bool
 */
function exist_int_retp($nint, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETP . "` WHERE `retp_codeint` = '" . $db->escape($nint) . "'";
    $nbres = $db->get_var($rech2);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}


// Demandes de retour

/**
 * Nombre d'enregistrements demandes de retour
 *
 * @param bool $open
 * @return null
 */
function nb_retd($open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETD . '`';
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Lecture d'un enregistrement demande de retour pour un produit donné
 *
 * @param $codepdt
 * @param bool $open
 * @return array|null
 */
function lit_enr_retd($codepdt, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBRETD . "` WHERE `retd_coderetp` = '" . $db->escape($codepdt) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Lecture d'un enregistrement demande de retour sur la base d'un ID
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_enr_retd_id($id, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBRETD . "` WHERE `retd_id` = '" . (int)$id . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérifie existence demande de retour pour un produit donné
 *
 * @param $codepdt
 * @param bool $open
 * @return bool
 */
function exist_enr_retd($codepdt, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETD . "` WHERE `retd_coderetp` = '" . $db->escape($codepdt) . "'";
    $nbres = $db->get_var($rech2);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}


// Envois Retours

/**
 * Nombre d'enregistrements envois de retours
 * Pour un code envoi donné ou pour tous
 *
 * @param $codeenv
 * @param bool $open
 * @return null
 */
function nb_rete($codeenv, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETE . '`';
    if (trim($codeenv) <> '') {
        $rech2 .= " WHERE `rete_codeenv` = '" . $db->escape($codeenv) . "'";
    }
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Lecture d'un enregistrement envoi de retour
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_rete($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBRETE . "` WHERE `rete_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Lecture d'un enregistrement envoi de retour sur la base de l'id
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_enr_rete_id($id, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBRETE . "` WHERE `rete_id` = '" . (int)$id . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérifie existence envoi de retour pour un produit donné
 *
 * @param $codepdt
 * @param bool $open
 * @return bool
 */
function exist_enr_rete($codepdt, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETEP . "` WHERE `retep_coderetp` = '" . $db->escape($codepdt) . "'";
    $nbres = $db->get_var($rech2);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérifie existence envoi de retour clos pour un produit donné
 *
 * @param $codepdt
 * @param bool $open
 * @return bool
 */
function exist_enr_rete_clos($codepdt, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT retep_codeenv FROM `' . TBRETEP . "` WHERE `retep_coderetp` = '" . $db->escape($codepdt) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
        $rech3 = 'SELECT COUNT(rete_clos) FROM `' . TBRETE . "` WHERE `rete_code` = '$retep_codeenv' AND `rete_clos` = '1'";
        $nbres = $db->get_var($rech3);
        if ($nbres > 0) {
            $exist = TRUE;
        }
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérifie Nombre de produit dans un envoi
 *
 * @param $coderete
 * @param bool $open
 * @return null
 */
function nb_art_in_envoi($coderete, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETEP . "` WHERE `retep_codeenv` = '" . $db->escape($coderete) . "'";
    $nb = $db->get_var($rech2);
    if ($open == TRUE) {
        close_database();
    }

    return $nb;
}

/**
 * Vérifie Nombre de produit traités dans un envoi
 *
 * @param $coderete
 * @param bool $open
 * @return int
 */
function nb_art_traites_in_envoi($coderete, $open = TRUE)
{
    global
    $db;
    $nb = 0;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT retep_coderetp FROM `' . TBRETEP . "` WHERE `retep_codeenv` = '" . $db->escape($coderete) . "'";
    $rows2 = $db->get_results($rech2, ARRAY_A);
    if ($rows2 AND (count($rows2) > 0)) {
        foreach ($rows2 as $row2) {
            extract($row2);
            $rech3 = 'SELECT COUNT(*) FROM `' . TBRETR . "` WHERE `retr_coderetp` = '$retep_coderetp'";
            $nbres = $db->get_var($rech3);
            if ($nbres > 0) {
                $nb++;
            }
        }
    }
    if ($open == TRUE) {
        close_database();
    }

    return $nb;
}

/**
 * Vérifie si un envoi est clos
 *
 * @param $coderete
 * @param bool $open
 * @return bool
 */
function envoi_clos($coderete, $open = TRUE)
{
    global
    $db;
    $clos = FALSE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT rete_clos FROM `' . TBRETE . "` WHERE `rete_code` = '" . $db->escape($coderete) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
        if ($rete_clos == TRUE) {
            $clos = TRUE;
        }
    }
    if ($open == TRUE) {
        close_database();
    }

    return $clos;
}

/**
 * Donne le nombre de renvois non clos pour un distribueur donné
 *
 * @param $codemque
 * @param bool $open
 * @return null
 */
function nb_envoi_nonclos($codemque, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETE . "` WHERE `rete_codemque` = '" . $db->escape($codemque) . "'";
    $rech2 .= " AND `rete_clos` = '0'";
    $nb = $db->get_var($rech2);
    if ($open == TRUE) {
        close_database();
    }

    return $nb;
}

/**
 * Donne la date d'envoi
 *
 * @param $coderete
 * @param bool $open
 * @return string
 */
function date_envoi($coderete, $open = TRUE)
{
    global
    $db;
    $dateenv = '0000-00-00';

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT rete_dateenv FROM `' . TBRETE . "` WHERE `rete_code` = '" . $db->escape($coderete) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
        $dateenv = $rete_dateenv;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $dateenv;
}

/**
 * Vérifie si un envoi est entièrement soldé
 *
 * @param $coderete
 * @param bool $open
 * @return bool
 */
function envoi_solde($coderete, $open = TRUE)
{
    global
    $db;
    $solde = TRUE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBRETEP . "` WHERE `retep_codeenv` = '" . $db->escape($coderete) . "'";
    $rows2 = $db->get_results($rech2, ARRAY_A);
    if ($rows2 AND (count($rows2) > 0)) {
        foreach ($rows2 as $row2) {
            extract($row2);
            $rech3 = 'SELECT COUNT(*) FROM `' . TBRETR . "` WHERE `retr_coderetp` = '$retep_coderetp' AND `retr_clos` = '1'";
            $nbres = $db->get_var($rech3);
            if ($nbres == 0) {
                $solde = FALSE;
                break;
            }
        }
    } else {
        $solde = FALSE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $solde;
}

// Retours fournisseurs

/**
 * Nombre d'enregistrements retours fournisseur
 *
 * @param bool $open
 * @return null
 */
function nb_retr($open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETR . '`';
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Lecture d'un enregistrement retour fournisseur pour un produit donné
 *
 * @param $codepdt
 * @param bool $open
 * @return array|null
 */
function lit_enr_retr($codepdt, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBRETR . "` WHERE `retr_coderetp` = '" . $db->escape($codepdt) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérifie existence retour pour un produit donné
 *
 * @param $codepdt
 * @param bool $open
 * @return bool
 */
function exist_enr_retr($codepdt, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETR . "` WHERE `retr_coderetp` = '" . $db->escape($codepdt) . "'";
    $nbres = $db->get_var($rech2);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Dit si traitement de retour est clot
 *
 * @param $codepdt
 * @param bool $open
 * @return bool
 */
function clos_retr($codepdt, $open = TRUE)
{
    global
    $db;
    $clos = FALSE;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBRETR . "` WHERE `retr_coderetp` = '" . $db->escape($codepdt) . "' AND `retr_clos` = '1' ";
    $nbres = $db->get_var($rech2);
    if ($nbres > 0) {
        $clos = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $clos;
}


// Moyens d'envoi

/**
 * Lecture d'un enregistrement moyen d'envoi
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_me($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBME . "` WHERE me_code = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Donne le nom d'un moyen d'envoi
 *
 * @param $code
 * @param bool $open
 * @return string
 */
function nom_me($code, $open = TRUE)
{
    global
    $db;
    $me_nom = _('Sans Objet');

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT me_nom FROM `' . TBME . "` WHERE me_code = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        $row2 = encode_str($row2);
        extract($row2);
    }
    if ($open == TRUE) {
        close_database();
    }

    return $me_nom;
}

/**
 * Nombre d'enregistrements moyens d'envoi
 *
 * @param bool $open
 * @return array|null
 */
function nb_me($open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBME . '`';
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

// Types de demandes

/**
 * Lecture d'un enregistrement type de demande
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_md($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBMD . "` WHERE md_code = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Donne le nom d'un type de demande
 *
 * @param $code
 * @param bool $open
 * @return string
 */
function nom_md($code, $open = TRUE)
{
    global
    $db;
    $md_nom = '';

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT md_nom FROM `' . TBMD . "` WHERE md_code = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
    }
    if ($open == TRUE) {
        close_database();
    }

    return $md_nom;
}

/**
 * Nombre d'enregistrements type de demande
 *
 * @param bool $open
 * @return null
 */
function nb_md($open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBMD . '`';
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Création du code Javascript pour affichage des articles de l'envoi
 *
 * @param $id_html
 * @param $td
 * @param $code
 * @param $from
 * @param bool $open
 */
function cre_html_pdte($id_html, $td, $code, $from, $open = TRUE)
{
    global
    $db,
    $_SESSION;

    // Création de la chaine HTML
    $str_html = '';

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }

    if (nb_art_in_envoi($code, TRUE) > 0) {
        if ($open == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        }

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

        // Création entête
        $str_html .= '<th>';
        $str_html .= _('Code');
        $str_html .= '</th>';

        $str_html .= '<th>';
        $str_html .= _('Code Fabr.');
        $str_html .= '</th>';

        $str_html .= '<th>';
        $str_html .= _('Désignation');
        $str_html .= '</th>';

        $str_html .= '<th>';
        $str_html .= _('RMA');
        $str_html .= '</th>';

        // Récupération des données de l'envoi
        $rech = 'SELECT * FROM `' . TBRETE . "` WHERE rete_code='" . $db->escape($code) . "'";
        $row = $db->get_row($rech, ARRAY_A);

        if ($row) {
            $row = encode_str($row);
            extract($row);

            if (($td == 'edit') OR (($td == 'view') AND ($rete_clos == 1))) {
                $str_html .= '<th>';
                $str_html .= _('Cliquer');
                $str_html .= '</th>';
            }

            // Lignes des articles
            $rech2 = 'SELECT * FROM `' . TBRETEP . "` WHERE retep_codeenv='" . $db->escape($code) . "' ORDER BY retep_id";
            $rows2 = $db->get_results($rech2, ARRAY_A);

            $i = 0;

            foreach ($rows2 as $row2) {
                $row2 = encode_str($row2);
                extract($row2);

                $rech3 = 'SELECT * FROM `' . TBRETP . "` WHERE retp_code='" . $retep_coderetp . "'";
                $row3 = $db->get_row($rech3, ARRAY_A);

                if ($row3) {
                    $row3 = encode_str($row3);
                    extract($row3);

                    $rech4 = 'SELECT * FROM `' . TBRETD . "` WHERE retd_coderetp='" . $retep_coderetp . "'";
                    $row4 = $db->get_row($rech4, ARRAY_A);

                    if ($row4) {
                        $row4 = encode_str($row4);
                        extract($row4);

                        // Couleurs alternées
                        $str_html .= '<tr>';

                        // Code produit à retourner
                        $str_html .= '<td class="tdlst" valign="top">';
                        $str_html .= $retp_code;
                        if ($rete_clos <> 0) {
                            $str_html .= ' ';
                            $str_html .= "<img src=\"../img/ok.png\" border=\"0\" align=\"absmiddle\" title=\"" . _('Produit traité') . "\"></a>";
                        }
                        $str_html .= '</td>';

                        // Code fabricant
                        $str_html .= '<td class="tdlst" valign="top">';
                        $str_html .= $retd_codepdtm;
                        $str_html .= '</td>';

                        // Désignation
                        $str_html .= '<td class="tdlst" valign="top">';
                        $str_html .= $retp_des;

                        // Affichage ligne(s) du commentaire
                        if (trim($retp_ns) <> '') {
                            $str_html .= '<div class="annot_moy_it">';
                            $str_html .= ' ' . _('Sur') . ' ' . $desart;
                            $str_html .= _('N/S') . ' : ' . $retp_ns;
                            $str_html .= '</div>';
                        }
                        $str_html .= '</td>';

                        // RMA
                        $str_html .= '<td class="tdlst" valign="top">';
                        $str_html .= $retd_rma;
                        $str_html .= '</td>';

                        if ($td == 'edit') {
                            $urldelpdt = 'retep_enr.php?td=del&tdo=' . $td . '&ce=' . $code . '&cp=' . $retep_coderetp;
                            $str_html .= '<td class="tdlst" valign="top" align="center">';
                            $str_html .= '<a href="javascript:;" onclick="init_new_window(\'' . $urldelpdt . '\',\'retep_del_' . $retep_id . '\',0,0,\'yes\')")>';
                            $str_html .= '<img src="../img/del.png" border="0" align="absmiddle" title="' . _('Suppression') . '"></a>';
                            $str_html .= '</td>';
                        } else {
                            if ($rete_clos == 1) {
                                $rech5 = 'SELECT * FROM `' . TBRETR . "` WHERE retr_coderetp='" . $retep_coderetp . "'";
                                $row5 = $db->get_row($rech5, ARRAY_A);

                                $clos_delok = FALSE;
                                if ($row5) {
                                    $row5 = encode_str($row5);
                                    extract($row5);

                                    if ($retr_clos == 0) {
                                        $urlretpdt = 'retr_maj.php?td=edit&tdo=' . $td . '&cpr=' . $retep_coderetp . '&ce=' . $code . '&tx=e';
                                        $txt = _('Editer retour');
                                        $nimg = 'edit.png';
                                        $clos_delok = TRUE;
                                    } else {
                                        $urlretpdt = 'retr_maj.php?td=view&tdo=' . $td . '&cpr=' . $retep_coderetp . '&ce=' . $code . '&tx=e';
                                        $txt = _('Voir retour');
                                        $nimg = 'eye.png';
                                    }
                                } else {
                                    $urlretpdt = 'retr_maj.php?td=add&tdo=' . $td . '&cpr=' . $retep_coderetp . '&ce=' . $code . '&tx=e';
                                    $txt = _('Traiter retour');
                                    $nimg = 'retr.png';
                                }

                                if ($urlretpdt <> '') {
                                    $str_html .= '<td class="tdlst" valign="top" align="center">';
                                    $str_html .= '<a href="javascript:;" onclick="init_new_window(\'' . $urlretpdt . '\',\'retr_edit_' . $retep_id . '\',1024,650,\'yes\')")>';
                                    $str_html .= '<img src="../img/' . $nimg . '" border="0" align="absmiddle" title="' . $txt . '"></a>';
                                    if ($clos_delok == TRUE) {
                                        $urldelpdt = 'retr_maj.php?td=del&tdo=' . $td . '&cpr=' . $retep_coderetp . '&ce=' . $code . '&tx=e';
                                        //$str_html .= cre_falselnk_img('', 'retr_del_' . $retep_id, 'span', '', _('Suppression'), 'lnkpop', $urldelpdt, '../img/del.png', 1024, 650, true);
                                        $str_html .= '<a href="javascript:;" onclick="init_new_window(\'' . $urldelpdt . '\',\'retr_del_' . $retep_id . '\',1024,650,\'yes\')")>';
                                        $str_html .= '<img src="../img/del.png" border="0" align="absmiddle" title="' . _('Suppression') . '"></a>';
                                    }
                                    $str_html .= '</td>';
                                } else {
                                    $str_html .= '<td class="tdlst" valign="top" align="center">';
                                    $str_html .= ' ';
                                    $str_html .= '</td>';
                                }
                            }
                        }

                        $str_html .= '</tr>';
                        $i++;
                    }
                }
            }
        }

        // Fermeture de la table
        $str_html .= '</table>';
    }
    if ($open == TRUE) {
        close_database();
    }


    echo 'function updatenv() {';
    echo 'if ((' . $from . '$("#' . $id_html . '").length) > 0) {';
    echo $from . "$('#" . $id_html . "').html('" . str_replace("'", "\'", $str_html) . "');";
    echo '}';
    echo '}';
}


/**
 * Création du code Javascript pour affichage des moyens d'envoi
 *
 * @param string $from
 * @param string $id_sel
 * @param bool $open
 */
function cre_sel_me($from = '', $id_sel = '', $open = TRUE)
{
    global
    $db;

    echo 'function update_sel_me() {';
    echo 'var id_sel = ' . $from . '$(\'#' . $id_sel . '\');';
    echo 'var val_act = id_sel.val();';
    echo 'id_sel.empty();';
    echo 'id_sel.append(\'<option value="">' . _('-- Faites votre choix --') . '</option>\');';
    echo 'id_sel.append(\'<option value="-1">' . _('Sans objet') . '</option>\');';
    // On se connecte a la base
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }

    $rech = 'SELECT me_code,me_nom FROM `' . TBME . '` ORDER BY me_nom';
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows AND (count($rows) > 0)) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            echo 'id_sel.append(\'<option value="' . $me_code . '">' . str_escape($me_nom) . '</option>\');';
        }
    }
    if ($open == TRUE) {
        close_database();
    }

    echo 'id_sel.val(val_act);';
    echo '}';
}
