<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  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/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_ot.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(@$_SESSION["code_log"]);
$_SESSION["typ_filtres"] = "sitesi";

// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);
$_GET = clean_str($_GET);

$pbm_site = FALSE;
$pbm_cli = FALSE;
$pbm_ot = FALSE;
$sitep= FALSE;
$form_ok = FALSE; // Formulaire validé ?

$action_to_do = '';
if ((trim(@$_POST["nbutton"]) == 'enr1') OR (trim(@$_POST["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 Ordre Travail
    $td = $_GET['td']; // Récupération type demande
    $dte = datetomysql($_GET['dte']); // Récupération date de Contrôle
    $si = intval($_GET['si']); // Récupération site Intervention
    $ca = $_GET['ca']; // Récupération code agence gestionnaire
    $tr = $_GET['tr']; // Récupération code technicien responsable

    if (($td <> "edit") AND ($td <> "view") AND ($td <> "add"))
        $pbm_ot = TRUE;

    // 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 ((@$_SESSION["niv_log"] == 1) AND ($cc <> @$_SESSION["code_log"]))
            $pbm_cli = TRUE;

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

            // Contrôle qu'une agence n'essaye pas de "hacker" un contrat
            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    if (client_age_ok($cli_code, @$_SESSION["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 Ordre Travail
        $row = lit_enr_ot($co);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            // Si intervention terminée, force le mode "view"
            if (datetolocal($ot_datefin) <> "")
                $td = "view";

            if ($td <> "add") {
                // Contrôle qu'un client n'essaye pas de "hacker" un ordre intervention
                if ((@$_SESSION["niv_log"] == 1) AND ($ot_codecli <> @$_SESSION["code_log"]))
                    $pbm_ot = TRUE;
                // Contrôle qu'une agence n'essaye pas de "hacker" une intervention
                if ($ot_codeage <> @$_SESSION["code_age"]) {
                    // Intervention non gérée ?
                    if (trim($ot_codeage) == "") {
                        // On vérifie si client couvert par l'agence
                        if (dep_in_zcc_age_open($cli_dep, $cli_codepays, @$_SESSION["code_age"], TRUE) == FALSE)
                            $pbm_ot = TRUE;
                        else {
                            // On affecte l'intervention à cette agence
                            $ot_codeage = @$_SESSION["code_age"];
                            // On affecte le client à cette agence
                            $cli_prop = @$_SESSION["code_age"];
                        }
                    }
                }
            }
        } else {
            if ($td <> "add")
                $pbm_ot = TRUE;
        }
    } else {
        if ($td <> "add")
            $pbm_ot = TRUE;
    }
    if ($tr <> "") {
        $row = lit_enr_tec($tr, TRUE);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);
        } else
            $pbm_ot = TRUE;
    }
    /*
	else
	{
		if ($_SESSION["niv_log"]<>2)
			$pbm_ot = TRUE;
	}
    */
} else {
    $form_ok = TRUE;
    $td = $_POST['td']; // Récupération type demande
    $cc = $_POST['cc']; // Récupération code Client
    $co = $_POST['co']; // Récupération code Ordre Travail
    $si = intval($_POST['si']); // Récupération site Intervention
    $ca = $_POST['ca']; // Récupération code Agence
}
?>
<!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)
        {
            $tab_maitre = give_tab_maitre_cli($cc,TRUE);
            cre_sel_ddeurs_cli($cc, intval($_POST["scli_choisi"]), $tab_maitre, 'window.opener.', 'ddeur');
                echo "\n";
        }
        ?>

        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','siteso_lst','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                echo "init_valid_form('enr2','siteso_lst','".$_SESSION['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');";
                    }
                }
                */
                $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 "self.close();";
            }
            ?>
        }

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

</head>

<body>

