<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  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)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_mque.inc.php");
include_once("../inc/fic_ret.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_geo.inc.php");
include_once("../inc/func_txt.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(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$td = $_GET['td']; // Récupération type demande
$ce = $_GET['ce']; // Récupération code Envoi
$cm = $_GET['cm']; // Récupération code Marque
$ca = $_GET['ca']; // Récupération code Agence

$pbm_rete = FALSE;
$sais_dteenv = TRUE;
$nbartrete = 0;

if ($ca <> "") {
    // Contrôle qu'une agence n'essaye pas de "hacker" un contrat
    if ((@$_SESSION["niv_log"] == 4) AND ($ca <> @$_SESSION["code_age"]))
        $pbm_rete = TRUE;
    $rete_codeage = $ca;
}

if ($ce <> "") {
    // vérification qu'il existe bien des articles dans l'intervention
    $nbartrete = nb_art_in_envoi($ce, TRUE);

    // Récupération des informations envoi
    $row = lit_enr_rete($ce, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        $row = lit_enr_mque($rete_codemque, TRUE);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            if ($td <> "add") {
                // Contrôle qu'une agence n'essaye pas de "hacker" un envoi
                if ((@$_SESSION["niv_log"] == 4) AND ($rete_codeage <> @$_SESSION["code_age"]))
                    $pbm_rete = TRUE;
            }

            // Formatage des dates en français
            $rete_datecreaf = datetolocal($rete_datecrea);
            $rete_dateenvf = datetolocal($rete_dateenv);

            // Si envoi soldé, on force le mode "view"
            if ((envoi_solde($rete_code, TRUE) == TRUE) OR ($rete_clos == 1))
                $td = "view";

            if ($td == "edit") {
                $cm = $rete_codemque;
                $ca = $rete_codeage;
            }

            if ($td == "add") {
                $rete_datecreaf = $datef;
                $rete_dateenvf = $datef;
                $rete_code = $ce;
                $rete_codemque = $cm;
                $rete_codeage = $ca;
                $rete_nbcol = 1;
            }
        } else
            $pbm_rete = TRUE;
    } else
        $pbm_rete = TRUE;
} else {
    if ($td == "add") {
        $row = lit_enr_mque($cm, TRUE);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            $rete_datecreaf = $datef;
            $rete_dateenvf = $datef;
            $rete_code = crecodesession("ER");
            $rete_codemque = $cm;
            $rete_nbcol = 1;
            $ce = $rete_code;
        } else
            $pbm_rete = TRUE;
    } else
        $pbm_rete = TRUE;
}

switch ($td) {
    case "add" :
        $titre = _("SAISIE FICHE DE RENVOI");
        break;
    case "edit" :
        $titre = _("MISE A JOUR FICHE DE RENVOI");
        break;
    case "view" :
        $titre = _("VISUALISATION FICHE DE RENVOI");
        $sais_dteenv = FALSE;
        break;
    case "del":
        $titre = _("SUPPRESSION FICHE DE RENVOI");
        $sais_dteenv = FALSE;
        break;
    default:
        $titre = _("PROBLEME");
        $pbm_rete = TRUE;
        break;
}

if ((@$_SESSION["niv_log"] <> 5) AND (@$_SESSION["niv_log"] <> 9)) {
    $lst_id_readonly = 'codeage';
    $rete_codeage = @$_SESSION["code_age"];
}

