<?php

	/**
	 * FPDF Template für die Generierung des Lieferscheins
	 */

	require_once WPSG_PATH_LIB.'fpdf/fpdf.php';
	require_once WPSG_PATH_LIB.'fpdf/fpdi.php';
	require_once WPSG_PATH_LIB.'wpsg_fpdf.class.php';
	
	global $absender_left, $absender_top, $adress_left, $adress_top, $dndata_left, $dndata_top;
			
	if (!function_exists('AddDeliverynotePage'))
	{
	
		function AddDeliverynotePage($shop, $pdf)
		{
	
			global $absender_left, $absender_top, $adress_left, $adress_top, $dndata_left, $dndata_top;
							
			$pdf->AddPage();
			
			if (file_exists($shop->callMod('wpsg_mod_deliverynote', getFilePath, array(''))."wpsg_deliverynote_bp.pdf"))
			{
				
				$pagecount = $pdf->setSourceFile($shop->callMod('wpsg_mod_deliverynote', getFilePath, array(''))."wpsg_deliverynote_bp.pdf");
				$tplidx = $pdf->importPage(1, '/MediaBox');
				$pdf->useTemplate($tplidx, 0, 0, 210);
			
			}
			else if (file_exists($shop->callMod('wpsg_mod_deliverynote', getFilePath, array(''))."wpsg_deliverynote_bp.jpg"))
			{
				
				$pdf->image($shop->callMod('wpsg_mod_deliverynote', getFilePath, array(''))."wpsg_deliverynote_bp.jpg", 0, 0, 210, 297, 'jpg');
				
			}
				
				
			if (file_exists($shop->callMod('wpsg_mod_deliverynote', getFilePath, array(''))."wpsg_deliverynote_logo.jpg"))
			{
			
				list($width, $height, $type, $attr) = getimagesize($shop->callMod('wpsg_mod_deliverynote', getFilePath, array(''))."wpsg_deliverynote_logo.jpg");
			 
				$wPix = (25.4 * $width) / 96;
				$hPix = (25.4 * $height) / 96;
			
				$pdf->image($shop->callMod('wpsg_mod_deliverynote', getFilePath, array(''))."wpsg_deliverynote_logo.jpg", 210 - $wPix, 0, $wPix, $hPix);
			
			}
				
			// Absenderadresszeile (Wird in der Konfiguration hinterlegt)
			$pdf->SetFont('Arial', '', 6);
			$pdf->Text($absender_left, $absender_top, $shop->replaceUniversalPlatzhalter($shop->get_option("wpsg_mod_deliverynote_adressrow"), $shop->view['order']['id']));
			
			if ($shop->view['kunde']['kuerzel'] != "") $shop->view['kunde']['kuerzel'] = $shop->view['kunde']['kuerzel'].'-';
				
			// Adresse des Kunden
			$pdf->SetFont('Arial', '', 12);
			$pdf->Text($adress_left, $adress_top, $shop->view['data']['firma']);
			$pdf->Text($adress_left, $adress_top + 5, $shop->view['data']['vname'].' '.$shop->view['data']['name']);
			$pdf->Text($adress_left, $adress_top + 10, $shop->view['data']['strasse'].' '.$shop->view['data']['hausnr']);
			$pdf->Text($adress_left, $adress_top + 15, $shop->view['data']['plz'].' '.$shop->view['data']['ort']);
			if ($shop->get_option('wpsg_mod_deliverynote_hideCountry') != '1') $pdf->Text($adress_left, $adress_top + 20, strtoupper($shop->view['oOrder']->getShippingCountryName()));
			
			// Lieferscheindaten
			$pdf->SetFont('Arial', 'B', 16);
			$pdf->Text($dndata_left, $dndata_top, $shop->view['title']);
			$pdf->SetFont('Arial', 'B', 9);
			$pdf->Text($dndata_left, $dndata_top + 6, $shop->view['lnr']);
			$pdf->SetFont('Arial', '', 9);
			
			$pdf->Text($rdata_left + 100, $dndata_top, __("Kunden-Nr", "wpsg"));
			$pdf->Text($rdata_left + 100, $dndata_top + 6, (($shop->view['customer']['knr'] != '')?$shop->view['customer']['knr']:$shop->view['customer']['id']));
				
			$pdf->Text($rdata_left + 125, $dndata_top, __("Best. Nr.", "wpsg"));
			$pdf->Text($rdata_left + 125, $dndata_top + 6, ((trim($shop->view['order']['onr']) != '')?$shop->view['order']['onr']:$shop->view['order']['id']));
			
			$pdf->Text($rdata_left + 145, $dndata_top, __("Bestelldatum", "wpsg"));
			$pdf->Text($rdata_left + 145, $dndata_top + 6, wpsg_formatTimestamp(strtotime($shop->view['order']['cdate']), true));
				
			$pdf->Text($rdata_left + 170, $dndata_top, __("Lieferdatum", "wpsg"));
			$pdf->Text($rdata_left + 170, $dndata_top + 6, wpsg_formatTimestamp($shop->view['lDatum'], true));
			
			if ($shop->view['oOrder']->isInnerEu())
			{
					
				$pdf->SetFont('Arial', '', 9);
				$pdf->Text($adress_left, $dndata_top + 11.5, __("Innergemeinschaftliche Lieferung.", "wpsg"));
			
			}
			
			// Benutzerdefinierte Felder
			$arTexte = $shop->callMod('wpsg_mod_deliverynote', 'getTexte', array($shop->view['data']['id'])); 
			
			foreach ((array)$arTexte as $text)
			{
			
				if (isset($text['aktiv']) && $text['aktiv'] == 1)
				{
						
					$pdf->SetFont('Arial', 'B', ((intval($text['fontsize']) > 0)?intval($text['fontsize']):10));
					$pdf->SetTextColor($text['color']);
					$pdf->MultiCell($text['x'], $text['y'], 5, utf8_encode($text['text']));
					$pdf->SetTextColor("#000000");
						
				}
			
			}
			
		}
		
	}
	
	// Positionierung der Absenderadresszeile
	$absender_left				= 25;
	$absender_top				= 50;
	
	// Positionierung der Zieladress
	$adress_left 				= 25;
	$adress_top					= 55;
	
	// Positionierund des Lieferscheinkopfes
	$dndata_left				= 25;
	$dndata_top					= 90;
	
	// Positionierung der Produktdaten
	$prod_left					= 25;
	$prod_top					= 105;
	
	// Anzahl an Produkten pro Seite
	$prod_break					= 10;
	
	$pdf = new wpsg_fpdf();
	$pdf->SetAutoPageBreak(true, 5);
	AddDeliverynotePage($this, $pdf);

	$filename = $this->view['filename'].".pdf";
	
	$bKopf = false; $pnr = 1; $offset = 0; $count = 0;
	foreach ($this->view['data']['products'] as $p)
	{
		
		$product_data = $this->loadProduktArray($this->getProduktID($p['p_id']));
			
		if (!$bKopf)
		{
	
			$pdf->SetFont('Arial', 'B', 9);
			$pdf->setXY($prod_left, $prod_top);
			$pdf->Cell(10, 8, __("Nr.", "wpsg"), 1, 0, 'C');
	
			$pdf->setXY($prod_left + 10, $prod_top);
			$pdf->Cell(137, 8, "Name", 1, 0, 'L');
							
			$pdf->setXY($prod_left + 147, $prod_top);
			$pdf->Cell(15, 8, __("Menge", "wpsg"), 1, 0, 'C');
	
			$offset = 8;
			$bKopf = true;
				
		}

		/*
		if ($this->hasMod('wpsg_mod_varianten') && isset($p['mod_vp_varkey']))
		{
		
			$variante = $this->callMod('wpsg_mod_varianten', 'getVariantenInfoArray', array($p['mod_vp_varkey']));
			$anr = $variante['akey'];
			
		}
		else
		{
			
			$anr = $product_data['anr'];
			
		}*/
		 
		$pdf->SetFont('Arial', '', 9);		
		$pdf->setXY($prod_left, $prod_top + $offset);
		
		if ($this->get_option('wpsg_mod_deliverynote_anr') === '1')
		{

			$anr = $this->getProductAnr($p['productkey']);
			$pdf->Cell(10, 8, $anr, 0, 0, 'C'); /* Artikelnummer anzeigen */
			
		}
		else
		{
		
			$pdf->Cell(10, 8, $pnr.".", 0, 0, 'C');
			
		}
		
		$produkt_text = $this->getProductName($this->getProduktID($p['p_id']), true);
		$produktBeschreibung = trim(strip_tags($p['beschreibung']));
		
		if ($this->isOtherLang())
		{
				
			$trans_db = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_PRODUCTS."` WHERE `lang_parent` = '".wpsg_q($this->getProduktID($p['productkey']))."' AND `lang_locale` = '".wpsg_q(get_locale())."'");
				
			if (is_array($trans_db) && sizeof($trans_db) > 0)
			{
		
				$produkt_text = $trans_db['name'];
				$produktBeschreibung = trim(strip_tags($trans_db['beschreibung']));
					
			}
				
		}
			
		$height = 0; // Höhe der Zeile für den Rahmen
		
		$pdf->setXY($prod_left + 10, $prod_top + $offset);
		
		// Produktvariablen
		if ($this->hasMod('wpsg_mod_productvars') && $this->get_option("wpsg_mod_rechnungen_showpv") == "1")
		{
				
			$arPV = $this->callMod('wpsg_mod_productvars', 'getAllProductVarValues', array($this->view['data']['id'], $p['productkey']));
				
			if (wpsg_isSizedArray($arPV))
			{
		
				foreach ($arPV as $pv)
				{
		
					$produkt_text .= "\r\n".$pv['name'].': '.$pv['value'];
					$height += 5;
						
				}
		
			}
				
		} 
		
		if ($this->get_option('wpsg_mod_deliverynote_anr') === '2')
		{
			
			$anr = $this->getProductAnr($p['productkey']);
			$produkt_text .= "\r\n".wpsg_translate(__('Artikelnummer: #1#', 'wpsg'), $anr);
			
		}
		
		$produkt_text_cell_width = 137;		
		$produkt_text_width = $pdf->GetStringWidth($produkt_text); 
		
		$height += 5 * ceil($produkt_text_width / $produkt_text_cell_width) + 3;
		
		$height_y = $pdf->getY();
		$height = 2 + $pdf->MultiCell($prod_left + 10, $prod_top + $offset + 1.5, 5, utf8_encode($produkt_text), 0, 'L', 0, $produkt_text_cell_width);
		
		$pdf->setXY($prod_left + 147, $prod_top + $offset);
		$pdf->Cell(15, 8, $p['menge'], 0, 0, 'C');
		
		/**
		 * Produktbeschreibung anzeigen Ja/Nein
		*/
		$pBeschreibungHeight = 0;
		if ($this->get_option("wpsg_mod_deliverynote_pdf_beschreibung") == "1" && trim(strip_tags($p['beschreibung'])) != '')
		{
				
			$produktBeschreibung = nl2br($produktBeschreibung);
			preg_match_all('/\<br \/\>/', $produktBeschreibung, $treffer);
			$produktBeschreibung = strip_tags($produktBeschreibung);
				
			$pBeschreibungWidth = $pdf->getStringWidth($produktBeschreibung);
			$pBeschreibungHeight = 5 * (ceil($pBeschreibungWidth / $produkt_text_cell_width) + @sizeof($treffer[0]));
				
			$pdf->SetFont('Arial', 'I', 9);
				
			$cellY = $pdf->getY();
			$pdf->MultiCell($prod_left + 10, $prod_top + $height + $offset - 1, 5, utf8_encode($produktBeschreibung), 0, 'L', 0, $produkt_text_cell_width);
			$pBeschreibungHeight = $pdf->getY() - $cellY - $height + 1;
			$pdf->SetFont('Arial', '', 9);
				
		}
			
		$height += $pBeschreibungHeight;
		
		/**
		 * Variante ?
		 */
		if ($this->get_option("wpsg_mod_deliverynote_pdf_varianten") == "1" && preg_match('/vp_(.*)/', $p['productkey']))
		{
				
			$variInfo = $this->callMod('wpsg_mod_varianten', 'getVariantenInfoArray', array($p['productkey']));
		
			$height += $pdf->MultiCell($prod_left + 10, $prod_top + $height + $offset - 1.5, 5, utf8_encode(wpsg_translate(__('Variante: #1#', 'wpsg'), $variInfo['key'])), 0, 'L', 0, $produkt_text_cell_width);
				 
		}
		
		// Jetzt die Rahmen zeichnen
		$pdf->Rect($prod_left, $prod_top + $offset, 10, $height);		
		$pdf->Rect($prod_left + 10, $prod_top + $offset, 137, $height);		 		
		$pdf->Rect($prod_left + 147, $prod_top + $offset, 15, $height);		 
		
		$offset += $height;
		 
		$pnr ++;
		$count ++;
		
		$summe += $p['price'] * $p['menge'];
		
		if ($pdf->getY() > 220 || ($count >= $prod_break && sizeof($this->view['data']['products']) > ($pnr - 1)))
		{
				
			AddDeliverynotePage($this, $pdf);
			$bKopf = false; $offset = 0; $count = 0;
				
		}
		
	}
	
	$offset += 5;
	
	if ($this->view['fussText'] != "")
	{
	
		$pdf->MultiCell($prod_left - 1, $prod_top + $offset, 5, utf8_encode($this->view['fussText']));
		$pdf->SetFont('Arial', 'B', 9);
		$offset += 10;
	
	}
	
	if ($this->view['preview'])
	{
		$pdf->Output($this->view['filename'], 'I');
	}
	else
	{
	
		$pdf->Output($this->callMod('wpsg_mod_deliverynote', 'getFilePath', array($this->view['order']['id'])).$this->view['filename'], 'F');
	
		if ($this->view['output'] === true) $pdf->Output($filename, 'I');
			
	}
	
?>