<?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_dloc.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_geol.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../classes/geoplugin/geoplugin.class.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();
$_GET = clean_str($_GET);

$td = @$_GET['td'];
$coderech = @$_GET['nom'];
$dloc_dep = @$_GET['dep'];

$pbm_dloc = FALSE;

if ($coderech <> "") {
    // Récupération des informations Dépôt
    $row = lit_enr_dloc($coderech);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
        if (($td <> "del") AND ($dloc_ouvert == FALSE))
            $td = "view";
    } else
        $pbm_dloc = TRUE;
} else {
    if ($td == "add") {
        $dloc_ouvert = TRUE;
        $dloc_codepays = pays_connect_code($_SERVER['REMOTE_ADDR']);
    } else
        $pbm_dloc = TRUE;
}

switch ($td) {
    case "add" :
        $titre = _("CREATION DEPOT");
        break;
    case "edit" :
        $titre = _("MISE A JOUR DEPOT");
        break;
    case "del" :
        $titre = _("SUPPRESSION DEPOT");
        break;
    case "view" :
        $titre = _("VISUALISATION DEPOT");
        break;
    default :
        $pbm_dloc = TRUE;
        break;
}
?>

<!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 Dépôt de Location"); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:nom ; format:txt ; req:Y ; txt:<?PHP echo _("Code"); ?>; lmn:1 ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:adr1 ; format:txt ; req:Y ; txt:<?PHP echo _("Adresse"); ?> ; lmn:1 ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:adr2 ; format:txt ; req:N ; txt:<?PHP echo _("Adresse (2)"); ?> ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:ville ; format:liste ; req:Y ; txt:<?PHP echo _("Ville"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:dep ; format:liste ; req:Y ; txt:<?PHP echo _("Département"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:pays ; format:liste ; req:Y ; txt:<?PHP echo _("Pays"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:telf ; format:tel ; req:Y ; txt:<?PHP echo _("Téléphone Fixe"); ?>; stok:frmok ; stbad:frmbad",
            "nom:fax ; format:tel ; req:N ; txt:<?PHP echo _("Télécopieur"); ?>; stok:frmok ; stbad:frmbad",
            "nom:rcivil ; format:liste ; req:Y ; txt:<?PHP echo _("Civilité responsable d'agence"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:rprenom ; format:txt ; req:Y ; txt:<?PHP echo _("Prénom responsable d'agence"); ?> ; lmn:1; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:rnom ; format:txt ; req:Y ; txt:<?PHP echo _("Nom responsable d'agence"); ?>; lmn:1; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:rtelf ; format:tel ; req:Y ; txt:<?PHP echo _("N° tel fixe responsable d'agence"); ?>; stok:frmok ; stbad:frmbad",
            "nom:email ; format:mail ; req:Y ; txt:<?PHP echo _("Email")." "._("Général"); ?> ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:emaild ; format:mail ; req:Y ; txt:<?PHP echo _("Email")." "._("Direction"); ?> ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:emails ; format:mail ; req:N ; txt:<?PHP echo _("Email")." "._("Gestionnaire de stock"); ?> ; lmx:50; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <?PHP if ($pbm_dloc == FALSE) { ?>
        <script type="text/javascript">
            <!--
            $(function () {
                $("#code").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#nom").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#adr1").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#adr2").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#rnom").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#email").Setcase({caseValue: 'lower', changeonFocusout: true});
                $("#emaild").Setcase({caseValue: 'lower', changeonFocusout: true});
                $("#emails").Setcase({caseValue: 'lower', changeonFocusout: true});
                $("#rprenom").Setcase({caseValue: 'pascal', changeonFocusout: true});
            });

            function init_all() {
                init_Body();
                <?php
                switch ($td)
                {
                    case "add" :
                        echo "init_valid_form('enr','dloc_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                        break;
                    case "edit" :
                        echo "init_valid_form('enr','dloc_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                        echo "readonly_id('nom',false);";
                        break;
                    case "view" :
                        echo "readonly_all();";
                        break;
                    case "del" :
                        echo "readonly_all();";
                        echo "warn_del('');";
                        break;
                }
                if (@$_SESSION["niv_log"] < 5)
                    echo "readonly_id('nom-adr1-adr2',false);";
                ?>
                init_ajax_ville();
                init_ajax_dep();
                init_back('annul', 'click');
                Focus_first();
            }

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

    <?PHP } ?>
