<?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');
if ($pos !== FALSE) {
    header('Location: ../index.php');
}

include_once('../inc/config.inc.php');
include_once('../inc/fic_cocl.inc.php');
include_once('../inc/fic_droits.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_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(valsession('code_log'));

$idcocla = 0;
// Récupération code Contrat
if (isset($_GET['coa'])) {
    $idcocla = (int)$_GET['coa'];
}
if ($idcocla <> 0) {
    $row = lit_enr_cocla_id($idcocla);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        if ($cocla_codecocl <> '') {
            $row2 = lit_enr_cocl($cocla_codecocl);
            if (is_array($row2)) {
                $row2 = encode_str($row2);
                extract($row2);

                // 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;
                }
            } else {
                $pbm_cocl = TRUE;
            }
        }

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

$titre = _('RECEPTION ANNEXE CONTRAT SIGNEE');
?>
<!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 _('Accusé réception annexe contrat'); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:codecocl ; format:liste ; req:Y ; txt:<?php echo _('Annexe non encore signée'); ?> ; stok:frmok ; stbad:frmbad",
            "nom:signataire ; format:txt ; req:Y ; txt:<?php echo _('Signé par'); ?> ; lmx:70 ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        function init_datepicker() {
            <?php include_once ('../jscript/datepicker_prm.js.php'); ?>
            $("#datesigf").datepicker({
                beforeShow: function() {
                    var mindate = $('#dateenvf').val();
                    $(this).datepicker('option', 'minDate', mindate);
                    $(this).datepicker('option', 'maxDate', 0);
                },
                onSelect: function () {
                    $("#datesigf").blur();
                }
            });
        }

        function init_all() {
            init_Body();
            init_datepicker();
            <?php
            if ($idcocla == 0) {
                echo '$( "#datesigf" ).datepicker( "option", "disabled", true );';
            }

            echo "init_valid_form('enr','coclreca_maj','".valsession('date_fmtshort')."',tab1_champ,'','');";
            ?>
            init_ajax_cocla4();
            init_autoraz('razdatesigf', 'datesigf', 'click');
            init_back('annul', 'click');
            <?php
            if ($idcocla == 0) {
                echo "readonly_id('datesigf-signataire');";
            }
            ?>
            Focus_first();
        }

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

</head>

<body>

<?php
include_once('../inc/entete.inc.php');
cre_ent_form($titre, '', '', '', FALSE);
if ($pbm_cocl == FALSE) {
?>
    <form name="coclreca_maj" id="coclreca_maj" method="post" action="coclreca_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?php echo $titre ?>">
        <input name="dateenvf" type="hidden" id="dateenvf" value="<?php echo datetolocal($cocla_dateenv); ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <?php
        $rien_a_receptionner = TRUE;

        // création de la liste des contrats non terminés, envoyés et non reçus
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        $rech = 'SELECT * FROM `' . TBCOCL . '`, `' . TBCOCLA . '` WHERE ' . TBCOCL . ".cocl_datefin = '0000-00-00' AND " . TBCOCLA . ".cocla_dateenv <> '0000-00-00' AND " . TBCOCLA . ".cocla_daterec = '0000-00-00'";
        $rech .= ' AND ' . TBCOCL . '.cocl_code = ' . TBCOCLA . '.cocla_codecocl';
        if (CheckNivLog('3-4')) {
            $rech .= ' AND ' . TBCOCL . ".cocl_codeage = '" . valsession('code_age') . "'";
        }
        $rech .= ' ORDER BY ' . TBCOCL . '.cocl_code DESC';

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

        if ($rows AND (count($rows) > 0))
        {
        $rien_a_receptionner = FALSE;
        ?>
        <table width="50%" border="0" cellspacing="0" cellpadding="0" align="center">
            <tr>
                <td class="rubfrm" id="f_codecocl">
                    <div align="center"><?php echo _('Choisissez une annexe non encore signée'); ?> :</div>
                </td>
            </tr>
            <tr>
                <td>
                    <div align="center">
                        <?php
                        $tab_cocl = [];
                        foreach ($rows as $row) {
                            $row = encode_str($row);
                            extract($row);

                            $rech2 = 'SELECT cli_ste,cli_nom FROM `' . TBCLI . "` WHERE `cli_code` = '" . $cocl_codecli . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);

                            if ($row2) {
                                $row2 = encode_str($row2);
                                extract($row2);
                            }

                            $texte = $cocl_code . ' - ' . $cocla_code . ' - ';
                            if ($cli_ste <> '') {
                                $texte .= $cli_ste;
                            } else {
                                $texte .= $cli_nom;
                            }
                            $texte .= ' (' . _('envoyée le') . ' ' . datetolocal($cocla_dateenv) . ')';
                            $tab_cocl[$cocla_id] = $texte;
                        }
                        cre_select('codecocl', 'codecocl', $tab_cocl, $idcocla, '', _('-- Faites votre choix --'));
                        ?>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
                <td align="center" class="rubfrm" id="f_datesig">
                    <?php echo _('Signé le :'); ?>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <input name="datesigf" type="text" id="datesigf" value="" size="10" maxlength="10" readonly>
                    <img src="../img/raz.png" id="razdatesigf" border="0" align="absmiddle">
                </td>
            </tr>
            <tr>
                <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
                <td align="center" class="rubfrm" id="f_signataire">
                    <?php echo _('Signé par :'); ?>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <input name="signataire" type="text" id="signataire" value="" size="70" maxlength="70">
                </td>
            </tr>
        </table>
        <p align="center">
            <?php
            echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _('Valider') . '">';
            echo '<input name="annul" type="button" class="bton_std" id="annul" value="' . _('Annuler') . '">';
            }
            else {
                echo '<span class="annot_gras_it">';
                echo '<div align="center">';
                echo _("Pas d'annexe de contrat ouverte, envoyée et non reçue");
                echo '</div>';
                echo '</span>';
            }

            close_database();
            ?>
        </p>
    </form>
<?php
} else {
    if ($pbm_cocl == TRUE) {
        pop_ret_auto(_('Merci de choisir une de vos annexes contrats'), 'warn', 'cocla_lst.php?ord=DES&tri=Code');
    }
}

include_once('pied.php');
?>
</body>
</html>