<?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_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(valsession('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(valpost('nbutton')) == 'val') // On a cliqué sur valider
{
    $action_to_do = 'valid';
}
if (trim(valpost('nbutton')) == 'del') // On a cliqué sur supprimer
{
    $action_to_do = 'delete';
}

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

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

if (isset($_GET['icpr'])) {
    $idpdtlocr = (int)valget('icpr');
} else {
    if (isset($_POST['icpr'])) {
        $idpdtlocr = (int)valpost('icpr');
    }
}

$totalpdtlocr = 0;
$retourliste = FALSE;

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

        if (CheckNivLog('3-4')) {
            $pdtlocr_codeage = $_SESSION['code_age'];
        }
        if (CheckNivLog('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 = (int)valpost('icpr');
            $code = valpost('cp');
            $codeage = str_majuscules(valpost('codeage'));
            $codetec = str_majuscules(valpost('codetec'));
            $date = datetomysql(valpost('datef'));
            // $durees = heure_sec(valpost('duree') . ':00');
            $durees = (int)valpost('duree');
            $mtp = (int)valpost('mtp');
            $fait = valpost('fait');

            $rech = '';
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            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;
            }
            close_database();
            // 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 ($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 (CheckNivLog('3-4') AND ($pdtlocr_codeage <> valsession('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() {
            <?php include_once ('../jscript/datepicker_prm.js.php'); ?>
        }

        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',".valsession('num_nbdec'). ',true);';
            if ($retourliste == false)
            {
                // echo "init_fdeci('mtp', ".valsession('num_nbdec').");";
                switch ($td)
                {
                    case 'add' :
                        echo 'init_datepicker();';
                        echo '$("#datef").datepicker({';
                        echo '    onSelect: function (datesel) {';
                        echo '        $("#datef").blur();';
                        echo "        chk_datedde('datef', '".valsession('date_fmtshort')."', true);";
                        echo '    },';
                        echo '});';
                        echo "init_autoraz('razdatef','datef','click');";
                        if (CheckNivLog('3-4'))
                            {echo "readonly_id('codeage',false);";}
                        if (CheckNivLog('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', '".valsession('date_fmtshort')."', true);";
                        echo '    },';
                        echo '});';
                        echo "init_autoraz('razdatef','datef','click');";
                        if (CheckNivLog('3-4'))
                            {echo "readonly_id('codeage',false);";}
                        if (CheckNivLog('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','".valsession('date_fmtshort')."',tab_champ,'','');";
                echo "init_valid_form('del','pdtlocr_maj','".valsession('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) {
    cre_ent_form($titre, '', '', '', FALSE);
    ?>
    <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, 'utf8');
                                $rech = 'SELECT * FROM `' . TBTEC . "` WHERE `tec_codeage` = '$pdtlocr_codeage' AND `tec_out` = '0' ORDER BY tec_nom";
                                $rows = $db->get_results($rech, ARRAY_A);
                                close_database();
                            }
                            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, (int)$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, 'utf8');
            $rech = 'SELECT * FROM `' . TBPDTLOCR . "` WHERE `pdtlocr_codepdtloc` = '" . $codepdtloc . "' ORDER BY 'pdtlocr_date'";
            if (CheckNivLog('3-4')) {
                $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">';

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

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

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

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

                    echo '<td align="center">';
                    $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>';
                    echo '</tr>';
                }
                echo '</table>';
            }
            close_database();

            // 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>
