<?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');
$ok_inc = FALSE;
if ($pos === FALSE) {
    $ok_inc = TRUE;
}
if ($ok_inc == FALSE) {
    header('Location: ../index.php');
}

include_once('../inc/fic_age.inc.php');
include_once('../inc/fic_cli.inc.php');
include_once('../inc/fic_cocl.inc.php');
include_once('../inc/fic_dos.inc.php');
include_once('../inc/fic_droits.inc.php');
include_once('../inc/fic_int.inc.php');
include_once('../inc/fic_loc.inc.php');
include_once('../inc/fic_ot.inc.php');
include_once('../inc/fic_rel.inc.php');
include_once('../inc/fic_st.inc.php');
include_once('../inc/fic_tac.inc.php');
include_once('../inc/fic_tec.inc.php');
include_once('../inc/fic_zc.inc.php');
include_once('../inc/func.inc.php');
include_once('../inc/func_time.inc.php');
include_once('../inc/func_date.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(valsession('code_log'));
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$td = $_GET['td']; // Récupération type demande
$ct = $_GET['ct']; // Récupération code Tâche

$pbm_tac = FALSE;
$sais_datefinp = TRUE;
$sais_datefin = TRUE;
$readonly = '';
$tr = '';

if ($ct <> '') {
    // Récupération des informations tâche
    $row = lit_enr_tac($ct);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        if ($td <> 'add') {
            // Contrôle qu'on n'essaye pas de "hacker" une tâche
            switch (valsession('niv_log')) {
                // Technicien agence
                case 3 :
                    if (($tac_typdest <> 1) OR ($tac_codedest <> valsession('code_log'))) {
                        $pbm_tac = TRUE;
                    }
                    break;
                // Agence
                case 4 :
                    if (($tac_typdest <> 1) AND ($tac_typdest <> 2)) {
                        $pbm_tac = TRUE;
                    } else {
                        if ($tac_typdest == 1) {
                            $row2 = lit_enr_tec($tac_codedest, TRUE);
                            if (is_array($row2)) {
                                $row2 = encode_str($row2);
                                extract($row2);
                                if ($tec_codeage <> valsession('code_log')) {
                                    $pbm_tac = TRUE;
                                }
                            } else {
                                $pbm_tac = TRUE;
                            }
                        } else {
                            if ($tac_codedest <> valsession('code_log')) {
                                $pbm_tac = TRUE;
                            }
                        }
                    }
                    break;
                // Sous-traitant
                case 10 :
                    if (($tac_typdest <> 1) AND ($tac_typdest <> 2) AND ($tac_typdest <> 3) AND ($tac_typdest <> 4)) {
                        $pbm_tac = TRUE;
                    } else {
                        if (($tac_typdest == 1) OR ($tac_typdest == 3)) {
                            $row2 = lit_enr_tec($tac_codedest, TRUE);
                            if (is_array($row2)) {
                                $row2 = encode_str($row2);
                                extract($row2);
                                if ($tec_codest <> valsession('code_log')) {
                                    $pbm_tac = TRUE;
                                }
                            } else {
                                $pbm_tac = TRUE;
                            }
                        } else {
                            if ($tac_codedest <> valsession('code_log')) {
                                $pbm_tac = TRUE;
                            }
                        }
                    }
                    break;
                // Technicien sous-traitant
                case 11 :
                    if ((($tac_typdest <> 1) AND ($tac_typdest <> 3)) OR ($tac_codedest <> valsession('code_log'))) {
                        $pbm_tac = TRUE;
                    }
                    break;
            }
            if ($tac_codedest == valsession('code_log')) {
                $tac_typdest = -1;
            }
        }

        // Formatage des dates en français
        $tac_datecreaf = datetolocal($tac_datecrea);
        $tac_datemodf = datetolocal($tac_datemod);
        $tac_datefinpf = datetolocal($tac_datefinp);
        $tac_datefinf = datetolocal($tac_datefin);

        // On vérifie si le droit de modification est ok
        if (($tac_codecrea <> valsession('code_log')) AND (valsession('niv_log') <> 9)) {
            $td = 'view';
        }

        // Si dossier terminé, on force le mode "view"
        if ($tac_datefinf <> '') {
            $td = 'view';
        }
    } else {
        $pbm_tac = TRUE;
    }
} else {
    if ($td == 'add') {
        $tac_code = crecodesession('TA');
        $ct = $tac_code;
    } else {
        $pbm_tac = TRUE;
    }
}
// On vérifie si appelé par ligne de commande (uniquement en ajout) + tentative de hack
if ($pbm_tac == FALSE) {
    if (isset($_GET['tr'])) {
        if ($td == 'add') {
            $tr = (int)$_GET['tr']; // Récupération type rattaché
            if (isset($_GET['cr'])) {
                $cr = $_GET['cr']; // Récupération code rattaché
                switch ($tr) {
                    // Intervention
                    case 1 :
                        $row = lit_enr_int($cr);
                        if (is_array($row)) {
                            extract($row);
                            switch (valsession('niv_log')) {
                                case 3 :
                                case 4 :
                                    if (($int_codeage <> valsession('code_age')) AND ($int_codeages <> valsession('code_age'))) {
                                        $pbm_tac = TRUE;
                                    }
                                    break;
                                case 5 :
                                case 9 :
                                    break;
                                default :
                                    $pbm_tac = TRUE;
                                    break;
                            }
                            if (datetolocal($int_datefin) <> '') {
                                $pbm_tac = TRUE;
                            }
                        } else {
                            $pbm_tac = TRUE;
                        }
                        break;
                    // Contrat de maintenance
                    case 2 :
                        $row = lit_enr_cocl($cr);
                        if (is_array($row)) {
                            extract($row);
                            switch (valsession('niv_log')) {
                                case 3 :
                                case 4 :
                                    if ($cocl_codeage <> valsession('code_age')) {
                                        $pbm_tac = TRUE;
                                    }
                                    break;
                                case 5 :
                                case 9 :
                                    break;
                                default :
                                    $pbm_tac = TRUE;
                                    break;
                            }
                            if (datetolocal($cocl_datefin) <> '') {
                                $pbm_tac = TRUE;
                            }
                        } else {
                            $pbm_tac = TRUE;
                        }
                        break;
                    // Contrat de location
                    case 3 :
                        $row = lit_enr_coloc($cr);
                        if (is_array($row)) {
                            extract($row);
                            switch (valsession('niv_log')) {
                                case 3 :
                                case 4 :
                                    if ($coloc_codeage <> valsession('code_age')) {
                                        $pbm_tac = TRUE;
                                    }
                                    break;
                                case 5 :
                                case 9 :
                                    break;
                                default :
                                    $pbm_tac = TRUE;
                                    break;
                            }
                            if (datetolocal($coloc_dateclos) <> '') {
                                $pbm_tac = TRUE;
                            }
                        } else {
                            $pbm_tac = TRUE;
                        }
                        break;
                    // Dossier de suivi
                    case 4 :
                        $row = lit_enr_dos($cr);
                        if (is_array($row)) {
                            extract($row);
                            switch (valsession('niv_log')) {
                                case 3 :
                                case 4 :
                                    if ($dos_codeage <> valsession('code_age')) {
                                        $pbm_tac = TRUE;
                                    }
                                    break;
                                case 5 :
                                case 9 :
                                    break;
                                default :
                                    $pbm_tac = TRUE;
                                    break;
                            }
                            if (datetolocal($dos_datefin) <> '') {
                                $pbm_tac = TRUE;
                            }
                        } else {
                            $pbm_tac = TRUE;
                        }
                        break;
                    // relevé de parc
                    case 5 :
                        $row = lit_enr_rel($cr);
                        if (is_array($row)) {
                            extract($row);
                            switch (valsession('niv_log')) {
                                case 3 :
                                case 4 :
                                    if ($rel_codeage <> valsession('code_age')) {
                                        $pbm_tac = TRUE;
                                    }
                                    break;
                                case 5 :
                                case 9 :
                                    break;
                                default :
                                    $pbm_tac = TRUE;
                                    break;
                            }
                        } else {
                            $pbm_tac = TRUE;
                        }
                        break;
                    // Ordre de travail
                    case 6 :
                        $row = lit_enr_ot($cr);
                        if (is_array($row)) {
                            extract($row);
                            switch (valsession('niv_log')) {
                                case 10 :
                                case 11 :
                                    if ($ot_codest <> valsession('code_st')) {
                                        $pbm_tac = TRUE;
                                    }
                                    break;
                                case 3 :
                                case 4 :
                                    $row = lit_enr_int($ot_codeint);
                                    if ($row) {
                                        extract($row);
                                        if (($int_codeage <> valsession('code_age')) AND ($int_codeages <> valsession('code_age'))) {
                                            $pbm_tac = TRUE;
                                        }
                                    } else {
                                        $rpbm_tac = TRUE;
                                    }
                                    break;
                                case 5 :
                                case 9 :
                                    break;
                                default :
                                    $pbm_tac = TRUE;
                                    break;
                            }
                        } else {
                            $pbm_tac = TRUE;
                        }
                        break;
                    default :
                        $pbm_tac = TRUE;
                        break;
                }

                if ($pbm_tac == FALSE) {
                    $tac_typrat = $tr;
                    $tac_coderat = $cr;
                    $readonly = 'typrat-rat';
                }
            } else {
                $pbm_tac = TRUE;
            }
        } else {
            $pbm_tac = TRUE;
        }
    } else {
        if (isset($_GET['cr'])) {
            $pbm_tac = TRUE;
        }
    }
}

