<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

/**********************************************/
/* Fonctions pour les accès aux Ordres        */
/* et fichiers rattachés                      */
/**********************************************/

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

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBOT . "` WHERE `ot_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Vérification de la présence d'un sous-traitant en tant que code de création d'une fiche ordre
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_in_ot_codecrea($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBOT . "` WHERE `ot_codecrea` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0)
        $exist = TRUE;
    if ($open == TRUE)
        $db->disconnect();

    return $exist;
}

/**
 * Nombre de bons de travail
 *
 * $codeint : code intervention (si filtré sur une intervention)
 * $idiste : id du site (si,filtré sur un site en particulier)
 * @param $codeint
 * @param int $idsite
 * @param bool $open
 * @return null
 */
function nb_ot($codeint, $idsite = 0, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT COUNT(*) FROM `" . TBOT . "`";
    if ($codeint <> "")
        $rech2 .= " WHERE `ot_codeint` = '" . $db->escape($codeint) . "'";
    if ($idsite <> 0) {
        if ($codeint <> "")
            $rech2 .= " AND";
        $rech2 .= " `ot_idsite` =" . $idsite;
    }
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Nombre de d'ordres non clos pour une intervention
 *
 * @param $code
 * @param bool $open
 * @return null
 */
function nb_ot_actifs($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT COUNT(*) FROM `" . TBOT . "` WHERE `ot_codeint` = '" . $db->escape($code) . "' AND `ot_datefin` = '0000-00-00'";
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Nombre d'ordres à régulariser
 *
 * @param $codecli
 * @param int $idsite
 * @param bool $open
 * @return null
 */
function nb_ot_areg($codecli, $idsite = 0, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT COUNT(*) FROM `" . TBOT . "` WHERE `ot_codeint` = '--AR--'";
    $rech2 .= " AND ot_codecli ='" . $codecli . "'";
    if ($idsite <> 0)
        $rech2 .= " AND `ot_idsite` =" . $idsite;
    $nbexist = $db->get_var($rech2);

    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Nombre de détail horaire pour un ordre
 *
 * @param $code
 * @param bool $open
 * @return null
 */
function nb_oth($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT COUNT(*) FROM `" . TBOTH . "` WHERE `oth_codeot` = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Lecture d'un enregistrement détail horaire ordre
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_enr_oth($id, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBOTH . "` WHERE `oth_id` = '$id'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Donne le temps travaillé sur un ordre de travail
 *
 * @param $codeot
 * @param bool $open
 *
 * @return int
 */
function tps_travaille_ot($codeot, $open = TRUE)
{
    global
    $db;
    $tps = 0;

    // On se connecte a la base
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    // Récupération total travaillé
    $rech = "SELECT * FROM `" . TBOTH . "` WHERE `oth_codeot`= '" . $db->escape($codeot) . "'";
    $rows = $db->get_results($rech, ARRAY_A);
    if (($rows) AND (count($rows) > 0)) {
        foreach ($rows as $row) {
            $tps = $tps + (diff_time_sec($row["oth_heurea"], $row["oth_heured"]));
        }
    }

    if ($open == TRUE)
        $db->disconnect();

    return $tps;

}

/**
 * Retourne le temps offert sur un ordre de travail
 *
 * @param $codeot
 * @param bool $open
 *
 * @return int
 */
function tps_offert_ot($codeot, $open = TRUE)
{
    global
    $db;
    $tps = 0;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    // Récupération total travaillé
    $rech = "SELECT * FROM `" . TBOTH . "` WHERE `oth_codeot`= '" . $db->escape($codeot) . "' AND `oth_offert`= 1";
    $rows = $db->get_results($rech, ARRAY_A);
    if (($rows) AND (count($rows) > 0)) {
        foreach ($rows as $row) {
            $tps = $tps + (diff_time_sec($row["oth_heurea"], $row["oth_heured"]));
        }
    }

    if ($open == TRUE)
        $db->disconnect();

    return $tps;
}


/**
 * Donne la chaîne des techniciens traitants sur une intervention
 * Les clients ne voient pas le détail des agences
 *
 * @param $codeot
 * @param bool $open
 * @return string
 */
function str_tec_traitants_ot($codeot, $open = TRUE)
{
    global
    $db,
    $_SESSION;
    $str = "";

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    $rech = "SELECT DISTINCT oth_codetec FROM `" . TBOTH . "` WHERE `oth_codeot`= '" . $db->escape($codeot) . "'";
    $rows = $db->get_results($rech, ARRAY_A);
    if (($rows) AND (count($rows) > 0)) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            $rech2 = "SELECT tec_nom, tec_prenom,tec_codest FROM `" . TBTEC . "` WHERE `tec_code`= '" . $row["oth_codetec"] . "' ORDER BY tec_nom";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                $row2 = encode_str($row2);
                $str .= $row2["tec_nom"] . " " . $row2["tec_prenom"];
                if ($_SESSION["niv_log"] <> 1)
                    $str .= " (" . substr($row2["tec_codest"], 1, strlen($row2["tec_codest"])) . ")";
                $str .= ", ";
            }
        }
    }
    if ($str != "")
        $str = substr($str, 0, strlen($str) - 2) . ".";
    if ($open == TRUE)
        $db->disconnect();

    return $str;
}
?>