<?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");

include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_prm.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$titre = @$_POST["titre"];
$td = @$_POST["td"];
$tx = @$_POST["tx"];

$codea = str_majuscules(@$_POST["codea"]);
$coden = intval(@$_POST["coden"]);
$nom = @$_POST["nom"];
$symiso = @$_POST["symiso"];
$symtxt = @$_POST["symtxt"];
$defaut = intval(@$_POST["defaut"]);
$lng = str_majuscules(@$_POST["lng"]);

$retourliste = TRUE;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche Devise
    case "add" :
        $rech = "SELECT COUNT(*) FROM `" . TBDEV . "` WHERE `dev_codea` = '" . $db->escape($codea) . "' AND `dev_lng` = '$lng'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            // On vérifie si monnaie par défaut
            if ($defaut == 1) {
                // Si on a choisi "par défaut", on décide que toutes les autres monnaies ne le sont pas
                $rech = "UPDATE `" . TBDEV . "` SET `dev_defaut`='0'";
                $rech .= " WHERE `dev_codea` <> '" . $db->escape($codea) . "'";
                $db->query($rech);
            }
            $rech2 = "INSERT INTO `" . TBDEV . "` (dev_codea,dev_coden,dev_nom,dev_symiso,dev_symtxt,dev_defaut,dev_lng)";
            $rech2 .= " VALUES ('" . $db->escape($codea) . "','" . $db->escape($coden) . "','" . $db->escape($nom) . "','" . $db->escape($symiso) . "',";
            $rech2 .= "'" . $db->escape($symtxt) . "','" . $db->escape($defaut) . "','$lng')";
            $db->query($rech2);
            $msg = _("Fiche ajoutée");
        } else {
            $msg = _("Ce code existe déjà");
            $retourliste = FALSE;
        }
        break;

    // Modification de la fiche Devise
    case "edit" :
        $rech = "SELECT COUNT(*) FROM `" . TBDEV . "` WHERE `dev_codea` = '" . $db->escape($codea) . "' AND `dev_lng` = '$lng'";
        $nbres = $db->get_var($rech);
        if ($nbres == 1) {
            // On vérifie si monnaie par défaut
            if ($defaut == 1) {
                // Si on a choisi "par défaut", on décide que toutes les autres monnaies ne le sont pas
                $rech = "UPDATE `" . TBDEV . "` SET `dev_defaut`='0'";
                $rech .= " WHERE `dev_codea` <> '" . $db->escape($codea) . "'";
                $db->query($rech);
            }

            $rech = "UPDATE `" . TBDEV . "` SET `dev_coden`='" . $db->escape($coden) . "', `dev_nom`='" . $db->escape($nom) . "',";
            $rech .= " `dev_symiso`='" . $db->escape($symiso) . "', `dev_symtxt`='" . $db->escape($symtxt) . "',";
            $rech .= " `dev_defaut`='" . $db->escape($defaut) . "', `dev_lng`='" . $lng . "'";
            $rech .= " WHERE `dev_codea` = '" . $db->escape($codea) . "' AND `dev_lng` = '" . $lng . "'";
            $db->query($rech);
            $msg = _("Fiche modifiée");
        } else {
            $msg = _("Modification impossible");
            $retourliste = FALSE;
        }
        break;

    // Suppression de la fiche Devise
    case "del" :
        // Vérifie si on peut supprimer cette devise
        $delok = TRUE;

        // Fiche suppressible ?
        $rexist = "SELECT COUNT(*) FROM `" . TBDEV . "` WHERE `dev_codea` = '" . $db->escape($codea) . "' AND `dev_delok` = '1' AND `dev_lng`='$lng'";
        $nbres = $db->get_var($rexist);
        if ($nbres == 0)
            $delok = FALSE;

        // Fiche par défaut ?
        $rexist = "SELECT COUNT(*) FROM `" . TBDEV . "` WHERE `dev_codea` = '" . $db->escape($codea) . "' AND `dev_defaut` = '1' AND `dev_lng`='$lng'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans un pays ?
        $rexist = "SELECT COUNT(pays_id) FROM `" . TBPAYS . "` WHERE `pays_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans une agence ?
        $rexist = "SELECT COUNT(age_id) FROM `" . TBAGE . "` WHERE `age_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans un sous-traitant ?
        $rexist = "SELECT COUNT(st_id) FROM `" . TBST . "` WHERE `st_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans un donneur d'ordres ?
        $rexist = "SELECT COUNT(do_id) FROM `" . TBDO . "` WHERE `do_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans un client ?
        $rexist = "SELECT COUNT(cli_id) FROM `" . TBCLI . "` WHERE `cli_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans un contrat ?
        $rexist = "SELECT COUNT(cocl_id) FROM `" . TBCOCL . "` WHERE `cocl_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans un contrat de location ?
        $rexist = "SELECT COUNT(coloc_id) FROM `" . TBCOLOC . "` WHERE `coloc_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans un produit de location ?
        $rexist = "SELECT COUNT(pdtloc_id) FROM `" . TBPDTLOC . "` WHERE `pdtloc_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        // Utilisée dans un dossier de suivi ?
        $rexist = "SELECT COUNT(dos_id) FROM `" . TBDOS . "` WHERE `dos_codeadev` = '" . $db->escape($codea) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0)
            $delok = FALSE;

        if ($delok == TRUE) {
            $rech = "DELETE FROM `" . TBDEV . "` WHERE `dev_codea` = '" . $db->escape($codea) . "' AND `dev_lng`='$lng'";
            $db->query($rech);
            $msg = _("Fiche supprimée");
        } else {
            $msg = _("Suppression impossible");
            $retourliste = FALSE;
        }
        break;
}
$db->disconnect();
?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("Fiche Devise"); ?></title>

        <?PHP include_once("../inc/header.inc.php"); ?>

        <script type="text/javascript">
            <!--
            function init_all() {
                init_Body();
                <?PHP
                if ($tx <> 'f')
                {
                    if ($retourliste == FALSE)
                        echo "warn_txt('" . str_escape($msg) . "');";
                }
                ?>
                Focus_first();
            }

            $(document).ready(function () {
                init_all();
            });
            //-->
        </script>

    </head>
<body>
<?PHP
if ($tx <> "f")
    include_once("../inc/entete.inc.php");
?>
    <p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
    <p align="center">&nbsp;</p>
<?PHP
if ($tx <> "f") {
    if ($retourliste == FALSE)
        aff_ret_idx_man($td, "dev_lst.php", _("Retour à la liste"), TRUE);
    else
        pop_ret_auto($msg, 'ok', "dev_lst.php");

} else {
    close_fen_auto('', 'fscomp');
}
if ($tx <> "f")
    include_once("pied.php")
?>