<?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/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/fic_age.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_prm.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);
$_POST = clean_str($_POST);

$td = "";
if (isset($_GET['td']))
    $td = @$_GET['td'];
else
    $td = @$_POST['td'];
$codeage = "";
if (isset($_GET['ca']))
    $codeage = @$_GET['ca'];
else
    $codeage = @$_POST['ca'];
$id = "";
if (isset($_GET['id']))
    $id = @$_GET['id'];
else
    $id = @$_POST['id'];

$pbm_age = FALSE;
$pbm_enr_agepr = FALSE;
$msg = "";
$lastid = 0;
$lastdate = "";
$totalpr = 0;

if ($codeage <> "") {
    $row = lit_enr_age($codeage, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        $codeadev = give_dev_codea_defaut_age($codeage, $_SESSION["lng"]);

        // vérification tentative de hack d'une agence
        if ((@$_SESSION["niv_log"] == 4) AND ($age_code <> @$_SESSION["code_age"]))
            $pbm_age = TRUE;

        if ($pbm_age == FALSE) {
            $totalpr = nb_agepr($codeage, TRUE);
            if ($totalpr == 0)
                $td = "add";
            if ((@$_POST["nbutton"] == "add") OR (@$_POST["nbutton"] == "mod")) {
                $datesais = @$_POST["date"];
                if ($datesais <> "")
                    $datesaisa = datetomysql($datesais);
                else
                    $datesaisa = "";
                $couth = @$_POST["couth"];

                if (($datesaisa <> "") AND (floatval($couth) > 0)) {
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    if (@$_POST["nbutton"] == "add") {
                        $rech = "INSERT INTO `" . TBAGEPR . "`";
                        $rech .= " (agepr_codeage,agepr_date,agepr_couth) VALUES";
                        $rech .= " ('$codeage','$datesaisa','$couth')";
                    }
                    if (@$_POST["nbutton"] == "mod") {
                        if ($id <> "") {
                            $rech = "UPDATE `" . TBAGEPR;
                            $rech .= "` SET `agepr_date`='" . $datesaisa . "',`agepr_couth`='" . $couth . "' WHERE `agepr_id` = '" . $id . "'";
                        }
                    }
                    $db->query($rech);
                    $db->disconnect();
                    unset($_POST);
                    $td = "add";
                } else {
                    $pbm_enr_agepr = TRUE;
                    $msg = _("Problème de saisie");
                }
            } else {
                if (@$_POST["nbutton"] == "del") {
                    if ($id <> "") {
                        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                        $rech = "DELETE FROM `" . TBAGEPR . "` WHERE `agepr_id` = '$id'";
                        $db->query($rech);
                        $db->disconnect();
                    }
                    unset($_POST);
                    $td = "add";
                }
            }
        }
    } else
        $pbm_age = TRUE;
} else
    $pbm_age = TRUE;

if ($pbm_age == FALSE) {
    $lastid = last_id_agepr($codeage);
    $lastdate = last_date_agepr($lastid);

    if ($td == "add") {
        $titre = _("Ajouter prix de revient");
        if ($lastid > 0)
            $datesais = datetolocal(nouv_date_plus_jours($lastdate, 1));
        else
            $datesais = datetolocal($age_datecrea);
    }
    if ($td == "edit") {
        if ($id <> "") {
            $row = lit_enr_agepr($id);
            if (is_array($row))
                extract($row);
            $datesais = datetolocal($agepr_date);
        }
        $titre = _("Editer prix de revient");
    }
    if ($td == "view") {
        if ($id <> "") {
            $row = lit_enr_agepr($id);
            if (is_array($row))
                extract($row);
            $datesais = datetolocal($agepr_date);
        }
        $titre = _("Voir prix de revient");
    }
    if ($td == "del") {
        if ($id <> "") {
            $row = lit_enr_agepr($id);
            if (is_array($row))
                extract($row);
            $datesais = datetolocal($agepr_date);
        }
        $titre = _("Supprimer prix de revient");
    }
}
?>
<!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 _("Prix de revient responsable agence"); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:date ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date"); ?> ; vmn:<?PHP if ($lastid>0) {if ($td=="add") echo datetolocal(nouv_date_plus_jours($lastdate,1)); else echo datetolocal($lastdate); } else echo datetolocal($age_datecrea); ?> ; stok:frmok ; stbad:frmbad",
            "nom:couth; format:reel_ns ; req:Y ; txt:<?PHP echo _("coût horaire"); ?> ; vmn:0.01 ; 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"); ?>"
            });

            $("#date").datepicker({
                onSelect: function (datesel) {
                    $("#date").blur();
                    chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', true, 'datechk', '', '',false);
                }
            });
        }


        function init_all() {
            init_Body();
            init_autohref_classe('falselnkimg','');
            init_datepicker();
            init_fdeci('couth', <?PHP echo $_SESSION['num_nbdec']; ?>);
            <?php
            switch ($td)
            {
                case "add" :
                    echo "init_valid_form('add','agepr_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "edit" :
                    echo "init_valid_form('mod','agepr_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
                case "del" :
                    echo "init_valid_form('del','agepr_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
            }
            echo "init_autoraz('razdate','date','click');";
            ?>
            add_func_click('quit', 'sclose','');
            Focus_first();
        }

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

</head>

<body>