</head>

<body>

<?PHP
include_once("../inc/entete.inc.php");
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<!-- affiché uniquement s'il y a au moins une civilité -->
<?PHP
if ($pbm_dloc == FALSE) {
    if (nb_civ() <> 0) {
        cre_ent_form($titre, 'dloc_lst.php', $_SERVER['HTTP_REFERER'], '');
        ?>
        <!-- Formulaire de saisie de la fiche -->
        <form id="dloc_maj" name="dloc_maj" id="dloc_maj" method="post" action="dloc_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
        <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
        <?PHP if ($td == "add") { ?>
            <input name="ouvert" type="hidden" id="ouvert" value="Oui">
        <?PHP } ?>
        <input name="codeage" type="hidden" id="codeage" value="<?PHP echo $dloc_codeage ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">

        <!-- Ouverture (avec dates) du Dépôt -->
        <?PHP
        if (($td == "edit") OR ($td == "del") OR ($td == "view")) {
            ?>
            <table width="100%" border="0">
                <tr>
                    <td width="160" class="rubfrm" id="f_ouvert" align="right"><?PHP echo _("Dépôt Ouvert"); ?></td>
                    <td>
                        <?PHP
                        cre_select('ouvert', 'ouvert', $non_oui, (int)$dloc_ouvert);
                        echo '<span class="annot_std_it">';
                        if ($dloc_ouvert == 1)
                            echo ' (' . _("Depuis le") . ' : ' . datetolocal($dloc_datecrea) . ')';
                        else
                            echo ' (' . _("Ouvert le") . ' : ' . datetolocal($dloc_datecrea) . ', ' . _("fermé le") . ' : ' . datetolocal($dloc_datec) . ')';
                        ?>
                    </td>
                </tr>
            </table>
        <?PHP
        }
        ?>

        <!-- Fiche principale -->
        <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_nom" align="right"><?PHP echo _("Nom"); ?></td>
            <td>
                <input name="nom" type="text" id="nom" value="<?PHP echo $dloc_nom ?>" size="50">
                <?PHP
                if ($td == "add")
                    echo ' <img src="../img/oblig.png" align="absmiddle" alt="'._("Champ obligatoire").'">';
                ?>
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_adr1" align="right"><?PHP echo _("Adresse"); ?></td>
            <td>
                <input name="adr1" type="text" id="adr1" value="<?PHP echo $dloc_adr1 ?>" size="50">
                 <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_adr2" align="right"><?PHP echo _("Adresse (2)"); ?></td>
            <td>
                <input name="adr2" type="text" id="adr2" value="<?PHP echo $dloc_adr2 ?>" size="50">
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_pays" align="right"><?PHP echo _("Pays"); ?></td>
            <td>
                <?PHP
                // Création de la liste des pays
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBPAYS . "` ORDER BY pays_nom";
                $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('pays', 'pays', $rows, 'pays_code', 'pays_nom', '', $dloc_codepays, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                ?>
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_dep" align="right"><?PHP echo _("Département"); ?></td>
            <td>
                <?PHP
                // Création de la liste des départements
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT dep_code,dep_nom FROM `" . TBDEP . "` WHERE `dep_codepays`='" . $dloc_codepays . "' ORDER BY dep_code";
                $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('dep', 'dep', $rows, 'dep_code', 'dep_code', 'dep_nom', $dloc_dep, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                ?>
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_ville" align="right"><?PHP echo _("Ville"); ?></td>
            <td>
                <?PHP
                // Création de la liste des villes
                $rech = "";
                if ($dloc_dep <> "") {
                    if ($dloc_dep <> "-1") {
                        $filtre = $dloc_dep;
                        if (($filtre == "2A") OR ($filtre == "2B"))
                            $filtre = "20";
                        $rech = "SELECT * FROM `" . TBVILLES . "` WHERE `villes_dep` = '" . str_majuscules($filtre) . "' AND `villes_codepays`='" . str_majuscules($dloc_codepays) . "' ORDER BY villes_nom";
                    } else {
                        $rech = "SELECT * FROM `" . TBVILLES . "` WHERE `villes_codepays`='" . str_majuscules($dloc_codepays) . "' ORDER BY villes_nom";
                    }
                }
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                if ($rech <> "")
                    $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('ville', 'ville', $rows, 'villes_id', 'villes_nom', 'villes_cp', $dloc_ville, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                ?>
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_telf" align="right">
                <?PHP echo _("Téléphone Fixe"); ?><br />
            </td>
            <td>
                <input name="telf" type="text" id="telf" value="<?PHP echo $dloc_telf ?>" size="20" maxlength="20">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>"><br />
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_fax" align="right">
                <?PHP echo _("Télécopieur"); ?><br />
            </td>
            <td>
                <input name="fax" type="text" id="fax" value="<?PHP echo $dloc_fax ?>" size="20" maxlength="20">
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>
        <tr>
            <td colspan="2" class="rubfrm" id="f_web" align="center"><?PHP echo _("Responsable du dépôt"); ?></td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_rcivil" align="right"><?PHP echo _("Civilité"); ?></td>
            <td>
                <?PHP
                // Création de la liste des civilités
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBCIV . "` ORDER BY civ_desc";
                $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('rcivil', 'rcivil', $rows, 'civ_code', 'civ_desc', '', $dloc_resp_civilite, '', _("-- Faites votre choix --"));
                ?>
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_rprenom" align="right"><?PHP echo _("Prénom"); ?></td>
            <td>
                <input name="rprenom" type="text" id="rprenom" value="<?PHP echo $dloc_resp_prenom ?>" size="50">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_rnom" align="right"><?PHP echo _("Nom"); ?></td>
            <td>
                <input name="rnom" type="text" id="rnom" value="<?PHP echo $dloc_resp_nom ?>" size="50">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
        <tr>
            <td class="rubfrm" id="f_rtelf" align="right"><?PHP echo _("Tel. Fixe"); ?></td>
            <td>
                <input name="rtelf" type="text" id="rtelf" value="<?PHP echo $dloc_resp_telf ?>" size="20" maxlength="20">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td colspan="2" class="rubfrm" align="center"><?PHP echo _("Emails"); ?></td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_email" align="right"><?PHP echo _("Général"); ?></td>
            <td>
                <input name="email" type="text" id="email" value="<?PHP echo $dloc_email ?>" size="50" maxlength="50">
                <?PHP if ($td == "edit") { ?>
                    <a href="mailto:<?PHP echo $dloc_email ?>"><img src="../img/mail.png"
                                                                    title="<?PHP echo _("Envoyer e-mail"); ?>"
                                                                    border="0" align="absmiddle"></a>
                <?PHP } ?>
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_emaild" align="right"><?PHP echo _("Direction"); ?></td>
            <td>
                <input name="emaild" type="text" id="emaild" value="<?PHP echo $dloc_emaild ?>" size="50"
                       maxlength="50">
                <?PHP if ($td == "edit") { ?>
                    <a href="mailto:<?PHP echo $dloc_emaild ?>"><img src="../img/mail.png"
                                                                     title="<?PHP echo _("Envoyer e-mail"); ?>"
                                                                     border="0" align="absmiddle"></a>
                <?PHP } ?>
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_emails" align="right"><?PHP echo _("Gestionnaire du stock"); ?></td>
            <td>
                <input name="emails" type="text" id="emails" value="<?PHP echo $dloc_emails ?>" size="50"
                       maxlength="50">
                <?PHP if ($td == "edit") { ?>
                    <a href="mailto:<?PHP echo $dloc_emails ?>"><img src="../img/mail.png"
                                                                     title="<?PHP echo _("Envoyer e-mail"); ?>"
                                                                     border="0" align="absmiddle"></a>
                <?PHP } ?>
            </td>
        </tr>
        </table>

        <p align="center">
        <?PHP
        switch ($td)
        {
            case "add" :
            case "edit" :
                echo '<input name="enr" type="button" class="bton_std" id="enr" value="'._("Valider").'">';
                break;
            case "del" :
                echo '<input name="del" type="submit" class="bton_std" id="del" value="'._("Supprimer").'">';
                break;
        }
        echo ' <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
        ?>
        </p>
        </form>
        <!-- Fin affiché uniquement s'il y a au moins une civilité -->
    <?PHP
    } else {
        if (nb_civ() == 0)
            pop_ret_auto(_("Aucune civilité"), 'warn', "civ_lst.php");
    }
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
include_once("pied.php");
?>
</body>
</html>