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

session_start();

include_once('../inc/config.inc.php');
include_once('../inc/func_log.inc.php');
if ((CheckURLLog(FALSE) == FALSE) OR (CheckNivLog('5-9') == FALSE)) {
    header('Location: ../index.php');
}
include_once('../inc/func.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('../inc/fic_age.inc.php');
include_once('../inc/fic_cli.inc.php');
include_once('../inc/fic_droits.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);

$pbm_syn = FALSE;
$ts_ok = TRUE;
$ts = (int)valget('ts');

switch ($ts) {
    case 1 :
    case 2 :
    case 3 :
    case 4 :
    case 5 :
    case 6 :
    case 7 :
        // Les dates sont remplies sur un formulaire validé, donc on peut afficher les statistiques
        if (isset($_GET['date1f']) AND isset($_GET['date2f'])) {
            $age_select = '';
            for ($i = 0; $i < count($_GET['age']); $i++) {
                $age_select .= $_GET['age'][$i] . '-';
            }
            $age_select_tab = explode('-', substr($age_select, 0, - 1));

            $cat = valget('cat');

            $date1 = datetomysql(valget('date1f'));
            $date2 = datetomysql(valget('date2f'));
        }
        break;
    default :
        $pbm_syn = TRUE;
        $ts_ok = FALSE;
        break;
}

?>
<!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 _('Synthèses Interventions'); ?></title>

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

    <script language="javascript">
        <!--
        var tab_champ = [];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        function init_datepicker() {
            <?php include_once ('../jscript/datepicker_prm.js.php'); ?>
            $("#date1f").datepicker({
                onSelect: function (datesel) {
                    $("#date1f").blur();
                    chk_datedif(datesel, this.id, '<?php echo valsession('date_fmtshort'); ?>', true, '', '', 'date2f', false);
                }
            });
            $("#date2f").datepicker({
                onSelect: function (datesel) {
                    $("#date2f").blur();
                    chk_datedif(datesel, this.id, '<?php echo valsession('date_fmtshort'); ?>', true, 'date1f', '', '', false);
                }
            });
        }

        function init_all() {
            init_Body();
            init_datepicker();
            <?php
            if ($ts_ok == TRUE)
                {echo "readonly_id('ts',false);";}
            ?>
            init_valid_form('enr', 'inter', '<?php echo valsession('date_fmtshort'); ?>', tab_champ, '', '');
            init_autohref('new', 'click', 'int_syn.php', false);
            Focus_first();
        }

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

</head>

<body>
<?php include_once('../inc/entete.inc.php');
cre_ent_form(_('SYNTHESE INTERVENTIONS'), '', '', '', FALSE);
if ($pbm_syn == FALSE) {
    echo '<form name="inter" id="inter"';
    if ($ts_ok == FALSE) {
        echo ' method="get" action="int_syn.php"';
    } else {
        echo ' method="get" action="int_syn_lst.php"';
        echo ' target="_blank"';
    }
    echo '>';
    echo '<input name="nbutton" type="hidden" id="nbutton" value="">';
    echo '<br />';
    echo '<div align="center">';
    cre_select('ts', 'ts', $tab_txt_syn_int, (int)$ts, '', _('-- Faites votre choix --'), 1, FALSE, '', '', FALSE);

    switch ($ts) {
        // Synthèse des interventions, pour un client, sur une période donnée
        case 1 :
            echo '<br />';
            echo '<br />';
            if (nb_cli('') > 0) {
                echo '<select name="cli" id="cli">';
                echo '<option value="">' . _('-- Faites votre choix --') . '</option>';
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                $rech = 'SELECT * FROM `' . TBCLI . '` ORDER BY cli_ste,cli_nom';
                $rows = $db->get_results($rech, ARRAY_A);
                if ($rows) {
                    foreach ($rows as $row) {
                        $row = encode_str($row);
                        extract($row);
                        if ($cli_ste <> '') {
                            $txt_cli = $cli_ste . ' (' . Retournecp_Id($cli_ville, FALSE) . ', ' . Retourneville_Id($cli_ville, FALSE) . ') - ' . $cli_code;
                        } else {
                            $txt_cli = $cli_nom . ' ' . $cli_prenom . ' (' . Retournecp_Id($cli_ville, FALSE) . ', ' . Retourneville_Id($cli_ville, FALSE) . ') - ' . $cli_code;
                        }
                        echo '<option value="' . $cli_code . '">' . $txt_cli . '</option>';
                    }
                }
                close_database();
                echo '</select>';
            }
            break;

        // Synthèse des interventions, pour une agence, sur une période donnée
        case 2 :
            // Synthèse des interventions, pour une agence gestionnaire, sur une période donnée
        case 3 :
            // Synthèse des interventions, pour une agence responsable de site, sur une période donnée
        case 4 :
            echo '<br />';
            echo '<br />';
            if (nb_age() > 0) {
                echo '<select name="age" id="age">';
                echo '<option value="">' . _('-- Faites votre choix --') . '</option>';
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                $rech = 'SELECT * FROM `' . TBAGE . '` ORDER BY age_nom';
                $rows = $db->get_results($rech, ARRAY_A);
                if ($rows) {
                    foreach ($rows as $row) {
                        $row = encode_str($row);
                        extract($row);
                        $txt_age = $age_nom . ' (' . $age_code;
                        if ($age_ouvert == 1) {
                            $txt_age .= ' - ' . _('ouvert');
                        } else {
                            $txt_age .= ' - ' . _('fermée');
                        }
                        $txt_age .= ')';
                        echo '<option value="' . $age_code . '">' . $txt_age . '</option>';
                    }
                }
                close_database();
                echo '</select>';
            }
            break;

        // Synthèse des interventions, pour un technicien, sur une période donnée
        case 5 :
            // Synthèse des interventions, pour un technicien responsable, sur une période donnée
        case 6 :
            // Synthèse des interventions, pour un technicien intervenant, sur une période donnée
        case 7 :
            echo '<br />';
            echo '<br />';
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            $rech = 'SELECT * FROM `' . TBTEC . '`';
            $rech .= " WHERE tec_codeage <> ''";
            $rech .= ' ORDER BY tec_nom, tec_prenom';
            $rows = $db->get_results($rech, ARRAY_A);
            close_database();
            cre_select_tab('tec', 'tec', $rows, 'tec_code', 'tec_nom', 'tec_prenom-tec_codeage', '', '', _('-- Faites votre choix --'));
            break;
    }

    if ($ts_ok == TRUE) {
        echo '<br />';
        echo '<br />';
        ?>
        <table width="400" border="0" align="center">
            <tr>
                <td width="30" class="rubfrm" id="f_date1" align="right"><?php echo _('Du'); ?>...</td>
                <td>
                    <input name="date1f" type="text" id="date1f" size="10" maxlength="10" readonly>
                    <img src="../img/oblig.png" align="absmiddle" alt="<?php echo _('Champ obligatoire'); ?>">
                </td>
                <td width="30" class="rubfrm" id="f_date2f" align="right"><?php echo _('Au'); ?>...</td>
                <td>
                    <input name="date2f" type="text" id="date2f" size="10" maxlength="10" readonly>
                    <img src="../img/oblig.png" align="absmiddle" alt="<?php echo _('Champ obligatoire'); ?>">
                </td>
            </tr>
        </table>
        <?php
    }

    echo '</div>';
    echo '<p align="center">';
    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _('Valider') . '">';
    echo '<input name="init" type="reset" class="bton_std" id="init" value="' . _('Réinitialiser') . '">';
    if ($ts_ok == TRUE) {
        echo '<input name="new" type="button" class="bton_std" id="new" value="' . _('Nouvelle synthèse') . '">';
    }
    echo '</p>';
    echo '</form>';

} else {
    pop_ret_auto(_('Problème de paramètres'), 'warn', 'index.php');
}
include_once('pied.php');
?>
