<?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/func.inc.php");
include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.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("../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);
$pbm_intf = FALSE;
$cf = '';

$tx = $_GET['tx']; // Récupération origine ('m' : menu  / 'i' : intervention)
$td = $_GET['td']; // Récupération type demande
$ci = $_GET['ci']; // Récupération code Intervention
if (isset($_GET['cf']))
    $cf = $_GET['cf']; // Récupération code Facturation

if (($tx <> "m") AND ($tx <> "i"))
    $pbm_intf = TRUE;

if ($pbm_intf == FALSE) {
    if ($ci <> "") {
        // Récupération des informations intervention
        $row = lit_enr_int($ci);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            $int_dateddef = datetolocal($int_datedde);

            switch ($_SESSION["niv_log"]) {
                // Contrôle qu'une agence n'essaye pas de "hacker" une intervention
                case 4 :
                    if ($int_codeage == @$_SESSION["code_age"]) {
                        $row = lit_enr_age($int_codeage, TRUE);
                        if (is_array($row)) {
                            $row = encode_str($row);
                            extract($row);

                            if (intval($age_fact) == 0)
                                $pbm_intf = TRUE;
                        } else
                            $pbm_intf = TRUE;
                    } else
                        $pbm_intf = TRUE;
                    break;
            }
            if ($td <> "add") {
                if ($pbm_intf == FALSE) {
                    $row = lit_enr_intf($ci, $cf, TRUE);
                    if (is_array($row)) {
                        $row = encode_str($row);
                        extract($row);

                        // Formatage des dates en français
                        $intf_datefacf = datetolocal($intf_datefac);
                    } else
                        $pbm_intf = TRUE;
                }
            } else
                $intf_datecreaf = $datef;
        } else
            $pbm_intf = TRUE;
    } else
        $pbm_intf = TRUE;
}

