<?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_dos.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_time.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"];
$codedos = @$_POST["codedos"];
$iddosd = intval(@$_POST["nd"]);

$typea = str_majuscules(@$_POST["typea"]);
$clos = 0;
$clore = @$_POST["clore"];
if ($clore == "clos")
    $clos = 1;
$dateact = datetomysql(@$_POST["dateactf"]);
$heured = @$_POST["heured"];
if ($heured <> "")
    $heured .= ":00";
$heuref = @$_POST["heuref"];
if ($heuref <> "")
    $heuref .= ":00";
$acodemt = @$_POST["a_mt"];
$aheured = @$_POST["a_heured"];
$aheured_full = $aheured . ":00";
$aheuref = @$_POST["a_heuref"];
$aheuref_full = $aheuref . ":00";
$akm = @$_POST["a_km"];
$acd = @$_POST["a_cd"];
$afa = @$_POST["a_fa"];
$afad = @$_POST["a_fad"];
$rcodemt = @$_POST["r_mt"];
$rheured = @$_POST["r_heured"];
$rheured_full = $rheured . ":00";
$rheuref = @$_POST["r_heuref"];
$rheuref_full = $rheuref . ":00";
$rkm = @$_POST["r_km"];
$rcd = @$_POST["r_cd"];
$rfa = @$_POST["r_fa"];
$rfad = @$_POST["r_fad"];
$obj = @$_POST["obj"];
$res = @$_POST["res"];
$concl = @$_POST["concl"];
$age = $_POST["codeage"];
$tab_cage = $_POST["cage"];
$tab_codetec = $_POST["codetec"];
$cli = $_POST["codecli"];
$tab_ccli = $_POST["ccli"];
$mque = $_POST["mque"];
$tab_cmque = $_POST["cmque"];
$do = $_POST["do"];
$tab_cdo = $_POST["cdo"];
$st = $_POST["st"];
$tab_cst = $_POST["cst"];
$codestatut = str_majuscules(@$_POST["codestatut"]);
$urg = @$_POST["urgence"];
$proba = @$_POST["proba"];
$ave = @$_POST["ave"];

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