<?PHP
if (($pbm_site == FALSE) AND ($pbm_cli == FALSE) AND ($pbm_ot == FALSE) AND ($pbm_cocl == FALSE))
{
    if ($form_ok == FALSE) // Si le formulaire n'a pas été validé
    {
?>
        <p align="center"><span class="titrepage"><?PHP echo _("SITES D'INTERVENTION"); ?></span></p>

        <?PHP
        if (($td == "add") OR ($td == "edit")) {
            echo '<div align="center">';
            echo _("Merci de choisir le site d'intervention");
            echo '<br />';
            echo '<div class="annot_std_it">';
            echo _("Liste de tous les sites actifs");
            echo '</div>';
            echo '</div>';
        }
        ?>

        <form name="siteso_lst" id="siteso_lst" method="post" action="siteso_lst.php">
        <input name="co" type="hidden" id="ci" value="<?PHP echo $co ?>">
        <input name="cc" type="hidden" id="cc" value="<?PHP echo $cc ?>">
        <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
        <input name="dte" type="hidden" id="dte" value="<?PHP echo $dte ?>">
        <input name="si" type="hidden" id="si" value="<?PHP echo $si ?>">
        <input name="ca" type="hidden" id="ca" value="<?PHP echo $ca ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <?PHP
        // création de la liste des sites secondaires
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT * FROM `" . TBSCLI . "` WHERE `scli_codecli`= '" . $cc . "' AND `scli_actif`=1";
        switch (@$_SESSION["niv_log"]) {
            case 3 :
            case 4 :
                // L'agence n'est pas celle qui gère le compte principal
                if ($cli_prop <> @$_SESSION["code_age"])
                    $rech .= " AND `scli_prop`='" . @$_SESSION["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">';
            if ($td <> "view") {
                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") . '">';
            } else
                echo '<input name="annul1" type="button" class="bton_std" id="annul1" value="' . _("Fermer") . '">';
            echo '</p>';

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

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

            $j = 0;
            $affsitep = TRUE;
            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    // L'agence n'est pas celle qui gère le compte principal
                    if ($cli_prop <> @$_SESSION["code_age"])
                        $affsitep = FALSE;
                    break;
                default :
                    break;
            }

            if ($affsitep == TRUE) {
                // création de l'entête de ligne (TR)
                creetrlstdet($j);

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

                echo "<td align=\"center\">\n";

                $choisi = FALSE;

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

                $readonly = "";
                if ($td == "view")
                    $readonly = "disabled";

                if ($choisi == TRUE) {
                    if ($j % 2 == 0)
                        echo "<input type=\"radio\" name=\"scli_choisi\" value=\"0\" class=\"TRalter1D\" checked " . $readonly . ">";
                    else
                        echo "<input type=\"radio\" name=\"scli_choisi\" value=\"0\" class=\"TRalter2D\" checked " . $readonly . ">";
                } else {
                    if ($j % 2 == 0)
                        echo "<input type=\"radio\" name=\"scli_choisi\" value=\"0\" class=\"TRalter1D\" " . $readonly . ">";
                    else
                        echo "<input type=\"radio\" name=\"scli_choisi\" value=\"0\" class=\"TRalter2D\" " . $readonly . ">";
                }
                echo "</td>\n";
                echo "</tr>\n";

                $j++;
            }

            if ($rows) {
                foreach ($rows as $row) {
                    $row = encode_str($row);
                    // création de l'entête de ligne (TR)
                    creetrlstdet($j);

                    echo "<td>\n";
                    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 (((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) OR (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) AND ($cli_prop = @$_SESSION["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>\n";
                    echo "<td align=\"center\">\n";
                    echo _("T.") . $row["scli_telf"];
                    if ($row["scli_fax"] <> "") {
                        echo "<br />";
                        echo _("F.") . $row["scli_fax"];
                    }
                    echo "</td>\n";
                    echo "<td align=\"center\">\n";
                    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>\n";
                    echo "<td align=\"center\">\n";
                    echo datetolocal($row["scli_datecrea"]);
                    echo "</td>\n";

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

                    $readonly = "";
                    if ($td == "view") {
                        $readonly = "disabled";
                    }

                    if ($choisi == TRUE) {
                        if ($j % 2 == 0)
                            echo "<input type=\"radio\" name=\"scli_choisi\" value=\"" . $row["scli_id"] . "\" class=\"TRalter1D\" checked " . $readonly . ">";
                        else
                            echo "<input type=\"radio\" name=\"scli_choisi\" value=\"" . $row["scli_id"] . "\" class=\"TRalter2D\" checked " . $readonly . ">";
                    } else {
                        if ($j % 2 == 0)
                            echo "<input type=\"radio\" name=\"scli_choisi\" value=\"" . $row["scli_id"] . "\" class=\"TRalter1D\" " . $readonly . ">";
                        else
                            echo "<input type=\"radio\" name=\"scli_choisi\" value=\"" . $row["scli_id"] . "\" class=\"TRalter2D\" " . $readonly . ">";
                    }
                    echo "</td>\n";
                    echo "</tr>\n";

                    $j++;
                }

                echo "</table>\n";
                // Fin affichage table liste sites
            }

            echo '<p align="center">';
            if ($td <> "view") {
                echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _("Remise à Zéro") . '">';
                echo '<input name="enr2" type="button" class="bton_std" id="enr2" value="' . _("Valider") . '">';
                echo '<input name="annul2" type="button" class="bton_std" id="annul2" value="' . _("Annuler") . '">';
            } else
                echo '<input name="annul2" type="button" class="bton_std" id="annul2" value="' . _("Fermer") . '">';
            echo '</p>';
        }
        else
        {
            echo '<div align="center" class="annot_gras_it">';
            echo _("Aucun site disponible");
            echo '</div>';
        }
        $db->disconnect();

        echo '</form>';
    }
}
else
    close_fen_auto('','');
?>
</body>
</html>
