<?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');
}

/************************************************/
/* Fonctions pour les accès aux fichiers Tâches */
/************************************************/

// Tâches

/**
 * Lecture d'un enregistrement tâche
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_tac($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBTAC . "` WHERE `tac_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}


/**
 * Création de la requête SQL pour recherche nombre de tâches
 *
 * @param $typrat
 * @param $coderat
 * @return string
 */
function cree_req_tac($typrat, $coderat)
{
    global
    $db,
    $_SESSION;

    $rexist = 'SELECT COUNT(*) FROM `' . TBTAC . '`';
    if (((int)$typrat >= 1) AND ((int)$typrat <= 6)) {
        $rexist .= ' WHERE tac_typrat=' . (int)$typrat;
    }
    if (trim($coderat) <> '') {
        if (!stristr($rexist, 'WHERE ')) {
            $rexist .= " WHERE tac_coderat='" . $db->escape($coderat) . "'";
        } else {
            $rexist .= " AND tac_coderat='" . $db->escape($coderat) . "'";
        }
    }

    // Filtre pour les techniciens agences ou sous-traitants
    if (CheckNivLog('3-11')) {
        if (!stristr($rexist, 'WHERE ')) {
            $rexist .= ' WHERE ' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
        } else {
            $rexist .= ' AND ' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
        }
    }

    // Filtre pour les agences
    if (CheckNivLog('4')) {
        // On construit la liste des techniciens actifs de l'agence
        $tab_lst_tec = [];
        $rexist2 = 'SELECT tec_code FROM `' . TBTEC . "` WHERE tec_codeage='" . valsession('code_log') . "' AND tec_out=0";
        $rows2 = $db->get_results($rexist2, ARRAY_A);
        if ($rows2) {
            foreach ($rows2 as $row2) {
                extract($row2);
                $tab_lst_tec[] = $tec_code;
            }
        }

        if (!stristr($rexist, 'WHERE ')) {
            $rexist .= ' WHERE (' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
        } else {
            $rexist .= ' AND (' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
        }
        foreach ($tab_lst_tec as $value) {
            $rexist .= ' OR ' . TBTAC . ".tac_codedest='" . $value . "'";
        }
        $rexist .= ')';
    }

    // Filtre pour le centre de gestion
    if (CheckNivLog('5')) {
        if (!stristr($rexist, 'WHERE ')) {
            $rexist .= ' WHERE ' . TBTAC . ".tac_codedest<>'" . $admin_code . "'";
        } else {
            $rexist .= ' AND ' . TBTAC . ".tac_codedest<>'" . $admin_code . "'";
        }
    }

    // Filtre pour les sous-traitants
    if (CheckNivLog('10')) {
        // On construit la liste des techniciens actifs du sous-traitant
        $tab_lst_tec = [];
        $rexist2 = 'SELECT tec_code FROM `' . TBTEC . "` WHERE tec_codest='" . valsession('code_log') . "' AND tec_out=0";
        $rows2 = $db->get_results($rexist2, ARRAY_A);
        if ($rows2) {
            foreach ($rows2 as $row2) {
                extract($row2);
                $tab_lst_tec[] = $tec_code;
            }
        }

        if (!stristr($rexist, 'WHERE ')) {
            $rexist .= ' WHERE (' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
        } else {
            $rexist .= ' AND (' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
        }
        foreach ($tab_lst_tec as $value) {
            $rexist .= ' OR ' . TBTAC . ".tac_codedest='" . $value . "'";
        }
        $rexist .= ')';
    }

    return $rexist;
}

/**
 * Vérification du nombre de tâches
 *
 * @param $typrat
 * @param $coderat
 * @param bool $open
 * @return null
 */
function nb_tac($typrat, $coderat, $open = TRUE)
{
    global
    $db,
    $_SESSION;


    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = cree_req_tac($typrat, $coderat);
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Vérification du nombre de tâches actives
 *
 * @param $typrat
 * @param $coderat
 * @param bool $open
 * @return null
 */
function nb_tac_actives($typrat, $coderat, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = cree_req_tac($typrat, $coderat);
    if (!stristr($rexist, 'WHERE ')) {
        $rexist .= " WHERE tac_datefin='0000-00-00'";
    } else {
        $rexist .= " AND tac_datefin='0000-00-00'";
    }
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

// Catégories de tâches

/**
 * Lecture d'un enregistrement tâche
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_tacc($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBTACC . "` WHERE `tacc_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérification du nombre de catégories de tâches
 *
 * @param bool $open
 * @return null
 */
function nb_tacc($open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBTACC . '`';
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}
