<?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/config.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_int.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("../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);

$co = "";
$rnum = "";
$client = "";
$typh = 0;
$nbsnc = 0;
$renauto = 0;

$pbm_cocl = FALSE;

if (isset($_GET['co'])) {
    $co = $_GET['co']; // Récupération code contrat
    $row = lit_enr_cocl($co);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
        $client = $cocl_codecli;
        $typh = $cocl_typh;
        $nbsnc = $cocl_nbsnc;
        $renauto = $cocl_renauto;
        $rnum = $cocl_rnum;
        $row = lit_enr_cli($client, TRUE);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);
        } else
            $pbm_cocl = TRUE;
    } else
        $pbm_cocl = TRUE;
}

$titre = _("CLOTURE MANUELLE CONTRAT");
?>
<!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 _("Cloture manuelle contrat"); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = new Array(
            "nom:codecocl ; format:liste ; req:Y ; txt:<?PHP echo _("Contrat à clore"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad");
        //-->
    </script>

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_valid_coclclos('enr','coclclos_maj','<?PHP echo $_SESSION['date_fmtshort']; ?>',tab1_champ,'','');
            init_ajax_cocl3();
            init_back('annul', 'click');
            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 $titre ?></span></p>
<?PHP if ($pbm_cocl == FALSE) { ?>
    <form name="coclclos_maj" id="coclclos_maj" method="post" action="coclclos_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
        <input name="prev" type="hidden" id="prev" value="<?PHP echo $cli_prev ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <?PHP
        // Création de la liste des contrats non terminés et réceptionnés
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT * FROM `" . TBCOCL . "` WHERE `cocl_datefin` = '0000-00-00' AND `cocl_dateenv` <> '0000-00-00' AND `cocl_daterec` <> '0000-00-00'";
        if (@$_SESSION["niv_log"] == 4)
            $rech .= " AND `cocl_codeage`='" . @$_SESSION["code_age"] . "'";
        $rech .= " ORDER BY cocl_code DESC";
        $rows = $db->get_results($rech, ARRAY_A);

        if (($rows) AND (count($rows) > 0)) {
            ?>
            <br />
            <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">
                <tr>
                    <td class="rubfrm" id="f_codecocl">
                        <div align="center"><?PHP echo _("Contrat à clore"); ?></div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div align="center">
                            <select name="codecocl" id="codecocl">
                                <?PHP
                                echo '<option value="">'._("-- Faites votre choix --").'</option>';

                                foreach ($rows as $row) {
                                    $row = encode_str($row);
                                    extract($row);

                                    $rech2 = "SELECT * FROM `" . TBCO . "` WHERE co_code = '" . $cocl_codeco . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2) {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }
                                    $txt_cocl = $cocl_code . " - " . $co_nom;
                                    $rech2 = "SELECT * FROM `" . TBCLI . "` WHERE `cli_code` = '" . $cocl_codecli . "'";
                                    $row2 = $db->get_row($rech2, ARRAY_A);
                                    if ($row2) {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                    }

                                    if ($cli_ste <> "")
                                        $txt_cocl .= " (" . $cli_ste;
                                    else
                                        $txt_cocl .= " (" . $cli_nom;

                                    if (tps_trav_non_clot_sec($cocl_code, FALSE) > 0)
                                        $txt_cocl .= " - " . _("Impossible à clore, interventions en cours avec travail déjà saisi") . ")";
                                    else {
                                        if ($cocl_delai <> 0)
                                            $txt_cocl .= " - " . $cocl_delai . _("h");
                                        else
                                            $txt_cocl .= " " . _("Pas de délai");

                                        if ($cocl_datef <> "0000-00-00")
                                            $txt_cocl .= ", " . _("ech.") . " " . datetolocal($cocl_datef);
                                        else
                                            $txt_cocl .= ", " . _("sans ech.") . " ";
                                        if (intval($cocl_typh) == 1)
                                        {
                                            $solde_reel = solde_reel_contrat_sec($cocl_code, FALSE);
                                            $txt_cocl .= ", " . _("solde réel") . " " . heure_to_hhmm(sec_heure($solde_reel, 3, TRUE));
                                        }
                                        $txt_cocl .= ")";
                                    }

                                    echo "<option value=\"" . $cocl_code . "\"";
                                    if (tps_trav_non_clot_sec($cocl_code, FALSE) > 0)
                                        echo " disabled";
                                    else {
                                        if ($co == $cocl_code)
                                            echo " selected";
                                    }
                                    echo ">" . $txt_cocl . "</option>";

                                }
                                ?>
                            </select>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">&nbsp;</td>
                </tr>
                <tr>
                    <td align="center" class="rubfrm" id="f_lsthnc">
                        <?PHP echo _("Type de report"); ?>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <?PHP
                        $tab_cocl = cre_tab_cocl_report_clot($co);
                        echo '<select name="lsthnc" id="lsthnc">';
                        if (count($tab_cocl) >0)
                        {
                            foreach ($tab_cocl as $key => $value)
                            {
                                echo '<option value="' . $value["cocl_code"] . '">' . $value["cocl_desc"] . '</option>';
                            }
                        }
                        echo '</select>';
                        ?>
                    </td>
                </tr>
            </table>
            <p align="center">
                <input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo _("Annuler"); ?>">
                <input type="button" class="bton_std" name="enr" id="enr" value="<?PHP echo _("Valider"); ?>">
            </p>
        <?PHP
        } else {
            echo "<span class=\"annot_gras_it\">";
            echo "<div align=\"center\">";
            echo _("Pas de contrat pouvant être clot");
            echo "</div>";
            echo "</span>";
        }

        $db->disconnect();
        ?>
    </form>
<?PHP
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
include_once("pied.php");
?>
</body>
</html>