<?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/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_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_time.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"]);
$_SESSION["typ_filtres"] = "coclren";
?>
<!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 _("Suivi contrats à renouveler"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_autohref_classe('falselnkimg','');
            Focus_first();
        }

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

</head>

<body>

<?php
include_once("../inc/entete.inc.php");
?>
<p align="center"><span class="titrepage"><?PHP echo _("LISTE DES CONTRATS RECUS & SIGNES"); ?></span></p>

<p align="center">(<?PHP echo _("Et pas encore renouvelés"); ?>)</p>
<?PHP
// $fic_imp = "coclren_lsti.php";

$preavis_mini = preavis_min();

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

$rech = cree_strrechlstcoclr();
if (@$_SESSION["niv_log"] == 4) {
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBCOCL . ".cocl_codeage='" . @$_SESSION["code_age"] . "' AND " . TBCOCL . ".cocl_daterec <> '0000-00-00'";
    else
        $rech .= " AND " . TBCOCL . ".cocl_codeage='" . @$_SESSION["code_age"] . "' AND " . TBCOCL . ".cocl_daterec <> '0000-00-00'";

} else {
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBCOCL . ".cocl_daterec <> '0000-00-00'";
    else
        $rech .= " AND " . TBCOCL . ".cocl_daterec <> '0000-00-00'";
}

$rech .= crestr_triord($coclr_titre_col, "cocl_code", FALSE, $limit);

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

