<?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/config.inc.php');
include_once('../inc/func.inc.php');
include_once('../inc/func_date.inc.php');
include_once('../inc/func_log.inc.php');
include_once('../inc/func_geo.inc.php');
include_once('../inc/func_time.inc.php');
include_once('../inc/func_txt.inc.php');
include_once('../inc/lst.inc.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_droits.inc.php');
include_once('../inc/fic_hum.inc.php');
include_once('../inc/fic_int.inc.php');
include_once('../inc/fic_rel.inc.php');
include_once('../inc/fic_tec.inc.php');
include_once('../inc/fic_zc.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);
$_POST = clean_str($_POST);

$_SESSION['typ_filtres'] = 'sitesip';

$pbm_site = FALSE;
$pbm_cli = FALSE;
$pbm_int = FALSE;
$pbm_cocl = FALSE;
$form_ok = FALSE; // Formulaire validé ?

$sitep = FALSE;
$lstsites = '';

$action_to_do = '';
if ((trim(valpost('nbutton')) == 'enr1') OR (trim(valpost('nbutton')) == 'enr2')) // On a cliqué sur valider
{
    $action_to_do = 'valid';
}

if ($action_to_do <> 'valid') // On a pas validé
{
    $cc = $_GET['cc']; // Récupération code Client
    $co = $_GET['co']; // Récupération code Contrat
    $si = (int)$_GET['si']; // Récupération site Intervention
    $ca = $_GET['ca']; // Récupération code agence gestionnaire
    $cas = $_GET['cas']; // Récupération code agence site
    $tr = $_GET['tr']; // Récupération code technicien responsable

    // On Contrôle le code site
    if (($si == NULL) OR ($si < 0)) {
        $si = 0;
    }
    if ($si == 0) {
        $sitep = TRUE;
    }

    if ($si <> 0) {
        $row = lit_enr_scli_id($si, TRUE);
        if (!is_array($row)) {
            $pbm_site = TRUE;
        }
    }

    if ($cc <> '') {
        // Contrôle qu'un client n'essaye pas de "hacker" un contrat
        if (CheckNivLog('1') AND ($cc <> valsession('code_log'))) {
            $pbm_cli = TRUE;
        }

        $row = lit_enr_cli($cc, TRUE);
        if (is_array($row)) {
            extract($row);

            // Contrôle qu'une agence n'essaye pas de "hacker" un contrat
            switch (valsession('niv_log')) {
                case 3 :
                case 4 :
                    if (client_age_ok($cli_code, valsession('code_age'), TRUE) == FALSE) {
                        $detail_ok = TRUE;
                    }
                    break;
                default :
                    break;
            }
        } else {
            $pbm_cli = TRUE;
        }
    } else {
        $pbm_cli = TRUE;
    }

    if ($co <> '') {
        // Récupération des informations contrat client
        $row = lit_enr_cocl($co);
        if (is_array($row)) {
            extract($row);

            // Construction de la chaîne des sites autorisés
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            $rech = 'SELECT scli_id FROM `' . TBSCLI . '`,`' . TBSCLIC . '` WHERE ' . TBSCLIC . ".sclic_codecocl = '" . $co . "' AND " . TBSCLI . '.scli_id = ' . TBSCLIC . '.sclic_idscli';
            $rows = $db->get_results($rech, ARRAY_A);
            if ($rows) {
                foreach ($rows as $row) {
                    extract($row);
                    $lstsites .= $scli_id . '-';
                }
            }
            if ($lstsites != '') {
                $lstsites = substr($lstsites, 0, - 1);
            }
            $rech = 'SELECT cocl_sitep FROM `' . TBCOCL . "` WHERE cocl_code = '" . $co . "' AND cocl_sitep = 1";
            $row = $db->get_row($rech, ARRAY_A);
            if ($row) {
                $sitep = TRUE;
            }
            close_database();

            // Contrôle qu'un client n'essaye pas de "hacker" un contrat
            if (CheckNivLog('1') AND ($cocl_codecli <> valsession('code_log'))) {
                $pbm_cocl = TRUE;
            }
            // Contrôle qu'une agence n'essaye pas de "hacker" un contrat
            if (CheckNivLog('3-4') AND ($cocl_codeage <> valsession('code_age'))) {
                $pbm_cocl = TRUE;
            }
        }
    }

    if ($tr <> '') {
        $row = lit_enr_tec($tr, TRUE);
        if (is_array($row)) {
            extract($row);
        } else {
            $pbm_int = TRUE;
        }
    } else {
        $pbm_int = TRUE;
    }
} else {
    $form_ok = TRUE;
    $td = $_POST['td']; // Récupération type demande
    $cc = $_POST['cc']; // Récupération code Client
    $ci = $_POST['ci']; // Récupération code Intervention
    $si = (int)$_POST['si']; // Récupération site Intervention
    $ca = $_POST['ca']; // Récupération code Agence
    $cas = $_POST['cas']; // Récupération code agence site
}
$tab_sites = explode('-', substr($lstsites, 0, - 1));
?>
<!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 _('Sites intervention'); ?></title>

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

    <script language="javascript">
        <!--
        var tab_champ = new Array(
            "nom:scli_choisi ; format:radio ; req:Y ; txt:<?php echo _("Site d'intervention"); ?> ; stok:saisieok ; stbad:saisiepasok");
        //-->
    </script>

    <script type="text/javascript">
        <!--
        <?php
        if ($form_ok == TRUE)
        {
            if ($_SESSION['niv_log']<> 2)
            {
                cre_sel_agesite($td,$ci,$cc, (int)$_POST['scli_choisi'], 'window.opener.', 'agesite');
                echo "\n";
                cre_sel_tec($ca,$cas,$ci,$cc, (int)$_POST['scli_choisi'], 'window.opener.', 'codetec');
                echo "\n";
                $display='none';
                if (exist_rpbm_actif_site($td,$cc,$ci,(int)$_POST['scli_choisi']) == TRUE)
                    {$display='block';}
            }
            $tab_maitre = give_tab_maitre_cli($cc,TRUE);
            cre_sel_ddeurs_cli($cc, (int)$_POST['scli_choisi'], $tab_maitre, 'window.opener.', 'ddeur');
                echo "\n";
            cre_sel_codecocl($td,$ci,$cc, (int)$_POST['scli_choisi'], 'window.opener.', 'codecocl');
                echo "\n";

            /*
            $tab_maitre = give_tab_maitre_cli($cc,TRUE);
            cre_sel_ddeurs_cli($cc, (int)$_POST['scli_choisi'], $tab_maitre, 'window.opener.', 'ddeur');
            */
        }
        ?>

        function init_all() {
            init_Body();
            <?php
            if ($form_ok == FALSE)
            {
                echo "add_func_click('annul1','sclose','');";
                echo "add_func_click('annul2','sclose','');";
                echo "init_valid_form('enr1','sitesi_lst','".valsession('date_fmtshort')."',tab_champ,'','');";
                echo "init_valid_form('enr2','sitesi_lst','".valsession('date_fmtshort')."',tab_champ,'','');";
                echo 'Focus_first();';
            }
            else
            {
                if ($_SESSION['niv_log']<> 2)
                {
                    echo 'update_sel_codeages();';
                    echo 'update_sel_tec();';
                    // On remet les valeurs générées à zéro si on repart sur un nouveau site
                    if ($si<>$_POST['scli_choisi'])
                    {
                        echo "update_detail_pbm(true,'detailpbm','','viewpbm','viewpbmfait');";
                        echo "update_fait_pbm(true,'detailpbmfait','','viewpbmfait');";
                        $_SESSION['pbm_choisis'] = '';
                        $_SESSION['pbm_faits'] = '';
                        $nom_scli = nom_scli($_POST['scli_choisi'],TRUE,TRUE,FALSE);
                        echo "update_scli_from_fen_sites(true,'nomsite','".str_escape($nom_scli)."','idsite','".$_POST['scli_choisi']."');";
                    }
                }
                echo 'update_sel_ddeurs();';
                echo 'update_sel_codecocl();';
                echo 'self.close();';

                // echo "update_sel_ddeurs();";
            }
            ?>
        }

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

</head>

<body>

<?php
if (($pbm_site == FALSE) AND ($pbm_cli == FALSE) AND ($pbm_int == FALSE) AND ($pbm_cocl == FALSE))
{
if ($form_ok == FALSE) // Si le formulaire n'a pas été validé
{
cre_ent_form(_("SITES D'INTERVENTION"), '', '', '', FALSE);
?>
<p align="center">
    <?php
    echo _("Merci de choisir le site d'intervention") . '<br />';
    if ((valsession('niv_log') <> 2) AND (valsession('niv_log') <> 6) AND (valsession('niv_log') <> 7)) {
        echo '<span class="annot_petit_it">';
        echo _("Si vous modifiez votre choix par rapport à l'actuel,");
        echo _('la liste des matériels/problèmes/travaux effectués précédemment générée,');
        echo '<br />' . _('celle des contrats possibles, et celle des techniciens seront remises à zéro.');
        echo '</span>';
    }
    echo '<br /><br />';
    echo '<span class="annot_std_it">';
    if ($co <> '') {
        echo _('Liste des sites couverts par le contrat') . ' ' . $co;
    } else {
        echo _('Liste de tous les sites actifs');
    }
    echo '</span>';
    ?>
</p>

<form name="sitesip_lst" id="sitesip_lst" method="post" action="sitesip_lst.php">
    <input name="cc" type="hidden" id="cc" value="<?php echo $cc ?>">
    <input name="co" type="hidden" id="co" value="<?php echo $co ?>">
    <input name="si" type="hidden" id="si" value="<?php echo $si ?>">
    <input name="ca" type="hidden" id="ca" value="<?php echo $ca ?>">
    <input name="cas" type="hidden" id="cas" value="<?php echo $cas ?>">
    <input name="nbutton" type="hidden" id="nbutton" value="">
    <?php
    // création de la liste des sites secondaires
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    $rech = 'SELECT * FROM `' . TBSCLI . "` WHERE `scli_codecli`= '" . $cc . "' AND `scli_actif`=1";
    switch (valsession('niv_log')) {
        case 3 :
        case 4 :
            // L'agence n'est pas celle qui gère le compte principal
            if ($cli_prop <> valsession('code_age')) {
                $rech .= " AND `scli_prop`='" . valsession('code_age') . "'";
            }
            break;
        default :
            break;
    }
    $rech .= ' ORDER BY `scli_ste`';
    $rows = $db->get_results($rech, ARRAY_A);

    if ($rows OR ($sitep == TRUE)) {
        echo '<p align="center">';
        echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _('Remise à Zéro') . '">';
        echo '<input name="enr1" type="button" class="bton_std" id="enr1" value="' . _('Valider') . '">';
        echo '<input name="annul1" type="button" class="bton_std" id="annul1" value="' . _('Annuler') . '">';
        echo '</p>';

        echo '<table class="MtableD" border="0" width="890" cellpadding="0" cellspacing="0" align="center">';

        // création de l'entête de la table "sites d'intervention"
        echo '<th>';
        echo _('Coordonnées');
        echo '</th>';
        echo '<th>';
        echo _('Tel/Fax');
        echo '</th>';
        echo '<th>';
        echo _('Contact');
        echo '</th>';
        echo '<th>';
        echo _('Créé le');
        echo '</th>';
        echo '<th>';
        echo _('Choisir');
        echo '</th>';

        if (($sitep == TRUE) OR ($co == '')) {
            $affsitep = TRUE;
            switch (valsession('niv_log')) {
                case 3 :
                case 4 :
                    // L'agence n'est pas celle qui gère le compte principal
                    if ($cli_prop <> valsession('code_age')) {
                        $affsitep = FALSE;
                    }
                    break;
                default :
                    break;
            }

            if ($affsitep == TRUE) {
                // création de l'entête de ligne (TR)
                echo '<tr>';

                echo '<td>';
                echo _('Site principal');
                echo '</td>';
                echo '<td align="center">';
                echo '&nbsp;';
                echo '</td>';
                echo '<td align="center">';
                echo '&nbsp;';
                echo '</td>';
                echo '<td align="center">';
                echo '&nbsp;';
                echo '</td>';

                echo '<td align="center">';

                $choisi = FALSE;

                if ($si == 0) {
                    $choisi = TRUE;
                }

                $readonly = '';

                echo '<input type="radio" name="scli_choisi" value="0"';
                if ($choisi == TRUE) {
                    echo ' checked ';
                }
                echo $readonly . '>';
                echo '</td>';
                echo '</tr>';
            }
        }

        $tab_sites = explode('-', $lstsites);

        foreach ($rows as $row) {
            $row = encode_str($row);
            if ((exist_in_array($row['scli_id'], $tab_sites) == TRUE) OR ($co == '')) {
                // création de l'entête de ligne (TR)
                echo '<tr>';

                echo '<td>';
                echo $row['scli_ste'];
                echo '<br />';
                echo $row['scli_adr1'];
                echo '<br />';
                if ($row['scli_adr2'] <> '') {
                    echo $row['scli_adr2'];
                    echo '<br />';
                }
                echo Retournecp_Id($row['scli_ville'], FALSE) . ' ' . Retourneville_Id($row['scli_ville'], FALSE) . '<br />';
                // détail propriétaire
                if (CheckNivLog('5-9') OR (CheckNivLog('3-4') AND ($cli_prop = valsession('code_age')))) {
                    echo '<div class="annot_petit_it" align="right">';
                    $txt = _('Géré par') . ' ';
                    switch ($row['scli_prop']) {
                        case '-1' :
                            $txt .= _('agences couvrant le département');
                            break;
                        case '0' :
                            if (trim($cli_prop) <> '') {
                                $txt .= $cli_prop;
                            } else {
                                $txt = _('Non affecté');
                            }
                            break;
                        default :
                            if (trim($row['scli_prop']) <> '') {
                                $txt .= $row['scli_prop'];
                            } else {
                                if (trim($cli_prop) <> '') {
                                    $txt .= $cli_prop;
                                } else {
                                    $txt = _('Non affecté');
                                }
                            }
                            break;
                    }
                    echo $txt;
                    echo '</div>';
                }
                echo '</td>';
                echo '<td align="center">';
                echo _('T.') . $row['scli_telf'];
                if ($row['scli_fax'] <> '') {
                    echo '<br />';
                    echo _('F.') . $row['scli_fax'];
                }
                echo '</td>';
                echo '<td align="center">';
                echo $row['scli_rcivil'] . ' ' . $row['scli_rprenom'] . ' ' . $row['scli_rnom'];
                echo '<br />';
                echo _('T.') . $row['scli_rtelf'];
                if ($row['scli_rfax'] <> '') {
                    echo '<br />';
                    echo _('F.') . $row['scli_rfax'];
                }
                if ($row['scli_rtelp'] <> '') {
                    echo '<br />';
                    echo _('P.') . $row['scli_rtelp'];
                }
                echo '</td>';
                echo '<td align="center">';
                echo datetolocal($row['scli_datecrea']);
                echo '</td>';

                echo '<td align="center">';
                $choisi = FALSE;
                if ($row['scli_id'] == $si) {
                    $choisi = TRUE;
                }

                $readonly = '';

                echo '<input type="radio" name="scli_choisi" value="' . $row['scli_id'] . '"';
                if ($choisi == TRUE) {
                    echo ' checked ';
                }
                echo $readonly . '>';
                echo '</td>';
                echo '</tr>';
            }
        }

        echo '</table>';
        // Fin affichage table liste sites
    }
    close_database();

    echo '<p align="center">';
    echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _('Remise à Zéro') . '">';
    echo '<input name="enr2" type="submit" class="bton_std" id="enr2" value="' . _('Valider') . '">';
    echo '<input name="annul2" type="button" class="bton_std" id="annul2" value="' . _('Annuler') . '">';
    echo '</p>';
    echo '</form>';
    }
    }
    else {
        close_fen_auto('', '');
    }
    ?>
</body>
</html>
