<?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_droits.inc.php");
include_once("../inc/fic_loc.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(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$titre = @$_POST["titre"];
$td = @$_POST["td"];
$codecoloc = @$_POST["codecoloc"];

$code = str_majuscules(@$_POST["code"]);
$dd = datetomysql(@$_POST["dd"]);
$hd = @$_POST["hd"] . ":00";
$df = datetomysql(@$_POST["df"]);
$hf = @$_POST["hf"] . ":00";
$coderegc = str_majuscules(@$_POST["coderegc"]);
$datedep = datetomysql(@$_POST["datedepf"]);
$heuredep = @$_POST["hdep"] . ":00";
$didx1 = @$_POST["didx1"];
$didx2 = @$_POST["didx2"];
$prispar = @$_POST["prispar"];
$dateret = datetomysql(@$_POST["dateretf"]);
$heureret = @$_POST["hret"] . ":00";
$ridx1 = @$_POST["ridx1"];
$ridx2 = @$_POST["ridx2"];
$retpar = @$_POST["prispar"];
$rcaut = @$_POST["rcaut"];
$reloc = @$_POST["reloc"];
$noted = @$_POST["noted"];
$noter = @$_POST["noter"];

$retourliste = TRUE;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche Produit Location (réservation)
    case "add" :
        $rech = "SELECT COUNT(*) FROM `" . TBPDTL . "` WHERE pdtl_codecoloc = '$codecoloc' AND pdtl_codepdtloc = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rech, ARRAY_A);
        if ($nbres == 0) {
            if ($_SESSION["niv_log"] == 3)
                $codeuser = @$_SESSION["code_log"];
            else
                $codeuser = @$_POST["codecrea"];

            $rech2 = "INSERT INTO `" . TBPDTL . "`";
            $rech2 .= " (pdtl_codecrea,pdtl_codemod,pdtl_codecoloc,pdtl_codepdtloc,pdtl_datedeb,pdtl_heuredeb,pdtl_datefin,pdtl_heurefin) VALUES";
            $rech2 .= " ('$codeuser','$codeuser','" . $db->escape($codecoloc) . "','" . $db->escape($code) . "','$dd','$hd','$df','$hf')";
            $db->query($rech2);
            $lastid = $db->insert_id;

            // Ajout du barème rattaché
            $rech2 = "INSERT INTO `" . TBPDTLBL . "`";
            $rech2 .= " (pdtlbl_idpdtl,pdtlbl_idbarloc,pdtlbl_qte,pdtlbl_pvht,pdtlbl_codetva,pdtlbl_caution,pdtlbl_tvacaut,";
            $rech2 .= "pdtlbl_avecass,pdtlbl_mtass,pdtlbl_codetvaass,pdtlbl_qconsi1,pdtlbl_pdtuconsi1,pdtlbl_puconsi1,";
            $rech2 .= "pdtlbl_qconsi2,pdtlbl_pdtuconsi2,pdtlbl_puconsi2) VALUES";
            $rech2 .= " ('$lastid','" . $_SESSION["pdtlbl"][0]["idbarloc"] . "','" . $_SESSION["pdtlbl"][0]["qte"] . "','" . $_SESSION["pdtlbl"][0]["pvht"] . "','" . $_SESSION["pdtlbl"][0]["codetva"] . "',";
            $rech2 .= "'" . $_SESSION["pdtlbl"][0]["caution"] . "','" . $_SESSION["pdtlbl"][0]["tvacaut"] . "','" . $_SESSION["pdtlbl"][0]["avecass"] . "',";
            $rech2 .= "'" . $_SESSION["pdtlbl"][0]["mtass"] . "','" . $_SESSION["pdtlbl"][0]["codetvaass"] . "','" . $_SESSION["pdtlbl"][0]["qconsi1"] . "','" . $_SESSION["pdtlbl"][0]["pdtuconsi1"] . "',";
            $rech2 .= "'" . $_SESSION["pdtlbl"][0]["puconsi1"] . "','" . $_SESSION["pdtlbl"][0]["qconsi2"] . "','" . $_SESSION["pdtlbl"][0]["pdtuconsi2"] . "','" . $_SESSION["pdtlbl"][0]["puconsi2"] . "')";
            $db->query($rech2);

            $msg = _("Produit ajouté");
        } else {
            $msg = _("Ce produit est déjà saisi dans cette location");
            $retourliste = FALSE;
        }
        break;

    // MAJ de la fiche Produit Location (Réservation)
    case "edit" :
        $rech = "SELECT * FROM `" . TBPDTL . "` WHERE pdtl_codecoloc = '" . $db->escape($codecoloc) . "' AND pdtl_codepdtloc = '" . $db->escape($code) . "'";
        $row = $db->get_row($rech, ARRAY_A);
        if ($row) {
            extract($row);

            // On met à jour le barème rattaché
            $rech2 = "UPDATE `" . TBPDTLBL . "` SET `pdtlbl_avecass`='" . $_SESSION["pdtlbl"][0]["avecass"] . "'";
            $rech2 .= " WHERE `pdtlbl_idpdtl` = '" . $pdtl_id . "'";
            $db->query($rech2);

            // On met à jour la fiche produit
            $codeuser = @$_SESSION["code_log"];

            $rech2 = "UPDATE `" . TBPDTL . "` SET `pdtl_codemod`='" . $codeuser . "'";
            $rech2 .= " WHERE `pdtl_id` = '" . $pdtl_id . "'";
            $db->query($rech2);
        }

        $msg = _("Produit mis à jour");
        break;

    // MAJ de la fiche Produit Location (Départ produit)
    case "dep" :
        if ($_SESSION["niv_log"] == 3)
            $codeuser = @$_SESSION["code_log"];
        else
            $codeuser = @$_POST["codedep"];

        $rech = "UPDATE `" . TBPDTL . "` SET `pdtl_coderegc`='" . $coderegc . "', `pdtl_datedep`='" . $datedep . "',";
        $rech .= " `pdtl_heuredep`='" . $heuredep . "', `pdtl_didx1`='" . $didx1 . "', `pdtl_didx2`='" . $didx2 . "',";
        $rech .= " `pdtl_prispar`='" . $prispar . "', `pdtl_noted`='" . $noted . "', `pdtl_codedep`='" . $codeuser . "'";
        $rech .= " WHERE `pdtl_codecoloc` = '" . $db->escape($codecoloc) . "' AND `pdtl_codepdtloc` = '" . $db->escape($code) . "'";
        $db->query($rech);

        // On met à jour la fiche produit
        $rech = "UPDATE `" . TBPDTLOC . "` SET `pdtloc_louable`=0, `pdtloc_loue`=1";
        $rech .= " WHERE `pdtloc_code` = '" . $db->escape($code) . "'";
        $db->query($rech);

        $msg = _("Produit mis à jour");
        break;

    // MAJ de la fiche Produit Location (Retour produit)
    case "ret" :
        $rech = "SELECT * FROM `" . TBPDTL . "` WHERE pdtl_codecoloc = '" . $db->escape($codecoloc) . "' AND pdtl_codepdtloc = '" . $db->escape($code) . "'";
        $row = $db->get_row($rech, ARRAY_A);
        if ($row) {
            extract($row);

            // Suppression de la pénalité location
            $rech2 = "DELETE FROM `" . TBPDTLPL . "` WHERE `pdtlpl_idpdtl` = '$pdtl_id'";
            $db->query($rech2);
            // Injection de la pénalité location
            if ($_SESSION["pdtlpl"][0]["codepen"] <> "-1") {
                $pdtlpl_codepen = $_SESSION["pdtlpl"][0]["codepen"];
                $pdtlpl_valeur = $_SESSION["pdtlpl"][0]["valeur"];
                $pdtlpl_codetva = $_SESSION["pdtlpl"][0]["tva"];
                $rech2 = "INSERT INTO `" . TBPDTLPL . "`";
                $rech2 .= " (pdtlpl_idpdtl,pdtlpl_codepen,pdtlpl_valeur,pdtlpl_codetva) VALUES";
                $rech2 .= " ('$pdtl_id','$pdtlpl_codepen','$pdtlpl_valeur','$pdtlpl_codetva')";
                $db->query($rech2);
            }

            // Suppression du monta consommables
            $rech2 = "DELETE FROM `" . TBPDTLCL . "` WHERE `pdtlcl_idpdtl` = '$pdtl_id'";
            $db->query($rech2);
            // Injection du monta consommables
            if ($_SESSION["pdtlcl"][0]["valeur"] <> "-1") {
                $pdtlcl_valeur = $_SESSION["pdtlcl"][0]["valeur"];
                $pdtlcl_codetva = $_SESSION["pdtlcl"][0]["tva"];
                $rech2 = "INSERT INTO `" . TBPDTLCL . "`";
                $rech2 .= " (pdtlcl_idpdtl,pdtlcl_valeur,pdtlcl_codetva) VALUES";
                $rech2 .= " ('$pdtl_id','$pdtlcl_valeur','$pdtlcl_codetva')";
                $db->query($rech2);
            }
        }

        if ($_SESSION["niv_log"] == 3)
            $codeuser = @$_SESSION["code_log"];
        else
            $codeuser = @$_POST["coderet"];

        $rech = "UPDATE `" . TBPDTL . "` SET `pdtl_dateret`='" . $dateret . "', `pdtl_heureret`='" . $heureret . "',";
        $rech .= "`pdtl_ridx1`='" . $ridx1 . "', `pdtl_ridx2`='" . $ridx2 . "', `pdtl_rcaut`='" . $rcaut . "', `pdtl_reloc`='" . $reloc . "',";
        $rech .= "`pdtl_retpar`='" . $retpar . "', `pdtl_noter`='" . $noter . "', `pdtl_coderet`='" . $codeuser . "'";
        $rech .= " WHERE `pdtl_codecoloc` = '" . $db->escape($codecoloc) . "' AND `pdtl_codepdtloc` = '" . $db->escape($code) . "'";
        $db->query($rech);

        // On met à jour la fiche produit
        $rech = "UPDATE `" . TBPDTLOC . "` SET `pdtloc_loue`=0";
        switch ($reloc) {
            case 0 :
                $rech .= ", `pdtloc_louable`=0";
                break;
            case 1 :
                $rech .= ", `pdtloc_louable`=1";
                break;
        }
        $rech .= " WHERE `pdtloc_code` = '" . $code . "'";
        $db->query($rech);

        $msg = _("Produit mis à jour");
        break;

    // Suppression de la fiche Produit location
    case "del" :
        $rech = "SELECT * FROM `" . TBPDTL . "` WHERE pdtl_codecoloc = '" . $db->escape($codecoloc) . "' AND pdtl_codepdtloc = '" . $db->escape($code) . "'";
        $row = $db->get_row($rech, ARRAY_A);
        if ($row) {
            extract($row);
            // Suppression du barème location
            $rech2 = "DELETE FROM `" . TBPDTLBL . "` WHERE `pdtlbl_idpdtl` = '$pdtl_id'";
            $db->query($rech2);
            // Suppression de la pénalité location
            $rech2 = "DELETE FROM `" . TBPDTLPL . "` WHERE `pdtlpl_idpdtl` = '$pdtl_id'";
            $db->query($rech2);
            // Suppression du montant consommables
            $rech2 = "DELETE FROM `" . TBPDTLCL . "` WHERE `pdtlcl_idpdtl` = '$pdtl_id'";
            $db->query($rech2);

            // On met à jour la fiche produit
            $rech = "UPDATE `" . TBPDTLOC . "` SET `pdtloc_loue`=0";
            $rech .= ", `pdtloc_louable`=1";
            $rech .= " WHERE `pdtloc_code` = '" . $db->escape($code) . "'";
            $db->query($rech);
        }
        $rech = "DELETE FROM `" . TBPDTL . "` WHERE `pdtl_id` = '$pdtl_id'";
        $db->query($rech);

        $msg = _("Produit supprimé");
        break;

    default :
        break;
}
$db->disconnect();
?>
<!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 _("Article Location"); ?></title>

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

    <script type="text/javascript">
        <!--
        <?PHP
            cre_html_pdtincloc('pdtincloc', 'edit', $codecoloc, 'window.opener.');
        ?>

        function init_all() {
            init_Body();
            <?PHP
            $nbpdtloc = nb_pdtloc_in_coloc($codecoloc);
            if ($nbpdtloc == 0)
                echo "readonly_id('enr-enrimp',true);";
            else
                echo "readok_id('enr-enrimp',true);";
            echo "pdtincloc();";
            if (($td == "add") OR ($td == "dep") OR ($td == "ret"))
                $urlimp = "pdtldr_pdf.php?cl=" . $codecoloc . "&ca=" . $code;
            echo "init_fenimp('".$urlimp."');";
            echo "self.close();";
            ?>
            Focus_first();
        }

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

</head>
<body>

<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
</body>
</html>
