<?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_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("../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"]));
$tc = strip_tags(trim(@$_POST["tc"]));

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

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

$retourliste = TRUE;
$enr_ok = TRUE;

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


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

		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "INSERT INTO " .TBCOLOC;
		$rech.= " (coloc_datecrea,coloc_codecrea,coloc_code,coloc_codeage,coloc_typcli,coloc_codecli,coloc_idsite,coloc_ddeur,coloc_datedeb,coloc_datefin)";
		$rech.= " VALUES";
		$rech.= " ('$datecrea','$codeuser','$codeloc','$codeage','$tc','$codecli','$idsite','$ddeur','$datedeb','$datefin')";
		$rs = mysql_query($rech) or die(mysql_error());

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

		mysql_close($conn);

		$msg = "Contrat ".$codeloc." cr&eacute;&eacute;.";

		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" :
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "UPDATE `" .TBCOLOC;
		$rech .= "` SET `coloc_codeage`='".$codeage."',`coloc_codecli`='".$codecli."',`coloc_idsite`='".$idsite."',";
		$rech .=" `coloc_ddeur`='".$ddeur."', `coloc_datedeb`='".$datedeb."', `coloc_datefin`='".$datefin."'";
		$rech .=" WHERE `coloc_code` = '".$code."'";

		$rs = mysql_query($rech) or die(mysql_error());

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

		mysql_close($conn);

		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&eacute;.";
		break;
		
	// Suppression de la fiche Contrat location
	case "del" :
		if (formatedate($dateclos) == "")
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// On supprime le contrat
			$rech = "DELETE FROM `" .TBCOLOC. "` WHERE `coloc_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			// On remet les produits en "louables" et on supprime les produits rattachés
			$rech = "SELECT * FROM " .TBPDTL. " WHERE pdtl_codecoloc = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) > 0)
			{
				while ($row=mysql_fetch_assoc($rs))
				{
					extract($row);

					// On met à jour la fiche produit
					$rech2 = "UPDATE `" .TBPDTLOC. "` SET `pdtloc_louable`=1, `pdtloc_loue`=0";
					$rech2 .= " WHERE `pdtloc_code` = '".$pdtl_codepdtloc."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					
					// On supprime le produit dans la location
					$rech2 = "DELETE FROM `" .TBPDTL. "` WHERE `pdtl_id` = '$pdtl_id'";
					$rs2 = mysql_query($rech2) or die(mysql_error());

					// On supprime le barême qui était rattaché à ce produit dans la location
					$rech2 = "DELETE FROM `" .TBPDTLBL. "` WHERE `pdtlbl_idpdtl` = '$pdtl_id'";
					$rs2 = mysql_query($rech2) or die(mysql_error());

					// On supprime la pénalité qui était rattachée à ce produit dans la location
					$rech2 = "DELETE FROM `" .TBPDTLPL. "` WHERE `pdtlpl_idpdtl` = '$pdtl_id'";
					$rs2 = mysql_query($rech2) or die(mysql_error());

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

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

			$msg = "Contrat ".$code." supprim&eacute;";
		}
		else
		{
			$msg = "Impossible de supprimer un contrat cl&ocirc;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=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : Fiche Contrat Location</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>

<?PHP if ($retourliste == FALSE) 
{ 
	if ($imprim == TRUE)
	{
		if ($td == "add")
			$urlimp = "coloc_pdf.php?cl=".$codeloc;
		else
			$urlimp = "coloc_pdf.php?cl=".$code;
?>
	<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,"coloc_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
	}

	if ($td == "add")
		aff_ret_sais_auto("coloc_maj.php?td=edit&tc=".$tc."&co=".$codeloc."&cc=".$codecli."&ca=".$codeage,2000);
	else
		aff_ret_lst_auto("coloc_lst.php?ord=DES&tri=Code",2000);
}
include_once("pied.php");
?>
