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

Copyright (C) 2006  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
****************************************************************************************/

include ("../inc/config.inc.php");
?>
<?php require ("../inc/fic_fpdf.inc.php") ?>
<?php require ("../inc/func_date.inc.php") ?>
<?php include ("../inc/func_log.inc.php") ?>
<?php require ("../inc/txt.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);

	$cr = strip_tags(trim($_GET['cr']));	// Récupération code Relevé
	$pbm = strip_tags(trim($_GET['pbm']));	// Récupération type Relevé (0 : sans détail problèmes, 1 : avec détail problèmes)
	
	function Retourneville($insee)
	{
		$ville_nom = "";
		
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rnom = "SELECT villes_insee,villes_nom FROM " .TBVILLES. " WHERE villes_insee='".$insee."'";;
		$rsnom = mysql_query($rnom) or die(mysql_error());
		if (mysql_num_rows($rsnom) == 1) // Uniquement UN SEUL enregistrement !
		{
			if ($rowville = mysql_fetch_array($rsnom)) 
			{
				extract ($rowville);
			}
		}
	
		mysql_free_result($rsnom);
		mysql_close($conn2);  
	
		$ville = str_majuscules($villes_nom);
		
		// Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
		
		if (stristr($villes_nom,"LYON"))
			$ville="LYON";
		if (stristr($villes_nom,"MARSEILLE"))
			$ville="MARSEILLE";
		if (stristr($villes_nom,"PARIS"))
			$ville="PARIS";
			
		return $ville;
	}

	function Retournecp($insee)
	{
		$villes_cp = "";
		
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rcp = "SELECT villes_insee,villes_cp FROM " .TBVILLES. " WHERE villes_insee='".$insee."'";;
		$rscp = mysql_query($rcp) or die(mysql_error());
		if (mysql_num_rows($rscp) == 1) // Uniquement UN SEUL enregistrement !
		{
			if ($rowcp = mysql_fetch_array($rscp)) 
			{
				extract ($rowcp);
			}
		}
	
		mysql_free_result($rscp);
		mysql_close($conn2);  
	
		return $villes_cp;
	}

	function formatetxt($texte)
	{
		$saut = chr(13).chr(10);
		$txt = ereg_replace($saut,"\n",$texte);
		
		return $txt;
	}

	function str_majuscules($texte)
	{
		$accentue="ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËéèêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ";
		$nonaccentue="AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn";

		$txt = strtr($texte,$accentue,$nonaccentue); 
		$txt = strtoupper($txt);
	
		return $txt;
	}

	if ($cr <> "")
	{
		// Récupération des informations dossier relevé
		$row = lit_enr_rel($cr);
		if (is_array($row))
			extract ($row);

		$rel_datecreaf = formatedate($rel_datecrea);
		$rel_datemodf = formatedate($rel_datemod);

		// Récupération des données agence
		$row = lit_enr_age($rel_codeage);
		extract ($row);

		$cp_age = Retournecp($age_ville);
		$ville_age = Retourneville($age_ville);

		// Récupération des données client
		$row = lit_enr_cli($rel_codecli);
		extract ($row);

		require('../classes/fpdf/fpdf.php');
		
		class PDF extends FPDF
		{
			//En-tête
			function Header()
			{
				global
					$rel_code,
					$rel_codeage,
					$rel_datecreaf,
					$rel_datemodf,
					$chemin_logos,
					$pv_ent_age,
					$pv_ent_rel,
					$age_adr1,
					$age_adr2,
					$age_ville,
					$age_telf,
					$age_fax,
					$age_txtfax,
					$age_web,
					$age_email,
					$age_heures,
					$age_txtent,
					$cp_age,
					$ville_age;
					
		

				$pv_ent_age = 30; // Valeur de départ (mm) du pointeur pour l'affichage agence par rapport au bord haut de la feuille
								  // Cette valeur comprend la hauteur du logo (20 mm)
		
				$pv_ent_rel = 20; // Valeur de départ (mm) du pointeur pour l'affichage de l'entête intervention par rapport au bord haut de la feuille


				// Logo Agence
				$urlimg = $chemin_logos."/log".strtolower($rel_codeage).".png"; 
				$this->Image($urlimg,10,10,55,15); // Image à 10mm du bord gauche, 10mm du bord haut, largeur maximum 55mm
							
				// Entête Agence
				
					// N° de Siret
					$this->SetY($pv_ent_age);
					$this->SetFont('Arial','I',6);
					$this->Cell(95,3,$age_rcs,0,0,'L');
	
					// Coordonnées
					$ent_age = $age_adr1."\n";
					if ($age_adr2 <> "")
						$ent_age.= $age_adr2."\n";
					$ent_age.= $cp_age." ".$ville_age."\n";
					$ent_age.= "Tel : ";
					$ent_age.=$age_telf;
					if ($age_txttelf <> "")
						$ent_age.= " (".$age_txttelf.")\n";
					else
						$ent_age.= "\n";
					$ent_age.="Fax : ";
					$ent_age.= $age_fax;
					if ($age_txtfax <> "")
						$ent_age.= " (".$age_txtfax.")\n";
					else
						$ent_age.= "\n";
	
					$pv_ent_age = $pv_ent_age + 3;
					$this->SetY($pv_ent_age);
					$this->SetFont('Arial','',8);
					$this->MultiCell(95,3,$ent_age,0,'L');
	
					// Textes complémentaires
					$ent_age = "";
					if ($age_web <> "")
					{
						$ent_age = "Site Web : ";
						$ent_age.= $age_web." / ";
					}
					$ent_age.= "Email : ";
					$ent_age.= $age_email."\n";
					$ent_age.= "Horaires : ";
					$ent_age.= $age_heures."\n";
					$ent_age.= formatetxt($age_txtent);
					$this->SetFont('Arial','I',6); // Pas besoin de SetY car le MultiCell renvoie juste en dessous
					$this->MultiCell(95,3,$ent_age,0,'L');
					$pv_ent_age = $this->GetY(); // Récupération de la position du pointeur
	
				// Entête Relevé
				
				$this->SetY($pv_ent_rel);
				$this->Cell(95);
				$this->SetFont('Arial','BU',14);
				$this->Cell(95,10,'RELEVE DE PARC',0,1,'C');
				$ent_rel = 	"N°".$rel_code."\n";
				$ent_rel.= 	"Date création : ";
				$ent_rel.= $rel_datecreaf;
				$pv_ent_rel = $pv_ent_rel + 10;
				$this->SetY($pv_ent_rel);
				$this->Cell(95);
				$this->SetFont('Arial','',10);
				$this->MultiCell(95,5,$ent_rel,0,'C');
				
				$this->SetDrawColor(125);
				$this->SetLineWidth(0.4);
				$this->Line(10,$pv_ent_age,287,$pv_ent_age);
				$this->SetDrawColor(0);
				$this->SetLineWidth(0.2);

				$pv_ent_age++;
				$this->SetY($pv_ent_age);

			}
			
			//Pied de page
			function Footer()
			{
				//Positionnement à 1,5 cm du bas
				$this->SetY(-15);
				//Police Arial italique 8
				$this->SetFont('Arial','I',8);
				//Numéro de page
				$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'R');
			}

			var $B;
			var $I;
			var $U;
			var $HREF;
			
			function PDF($orientation='P',$unit='mm',$format='A4')
			{
				//Appel au constructeur parent
				$this->FPDF($orientation,$unit,$format);
				//Initialisation
				$this->B=0;
				$this->I=0;
				$this->U=0;
				$this->HREF='';
			}
			
			function WriteHTML($html)
			{
				//Parseur HTML
				/*
				$html=str_replace("\n",' ',$html);
				*/
				$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
				foreach($a as $i=>$e)
				{
					if($i%2==0)
					{
						//Texte
						if($this->HREF)
							$this->PutLink($this->HREF,$e);
						else
							$this->Write(3,$e);
					}
					else
					{
						//Balise
						if($e{0}=='/')
							$this->CloseTag(str_majuscules(substr($e,1)));
						else
						{
							//Extraction des attributs
							$a2=explode(' ',$e);
							$tag=str_majuscules(array_shift($a2));
							$attr=array();
							foreach($a2 as $v)
								if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
									$attr[str_majuscules($a3[1])]=$a3[2];
							$this->OpenTag($tag,$attr);
						}
					}
				}
			}
			
			function OpenTag($tag,$attr)
			{
				//Balise ouvrante
				if($tag=='B' or $tag=='I' or $tag=='U')
					$this->SetStyle($tag,true);
				if($tag=='A')
					$this->HREF=$attr['HREF'];
				if($tag=='BR')
					$this->Ln(5);
				if($tag=='SDP')
					$this->AddPage();
			}
			
			function CloseTag($tag)
			{
				//Balise fermante
				if($tag=='B' or $tag=='I' or $tag=='U')
					$this->SetStyle($tag,false);
				if($tag=='A')
					$this->HREF='';
			}
			
			function SetStyle($tag,$enable)
			{
				//Modifie le style et sélectionne la police correspondante
				$this->$tag+=($enable ? 1 : -1);
				$style='';
				foreach(array('B','I','U') as $s)
					if($this->$s>0)
						$style.=$s;
				$this->SetFont('',$style);
			}
			
			function PutLink($URL,$txt)
			{
				//Place un hyperlien
				$this->SetTextColor(0,0,255);
				$this->SetStyle('U',true);
				$this->Write(3,$txt,$URL);
				$this->SetStyle('U',false);
				$this->SetTextColor(0);
			}

			function Rotate($angle,$x=-1,$y=-1)
			{
				if($x==-1)
					$x=$this->x;
				if($y==-1)
					$y=$this->y;
				if($this->angle!=0)
					$this->_out('Q');
				$this->angle=$angle;
				if($angle!=0)
				{
					$angle*=M_PI/180;
					$c=cos($angle);
					$s=sin($angle);
					$cx=$x*$this->k;
					$cy=($this->h-$y)*$this->k;
					$this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
				}
			}
			
			// Permet de rajouter un fonds de page en sous-impression
			function fond($texte)
			{
				$this->SetFont('Arial','B',60);
				$this->SetTextColor(203,203,203);
				$this->Rotate(45,55,190);
				$this->Text(55,190,$texte);
				$this->Rotate(0);
				$this->SetTextColor(0,0,0);
			}
		}
	
		$pdf=new PDF();
		$pdf->FPDF('L','mm','A4');
		$pdf->SetDisplayMode('real');
		$pdf->AliasNbPages();
		$pdf->SetTitle('Relevé N°'.$rel_code);
		$pdf->SetSubject('Relevé N°'.$rel_code);
		$pdf->AddPage();

		$pv_det_cli = $pv_ent_age; // Récupération de la position sous l'entête agence pour le pointeur détail client
		$pv_det_rel = $pv_ent_age; // Récupération de la position sous l'entête agence pour le pointeur détail intervention

		$pdf->SetLineWidth(0.4);

		// Impression du détail client

		$pdf->SetY($pv_det_cli); // Renvoie le pointeur SOUS l'entête agence

		$pdf->SetFont('Arial','BU',12); // Titre
		$pdf->Cell(138,5,'CLIENT','LTR',1,'C');
		
		$ent_cli = ""; // Détail
		if ($cli_ste <>"") 
		{
			$ent_cli.=$cli_ste."\n";
			$ent_cli.= $cli_civilite." ".$cli_prenom." ".$cli_nom."\n";
		}
		else
		{
			$ent_cli.= $cli_civilite." ".$cli_prenom." ".$cli_nom."\n\n"; // Double saut pour compenser le manque de la société
		}
		$pdf->SetFont('Arial','B',10);
		$pdf->MultiCell(138,4,$ent_cli,'LR','L');

		$ent_cli = $cli_adr1."\n"; // Suite...
		$ent_cli.= $cli_adr2."\n";
		$ent_cli.= Retournecp($cli_ville)." ".Retourneville($cli_ville)."\n";
		$ent_cli.= "Tel : ".$cli_telf."\n";
		$ent_cli.= "Fax : ".$cli_fax;
		$pdf->SetFont('Arial','',10);
		$pdf->MultiCell(138,4,$ent_cli,'LRB','L');

		// Impression du détail relevé

		$pdf->SetY($pv_det_rel); // Renvoie le pointeur SOUS l'entête agence

		$pdf->Cell(138);

		$pdf->SetFont('Arial','BU',12);
		$pdf->Cell(139,5,'DETAIL','LTR',1,'C');

		$pdf->Cell(138);

		$pdf->SetFont('Arial','',8);
		$ent_rel = "\n";
		$ent_rel.= "Racine codage :\n";
		$ent_rel.= "Dernier n° machine :\n";
		$ent_rel.= "\n";
		$ent_rel.= "\n";
		$ent_rel.= "\n";
		$ent_rel.= "\n";

		$pdf->MultiCell(30,4,$ent_rel,'B','R');

		$pdf->SetY($pv_det_rel+5); // Renvoie le pointeur SOUS l'entête agence + 5mm (cellule niveau urgence)
		$pdf->Cell(168);

		$pdf->SetFont('Arial','',8);
		$ent_rel = "\n";
		$ent_rel.= $rel_rnum."\n";
		$ent_rel.= $rel_num."\n";
		$ent_rel.= "\n";
		$ent_rel.= "\n";
		$ent_rel.= "\n";
		$ent_rel.= "\n";

		$pdf->MultiCell(109,4,$ent_rel,'BR','L');

		//Notes
		$pdf->SetFont('Arial','UI',8);
		$pdf->MultiCell(277,4,'Notes :','','L');

		$pdf->SetFont('Arial','I',8);
		$ent_rel= $rel_nota."\n";

		$pdf->MultiCell(277,4,$ent_rel,'','L');

		$pdf->SetDrawColor(125);
		$pdf->SetLineWidth(0.4);
		$PosY = $pdf->GetY();
		$pdf->Line(10,$PosY,287,$PosY);


		// Impression des fiches licences
		$pdf->Ln(2);
		$pdf->SetFont('Arial','BU',12);
		$pdf->Cell(277,5,'LICENCES','',1,'C');
		$pdf->Ln(2);

		$PosY = $pdf->GetY();
		$pdf->Line(10,$PosY,287,$PosY);
		$pdf->SetDrawColor(0);
		$pdf->SetLineWidth(0.2);


		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	
		$rech = "SELECT * FROM ".TBRLIC." WHERE `rlic_codecli` = '".$rel_codecli."' ORDER BY rlic_code";
		
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) <> 0)
		{
			while ($row = mysql_fetch_assoc($rs)) 
			{         
				extract($row);
				
				$pdf->Ln(1);
				$pdf->SetFont('Arial','',10);
				$pdf->SetFillColor(255);
				$pdf->SetTextColor(255);
				$pdf->Cell(80,5,'Relevé : '.$rlic_code,1,1,'C',1);
				$pdf->SetFillColor(255);
				$pdf->SetTextColor(0);
				

				// Relevé effectué "par"
				$pdf->SetFont('Arial','I',8);

				$rech2 = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '$rlic_codetec'";
				$rs2 = mysql_query($rech2)or die(mysql_error());
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract($row2);
				}

				$pdf->Cell(140,4,'Fiche créée le '.formatedate($rlic_datecrea).'. Relevé effectué par '.$tec_nom.' '.$tec_prenom.'.',0,0,'L');
				$pdf->SetFont('Arial','',8);

				if (formatedate($rlic_dateout) == "")
				{
					$pdf->Ln(5);
				}
				else
				{
					$pdf->SetFont('Arial','BI',8);
					$pdf->Cell(137,5,'Sortie du parc le '.formatedate($rlic_dateout),0,1,'R');
					$pdf->SetFont('Arial','',8);
				}

				// Marque
				$rech2 = "SELECT * FROM ".TBRLPMA." WHERE `rlpma_code` = '".$rlic_codema."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Marque : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlpma_desc,0,1,'L');

				// Modèle
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Modèle : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlic_modele,0,1,'L');

				// Version
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Version : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlic_version,0,1,'L');

				// Type
				$rech2 = "SELECT * FROM ".TBRLTL." WHERE `rltl_code` = '".$rlic_codetl."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Type : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rltl_desc,0,1,'L');

				// Maximum de postes
				$rech2 = "SELECT * FROM ".TBRLTL." WHERE `rltl_code` = '".$rlic_codetl."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$nbutilise=0;
				$rexist = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_codecli` = '".$rlic_codecli."'";
				$rs2 = mysql_query($rexist) or die(mysql_error());
			
				if (mysql_num_rows($rs2) <> 0)
				{
					while ($resultat = mysql_fetch_array($rs2)) 
					{
						if (ereg($rlic_code,$resultat["rpdt_coderlic"]))
							$nbutilise++;
					}
				}
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Maximum de postes : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlic_maxcli.' (Utilisé : '.$nbutilise.')',0,1,'L');

				if ($nbutilise <> 0)
				{
					$pdf->SetFont('Arial','IB',8);
					$pdf->Cell(7);
					$pdf->Cell(30,4,'Détail utilisation : ','LT',0,'R');
					$rech2 = "SELECT * FROM ".TBRPDT." WHERE rpdt_codecli = '".$rlic_codecli."' ORDER BY rpdt_code";
					$rs2 = mysql_query($rech2);
			
					if (mysql_num_rows($rs2) <> 0)
					{
						$pdf->SetFont('Arial','IU',7); // Titres
						$pdf->Cell(30,4,'Code Fiche',1,0,'C');
						$pdf->Cell(14,4,'Dte Relevé',1,0,'C');
						$pdf->Cell(58,4,'Catégorie',1,0,'C');
						$pdf->Cell(58,4,'Marque / Modèle / NS / Emplacement',1,0,'C');
						$pdf->Cell(14,4,'Fin Gar.',1,1,'C');
						$pdf->SetFont('Arial','I',8);

						while ($row2 = mysql_fetch_assoc($rs2)) 
						{         
							extract($row2);
							if (ereg($rlic_code,$rpdt_coderlic))
							{
								$pdf->Cell(7);
								$pdf->Cell(30,4,'','LR',0,'R');
								$pdf->SetFont('Arial','I',7);
								$pdf->Cell(30,4,$rpdt_code,'LR',0,'L');
								$pdf->Cell(14,4,formatedate($rpdt_daterel),'LR',0,'C');
								$rech3 = "SELECT * FROM ".TBRPCAT." WHERE `rpcat_code` = '".$rpdt_codecat."'";
								$rs3 = mysql_query($rech3) or die(mysql_error());
								if (mysql_num_rows($rs3) <> 0)
								{
									if ($row3 = mysql_fetch_array($rs3))
										extract($row3);
								}
								$pdf->Cell(58,4,$rpcat_desc,'LR',0,'L');
								$rech3 = "SELECT * FROM ".TBRLPMA." WHERE `rlpma_code` = '".$rpdt_codema."'";
								$rs3 = mysql_query($rech3) or die(mysql_error());
								if (mysql_num_rows($rs3) <> 0)
								{
									if ($row3 = mysql_fetch_array($rs3))
										extract($row3);
								}
								$pdf->Cell(58,4,$rlpma_desc,'LR',0,'L');
								$pdf->Cell(14,4,formatedate($rpdt_fingar),'LR',1,'C');

								if ($rpdt_modele <> "")
								{
									$pdf->Cell(7);
									$pdf->Cell(30,4,'','LR',0,'R');
									$pdf->Cell(30,4,'','LR',0,'L');
									$pdf->Cell(14,4,'','LR',0,'C');
									$pdf->Cell(58,4,'','LR',0,'L');
									$pdf->Cell(58,4,$rpdt_modele,'LR',0,'L');
									$pdf->Cell(14,4,'','LR',1,'C');
								}

								if ($rpdt_ns <> "")
								{
									$pdf->Cell(7);
									$pdf->Cell(30,4,'','LR',0,'R');
									$pdf->Cell(30,4,'','LR',0,'L');
									$pdf->Cell(14,4,'','LR',0,'C');
									$pdf->Cell(58,4,'','LR',0,'L');
									$pdf->Cell(58,4,'NS : '.$rpdt_ns,'LR',0,'L');
									$pdf->Cell(14,4,'','LR',1,'C');
								}

								if ($rpdt_empl <> "")
								{
									$pdf->Cell(7);
									$pdf->Cell(30,4,'','LR',0,'R');
									$pdf->Cell(30,4,'','LR',0,'L');
									$pdf->Cell(14,4,'','LR',0,'C');
									$pdf->Cell(58,4,'','LR',0,'L');
									$pdf->Cell(58,4,'Empl. : '.$rpdt_empl,'LR',0,'L');
									$pdf->Cell(14,4,'','LR',1,'C');
								}
							}

							

						}
					}
					$PosY = $pdf->GetY();
					$pdf->Line(17,$PosY,221,$PosY); // On "ferme" le tableau
				}
				$pdf->SetFont('Arial','',8);

				// Média
				$rech2 = "SELECT * FROM ".TBRLMED." WHERE `rlmed_code` = '".$rlic_codemed."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Média : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlmed_desc,0,1,'L');

				// Numéro
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Numéro : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlic_numlic,0,1,'L');

				// Clef
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Clef d\'activation : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlic_cleflic,0,1,'L');

				// A jour / Légale ?
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'A jour / Légale : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				if ($rlic_ajour == TRUE)
					$ajour = "OUI";
				else
					$ajour = "NON";
				if ($rlic_legal == TRUE)
					$legal = "OUI";
				else
					$legal = "NON";
				$pdf->Cell(247,4,$ajour.' / '.$legal,0,1,'L');

				// Date Validité
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Date de validité : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,formatedate($rlic_dateval),0,1,'L');

				// Notes
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Notes : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->MultiCell(247,4,$rlic_nota,0,'L');

				// Validation
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Validé par : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlic_signataire,0,1,'L');

				if ($pbm == 1)
				{
					$pdf->Ln(2);
					$pdf->SetFont('Arial','IU',10);
					$pdf->Cell(277,5,'PROBLEMES',0,1,'C');
					$pdf->Ln(2);

					$rech2 = "SELECT * FROM ".TBRPBM." WHERE `rpbm_coderlp` = '".$rlic_code."' ORDER BY `rpbm_datecrea`";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					if (mysql_num_rows($rs2) <> 0)
					{
						$pdf->SetFont('Arial','IU',7); // Titres
						$pdf->Cell(30,4,'Déclaré Le',1,0,'C');
						$pdf->Cell(73,4,'Déclaré par',1,0,'C');
						$pdf->Cell(144,4,'Problème',1,0,'C');
						$pdf->Cell(30,4,'Résolu Le',1,1,'C');
						$pdf->SetFont('Arial','I',8);

						while ($row2 = mysql_fetch_assoc($rs2))
						{
							extract($row2);

							$pdf->SetFont('Arial','I',7);
							if (($rpbm_codeint <> "") AND (formatedate($rpbm_datett) == ""))
								$pdf->Cell(30,4,formatedate($rpbm_datecrea).' ('.$rpbm_codeint.')','LR',0,'L');
							else
								$pdf->Cell(30,4,formatedate($rpbm_datecrea),'LR',0,'L');
							$pdf->Cell(73,4,$rpbm_signale,'LR',0,'L');
							$pdf->Cell(144,4,$rpbm_pbm,'LR',0,'L');
							if (formatedate($rpbm_datett) <> "")
								$pdf->Cell(30,4,formatedate($rpbm_datett).' ('.$rpbm_codeint.')','LR',1,'L');
							else
								$pdf->Cell(30,4,formatedate($rpbm_datett),'LR',1,'L');
						}
						$PosY = $pdf->GetY();
						$pdf->Line(10,$PosY,287,$PosY); // On "ferme" le tableau
					}
					else
					{
						$pdf->SetFont('Arial','I',8);
						$pdf->Cell(277,4,'Aucun problème déclaré',0,1,'C');
					}
				}

				$pdf->Ln(1);
				$pdf->SetDrawColor(120);
				$pdf->SetLineWidth(0.4);
				$PosY = $pdf->GetY();
				$pdf->Line(10,$PosY,287,$PosY);
				$pdf->SetDrawColor(0);
				$pdf->SetLineWidth(0.2);
			}
		}
		else
		{
			$pdf->Ln(1);
			$pdf->SetFont('Arial','I',8);
			$pdf->Cell(277,4,'Aucune fiche relevé licence créée.',0,1,'L');
		}
		mysql_free_result($rs);
		mysql_close($conn);  

		$pdf->AddPage();
		$pdf->SetDrawColor(125);
		$pdf->SetLineWidth(0.4);
		$PosY = $pv_ent_age;
		$pdf->Line(10,$PosY+1,287,$PosY+1);
		$pdf->SetY($PosY+1);

		//
		// Impression des fiches Matériels 
		//

		$pdf->Ln(2);

		$pdf->SetFont('Arial','BU',12);
		$pdf->Cell(277,5,'MATERIELS','',1,'C');

		$pdf->Ln(2);

		$PosY = $pdf->GetY();
		$pdf->Line(10,$PosY,287,$PosY);
		$pdf->SetDrawColor(0);
		$pdf->SetLineWidth(0.2);

		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	
		$rech = "SELECT * FROM ".TBRPDT." WHERE `rpdt_codecli` = '".$rel_codecli."' ORDER BY rpdt_code";
		
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) <> 0)
		{
			while ($row = mysql_fetch_assoc($rs)) 
			{         
				extract($row);
				
				$pdf->Ln(1);
				$pdf->SetFont('Arial','',10);
				$pdf->SetFillColor(0);
				$pdf->SetTextColor(255);
				$pdf->Cell(80,5,'Relevé : '.$rpdt_code,1,1,'C',1);
				$pdf->SetFillColor(255);
				$pdf->SetTextColor(0);
				

				// Relevé effectué "par"
				$pdf->SetFont('Arial','I',8);

				$rech2 = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '$rpdt_codetec'";
				$rs2 = mysql_query($rech2)or die(mysql_error());
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract($row2);
				}

				$pdf->Cell(140,4,'Fiche créée le '.formatedate($rpdt_datecrea).'. Relevé effectué par '.$tec_nom.' '.$tec_prenom.'.',0,0,'L');
				$pdf->SetFont('Arial','',8);

				if (formatedate($rpdt_dateout) == "")
				{
					$pdf->Ln(5);
				}
				else
				{
					$pdf->SetFont('Arial','BI',8);
					$pdf->Cell(137,5,'Sorti du parc le '.formatedate($rpdt_dateout),0,1,'R');
					$pdf->SetFont('Arial','',8);
				}

				// Catégorie
				$rech2 = "SELECT * FROM ".TBRPCAT." WHERE `rpcat_code` = '".$rpdt_codecat."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Catégorie : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpcat_desc,0,1,'L');

				// Marque
				$rech2 = "SELECT * FROM ".TBRLPMA." WHERE `rlpma_code` = '".$rpdt_codema."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Marque : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rlpma_desc,0,1,'L');

				// Modèle & NS
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Modèle / NS : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_modele.' / '.$rpdt_ns,0,1,'L');

				// Licences
				$rech2 = "SELECT * FROM ".TBRLIC." WHERE rlic_dateout = '0000-00-00' ORDER BY rlic_code";
				$rs2 = mysql_query($rech2);
		
				if (mysql_num_rows($rs2) <> 0)
				{
					$pdf->SetFont('Arial','IB',8);
					$pdf->Cell(7);
					$pdf->Cell(30,4,'Détail licences : ','LT',0,'R');
			
					$pdf->SetFont('Arial','IU',7); // Titres
					$pdf->Cell(30,4,'Code Fiche',1,0,'C');
					$pdf->Cell(14,4,'Dte Relevé',1,0,'C');
					$pdf->Cell(58,4,'Marque',1,0,'C');
					$pdf->Cell(58,4,'Modèle / Version',1,0,'C');
					$pdf->Cell(14,4,'Validité',1,0,'C');
					$pdf->Cell(14,4,'Utilisée',1,1,'C');
					$pdf->SetFont('Arial','I',8);

					$warn = FALSE;

					while ($row2 = mysql_fetch_assoc($rs2)) 
					{         
						extract($row2);

						$pdf->Cell(7);
						$pdf->Cell(30,4,'','LR',0,'R');
						$pdf->SetFont('Arial','I',7);
						$pdf->Cell(30,4,$rlic_code,'LR',0,'L');
						$pdf->Cell(14,4,formatedate($rlic_daterel),'LR',0,'C');

						$rech3 = "SELECT * FROM ".TBRLPMA." WHERE `rlpma_code` = '".$rlic_codema."'";
						$rs3 = mysql_query($rech3) or die(mysql_error());
						if (mysql_num_rows($rs3) <> 0)
						{
							if ($row3 = mysql_fetch_array($rs3))
								extract($row3);
						}
						$pdf->Cell(58,4,$rlpma_desc,'LR',0,'L');
						$pdf->Cell(58,4,$rlic_modele,'LR',0,'L');
						$pdf->Cell(14,4,formatedate($rlic_dateval),'LR',0,'C');
						// Alertes complémentaires
						$txt_warn="";							
						if ($rlic_legal == FALSE)
						{
							$warn = TRUE;
							$txt_warn.="I";
						}
						$nb_rlic_utilisees = 0;
						$rech3 = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_codecli` = '".$rpdt_codecli."'";
						$rs3 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs3) <> 0)
						{
							while ($resultat = mysql_fetch_array($rs3)) 
							{
								if (ereg($rlic_code,$resultat["rpdt_coderlic"]))
									$nb_rlic_utilisees++;
							}
						}
						if ($nb_rlic_utilisees >= $rlic_maxcli)
						{
							$warn=TRUE;
							$txt_warn.="M";
						}
						if  (formatedate($rlic_dateval) <> "")
						{
							if ((echeance_depassee_date($datea,$rlic_dateval) == TRUE)  AND (formatedate($rlic_dateout) == ""))
							$warn=TRUE;
							$txt_warn.="V";
						}


						if ($txt_warn <> "")
						{
							if (ereg($rlic_code,$rpdt_coderlic))
								$pdf->Cell(14,4,'Oui','LR',0,'C');
							else
								$pdf->Cell(14,4,'Non','LR',0,'C');
							$pdf->Cell(5,4,$txt_warn,0,1,'L');
						}
						else
						{
							if (ereg($rlic_code,$rpdt_coderlic))
								$pdf->Cell(14,4,'Oui','LR',1,'C');
							else
								$pdf->Cell(14,4,'Non','LR',1,'C');
						}
						
						if ($rlic_version <> "")
						{
							$pdf->Cell(7);
							$pdf->Cell(30,4,'','LR',0,'L');
							$pdf->Cell(30,4,'','LR',0,'L');
							$pdf->Cell(14,4,'','LR',0,'L');
							$pdf->Cell(58,4,'','LR',0,'L');
							$pdf->Cell(58,4,'Version : '.$rlic_version,'LR',0,'L');
							$pdf->Cell(14,4,'','LR',0,'L');
							$pdf->Cell(14,4,'','LR',1,'L');
						}
					}
					$PosY = $pdf->GetY();
					$pdf->Line(17,$PosY,235,$PosY); // On "ferme" le tableau

					// Légende si alertes affichées
					if ($warn == TRUE)
					{
						$pdf->SetFont('Arial','I',8);
						$pdf->Cell(7);
						$pdf->Cell(218,4,'Légende : \'I\': Licence illégale / \'M\': Maximum dépassé / \'V\': Validité dépassée',0,1,'R');
					}
				}
				$pdf->SetFont('Arial','',8);

				// Emplacement
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Emplacement : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_empl,0,1,'L');

				// Codage
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Codage Produit : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_codep,0,1,'L');

				// Utilisateur
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Utilisateur : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_util,0,1,'L');

				// Date Achat
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Date d\'achat : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,formatedate($rpdt_dateach),0,1,'L');

				// Fin de Garantie
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Date d\'achat : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,formatedate($rpdt_fingar),0,1,'L');

				// Gtie Sur site / propriétaire
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'G. site / Propriétaire : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				if ($rpdt_gars == TRUE)
					$gsite = "OUI";
				else
					$gsite = "NON";
				if ($rpdt_prop == TRUE)
					$prop = "OUI";
				else
					$prop = "NON";
				$pdf->Cell(247,4,$gsite.' / '.$prop,0,1,'L');

				// Nom du poste
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Nom du poste : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_nomp,0,1,'L');

				// Groupe Travail
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Groupe de Travail : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_gt,0,1,'L');

				// Domaine
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Domaine : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_dom,0,1,'L');

				// Login / MDP
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Login / MDP : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_login." / ".$rpdt_mdp,0,1,'L');

				// IP / Masque / Passerelle
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'IP / Masque / Pass. : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_ip." / ".$rpdt_masq." / ".$rpdt_pass,0,1,'L');

				// SID / WEP
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'SID / WEP : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_sid." / ".$rpdt_wep,0,1,'L');

				// Notes
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Notes : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->MultiCell(247,4,$rpdt_nota,0,'L');

				// Validation
				$pdf->SetFont('Arial','B',8);
				$pdf->Cell(30,4,'Validé par : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$pdf->Cell(247,4,$rpdt_signataire,0,1,'L');

				if ($pbm == 1)
				{
					$pdf->Ln(2);
					$pdf->SetFont('Arial','IU',10);
					$pdf->Cell(277,5,'PROBLEMES',0,1,'C');
					$pdf->Ln(2);

					$rech2 = "SELECT * FROM ".TBRPBM." WHERE `rpbm_coderlp` = '".$rpdt_code."' ORDER BY `rpbm_datecrea`";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					if (mysql_num_rows($rs2) <> 0)
					{
						$pdf->SetFont('Arial','IU',7); // Titres
						$pdf->Cell(30,4,'Déclaré Le',1,0,'C');
						$pdf->Cell(73,4,'Déclaré par',1,0,'C');
						$pdf->Cell(144,4,'Problème',1,0,'C');
						$pdf->Cell(30,4,'Résolu Le',1,1,'C');
						$pdf->SetFont('Arial','I',8);

						while ($row2 = mysql_fetch_assoc($rs2))
						{
							extract($row2);

							$pdf->SetFont('Arial','I',7);
							if (($rpbm_codeint <> "") AND (formatedate($rpbm_datett) == ""))
								$pdf->Cell(30,4,formatedate($rpbm_datecrea).' ('.$rpbm_codeint.')','LR',0,'L');
							else
								$pdf->Cell(30,4,formatedate($rpbm_datecrea),'LR',0,'L');
							$pdf->Cell(73,4,$rpbm_signale,'LR',0,'L');
							$pdf->Cell(144,4,$rpbm_pbm,'LR',0,'L');
							if (formatedate($rpbm_datett) <> "")
								$pdf->Cell(30,4,formatedate($rpbm_datett).' ('.$rpbm_codeint.')','LR',1,'L');
							else
								$pdf->Cell(30,4,formatedate($rpbm_datett),'LR',1,'L');
						}
						$PosY = $pdf->GetY();
						$pdf->Line(10,$PosY,287,$PosY); // On "ferme" le tableau
					}
					else
					{
						$pdf->SetFont('Arial','I',8);
						$pdf->Cell(277,4,'Aucun problème déclaré',0,1,'C');
					}
				}

				$pdf->Ln(1);
				$pdf->SetDrawColor(120);
				$pdf->SetLineWidth(0.4);
				$PosY = $pdf->GetY();
				$pdf->Line(10,$PosY,287,$PosY);
				$pdf->SetDrawColor(0);
				$pdf->SetLineWidth(0.2);
			}
		}
		else
		{
			$pdf->Ln(1);
			$pdf->SetFont('Arial','I',8);
			$pdf->MultiCell(277,4,'Aucune fiche relevé matériel créée.','','L');
		}
		mysql_free_result($rs);
		mysql_close($conn);  
/*
		//Détermination d'un nom de fichier temporaire dans le répertoire courant
		$file=basename(tempnam(getcwd(),'tmp'));
		rename($file,$file.'.pdf');
		$file.='.pdf';
*/
		// $pdf->Output($file,'D');
		$pdf->Output('rel_'.$rel_code,'I');
	}
?>