if ($rows) {
    include_once("formr.inc.php");
    $numrows = count($rows);

    $rech = modstr_triord($rech, $coclr_titre_col, "cocl_code", TRUE, $limit);

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

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

    // *** Pagination de pied
    pagine_lst($numrows, $limit);


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

    // création entête
    titlst($coclr_titre_col);
    echo "<th>" . ucfirst(_("Date butoir").'<br />'._("de relance"))."</th>\n";
    echo "<th>" . _("Cliquer") . "</th>\n";


    $i = 0;

    foreach ($rows as $row) {
        $row = encode_str($row);
        // Affichage résumé Contrat Client

        // création de l'entête de ligne (TR)
        creetrlst($i);

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


        // CREATION d'icône si terminé
        if (datetolocal($row["cocl_datefin"]) <> "") {
            $lst_fini = TRUE;

            echo " <img src=\"../img/warn.png\" title =\""._("terminé le")." " . datetolocal($row["cocl_datefin"]) . "\" border=\"0\" align=\"absmiddle\">";
        }

        // détail contrat
        echo "<br />";
        echo "<span class=\"annot_petit_it\">";
        $rech2 = "SELECT co_nom FROM `" . TBCO . "` WHERE co_code = '" . $row["cocl_codeco"] . "'";
        $row2 = $db->get_row($rech2, ARRAY_A);

        if ($row2) {
            $row2 = encode_str($row2);
            $txt = formatetxt($row2["co_nom"]);
            $txt = cesuretxt($txt, 50);
            echo "->" . $txt;
        }
        echo "</span>";

        // Vérifie échéances et soldes horaires
        if (datetolocal($row["cocl_datefin"]) == "") {
            if (datetolocal($row["cocl_datef"]) <> "") {
                echo "<div align=\"right\" class=\"annot_petit_it\">";
                if (echeance_depassee_date($datea, $row["cocl_datef"]) == FALSE) {
                    $nb_jours_restants = diff_date_jours($datea, $row["cocl_datef"], TRUE);
                    echo _("Echéance dans") . ' ' . $nb_jours_restants . ' ' . _("jours");
                } else
                    echo _("Contrat terminé");
                echo "</div>";
            }

            if ($row["cocl_typh"] == 1) {
                $solde_reel_contrat = solde_reel_contrat_sec($row["cocl_code"], FALSE);
                echo "<div align=\"right\" class=\"annot_petit_it\">";
                echo _("Temps restant") . ' ' . heure_to_hhmm(sec_heure($solde_reel_contrat, 3, TRUE));
                echo "</div>";
            }
        } else {
            echo "<div align=\"right\" class=\"annot_petit_it\">";
            echo _("Contrat terminé");
            echo "</div>";
        }
        echo "</td>\n";

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

        // Affichage détail client

        $detailcli = "->";
        if ($row["cli_ste"] <> "")
            $detailcli .= $row["cli_ste"] . ", ";
        $detailcli .= $row["cli_civilite"] . " " . $row["cli_nom"];
        echo "<br />";
        echo "<span class=\"annot_std_it\">";
        echo $detailcli;
        echo "</span>";

        // Fin affichage détail client

        echo "</td>\n";

        echo "<td>" . datetolocal($row["cocl_datecrea"]) . "</td>\n";
        echo "<td>" . datetolocal($row["cocl_dated"]) . "</td>\n";
        echo "<td>";
        if (datetolocal($row["cocl_datef"]) == "") {
            echo "<div align=\"center\">";
            echo "-";
            echo "</div>";
        } else
            echo datetolocal($row["cocl_datef"]);
        echo "</td>\n";
        echo "<td>";
        if (datetolocal($row["cocl_daterel"]) == "") {
            echo "<div align=\"center\">";
            echo "-";
            echo "</div>";
        } else {
            echo datetolocal($row["cocl_daterel"]);

            // Affichage Nombre relances

            $nbrel = "";
            switch ($row["cocl_nivrel"]) {
                case 0 :
                    $nbrel = "";
                    break;
                case 1 :
                    $nbrel = " (" . _("1ère fois") . ")";
                    break;
                case 2 :
                    $nbrel = " (" . _("2nde fois") . ")";
                    break;

                default :
                    $nbrel = " (" . $row["cocl_nivrel"] . _("ème fois") . ")";
                    break;
            }
            if ($nbrel <> "") {
                echo "<br />";
                echo "<span class=\"annot_petit_it\">";
                echo $nbrel;
                echo "</span>";
            }

            // Fin affichage Nombre relances

        }
        echo "</td>\n";

        echo "<td>";

        // Affichage date butoir pour relance

        echo "<div align=\"center\">";
        if (datetolocal($row["cocl_datef"]) <> "") // Uniquement si date de fin
        {
            if (datetolocal($row["cocl_daterec"]) <> "") // Uniquement si reçu
            {
                $datemin = nouv_date_moins_jours($row["cocl_datef"], $preavis_mini);
                $datebut = nouv_date_moins_jours($row["cocl_datef"], $row["cocl_preavis"]);
                echo datetolocal($datebut);

                // Si c'est trop tôt (date mini de préavis < aujourd'hui) on affiche une icône complémentaire
                if (comp_date($datemin, $datea) == FALSE) {
                    $lst_ttot = TRUE;

                    echo " ";
                    echo '<img src="../img/tot.png" title="' . _("Trop tôt (à partir du") . ' ' . datetolocal($datemin) . ')" border="0" align="absmiddle">';
                }

                // Si c'est trop tard (aujourd'hui > date butoir) on affiche une icône complémentaire
                if (comp_date($datea, $datebut) == FALSE) {
                    $lst_ttard = TRUE;

                    echo " ";
                    echo '<img src="../img/tard.png" title="' . _("Trop tard (depuis le") . ' ' . datetolocal($datebut) . ')" border="0" align="absmiddle">';
                }
            } else
                echo '(' . _("pas reçu") . ')';
        } else
            echo '(' . _("illimité") . ')';

        echo "</div>";

        // Fin affichage détail renouvelé

        echo "</td>\n";

        // CREATION lien dynamique sur icône (édition, suppression, etc...)
        echo "<td>";
        echo "<div align=\"center\">";

        $renouv = TRUE;
        // Si le contrat n'a pas encore été renouvelé, on peut renouveller
        if ($row["cocl_renouv"] == 1)
            $renouv = FALSE;

        if ($renouv == TRUE) {
            $lst_ren = TRUE;
            cre_falselnk_img('', 'cocl_ren_'.$row["cocl_id"], 'span', 'center', _("Renouveler") , 'lnk', 'coclren_maj.php?co=' . $row["cocl_code"], '../img/renouv.png');
        } else {
            echo "-";
        }
        echo "</div>";
        echo "</td>\n";

        echo "</tr>\n";
        // Fin affichage résumé Contrat Client

        $i++;
    }
    echo "</table>\n";
    // *** Fin affichage de la table

    $db->disconnect();

    // *** Pagination de pied
    pagine_lst($numrows, $limit);

    $taille_piedla = "";
    include_once("piedla.inc.php");
} else
    retpp();
include_once("pied.php");
?>
</body>
</html>