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

/*********************************************************/
/* Récupération des RDV en fonction type état (Planning) */
/*********************************************************/

session_start();
sleep(1);
if (array_key_exists('rat', $_REQUEST)) {
    $tomorrow = 0; //60*60*24; // en nb de secondes
    header("Cache-Control: max-age=$tomorrow");
    print_plain();
} else {
    print "Usage : $_SERVER[PHP_SELF]?rat=un-type-rattachement_un-code-rattachement";
}

function print_plain()
{
    global
    $_SESSION;
    include_once('../inc/func_date.inc.php');
    header('Content-type: text/html; charset=UTF-8');
    $tab_rat = explode('_', $_REQUEST['rat']);
    $tab_rdv = get_rdv($tab_rat[0], $tab_rat[1]);

    $result = [];
    if (count($tab_rdv) > 0) {
        foreach ($tab_rdv as $row) {
            $result[] = $row[0] . '=' . $row[1];
        }
    }
    print implode(';', $result);
}

/**
 * @param $typ
 * @param $code
 * @return array
 */
function get_rdv($typ, $code)
{
    global
    $_SESSION;


    require_once('../lib/gettext/gettext.inc');
    include_once('../inc/config.inc.php');
    include_once('../inc/func.inc.php');
    include_once('../inc/func_date.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');

    // On recupere les enregistrements
    $rech = '';
    $result = [];
    switch ($typ) {
        // Rien
        case -1 :
            break;
        // Interventions
        case 1 :
            switch (valsession('niv_log')) {
                case 3 :
                case 4 :
                    $rech = 'SELECT * FROM `' . TBRDV . '`';
                    $rech .= " WHERE rdv_typrat=1 AND rdv_coderat='" . $code . "'";
                    $rech .= ' AND rdv_honore=0';
                    $rech .= ' ORDER BY rdv_date, rdv_heure';
                    break;
                case 10 :
                case 11 :
                    break;
                default :
                    $rech = 'SELECT * FROM `' . TBRDV . '`';
                    $rech .= " WHERE rdv_typrat=1 AND rdv_coderat='" . $code . "'";
                    $rech .= ' AND rdv_honore=0';
                    $rech .= ' ORDER BY rdv_date, rdv_heure';
                    break;
            }
            break;
        // Bon de travail
        case 2 :
            break;
    }

    if ($rech <> '') {
        // On se connecte a la base
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        $rows = $db->get_results($rech, ARRAY_A);
        close_database();
        if ($rows) {
            foreach ($rows as $row) {
                extract($row);
                $res = [];
                $txtrdv = '';
                switch ($typ) {
                    // Rien
                    case -1 :
                        break;
                    // Interventions
                    case 1 :
                        $txtrdv = _('Le') . ' ' . datetolocal($rdv_date) . ' ' . _('à') . ' ' . substr($rdv_heure, 0, 5);
                        break;
                    // Bons de travail
                    case 2 :
                        break;
                }
                if (trim($txtrdv) <> '') {
                    $res[0] = $rdv_id;
                    $res[1] = $txtrdv;
                    $result[] = $res;
                }
            }
        }
    }
    return $result;
}