?>
<!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 _("Fiche envoi de produits"); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:datecreaf ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date demande"); ?> ; stok:frmok ; stbad:frmbad",
            "nom:dateenvf ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date envoi"); ?> ; stok:frmok ; stbad:frmbad",
            "nom:codemque; format:liste ; req:Y ; txt:<?PHP echo _("Destinataire"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:codeage; format:liste ; req:Y ; txt:<?PHP echo _("Agence"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:codeme; format:liste ; req:Y ; txt:<?PHP echo _("Moyen d'expédition"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:bord; format:txt ; req:N ; txt:<?PHP echo _("Numéro de bordereau"); ?> ; stok:frmok ; stbad:frmbad",
            "nom:nbcol; format:unsigned ; req:Y ; txt:<?PHP echo _("Nombre de colis"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>


    <script type="text/javascript">
        <!--
        <?PHP cre_html_pdte('detpdte', $td, $rete_code, ''); ?>

        function init_datepicker() {
            $.datepicker.setDefaults({
                closeText: "<?PHP echo _("Fermer"); ?>",
                prevText: "<?PHP echo _("Précédent"); ?>",
                nextText: "<?PHP echo _("Suivant"); ?>",
                currentText: "<?PHP echo _("Aujourd'hui"); ?>",
                monthNames: ["<?PHP echo implode('","',$lng_tmoisf); ?>"],
                monthNamesShort: ["<?PHP echo implode('","',$lng_tmoism); ?>"],
                dayNames: ["<?PHP echo implode('","',$lng_tjourf); ?>"],
                dayNamesShort: ["<?PHP echo implode('","',$lng_tjourm); ?>"],
                dayNamesMin: ["<?PHP echo implode('","',$lng_tjours); ?>"],
                weekHeader: "<?PHP echo _("Sem."); ?>",
                dateFormat: "<?PHP echo $_SESSION['date_fmtshort']; ?>",
                firstDay: 1,
                isRTL: !1,
                changeMonth: true,
                changeYear: true,
                showMonthAfterYear: !1,
                yearSuffix: "",
                showButtonPanel: true,
                showOn: "both",
                buttonImageOnly: true,
                buttonImage: "../img/calendar.png",
                buttonText: "<?PHP echo _("Calendrier"); ?>"
            });
            <?PHP if ($sais_dteenv == TRUE) { ?>
            $("#dateenvf").datepicker({
                onSelect: function (datesel) {
                    $("#dateenvf").blur();
                    chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', true, 'datecreaf', '', 'today',false);
                }
            });
            <?PHP }
            ?>
        }

        function init_all() {
            init_Body();
            init_fdeci('nbcol', 0,'');
            init_datepicker();
            chk_me_bordereau('codeme', 'bord');
            <?php
            echo "updatenv();";
            switch ($td)
            {
                case "add" :
                    echo "init_warn('".nb_marques("","env")."mque','','back','','');";
                    echo "init_warn('".nb_age_actives()."age','','back','','');";
                    echo "init_fensaisie('cree_mque',1024,600);";
                    echo "init_chk_me_bordereau('codeme','bord');";
                    echo "init_chk_bordereau_me('bord','codeme','blur');";
                    echo "init_valid_rete('enr','rete_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "init_valid_rete('enrimp','rete_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "edit" :
                    echo "init_fensaisie('cree_mque',1024,600);";
                    echo "init_fensaisie('chx_art',1024,600);";
                    echo "init_chk_me_bordereau('codeme','bord');";
                    echo "init_chk_bordereau_me('bord','codeme','blur');";
                    echo "init_valid_rete('enr','rete_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "init_valid_rete('enrimp','rete_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "del" :
                    echo "warn_del('');";
                    echo "readonly_all();";
                    break;
                case "view" :
                    $row = lit_enr_me($rete_codeme, TRUE);
                    if (is_array($row)) {
                        $row = encode_str($row);
                        extract($row);
                    }
                    if (trim($me_lien) <> "")
                        echo "init_lien_ext('lnk_me','".$me_lien."');";
                    echo "readonly_all();";
                    break;
            }
            if ($lst_id_readonly <> '')
                echo "readonly_id('".$lst_id_readonly."',false);";
            echo "init_autoraz('razdateenvf','dateenvf','click');";
            ?>
            init_autohref_classe('falselnkimg','');
            Focus_first();
        }

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

<body>

<?PHP include_once("../inc/entete.inc.php"); ?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<!-- affiché uniquement s'il y a au moins une marque, une agence active -->
<?PHP
if ($pbm_rete == FALSE) {
    ?>
    <table width="100%" border="0">
        <tr>
            <td width="40%"
                class="annot_gras_it"><?PHP aff_lnk_lst_rech('rete_lst.php?ord=DES&tri=Code', _("Retour à la liste"), $_SERVER['HTTP_REFERER'], _("Retour à la page précédente")); ?></td>
            <td width="33%" align="center" class="annot_petit_it">
                <div id="oblig">(<img src="../img/oblig.png" align="absmiddle"
                                      alt="<?PHP echo _("Champ obligatoire"); ?>">
                    : <?PHP echo _("Saisie obligatoire"); ?>)
                </div>
            </td>
            <td width="40%">
                <div id="imp"
                     align="right" <?PHP if (nb_art_in_envoi($rete_code, TRUE) == 0) echo 'style="display:none"'; else echo 'style="display:block"'; ?>>
                    <?PHP
                    if (($td == "edit") OR ($td == "view")) {
                        $urlimp = "rete_pdf.php?ce=" . $ce;
                        if (url_ok($urlimp) == TRUE) {
                            echo '<div id="imprim" align="right" title="' . $urlimp . '" class="falselnk">';
                            echo '<img src="../img/print.png" title="' . _("Imprimer") . '" border="0" align="absmiddle"> ' . _("Imprimer");
                            echo '</div>';
                        } else
                            echo '&nbsp;';
                    } else
                        echo '&nbsp;';
                    ?>
            </td>
        </tr>
    </table>
    <form name="rete_maj" id="rete_maj" method="post" action="rete_enr.php">
    <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
    <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
    <input name="codemque" type="hidden" id="codemque" value="<?PHP echo $rete_codemque ?>">
    <input name="today" type="hidden" id="today" value="<?PHP echo $datef; ?>">
    <input name="nbutton" type="hidden" id="nbutton" value="">
    <?PHP if ($td == "add") { ?>
        <input name="code" type="hidden" id="code" value="<?PHP echo $rete_code ?>">
        <input name="clos" type="hidden" id="clos" value="">
    <?PHP
    }
    if (($td == "edit") OR ($td == "del")) {
        ?>
        <input name="codeage" type="hidden" id="codeage" value="<?PHP echo $rete_codeage ?>">
    <?PHP
    } ?>

    <!-- Code de l'envoi(affiché uniquement si édition ou suppression) -->
    <?PHP if ($td <> "add") { ?>
        <table width="900" border="0">
            <tr>
                <td width="160" class="rubfrm" id="f_code" align="right"><?PHP echo _("Code"); ?></td>
                <td>
                    <input name="code" type="text" id="code" value="<?PHP echo $rete_code ?>" size="20" maxlength="20"
                           readonly>
                    <?PHP if ($rete_clos == 1) {
                        echo "<span class=\"alerte1\">";
                        echo _("Envoyé");
                        echo "</span>";
                    }
                    ?>
                </td>
            </tr>
        </table>
    <?PHP } ?>

    <!-- Date création -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_datecreaf" align="right"><?PHP echo _("Créée le"); ?></td>
            <td>
                <input name="datecreaf" type="text" id="datecreaf" value="<?PHP echo $rete_datecreaf ?>" size="10"
                       maxlength="10" readonly>
            </td>
        </tr>
    </table>

    <!-- Affichage du destinataire -->
    <table width="100%" border="0">
        <tr>
            <td class="rubfrm" id="f_codemque">
                <div align="center"><?PHP echo _("Destinataire"); ?></div>
            </td>
        </tr>
    </table>

    <!-- détails destinataire -->
    <table width="100%" border="0">
        <tr>
            <td valign="top" bgcolor="#C5CED7">
                <?PHP
                echo "<span class=\"annot_gras_it\">";
                echo $mque_ste . "<br />";
                echo "</span>";
                echo $mque_adr1 . "<br />";
                if ($mque_adr2 <> "")
                    echo $mque_adr2 . "<br />";
                echo Retournecp_Id($mque_ville, TRUE) . " " . Retourneville_Id($mque_ville, TRUE) . "<br />";
                $chemin_imagesco = $chemin_images . "/telc.png";
                echo "<img src=\"" . $chemin_imagesco . "\" align=\"bottom\">";
                echo " " . $mque_telf . "<br />";
                ?>
            </td>
        </tr>
    </table>

    <!-- Code de l'agence (choisi uniquement si création, affiché uniquement si édition ou suppression) -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_agence" align="right"><?PHP echo _("Agence"); ?></td>
            <td>
                <?PHP
                $tab_age = give_tab_age_cli($cli_code);
                cre_select('codeage', 'codeage', $tab_age, $rete_codeage, '', _("-- Faites votre choix --"), 1, FALSE, '', _("-- Pas d'agence --"), TRUE, TRUE);
                ?>
            </td>
        </tr>
    </table>

    <!-- Date envoi -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_dateenvf" align="right"><?PHP echo _("Date Envoi"); ?></td>
            <td>
                <input name="dateenvf" type="text" id="dateenvf" value="<?PHP echo $rete_dateenvf ?>" size="10"
                       maxlength="10" readonly>
                <?PHP
                if (($td == "add") OR ($td == "edit"))
                    echo ' <img src="../img/raz.png" id ="razdateenvf" border="0" align="absmiddle">';
                ?>
            </td>
        </tr>
    </table>

    <!-- Mode d'envoi -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_codeme" align="right"><?PHP echo _("Mode d'envoi"); ?></td>
            <td>
                <?PHP
                $tab_first_options = [];
                $tab_first_options["-1"] = _("Sans objet");

                // création de la liste des mode d'envoi
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBME . "` ORDER BY me_nom";
                $rows = $db->get_results($rech, ARRAY_A);

                cre_select_tab('codeme', 'codeme', $rows, 'me_code', 'me_nom', '', $rete_codeme, '', _("-- Faites votre choix --"), 1, FALSE, '', '', TRUE, TRUE, FALSE, $tab_first_options);

                if ($td == "view")
                    echo ' <img src="../img/web_go.png" id ="lnk_me" border="0" align="absmiddle" alt="'._("Se connecter au site du transporteur").'">';
                ?>
            </td>
        </tr>
    </table>

    <!-- Numéro de bordereau -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_bord" align="right"><?PHP echo _("Bordereau"); ?></td>
            <td>
                <input name="bord" type="text" id="bord" value="<?PHP echo $rete_bord ?>" size="20">
            </td>
        </tr>
    </table>

    <!-- Nombre de colis -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_nbcol" align="right"><?PHP echo _("Nombre de colis"); ?></td>
            <td>
                <input name="nbcol" type="text" id="nbcol" value="<?PHP echo $rete_nbcol ?>" size="3">
            </td>
        </tr>
    </table>

    <?PHP if ($td == "edit") { ?>
        <!-- Cloture de fiche -->
        <table width="100%" border="0">
            <tr>
                <td width="160" class="rubfrm" id="f_clos" align="right"><?PHP echo _("Clore la fiche"); ?></td>
                <td>
                    <input type="checkbox" name="clos" id="clos" value="ok"<?PHP if (nb_art_in_envoi($rete_code, TRUE) == 0) echo 'disabled="true"'; ?>>
                    <span class="annot_petit_it">(<?PHP echo _("Si coché, cette fiche sera validée à la date d'envoi et ne sera plus modifiable"); ?>)</span>
                </td>
            </tr>
        </table>
    <?PHP } else { ?>
        <input type="hidden" name="clos" id="clos" value="<?PHP if ($rete_clos == 1) echo "ok"; ?>">
    <?PHP } ?>
    <!-- Zone de saisie des articles et prestations -->
    <?PHP
    if (($td == "edit") OR ($td == "del") OR ($td == "view")) {
        ?>
        <a name="envart"></a>
        <table width="100%" border="0">
            <tr>
                <td class="rubfrm" align="center">
                    <?PHP echo _("Produits renvoyés"); ?>
                </td>
            </tr>
        </table>
        <div id="detpdte">
        </div>

        <!-- Lien d'ajout d'article ou de prestation-->
        <?PHP
        if ($td == "edit") {
            $urlchoixart = "retp_lst.php?tx=e&td=" . $td . "&cm=" . $cm . "&ce=" . $ce . "&ca=" . $rete_codeage;
            echo '<table width="100%" border="0">';
            echo '<tr>';
            echo '<td width="50%">';
            cre_falselnk('', 'chx_art', 'span', '', _("Ajouter un produit"), $urlchoixart, '../img/add.png', _("Ajouter un produit"));
            echo '</td>';
            echo '<td width="50%">';
            echo '</td>';
            echo '</tr>';
            echo '</table>';
        }
    }
    ?>
    <p align="center">
        <?PHP
        switch($td)
        {
            case "add" :
            case "edit" :
                echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
                echo '<input name="enr" type="button" class="bton_std" id="enr" value="'._("Enregistrer").'">';
                if ($td== "edit")
                    echo '<span id="butimp"';
                    if (nb_art_in_envoi($ce, TRUE) > 0)
                       echo ' style="visibility:visible"';
                    else
                        echo ' style="visibility:hidden"';
                    echo '>';
                    echo '<input name="enrimp" type="button" class="bton_std" id="enrimp" value="'._("Enregistrer & Imprimer").'">';
                    echo '</span>';
                break;
            case "del" :
                echo '<input name="del" type="submit" class="bton_std" id="del" value="'._("Supprimer").'">';
                break;
        }
        ?>
    </p>
    </form>

    <!-- affiché uniquement s'il y a au moins un destinataire, une agence active -->
<?PHP
} else
    pop_ret_auto(_("Merci de choisir un de vos envois"), 'warn', "rete_lst.php?ord=DES&tri=Code");
include_once("pied.php");
?>
</body>
</html>