<?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/fic_age.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_ot.inc.php");
include_once("../inc/fic_statut.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);
$titre = @$_POST["titre"];
$tx = @$_POST["tx"];
$td = @$_POST["td"];
$ca = @$_POST["ca"];
$cas = @$_POST["cas"];
$cc = @$_POST["codecli"];
$si = intval(@$_POST["idsite"]);
$tr = @$_POST["tr"];

$datedde = datetomysql(@$_POST["dateddef"]);
$code = str_majuscules(@$_POST["code"]);
$codeint = str_majuscules(@$_POST["codeint"]);
$codest = str_majuscules(@$_POST["codest"]);
$ddeur = str_majuscules(@$_POST["ddeur"]);
$codestatut = str_majuscules(@$_POST["codestatut"]);
$codelieu = str_majuscules(@$_POST["codelieu"]);
$urg = @$_POST["urgence"];
$daterdvf = @$_POST["daterdvf"];
if ($daterdvf <> "")
    $daterdv = datetomysql($daterdvf);
else
    $daterdv = "";
$hrdv = @$_POST["hrdv"];
if ($hrdv <> "")
    $hrdv .= ":00";
$mat = @$_POST["mat"];
$pbm = @$_POST["pbm"];
$indexgar = @$_POST["gar"];
if ($indexgar == "0")
    $gar = 0;
else
    $gar = 1;

$fgar = @$_POST["fgar"];
if ($fgar == "") // Si pas de référence facture saisie, pas sous garantie
    $gar = 0;

$codetec = str_majuscules(@$_POST["codetec"]);
$duree = formateheure(@$_POST["duree"]);
$durees = heure_sec($duree);

$fait = @$_POST["fait"];
$dateprevf = @$_POST["dateprevf"];
if ($dateprevf <> "")
    $dateprev = datetomysql($dateprevf);
else
    $dateprev = "";

$datefinf = @$_POST["datefinf"];
if ($datefinf <> "") {
    $datefin = datetomysql($datefinf);
    if (trim($dateprev) == "") // Si pas de de date pour prévenir l'agence, on prend la date de clôture
        $dateprev = $datefin;
    $codestatut = "CLOT"; // S'il existe une date de fin, c'est que le dossier est clôt
} else
    $datefin = "";

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

$msg = "";
$retourliste = TRUE;
$imprim = FALSE;

switch ($_POST["nbutton"]) {
    case "enrimp" :
        $retourliste = FALSE;
        $imprim = TRUE;
        break;
    default :
        break;
}

switch ($td) {
    // Ajout de la fiche Ordre
    case "add" :
        $codeot = crecode("OR", TRUE);

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT COUNT(*) FROM `" . TBOT . "` WHERE `ot_code` = '" . $db->escape($codeot) . "'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            switch ($_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    $rech2 = "INSERT INTO `" . TBOT . "`"
                        . " (ot_datecrea,ot_codecrea,ot_datemod,ot_codemod,ot_datedde,ot_code,ot_codecli,ot_ccli,ot_idsite,ot_codeint,ot_codest,ot_codestatut,ot_codelieu,ot_urg,ot_daterdv,ot_hrdv,ot_mat,ot_pbm,ot_gar,ot_fgar)"
                        . " VALUES"
                        . " ('$datea','$codeuser','$datea','$codeuser','$datedde','" . $db->escape($codeot) . "','$cc','$ddeur','$si','$codeint','$codest','$codestatut','$codelieu','$urg','$daterdv','$hrdv','" . $db->escape($mat) . "','" . $db->escape($pbm) . "','$gar','$fgar')";
                    break;
                default :
                    $rech2 = "INSERT INTO `" . TBOT . "`"
                        . " (ot_datecrea,ot_codecrea,ot_datemod,ot_codemod,ot_datedde,ot_code,ot_codecli,ot_ccli,ot_idsite,ot_codeint,ot_codest,ot_codestatut,ot_codelieu,ot_urg,ot_daterdv,ot_hrdv,ot_mat,ot_pbm,ot_gar,ot_fgar,ot_codetec,ot_durees,ot_fait,ot_dateprev,ot_datefin)"
                        . " VALUES"
                        . " ('$datea','$codeuser','$datea','$codeuser','$datedde','" . $db->escape($codeot) . "','$cc','$ddeur','$si','$codeint','$codest','$codestatut','$codelieu','$urg','$daterdv','$hrdv','" . $db->escape($mat) . "','" . $db->escape($pbm) . "','$gar','$fgar','-1','$durees','" . $db->escape($fait) . "','$dateprev','$datefin')";
                    break;
            }
            $db->query($rech2);
            $msg = _("Bon enregistré");
        } else {
            $msg = _("Ce code existe déjà");
            $retourliste = FALSE;
            $imprim = FALSE;
        }
        $db->disconnect();

        if ($retourliste == TRUE) {
            // Envoi d'email vers le sous-traitant
            envoi_mail("", "Nouveau bon de travail (" . $codeot . ")", "ot", "cre", "xxx", "st", $codeot, "", "", TRUE);
            // Envoi d'email vers l'agence (Si pas créé par l'agence)
            if ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["niv_log"] <> 3))
                envoi_mail("", "Nouveau bon de travail (" . $codeot . ")", "ot", "cre", "xxx", "age", $codeot, "", "", FALSE);
            // Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
            if (@$_SESSION["niv_log"] <> 5)
                envoi_mail("", "Nouveau bon de travail (" . $codeot . ")", "ot", "cre", "xxx", "cdg", $codeot, "", "", FALSE);
        }
        break;

    // MAJ de la fiche Ordre
    case "edit" :

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        switch ($_SESSION["niv_log"]) {
            case 3 :
            case 4 :
                $rech = "UPDATE `" . TBOT . "` SET `ot_datemod`='" . $datea . "', `ot_codemod`='" . $codeuser . "', `ot_ccli`='" . $ddeur . "',";
                $rech .= " `ot_daterdv`='" . $daterdv . "',`ot_hrdv`='" . $hrdv . "', `ot_codest`='" . $codest . "', `ot_codestatut`='" . $codestatut . "',";
                $rech .= " `ot_codelieu`='" . $codelieu . "', `ot_urg`='" . $urg . "', `ot_mat`='" . $db->escape($mat) . "', `ot_pbm`='" . $db->escape($pbm) . "',";
                $rech .= " `ot_gar`='" . $gar . "', `ot_fgar`='" . $fgar . "'";
                $rech .= " WHERE `ot_code` = '" . $db->escape($code) . "'";
                break;
            case 10 :
            case 11 :
                $rech = "UPDATE `" . TBOT . "` SET `ot_datemod`='" . $datea . "', `ot_codemod`='" . $codeuser . "',`ot_daterdv`='" . $daterdv . "',";
                $rech .= " `ot_hrdv`='" . $hrdv . "', `ot_codestatut`='" . $codestatut . "',";
                $rech .= " `ot_codelieu`='" . $codelieu . "', `ot_urg`='" . $urg . "', `ot_mat`='" . $db->escape($mat) . "', `ot_pbm`='" . $db->escape($pbm) . "',";
                $rech .= " `ot_codetec`='" . $codetec . "',`ot_durees`='" . $durees . "', `ot_fait`='" . $db->escape($fait) . "', `ot_dateprev`='" . $dateprev . "',";
                $rech .= " `ot_datefin`='" . $datefin . "', `ot_gar`='" . $gar . "', `ot_fgar`='" . $fgar . "'";
                $rech .= " WHERE `ot_code` = '" . $db->escape($code) . "'";
                break;
            default :
                $rech = "UPDATE `" . TBOT . "` SET `ot_datemod`='" . $datea . "', `ot_codemod`='" . $codeuser . "',`ot_ccli`='" . $ddeur . "',";
                $rech .= " `ot_daterdv`='" . $daterdv . "', `ot_hrdv`='" . $hrdv . "', `ot_codestatut`='" . $codestatut . "',";
                $rech .= " `ot_codelieu`='" . $codelieu . "', `ot_urg`='" . $urg . "', `ot_mat`='" . $db->escape($mat) . "', `ot_pbm`='" . $db->escape($pbm) . "',";
                $rech .= " `ot_codetec`='" . $codetec . "',`ot_durees`='" . $durees . "', `ot_fait`='" . $db->escape($fait) . "', `ot_dateprev`='" . $dateprev . "',";
                $rech .= " `ot_datefin`='" . $datefin . "', `ot_gar`='" . $gar . "', `ot_fgar`='" . $fgar . "'";
                $rech .= " WHERE `ot_code` = '" . $db->escape($code) . "'";
                break;
        }
        $db->query($rech);

        $msg = _("Bon mis à jour");

        $db->disconnect();

        if ($codestatut <> "CLOT") {
            // Si l'intervention est terminée, on prévient l'agence et le centre de gestion
            if ($dateprevf <> "") {
                // On prévient l'agence (si pas saisi par l'agence)
                if ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["niv_log"] <> 3))
                    envoi_mail("", "Bon de travail " . $code . " exécuté", "ot", "fait", "xxx", "age", $code, "", "", TRUE);
                // Envoi d'email vers le centre de gestion (Si pas édité par le CDG)
                if (@$_SESSION["niv_log"] <> 5)
                    envoi_mail("", "Bon de travail " . $code . " exécuté", "ot", "fait", "xxx", "cdg", $code, "", "", FALSE);
            }
        } else {
            // Si l'intervention est terminée, on prévient l'agence et le centre de gestion
            if ($dateprevf <> "") {
                // On prévient l'agence (si pas saisi par l'agence)
                if ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["niv_log"] <> 3))
                    envoi_mail("", "Bon de travail " . $code . " clôt", "ot", "clot", "xxx", "age", $code, "", "", TRUE);
                // On prévient le centre de gestion (si pas saisi par lui)
                if (@$_SESSION["niv_log"] <> 5)
                    envoi_mail("", "Bon de travail " . $code . " clôt", "ot", "clot", "xxx", "cdg", $code, "", "", FALSE);
            }
        }
        break;


    // Suppression de la fiche Ordre
    case "del" :
        if ($codestatut <> "CLOT") {
            // Envoi d'email vers le sous-traitant
            envoi_mail("", "Annulation bon de travail " . $code, "ot", "del", "xxx", "st", $code, "", "", FALSE);
            // Envoi d'email vers le centre de geston
            if (@$_SESSION["niv_log"] <> 5)
                envoi_mail("", "Annulation bon de travail " . $code, "ot", "del", "xxx", "cdg", $code, "", "", FALSE);

            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "DELETE FROM `" . TBOT . "` WHERE `ot_code` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // Suppression du détail horaire
            $rech = "DELETE FROM `" . TBOTH . "` WHERE `oth_codeot` = '" . $db->escape($code) . "'";
            $db->query($rech);

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

            $db->disconnect();

            $msg = _("Bon supprimé");
        } else {
            $msg = _("Impossible de supprimer un bon clôt");
            $retourliste = FALSE;
            $imprim = FALSE;
        }
        break;

    default :
        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 _("Fiche Ordre Sous-traitant"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_new_fen('imprim','impression','click');
            Focus_first();
        }

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

