<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  Sylvain FATOME
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/


// vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/lst.inc.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$fi = "";
if (isset($_GET['fi']) && (trim($_GET['fi']) <> ""))
    $fi = intval($_GET['fi']);

$pbm_cocl = TRUE;
if ((@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 1))
    $pbm_cocl = FALSE;

if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) {
    $row = lit_enr_age(@$_SESSION["code_age"], TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
    } else
        $pbm_cocl = TRUE;
}
?>
<!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 _("Impression Liste Contrats Clients"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            warn_print(1);
        }

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


</head>

<body>

<?PHP
if ($pbm_cocl == FALSE) {
    if ((@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 4))
        entimplst("LISTE DE VOS CONTRATS CLIENTS");
    if (@$_SESSION["niv_log"] == 1)
        entimplst("LISTE DE VOS CONTRATS");
    if ((@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 5))
        entimplst("LISTE DES CONTRATS CLIENTS");

    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    switch (@$_SESSION["niv_log"]) {
        case 1 :
            if (count($tab_cli) == 0) {
                $rech = cree_strrechlstcoclc();
                if (!stristr($rech, "WHERE "))
                    $rech .= " WHERE cocl_codecli='" . @$_SESSION["code_log"] . "'";
                else
                    $rech .= " AND cocl_codecli='" . @$_SESSION["code_log"] . "'";
            } else {
                $rech = cree_strrechlstcocl();

                // On rajoute les comptes complémentaires (cas des regroupements de clients)
                if (!stristr($rech, "WHERE "))
                    $rech .= " WHERE ";
                else
                    $rech .= " AND ";
                if (count($tab_cli) > 1)
                    $rech .= "(";
                foreach ($tab_cli as $key => $value) {
                    $rech .= " " . TBCOCL . ".cocl_codecli='" . $value . "' OR";
                }
                // Suppression du dernier 'OR'
                $rech = substr($rech, 0, strlen($rech) - 3);

                if (count($tab_cli) > 1)
                    $rech .= ")";
            }
            break;
        default :
            $rech = cree_strrechlstcocl();
            if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) {
                if (!stristr($rech, "WHERE "))
                    $rech .= " WHERE " . TBCOCL . ".cocl_codeage='" . @$_SESSION["code_age"] . "'";
                else
                    $rech .= " AND " . TBCOCL . ".cocl_codeage='" . @$_SESSION["code_age"] . "'";
            }
            break;
    }

    switch ($fi) {
        case "1" :
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE cocl_datesig<>'0000-00-00' AND cocl_dated<='" . $datea . "' AND (cocl_datef>='" . $datea . "' OR cocl_datef='0000-00-00') AND cocl_datefin='0000-00-00'";
            else
                $rech .= " AND cocl_datesig<>'0000-00-00' AND cocl_dated<='" . $datea . "' AND (cocl_datef>='" . $datea . "' OR cocl_datef='0000-00-00') AND cocl_datefin='0000-00-00'";
            break;
        case "2" :
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE cocl_dateenv<>'0000-00-00' AND cocl_daterec='0000-00-00'";
            else
                $rech .= " AND cocl_dateenv<>'0000-00-00' AND cocl_daterec='0000-00-00'";
            break;
        case "3" :
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE cocl_renouv=0";
            else
                $rech .= " AND cocl_renouv=0";
            break;
        default :
            break;
    }

    switch (@$_SESSION["niv_log"]) {
        case 1 :
            if (count($tab_cli) == 0)
                $rech .= crestr_triord($coclc_titre_col, "cocl_code", FALSE, $limit);
            else
                $rech .= crestr_triord($cocl_titre_col, "cocl_code", FALSE, $limit);
            break;
        default :
            $rech .= crestr_triord($cocl_titre_col, "cocl_code", FALSE, $limit);
            break;
    }

    $rows = $db->get_results($rech, ARRAY_A);

    // *** Affichage de la table
    echo "<table class=\"MItable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";

    // création entête
    switch (@$_SESSION["niv_log"]) {
        case 1 :
            if (count($tab_cli) == 0)
                titlst($coclc_titre_col);
            else
                titlst($cocl_titre_col);
            break;
        default :
            titlst($cocl_titre_col);
            break;
    }

    foreach ($rows as $row) {

        $row = encode_str($row);

        // Affichage résumé Contrat Client

        echo "<tr>";

        $echeance_ok = TRUE;
        if (datetolocal($row["cocl_datef"]) <> "") {
            if (echeance_depassee_date($datea, $row["cocl_datef"]) == TRUE)
                $echeance_ok = FALSE;
        }

        echo "<td>" . $row["cocl_code"];

        // CREATION d'icône si signé
        if (datetolocal($row["cocl_datesig"]) <> "") {
            $lst_sig = TRUE;
            echo ' <a href="#" class="popa"><img src="../img/ok.png" alt="'._("Signé le").' '. datetolocal($row["cocl_datesig"]) . '" border="0" align="absmiddle"><span>'._("Signé le").' ' . datetolocal($row["cocl_datesig"]) . '</span></a>';
        }

        // CREATION d'icône si terminé
        if ((datetolocal($row["cocl_datefin"]) <> "") OR ($echeance_ok == FALSE)) {
            $lst_fini = TRUE;
            if (datetolocal($row["cocl_datefin"]) <> "")
                echo ' <a href="#" class="popw"><img src="../img/warn.png" alt="'._("Terminé le").' '. datetolocal($row["cocl_datefin"]) . '" border="0" align="absmiddle"><span>'._("Terminé le").' ' . datetolocal($row["cocl_datefin"]) . '</span></a>';
            else
                echo ' <a href="#" class="popw"><img src="../img/warn.png" alt="'._("Terminé le").' '. datetolocal($row["cocl_datef"]) . '" border="0" align="absmiddle"><span>'._("Terminé le").' '. datetolocal($row["cocl_datef"]) . '</span></a>';
        }

        // détail contrat
        echo "<br />";
        echo "<span class=\"annot_petit_it\">";
        $txt = formatetxt($row["co_nom"]);
        echo '->'.("Type").' : ' . $txt;
        echo "</span>";

        // Géré par
        if ((@$_SESSION["niv_log"] <> 3) AND (@$_SESSION["niv_log"] <> 4)) {
            echo "<br />";
            echo "<span class=\"annot_petit_it\">";
            $txt = formatetxt($row["cocl_codeage"]);
            echo "->Agence : " . $txt;
            echo "</span>";
        }
        echo "</td>\n";

        // Type de contrat
        echo "<td align=\"center\">" . $typ_horaire[$row["cocl_typh"]] . "</td>\n";

        switch (@$_SESSION["niv_log"]) {
            case 1 :
                if (count($tab_cli) > 1) {
                    echo "<td>";
                    echo $row["cocl_codecli"];
                    echo " <a href=\"cli_maj.php?td=edit&cc=" . $row["cocl_codecli"] . "&dep=" . $row["cli_dep"] . "\"><img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\" title=\"" . _("Editer client") . "\"></a>";

                    // Affichage détail client
                    $detailcli = "";
                    if ($row["cli_ste"] <> "")
                        $detailcli .= $row["cli_ste"] . "<br />";
                    $detailcli .= $row["cli_civilite"] . " " . $row["cli_nom"] . " " . $row["cli_prenom"];
                    echo "<div class=\"annot_petit_itgras\" style=\"margin-left:5px;\">";
                    echo $detailcli;
                    echo "</div>";
                    // Fin affichage détail client

                    echo "</td>\n";
                }
                break;
            default :
                echo "<td>";
                echo $row["cocl_codecli"];
                echo " <a href=\"cli_maj.php?td=edit&cc=" . $row["cocl_codecli"] . "&dep=" . $row["cli_dep"] . "\"><img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\" title=\"" . _("Editer client") . "\"></a>";

                // Affichage détail client
                $detailcli = "";
                if ($row["cli_ste"] <> "")
                    $detailcli .= $row["cli_ste"] . "<br />";
                $detailcli .= $row["cli_civilite"] . " " . $row["cli_nom"] . " " . $row["cli_prenom"];
                echo "<div class=\"annot_petit_itgras\" style=\"margin-left:5px;\">";
                echo $detailcli;
                echo "</div>";
                // Fin affichage détail client

                echo "</td>\n";
                break;
        }

        // création
        echo "<td>";
        echo "<div align=\"center\">";
        echo datetolocal($row["cocl_datecrea"]);
        echo "</div>";
        echo "</td>\n";
        // début
        echo "<td>";
        echo "<div align=\"center\">";
        echo datetolocal($row["cocl_dated"]);
        echo "</div>";
        echo "</td>\n";
        // Fin
        echo "<td>";
        echo "<div align=\"center\">";
        if (datetolocal($row["cocl_datef"]) == "")
            echo "&nbsp;";
        else
            echo datetolocal($row["cocl_datef"]);
        echo "</div>";
        echo "</td>\n";
        // Temps
        echo "<td>";
        echo "<div align=\"center\">";
        echo "<span class=\"annot_petit_it\">";
        if ($row["cocl_typh"] == 1) {
            echo "Solde initial : " . heure_to_hhmm(sec_heure($row["cocl_nbs"], 3, TRUE));
            $solde = solde_contrat_sec($row["cocl_code"], FALSE);
            $soldeas = tps_trav_non_clot_sec($row["cocl_code"], FALSE);
            echo "<br />Solde actuel : ";
            echo heure_to_hhmm(sec_heure($solde, 3, TRUE));
            echo "<br />A sortir : ";
            echo heure_to_hhmm(sec_heure($soldeas, 3, TRUE));
            $solde_reel = $solde - $soldeas;
            echo "<br />Solde réel : ";
            echo heure_to_hhmm(sec_heure($solde_reel, 3, TRUE));
        } else
            echo "-";
        echo "</span>";
        echo "</div>";
        echo "</td>\n";
        // relancé le
        echo "<td>";
        echo "<div align=\"center\">";
        if (datetolocal($row["cocl_daterel"]) == "")
            echo "&nbsp;";
        else
            echo datetolocal($row["cocl_daterel"]);
        echo "</div>";
        echo "</td>\n";

        // Renouvellement
        echo "<td>";

        // Affichage détail renouvelé
        echo "<div align=\"center\">";
        echo $non_oui[$row["cocl_renouv"]];

        if ($row["cocl_renouv"] == 1) {
            // Affichage détail renouvellement

            $detailren = "(" . $row["cocl_rnum"] . ")";
            echo "<br />";
            echo "<span class=\"annot_petit_it\">";
            echo $detailren;
            echo "</span>";

            // Fin affichage détail renouvellement
        }

        echo "</div>";

        // Fin affichage détail renouvelé

        echo "</td>\n";

        echo "</tr>\n";
        // Fin affichage résumé Contrat Client
    }
    echo "</table>\n";
    // *** Fin affichage de la table

    $db->disconnect();
} else {
    ?>
    <script type="text/javascript">
        <!--
        window.close();
        //-->
    </script>
<?PHP
}
?>
</body>
</html>