$retourliste = TRUE;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche détail
    case "add" :
        $rech = "SELECT COUNT(*) FROM `" . TBDOSD . "` WHERE dosd_codedos = '$codedos' AND dosd_id = '$iddosd'";
        $nbres = $db->get_var($rech);
        if ($nres == 0) {
            $rech2 = "INSERT INTO `" . TBDOSD . "`";
            $rech2 .= " (dosd_codedos,dosd_codeta,dosd_dateact,dosd_heured,dosd_heuref,dosd_obj,dosd_res,dosd_concl,dosd_clos,";
            $rech2 .= "dosd_acodemt,dosd_aheured,dosd_aheuref,dosd_akm,dosd_acd,dosd_afrais,dosd_afraisd,";
            $rech2 .= "dosd_rcodemt,dosd_rheured,dosd_rheuref,dosd_rkm,dosd_rcd,dosd_rfrais,dosd_rfraisd) VALUES";
            $rech2 .= " ('$codedos','$typea','$dateact','$heured','$heuref','$obj','$res','$concl','$clos',";
            $rech2 .= " '$acodemt','$aheured_full','$aheuref_full','$akm','$acd','$afa','$afad',";
            $rech2 .= " '$rcodemt','$rheured_full','$rheuref_full','$rkm','$rcd','$rfa','$rfad')";
            $db->query($rech2);

            $iddosd = $db->insert_id;

            // MAJ dossier
            if (($urg <> "") OR ($codestatut <> "") OR ($proba <> "") OR ($ave <> "")) {
                $rech = "UPDATE `" . TBDOS . "` SET ";
                $rech .= "`dos_codemod`='" . $codeuser . "'";
                if ($urg <> "")
                    $rech .= ",`dos_urg`='" . $urg . "'";
                if ($codestatut <> "")
                    $rech .= ",`dos_codedoss`='" . $codestatut . "'";
                if ($proba <> "")
                    $rech .= ",`dos_proba`='" . $proba . "'";
                if ($ave <> "")
                    $rech .= ",`dos_avance`='" . $ave . "'";
                $rech .= " WHERE `dos_code` = '" . $codedos . "'";
                $db->query($rech);
            }

            //...MAJ des intervenants agences
            if (count($tab_cage) > 0) {
                foreach ($tab_cage as $value) {
                    if ($value <> "") {
                        $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                        $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                        $rech3 .= " ('$iddosd','0','$value','$age')";
                        $db->query($rech3);
                    }
                }
            }

            //...MAJ des intervenants techniciens
            if (count($tab_codetec) > 0) {
                foreach ($tab_codetec as $value) {
                    // Récupération code technicien
                    if ($value <> "") {
                        $tec_code = "";
                        $rech2 = "SELECT tec_code FROM `" . TBTEC . "` WHERE `tec_id`='" . $value . "'";
                        $row2 = $db->get_row($rech2, ARRAY_A);
                        if ($row2)
                            extract($row2);
                        $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                        $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                        $rech3 .= " ('$iddosd','1','$value','$tec_code')";
                        $db->query($rech3);
                    }
                }
            }

            //...MAJ des intervenants clients
            if (count($tab_ccli) > 0) {
                foreach ($tab_ccli as $value) {
                    if ($value <> "") {
                        $typinter = 2;
                        $id = $value;
                        if (substr($value, 0, 2) == "S_") // C'est un site
                        {
                            $typinter = 6;
                            $id = substr($value, 2, strlen($value));
                        }
                        $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                        $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                        $rech3 .= " ('$iddosd','$typinter','$id','$cli')";
                        $db->query($rech3);
                    }
                }
            }

            //...MAJ des intervenants marques
            if (count($tab_cmque) > 0) {
                foreach ($tab_cmque as $value) {
                    if ($value <> "") {
                        $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                        $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                        $rech3 .= " ('$iddosd','3','$value','$mque')";
                        $db->query($rech3);
                    }
                }
            }

            //...MAJ des intervenants donneurs d'ordres
            if (count($tab_cdo) > 0) {
                foreach ($tab_cdo as $value) {
                    if ($value <> "") {
                        $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                        $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                        $rech3 .= " ('$iddosd','4','$value','$do')";
                        $db->query($rech3);
                    }
                }
            }

            //...MAJ des intervenants sous-traitants
            if (count($tab_cst) > 0) {
                foreach ($tab_cst as $value) {
                    if ($value <> "") {
                        $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                        $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                        $rech3 .= " ('$iddosd','5','$value','$st')";
                        $db->query($rech3);
                    }
                }
            }

            $msg = _("Fiche ajoutée");
        } else {
            $msg = _("Ce code existe déjà");
            $retourliste = FALSE;
        }
        break;

    // MAJ de la fiche détail
    case "edit" :
        $rech = "UPDATE `" . TBDOSD . "` SET `dosd_codedos`='" . $codedos . "', `dosd_codeta`='" . $typea . "',";
        $rech .= " `dosd_dateact`='" . $dateact . "', `dosd_heured`='" . $heured . "', `dosd_heuref`='" . $heuref . "',";
        $rech .= " `dosd_obj`='" . $obj . "',`dosd_res`='" . $res . "',`dosd_concl`='" . $concl . "',`dosd_clos`='" . $clos . "',";
        $rech .= " `dosd_acodemt`='" . $acodemt . "',`dosd_aheured`='" . $aheured_full . "',`dosd_aheuref`='" . $aheuref_full . "',";
        $rech .= " `dosd_akm`='" . $akm . "',`dosd_acd`='" . $acd . "',`dosd_afrais`='" . $afa . "', `dosd_afraisd`='" . $afad . "',";
        $rech .= " `dosd_rcodemt`='" . $rcodemt . "',`dosd_rheured`='" . $rheured_full . "',`dosd_rheuref`='" . $rheuref_full . "',";
        $rech .= " `dosd_rkm`='" . $rkm . "',`dosd_rcd`='" . $rcd . "',`dosd_rfrais`='" . $rfa . "', `dosd_rfraisd`='" . $rfad . "'";
        $rech .= " WHERE `dosd_codedos` = '" . $codedos . "' AND `dosd_id` = '" . $iddosd . "'";
        $db->query($rech);

        // MAJ dossier
        if (($urg <> "") OR ($codestatut <> "") OR ($proba <> "") OR ($ave <> "")) {
            $rech = "UPDATE `" . TBDOS . "` SET ";
            $rech .= "`dos_codemod`='" . $codeuser . "'";
            if ($urg <> "")
                $rech .= ",`dos_urg`='" . $urg . "'";
            if ($codestatut <> "")
                $rech .= ",`dos_codedoss`='" . $codestatut . "'";
            if ($proba <> "")
                $rech .= ",`dos_proba`='" . $proba . "'";
            if ($ave <> "")
                $rech .= ",`dos_avance`='" . $ave . "'";
            $rech .= " WHERE `dos_code` = '" . $codedos . "'";
            $db->query($rech);
        }

        // MAJ des intervenants
        //... On supprime d'abord tous les intervenants
        $rech = "DELETE FROM `" . TBDOSDI . "` WHERE `dosdi_iddosd` = '$iddosd'";
        $db->query($rech);

        //...MAJ des intervenants agences
        if (count($tab_cage) > 0) {
            foreach ($tab_cage as $value) {
                if ($value <> "") {
                    $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                    $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                    $rech3 .= " ('$iddosd','0','$value','$age')";
                    $db->query($rech3);
                }
            }
        }

        //...MAJ des intervenants internes (techniciens)
        if (count($tab_codetec) > 0) {
            foreach ($tab_codetec as $value) {
                // Récupération code technicien
                if ($value <> "") {
                    $tec_code = "";
                    $rech2 = "SELECT tec_code FROM `" . TBTEC . "` WHERE `tec_id`='" . $value . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2)
                        extract($row2);
                    $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                    $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                    $rech3 .= " ('$iddosd','1','$value','$tec_code')";
                    $db->query($rech3);
                }
            }
        }

        //...MAJ des intervenants clients
        if (count($tab_ccli) > 0) {
            foreach ($tab_ccli as $value) {
                if ($value <> "") {
                    $typinter = 2;
                    $id = $value;
                    if (substr($value, 0, 2) == "S_") // C'est un site
                    {
                        $typinter = 6;
                        $id = substr($value, 2, strlen($value));
                    }
                    $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                    $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                    $rech3 .= " ('$iddosd','$typinter','$id','$cli')";
                    $db->query($rech3);
                }
            }
        }

        //...MAJ des intervenants marques
        if (count($tab_cmque) > 0) {
            foreach ($tab_cmque as $value) {
                if ($value <> "") {
                    $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                    $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                    $rech3 .= " ('$iddosd','3','$value','$mque')";
                    $db->query($rech3);
                }
            }
        }

        //...MAJ des intervenants donneurs d'ordres
        if (count($tab_cdo) > 0) {
            foreach ($tab_cdo as $value) {
                if ($value <> "") {
                    $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                    $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                    $rech3 .= " ('$iddosd','4','$value','$do')";
                    $db->query($rech3);
                }
            }
        }

        //...MAJ des intervenants sous-traitants
        if (count($tab_cst) > 0) {
            foreach ($tab_cst as $value) {
                if ($value <> "") {
                    $rech3 = "INSERT INTO `" . TBDOSDI . "`";
                    $rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
                    $rech3 .= " ('$iddosd','5','$value','$st')";
                    $db->query($rech3);
                }
            }
        }

        $msg = _("Fiche modifiée");
        break;

    // Suppression de la fiche détail
    case "del" :
        if ($clos == 0) {
            $rech = "DELETE FROM `" . TBDOSD . "` WHERE `dosd_codedos` = '$codedos' AND `dosd_id` = '$iddosd'";
            $db->query($rech);

            // Suppression des intervenants
            $rech = "DELETE FROM `" . TBDOSDI . "` WHERE `dosdi_iddosd` = '$iddosd'";
            $db->query($rech);

            $msg = _("Fiche supprimée");
        } else {
            $msg = _("Suppression impossible");
            $retourliste = FALSE;
        }
        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 _("Fiche type action"); ?></title>

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

    <script type="text/javascript">
        <!--
        <?PHP
        if ($clos == 1)
            cre_html_detindos('view',$codedos,"window.opener.");
        else
            cre_html_detindos('edit',$codedos,"window.opener.");
        ?>

        function init_all() {
            init_Body();
            <?PHP
            if ($proba<>"")
            {
                echo "window.opener.$('#proba').val('".$proba."');";
                echo "img_checkpour('proba','nivproba',1,true);";
            }

            if ($ave<>"")
            {
                echo "window.opener.$('#ave').val('".$ave."');";
                echo "img_checkpour('ave','nivave',2,true);";
            }

            if ($urg<>"")
                echo "window.opener.$('#urgence').val('".$urg."');";

            if ($codestatut<>"")
                echo "window.opener.$('#codestatut').val('".$codestatut."');";

            if (exist_dosd_open_dos($codedos,TRUE)==TRUE)
                echo "window.opener.$('#chxdetdosd').hide();";
            else
                echo "window.opener.$('#chxdetdosd').show();";
            ?>
            window.opener.$('#pxr').html('<?PHP echo full_num_format(px_revient_dos($codedos,TRUE)); ?>');
            detindos();
            <?PHP
            if ($retourliste == FALSE)
                echo "warn_txt('" . str_escape($msg) . "');";
            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>
