<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.9

Copyright (C) 2006-2009  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://smitst.homelinux.com
****************************************************************************************/

// 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_dos.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("../inc/mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$titre = stripslashes(strip_tags(trim(@$_POST["titre"])));
$td = strip_tags(trim(@$_POST["td"]));

$datedde = datefrang(strip_tags(trim(@$_POST["dateddef"])));
$datefinp = datefrang(strip_tags(trim(@$_POST["datefinpf"])));
$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$codecli = str_majuscules(strip_tags(trim(@$_POST["codecli"])));
$codeage = str_majuscules(strip_tags(trim(@$_POST["codeage"])));
$codecat = str_majuscules(strip_tags(trim(@$_POST["codecat"])));
$codestatut = str_majuscules(strip_tags(trim(@$_POST["codestatut"])));
$urg = strip_tags(trim(@$_POST["urgence"]));
$proba = strip_tags(trim(@$_POST["proba"]));
$ave = strip_tags(trim(@$_POST["ave"]));
$titred = strip_tags(trim(@$_POST["titred"]));
$resum = str_escape(trim(@$_POST["resum"]));
$codesuiv = str_majuscules(strip_tags(trim(@$_POST["codesuiv"])));
$datefin = datefrang(strip_tags(trim(@$_POST["datefinf"])));

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

$msg="";
$retourliste = TRUE;

