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

/**
 * Nombre  signatures pour un code pièce rattachée
 *
 * @param $typrat
 * @param $coderat
 * @param bool $open
 * @return int|null
 */
function nb_sig_code($typrat, $coderat, $open = TRUE)
{
    global
    $db;
    $nb_sig = 0;

    if (((int)$typrat > 0) AND (trim($coderat) <> '')) {
        if ($open == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        }
        $rexist = 'SELECT COUNT(*) FROM `' . TBSIG . '`';
        $rexist .= ' WHERE sig_typrat=' . $typrat . " AND sig_coderat='" . $db->escape($coderat) . "'";
        $nb_sig = $db->get_var($rexist);
        if ($open == TRUE) {
            close_database();
        }
    }

    return $nb_sig;
}

/**
 * Lecture d'une signature par son code pièce rattachée
 *
 * @param $typrat
 * @param $coderat
 * @param int $idhisto
 * @param bool $open
 * @return array|null
 */
function lit_enr_sig_code($typrat, $coderat, $idhisto = 0, $open = TRUE)
{
    global
    $db;
    if ($idhisto > 0) {
        if (((int)$typrat > 0) AND (trim($coderat) <> '')) {
            if ($open == TRUE) {
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            }
            $rexist = 'SELECT * FROM `' . TBSIG . '`';
            $rexist .= ' WHERE sig_typrat=' . $typrat . " AND sig_coderat='" . $db->escape($coderat) . "'";
            $rexist .= " AND sig_idhisto='" . (int)$idhisto . "'";
            $row2 = $db->get_row($rexist, ARRAY_A);
            if ($open == TRUE) {
                close_database();
            }
        }
    }
    return $row2;
}

/**
 * Vérifie si un détail horaire est signé ou pas
 *
 * @param $coderat
 * @param $idhisto
 * @param bool $open
 * @return bool
 */
function inth_signee($coderat, $idhisto, $open = TRUE)
{
    $signee = FALSE;

    $row = lit_enr_sig_code(1, $coderat, $idhisto, $open);
    if (is_array($row)) {
        $signee = TRUE;
    }

    return $signee;
}