</head>
<body>
<?php
if ($tx == "m")
    include_once("../inc/entete.inc.php");
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<p align="center">&nbsp;</p>
<?PHP
$urlret = "ot_lst.php?tx=" . $tx;
if ($tx == "i")
    $urlret .= "&td=edit&ca=" . $ca . "&cas=" . $cas . "&cc=" . $cc . "&ci=" . $codeint . "&si=" . $si . "&tr=" . $tr;
$urlret .= "&tl=2&ord=DES&tri=Code";


if ($retourliste == FALSE) {
    exec_func_js("warn_txt('" . str_escape($msg) . "')");
    if ($imprim == TRUE) {
        if ($td == "add")
            $urlimp = "ot_pdf.php?co=" . $codeot;
        else
            $urlimp = "ot_pdf.php?co=" . $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>';
        }

        /*
		if ($surplus <> 0)
		{
			echo "<br /><br />";
			echo _("Que souhaitez-vous faire ?");
		}
        */
    }

    if ($codestatut <> "CLOT")
        aff_ret_idx_man($td, $urlret, _("Retour à la liste"), TRUE);
    else
        aff_ret_idx_man($td, $urlret, _("Retour à la liste"), FALSE);

    /*
	<br />
	<br />
	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%" align="right"><a href="<?PHP echo $urlret; ?></a></td>
		<td width="7%">&nbsp;</td>
		<td width="43%">
		<?PHP if ($tx=="m") { ?>
		<a href="index.php"><em><img src="../img/index.png" border="0" align="absmiddle"> </em><span class="annot_gras_it">Revenir à l'accueil</span> </a>
		<?PHP } else { ?>
		&nbsp;
		<?PHP } ?>
		</td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
    */
} else
    pop_ret_auto($msg, 'ok', $urlret);
if ($tx == "m")
    include_once("pied.php");
?>
</body>
</html>