<?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_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_loc.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_statut.inc.php");
include_once("../inc/fic_tec.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_geo.inc.php");
Archivelog(@$_SESSION["code_log"]);

$cl = strip_tags(trim($_GET['cl']));	// Récupération code Location
$ca = strip_tags(trim($_GET['ca']));	// Récupération code article
$niv_util = @$_SESSION["niv_log"];
$pbm_locpdf = FALSE;

if ($cl <> "")
{
	$nbartloc = nb_pdtloc_in_coloc($cl,TRUE);
	
	if ($nbartloc>0) // On imprime que s'il y a des articles dans la location
	{
		$row = lit_enr_coloc($cl);
		if (is_array($row))
		{
			extract ($row);
			
			$row=lit_enr_pdtloc($ca);
			if (is_array($row))
			{
				extract($row);
				
				$row=lit_enr_pdt_in_coloc($ca,$cl);
				if (is_array($row))
				{
					extract($row);
				
					// Contrôle de hack pour un client
					if (($niv_util == 1)  AND ($coloc_codecli <> @$_SESSION["code_log"]))
						$pbm_locpdf=TRUE;
			
					// Contrôle de hack pour une agence
					if ((($niv_util == 4) OR ($niv_util == 3)) AND ($coloc_codeage <> @$_SESSION["code_age"]))
						$pbm_locpdf=TRUE;
					
					// Contrôle de hack pour un donneur d'ordres
					if ((($niv_util == 7) OR ($niv_util == 6)) AND ($coloc_codedo <> @$_SESSION["code_do"]))
						$pbm_locpdf=TRUE;
					
					// Contrôle de hack pour un sous-traitant
					if ((($niv_util == 11) OR ($niv_util == 10)) AND ($coloc_codest <> @$_SESSION["code_st"]))
						$pbm_locpdf=TRUE;
					
					if ($pbm_locpdf==FALSE)
					{
						// Récupération des indexes
						$udesc1="";
						$udesc2="";
						$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);
						$rech = "SELECT * FROM ".TBPDTU." WHERE `pdtu_code`= '".$pdtloc_pdtucons1."'";
						$rs = mysql_query($rech)or die(mysql_error());
						if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
						{
							if ($row = mysql_fetch_array($rs))
							{
								extract ($row);
								$udesc1=$pdtu_desc;
							}
						}
						$rech = "SELECT * FROM ".TBPDTU." WHERE `pdtu_code`= '".$pdtloc_pdtucons2."'";
						$rs = mysql_query($rech)or die(mysql_error());
						if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
						{
							if ($row = mysql_fetch_array($rs))
							{
								extract ($row);
								$udesc2=$pdtu_desc;
							}
						}

						// Récuépartion du barême
						$rech = "SELECT * FROM ".TBPDTLBL." WHERE pdtlbl_idpdtl='".$pdtl_id."'";
						$rs = mysql_query($rech)or die(mysql_error());
						if ($row = mysql_fetch_array($rs)) 
							extract($row);

						// Calculs divers
						//... Montant location
						$pvht=$pdtlbl_pvht;
						$tothtloc=round(($pdtlbl_qte*$pvht)*100)/100;
						$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlbl_codetva."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if ($row2 = mysql_fetch_assoc($rs2))
							extract($row2);
						$txtva=$tva_tx/100;
						$totttcloc=round(($tothtloc*(1+$txtva))*100)/100;
						$tottvaloc=$totttcloc-$tothtloc;
						//... Caution
						$pvht=$pdtlbl_caution;
						$tothtcaut=round($pvht*100)/100;
						$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlbl_tvacaut."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if ($row2 = mysql_fetch_assoc($rs2))
							extract($row2);
						$txtva=$tva_tx/100;
						$totttccaut=round(($tothtcaut*(1+$txtva))*100)/100;
						$tottvacaut=$totttccaut-$tothtcaut;
						//... Assurance
						$tothtass=0;
						$totttcass=0;
						$tottvaass=0;
						if ($pdtlbl_avecass==1)
						{
							$pvht=$pdtlbl_mtass;
							$tothtass=round(($pdtlbl_qte*$pvht)*100)/100;
							$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlbl_codetvaass."'";
							$rs2 = mysql_query($rech2) or die(mysql_error());
							if ($row2 = mysql_fetch_assoc($rs2))
								extract($row2);
							$txtva=$tva_tx/100;
							$totttcass=round(($tothtass*(1+$txtva))*100)/100;
							$tottvaass=$totttcass-$tothtass;
						}
						//...Consommables
						$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlbl_codetva."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if ($row2 = mysql_fetch_assoc($rs2))
							extract($row2);
						$txtva=$tva_tx/100;
						//... Consommable 1
						$pvht=$pdtlbl_puconsi1;
						$tothtcons1=round(($pdtlbl_qte*$pvht)*100)/100;
						$totttccons1=round(($tothtcons1*(1+$txtva))*100)/100;
						$tottvacons1=$totttccons1-$tothtcons1;
						//... Consommable 2
						$pvht=$pdtlbl_puconsi2;
						$tothtcons2=round(($pdtlbl_qte*$pvht)*100)/100;
						$totttccons2=round(($tothtcons2*(1+$txtva))*100)/100;
						$tottvacons2=$totttccons2-$tothtcons2;

						mysql_free_result($rs);
						mysql_close($conn);

						$coloc_datecreaf = formatedate($coloc_datecrea);
						$coloc_datefinf = formatedate($coloc_datefin);
						$coloc_dateclosf = formatedate($coloc_dateclos);
				
						$row = lit_enr_age($coloc_codeage,TRUE);
						extract ($row);
				
						$cp_age = Retournecp_openId($age_ville,TRUE);
						$ville_age = Retourneville_openId($age_ville,TRUE);
				
						switch ($coloc_typcli)
						{
							// Coordonnées compte client
							case 0 :
								$row = lit_enr_cli($coloc_codecli,TRUE);
								extract ($row);
						
								// Coordonnées site intervention
								if ($coloc_idsite>0)
								{
									$row = lit_enr_scli_id($coloc_idsite,TRUE);
									extract ($row);
								}
								break;
							// Coordonnées compte agence
							case 1 :
								$row = lit_enr_age($coloc_codecli,TRUE);
								extract ($row);
								break;
							// Coordonnées compte technicien
							case 2 :
							case 4 :
								$row = lit_enr_tec($coloc_codecli,TRUE);
								extract ($row);
								break;
							// Coordonnées compte sous-traitant
							case 3 :
								$row = lit_enr_st($coloc_codecli,TRUE);
								extract ($row);
								break;
						}
				
						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('Départ/Retour '.$pdtloc_des);
						$pdf->SetSubject('Contrat Location N°'.$coloc_code);
						$pdf->settheader("dr");
						$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_loc = $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(95,5,'LOUE PAR','LTR',1,'C');
						
						$nl_ent=7;
						if ($coloc_idsite==0) // S'il s'agit du site principal
						{
							$ent_cli = ""; // Détail
							switch ($coloc_typcli)
							{
								// Client
								case 0 :
									if ($cli_ste <>"") 
										$ent_cli.=$cli_ste."\n";
									else
										$ent_cli.= $cli_civilite." ".$cli_prenom." ".$cli_nom."\n";
									$pdf->SetFont('Arial','B',10);
									$pdf->MultiCell(95,4,$ent_cli,'LR','L');
									$nl_ent=$nl_ent-1;
							
									$ent_cli = $cli_adr1."\n"; // Suite...
									if (trim($cli_adr2)<>"")
									{
										$ent_cli.= $cli_adr2."\n";
										$nl_ent=$nl_ent-1;
									}
									$ent_cli.= Retournecp_openId($cli_ville,TRUE)." ".Retourneville_openId($cli_ville,TRUE)."\n";
									$ent_cli.= "Tel : ".$cli_telf."\n";
									$ent_cli.= "Fax : ".$cli_fax;
									$nl_ent=$nl_ent-4;
									break;
								// Agence
								case 1 :
									$ent_cli.=$age_nom."\n";
									$pdf->SetFont('Arial','B',10);
									$pdf->MultiCell(95,4,$ent_cli,'LR','L');
									$nl_ent=$nl_ent-1;
							
									$ent_cli = $age_adr1."\n"; // Suite...
									if (trim($age_adr2)<>"")
									{
										$ent_cli.= $age_adr2."\n";
										$nl_ent=$nl_ent-1;
									}
									$ent_cli.= Retournecp_openId($age_ville,TRUE)." ".Retourneville_openId($age_ville,TRUE)."\n";
									$ent_cli.= "Tel : ".$age_telf."\n";
									$ent_cli.= "Fax : ".$age_fax;
									$nl_ent=$nl_ent-4;
									break;
								// Technicien
								case 2 :
								case 4 :
									$ent_cli.=$tec_nom."\n";
									$pdf->SetFont('Arial','B',10);
									$pdf->MultiCell(95,4,$ent_cli,'LR','L');
									$nl_ent=$nl_ent-1;
							
									$ent_cli = $tec_adr1."\n"; // Suite...
									if (trim($tec_adr2)<>"")
									{
										$ent_cli.= $tec_adr2."\n";
										$nl_ent=$nl_ent-1;
									}
									$ent_cli.= Retournecp_openId($tec_ville,TRUE)." ".Retourneville_openId($tec_ville,TRUE)."\n";
									$ent_cli.= "Tel : ".$tec_telf."\n";
									$nl_ent=$nl_ent-4;
									break;
								// Sous-traitant
								case 3 :
									$ent_cli.=$st_nom."\n";
									$pdf->SetFont('Arial','B',10);
									$pdf->MultiCell(95,4,$ent_cli,'LR','L');
									$nl_ent=$nl_ent-1;
							
									$ent_cli = $st_adr1."\n"; // Suite...
									if (trim($st_adr2)<>"")
									{
										$ent_cli.= $st_adr2."\n";
										$nl_ent=$nl_ent-1;
									}
									$ent_cli.= Retournecp_openId($st_ville,TRUE)." ".Retourneville_openId($st_ville,TRUE)."\n";
									$ent_cli.= "Tel : ".$st_telf."\n";
									$ent_cli.= "Fax : ".$st_fax;
									$nl_ent=$nl_ent-4;
									break;
							}
							if ($nl_ent>0)
							{
								$ent_cli.= "\n";
								for ($nl=$nl_ent; $nl>0; $nl--)
								{
									$ent_cli.= "\n";
								}
							}
			
							$pdf->SetFont('Arial','',10);
							$pdf->MultiCell(95,4,$ent_cli,'LRB','L');
						}
						else // S'il s'agit d'un site secondaire
						{
							$ent_scli = ""; // Détail
							$ent_scli.=$scli_ste."\n";
							$nl_ent=$nl_ent-1;
							$pdf->SetFont('Arial','B',10);
							$pdf->MultiCell(95,4,$ent_scli,'LR','L');
					
							$ent_scli = $scli_adr1."\n"; // Suite...
							if (trim($scli_adr2)<>"")
							{
								$ent_scli.= $scli_adr2."\n";
								$nl_ent=$nl_ent-1;
							}
							$ent_scli.= Retournecp_openId($scli_ville,TRUE)." ".Retourneville_openId($scli_ville,TRUE)."\n";
							$ent_scli.= "Tel : ".$scli_telf."\n";
							$ent_scli.= "Fax : ".$scli_fax;
							$nl_ent=$nl_ent-4;
							if ($nl_ent>0)
							{
								$ent_scli.= "\n";
								for ($nl=$nl_ent; $nl>0; $nl--)
								{
									$ent_scli.= "\n";
								}
							}
			
							$pdf->SetFont('Arial','',10);
							$pdf->MultiCell(95,4,$ent_scli,'LRB','L');
						}
				
						// Impression du détail location
						$pdf->SetY($pv_det_loc); // Renvoie le pointeur SOUS l'entête agence
				
				
						$pdf->Cell(95);
						
						$pdf->SetFont('Arial','',8);  // Niveau d'urgence
						$pdf->Cell(95,5,$coloc_urg,'LTR',1,'R');
				
						$pdf->Cell(95);
				
						$ent_loc = "Contrat :\n";
						$ent_loc.= "Agence :\n";
						$ent_loc.= "Demandé par :\n";
						$ent_loc.= "Départ prévu :\n";
						$ent_loc.= "Retour prévu :\n";
						$ent_loc.= "Caution :\n";

						for ($nl=1; $nl<2; $nl++)
						{
							$ent_loc.= "\n";
						}
				
						$pdf->MultiCell(22,4,$ent_loc,'B','R');
				
						$pdf->SetY($pv_det_loc+5); // Renvoie le pointeur SOUS l'entête agence + 5mm (cellule niveau urgence)
						$pdf->Cell(117);
				
						$ent_loc = $coloc_code."\n";
						$ent_loc.= $coloc_codeage."\n";
						$txt_ccli="Pas de contact";
						switch (intval($coloc_ddeur))
						{
							case -1 :
								switch ($tc)
								{
									case 0 :
										$txt_ccli = $cli_nom." ".$cli_prenom." (Contact principal)";
										break;
									case 1 :
										$txt_ccli = $age_resp_nom." ".$age_resp_prenom." (Contact principal)";
										break;
									case 3 :
										$txt_ccli = $st_resp_nom." ".$st_resp_prenom." (Contact principal)";
										break;
								}
								break;
							case 0 :
								$row = lit_enr_scli_id($coloc_idsite,TRUE);
								if (is_array($row))
								{
									extract ($row);
									$txt_ccli = $scli_rnom." ".$scli_rprenom." (Contact site)";
								}
								break;
							default :
								switch ($tc)
								{
									case 0 :
										$row = lit_enr_ccli_id_in_cli($coloc_ddeur,$cli_code);
										if (is_array($row))
										{
											extract ($row);
											$txt_ccli = $ccli_nom." ".$ccli_prenom." (".$cli_codefonc.")";
										}
										break;
									case 1 :
										$row = lit_enr_cage_id_in_age($coloc_ddeur,$age_code);
										if (is_array($row))
										{
											extract ($row);
											$txt_ccli = $cage_nom." ".$cage_prenom." (".$age_codefonc.")";
										}
										break;
									case 3 :
										$row = lit_enr_cst_id_in_st($coloc_ddeur,$st_code);
										if (is_array($row))
										{
											extract ($row);
											$txt_ccli = $cst_nom." ".$cst_prenom." (".$st_codefonc.")";
										}
										break;
								}
								break;
						}
						$ent_loc.= $txt_ccli."\n";
						$ent_loc.= formatedate($pdtl_datedeb)." ".substr($pdtl_heuredeb,0,5)."\n";
						$ent_loc.= formatedate($pdtl_datefin)." ".substr($pdtl_heurefin,0,5)."\n";

						$ent_loc.= $totttccaut." ".$lang['symbole_monnaie_txt']." TTC\n";

						for ($nl=1; $nl<2; $nl++)
						{
							$ent_loc.= "\n";
						}
				
						$pdf->MultiCell(73,4,$ent_loc,'BR','L');
						
						$pdf->ln(2);
						
						$pdf->SetLineWidth(0.2);
						$pdf->SetDrawColor(220);
				
						/* Barême applicable */
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Barême applicable : ',0,0,'R');
						$txtbarloc="";
						if ($pdtlbl_idbarloc<>0)
						{
							$row = lit_enr_barloc_id($pdtlbl_idbarloc,TRUE);
							if (is_array($row))
							{
								extract($row);
								$txtbarloc=$barloc_des;
							}
						}
						else
							$txtbarloc="Prêt de matériel (Gratuit)";
						$txtbarloc=$txtbarloc." (inclus caution";
						if ($pdtlbl_avecass==1)
							$txtbarloc.=" et assurance";
						else
							$txtbarloc.=", sans assurance";
						$txtbarloc.=")";
						$pdf->SetFont('Arial','',8);
						$pdf->MultiCell(150,5,$txtbarloc,'','L');

						// Montant barême
						//... Titres tableau
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'',0,0,'R');
						$pdf->SetFont('Arial','B',8);
						$pdf->SetFillColor(0);
						$pdf->SetTextColor(255);
						$pdf->Cell(20,5,'Qté',1,0,'C',1);
						$pdf->Cell(50,5,'Total HT',1,0,'C',1);
						$pdf->Cell(20,5,'TVA',1,0,'C',1);
						$pdf->Cell(50,5,'Total TTC',1,1,'C',1);
						//... Détail
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'',0,0,'R');
						$pdf->SetFont('Arial','',8);
						$pdf->SetFillColor(200);
						$pdf->SetTextColor(0);
						$pdf->Cell(20,5,$pdtlbl_qte,1,0,'C',1);

						// Affichage barême
						$totbarht=$tothtloc+$tothtcaut+$tothtass+$tothtcons1+$tothtcons2;
						$txt=format_num($totbarht,2);
						$txt.=" ".$lang['symbole_monnaie_txt'];
						$pdf->Cell(50,5,$txt,1,0,'R',1);
						$totbartva=$tottvaloc+$tottvacaut+$tottvaass+$tottvacons1+$tottvacons2;
						$txt=format_num($totbartva,2);
						$txt.=" ".$lang['symbole_monnaie_txt'];
						$pdf->Cell(20,5,$txt,1,0,'R',1);
						$totbarttc=$totttcloc+$totttccaut+$totttcass+$totttccons1+$totttccons2;
						$txt=format_num($totbarttc,2);
						$txt.=" ".$lang['symbole_monnaie_txt'];
						$pdf->Cell(50,5,$txt,1,1,'R',1);

						$txt="";
						if ($pdtlbl_qconsi1>0)
						{
							$txt.="Inclus ".$pdtlbl_qte." forfait(s) ".$pdtlbl_qconsi1." ".strtolower($udesc1);
							if ($pdtlbl_qconsi2>0)
								$txt.=" / ";
						}

						if ($pdtlbl_qconsi2>0)
							$txt.="Inclus ".$pdtlbl_qte." forfait(s) ".$pdtlbl_qconsi2." ".strtolower($udesc2);
						
						if (trim($txt)<>"")
						{
							$pdf->SetFont('Arial','B',10);
							$pdf->Cell(40,4,'',0,0,'R');
							$pdf->SetFont('Arial','I',8);
							$pdf->Cell(50,4,$txt,0,1,'L');
						}
						

						$pdf->ln(2);
			
						/* DEPART PRODUIT */
						$PosY = $pdf->GetY();
						$pdf->SetDrawColor(0);
						$pdf->SetLineWidth(0.4);
						$pdf->LineHP(10,205,$PosY,5,1);
						$pdf->SetDrawColor(220);
						$pdf->SetLineWidth(0.2);
	
						$pdf->ln(2);
						$pdf->SetFont('Arial','BU',12);
						$pdf->Cell(180,5,'DEPART PRODUIT','',1,'C');

						// Détail de celui qui prend le produit
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Pris par : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_datedep)=="")
							$pdf->MultiCell(150,5,$pdtl_prispar,1,'L');
						else
							$pdf->MultiCell(150,5,$pdtl_prispar,0,'L');

						// Date/heure du départ réels
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Remis le : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_datedep)=="")
							$pdf->Cell(30,5,'',1,0,'L');
						else
							$pdf->Cell(30,5,formatedate($pdtl_datedep),'',0,'L');

						$pdf->Cell(5,5,' à ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_datedep)=="")
							$pdf->Cell(30,5,'',1,1,'L');
						else
							$pdf->Cell(30,5,substr($pdtl_heuredep,0,5),'',1,'L');

						// Indexes
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Indexes : ',0,0,'R');
						if (trim($udesc1)<>"")
						{
							$pdf->SetFont('Arial','BI',8);
							$pdf->Cell(40,5,$udesc1.' :',0,0,'R');
							$pdf->SetFont('Arial','',8);
							if (formatedate($pdtl_datedep)=="")
								$pdf->Cell(40,5,'',1,0,'L');
							else
								$pdf->Cell(40,5,$pdtl_didx1,'',0,'L');
							$pdf->SetFont('Arial','I',8);
							$pdf->Cell(20,5,'(Précédent : '.$pdtloc_idxcons1.')','',1,'L');
							
						}
						if (trim($udesc2)<>"")
						{
							if (trim($udesc1)<>"")
							{
								$pdf->SetFont('Arial','B',10);
								$pdf->Cell(40,5,'',0,0,'R');
							}
							$pdf->SetFont('Arial','BI',8);
							$pdf->Cell(40,5,$udesc2.' :',0,0,'R');
							$pdf->SetFont('Arial','',8);
							if (formatedate($pdtl_datedep)=="")
								$pdf->Cell(40,5,'',1,0,'L');
							else
								$pdf->Cell(40,5,$pdtl_didx2,'',0,'L');
							$pdf->SetFont('Arial','I',8);
							$pdf->Cell(20,5,'(Précédent : '.$pdtloc_idxcons2.')','',1,'L');
						}
						if ((trim($udesc1)=="") AND (trim($udesc2)==""))
						{
							$pdf->SetFont('Arial','BI',8);
							$pdf->Cell(40,5,'Pas de consommables',0,1,'L');
						}

						// Caution
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Mode caution : ',0,0,'R');
						if (formatedate($pdtl_datedep)=="")
						{
							$pdf->SetFont('Arial','',8);
							$pdf->Cell(80,5,'',1,1,'L');
						}
						else
						{
							$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
							mysql_select_db(DBNAME);
							$rech = "SELECT * FROM ".TBREG." WHERE `reg_code` = '".$pdtl_coderegc."'";
							$rs = mysql_query($rech) or die(mysql_error());
							if ($row = mysql_fetch_array($rs))
							{
								extract ($row);
							}
							mysql_free_result($rs);
							mysql_close($conn);
							$pdf->SetFont('Arial','',8);
							$pdf->Cell(80,5,$reg_desc,0,1,'L');
						}

						// Notes départ
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Notes : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_datedep)=="")
						{
							$txt="";
							for ($i=0; $i<5; $i++)
							{
								$txt.="\n";
							}
							$pdf->MultiCell(150,5,$txt,1,'L');
						}
						else
							$pdf->MultiCell(150,5,$pdtl_noted,0,'L');

						// Date / Signature client
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Signature client : ',0,0,'R');
						$PosX = $pdf->GetX()+15;
						$PosY = $pdf->GetY()+40;
						$pdf->SetFont('Arial','',8);
						$txt="";
						for ($i=0; $i<5; $i++)
						{
							$txt.="\n";
						}
						$pdf->MultiCell(75,5,$txt,1,'L');
						
						// On signale si c'est déjà signé au départ
						if ((formatedate($pdtl_datedep)<>"") AND ($pdtl_datedep<>$datea))
						{
							$pdf->SetFont('Arial','B',20);
							$pdf->SetTextColor(203,203,203);
							$pdf->Rotate(20);
							$pdf->Text($PosX,$PosY,"Déjà signé");
							$pdf->Rotate(0);
							$pdf->SetTextColor(0,0,0);
						}

						$pdf->ln(2);

						/* RETOUR PRODUIT */
						$PosY = $pdf->GetY();
						$pdf->SetDrawColor(0);
						$pdf->SetLineWidth(0.4);
						$pdf->LineHP(10,205,$PosY,5,1);
						$pdf->SetDrawColor(220);
						$pdf->SetLineWidth(0.2);
	
						$pdf->ln(2);
						$pdf->SetFont('Arial','BU',12);
						$pdf->Cell(180,5,'RETOUR PRODUIT','',1,'C');

						// Détail de celui qui prend le produit
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Rendu par : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_dateret)=="")
							$pdf->MultiCell(150,5,$pdtl_retpar,1,'L');
						else
							$pdf->MultiCell(150,5,$pdtl_retpar,0,'L');

						// Date/heure du retour réels
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Rendu le : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_dateret)=="")
							$pdf->Cell(30,5,'',1,0,'L');
						else
							$pdf->Cell(30,5,formatedate($pdtl_dateret),'',0,'L');

						$pdf->Cell(5,5,' à ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_dateret)=="")
							$pdf->Cell(30,5,'',1,1,'L');
						else
							$pdf->Cell(30,5,substr($pdtl_heureret,0,5),'',1,'L');

						// Indexes
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Indexes : ',0,0,'R');
						if (trim($udesc1)<>"")
						{
							$pdf->SetFont('Arial','BI',8);
							$pdf->Cell(40,5,$udesc1.' :',0,0,'R');
							$pdf->SetFont('Arial','',8);
							if (formatedate($pdtl_dateret)=="")
								$pdf->Cell(40,5,'',1,1,'L');
							else
								$pdf->Cell(40,5,$pdtl_ridx1,'',1,'L');
							
						}
						if (trim($udesc2)<>"")
						{
							if (trim($udesc1)<>"")
							{
								$pdf->SetFont('Arial','B',10);
								$pdf->Cell(40,5,'',0,0,'R');
							}
							$pdf->SetFont('Arial','BI',8);
							$pdf->Cell(40,5,$udesc2.' :',0,0,'R');
							$pdf->SetFont('Arial','',8);
							if (formatedate($pdtl_dateret)=="")
								$pdf->Cell(40,5,'',1,1,'L');
							else
								$pdf->Cell(40,5,$pdtl_ridx2,'',1,'L');
						}
						if ((trim($udesc1)=="") AND (trim($udesc2)==""))
						{
							$pdf->SetFont('Arial','BI',8);
							$pdf->Cell(40,5,'Pas de consommables',0,1,'L');
						}

						// Caution rendue ?
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Caution rendue : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_dateret)=="")
							$pdf->Cell(10,5,'Oui / Non',0,1,'L');
						else
							$pdf->Cell(10,5,$non_oui[$pdtl_rcaut],0,1,'L');

						// Remise en location
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Remise en location : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_dateret)=="")
							$pdf->Cell(10,5,'Oui / Non',0,1,'L');
						else
							$pdf->Cell(10,5,$non_oui[$pdtl_reloc],0,1,'L');

						// Notes retour
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Notes : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						if (formatedate($pdtl_dateret)=="")
						{
							$txt="";
							for ($i=0; $i<5; $i++)
							{
								$txt.="\n";
							}
							$pdf->MultiCell(150,5,$txt,1,'L');
						}
						else
							$pdf->MultiCell(150,5,$pdtl_noter,0,'L');

						// Date / Signature client
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(40,5,'Signature client : ',0,0,'R');
						$pdf->SetFont('Arial','',8);
						$txt="";
						for ($i=0; $i<5; $i++)
						{
							$txt.="\n";
						}
						$pdf->MultiCell(75,5,$txt,1,'L');

						/* FACTURATION FINALE */
						if (formatedate($pdtl_dateret)<>"")
						{
							$pdf->ln(2);
	
							$PosY = $pdf->GetY();
							$pdf->SetDrawColor(0);
							$pdf->SetLineWidth(0.4);
							$pdf->LineHP(10,205,$PosY,5,1);
							$pdf->SetDrawColor(220);
							$pdf->SetLineWidth(0.2);
	
							$pdf->ln(2);
							$pdf->SetFont('Arial','BU',12);
							$pdf->Cell(180,5,'A FACTURER','',1,'C');

							// Montant barême
							//... Titres tableau
							$pdf->SetFont('Arial','B',8);
							$pdf->SetFillColor(0);
							$pdf->SetTextColor(255);
							$pdf->Cell(92,5,'Désignation',1,0,'C',1);
							$pdf->Cell(20,5,'Qté',1,0,'C',1);
							$pdf->Cell(30,5,'Total HT',1,0,'C',1);
							$pdf->Cell(20,5,'TVA',1,0,'C',1);
							$pdf->Cell(30,5,'Total TTC',1,1,'C',1);
							//... Détail
							$pdf->SetFont('Arial','',7);
							$pdf->SetFillColor(200);
							$pdf->SetTextColor(0);
							$pdf->Cell(92,5,$txtbarloc,1,0,'L',1);
							$pdf->SetFont('Arial','',8);
							$pdf->Cell(20,5,$pdtlbl_qte,1,0,'C',1);
	
							// BAREME APPLICABLE
							$totbarht=$tothtloc+$tothtcaut+$tothtass+$tothtcons1+$tothtcons2;
							$txt=format_num($totbarht,2);
							$txt.=" ".$lang['symbole_monnaie_txt'];
							$pdf->Cell(30,5,$txt,1,0,'R',1);
							$totbartva=$tottvaloc+$tottvacaut+$tottvaass+$tottvacons1+$tottvacons2;
							$txt=format_num($totbartva,2);
							$txt.=" ".$lang['symbole_monnaie_txt'];
							$pdf->Cell(20,5,$txt,1,0,'R',1);
							$totbarttc=$totttcloc+$totttccaut+$totttcass+$totttccons1+$totttccons2;
							$txt=format_num($totbarttc,2);
							$txt.=" ".$lang['symbole_monnaie_txt'];
							$pdf->Cell(30,5,$txt,1,1,'R',1);
							
							// PENALITE
							$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
							mysql_select_db(DBNAME);
							$rech2 = "SELECT * FROM `" .TBPDTLPL. "` WHERE `pdtlpl_idpdtl` = '$pdtl_id'";
							$rs2 = mysql_query($rech2)or die(mysql_error());
							if (mysql_num_rows($rs2) > 0)
							{
								if ($row2 = mysql_fetch_array($rs2))
									extract($row2);
							}

							// Calcul montant pénalité
							$penht=$pdtlpl_valeur;
							$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlpl_codetva."'";
							$rs2 = mysql_query($rech2) or die(mysql_error());
							if ($row2 = mysql_fetch_assoc($rs2))
								extract($row2);
							$txtva=$tva_tx/100;
							$penttc=round(($penht*(1+$txtva))*100)/100;
							$pentva=$penttc-$penht;
				
							if ($pdtlpl_valeur<>0) // On affiche que si pénalité non nulle
							{
								// Affichage pénalité
								$pdf->SetFont('Arial','',7);
								$pdf->SetFillColor(220);
								$pdf->SetTextColor(0);
								$pdf->Cell(92,5,'Pénalité sur retard',1,0,'L',1);
								$pdf->SetFont('Arial','',8);
								$pdf->Cell(20,5,'1',1,0,'C',1);
								$txt=format_num($penht,2);
								$txt.=" ".$lang['symbole_monnaie_txt'];
								$pdf->Cell(30,5,$txt,1,0,'R',1);
								$txt=format_num($pentva,2);
								$txt.=" ".$lang['symbole_monnaie_txt'];
								$pdf->Cell(20,5,$txt,1,0,'R',1);
								$txt=format_num($penttc,2);
								$txt.=" ".$lang['symbole_monnaie_txt'];
								$pdf->Cell(30,5,$txt,1,1,'R',1);
							}


							// CONSOMMABLES
							$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
							mysql_select_db(DBNAME);
							// ...On vérifie s'il n'y a pas déjà un montant consommables enregistré
							$rech2 = "SELECT * FROM `" .TBPDTLCL. "` WHERE `pdtlcl_idpdtl` = '$pdtl_id'";
							$rs2 = mysql_query($rech2)or die(mysql_error());
							if (mysql_num_rows($rs2) > 0)
							{
								if ($row2 = mysql_fetch_array($rs2))
									extract($row2);
							}

							// Calcul montant pénalité
							$consht=$pdtlcl_valeur;
							$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlpl_codetva."'";
							$rs2 = mysql_query($rech2) or die(mysql_error());
							if ($row2 = mysql_fetch_assoc($rs2))
								extract($row2);
							$txtva=$tva_tx/100;
							$consttc=round(($consht*(1+$txtva))*100)/100;
							$constva=$consttc-$consht;
				
							if ($pdtlcl_valeur<>0) // On affiche que si montant non nul
							{
								// Affichage consommables
								$pdf->SetFont('Arial','',7);
								$pdf->SetFillColor(200);
								$pdf->SetTextColor(0);
								$pdf->Cell(92,5,'Consommables en sus',1,0,'L',1);
								$pdf->SetFont('Arial','',8);
								$pdf->Cell(20,5,'1',1,0,'C',1);
								$txt=format_num($consht,2);
								$txt.=" ".$lang['symbole_monnaie_txt'];
								$pdf->Cell(30,5,$txt,1,0,'R',1);
								$txt=format_num($constva,2);
								$txt.=" ".$lang['symbole_monnaie_txt'];
								$pdf->Cell(20,5,$txt,1,0,'R',1);
								$txt=format_num($consttc,2);
								$txt.=" ".$lang['symbole_monnaie_txt'];
								$pdf->Cell(30,5,$txt,1,1,'R',1);
							}

							mysql_free_result($rs2);
							mysql_close($conn);

							// Totaux généraux
							$pdf->SetFont('Arial','BI',8);
							$pdf->SetFillColor(255);
							$pdf->SetTextColor(0);
							$pdf->Cell(92,5,'Total général',1,0,'R',1);
							$pdf->Cell(20,5,'',1,0,'C',1);
							$txt=format_num(($totbarht+$penht+$consht),2);
							$txt.=" ".$lang['symbole_monnaie_txt'];
							$pdf->Cell(30,5,$txt,1,0,'R',1);
							$txt=format_num(($totbartva+$pentva+$constva),2);
							$txt.=" ".$lang['symbole_monnaie_txt'];
							$pdf->Cell(20,5,$txt,1,0,'R',1);
							$txt=format_num(($totbarttc+$penttc+$consttc),2);
							$txt.=" ".$lang['symbole_monnaie_txt'];
							$pdf->Cell(30,5,$txt,1,1,'R',1);

						}
				/*
						//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('loc_'.$coloc_code,'I');
					}
				}
				else
				{
					$pbm_locpdf = TRUE;
				}			
			}
			else
			{
				$pbm_locpdf = TRUE;
			}			
		}
		else
		{
			$pbm_locpdf = TRUE;
		}			
	}
	else
	{
		$pbm_locpdf = TRUE;
	}			
}
else
{
	$pbm_locpdf = TRUE;
}

if ($pbm_locpdf == TRUE)
{
?>
	<script type="text/javascript">
	<!--
	self.close('');
	//-->
	</script>
<?PHP
}
?>
