<?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 donneurs d'ordres  */
/* et fichiers rattachés                                    */
/************************************************************/

/**
 * Vérification du type de facturation sur un donneur d'ordre (par le CDG ou par le donneur d'ordres)
 *
 * @param $code
 * @param bool $open
 * @return int
 */
function mod_fact_do($code, $open = TRUE)
{
    global
    $db;
    $fact_do = 0;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT do_fact FROM `' . TBDO . "` WHERE do_code = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        $fact_do = $row2['do_fact'];
    }
    if ($open == TRUE) {
        close_database();
    }

    return $fact_do;
}

/**
 * Vérification du nombre de donneurs d'ordres actifs
 *
 * @param bool $open
 * @return int|null
 */
function nb_do_actifs($open = TRUE)
{
    global
    $db;

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

    return $nbexist;
}

/**
 * Donne le nombre de donneurs d'ordres
 *
 * @param bool $open
 * @return int|null
 */
function nb_do($open = TRUE)
{
    global
    $db;

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

    return $nbexist;
}

/**
 * Lecture d'un enregistrement donneur d'ordres
 *
 * @param $code
 * @param bool $open
 * @param string $typ
 * @return array|null
 */
function lit_enr_do($code, $open = TRUE, $typ = '')
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBDO . "` WHERE do_code = '" . $db->escape($code) . "'";
    switch ($typ) {
        case 'num' :
            $row2 = $db->get_row($rech2, ARRAY_N);
            break;
        default :
            $row2 = $db->get_row($rech2, ARRAY_A);
            break;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérification du nombre d'interventions sur un donneur d'ordres
 *
 * @param $do
 * @param bool $open
 * @return int
 */
function nb_int_do($do, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    if ($do <> '') {
        $rexist = 'SELECT COUNT(*) FROM `' . TBINT . "` WHERE `int_codedo` = '" . $db->escape($do) . "'";
    } else {
        $rexist = 'SELECT COUNT(*) FROM `' . TBINT . '`';
    }
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Vérification de la présence d'un donneur d'ordres dans une fiche intervention
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_do_in_int($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBINT . "` WHERE `int_codedo` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérification de la présence d'un donneur d'ordres dans une fiche intervention non close
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_do_in_int_ouv($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBINT . "` WHERE `int_codedo` = '" . $db->escape($code) . "' AND `int_datefin` = '0000-00-00'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérification de la présence d'un donneur d'ordres dans une fiche technicien non licencié
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_do_in_tec_present($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "` WHERE `tec_codedo` = '" . $db->escape($code) . "' AND `tec_out` = '0'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérification de la présence d'un donneur d'ordres dans une fiche technicien
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_do_in_tec($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "` WHERE `tec_codedo` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

// Contacts Donneurs d'ordres

/**
 * Vérification du nombre de contacts dans une fiche donneur d'ordres
 *
 * @param $code
 * @param bool $open
 * @return null
 */
function nb_cdo_in_do($code, $open = TRUE)
{
    global
    $db;

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

    return $nbexist;
}

/**
 * Lecture d'un contact donneur d'ordres
 *
 * @param $id
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_cdo_id_in_do($id, $code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT * FROM `' . TBCDO . "` WHERE `cdo_id` = '$id' AND `cdo_codedo` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rexist, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Suppression des contacts dans une fiche donneur d'ordres
 *
 * @param $code
 * @param bool $open
 */
function del_cdo_in_do($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'DELETE FROM `' . TBCDO . "` WHERE `cdo_codedo` = '" . $db->escape($code) . "'";
    $db->query($rexist);
    if ($open == TRUE) {
        close_database();
    }
}

/**
 * Création de la liste des donneurs d'ordres ouverts
 * $except : sauf un donneur d'ordres (si vide, on en tient pas compte)
 *
 * @param string $except
 * @param bool $open
 * @return array
 */
function give_tab_do($except = '', $open = TRUE)
{
    global
    $db;

    $tab_res = [];

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }

    $rech = 'SELECT do_code,do_nom FROM `' . TBDO . "` WHERE `do_ouvert` = '1'";

    if ($except <> '') {
        $rech .= " AND `do_code` <> '" . $except . "'";
    }
    $rech .= ' ORDER BY do_nom';

    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            $tab_res[$do_code] = $do_nom . ' (' . $do_code . ')';
        }
    }

    if ($open == TRUE) {
        close_database();
    }

    return $tab_res;
}

/**
 * Retourne la liste des contacts donneur d'ordre
 *
 * @param $code
 * @param bool $open
 * @return array
 */
function give_tab_cdo($code, $open = TRUE)
{
    global
    $db;

    $tab_cdo = [];

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }

    // Récupération du contact principal
    $rech = 'SELECT do_resp_nom,do_resp_prenom, do_resp_telf, do_resp_telp FROM `' . TBDO . "` WHERE `do_code` = '" . $db->escape($code) . "'";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        $row = encode_str($row);
        extract($row);

        // Contact principal
        $txt_cdo = $do_resp_nom . ' ' . $do_resp_prenom . ' (';
        $txt_cdo .= _('Contact principal');
        $txt_cdo .= ' - ' . $do_resp_telf;
        if ($do_resp_telp <> '') {
            $txt_cdo .= ' / ' . $do_resp_telp;
        }
        $txt_cdo .= ')';
        $tab_cdo[-1] = $txt_cdo;
    }
    // Contacts du donneur d'ordres
    $rech = 'SELECT * FROM `' . TBDO . "` WHERE `cdo_codedo` = '" . $db->escape($code) . "'";
    $rech .= ' ORDER BY cdo_nom';
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);

            $txt_cdo = $cdo_nom . ' ' . $cdo_prenom;
            $row2 = lit_enr_fonc($cdo_codefonc, FALSE);
            if (is_array($row2)) {
                $row2 = encode_str($row2);
                extract($row2);
                $txt_fonc = $fonc_desc;
            } else {
                $txt_fonc = $cdo_codefonc;
            }
            $txt_cdo .= ' (';
            $txt_cdo .= $txt_fonc;
            $txt_cdo .= ' - ' . $cdo_telf;
            if ($cdo_telp <> '') {
                $txt_cdo .= ' / ' . $cdo_telp;
            }
            $txt_cdo .= ')';
            $tab_cdo[$cdo_id] = $txt_cdo;
        }
    }
    if ($open == TRUE) {
        close_database();
    }

    return $tab_cdo;
}
