<?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 fichiers dépôts de location */
/************************************************************/

/**
 * Vérification du nombre de dépôts actifs
 *
 * @param bool $open
 * @return null
 */
function nb_dloc_actifs($open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBDLOC . "`  WHERE `dloc_ouvert` = '1'";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Donne le nombre de dépôts
 *
 * @param bool $open
 * @return null
 */
function nb_dloc($open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT COUNT(*) FROM `" . TBDLOC . "`";
    $nbexist = $db->get_var($rech);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Lecture d'un enregistrement dépôt
 *
 * @param $nom
 * @param bool $open
 * @return array|null
 */
function lit_enr_dloc($nom, $open = TRUE)
{
    global
    $db;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBDLOC . "` WHERE dloc_nom = '" . $db->escape($nom) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Lecture d'un enregistrement dépôt par son id
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_enr_dloc_id($id, $open = TRUE)
{
    global
    $db;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBDLOC . "` WHERE dloc_id = '$id'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Donne le numéro d'ID du dépôt principal d'une agence
 *
 * @param $age
 * @param bool $open
 * @return int
 */
function id_dloc_page($age, $open = TRUE)
{
    global
    $db;
    $dloc_id = -1;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT dloc_id FROM `" . TBDLOC . "` WHERE dloc_codeage = '" . $db->escape($age) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2)
        extract($row2);
    if ($open == TRUE)
        $db->disconnect();

    return $dloc_id;
}

/**
 * Vérification de la présence d'un dépôt dans un produit à louer
 *
 * @param $nom
 * @param bool $open
 * @return bool
 */
function exist_dloc_in_pdtloc($nom, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBDLOC . "`, `" . TBPDTLOC . "`";
    $rexist .= " WHERE dloc_nom = '" . $db->escape($nom) . "'";
    $rexist .= " AND " . TBDLOC . ".dloc_id = " . TBPDTLOC . ".pdtloc_iddloc";
    $nb_rs = $db->get_var($rexist);
    if ($nb_rs <> 0)
        $exist = TRUE;
    if ($open == TRUE)
        $db->disconnect();

    return $exist;
}

/**
 * Vérification de la présence d'un dépôt dans un produit louable
 *
 * @param $nom
 * @param bool $open
 * @return bool
 */
function exist_dloc_in_pdtloc_louable($nom, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBDLOC . "`, `" . TBPDTLOC . "`";
    $rexist .= " WHERE " . TBDLOC . ".dloc_nom = '" . $db->escape($nom) . "'";
    $rexist .= " AND " . TBDLOC . ".dloc_id = " . TBPDTLOC . ".pdtloc_iddloc";
    $rexist .= " AND " . TBPDTLOC . ".pdtloc_louable = '1'";
    $nb_rs = $db->get_var($rexist);
    if ($nb_rs <> 0)
        $exist = TRUE;
    if ($open == TRUE)
        $db->disconnect();

    return $exist;
}
?>