<?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
$niv_util = @$_SESSION["niv_log"];
$cgv = TRUE;
$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);
			
			// 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)
			{
				$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('Contrat Location N°'.$coloc_code);
				$pdf->SetSubject('Contrat Location N°'.$coloc_code);
				$pdf->settheader("col");
				$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;
							if (trim($cli_tvai)<>"")
							{
								$ent_cli.= "\nTVA : ".$cli_tvai;
								$nl_ent=$nl_ent-1;
							}
							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;
							if (trim($age_tvai)<>"")
							{
								$ent_cli.= "\nTVA : ".$age_tvai;
								$nl_ent=$nl_ent-1;
							}
							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;
							if (trim($st_tvai)<>"")
							{
								$ent_cli.= "\nTVA : ".$st_tvai;
								$nl_ent=$nl_ent-1;
							}
							break;
					}
					if ($nl_ent>0)
					{
						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 (trim($scli_tvai)<>"")
					{
						$ent_scli.= "\nTVA : ".$scli_tvai;
						$nl_ent=$nl_ent-1;
					}
					if ($nl_ent>0)
					{
						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 = "Agence :\n";
				$ent_loc.= "Fin prévue le :\n";
				for ($nl=1; $nl<6; $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_codeage."\n";
				$ent_loc.= $coloc_datefinf."\n";
				for ($nl=1; $nl<6; $nl++)
				{
					$ent_loc.= "\n";
				}
		
				$pdf->MultiCell(73,4,$ent_loc,'BR','L');
				
				$pdf->ln(2);
				
				$pdf->SetLineWidth(0.2);
				$pdf->SetDrawColor(220);
		
				// Détail de la prise en charge
	
				$pdf->SetFont('Arial','B',10);
				$pdf->Cell(40,5,'Demandé par : ',0,0,'R');
				$pdf->SetFont('Arial','',8);
				$txt_ccli="Pas de contact";
				switch (intval($coloc_ddeur))
				{
					case -1 :
						$txt_ccli = $cli_nom." ".$cli_prenom." (Contact principal";
						$txt_ccli.= " - ".$cli_telf.")";
						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";
							$txt_ccli.= " - ".$scli_telf.")";
						}
						break;
					default :
						$row = lit_enr_ccli_id_in_cli($coloc_ddeur,$cli_code);
						if (is_array($row))
						{
							extract ($row);
							$row2=lit_enr_fonc($ccli_codefonc,TRUE);
							if (is_array($row2))
							{
								extract($row2);
								$txt_fonc=$fonc_desc;
							}
							else
								$txt_fonc=$ccli_codefonc;
							$txt_ccli = $ccli_nom." ".$ccli_prenom." (".$txt_fonc." - ".$ccli_telf.")";
						}
						break;
				}
				$pdf->MultiCell(150,4,$txt_ccli,1,'L');
		
				$pdf->ln(2);
	
				if ($nbartloc <> 0) // S'il y a des articles facturés, on les affiche
				{ 
					$pdf->SetFont('Arial','BU',12);
					$pdf->Cell(180,5,'ARTICLES RESERVES OU LOUES','',1,'C');
					$pdf->SetFont('Arial','I',8);
					$pdf->Cell(180,4,'Tarifs comprenant les éventuelles cautions','',1,'C');
					$pdf->SetFont('Arial','I',8);
					$pdf->Cell(180,4,'(P) : Produit parti - (R) : Produit revenu','',1,'C');
	
					// Type de facturation
					$factTTC=TRUE;
					switch ($coloc_typcli)
					{
						case 0 :
							if (($cli_modfact=="0") OR ($cli_modfact=="2"))
								$factTTC=FALSE;
							break;
						case 1 :
						case 3 :
							$factTTC=FALSE;
							break;
					}
	
					$pdf->SetFont('Arial','B',8);
					$pdf->SetFillColor(0);
					$pdf->SetTextColor(255);
		
					$pdf->Cell(85,5,'Désignation',1,0,'C',1);
					$pdf->Cell(35,5,'Du',1,0,'C',1);
					$pdf->Cell(35,5,'Au',1,0,'C',1);
					if ($factTTC==TRUE)
						$txt= "TOTAL TTC";
					else
						$txt= "TOTAL HT";
					$pdf->Cell(35,5,$txt,1,1,'C',1);
		
					$pdf->SetFillColor(255);
					$pdf->SetTextColor(0);
		
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
	
					$rech = "SELECT * FROM ".TBPDTL." WHERE pdtl_codecoloc='".$cl."' ORDER BY `pdtl_codepdtloc`";
					$rs = mysql_query($rech) or die(mysql_error());
						
					$i = 0;
		
					$pdf->SetFont('Arial','',8);
		
					while ($row = mysql_fetch_assoc($rs)) 
					{         
						extract($row);
						
						// Couleurs alternées
						if($i%2 == 0)  
							$pdf->SetFillColor(200);
						else
							$pdf->SetFillColor(255);
					
						$pdf->SetFont('Arial','',8);
	
						// Désignation
						$rech2 = "SELECT pdtloc_des FROM ".TBPDTLOC." WHERE pdtloc_code='".$pdtl_codepdtloc."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if ($row2 = mysql_fetch_assoc($rs2))
							extract($row2);
						// Récupération enregistrement barême location du produit dans ce contrat
						$rech2 = "SELECT * FROM ".TBPDTLBL." WHERE pdtlbl_idpdtl='".$pdtl_id."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if ($row2 = mysql_fetch_assoc($rs2))
							extract($row2);
						if ($pdtlbl_idbarloc==0)
							$pdtloc_des.=" (prêt)";
						if (formatedate($pdtl_datedep)<>"")
							$pdtloc_des.=" (P)";
						if (formatedate($pdtl_dateret)<>"")
							$pdtloc_des.=" (R)";
						$pdf->Cell(85,5,$pdtloc_des,'LR',0,'L',1);
	
						// Du...
						$pdf->Cell(35,5,formatedate($pdtl_datedeb)." (".substr($pdtl_heuredeb,0,5).")",'LR',0,'C',1);
	
						// Au...
						$pdf->Cell(35,5,formatedate($pdtl_datefin)." (".substr($pdtl_heurefin,0,5).")",'LR',0,'C',1);
	
						// 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;
			
						// Total
						if ($factTTC==TRUE)
							$txt=format_num(($totttcloc+$totttccaut+$totttcass+$totttccons1+$totttccons2),2);
						else
							$txt=format_num(($tothtloc+$tothtcaut+$tothtass+$tothtcons1+$tothtcons2),2);
						$txt.=" ".$lang['symbole_monnaie_txt'];
	
						$pdf->Cell(35,5,$txt,'LR',1,'R',1);
						$i++;
					}  
	
					mysql_free_result($rs);
					mysql_close($conn);
	
					$PosY = $pdf->GetY();
					$pdf->Line(10,$PosY,200,$PosY); // On "ferme" le tableau
	
				}
		
				if ($cgv == TRUE)
				{
					$pdf->AddPage();
					
					$txt_entete="";
					$txt_texte="";
					$row = lit_enr_txt("CGV_LOC");
					if (is_array($row))
						extract ($row);
		
					$pdf->SetFont('Arial','BU',10);
					
					$txt_ent = parse_txtcorps("cdg","",$txt_entete,"");
					$pdf->MultiCell(190,5,$txt_ent."\n",0,'C');
		
					$txt_texte = formatetxt($txt_texte);
					$txt_cgl = parse_txtcorps("age",$coloc_codeage,$txt_texte,"");
					$txt_cgl = parse_txtcorps("cli",$coloc_codecli,$txt_cgl,"");
					$txt_cgl = parse_txtcorps("cdg","",$txt_cgl,"");
					$txt_cgl = parse_txtcorps("col",$coloc_code,$txt_cgl,"")."<BR>"; // BR important pour retour à la ligne final
	
					$pdf->SetFont('Arial','',10);
					$pdf->WriteHTML($txt_cgl);
				}
				$pdf->Output('loc_'.$coloc_code,'I');
			}
		}
		else
		{
			$pbm_locpdf = TRUE;
		}			
	}
	else
	{
		$pbm_locpdf = TRUE;
	}			
}
else
{
	$pbm_locpdf = TRUE;
}

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