<?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 TVA      */
/* et taxes parafiscales                          */
/**************************************************/

// TVA

/**
 * Vérification du nombre de TVA
 *
 * @param bool $open
 * @return null
 */
function nb_tva($open = TRUE)
{
    global
    $db;

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

    return $nbexist;
}

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

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

    return $row2;
}

/**
 * Retourne le code TVA par défaut
 *
 * @param bool $open
 * @return array|null
 */
function give_code_tva_default($open = TRUE)
{
    global
    $db;

    $tva_code = '';

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT tva_code FROM `' . TBTVA . '` WHERE `tva_default` = 1';
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
    }
    if ($open == TRUE) {
        close_database();
    }

    return $tva_code;
}


/**
 * Vérifie si une TVA est modifiable
 * Non, si déjà utilisée  :
 *     dans une intervention
 *     dans une fiche produit intervention
 *     dans un contrat signé
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function tva_modifiable($code, $open = TRUE)
{
    global
    $db;
    $used = FALSE;

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

    // Dans une fiche produit intervention
    $rech2 = 'SELECT COUNT(*) FROM `' . TBPDTI . "` WHERE pdti_codetva = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

    // Dans une intervention
    $rech2 = 'SELECT COUNT(*) FROM `' . TBTPFI . "` WHERE tpfi_codetva = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

    // Dans un contrat signé
    $rech2 = 'SELECT COUNT(*) FROM `' . TBCOCL . "` WHERE cocl_codetva = '" . $db->escape($code) . "' AND cocl_datesig <> '0000-00-00' ";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

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

    return !$used;
}


/**
 * Vérifie si une TVA est désactivable
 * Non, si déjà utilisée  :
 *     dans une fiche produit d'une intervention non close
 *     dans un contrat non terminé
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function tva_desactivable($code, $open = TRUE)
{
    global
    $db;
    $used = FALSE;

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

    // Dans une fiche produit d'une intervention non close
    $rech2 = 'SELECT COUNT(*) FROM `' . TBINT . '`, `' . TBPDTI . '` WHERE ' . TBINT . '.int_code = ' . TBPDTI . '.pdti_codeint AND ' . TBINT . ".int_datefin = '0000-00-00' AND " . TBPDTI . ".pdti_codetva = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

    // Dans une intervention non close
    $rech2 = 'SELECT COUNT(*) FROM `' . TBINT . '`, `' . TBTPFI . '` WHERE ' . TBINT . '.int_code = ' . TBTPFI . '.tpfi_codeint AND ' . TBINT . ".int_datefin = '0000-00-00' AND " . TBTPFI . ".tpfi_codetva = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

    // Dans un contrat non clot
    $rech2 = 'SELECT COUNT(*) FROM `' . TBCOCL . "` WHERE cocl_codetva = '" . $db->escape($code) . "' AND cocl_datefin = '0000-00-00' ";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

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

    return !$used;
}


// Taxes Parafiscales

/**
 * Vérification du nombre de taxes
 *
 * @param bool $open
 * @return null
 */
function nb_tpf($open = TRUE)
{
    global
    $db;

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

    return $nbexist;
}

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

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

    return $row2;
}

/**
 * Donne le nombre de taxes dans une fiche article
 *
 * @param $codep
 * @param bool $open
 * @return null
 */
function nb_tpf_in_pdt($codep, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT COUNT(*) FROM `' . TBTPFA . "` WHERE `tpfa_codepdt` = '" . $db->escape($codep) . "'";
    $nbtpf = $db->get_var($rech2);
    if ($open == TRUE) {
        close_database();
    }

    return $nbtpf;
}

/**
 * Lit une taxe dans une fiche article
 *
 * @param $codet
 * @param $codep
 * @param bool $open
 * @return array|null
 */
function lit_enr_tpf_in_pdt($codet, $codep, $open = TRUE)
{
    global
    $db;

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

    $rech2 = 'SELECT * FROM `' . TBTPFA . "` WHERE `tpfa_codetpf` = '" . $db->escape($codet) . "' AND `tpfa_codepdt` = '" . $db->escape($codep) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérification de la présence d'une taxe dans une fiche intervention
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_tpf_in_int($code, $open = TRUE)
{
    global
    $db;

    $exist = FALSE;

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

    return $exist;
}

/**
 * Lit une taxe dans une fiche intervention
 *
 * @param $codet
 * @param $codei
 * @param bool $open
 * @return array|null
 */
function lit_enr_tpf_in_int($codet, $codei, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBTPFI . "` WHERE `tpfi_codetpf` = '" . $db->escape($codet) . "' AND `tpfi_codeint` = '" . $db->escape($codei) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérification de la présence d'une taxe dans une fiche intervention non close
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_tpf_in_int_ouv($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

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

    $rech2 = 'SELECT ' . TBTPFI . '.tpfi_codetpf, ' . TBTPFI . '.tpfi_codeint, ' . TBINT . '.int_code, ';
    $rech2 .= TBINT . '.int_datefin FROM `' . TBTPFI . '`, `' . TBINT . '` WHERE (' . TBTPFI . ".tpfi_codetpf = '" . $db->escape($code) . "' AND " . TBTPFI . '.tpfi_codeint = ' . TBINT . ".int_code AND int_datefin = '0000-00-00')";
    $rows2 = $db->get_results($rech2, ARRAY_A);
    if (count($rows2) > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérifie si une taxe est modifiable
 * Non, si déjà utilisée  :
 *     dans une fiche produit intervention
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function tpf_modifiable($code, $open = TRUE)
{
    global
    $db;
    $used = FALSE;

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

    // Dans une fiche produit intervention
    $rech2 = 'SELECT COUNT(*) FROM `' . TBTPFA . "` WHERE tpfa_codetpf = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

    // Dans une intervention
    $rech2 = 'SELECT COUNT(*) FROM `' . TBTPFI . "` WHERE tpfi_codetpf = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

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

    return !$used;
}


/**
 * Vérifie si une taxe est désactivable
 * Non, si déjà utilisée  :
 *     dans une fiche produit d'une intervention non close
 *     dans une intervention non close
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function tpf_desactivable($code, $open = TRUE)
{
    global
    $db;
    $used = FALSE;

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

    // Dans une fiche produit d'une intervention non close
    $rech2 = 'SELECT * FROM `' . TBINT . '`, `' . TBPDTI . '` WHERE ' . TBINT . '.int_code = ' . TBPDTI . '.pdti_codeint';
    $rech2 .= ' AND ' . TBINT . ".int_datefin = '0000-00-00'";
    $rows2 = $db->get_results($rech2, ARRAY_A);
    if ($rows2 AND (count($rows2) > 0)) {
        foreach ($rows2 as $row2) {
            extract($row2);
            $rech3 = 'SELECT COUNT(*) FROM `' . TBTPFA . "` WHERE tpfa_codetpf = '" . $db->escape($code) . "' AND tpfa_codepdt = '$pdti_codepdt'";
            $nbres3 = $db->get_var($rech3, ARRAY_A);
            if ($nbres3 > 0) {
                $used = TRUE;
                break;
            }
        }
    }

    // Dans une intervention non close
    $rech2 = 'SELECT COUNT(*) FROM `' . TBINT . '`, `' . TBTPFI . '` WHERE ' . TBINT . '.int_code = ' . TBTPFI . '.tpfi_codeint';
    $rech2 .= ' AND ' . TBINT . ".int_datefin = '0000-00-00' AND " . TBTPFI . ".tpfi_codetpf = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($nbexist > 0) {
        $used = TRUE;
    }

    if ($open == TRUE) {
        close_database();
    }
    return !$used;
}
