<?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_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_do.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
$rlp_choisis = @$_SESSION["rlp_choisis"];

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

$datecrea = datefrang(strip_tags(trim(@$_POST["datecreaf"])));
$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$codecli = str_majuscules(strip_tags(trim(@$_POST["codecli"])));
$prev = strip_tags(trim(@$_POST["prev"]));
if (strtolower(strip_tags(trim(@$_POST["sitep"]))) == "sitep")
	$sitep=1;
else
{
	// On doit au moins choisir un site...
	if ($_SESSION["scli_choisis"] <> "")
		$sitep=0;
	else
		$sitep=1;
}
$codeage = str_majuscules(strip_tags(trim(@$_POST["codeage"])));
$co = str_majuscules(strip_tags(trim(@$_POST["co"])));
$cope = str_majuscules(strip_tags(trim(@$_POST["cope"])));

$row = lit_enr_per($cope);
if (is_array($row)==TRUE)
	extract ($row);
else
	$per_nbj=0;
$delai = strip_tags(trim(@$_POST["delai"]));
$nbh = strip_tags(trim(@$_POST["nbh"])).":00";
$nbs = heure_sec($nbh);
$indexnbsnc = strip_tags(trim(@$_POST["nbsnc"]));
if ($indexnbsnc == "0")
	$nbsnc = 0;
else
	$nbsnc = 1;
$indexnbsdep = strip_tags(trim(@$_POST["nbsdep"]));
if ($indexnbsdep == "0")
	$nbsdep = 0;
else
	$nbsdep = 1;
$indextyph = strip_tags(trim(@$_POST["typh"]));
if ($indextyph == "0")
	$typh = 0;
else
	$typh = 1;

$datedf = strip_tags(trim(@$_POST["datedf"]));
if ($datedf <> "")
	$dated = datefrang($datedf);
else
	$dated = $datedf;
$dateff = strip_tags(trim(@$_POST["dateff"]));
if ($dateff <> "")
	$datef = datefrang($dateff);
else
	$datef = $dateff;
$indexrenauto = strip_tags(trim(@$_POST["renauto"]));
if ($indexrenauto == "0")
	$renauto = 0;
else
	$renauto = 1;

$preavis = strip_tags(trim(@$_POST["preavis"]));

// On ne peut pas avoir un préavis inférieur au maximum ("dernier délai") choisi
if ($preavis < preavis_max())
	$preavis = preavis_max();			

// On ne peut pas avoir un préavis supérieur au maximum ("premier délai autorisé") choisi
if ($preavis > preavis_min())
	$preavis = preavis_min();			

$matos = strip_tags(str_escape(trim(@$_POST["matos"])));

$datesigf = strip_tags(trim(@$_POST["datesig"]));
if ($datesigf <> "")
	$datesig = datefrang($datesigf);
else
	$datesig = "";
$signe = strip_tags(str_escape(trim(@$_POST["signe"])));

$datefinf = strip_tags(trim(@$_POST["datefinf"]));
if ($datefinf <> "")
	$datefin = datefrang($datefinf);
else
	$datefin = "";

if ($td == "add")
{
	$renouv =0;
	$rnum = "";
}
else
{
	$renouv = strip_tags(trim(@$_POST["renouv"]));
	$rnum = strip_tags(trim(@$_POST["num"]));
}

$tva = strip_tags(trim(@$_POST["tva"]));
$pu = strip_tags(trim(@$_POST["pu"]));

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

$enr_ok = TRUE;
$pcli=FALSE;
$retourliste = TRUE;
$enrimp = FALSE;

if (strip_tags(trim(@$_POST["enrimp"])) == "Enregistrer & Imprimer")
{
	$enrimp = TRUE;
	$retourliste = FALSE;
}