switch ($td) {
    case 'add' :
        $titre = _('SAISIE TACHE');
        $tac_datecreaf = $datef;
        $tac_codecrea = valsession('code_log');
        $tac_proba = '';
        $tac_urg = -1;
        $tac_typrat = $tr;
        $tac_coderat = $cr;
        break;
    case 'edit' :
        $titre = _('MISE A JOUR TACHE');
        break;
    case 'view' :
        $titre = _('VISUALISATION TACHE');
        $sais_datefinp = FALSE;
        $sais_datefin = FALSE;
        break;
    case 'del':
        $titre = _('SUPPRESSION TACHE');
        $sais_datefinp = FALSE;
        $sais_datefin = FALSE;
        break;
    default:
        $titre = _('PROBLEME');
        $pbm_tac = 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 Tâche'); ?></title>

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

    <?php
    if (($td == 'edit') OR ($td == 'add')) {
        ?>
        <script language="javascript">
            <!--
            var tab1_champ = ["nom:datecreaf ; format:date ; masque:<?php echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?php echo _('Date création'); ?>  ; stok:frmok ; stbad:frmbad",
                "nom:datefinpf ; format:date ; masque:<?php echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?php echo _('Date butoir'); ?>  ; stok:frmok ; stbad:frmbad",
                "nom:titred; format:txt ; req:Y ; txt:<?php echo _('Titre'); ?> ; lmx:50 ; stok:frmok ; stbad:frmbad",
                "nom:afaire; format:txt ; req:Y ; txt:<?php echo _('A faire'); ?> ; stok:frmok ; stbad:frmbad",
                "nom:codecat; format:liste ; req:Y ; txt:<?php echo _('Catégorie'); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:typdest; format:liste ; req:Y ; txt:<?php echo _('Type destinataire'); ?>  ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:dest; format:liste ; req:Y ; txt:<?php echo _('Destinataire'); ?>(s) ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:typrat; format:liste ; req:Y ; txt:<?php echo _('Type rattachement'); ?>  ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:rat; format:liste ; req:N ; txt:<?php echo _('Rattaché à'); ?> ; stok:frmok ; stbad:frmbad"];
            //-->
        </script>
        <?php
    }
    ?>

    <script type="text/javascript">
        <!--
        var tab_trat = [];
        <?php
        foreach ($tac_trat as $key=>$value)
        {
            echo 'tab_trat['.$key.'] = "'.$value.'";';
        }
        echo 'var nl = '.valsession('niv_log').';';
        ?>

        function init_datepicker() {
            <?php include_once ('../jscript/datepicker_prm.js.php'); ?>
            <?php if ($td == 'add') { ?>
            $("#datecreaf").datepicker({
                beforeShow: function() {
                    $(this).datepicker('option', 'minDate', 0);
                },
                onSelect: function (datesel) {
                    $("#datecreaf").blur();
                    chk_datedif(datesel, this.id, '<?php echo valsession('date_fmtshort'); ?>', true, 'today', '', '', false);
                }
            });
            <?php }

            if ($sais_datefinp == TRUE) { ?>
            $("#datefinpf").datepicker({
                beforeShow: function() {
                    var mindate = $('#datecreaf').val();
                    $(this).datepicker('option', 'minDate', mindate);
                },
                onSelect: function (datesel) {
                    $("#datefinpf").blur();
                    if (chk_datedif(datesel, this.id, '<?php echo valsession('date_fmtshort'); ?>', true, 'datecreaf,today', '', '', false)) {
                        chk_date_affwarnech(this.id, 'warn_ech', '<?php echo valsession('date_fmtshort'); ?>', '', '', 'today');
                    }
                }
            });
            <?php }

            if ($sais_datefin == TRUE) { ?>
            $("#datefinf").datepicker({
                beforeShow: function() {
                    var mindate = $('#datecreaf').val();
                    $(this).datepicker('option', 'minDate', mindate);
                },
                onSelect: function (datesel) {
                    $("#datefinf").blur();
                    chk_datedif(datesel, this.id, '<?php echo valsession('date_fmtshort'); ?>', true, 'datecreaf,today', '', '', false);
                }
            });
            <?php }
            ?>
        }

        function init_all() {
            init_Body();
            init_datepicker();
            img_checkpour('ave', 'nivave', 2);
            <?php
            if ($pbm_tac==FALSE)
            {
                if ($readonly <> '')
                    {echo "readonly_id('".$readonly."',false);";}
                switch ($td)
                {
                    case 'add' :
                        echo "init_autoraz('razdatecreaf','datecreaf','click');";
                        echo 'init_ajax_tdest('.(int)($tr=='').');';
                        echo 'init_ajax_dest('.(int)($tr=='').');';
                        echo 'init_ajax_trat();';
                        echo "init_checkpour('ave','nivave',2);";
                        echo "init_valid_form('enr','tac_maj','".valsession('date_fmtshort')."',tab1_champ,'','');";
                        break;
                    case 'edit' :
                        echo "chk_date_affwarnech('datefinpf','warn_ech','".valsession('date_fmtshort')."','','','today');";
                        echo "init_autoraz('razdatefinpf','datefinpf','click');";
                        echo "init_autoraz('razdatefinf','datefinf','click');";
                        echo 'init_ajax_tdest('.(int)($tr=='').');';
                        echo 'init_ajax_dest('.(int)($tr=='').');';
                        echo 'init_ajax_trat();';
                        echo "checkpour('ave','nivave',2);";
                        echo "init_checkpour('ave','nivave',2);";
                        echo "init_valid_form('enr','tac_maj','".valsession('date_fmtshort')."',tab1_champ,'','');";
                        break;
                    case 'del' :
                        echo "chk_date_affwarnech('datefinpf','warn_ech','".valsession('date_fmtshort')."','','','today');";
                        echo "checkpour('ave','nivave',2);";
                        echo "warn_del('');";
                        echo 'readonly_all();';
                        break;
                    case 'view' :
                        echo "chk_date_affwarnech('datefinpf','warn_ech','".valsession('date_fmtshort')."','','','today');";
                        echo "checkpour('ave','nivave',2);";
                        echo 'readonly_all();';
                        break;
                }
            }
            ?>
            Focus_first();
        }

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

</head>
<body>
<?php include_once('../inc/entete.inc.php');
if ($pbm_tac == FALSE) {
    if (nb_tacc(TRUE) > 0) {
        cre_ent_form($titre, 'tac_lst.php?ord=DES&tri=Code', $_SERVER['HTTP_REFERER'], '');
        ?>
        <form id="tac_maj" name="tac_maj" method="post" action="tac_enr.php">
            <input name="titre" type="hidden" id="titre" value="<?php echo $titre ?>">
            <input name="td" type="hidden" id="td" value="<?php echo $td ?>">
            <input name="tr" type="hidden" id="tr" value="<?php echo $tr ?>">
            <input name="today" type="hidden" id="today" value="<?php echo $datef; ?>">
            <input name="nbutton" type="hidden" id="nbutton" value="">
            <?php
            if ($td == 'add') {
                echo '<input name="code" type="hidden" id="code" value="' . $tac_code . '">';
            }
            ?>

            <!-- Code de la tâche (affiché uniquement si édition ou suppression) -->
            <?php if ($td <> 'add') { ?>
                <table width="100%" border="0">
                    <tr>
                        <td width="160" class="rubfrm" id="f_code" align="right"><?php echo _('Code'); ?></td>
                        <td>
                            <input name="code" type="text" id="code" value="<?php echo $tac_code ?>" size="20"
                                   maxlength="20" readonly>
                        </td>
                    </tr>
                </table>
            <?php } ?>

            <!-- Date de création et fin prévue-->
            <table width="100%" border="0">
                <tr>
                    <td width="160" class="rubfrm" id="f_datecreaf" align="right"><?php echo _('Date Création'); ?></td>
                    <td>
                        <input name="datecreaf" type="text" id="datecreaf" value="<?php echo $tac_datecreaf; ?>"
                               size="10"
                               maxlength="10" readonly>
                        <?php
                        if ($td == 'add') {
                            echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _('Champ obligatoire') . '">';
                            echo ' <img src="../img/raz.png" id ="razdatecreaf" border="0" align="absmiddle">';
                        }
                        ?>
                    </td>
                    <td align="right">
                    <span class="annot_std_gras"><?php echo _('Date butoir'); ?> :
                    </span><input name="datefinpf" type="text" id="datefinpf" value="<?php echo $tac_datefinpf ?>"
                                  size="10" maxlength="10" readonly>
                        <?php
                        if ($sais_datefinp == TRUE) {
                            echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _('Champ obligatoire') . '">';
                            echo ' <img src="../img/raz.png" id ="razdatefinpf" border="0" align="absmiddle">';
                        }
                        echo '<span id="warn_ech" style="display:none;">';
                        echo ' <img src="../img/warn.png" title ="' . _('A échéance') . '" border="0" align="absmiddle">';
                        echo '</span>';
                        ?>
                    </td>
                </tr>
            </table>

            <!-- Titre et "à faire" de la tâche -->
            <table width="100%" border="0">
                <tr>
                    <td width="160" class="rubfrm" id="f_titred" align="right"><?php echo _('Titre'); ?></td>
                    <td>
                        <input name="titred" type="text" id="titred" value="<?php echo $tac_titre ?>" size="50">
                        <img src="../img/oblig.png" align="absmiddle" alt="<?php echo _('Champ obligatoire'); ?>">
                    </td>
                    <td class="rubfrm" id="f_urg" align="right"><?php echo _("Niveau d'urgence"); ?></td>
                    <td>
                        <?php
                        // création de la liste des Niveaux d'urgence
                        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                        $rech = 'SELECT * FROM `' . TBURG . '` ORDER BY urg_niveau';
                        $rows = $db->get_results($rech, ARRAY_A);
                        close_database();
                        cre_select_tab('urgence', 'urgence', $rows, 'urg_niveau', 'urg_des', '', $tac_urg);
                        ?>
                    </td>
                </tr>
            </table>

            <table width="100%" border="0">
                <tr>
                    <td width="160" class="rubfrm" id="f_afaire" align="right"><?php echo _('A faire'); ?></td>
                    <td>
                    <textarea name="afaire" rows="5" id="afaire"
                              style="width:95%;"><?php echo $tac_afaire; ?></textarea>
                        <img src="../img/oblig.png" align="top" alt="<?php echo _('Champ obligatoire'); ?>">
                    </td>
                </tr>
            </table>

            <!-- Paramètres de la tâche -->
            <table width="100%" border="0">
                <tr>
                    <td width="160" class="rubfrm" id="f_cat" align="right"><?php echo _('Catégorie'); ?></td>
                    <td>
                        <?php
                        // création de la liste des Catégories
                        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                        $rech = 'SELECT * FROM `' . TBTACC . '` ORDER BY tacc_desc';
                        $rows = $db->get_results($rech, ARRAY_A);
                        close_database();
                        cre_select_tab('codecat', 'codecat', $rows, 'tacc_code', 'tacc_desc', '', $tac_codetacc, '', _('-- Faites votre choix --'));
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" id="f_typdest" align="right"><?php echo _('Type destinataire'); ?></td>
                    <td>
                        <?php
                        echo '<select name="typdest" id="typdest">';
                        echo '<option value="">' . _('-- Faites votre choix --') . '</option>';
                        foreach ($tac_tdest as $key => $value) {
                            $affok = TRUE;
                            switch (valsession('niv_log')) {
                                case 3 :
                                case 11 :
                                    if ($key <> -1) {
                                        $affok = FALSE;
                                    }
                                    break;
                                case 4 :
                                    if (($key <> -1) AND ($key <> 1) AND ($key <> 4)) {
                                        $affok = FALSE;
                                    }
                                    break;
                                case 5 :
                                    if ($key == 6) {
                                        $affok = FALSE;
                                    }
                                    break;
                                case 10 :
                                    if (($key <> -1) AND ($key <> 3)) {
                                        $affok = FALSE;
                                    }
                                    break;
                            }
                            if ($affok == TRUE) {
                                echo '<option value="' . $key . '"';
                                if ($tac_typdest == $key) {
                                    echo ' selected';
                                }
                                echo '>';
                                echo $value . '</option>';
                            }
                        }
                        echo '</select>';
                        echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _('Champ obligatoire') . '">';
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" id="f_dest" align="right"><?php echo _('Destinataire(s)'); ?></td>
                    <td>
                        <?php
                        $rech = '';
                        $nivdest = 0;
                        switch ($tac_typdest) {
                            case -1 :
                                switch (valsession('niv_log')) {
                                    case 3 :
                                        $nivdest = 1;
                                        break;
                                    case 4 :
                                        $nivdest = 2;
                                        break;
                                    case 5 :
                                        $nivdest = 5;
                                        break;
                                    case 9 :
                                        $nivdest = 6;
                                        break;
                                    case 10 :
                                        $nivdest = 4;
                                        break;
                                    case 11 :
                                        $nivdest = 3;
                                        break;
                                }
                                break;
                            default :
                                $nivdest = (int)$tac_typdest;
                                break;
                        }
                        echo '<select name="dest" id="dest">';
                        echo '<option value="">' . _('-- Faites votre choix --') . '</option>';
                        switch ($nivdest) {
                            case 1 :
                                $rech = 'SELECT * FROM `' . TBTEC . '`';
                                switch (valsession('niv_log')) {
                                    case 3 :
                                    case 11 :
                                        $rech .= " WHERE tec_code='" . valsession('code_log') . "'";
                                        break;
                                    case 4 :
                                        $rech .= " WHERE tec_codeage='" . valsession('code_log') . "'";
                                        break;
                                    default :
                                        $rech .= " WHERE tec_codeage<>''";
                                        break;
                                }
                                $rech .= ' AND tec_out=0 ORDER BY tec_nom';
                                break;
                            case 2 :
                                $rech = 'SELECT * FROM `' . TBAGE . '` WHERE age_ouvert=1 ORDER BY age_nom';
                                break;
                            case 3 :
                                $rech = 'SELECT * FROM `' . TBTEC . '`';
                                if (valsession('niv_log') <> 10) {
                                    $rech .= " WHERE tec_codest<>''";
                                } else {
                                    $rech .= " WHERE tec_codest='" . valsession('code_log') . "'";
                                }
                                $rech .= ' AND tec_out=0 ORDER BY tec_nom';
                                break;
                            case 4 :
                                $rech = 'SELECT * FROM `' . TBST . '` WHERE st_ouvert=1 ORDER BY st_nom';
                                break;
                        }
                        if ($rech <> '') {
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                            $rows = $db->get_results($rech, ARRAY_A);
                            if ($rows) {
                                foreach ($rows as $row) {
                                    $row = encode_str($row);
                                    extract($row);
                                    $codedest = '';
                                    switch ($nivdest) {
                                        case 1 :
                                            $codedest = $tec_code;
                                            $txtdest = $tec_nom . ' ' . $tec_prenom . ' (' . $tec_codeage . ')';
                                            break;
                                        case 2 :
                                            $codedest = $age_code;
                                            $txtdest = $age_resp_nom . ' ' . $age_resp_prenom . ' (' . $age_code . ')';
                                            break;
                                        case 3 :
                                            $codedest = $tec_code;
                                            $txtdest = $tec_nom . ' ' . $tec_prenom . ' (' . $tec_codest . ')';
                                            break;
                                        case 4 :
                                            $codedest = $st_code;
                                            $txtdest = $st_resp_nom . ' ' . $st_resp_prenom . ' (' . $st_code . ')';
                                            break;
                                    }
                                    if ($codedest <> '') {
                                        echo '<option value="' . $codedest . '"';
                                        if ($tac_codedest == $codedest) {
                                            echo ' selected';
                                        }
                                        echo '>' . $txtdest . '</option>';
                                    }
                                }
                            }
                            close_database();
                        } else {
                            $codedest = '';
                            switch ($nivdest) {
                                case 5 :
                                    $codedest = 'CGE';
                                    $txtdest = _('Centre de gestion');
                                    break;
                                case 6 :
                                    $codedest = $admin_code;
                                    $txtdest = $admin_code;
                                    break;
                            }
                            if ($codedest <> '') {
                                echo '<option value="' . $codedest . '"';
                                if ($tac_codedest == $codedest) {
                                    echo ' selected';
                                }
                                echo '>' . $txtdest . '</option>';
                            }
                        }
                        echo '</select>';
                        echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _('Champ obligatoire') . '">';
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" id="f_typrat" align="right"><?php echo _('Type rattachement'); ?></td>
                    <td>
                        <?php
                        echo '<select name="typrat" id="typrat">';
                        echo '<option value="">' . _('-- Faites votre choix --') . '</option>';
                        foreach ($tac_trat as $key => $value) {
                            $affok = TRUE;
                            switch (valsession('niv_log')) {
                                case 10 :
                                case 11 :
                                    if (($key <> 6) AND ($key <> -1)) {
                                        $affok = FALSE;
                                    }
                                    break;
                                default :
                                    break;
                            }
                            if ($affok == TRUE) {
                                echo '<option value="' . $key . '"';
                                if ($tac_typrat == $key) {
                                    echo ' selected';
                                }
                                echo '>';
                                echo $value . '</option>';
                            }
                        }
                        echo '</select>';
                        echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _('Champ obligatoire') . '">';
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" id="f_rat" align="right"><?php echo _('Rattaché à'); ?></td>
                    <td>
                        <?php
                        $rech = '';
                        echo '<select name="rat" id="rat">';
                        echo '<option value="">' . _('-- Faites votre choix --') . '</option>';
                        switch ($tac_typrat) {
                            // Rien
                            case -1 :
                                break;
                            // Interventions
                            case 1 :
                                switch (valsession('niv_log')) {
                                    case 3 :
                                    case 4 :
                                        $rech = 'SELECT * FROM `' . TBINT . '`';
                                        $rech .= " WHERE int_codeage='" . valsession('code_age') . "' OR int_codeages='" . valsession('code_age') . "'";
                                        $rech .= " AND int_datefin='0000-00-00'";
                                        $rech .= ' ORDER BY int_code DESC';
                                        break;
                                    case 10 :
                                    case 11 :
                                        break;
                                    default :
                                        $rech = 'SELECT * FROM `' . TBINT . '`';
                                        $rech .= " WHERE int_datefin='0000-00-00'";
                                        $rech .= ' ORDER BY int_code DESC';
                                        break;
                                }
                                break;
                            // Contrat de maintenance
                            case 2 :
                                switch (valsession('niv_log')) {
                                    case 3 :
                                    case 4 :
                                        $rech = 'SELECT * FROM `' . TBCOCL . '`';
                                        $rech .= " WHERE cocl_codeage='" . valsession('code_age') . "'";
                                        $rech .= " AND cocl_datefin='0000-00-00'";
                                        $rech .= ' ORDER BY cocl_code DESC';
                                        break;
                                    case 10 :
                                    case 11 :
                                        break;
                                    default :
                                        $rech = 'SELECT * FROM `' . TBCOCL . '`';
                                        $rech .= " WHERE cocl_datefin='0000-00-00'";
                                        $rech .= ' ORDER BY cocl_code DESC';
                                        break;
                                }
                                break;
                            // Contrat de location
                            case 3 :
                                switch (valsession('niv_log')) {
                                    case 3 :
                                    case 4 :
                                        $rech = 'SELECT * FROM `' . TBCOLOC . '`';
                                        $rech .= " WHERE coloc_codeage='" . valsession('code_age') . "'";
                                        $rech .= " AND coloc_dateclos='0000-00-00'";
                                        $rech .= ' ORDER BY coloc_code DESC';
                                        break;
                                    case 10 :
                                    case 11 :
                                        break;
                                    default :
                                        $rech = 'SELECT * FROM `' . TBCOLOC . '`';
                                        $rech .= " WHERE coloc_dateclos='0000-00-00'";
                                        $rech .= ' ORDER BY coloc_code DESC';
                                        break;
                                }
                                break;
                            // Dossier de suivi
                            case 4 :
                                switch (valsession('niv_log')) {
                                    case 3 :
                                    case 4 :
                                        $rech = 'SELECT * FROM `' . TBDOS . '`';
                                        $rech .= " WHERE dos_codeage='" . valsession('code_age') . "'";
                                        $rech .= " AND dos_datefin='0000-00-00'";
                                        $rech .= ' ORDER BY dos_code DESC';
                                        break;
                                    case 10 :
                                    case 11 :
                                        break;
                                    default :
                                        $rech = 'SELECT * FROM `' . TBDOS . '`';
                                        $rech .= " WHERE dos_datefin='0000-00-00'";
                                        $rech .= ' ORDER BY dos_code DESC';
                                        break;
                                }
                                break;
                            // relevé de parc
                            case 5 :
                                switch (valsession('niv_log')) {
                                    case 3 :
                                    case 4 :
                                        $rech = 'SELECT * FROM `' . TBREL . '`';
                                        $rech .= " WHERE rel_codeage='" . valsession('code_age') . "'";
                                        $rech .= ' ORDER BY rel_code DESC';
                                        break;
                                    case 10 :
                                    case 11 :
                                        break;
                                    default :
                                        $rech = 'SELECT * FROM `' . TBREL . '`';
                                        $rech .= ' ORDER BY rel_code DESC';
                                        break;
                                }
                                break;
                            // Bon de travail
                            case 6 :
                                switch (valsession('niv_log')) {
                                    case 3 :
                                    case 4 :
                                        $rech = 'SELECT * FROM `' . TBOT . '`, `' . TBINT . '`';
                                        $rech .= ' WHERE (' . TBOT . '.ot_codeint=' . TBINT . '.int_code';
                                        $rech .= ' AND ' . TBINT . ".int_codeage='" . valsession('code_age') . "'";
                                        $rech .= ' AND ' . TBOT . ".ot_datefin='0000-00-00')";
                                        $rech .= ' ORDER BY ot_code DESC';
                                        break;
                                    case 10 :
                                    case 11 :
                                        $rech = 'SELECT * FROM `' . TBOT . '`';
                                        $rech .= " WHERE ot_codest='" . valsession('code_st') . "'";
                                        $rech .= " AND ot_datefin='0000-00-00'";
                                        $rech .= ' ORDER BY ot_code DESC';
                                        break;
                                    default :
                                        $rech = 'SELECT * FROM `' . TBOT . '`';
                                        $rech .= " WHERE ot_datefin='0000-00-00'";
                                        $rech .= ' ORDER BY ot_code DESC';
                                        break;
                                }
                                break;
                        }
                        if ($rech <> '') {
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                            $rows = $db->get_results($rech, ARRAY_A);
                            if ($rows) {
                                foreach ($rows as $row) {
                                    $row = encode_str($row);
                                    extract($row);
                                    switch ($tac_typrat) {
                                        // Rien
                                        case -1 :
                                            break;
                                        // Interventions
                                        case 1 :
                                            $coderat = $int_code;
                                            $txtcli = '';
                                            $rech2 = 'SELECT cli_ste,cli_nom,cli_prenom FROM `' . TBCLI . '`';
                                            $rech2 .= " WHERE cli_code='" . $int_codecli . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2) {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            if (trim($cli_ste) <> '') {
                                                $txtcli = $cli_ste;
                                            } else {
                                                $txtcli = $cli_nom . ' ' . $cli_prenom;
                                            }
                                            $txtrat = $int_code . ' ' . _('du') . ' ' . datetolocal($int_datedde) . ' (' . $txtcli . ')';
                                            break;
                                        // Contrat de maintenance
                                        case 2 :
                                            $coderat = $cocl_code;
                                            $txtcli = '';
                                            $rech2 = 'SELECT cli_ste,cli_nom,cli_prenom FROM `' . TBCLI . '`';
                                            $rech2 .= " WHERE cli_code='" . $cocl_codecli . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2) {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            if (trim($cli_ste) <> '') {
                                                $txtcli = $cli_ste;
                                            } else {
                                                $txtcli = $cli_nom . ' ' . $cli_prenom;
                                            }
                                            $txtrat = $cocl_code . ' ' . _('du') . ' ' . datetolocal($cocl_datecrea) . ' (' . $txtcli . ')';
                                            break;
                                        // Contrat de location
                                        case 3 :
                                            $coderat = $coloc_code;
                                            $txtcli = '';
                                            switch ($coloc_typcli) {
                                                // Client
                                                case 0 :
                                                    $rech2 = 'SELECT cli_ste,cli_nom,cli_prenom FROM `' . TBCLI . '`';
                                                    $rech2 .= " WHERE cli_code='" . $coloc_codecli . "'";
                                                    break;
                                                // Agence
                                                case 1 :
                                                    $rech2 = 'SELECT age_nom FROM `' . TBAGE . '`';
                                                    $rech2 .= " WHERE age_code='" . $coloc_codecli . "'";
                                                    break;
                                                // Technicien agence
                                                case 2 :
                                                    // Technicien Sous-traitant
                                                case 4 :
                                                    $rech2 = 'SELECT tec_nom,tec_prenom FROM `' . TBTEC . '`';
                                                    $rech2 .= " WHERE tec_code='" . $coloc_codecli . "'";
                                                    break;
                                                // Sous-traitant
                                                case 3 :
                                                    $rech2 = 'SELECT st_nom FROM `' . TBST . '`';
                                                    $rech2 .= " WHERE st_code='" . $coloc_codecli . "'";
                                                    break;
                                            }
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2) {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            switch ($coloc_typcli) {
                                                // Client
                                                case 0 :
                                                    if (trim($cli_ste) <> '') {
                                                        $txtcli = $cli_ste;
                                                    } else {
                                                        $txtcli = $cli_nom . ' ' . $cli_prenom;
                                                    }
                                                    break;
                                                // Agence
                                                case 1 :
                                                    $txtcli = $age_nom;
                                                    break;
                                                // Technicien agence
                                                case 2 :
                                                    // Technicien Sous-traitant
                                                case 4 :
                                                    $txtcli = $tec_nom . ' ' . $tec_prenom;
                                                    break;
                                                // Sous-traitant
                                                case 3 :
                                                    $txtcli = $st_nom;
                                                    break;
                                            }
                                            $txtrat = $coloc_code . ' ' . _('du') . ' ' . datetolocal($coloc_datecrea) . ' (' . $txtcli . ')';
                                            break;
                                        // Dossier de suivi
                                        case 4 :
                                            $coderat = $dos_code;
                                            $txtcli = '';
                                            $rech2 = 'SELECT cli_ste,cli_nom,cli_prenom FROM `' . TBCLI . '`';
                                            $rech2 .= " WHERE cli_code='" . $dos_codecli . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2) {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            if (trim($cli_ste) <> '') {
                                                $txtcli = $cli_ste;
                                            } else {
                                                $txtcli = $cli_nom . ' ' . $cli_prenom;
                                            }
                                            $txtrat = $dos_code . ' ' . _('du') . ' ' . datetolocal($dos_datecrea) . ' (' . $txtcli . ')';
                                            break;
                                        // relevé de parc
                                        case 5 :
                                            $coderat = $rel_code;
                                            $txtcli = '';
                                            $rech2 = 'SELECT cli_ste,cli_nom,cli_prenom FROM `' . TBCLI . '`';
                                            $rech2 .= " WHERE cli_code='" . $rel_codecli . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2) {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            if (trim($cli_ste) <> '') {
                                                $txtcli = $cli_ste;
                                            } else {
                                                $txtcli = $cli_nom . ' ' . $cli_prenom;
                                            }
                                            $txtrat = $rel_code . ' ' . _('du') . ' ' . datetolocal($rel_datecrea) . ' (' . $txtcli . ')';
                                            break;
                                        // Bon de travail
                                        case 6 :
                                            $coderat = $ot_code;
                                            $rech2 = 'SELECT int_codecli FROM `' . TBINT . '`';
                                            $rech2 .= " WHERE int_code='" . $ot_codeint . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2) {
                                                extract($row2);
                                            }
                                            $txtcli = '';
                                            $rech2 = 'SELECT cli_ste,cli_nom,cli_prenom FROM `' . TBCLI . '`';
                                            $rech2 .= " WHERE cli_code='" . $int_codecli . "'";
                                            $row2 = $db->get_row($rech2, ARRAY_A);
                                            if ($row2) {
                                                $row2 = encode_str($row2);
                                                extract($row2);
                                            }
                                            if (trim($cli_ste) <> '') {
                                                $txtcli = $cli_ste;
                                            } else {
                                                $txtcli = $cli_nom . ' ' . $cli_prenom;
                                            }
                                            $txtrat = $ot_code . ' ' . _('du') . ' ' . datetolocal($ot_datedde) . ' (' . $txtcli . ')';
                                            break;
                                    }
                                    echo '<option value="' . $coderat . '"';
                                    if ($tac_coderat == $coderat) {
                                        echo ' selected';
                                    }
                                    echo '>' . $txtrat . '</option>';
                                }
                            }
                            close_database();
                        }
                        echo '</select>';
                        echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _('Champ obligatoire') . '">';
                        ?>
                    </td>
                </tr>
                <?php if ($td <> 'add') { ?>
                    <tr>
                        <td class="rubfrm" id="f_ave" align="right"><?php echo _('Etat avancement'); ?></td>
                        <td>
                            <select name="ave" id="ave">
                                <option value="" selected><?php echo _('-- Faites votre choix --'); ?></option>
                                <?php

                                // création des niveaux d'avancement
                                for ($i = 0; $i < 110; $i += 10) {
                                    if (($tac_avance <> '') AND ($tac_avance == $i)) {
                                        echo '<option value="' . $i . '" selected>' . $i . '%</option>';
                                    } else {
                                        echo '<option value="' . $i . '">' . $i . '%</option>';
                                    }
                                }
                                ?>
                            </select>
                            <img src="../img/oblig.png" align="absmiddle" alt="<?php echo _('Champ obligatoire'); ?>">
                            <span id="nivave"></span>
                            <br/><span class="annot_petit_it">(<?php echo _("Niveau d'avancement du dossier"); ?>
                                )</span>
                        </td>
                    </tr>
                <?php } ?>
            </table>

            <?php if ($td <> 'add') { ?>
                <!-- Date de clôture de la tâche -->
                <table width="100%" border="0">
                    <tr>
                        <td width="160" class="rubfrm" id="f_datefinf"
                            align="right"><?php echo _('Tâche terminée le'); ?></td>
                        <td>
                            <table width="100%" border="0">
                                <tr>
                                    <td>
                                        <input name="datefinf" type="text" id="datefinf"
                                               value="<?php echo $tac_datefinf ?>"
                                               size="10" readonly>
                                        <?php
                                        if ($sais_datefin == TRUE) {
                                            echo ' <img src="../img/raz.png" id ="razdatefinf" border="0" align="absmiddle">';
                                            echo '<span class="annot_petit_it">(' . _('Si vous saisissez une date, la tâche sera close') . ')</span>';
                                        }
                                        ?>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            <?php } ?> <!-- Fin de zone exclue si création -->

            <p align="center">
                <?php
                if (($td == 'edit') OR ($td == 'add')) {
                    ?>
                    <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?php echo _('Réinitialiser'); ?>">
                    <input name="enr" type="button" class="bton_std" id="enr" value="<?php echo _('Enregistrer'); ?>">
                    <?php
                }
                if ($td == 'del') // Bouton si en mode suppression
                {
                    ?>
                    <input name="del" type="submit" class="bton_std" id="del" value="<?php echo _('Supprimer'); ?>">
                    <?php
                }
                ?>
            </p>
        </form>

        <?php
    } else {
        pop_ret_auto(_('Aucune catégorie de tâche créée'), 'warn', 'tacc_lst.php');
    }
} else {
    pop_ret_auto(_('Problème de paramètres'), 'warn', 'index.php');
}
include_once('pied.php');
?>
</body>
</html>