switch ($td) {
    case "add" :
    case "edit" :
        $titre = _("SUIVI FACTURATION") . " (";
        $titre .= $int_code . ")";
        break;
    case "del" :
        $titre = _("SUPPRESSION FACTURATION") . " (";
        $titre .= $int_code . ")";
        break;
    case "view" :
        $titre = _("VISUALISATION FACTURATION") . " (";
        $titre .= $int_code . ")";
        break;
    default:
        $titre = _("PROBLEME");
        $pbm_intf = TRUE;
        break;
}
?>
<!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 facturation"); ?></title>

    <?PHP include_once("../inc/header.inc.php"); ?>
    <script language="javascript">
        <!--
        var tab_champ = ["nom:datefacf ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date"); ?> facturation ; stok:frmok ; stbad:frmbad",
            "nom:numf; format:alphanum ; req:Y ; txt:<?PHP echo _("Numéro facture"); ?> ; lmx:20 ; stok:frmok ; stbad:frmbad",
            "nom:mt; format:reel_ns ; req:Y ; txt:<?PHP echo _("Montant"); ?> ; vmn:0.01 ; lmx:10 ; stok:frmok ; stbad:frmbad",
            "nom:nomf; format:txt ; req:Y ; txt:<?PHP echo _("Facturé par"); ?> ; lmx:50 ; stok:frmok ; stbad:frmbad",
            "nom:codeme; format:liste ; req:Y ; txt:<?PHP echo _("Mode envoi"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        function init_datepicker() {
            $.datepicker.setDefaults({
                closeText: "<?PHP echo _("Fermer"); ?>",
                prevText: "<?PHP echo _("Précédent"); ?>",
                nextText: "<?PHP echo _("Suivant"); ?>",
                currentText: "<?PHP echo _("Aujourd'hui"); ?>",
                monthNames: ["<?PHP echo implode('","',$lng_tmoisf); ?>"],
                monthNamesShort: ["<?PHP echo implode('","',$lng_tmoism); ?>"],
                dayNames: ["<?PHP echo implode('","',$lng_tjourf); ?>"],
                dayNamesShort: ["<?PHP echo implode('","',$lng_tjourm); ?>"],
                dayNamesMin: ["<?PHP echo implode('","',$lng_tjours); ?>"],
                weekHeader: "<?PHP echo _("Sem."); ?>",
                dateFormat: "<?PHP echo $_SESSION['date_fmtshort']; ?>",
                firstDay: 1,
                isRTL: !1,
                changeMonth: true,
                changeYear: true,
                showMonthAfterYear: !1,
                yearSuffix: "",
                showButtonPanel: true,
                showOn: "both",
                buttonImageOnly: true,
                buttonImage: "../img/calendar.png",
                buttonText: "<?PHP echo _("Calendrier"); ?>"
            });
            $("#datefacf").datepicker({
                onSelect: function (datesel) {
                    $("#datefacf").blur();
                    <?PHP
                    if ($tx =='m')
                        echo "chk_datedif(datesel, this.id, '".$_SESSION['date_fmtshort']."', true, 'dateddef', '', '', false);";
                    else
                        echo "chk_datedif(datesel, this.id, '".$_SESSION['date_fmtshort']."', true, 'dateddef', '', '', true);";
                    ?>
                }
            });
        }

        function init_all() {
            init_Body();
            init_datepicker();
            chk_typfact_affoblig('fnulle', '<?PHP echo $td; ?>',<?PHP echo $_SESSION['num_nbdec']; ?>);
            init_chk_typfact_affoblig('fnulle', '<?PHP echo $td; ?>',<?PHP echo $_SESSION['num_nbdec']; ?>);
            init_chk_typfact_affoblig('init', '<?PHP echo $td; ?>', <?PHP echo $_SESSION['num_nbdec']; ?>, 'click');
            init_fdeci('mt', <?PHP echo $_SESSION['num_nbdec']; ?>);
            <?PHP
            switch($td)
            {
                case "add" :
                    echo "init_fensaisie('cree_me',1024,600);";
                    echo "init_valid_intf('create','intf_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                    break;
                case "edit" :
                    echo "init_fensaisie('cree_me',1024,600);";
                    echo "init_valid_intf('create','intf_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                    echo "init_valid_intf('del','intf_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
                case "del" :
                    echo "warn_del('');";
                    echo "readonly_all();";
                    break;
            }
            ?>
            Focus_first();
        }

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

</head>

<body>
<?PHP
if ($tx == "m")
    include_once("../inc/entete.inc.php");

if ($pbm_intf == FALSE) {
    cre_ent_form($titre, 'intf_lst.php?tx='.$tx.'&ci='.$ci.'&ord=ASC&tri=Facture', $_SERVER['HTTP_REFERER'], '');
    ?>
    <form name="intf_maj" id="intf_maj" method="post" action="intf_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre; ?>">
        <input name="tx" type="hidden" id="tx" value="<?PHP echo $tx; ?>">
        <input name="td" type="hidden" id="td" value="<?PHP echo $td; ?>">
        <input name="ci" type="hidden" id="ci" value="<?PHP echo $ci; ?>">
        <input name="cf" type="hidden" id="cf" value="<?PHP echo $cf; ?>">
        <input name="dateddeff" type="hidden" id="dateddef" value="<?PHP echo $int_dateddef; ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <table border="0" align="center">
            <tr>
                <td width="160" class="rubfrm" id="f_fnulle" align="right"><?PHP echo _("Facture nulle"); ?></td>
                <td>
                    <?PHP
                    cre_select('fnulle', 'fnulle', $non_oui, (int)$intf_fnulle);
                    ?>
                    <div
                        class="annot_petit_it"><?PHP echo("'Non' : La facture a été émise. L'intervention sera également notée comme facturée"); ?></div>
                    <div
                        class="annot_petit_it"><?PHP echo _("'Oui' : La facture n'a pas été émise ou a été émise avec un montant nul. Seule l'intervention sera notée comme facturée"); ?></div>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_dateddef" align="right"><?PHP echo _("Date facturation"); ?></td>
                <td>
                    <input name="datefacf" type="text" id="datefacf" value="<?PHP echo $intf_datefacf ?>" size="10"
                           maxlength="10" readonly>
                    <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_numf" align="right"><?PHP echo _("Numéro facture"); ?></td>
                <td>
                    <input name="numf" type="text" id="numf" value="<?PHP echo $intf_numf ?>" size="20" maxlength="20">
                    <span id="obl2"><img src="../img/oblig.png" align="absmiddle"
                                         alt="<?PHP echo _("Champ obligatoire"); ?>"></span>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_mt" align="right"><?PHP echo _("Montant"); ?></td>
                <td>
                    <?PHP
                    cre_input_num('mt', $intf_mt, TRUE);
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_nomf" align="right"><?PHP echo _("Facturé par"); ?></td>
                <td>
                    <input name="nomf" type="text" id="nomf" value="<?PHP echo $intf_nomf ?>" size="50" maxlength="50">
                    <span id="obl3"><img src="../img/oblig.png" align="absmiddle"
                                         alt="<?PHP echo _("Champ obligatoire"); ?>"></span>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_codeme" align="right"><?PHP echo _("Mode d'envoi"); ?></td>
                <td>
                    <?PHP
                    // création de la liste des mode d'envoi
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBME . "` ORDER BY me_nom";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('codeme', 'codeme', $rows, 'me_code', 'me_nom', '', $intf_codeme, '', _("-- Faites votre choix --"));

                    if (($td == "add") OR ($td == "edit")) // Ajout/Edition = zone modifiable (on peut choisir un mode)
                    {
                        $urlcreeme = "me_maj.php?td=add&tx=f";
                        echo '<span id="creme">';
                        cre_falselnk('', 'cree_me', 'span', '', _("Ajouter un mode"), $urlcreeme, '../img/add.png', _("Ajouter un mode"));
                        echo '</span>';
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_notaa" align="right"><?PHP echo _("Notes"); ?></td>
                <td>
                    <textarea name="nota" rows="5" id="nota"><?PHP echo $intf_nota ?></textarea>
                </td>
            </tr>
        </table>
        <br /><br />

        <p align="center">
            <input name="create" type="button" class="bton_std" id="create" value="<?PHP echo _("Valider"); ?>">
            <input name="init" type="reset" class="bton_std" id="init" value="<?PHP echo _("Réinitialiser"); ?>">
            <?PHP if ($td == "edit") { ?>
                <input name="del" type="button" class="bton_std" id="del" value="<?PHP echo _("Supprimer"); ?>">
            <?PHP } ?>
        </p>
    </form>
<?PHP
} else {
    switch ($tx) {
        case "i" :
            // close_fen_auto('', '');
            break;
        default :
            pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
            break;
    }
}
if ($tx == "m")
    include_once("pied.php");
?>
</body>
</html>