switch ($td)
{
	// Ajout de la fiche Contrat Client
	case "add" :
		if (($per_nbj == 0) AND ($delai == ""))
		{
			$enr_ok = FALSE;
			$msg = "Il faut imp&eacute;rativement un d&eacute;lai d'intervention pour cette p&eacute;riodicit&eacute;.";
			$retourliste = FALSE;
			$enrimp = FALSE;
		}
		else			
		{
			$codeco = crecode("CO",TRUE);
	
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "INSERT INTO " .TBCOCL;
			$rech.= " (cocl_datecrea,cocl_codecrea,cocl_datemod,cocl_codemod,cocl_dateenv,cocl_daterec,cocl_code,cocl_codeco,cocl_codeper,";
			$rech.= "cocl_codeage,cocl_codecli,cocl_delai,cocl_nbs,cocl_typh,cocl_dated,cocl_datef,cocl_preavis,cocl_daterel,cocl_nivrel,";
			$rech.= "cocl_renouv,cocl_rnum,cocl_datefin,cocl_signe,cocl_matos,cocl_pvht,cocl_coderlp,cocl_sitep,cocl_codetva,";
			$rech.= "cocl_nbsnc,cocl_renauto,cocl_nbsdep)";
			$rech.= " VALUES";
			$rech.= " ('$datecrea','$codeuser','$datea','$codeuser','$datea','','$codeco','$co','$cope',";
			$rech.= "'$codeage','$codecli','$delai','$nbs','$typh','$dated','$datef','$preavis','','',";
			$rech.= "'$renouv','$rnum','$datefin','','$matos','$pu','$rlp_choisis','$sitep','$tva',";
			$rech.= "'$nbsnc','$renauto','$nbsdep')";
			$rs = mysql_query($rech) or die(mysql_error());
	
			/* Vérification du type de client et MAJ si nécessaire */
			
			// MAJ de la fiche Client (pour modification du type), et passage en type 'déjà client'
			$rech2 = "UPDATE `" .TBCLI. "` SET `cli_datemod`='".$datea."', `cli_codemod`='".$codeuser."', `cli_prop`='".$codeage."', `cli_type`='2'  WHERE `cli_code` = '".$codecli."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
	
			// Balayage et activation des sites sélectionnés
			if (trim($_SESSION["scli_choisis"]) <> "")
			{
				$tab_sites = explode("-",$_SESSION["scli_choisis"]);
				foreach ($tab_sites as $id_site)
				{
					if (($id_site <> "") AND ($id_site <> 0))
					{
						$rech = "INSERT INTO ". TBSCLIC;
						$rech.= " (sclic_datecrea,sclic_datemod,sclic_idscli,sclic_codecocl)";
						$rech.= " VALUES";
						$rech.= " ('$datecrea','$datea','$id_site','$codeco')";
						$rs = mysql_query($rech) or die(mysql_error());
					}
				}
			}
			mysql_close($conn);
	
			$msg = "Contrat ".$codeco." cr&eacute;&eacute;.";
	
			if ($retourliste == TRUE)
			{
				// Envoi d'email vers le client
				if ($prev==0)
					envoi_mail("","Création de votre contrat ".$codeco,"con","cre","xxx","cli",$codeco,"","",FALSE);
				else
				{
					if ($prev<>4) // Le niveau 4 est "Ne pas prévenir"
					{
						$pcli=TRUE;
						$txtcorps=cre_corps_txtc("con","cre","xxx","cli",$codeco,"","");
						$_SESSION["txt"] = $txtcorps;
					}
					// Envoi d'email vers le webmaster
					envoi_mailwm("","Création d'un contrat","con","cre","xxx","cli",$codeco,"","");
				}				
				// Envoi d'email vers l'agence
				if (@$_SESSION["niv_log"] <> 4)
					envoi_mail("","Création de contrat","con","cre","xxx","age",$codeco,"","",FALSE);
				// Envoi d'email vers le centre de gestion
				envoi_mail("","Création de contrat","con","cre","xxx","cdg",$codeco,"","",TRUE);
			}
		}
		break;
		
	// MAJ de la fiche Contrat Client
	case "edit" :
		if (($per_nbj == 0) AND ($delai == ""))
		{
			$msg = "Il faut imp&eacute;rativement un d&eacute;lai d'intervention pour cette p&eacute;riodicit&eacute;.";
			$enr_ok = FALSE;
			$retourliste = FALSE;
			$enrimp = FALSE;
		}	
		else
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "UPDATE `" .TBCOCL;
			$rech .= "` SET `cocl_datemod`='".$datea."', `cocl_codemod`='".$codeuser."', `cocl_codeper`='".$cope."', `cocl_codecli`='".$codecli."', `cocl_delai`='".$delai."', `cocl_nbs`='".$nbs."', `cocl_typh`='".$typh."',";
			$rech .=" `cocl_dated`='".$dated."', `cocl_datef`='".$datef."', `cocl_preavis`='".$preavis."', `cocl_renouv`='".$renouv."', `cocl_rnum`='".$rnum."', `cocl_datefin`='".$datefin."',";
			$rech .=" `cocl_signe`='".$signe."', `cocl_matos`='".$matos."', `cocl_pvht`='".$pu."', `cocl_coderlp`='".$rlp_choisis."', `cocl_sitep`='".$sitep."', `cocl_codetva`='".$tva."',";
			$rech .=" `cocl_nbsnc`='".$nbsnc."', `cocl_renauto`='".$renauto."', `cocl_nbsdep`='".$nbsdep."'";
			$rech .=" WHERE `cocl_code` = '".$code."'";

			$rs = mysql_query($rech) or die(mysql_error());
	
			/* Vérification du type de client et MAJ si nécessaire */
			
			// MAJ de la fiche Client (pour modification du type), et passage en type 'déjà client'
			$rech2 = "UPDATE `" .TBCLI. "` SET `cli_datemod`='".$datea."', `cli_codemod`='".$codeuser."', `cli_type`='2'  WHERE `cli_code` = '".$codecli."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
	
	
			// Mise à jour des sites sélectionnés
	
			// On vide d'abord tout
			$rech = "DELETE FROM `" .TBSCLIC. "` WHERE `sclic_codecocl`= '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			// On reconstruit si des sites ont été choisis
			if (trim($_SESSION["scli_choisis"]) <> "")
			{
				$tab_sites = explode("-",$_SESSION["scli_choisis"]);
				foreach ($tab_sites as $id_site)
				{
					if (($id_site <> "") AND ($id_site <> 0))
					{
						$rech = "INSERT INTO ". TBSCLIC;
						$rech.= " (sclic_datecrea,sclic_datemod,sclic_idscli,sclic_codecocl)";
						$rech.= " VALUES";
						$rech.= " ('$datecrea','$datea','$id_site','$code')";
						$rs = mysql_query($rech) or die(mysql_error());
					}
				}
			}
	
			mysql_close($conn);
	
			if ($retourliste == TRUE)
			{
				if ($datefin <> "") // Si date de clôture, on prévient le client
				{
					// Envoi d'email vers le client
					if ($prev==0)
						envoi_mail("","Clôture de votre contrat ".$code,"con","clo","xxx","cli",$code,"","",FALSE);
					else
					{
						if ($prev<>4) // Le niveau 4 est "Ne pas prévenir"
						{
							$pcli=TRUE;
							$txtcorps=cre_corps_txtc("con","cli","xxx","cli",$code,"","");
							$_SESSION["txt"] = $txtcorps;
						}
						envoi_mailwm("","Clôture du contrat ".$code,"con","clo","xxx","cli",$code,"","");
					}				
					// Envoi d'email vers l'agence
					if (@$_SESSION["niv_log"] <> 4)
						envoi_mail("","Clôture du contrat ".$code,"con","clo","xxx","age",$code,"","",FALSE);
					// Envoi d'email vers le centre de gestion
					envoi_mail("","Clôture du contrat ".$code,"con","clo","xxx","cdg",$code,"","",TRUE);
				}
				else // Si pas de date de clôture, on le prévient simplement de la modification
				{	
					// Envoi d'email vers le client
					if ($prev==0)
						envoi_mail("","Modification de votre contrat ".$code,"con","mod","xxx","cli",$code,"","",FALSE);
					else
					{
						if ($prev<>4) // Le niveau 4 est "Ne pas prévenir"
						{
							$pcli=TRUE;
							$txtcorps=cre_corps_txtc("con","cli","xxx","cli",$code,"","");
							$_SESSION["txt"] = $txtcorps;
						}
						// Envoi d'email vers le webmaster
						envoi_mailwm("","Modification du contrat ".$code,"con","mod","xxx","cli",$code,"","");
					}				
					// Envoi d'email vers l'agence
					if (@$_SESSION["niv_log"] <> 4)
						envoi_mail("","Modification du contrat ".$code,"con","mod","xxx","age",$code,"","",FALSE);
					// Envoi d'email vers le centre de gestion
					envoi_mail("","Modification du contrat ".$code,"con","mod","xxx","cdg",$code,"","",FALSE);
				}
			}			
			$msg = "Contrat ".$code." modifi&eacute;.";
		}
		break;
		
	// Suppression de la fiche Contrat Client
	case "del" :
		if ($datefin == "")
		{
			if (nb_cocl_in_coclh($code) == 0)
			{		
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				
				// Contrôle que ce contrat n'est pas un renouvellement
				$rech = "SELECT `cocl_id` FROM `". TBCOCL. "` WHERE `cocl_renouv` = 1 AND `cocl_rnum` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());
				if (mysql_num_rows($rs)>0)
				{
					// Oui, on supprime alors l'info de renouvellement
					while ($row = mysql_fetch_assoc($rs)) 
					{         
						$rech2 = "UPDATE `" .TBCOCL;
						$rech2 .= "` SET `cocl_datemod`='".$datea."', `cocl_codemod`='".$codeuser."', ";
						$rech2 .=" `cocl_renouv`='0', `cocl_rnum`=''";
						$rech2 .=" WHERE `cocl_id` = '".$row["cocl_id"]."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
					}
				}
				mysql_free_result($rs);
	
				// On supprime le contrat
				$rech = "DELETE FROM `" .TBCOCL. "` WHERE `cocl_code` = '$code'";
				$rs = mysql_query($rech) or die(mysql_error());
	
				// On supprime les affectations de sites
				$rech = "DELETE FROM `" .TBSCLIC. "` WHERE `sclic_codecocl`= '".$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 = "Contrat ".$code." supprim&eacute;";
			}
			else
			{
				$msg = "Le contrat ".$code." a d&eacute;j&agrave; &eacute;t&eacute; utilis&eacute; dans une intervention.";
				$retourliste = FALSE;
				$enrimp = FALSE;
			}
		}
		else
		{
			$msg = "Impossible de supprimer un contrat cl&ocirc;t.";
			$retourliste = FALSE;
			$enrimp = 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=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : Fiche Contrat Client</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) AND ($enrimp==FALSE) AND ($enrmail==FALSE))
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
?>
<?PHP echo $msg ?>
</span>
</p>

<?PHP if ($retourliste == FALSE) 
{ 
	if ($enrimp == TRUE)
	{
		if ($td == "add")
			$urlimp = "cocl_pdf.php?code=".$codeco."&timp=0";
		else
			$urlimp = "cocl_pdf.php?code=".$code."&timp=0";
?>
	<p align="center">
    <a href="javascript:;" onClick="lanceimp('<?PHP echo $urlimp; ?>');")><img src="../img/print.png" title="<?PHP echo $alt_impcocl; ?>" border="0" align="absmiddle"></a> 
		<a href="javascript:;" onClick="lanceimp('<?PHP echo $urlimp; ?>');")> <?PHP echo $alt_impcocl; ?> </a></div>
	</p>
<?PHP
	}
	aff_ret_idx_man($td,"cocl_lst.php?ord=DES&tri=Code",$txt_lnk_ret_liste,TRUE);
} 
else 
{
	if ($pcli==TRUE)
	{
?>
		<script type="text/javascript">
		<!--
		fen_prev_client(<?PHP echo $prev; ?>,"");
		//-->
		</script>
<?PHP
	}

	aff_ret_lst_auto("cocl_lst.php?ord=DES&tri=Code",2000);
}
include_once("pied.php");
?>