switch ($td)
{
	// Ajout de la fiche Dossier
	case "add" :
		$coded = crecode("DS",TRUE);
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM " .TBDOS. " WHERE `dos_code` = '$coded'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rech2 = "INSERT INTO " .TBDOS;
			$rech2.=" (dos_code, dos_codecli, dos_titre, dos_res, dos_datecrea,dos_codecrea,dos_datemod,dos_codemod,";
			$rech2.="dos_datedde,dos_datefinp,dos_codeage,dos_urg,dos_codesuiv,dos_codedosc,dos_codedoss,dos_proba,";
			$rech2.="dos_avance,dos_datefin)";
			$rech2.=" VALUES";
			$rech2.=" ('$coded','$codecli','$titred','$resum','$datea','$codeuser','$datea','$codeuser','$datedde','$datefinp',";
			$rech2.="'$codeage','$urg','$codesuiv','$codecat','$codestatut','$proba','0','')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
	
			// Vérification du type de client et MAJ si nécessaire
			$rech2 = "SELECT cli_type FROM " .TBCLI. " WHERE `cli_code` = '$codecli'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row = mysql_fetch_array($rs2))
			{
				extract ($row);
				
				// MAJ de la fiche Client (pour modification du type), et passage en type 'demande d'intervention' si pas déjà client
				if ($cli_type < 1)
				{
					$rech2 = "UPDATE `" .TBCLI. "` SET `cli_datemod`='".$datea."', `cli_codemod`='".$codeuser."', `cli_prop`='".$codeage."', `cli_type`='1'  WHERE `cli_code` = '".$codecli."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
				}
			}
			
			$msg = $txt_fiche_add;
	
		}
		else
		{
			$msg = $txt_fiche_addnotok;
			$retourliste = FALSE;
		}
	
		mysql_free_result($rs);
		mysql_close($conn);
	
		if ($retourliste == TRUE)
		{
			// Envoi d'email vers le webmaster
			envoi_mailwm("","Mise en place d'un nouveau dossier de suivi (".$coded.")","dos","cre","xxx","age",$coded,"","");
			// Envoi d'email vers l'agence (Si pas créé par une agence)
			if (@$_SESSION["niv_log"] <> 4)
				envoi_mail("","Nouveau dossier de suivi","dos","cre","xxx","age",$coded,"","",FALSE);
			// Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
			if (@$_SESSION["niv_log"] <> 5)
				envoi_mail("","Nouveau dossier de suivi","dos","cre","xxx","cdg",$coded,"","",FALSE);
		}

		break;
		
	// MAJ de la fiche Dossier
	case "edit" :
		$majok=TRUE;
		// Contrôle si date de cloture
		if (formatedate($datefin) <> "")
		{
			if (exist_dosd_open_dos($code,TRUE)==TRUE)
			{
				$majok=FALSE;
				$retourliste=FALSE;
				$msg=$txt_dosd_opened;
			}
		}
		
		if ($majok==TRUE)
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			$rech = "UPDATE `" .TBDOS. "` SET `dos_titre`='".$titred."', `dos_res`='".$resum."',`dos_datemod`='".$datea."',";
			$rech.= "`dos_codemod`='".$codeuser."',`dos_urg`='".$urg."',`dos_codesuiv`='".$codesuiv."',`dos_codedosc`='".$codecat."',";
			$rech.="`dos_codedoss`='".$codestatut."',`dos_proba`='".$proba."',`dos_avance`='".$ave."',`dos_datefin`='".$datefin."'";
			$rech .=" WHERE `dos_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
					
			// Remise à niveau type client
			upd_nivcli($codecli,FALSE);
				
			mysql_close($conn);
	
			$msg = $txt_fiche_mod;

			// On prévient le Webmaster
			envoi_mailwm("","Dossier de suivi n°".$code." mis à jour","dos","mod","xxx","age",$code,"","");
			// Envoi d'email vers l'agence
			if (@$_SESSION["niv_log"] <> 4)
				envoi_mail("","Dossier de suivi n°".$code." mis à jour","dos","mod","xxx","age",$code,"","",FALSE);
			// Envoi d'email vers le centre de gestion
			if (@$_SESSION["niv_log"] <> 5)
				envoi_mail("","Dossier de suivi n°".$code." mis à jour","dos","mod","xxx","cdg",$code,"","",FALSE);
		}
		break;		


	// Suppression de la fiche Dossier
	case "del" :
		if ($codestatut <> "CLOT")
		{
			// Envoi d'email vers l'agence
			if (@$_SESSION["niv_log"] <> 4)
				envoi_mail("","Annulation de dossier de suivi","dos","del","xxx","age",$code,"","",FALSE);
			// Envoi d'email vers le centre de geston
			if (@$_SESSION["niv_log"] <> 5)
				envoi_mail("","Annulation de dossier de suivi","dos","del","xxx","cdg",$code,"","",FALSE);

			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "DELETE FROM `" .TBDOS. "` WHERE `dos_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
		
			// Remise à niveau type client
			upd_nivcli($codecli,FALSE);

			// Suppression des intervenants pour chaque détails du dossier
			$rech="SELECT dosd_id FROM ".TBDOSD." WHERE `dosd_codedos`='".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs)>0)
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract($row);

					$rech = "DELETE FROM `" .TBDOSDI. "` WHERE `dosdi_iddosd` = '$dosd_id'";
					$rs = mysql_query($rech) or die(mysql_error());
				}
			}
					
			// Suppression des détails du dossier
			$rech = "DELETE FROM `" .TBDOSD. "` WHERE `dosd_codedos` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());

			// Suppression des tâches rattachées à ce contrat
			$rech = "DELETE FROM `" .TBTAC. "` WHERE `tac_coderat` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());

			mysql_close($conn);
	
			$msg = $txt_fiche_del;
		}
		else
		{
			$msg = $txt_fiche_delnotok;
			$retourliste = 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=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : Fiche Dossier</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->

<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></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>
<p align="center">
<?PHP if ($retourliste==FALSE)
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
?>
<?PHP echo $msg ?>
</span>
</p>

<br>
<br>
<?PHP if ($retourliste == FALSE)
	aff_ret_idx_man($td,"dos_lst.php?ord=ASC&tri=Fin",$txt_lnk_ret_liste,TRUE);
else 
	aff_ret_lst_auto("dos_lst.php?ord=ASC&tri=Fin",2000);
include_once("pied.php");
?>
