<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_loc.inc.php");
include_once("../inc/fic_prm.inc.php");
include_once("../inc/fic_tec.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(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);
$_POST = clean_str($_POST);

$pbm_pdtlocr = FALSE;
$td = '';
$codepdtloc = '';
$action_to_do = '';
$idpdtlocr = 0;

if (trim(@$_POST["nbutton"]) == 'val') // On a cliqué sur valider
    $action_to_do = 'valid';
if (trim(@$_POST["nbutton"]) == 'del') // On a cliqué sur supprimer
    $action_to_do = 'delete';

if (isset($_GET['td']))
    $td = @$_GET['td'];
else {
    if (isset($_POST['td']))
        $td = @$_POST['td'];
    else
        $pbm_pdtlocr = TRUE;
}

if (isset($_GET['cp']))
    $codepdtloc = @$_GET['cp'];
else {
    if (isset($_POST['cp']))
        $codepdtloc = @$_POST['cp'];
    else
        $pbm_pdtlocr = TRUE;
}

if (isset($_GET['icpr']))
    $idpdtlocr = intval(@$_GET['icpr']);
else {
    if (isset($_POST['icpr']))
        $idpdtlocr = intval(@$_POST['icpr']);
}

$totalpdtlocr = 0;
$retourliste = FALSE;

if ($codepdtloc <> "") {
    $row = lit_enr_pdtloc($codepdtloc, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
            $pdtlocr_codeage = $_SESSION["code_age"];
        if (@$_SESSION["niv_log"] == 3)
            $pdtlocr_codetec = $_SESSION["code_tec"];

        $codeadev = give_dev_codea_defaut_pdtloc($pdtloc_code, $_SESSION["lng"], TRUE);

        $totalpdtlocr = nb_pdtlocr($codepdtloc);
        if ($totalpdtlocr == 0)
            $td = "add";

        if (($action_to_do == 'valid') OR ($action_to_do == 'delete')) {
            $idpdtlocr = intval(@$_POST["icpr"]);
            $code = @$_POST["cp"];
            $codeage = str_majuscules(@$_POST["codeage"]);
            $codetec = str_majuscules(@$_POST["codetec"]);
            $date = datetomysql(@$_POST["datef"]);
            // $durees = heure_sec(@$_POST["duree"] . ":00");
            $durees = intval(@$_POST["duree"]);
            $mtp = floatval(@$_POST["mtp"]);
            $fait = @$_POST["fait"];

            $rech = "";
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            switch ($td) {
                case "add" :
                    $rech = "INSERT INTO `" . TBPDTLOCR . "`";
                    $rech .= " (pdtlocr_codepdtloc,pdtlocr_codeage,pdtlocr_codetec,pdtlocr_date,pdtlocr_durees,pdtlocr_mtp,pdtlocr_fait) VALUES";
                    $rech .= " ('$code','$codeage','$codetec','$date','$durees','$mtp','$fait')";
                    break;
                case "edit" :
                    $rech = "UPDATE `" . TBPDTLOCR . "` SET `pdtlocr_codepdtloc`='" . $code . "', `pdtlocr_codeage`='" . $codeage . "',";
                    $rech .= " `pdtlocr_codetec`='" . $codetec . "', `pdtlocr_date`='" . $date . "', `pdtlocr_durees`='" . $durees . "',";
                    $rech .= " `pdtlocr_mtp`='" . $mtp . "', `pdtlocr_fait`='" . $fait . "'";
                    $rech .= "  WHERE `pdtlocr_id` = '" . $idpdtlocr . "'";
                    break;
                case "del" :
                    $rech = "DELETE FROM `" . TBPDTLOCR . "` WHERE `pdtlocr_id` = '$idpdtlocr'";
                    break;
                default :
                    break;
            }
            if ($rech <> "") {
                $db->query($rech);
                $retourliste = TRUE;
            }
            $db->disconnect();
            // On recalcule le total réparations
            $totalpdtlocr = nb_pdtlocr($codepdtloc);
        } else {
            switch ($td) {
                case "add" :
                    $titre = _("SAISIE REPARATION");
                    $pdtlocr_datef = $datef;
                    break;
                case "edit" :
                case "del" :
                    if ($td == "edit")
                        $titre = _("EDITION REPARATION");
                    else
                        $titre = _("SUPPRESSION CONTACT");
                    $row = lit_enr_pdtlocr_id_in_pdtloc($idpdtlocr, $codepdtloc);
                    if (is_array($row)) {
                        extract($row);
                        // Formatage des dates en français
                        $pdtlocr_datef = datetolocal($pdtlocr_date);
                        // Contrôle qu'une agence n'essaye pas de "hacker" une réparation
                        if (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) AND ($pdtlocr_codeage <> @$_SESSION["code_age"]))
                            $pbm_pdtlocr = TRUE;
                    } else
                        $pbm_pdtlocr = TRUE;
                    break;
                case "lst" :
                    $titre = _("LISTE REPARATIONS");
                    break;
                default :
                    $pbm_pdtlocr = TRUE;
                    break;
            }
        }
    } else
        $pbm_pdtlocr = TRUE;
} else
    $pbm_pdtlocr = 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 _("Réparation article location"); ?></title>

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

    <script language="javascript">
        <!--
        var tab_champ = ["nom:datef ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date"); ?> ; stok:frmok ; stbad:frmbad",
            "nom:codeage; format:liste ; req:Y ; txt:<?PHP echo _("Agence"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:codetec; format:liste ; req:Y ; txt:<?PHP echo _("Technicien"); ?>; ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:duree ; format:liste ; req:Y ; txt:<?PHP echo _("Durée"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:mtp; format:reel_ns ; req:N ; txt:<?PHP echo _("Montant pièces"); ?> ; stok:frmok ; stbad:frmbad",
            "nom:fait; format:txt ; req:Y ; txt:<?PHP echo _("Travail effectué"); ?>; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        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"); ?>"
            });
        }

        function init_all() {
            init_Body();
            init_autohref_classe('falselnkimg','');
            <?PHP

            echo "window.opener.$('#mtrept').val('".full_num_format(px_revient_pdtlocr($codepdtloc))."');";
            echo "calc_somme('valnet','mtrept','totvalnet',".$_SESSION['num_nbdec'].",true);";
            if ($retourliste == false)
            {
                // echo "init_fdeci('mtp', ".$_SESSION['num_nbdec'].");";
                switch ($td)
                {
                    case "add" :
                        echo "init_datepicker();";
                        echo '$("#datef").datepicker({';
                        echo '    onSelect: function (datesel) {';
                        echo '        $("#datef").blur();';
                        echo "        chk_datedde('datef', '".$_SESSION['date_fmtshort']."', true);";
                        echo '    },';
                        echo '});';
                        echo "init_autoraz('razdatef','datef','click');";
                        if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
                            echo "readonly_id('codeage',false);";
                        if (@$_SESSION["niv_log"] == 3)
                            echo "readonly_id('codetec',false);";
                        break;
                    case "edit" :
                        echo "init_datepicker();";
                        echo '$("#datef").datepicker({';
                        echo '    onSelect: function (datesel) {';
                        echo '        $("#datef").blur();';
                        echo "        chk_datedde('datef', '".$_SESSION['date_fmtshort']."', true);";
                        echo '    },';
                        echo '});';
                        echo "init_autoraz('razdatef','datef','click');";
                        if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
                            echo "readonly_id('codeage',false);";
                        if (@$_SESSION["niv_log"] == 3)
                            echo "readonly_id('codetec',false);";
                        break;
                    case "view" :
                        echo "readonly_all();";
                        break;
                    case "del" :
                        echo "readonly_all();";
                        echo "warn_del('scli');";
                        break;
                }
                echo "init_ajax_tec2();";
                echo "init_valid_form('val','pdtlocr_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                echo "init_valid_form('del','pdtlocr_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                echo "add_func_click('quit', 'sclose','');";
                echo "init_autohref('lst', 'click', 'pdtlocr_maj.php?cp=".$codepdtloc."&td=lst',false);";
                echo "init_autohref('add', 'click', 'pdtlocr_maj.php?cp=".$codepdtloc."&td=add',false);";
                echo "Focus_first();";
            }
            else
                echo "self.location.href=('pdtlocr_maj.php?td=lst&cp=" . $pdtloc_code."');";
            ?>
        }

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

</head>
<body>
<?PHP
if ($pbm_pdtlocr == FALSE)
{
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<p align="center"><span class="titre_gras"><?PHP echo _("Pour l'article"); ?> : <?php echo $pdtloc_des; ?> </span>
    <br /><span class="annot_petit_gras"><?PHP echo _("NS"); ?> : <?php echo $pdtloc_ns; ?> </span></p>
<table width="890" border="0">
    <tr>
        <td width="33%">&nbsp;</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="33%" align="right">
            <input name="quit" type="button" class="bton_std" id="quit" value="<?PHP echo _("Quitter"); ?>"
                   style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;">
            <?PHP if (($totalpdtlocr > 0) AND ($td <> "lst")) { ?>
                <input name="lst" type="button" class="bton_std" id="lst" value="<?PHP echo _("Revenir à la liste"); ?>"
                       style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;">
            <?PHP } ?>
        </td>
    </tr>
</table>
<?PHP
switch ($td)
{
    case "add" :
    case "edit" :
    case "del" :
?>
    <form name="pdtlocr_maj" id="pdtlocr_maj" method="post" action="pdtlocr_maj.php">
    <input name="icpr" type="hidden" id="icc" value="<?PHP echo $pdtlocr_id ?>">
    <input name="cp" type="hidden" id="cp" value="<?PHP echo $codepdtloc ?>">
    <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
    <input name="nbutton" type="hidden" id="nbutton" value="">
    <table width="890" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_datef" align="right"><?PHP echo _("Date réparation"); ?></td>
            <td>
                <input name="datef" type="text" id="datef" value="<?PHP echo $pdtlocr_datef ?>" size="10" maxlength="10" readonly>
                <?PHP if ($td == "add")
                {
                    echo ' <img src="../img/oblig.png" align="absmiddle" alt="'._("Champ obligatoire").'">';
                    echo ' <img src="../img/raz.png" id ="razdatef" border="0" align="absmiddle">';
                } ?>
            </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('');
                cre_select('codeage', 'codeage', $tab_age, $pdtlocr_codeage, '', _("-- Faites votre choix --"), 1, FALSE, '', _("-- Pas d'agence --"));
                ?>
            </td>
        </tr>
    </table>

    <!-- Technicien -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_codetec" align="right"><?PHP echo _("Technicien"); ?></td>
            <td>
                <?PHP
                // Création de la liste des Techniciens de l'agence
                $rows= [];
                if ($pdtlocr_codeage <> "") {
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBTEC . "` WHERE `tec_codeage` = '$pdtlocr_codeage' AND `tec_out` = '0' ORDER BY tec_nom";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                }
                cre_select_tab('codetec', 'codetec', $rows, 'tec_code', 'tec_nom', 'tec_prenom-tec_codeage', $pdtlocr_codetec, '', _("-- Faites votre choix --"), 1, FALSE, '', _("--- Pas de technicien affecté --"));
                ?>
            </td>
        </tr>
    </table>

    <!-- Durée -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_duree" align="right"><?PHP echo _("Durées"); ?></td>
            <td>
                <?PHP
                $tab_dureemin = give_tab_dureemin($pla_stepmin, 5940, $pla_stepmin);
                cre_select('duree', 'duree', $tab_dureemin, intval($pdtlocr_durees));
                ?>
            </td>
        </tr>
    </table>

    <!-- Montant des pièces changées -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_mtp" align="right"><?PHP echo _("Montant HT pièces"); ?></td>
            <td>
                <?PHP
                cre_input_num('mtp', $pdtlocr_mtp, TRUE);
                ?>
            </td>
        </tr>
    </table>

    <!-- Travail effectué -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_fait" align="right"><?PHP echo _("Travail effectué"); ?></td>
            <td>
                <textarea name="fait" rows="5" id="fait"><?PHP echo $pdtlocr_fait ?></textarea>
            </td>
        </tr>
    </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="val" type="button" class="bton_std" id="val" value="'._("Enregistrer").'">';
                if ($td == "edit")
                {
                    echo '<br />';
                    echo '<br />';
                    echo '<input name="add" type="button" class="bton_std" id="add" value="'._("Saisir une autre réparation").'">';
                }
                break;
            case "view" :
                break;
            case "del" :
                echo '<input name="del" type="button" class="bton_std" id="del" value="'._("Supprimer").'">';
                break;
        }
        ?>
    </p>
    </form>
<?PHP
    break;
    case "lst" :
        // création de la liste des réparations de ce produit
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT * FROM `" . TBPDTLOCR . "` WHERE `pdtlocr_codepdtloc` = '" . $codepdtloc . "' ORDER BY 'pdtlocr_date'";
        if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
            $rech .= " AND `pdtlocr_codeage` = '" . $_SESSION["code_age"] . "'";

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

        if ($rows) {
            echo "<table class=\"MtableD\" border=\"0\" width=\"890\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";

            // création de l'entête de la table contacts clients
            echo "<th>";
            echo _("Date");
            echo "</th>\n";
            echo "<th>";
            echo _("Agence");
            echo "</th>\n";
            echo "<th>";
            echo _("Technicien");
            echo "</th>\n";
            echo "<th>";
            echo _("Choisir");
            echo "</th>\n";

            $j = 0;

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

                // création de l'entête de ligne (TR)
                creetrlstdet($j);

                echo "<td>\n";
                echo datetolocal($row["pdtlocr_date"]);
                echo "</td>\n";
                echo "<td>\n";
                echo $row["pdtlocr_codeage"];
                echo "</td>\n";
                echo "<td>\n";
                echo $row["pdtlocr_codetec"];
                echo "</td>\n";

                echo "<td align=\"center\">\n";
                $lst_maj = TRUE;
                cre_falselnk_img('', 'pdtlocr_edit_'.$row["pdtlocr_id"], 'span', 'center', _("Edition"), 'lnk', 'pdtlocr_maj.php?td=edit&cp=' . $row["pdtlocr_codepdtloc"] . '&icpr=' . $row["pdtlocr_id"], '../img/edit.png');
                $lst_del = TRUE;
                cre_falselnk_img('', 'pdtlocr_del_'.$row["pdtlocr_id"], 'span', 'center', _("Suppression"), 'lnk', 'pdtlocr_maj.php?td=del&cp=' . $row["pdtlocr_codepdtloc"] . '&icpr=' . $row["pdtlocr_id"], '../img/del.png');
                echo "</td>\n";
                echo "</tr>\n";

                $j++;
            }
            echo "</table>\n";
        }
        $db->disconnect();

        // Fin affichage table liste réparations
        $taille_piedla = "";
        include_once("piedla.inc.php");

        echo '<p align="center">';
        echo '<input name="add" type="button" class="bton_std" id="add" value="'._("Saisir une réparation").'">';
        echo '</p>';
        break;

    default :
        break;
    }
}
else
    close_fen_auto('', 'fscomp');
?>
</body>
</html>
