<?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_int.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_ope.inc.php");
include_once("../inc/fic_txt.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"]);

$coderech = strip_tags(trim($_GET['code']));	// Récupération code Contrat
	
$pbm_cocl = FALSE;

if ($coderech == "")
	$pbm_cocl = TRUE;

// Niveau =2 interdit
if (@$_SESSION["niv_log"] == 2)
	$pbm_cocl = TRUE;

if ($pbm_cocl == FALSE)
{	
	// Récupération des informations contrat client
	$row = lit_enr_cocl($coderech);
	if (is_array($row))
	{
		extract ($row);
		// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
		if (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) AND ($cocl_codeage <> @$_SESSION["code_age"]))
			$pbm_cocl = TRUE;

		if ($pbm_cocl == FALSE)
		{
			$cocl_datecreaf = formatedate($cocl_datecrea);
			$cocl_datesigf = formatedate($cocl_datesig);
			$cocl_datefinf = formatedate($cocl_datefin);
		
			$row = lit_enr_co($cocl_codeco);
			extract($row);
		
			include_once('../classes/fpdf/fpdf.php');
			include_once('../inc/fpdf_ext.inc.php');
			
			$pdf=new PDF();
			$pdf->FPDF('P','mm','A4');
			$pdf->SetDisplayMode('real');
			$pdf->AliasNbPages();
			$pdf->SetTitle('Contrat N°'.$cocl_code);
			$pdf->SetSubject('Contrat N°'.$cocl_code);
			$pdf->settgen();
			$pdf->settheader("cocl");
			$pdf->AddPage();
		
			$pdf->SetLineWidth(0.4);
		
			$txt_texte = formatetxt($co_cgv);
			$txt_cocl = parse_txtcorps("age",$cocl_codeage,$txt_texte,"");
			$txt_cocl = parse_txtcorps("cdg","",$txt_cocl,"");
			$txt_cocl = parse_txtcorps("cli",$cocl_codecli,$txt_cocl,"");
			$txt_cocl = parse_txtcorps("con",$cocl_code,$txt_cocl,"")."<BR>"; // BR important pour retour à la ligne final
		
			$pdf->SetFont('Arial','',10);
			$pdf->WriteHTML($txt_cocl);

			$nompdf='con_'.$cocl_code.'.pdf';
			switch($pdf->tgen)
			{
				// Sortie vers fichier stocké et envoi email
				case 1 :
					$pdf->Output($url_gfc."/pj/".$nompdf,'F');
					$tact=strtolower($pdf->tact);
					$sujet="";
					$type_ope="";
					switch ($tact)
					{
						case "add" :
							$sujet="Création de votre contrat ".$cocl_code;
							$type_ope="cre";
							break;
						case "edit" :
							if (formatedate($cocl_datefin) <> "")
							{
								$sujet="Clôture de votre contrat ".$cocl_code;
								$type_ope="clo";
							}
							else
							{
								$sujet="Modification de votre contrat ".$cocl_code;
								$type_ope="mod";
							}
							break;
						case "cop" :
							$sujet="Copie du contrat ".$cocl_code;
							$type_ope="cop";
							break;
					}
					envoi_mail("",$sujet,"con",$type_ope,"xxx","cli",$cocl_code,"","",TRUE,$url_gfc."/pj/".$nompdf);
					@unlink($url_gfc."/pj/".$nompdf);
					break;
				// Sortie classique vers navigateur
				default :
					$pdfcontent = $pdf->Output($nompdf,'I');
					break;
			}

		}
	}
	else
		$pbm_cocl = TRUE;

}

if ($pbm_cocl == TRUE)
{
?>
	<script type="text/javascript">
	self.close('');
	</script>
<?PHP
}
else
{
	if ($pdf->tgen==1)
	{
?>
	<script type="text/javascript">
	self.close('');
	</script>
<?PHP
	}
}
?>

?>