<?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/fic_age.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_loc.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_time.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/func_txtcm.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../classes/phpmailer/class.smtp.php");
include_once("../inc/mail.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);

$rlp_choisis = @$_SESSION["rlp_choisis"];

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

$datecrea = datetomysql(@$_POST["datecreaf"]);
$dateclos = datetomysql(@$_POST["dateclosf"]);
$datedeb = datetomysql(@$_POST["datedebf"]);
$datefin = datetomysql(@$_POST["datefinf"]);
$code = str_majuscules(@$_POST["code"]);
$codeage = str_majuscules(@$_POST["codeage"]);
$codecli = str_majuscules(@$_POST["codecli"]);
$codeadev = str_majuscules(@$_POST["codeadev"]);
$prev = intval(@$_POST["prev"]);
$idsite = str_majuscules(@$_POST["idsite"]);
$ddeur = str_majuscules(@$_POST["ddeur"]);

$codeuser = @$_SESSION["code_log"];

$retourliste = TRUE;
$enr_ok = TRUE;

if (@$_POST["nbutton"] == "enrimp") {
    $imprim = TRUE;
    $retourliste = FALSE;
}


switch ($td) {
    // Ajout de la fiche Contrat location
    case "add" :
        $codeloc = crecode("LO", TRUE);

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "INSERT INTO `" . TBCOLOC . "`";
        $rech .= " (coloc_datecrea,coloc_codecrea,coloc_code,coloc_codeage,coloc_typcli,coloc_codecli,coloc_codeadev,";
        $rech .= "coloc_idsite,coloc_ddeur,coloc_datedeb,coloc_datefin)";
        $rech .= " VALUES";
        $rech .= " ('$datecrea','$codeuser','$codeloc','$codeage','$tc','$codecli','$codeadev','$idsite','$ddeur','$datedeb','$datefin')";
        $db->query($rech);

        /* vérification du type de client et MAJ si nécessaire */
        if ($tc == 0)
            upd_nivcli($codecli, FALSE);

        $db->disconnect();

        $msg = _("Contrat"). ' '  . $codeloc . ' ' ._("créé");

        if ($retourliste == TRUE) {
            // Envoi d'email vers le client
            if ($prev == 0)
                envoi_mail("", _("Création de votre contrat de location"), "col", "cre", "xxx", "cli", $codeloc, "", "", FALSE);
            else {
                if ($prev <> 4) // Le niveau 4 est "Ne pas prévenir"
                {
                    $pcli = TRUE;
                    $txtcorps = cre_corps_txtc("col", "cre", "xxx", "cli", $codeloc, "", "");
                    $_SESSION["txt"] = $txtcorps;
                }
                // Envoi d'email vers le webmaster
                envoi_mailwm("", _("Création d'un contrat de location"), "col", "cre", "xxx", "cli", $codeloc, "", "");
            }
            // Envoi d'email vers l'agence
            if (@$_SESSION["niv_log"] <> 4)
                envoi_mail("", _("Création de contrat de location"), "col", "cre", "xxx", "age", $codeloc, "", "", FALSE);
            // Envoi d'email vers le centre de gestion
            envoi_mail("", _("Création de contrat de location"), "col", "cre", "xxx", "cdg", $codeloc, "", "", TRUE);

        }

        break;

    // MAJ de la fiche Contrat location
    case "edit" :
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "UPDATE `" . TBCOLOC;
        $rech .= "` SET `coloc_codeage`='" . $codeage . "',`coloc_codecli`='" . $codecli . "',`coloc_codeadev`='" . $codeadev . "',`coloc_idsite`='" . $idsite . "',";
        $rech .= " `coloc_ddeur`='" . $ddeur . "', `coloc_datedeb`='" . $datedeb . "', `coloc_datefin`='" . $datefin . "'";
        $rech .= " WHERE `coloc_code` = '" . $db->escape($code) . "'";

        $db->query($rech);

        /* vérification du type de client et MAJ si nécessaire */
        if ($tc == 0)
            upd_nivcli($codecli, FALSE);

        $db->disconnect();

        if ($retourliste == TRUE) {
            // Envoi d'email vers le client
            if ($prev == 0)
                envoi_mail("", _("Modification de votre contrat de location"), "col", "mod", "xxx", "cli", $code, "", "", FALSE);
            else {
                if ($prev <> 4) // Le niveau 4 est "Ne pas prévenir"
                {
                    $pcli = TRUE;
                    $txtcorps = cre_corps_txtc("col", "cli", "xxx", "cli", $code, "", "");
                    $_SESSION["txt"] = $txtcorps;
                }
                // Envoi d'email vers le webmaster
                envoi_mailwm("", _("Modification d'un contrat de location"). ' ' . $code, "col", "mod", "xxx", "cli", $code, "", "");
            }
            // Envoi d'email vers l'agence
            if (@$_SESSION["niv_log"] <> 4)
                envoi_mail("", _("Modification d'un contrat de location"). ' ' . $code, "col", "mod", "xxx", "age", $code, "", "", FALSE);
            // Envoi d'email vers le centre de gestion
            envoi_mail("", _("Modification d'un contrat de location"). ' '  . $code, "col", "mod", "xxx", "cdg", $code, "", "", FALSE);
        }
        $msg = _("Contrat"). ' '. $code . ' ' ._("modifié");
        break;

    // Suppression de la fiche Contrat location
    case "del" :
        if (datetolocal($dateclos) == "") {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

            // On supprime le contrat
            $rech = "DELETE FROM `" . TBCOLOC . "` WHERE `coloc_code` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // On remet les produits en "louables" et on supprime les produits rattachés
            $rech = "SELECT pdtl_id,pdtl_codepdtloc FROM `" . TBPDTL . "` WHERE pdtl_codecoloc = '" . $db->escape($code) . "'";
            $rows = $db->get_results($rech, ARRAY_A);

            if (($rows) AND (count($rows) > 0)) {
                foreach ($rows as $row) {
                    extract($row);

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

                    // On supprime le produit dans la location
                    $rech2 = "DELETE FROM `" . TBPDTL . "` WHERE `pdtl_id` = '$pdtl_id'";
                    $db->query($rech2);

                    // On supprime le barème qui était rattaché à ce produit dans la location
                    $rech2 = "DELETE FROM `" . TBPDTLBL . "` WHERE `pdtlbl_idpdtl` = '$pdtl_id'";
                    $db->query($rech2);

                    // On supprime la pénalité qui était rattachée à ce produit dans la location
                    $rech2 = "DELETE FROM `" . TBPDTLPL . "` WHERE `pdtlpl_idpdtl` = '$pdtl_id'";
                    $db->query($rech2);

                    // Suppression des tâches rattachées à ce contrat
                    $rech2 = "DELETE FROM `" . TBTAC . "` WHERE `tac_coderat` = '" . $db->escape($code) . "'";
                    $db->query($rech2);
                }
            }

            /* vérification du type de client et MAJ si nécessaire */
            if ($tc == 0)
                upd_nivcli($codecli, FALSE);

            $db->disconnect();

            $msg = _("Contrat"). ' ' . $code . ' ' ._("supprimé");
        } else {
            $msg = _("Impossible de supprimer un contrat clôt");
            $retourliste = FALSE;
            $imprim = FALSE;
        }
}
?>
<!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 _("Fiche Contrat Location"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            <?PHP
            if ($retourliste == FALSE)
                echo "warn_txt('" . str_escape($msg) . "');";
            if ($pcli == TRUE)
                echo "fen_prev_cli(" . $prev . ",'');";
            ?>
            init_new_fen('imprim','impression','click');
            Focus_first();
        }

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

</head>
<body>

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

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

<p align="center">&nbsp;</p>
<?PHP
if ($retourliste == FALSE)
    aff_ret_idx_man($td, "coloc_lst.php?ord=DES&tri=Code", _("Retour à la liste"), TRUE);
else {

    if ($td == "add")
        pop_ret_auto($msg ,'ok',"coloc_maj.php?td=edit&tc=" . $tc . "&co=" . $codeloc . "&cc=" . $codecli . "&ca=" . $codeage);
    else
        pop_ret_auto($msg ,'ok',"coloc_lst.php?ord=DES&tri=Code");
}
if ($imprim == TRUE) {
    if ($td == "add")
        $urlimp = "coloc_pdf.php?cl=" . $codeloc;
    else
        $urlimp = "coloc_pdf.php?cl=" . $code;

    if (url_ok($urlimp) == TRUE) {
        echo '<div id="imprim" align="center" title="' . $urlimp . '" class="falselnk">';
        echo '<img src="../img/print.png" title="' . _("Imprimer") . '" border="0" align="absmiddle"> ' . _("Imprimer");
        echo '</div>';
    }
}
include_once("pied.php");
?>
</body>
</html>