<?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/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'];
$idcage = "";
if (isset($_GET['ica']))
    $idcage = intval(@$_GET['ica']);
else
    $idcage = intval(@$_POST['ica']);
$id = "";
if (isset($_GET['id']))
    $id = intval(@$_GET['id']);
else
    $id = intval(@$_POST['id']);

$pbm_cage = FALSE;
$pbm_enr_cagepr = FALSE;
$msg = "";
$lastid = 0;
$lastdate = "";
$totalpr = 0;
$datesais = '';

if ($id > 0) {
    $row = lit_enr_cagepr($id);
    if (is_array($row))
    {
        $row = encode_str($row);
        extract($row);
        $datesais = datetolocal($cagepr_date);
    }
    else
        $pbm_cage = TRUE;
}


if ($pbm_cage == FALSE)
{
    if (($idcage > 0) AND ($codeage <> "")) {
        // vérification tentative de hack d'une agence
        if ((@$_SESSION["niv_log"] == 4) AND ($cagepr_codeage <> @$_SESSION["code_age"]))
            $pbm_cage = TRUE;

        if ($pbm_cage == FALSE) {
            $row = lit_enr_age($codeage, TRUE);
            if (is_array($row)) {
                extract($row);

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

                $totalpr = nb_cagepr($idcage, TRUE);
                if ($totalpr == 0)
                    $td = "add";
                if ((@$_POST["nbutton"] == "add") OR (@$_POST["nbutton"] == "enr")) {
                    $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 `" . TBCAGEPR . "`";
                            $rech .= " (cagepr_idcage,cagepr_date,cagepr_couth) VALUES";
                            $rech .= " ('$idcage','$datesaisa','$couth')";
                        }
                        if (@$_POST["nbutton"] == "enr") {
                            if ($id > 0) {
                                $rech = "UPDATE `" . TBCAGEPR;
                                $rech .= "` SET `cagepr_date`='" . $datesaisa . "',`cagepr_couth`='" . $couth . "' WHERE `cagepr_id` = '" . $id . "'";
                            }
                        }
                        $db->query($rech);
                        $db->disconnect();
                        unset($_POST);
                        $td = "add";
                    } else {
                        $pbm_enr_cagepr = TRUE;
                        $msg = _("Problème de saisie");
                    }
                } else {
                    if (@$_POST["nbutton"] == _("del")) {
                        if ($id > 0) {
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                            $rech = "DELETE FROM `" . TBCAGEPR . "` WHERE `cagepr_id` = '$id'";
                            $db->query($rech);
                            $db->disconnect();
                        }
                        unset($_POST);
                        $td = "add";
                    }
                }
            } else
                $pbm_cage = TRUE;
        }
    } else
        $pbm_cage = TRUE;
}

if ($pbm_cage == FALSE) {
    $lastid = last_id_cagepr($idcage);
    $lastdate = last_date_cagepr($lastid);

    switch ($td)
    {
        case "add" :
            $titre = _("Ajouter prix de revient");
            if ($lastid > 0)
                $datesais = datetolocal(nouv_date_plus_jours($lastdate, 1));
            else
                $datesais = datetolocal($age_datecrea);
            break;
        case "edit" :
            $titre = _("Editer prix de revient");
            break;
        case "view" :
            $titre = _("Voir prix de revient");
            break;
        case "del" :
            $titre = _("Supprimer prix de revient");
            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 _("Prix de revient contact 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();
                    return chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', true, 'datesais', '', '',false)
                }
            });
        }


        function init_all() {
            init_Body();
            init_autohref_classe('falselnkimg','');
            init_datepicker();
            <?php
            switch ($td)
            {
                case "add" :
                    echo "init_valid_form('add','cagepr_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "edit" :
                    echo "init_valid_form('enr','cagepr_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "readonly_id('code',false);";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
                case "del" :
                    echo "init_valid_form('del','cagepr_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
            }
            echo "init_autohref('annul','click', 'cagepr_maj.php?td=add&ca=".$codeage."&ica=".$idcage."',false);";
            ?>
            init_autoraz('razdate', 'date', 'click');
            init_fdeci('couth', <?PHP echo $_SESSION['num_nbdec']; ?>);
            add_func_click('quit', 'sclose','');
            Focus_first();
        }

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

