<?php
/* Copyright (C) 2007-2008 Jrmie Ollivier <jeremie.o@laposte.net>
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
	
	include_once(DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php');
	include_once(DOL_DOCUMENT_ROOT.'/etiquettes/class/substituer.class.php');
	
	global $conf, $mysoc;
	
	$modele = $_SESSION['etiquettes_modele'];
	
	$dom = new DomDocument();
	$dom->load(DOL_DOCUMENT_ROOT.'/etiquettes/modeles/produits/'.$modele.'/etiquettes.xml');
	
	// Paramtres contenus dans le template ---------------------------
	$parametres = $dom->getElementsByTagName('parametres')->item(0);
	$params = $parametres->getElementsByTagName('param');
	
	foreach ( $params as $param ) {
	
		if ( $param->hasAttribute('id') ) {
			
			$id = $param->getAttribute('id');
			$val = $param->getAttribute('val');
			$$id = $val;
			
		}
	
	}
	// --------------------------------------------------------------
	
	
	global $conf;
	
	$pdf = new FPDF ('P', 'mm', $format_page);
	$pdf->AddPage ();

	$pdf->SetLeftMargin ($marges_x);
	$pdf->SetRightMargin ($marges_x);
	
	// Police par dfaut
	$pdf->SetFont ('Arial', '', 8);
	
	$nbr_par_page = $nbr_x * $nbr_y;	// Nombre d'tiquettes par page

	$indicex = 0;
	$indicey = 0;
	$compteur = 1;

	while ( $obj = $this->db->fetch_object($res) ) {
	
		$boucles = $obj->nbre;
		
		// Rcupration des proprits du produit
		include_once(DOL_DOCUMENT_ROOT.'/product/class/product.class.php');

		$prod = new Product($this->db);
		$prod->fetch($obj->fk_produit);
		$prod->fetch_barcode();

		// Remplissage des variables de substitution -------------------
		$var = array(
			'__NOM-SOCIETE__' => $conf->global->MAIN_INFO_SOCIETE_NOM,
			'__CODE-PRODUIT__' => $prod->ref,
			'__NOM-PRODUIT__' => $prod->libelle,
			'__DESCRIPTION-PRODUIT__' => $prod->description,
			'__ID-PRODUIT__' => $prod->id,
			'__PRIX-HT__' => $prod->price,
			'__PRIX-TTC__' => $prod->price_ttc,
			'__TAUX-TVA__' => $prod->tva_tx

		);
		$subst = new Substituer($db, $var);
		// -------------------------------------------------------------
		
		for ( $i = 1; $i <= $boucles; $i++ ) {
		
			if ( $indicex > $nbr_x-1) {
			
				$indicex = 0;
				$indicey++;
				
				if ( $indicey > $nbr_y-1) {
				
					$indicey = 0;
				
				}
	
			}
	
			if ( $compteur > ($nbr_par_page) ) {
			
				$pdf->AddPage ();
				$compteur = 1;
			
			}

			// Dfinition des marges internes
			$marge_int_x = $indicex > 0 ? $marges_int_x : 0;
			$marge_int_y = $indicey > 0 ? $marges_int_y : 0;
			
			$posx = $larg*$indicex+$marges_x+$marge_int_x;
			$posy = $haut*$indicey+$marges_y+$marge_int_y;
			
			
			// Cadre entourant l'tiquette
			if ( $cadre == 'true' ) {
			
				$pdf->SetXY($posx, $posy);
				$pdf->Cell ($larg, $haut, '', 1, 0);
			
			}
			
			
			// Construction  partir du template XML
			$elements = $dom->getElementsByTagName('elements')->item(0);
			$elems = $elements->getElementsByTagName('element');
			
			foreach ( $elems as $elem ) {
			
				// Paramtres communs  tous les lments
				$type = $elem->getAttribute('type');
				$x = $elem->getAttribute('posx');
				$y = $elem->getAttribute('posy');
				$l = $elem->getAttribute('larg');
				$h = $elem->getAttribute('haut');
				$desc = $elem->getAttribute('desc');
				
				// Affichage de l'lment en fonction du type
				switch ($type) {
				
					case 'Cell':
						$taille = $elem->getAttribute('taille');
						$aspect = $elem->getAttribute('aspect');
						$police = $elem->getAttribute('police');
						$align = $elem->getAttribute('alignement');
						$txt = utf8_decode($subst->sub($elem->nodeValue));
						$coupure = $elem->getAttribute('coupure');
						$format = $elem->getAttribute('format');
						
						// Gestion de la coupure
						if ( $coupure && $coupure > 0 ) {
							
							if ( strlen($txt) > $coupure ) {
							
								$txt = substr($txt, 0, $coupure - 3).'...';
							
							}
						
						}
						
						// Gestion du format
						if ( $format ) {
						
							switch ( $format ) {
							
								case 'prix':
									$txt = price($txt).' '.$conf->currency;
								break;
							
							}
						
						}
						
						$pdf->SetXY ($posx + $x, $posy + $y);
						$pdf->SetFont ($police, $aspect, $taille);
						$pdf->Cell ($l, $h, $txt, 0, 0, $align);
					break;
				
					case 'Codebarre':
						// Affichage du code barres
						$gencode = $etiquette->recup_barcode($prod->id);
						$image = $dossier_temporaire.'/barcode_'.$prod->barcode.'_'.$prod->barcode_type_code.'.png';
						
						if ( file_exists($image) ) $pdf->Image ($image, $posx+$x, $posy+$y, $l, $h);
					break;
				
					case 'Logo':
						// Affichage du logo de l'entreprise
						$logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
						
						if ( file_exists($logo) ) $pdf->Image ($logo, $posx+$x, $posy+$y, $l, $h);
					break;
				
				}
				
			}
			
			
			// On remet le texte  sa position d'origine
			$pdf->SetXY ($posx, $posy);
			$pdf->SetFont ('Arial', 'B', 8);
	
	
	
			// Incrmentations des compteurs
			$indicex++;
			$compteur++;
		
		}
	
	}
	
?>