<?PHP
if (($pbm_age == FALSE) AND ($pbm_enr_agepr == FALSE)) {
    ?>
    <p align="center"><span class="titrepage"><?PHP echo _("PRIX DE REVIENT"); ?></span></p>
    <p align="center"><span
            class="titre_gras"><?PHP echo _("Pour le responsable agence"); ?> : <?php echo $age_resp_nom . " " . $age_resp_prenom; ?> </span>
    </p>
    <div align="right">
        <input name="quit" type="button" class="bton_std" id="quit" value="<?PHP echo _("Quitter"); ?>"
               style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;">
    </div>
    <br />
<?PHP
$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
$rech = "SELECT * FROM `" . TBAGEPR . "` WHERE `agepr_codeage`= '" . $db->escape($codeage) . "' ORDER BY agepr_id";
$rows = $db->get_results($rech, ARRAY_A);
$db->disconnect();

if ($rows) {
    echo "<table class=\"MtableD\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";

    // création de l'entête de la table "détail prix de revient"
    echo "<th>";
    echo _("Date");
    echo "</th>\n";
    echo "<th>";
    echo _("Coût horaire");
    echo "</th>\n";
    echo "<th>";
    echo _("Cliquer");
    echo "</th>\n";

    $j = 0;

    foreach ($rows as $row) {
        // création de l'entête de ligne (TR)
        creetrlstdet($j);

        echo "<td align=\"center\">\n";
        echo datetolocal($row["agepr_date"]);
        echo "<td align=\"right\">\n";
        echo full_num_format($row["agepr_couth"]);
        echo "</td>\n";
        echo "<td align=\"center\">\n";
        if (($td <> "view") AND ($lastid == $row["agepr_id"])) {
            $lst_maj = TRUE;
            cre_falselnk_img('', 'agepr_edit_'.$row["agepr_id"], 'span', 'center', _("Edition"), 'lnk', 'agepr_maj.php?td=edit&ca=' . $codeage . '&id=' . $row["agepr_id"] , '../img/edit.png');
            $lst_del = TRUE;
            cre_falselnk_img('', 'agepr_del_'.$row["agepr_id"], 'span', 'center', _("Suppression"), 'lnk', 'agepr_maj.php?td=del&ca=' . $codeage . '&id=' . $row["agepr_id"] , '../img/del.png');
        } else
            echo "-";
        echo "</td>\n";

        echo "</tr>\n";

        $j++;
    }

    echo "</table>\n";
    // Fin affichage table détail horaire
} else
    echo '<p class="annot_petit_it">'._("Aucun prix de revient enregistré").'</p>';
$db->disconnect();

$taille_piedla = "";
include_once("piedla.inc.php");

if ($td <> "view")
{
?>
<br />
    <form name="agepr_maj" id="agepr_maj" class="bord_pointilles" method="post" action="agepr_maj.php">
        <p align="center" class="annot_std_gras"><?PHP echo $titre; ?></p>

        <p align="center" class="annot_petit_it"><?PHP echo _("Tous les champs sont obligatoires"); ?></p>
        <input name="td" type="hidden" id="td" value="<?PHP echo $td; ?>">
        <input name="ca" type="hidden" id="ca" value="<?PHP echo $codeage; ?>">
        <input name="id" type="hidden" id="id" value="<?PHP echo $id; ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <input name="datechk" type="hidden" id="datechk" value="<?PHP if ($lastid>0) {if ($td=="add") echo datetolocal(nouv_date_plus_jours($lastdate,1)); else echo datetolocal($lastdate); } else echo datetolocal($age_datecrea); ?>">
        <table width="580" border="0" align="center">
            <tr>
                <td width="30%" class="rubfrm" id="f_date" align="right"><?PHP echo _("Date"); ?></td>
                <td align="left">
                    <input name="date" type="text" id="date" value="<?PHP echo $datesais; ?>" size="10" maxlength="10" readonly="1">
                     <img src="../img/raz.png" id="razdate" border="0" align="absmiddle">
                    <?PHP
                    if ($td == "add") {
                        echo '<br />';
                        if ($lastid > 0)
                            echo '<span class="annot_petit_it">(' . _("Strictement supérieur à dernière date de modification") . ')</span>';
                        else
                            echo '<span class="annot_petit_it">(' . _("Supérieur ou égal à la date de création de l'agence") . ' : ' . datetolocal($age_datecrea) . '</span>';
                    }

                    if ($td == "edit")
                        echo '<br /><span class="annot_petit_it">(' . _("Ne peut être inférieure à la date précédemment saisie") . ')</span>';
                    ?>
                </td>
            </tr>
            <tr>
                <td width="30%" class="rubfrm" id="f_tec" align="right"><?PHP echo _("Coût horaire"); ?></td>
                <td align="left">
                    <?PHP
                    cre_input_num('couth', $agepr_couth, TRUE);
                    ?>
                </td>
            </tr>
        </table>
        <p align="center">
            <input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo _("Annuler"); ?>"
                   onclick="self.location.href='agepr_maj.php?td=add&ca=<?PHP echo $codeage; ?>';">
            <?PHP
            switch ($td)
            {
                case "add" :
                    echo '<input name="add" type="button" class="bton_std" id="add" value="'._("Ajouter").'">';
                    break;
                case "edit" :
                    echo '<input name="mod" type="button" class="bton_std" id="mod" value="'._("Modifier").'">';
                    break;
                case "del" :
                    echo '<input name="del" type="button" class="bton_std" id="del" value="'._("Supprimer").'">';
                    break;
            }
            echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
            ?>
        </p>
    </form>
<?PHP
}
} else {
    if ($pbm_age == TRUE)
        close_fen_auto(_("Problème de paramètres"), 'fscomp', '');

    if ($pbm_enr_agepr == TRUE)
        pop_ret_auto(_("Problème de paramètres"), 'warn', 'agepr_maj.php?ca=' . $codeage . '&td=' . $td . '&id=' . $id);
}
?>

</body>
</html>