</head>

<body>


<?PHP
if (($pbm_cage == FALSE) AND ($pbm_enr_cagepr == 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 contact"); ?> : <?php echo $cagepr_nom . " " . $cagepr_prenom; ?> </span>
    </p>
    <p align="center"><span class="titre_gras">(<?PHP Echo _("agence"); ?>  : <?php echo $age_nom; ?>)</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 `" . TBCAGEPR . "` WHERE `cagepr_idcage`= '" . $idcage . "' ORDER BY cagepr_id";
$rows = $db->get_results($rech, ARRAY_A);

if (($rows) AND (count($rows) <> 0))
{
    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["cagepr_date"]);
        echo "<td align=\"right\">\n";
        echo full_num_format($row["cagepr_couth"],'', '', TRUE, '', FALSE);
        echo "</td>\n";

        echo "<td align=\"center\">\n";
        if (($td <> "view") AND ($lastid == $row["cagepr_id"])) {
            $lst_maj = TRUE;
            cre_falselnk_img('', 'cagepr_edit_'.$row["cagepr_id"], 'span', 'center', _("Edition"), 'lnk', 'cagepr_maj.php?td=edit&ca=' . $codeage . '&ica=' . $idcage. '&id='.$row["cagepr_id"], '../img/edit.png');
            $lst_del = TRUE;
            cre_falselnk_img('', 'cagepr_del_'.$row["cagepr_id"], 'span', 'center', _("Suppression"), 'lnk', 'cagepr_maj.php?td=del&ca=' . $codeage . '&ica=' . $idcage. '&id='.$row["cagepr_id"], '../img/del.png');
        } else
            echo "-";
        echo "</td>\n";
        echo "</tr>\n";

        $j++;
    }

    echo "</table>\n";
    // Fin affichage table détail horaire
}
else
{
?>
    <p class="annot_petit_it"><?PHP echo _("Aucun prix de revient enregistré"); ?></p>
<?PHP
}
$db->disconnect();
$taille_piedla = "";
include_once("piedla.inc.php");

if ($td <> "view")
{
?>
<br />
    <form name="cagepr_maj" id="cagepr_maj" class="bord_pointilles" method="post" action="cagepr_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="ica" type="hidden" id="ica" value="<?PHP echo $idcage; ?>">
        <input name="id" type="hidden" id="id" value="<?PHP echo $id; ?>">
        <input name="datesais" type="hidden" id="datesais" value="<?PHP echo $datesais; ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <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">
                    <?PHP
                    switch ($td)
                    {
                        case "add" :
                            echo ' <img src="../img/raz.png" id="razdate" border="0" align="absmiddle">';
                            echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _("Champ obligatoire") . '">';
                            if ($lastid > 0)
                                echo '<br /><span class="annot_petit_it">('._("Strictement supérieur à dernière date de modification").' : '.$datesais.')</span>';
                            else
                                echo '<br /><span class="annot_petit_it">('._("Supérieur ou égal à la date de création de l'agence").' : '.$datesais.')</span>';
                            break;
                        case "edit" :
                            echo ' <img src="../img/raz.png" id="razdate" border="0" align="absmiddle">';
                            echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _("Champ obligatoire") . '">';
                            echo '<br /><span class="annot_petit_it">('._("Ne peut être inférieure à la date précédemment saisie").')</span>';
                            break;
                        default :
                            break;
                    }
                    ?>
                </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', $cagepr_couth);
                    ?>
                </td>
            </tr>
        </table>
        <p align="center">
            <input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo _("Annuler"); ?>">
            <?PHP
            switch ($td)
            {
                case "add" :
                    echo '<input name="add" type="button" class="bton_std" id="add" value="'._("Valider").'">';
                    break;
                case "edit" :
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="'._("Valider").'">';
                    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_cage == TRUE)
        close_fen_auto(_("Problème de paramètres"), 'fscomp2', '');

    if ($pbm_enr_cagepr == TRUE)
        pop_ret_auto($msg, 'warn', 'cagepr_maj.php?ca=' . $codeage . 'ica=' . $idcage . '&td=' . $td . '&id=' . $id);
}
?>
</body>
</html>
