<?php

	/**
	 * Klasse, die es ermöglicht Gutscheine zu kaufen
	 */
	class wpsg_mod_voucherproduct extends wpsg_mod_basic
	{
		
		var $id = 161;		
		var $lizenz = 1;
		var $version = "9.9.9";
		var $free = false;
		var $hilfeURL = 'http://wpshopgermany.maennchen1.de/?p=2908';		
		
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('GutscheinProdukt', 'wpsg');
			$this->group = __('Produkte', 'wpsg');
			$this->desc = __('Erlaubt das Verkaufen von Gutscheinen.', 'wpsg');
			
		} // public function __construct()
		
		public function install()
		{
			
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
						
			$sql = "CREATE TABLE ".WPSG_TBL_PRODUCTS." (
			  	wpsg_mod_voucherproduct_gs INT(11) NOT NULL,			  	
			  	wpsg_mod_voucherproduct_gslaenge INT(11) NOT NULL,
			  	wpsg_mod_voucherproduct_available INT(11) NOT NULL
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
			
			dbDelta($sql);
			
			// Grundeinstellungen
			$this->shop->checkDefault('wpsg_gutscheinproductmail_betreff', __('Ihr gekaufter Gutschein', 'wpsg'), false, true);
			$this->shop->checkDefault('wpsg_voucherproduct_width', '210');
			$this->shop->checkDefault('wpsg_voucherproduct_height', '297');
			
		} // public function install()
		
		public function be_ajax() 
		{ 
			
			if ($_REQUEST['wpsg_mod_action'] == 'getBP')
			{
				
				if (file_exists($this->getFilePath('').'/bp.pdf'))
				{
					
					header('Content-type: application/pdf');
					header('Content-Disposition: attachment; filename="bp.pdf"');
					readfile($this->getFilePath('').'/bp.pdf');
					
				}
				else if (file_exists($this->getFilePath('').'/bp.jpg'))
				{
					
					header('Content-type: image/jpeg');
					header('Content-Disposition: attachment; filename="bp.jpg"');
					readfile($this->getFilePath('').'/bp.jpg');
					
				}
				else 
				{
					
					die(__('Aufrufsfehler!', 'wpsg'));
					
				}
				
			}
			
		} // public function be_ajax()
		
		public function settings_edit()
		{
			
			if (file_exists($this->getFilePath('').'/bp.jpg') || file_exists($this->getFilePath('').'/bp.pdf'))
			{
				$this->shop->view['bp'] = WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Admin&action=module&modul=wpsg_mod_voucherproduct&noheader=1&wpsg_mod_action=getBP';
			}
			else
			{
				$this->shop->view['bp'] = false;
			}
			
			$this->shop->view['arTexte'] = $this->shop->get_option('wpsg_mod_voucherproduct_texte');
			if (!is_array($this->shop->view['arTexte'])) $this->shop->view['arTexte'] = array();
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save() 
		{
			
			$this->shop->update_option('wpsg_mod_voucherproduct_texte', $_REQUEST['text']);
			$this->shop->update_option('wpsg_voucherproduct_width', $_REQUEST['wpsg_voucherproduct_width']);
			$this->shop->update_option('wpsg_voucherproduct_height', $_REQUEST['wpsg_voucherproduct_height']);
			
			// Briefpapier
			if (file_exists($_FILES['wpsg_mod_voucherproduct_bp']['tmp_name']))
			{
				
				$ending = strtolower(preg_replace("/(.*)\./", "", $_FILES['wpsg_mod_voucherproduct_bp']['name']));
				
				if ($ending != "jpg" && $ending != "jpeg" && $ending != "pdf")
				{
					$this->shop->addBackendError(__("Ungültiger Dateityp (Briefpapier) ! Es sind nur JPG und PDF Dateien erlaubt!", "wpsg"));
				}
				else if ($ending == "jpg" || $ending == "jpeg")
				{	

					@unlink($this->getFilePath('').'bp.jpg');
					@unlink($this->getFilePath('').'bp.pdf');
					
					move_uploaded_file($_FILES['wpsg_mod_voucherproduct_bp']['tmp_name'], $this->getFilePath('').'bp.jpg');
										
				} 
				else if ($ending == "pdf")
				{
				
					@unlink($this->getFilePath('').'bp.jpg');
					@unlink($this->getFilePath('').'bp.pdf');
					
					move_uploaded_file($_FILES['wpsg_mod_voucherproduct_bp']['tmp_name'], $this->getFilePath('').'bp.pdf');
					
				}
					
			}
			else if ($_REQUEST['wpsg_rechnungen_bp_del'] == "1")
			{

				@unlink($this->getFilePath('').'bp.jpg');
				@unlink($this->getFilePath('').'bp.pdf');
				
			}
			
			if (isset($_REQUEST['submit-preview']))
			{
				
				$this->genGSDokument('TESTCODE', '-1', '-1', true, 0);
				
			}
			
		} // public function settings_save()
		
		public function produkt_save_before(&$produkt_data) 
		{ 
			
			$produkt_data['wpsg_mod_voucherproduct_gs'] = wpsg_q($_REQUEST['wpsg_mod_voucherproduct_gs']);			
			$produkt_data['wpsg_mod_voucherproduct_gslaenge'] = intval($_REQUEST['wpsg_mod_voucherproduct_gslaenge']);
			$produkt_data['wpsg_mod_voucherproduct_available'] = intval($_REQUEST['wpsg_mod_voucherproduct_available']);
			
		} // public function produkt_save_before(&$produkt_data)
		
		public function setOrderStatus($order_id, $status_id, $inform) 
		{ 
			
			if ($status_id == 100) // Zahlung eingegangen
			{
				
				$order_data = $this->shop->cache->loadOrder($order_id);
				if (trim($order_data['language']) != '') $this->shop->setTempLocale($order_data['language']);
				
				// Müsste eventuell umgebaut werden, da es nicht die Variante der Bestellung beachten kann. Geht aber vorerst so
				$arProdukte = $this->db->fetchAssoc("
					SELECT
						P.*,
						OP.`id` AS order_product_id,
						OP.`product_index`,
						OP.`menge`,
						OP.`price`
					FROM
						`".WPSG_TBL_ORDERPRODUCT."` AS OP
							LEFT JOIN `".WPSG_TBL_PRODUCTS."` AS P ON (OP.`p_id` = P.`id`)
					WHERE
						OP.`o_id` = '".wpsg_q($order_id)."'
				");
				 
				$arGSFiles = array();
				
				$this->shop->view['order'] = $this->shop->cache->loadOrder($order_id);
				
				foreach ($arProdukte as $p)
				{
					
					if ($p['wpsg_mod_voucherproduct_gs'] == '1')
					{
						
						$produkt_data = $this->shop->loadProduktArray($p['id']);
						
						for ($i = 1; $i <= $p['menge']; $i ++)
						{
													 
							$code = $this->shop->callMod('wpsg_mod_gutschein', 'genGS', array(
								$p['price'],
								'w',
								time(), 
								time() + (86400 * $p['wpsg_mod_voucherproduct_available']), 
								0, 
								$p['wpsg_mod_voucherproduct_gslaenge'],
								wpsg_translate(__('Automatisch Erstellt aus Bestellung #1# / Produkt #2#', 'wpsg'), $this->shop->view['order']['onr'], $produkt_data['name']),
								$order_id,
								$p['id'],
								$p['order_product_id']
							));
							
							$arGSFiles[] = array(
								'file' => $this->genGSDokument($code, $order_id, $p['id'], false, $p['product_index']),
								'code' => $code,
								'product_id' => $p['id'],
								'product_data' => $produkt_data
							);
							
						}
						
					}
					
				}
				
				// Mail nur senden wenn Gutscheine generiert und der Kunde benachrichtig werden soll
				if (sizeof($arGSFiles) > 0 && $inform === true)
				{
					
					$this->shop->view['arGS'] = $arGSFiles;
					$this->shop->view['order_id'] = $order_id;					
					$this->shop->view['customer'] = $this->shop->cache->loadKunden($this->shop->view['order']['k_id']); 
					$this->shop->view['basket'] = $this->shop->cache->loadBasketArray($order_id);
					
					$mail_text = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/gutschein_mail.phtml', false);
				
					if ($this->shop->htmlMail === true)
					{
						
						$mail_html = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/gutschein_mail_html.phtml', false);
						
					}
					else 
					{
						
						$mail_html = false;
						
					}
					
					$arAnhang = array();
					foreach ($arGSFiles as $k => $v) $arAnhang[] = $v['file'];
					
					$empfaenger = $this->shop->view['basket']['checkout']['email']; 
					$this->shop->sendMail($mail_text, $empfaenger, 'gutscheinproductmail', $arAnhang, $order_id, false, $mail_html);
					
					$this->shop->restoreTempLocale();
					
					$this->db->importQuery(WPSG_TBL_OL, array(
						"cdate" => "NOW()",
						"o_id" => wpsg_q($order_id),
						"title" => wpsg_translate(__("Gutscheinmail an: #1#", "wpsg"), $empfaenger),
						"mailtext" => $nachricht
					));

					$this->shop->addBackendMessage(wpsg_translate(__('Es wurden #1# Gutscheine generiert und an den Kunden verschickt.', 'wpsg'), sizeof($arGSFiles)));
					
				}
				else if (sizeof($arGSFiles) > 0)
				{
					
					$this->shop->restoreTempLocale();
					
					$this->shop->addBackendMessage(wpsg_translate(__('Es wurden #1# Gutscheine generiert aber nicht verschickt.', 'wpsg'), sizeof($arGSFiles)));
					
				}
				
			}
			else if ($status_id == 500) // Storniert
			{
				
				$arGS = $this->db->fetchAssoc("SELECT * FROM `".WPSG_TBL_GUTSCHEIN."` WHERE `autocreate_order` = '".wpsg_q($order_id)."'");
				
				$nUsed = 0;
				$nRemoved = 0;
				
				foreach ($arGS as $gs)
				{
					
					$gs_used = $this->db->fetchOne("SELECT COUNT(*) FROM `".WPSG_TBL_ORDER."` WHERE `gs_id` = '".wpsg_q($gs['id'])."'");
					
					// Was soll ich machen wenn ein Gutschein bereits eingelöst wurde ?
					if ($gs_used > 0)
					{
						
						$nUsed ++;
						
					}
					else 
					{
						
						$nRemoved ++;
						
						// Gutschein wurde noch nicht benutzt, ich lösche ihn einfach
						$this->db->Query("DELETE FROM `".WPSG_TBL_GUTSCHEIN."` WHERE `id` = '".wpsg_q($gs['id'])."'");
						
					}
					
				}
				
				if ($nUsed > 0) $this->shop->addBackendError(wpsg_translate(__('Es wurden bereits #1# Gutscheine eingelöst, diese wurden nicht gelöscht. Bitte greifen Sie ggf. manuell ein!', 'wpsg'), $nUsed));
				if ($nRemoved > 0) $this->shop->addBackendMessage(wpsg_translate(__('Es wurden #1# Gutscheine gelöscht.', 'wpsg'), $nRemoved));
								
			}
			
		} // public function setOrderStatus($order_id, $status_id, $inform)
		
		public function produkt_edit_sidebar(&$produkt_data) 
		{ 
		
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/produkt_edit_sidebar.phtml');
			
		} // public function produkt_edit_sidebar(&$produkt_data) 
		
		public function admin_emailconf_save()
		{
			
			wpsg_saveEMailConfig("gutscheinproductmail");
 			
		} // public function admin_emailconf_save()
		
		public function admin_emailconf() 
		{ 
			
			echo wpsg_drawEMailConfig(
				'gutscheinproductmail', 
				__('Gutscheinmail', 'wpsg'), 
				__('Diese Mail bekommt der Kunde beim Kauf (Bezahlung) von Gutscheinprodukten. Die PDF Gutscheine sind im Anhang dieser Mail.', 'wpsg')); 
			
		} // public function admin_emailconf()
		
		public function order_ajax() 
		{ 
			
			if ($_REQUEST['cmd'] == 'sendVoucher')
			{
				
				$order_id = $_REQUEST['order_id'];
				
				$order_data = $this->shop->cache->loadOrder($order_id);
				if (trim($order_data['language']) != '') $this->shop->setTempLocale($order_data['language']);
				
				$arGS = $this->db->fetchAssoc("
					SELECT
						*
					FROM
						`".WPSG_TBL_GUTSCHEIN."`
					WHERE
						`autocreate_order` = '".wpsg_q($order_id)."' 
				"); 
				
				if (sizeof($arGS) <= 0)
				{
					
					$this->shop->addBackendError(__('Keine Gutscheine für die Bestellung gefunden!', 'wpsg'));
					die($this->order_view_sidebar_content($_REQUEST['order_id']));
					
				}
				
				$arGSFiles = array();
				foreach ($arGS as $gs)
				{
					
					$arGSFiles[] = array(
						'file' => $this->getFilePath($_REQUEST['order_id']).'voucher_'.$gs['code'].'.pdf',
						'code' => $gs['code'],
						'product_id' => -1,
						'product_data' => array()
					);
					
				}
								
				$this->shop->view['order'] = $this->shop->cache->loadOrder($_REQUEST['order_id']);
				$this->shop->view['arGS'] = $arGSFiles;				
				$this->shop->view['order_id'] = $_REQUEST['order_id'];					
				$this->shop->view['customer'] = $this->shop->cache->loadKunden($this->shop->view['order']['k_id']); 
				$this->shop->view['basket'] = $this->shop->cache->loadBasketArray($_REQUEST['order_id']);
				
				$mail_text = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/gutschein_mail.phtml', false);
			
				if ($this->shop->get_option('wpsg_htmlmail') === '1')
				{
					
					$mail_html = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/gutschein_mail_html.phtml', false);
					
				}
				else
				{
					
					$mail_html = false;
					
				}
				
				$arAnhang = array();
				foreach ($arGSFiles as $k => $v) $arAnhang[] = $v['file'];
				
				$empfaenger = $this->shop->view['basket']['checkout']['email']; 
				$this->shop->sendMail($mail_text, $empfaenger, 'gutscheinproductmail', $arAnhang, $order_id, false, $mail_html);
				
				$this->shop->restoreTempLocale();
				
				$this->db->importQuery(WPSG_TBL_OL, array(
					"cdate" => "NOW()",
					"o_id" => wpsg_q($_REQUEST['order_id']),
					"title" => wpsg_translate(__("Gutscheinmail an: #1#", "wpsg"), $empfaenger),
					"mailtext" => $nachricht
				));

				$this->shop->addBackendMessage(wpsg_translate(__('Es wurden #1# Gutscheine an den Kunden verschickt.', 'wpsg'), sizeof($arGSFiles)));
								
				die($this->order_view_sidebar_content($_REQUEST['order_id']));
				
			}
			else if ($_REQUEST['cmd'] == 'getVoucher')
			{
				
				$gs_data = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_GUTSCHEIN."` WHERE `id` = '".wpsg_q($_REQUEST['gs_id'])."'");
				
				if ($gs_data['id'] <= 0) die(__('Gutschein nicht gefunden!', 'wpsg'));
				
				$filename = $this->getFilePath($_REQUEST['order_id']).'voucher_'.$gs_data['code'].'.pdf';
				
				if (!file_exists($filename)) die(__('Gutscheindokument nicht gefunden!', 'wpsg'));
				
				header("Cache-Control: no-cache, must-revalidate");
				header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 
				header('Content-type: application/pdf');
				
				die(file_get_contents($filename));
				
			}
			else if ($_REQUEST['cmd'] == 'content')
			{
				
				die($this->order_view_sidebar_content($_REQUEST['order_id']));
				
			}
			
		}
		
		public function order_view_sidebar(&$order_id)
		{
			
			if ($this->hasVocherProducts($order_id))
			{
			
				$this->shop->view['wpsg_mod_voucherproduct']['order_id'] = $order_id;
							
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/order_view_sidebar.phtml');
				
			}
			
		} // public function order_view_sidebar(&$order_id)
		
		public function wpsg_mod_export_getValue(&$return, $field_value, $o_id, $p_id, $productkey, $product_index, $profil_separator) 
		{ 
			 
			if (!in_array($field_value, array('wpsg_mod_voucher_code', 'wpsg_mod_voucher_validfrom', 'wpsg_mod_voucher_validto')) || !wpsg_isSizedInt($o_id) || !wpsg_isSizedInt($p_id));
			
			$arVoucher = $this->db->fetchAssoc("
				SELECT
					*
				FROM 
					`".WPSG_TBL_GUTSCHEIN."` AS V
				WHERE
					V.`autocreate_order` = '".wpsg_q($o_id)."' AND
					V.`autocreate_product` = '".wpsg_q($p_id)."'				
			");
		
			if (!is_array($arVoucher)) return;
			
			$arCode = array();
			$arValidFrom = array();
			$arValidTo = array();
			
			foreach ($arVoucher as $v)
			{
				
				$arCode[] = $v['code'];
				$arValidFrom[] = $v['start_date'];
				$arValidTo[] = $v['end_date'];
								
			}
			
			if ($profil_separator === ',') $separator = ';'; else $separator = ',';
						
			switch ($field_value)
			{
				
				case 'wpsg_mod_voucher_code': $return = implode($separator, $arCode); break;
				case 'wpsg_mod_voucher_validfrom': $return = implode($separator, $arValidFrom); break;
				case 'wpsg_mod_voucher_validto': $return = implode($separator, $arValidTo); break;
				
			}
			
		} // public function wpsg_mod_export_getValue(&$return, $field_value, $o_id, $p_id, $productkey, $product_index, $profil_separator)
		
		public function wpsg_mod_export_loadFields(&$arFields) 
		{ 
			
			$arFields[20]['fields']['wpsg_mod_voucher_code'] = __('Gutscheinprodukt: Generierter Code', 'wpsg');
			$arFields[20]['fields']['wpsg_mod_voucher_validfrom'] = __('Gutscheinprodukt: Gültig ab', 'wpsg');
			$arFields[20]['fields']['wpsg_mod_voucher_validto'] = __('Gutscheinprodukt: Gültig bis', 'wpsg');
			
		} // public function wpsg_mod_export_loadFields(&$arFields)
		
		/* Modulfunktionen */
		
		/**
		 * Gibt true zurück wenn zu dem Gutschein eine PDF Datei existiert, sonst false
		 */
		public function hasFile($order_id, $gs_id)
		{
			
			$gs_data = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_GUTSCHEIN."` WHERE `id` = '".wpsg_q($gs_id)."'");
			
			if ($gs_data['id'] <= 0) return false;
			
			if (file_exists($this->getFilePath($order_id).'voucher_'.$gs_data['code'].'.pdf')) return true;
						
			return false;
			
		} // public function hasFile($order_id, $gs_id)
		
		/**
		 * Gibt die DownloadURL für ein Produkt innerhalb des Backends zurück
		 */
		public function getBackendDownloadURL($order_id, $gs_id)
		{
			
			return WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Order&amp;action=ajax&amp;mod=wpsg_mod_voucherproduct&amp;gs_id='.$gs_id.'&amp;order_id='.$order_id.'&amp;noheader=1&amp;cmd=getVoucher';
			
		} // public function getBackendDownloadURL($order_id, $gs_id)
		
		/**
		 * Generiert das Gutscheindokument
		 *
		 * @param varchar $code Der Gutscheincode
		 * @param int $order_id Die Bestell ID
		 * @param int $product_id Die Produkt ID
		 */
		public function genGSDokument($code, $order_id, $product_id, $preview, $product_index)
		{
			
			if (!$preview)
			{
			
				$product_data = $this->shop->cache->loadProduct($product_id);
				$gs_data = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_GUTSCHEIN."` WHERE `code` = '".wpsg_q($code)."'");
				
			}
			else 
			{
				
				$this->shop->noReplace = true;
				
				$product_data = array();
				$gs_data = array(
					'end_date' => date('Y-m-d H:i:s'),
					'start_date' => date('Y-m-d H:i:s'),
					'calc_typ' => 'w',
					'value' => '14'
				);
				
			}
			
			if (file_exists($this->getFilePath('').'/bp.pdf'))
			{
				
				$this->shop->view['bp_pdf'] = $this->getFilePath('').'/bp.pdf';
				
			}
			else if (file_exists($this->getFilePath('').'/bp.jpg'))
			{
				
				$this->shop->view['bp_jpg'] = $this->getFilePath('').'/bp.jpg';
				
			}
			
			$this->shop->view['customReplace'] = array(
				'/%gs_code%/' => $code,
				'/%gs_value%/' => wpsg_ff($gs_data['value']),
				'/%gs_valid%/' => wpsg_formatTimestamp(strtotime($gs_data['end_date']), true),
				'/%gs_start%/' => wpsg_formatTimestamp(strtotime($gs_data['start_date']), true)
			);
			
			$this->shop->view['preview'] = $preview;
			$this->shop->view['filename'] = $this->getFilePath($order_id).'voucher_'.$code.'.pdf';
			
			$this->shop->view['product_id'] = $product_id;
			$this->shop->view['product_index'] = $product_index;
			$this->shop->view['order_id'] = $order_id;
			$this->shop->view['gs_data'] = $gs_data;
			$this->shop->view['product_data'] = $product_data;
			$this->shop->view['texte'] = $this->shop->get_option('wpsg_mod_voucherproduct_texte');
			if (!is_array($this->shop->view['texte'])) $this->shop->view['texte'] = array();
									
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/voucher_pdf.phtml', false);
						
			return $this->shop->view['filename'];
			
		} // public function genGSDokument($code, $order_id, $product_id)
		
		/**
		 * Gibt den Pfad zu den abgelegten Gutscheinen zurück
		 */
		public function getFilePath($order_id)
		{
			
			if ($this->shop->isMultiBlog())
			{

				$path = UPLOADS.'/wpsg/wpsg_mod_gutscheinprodukt/'.$order_id;
				$htaccess = UPLOADS.'/wpsg/wpsg_mod_gutscheinprodukt/.htaccess';
				
				$strReturn = $path.'/'.$order_id.'/';
								 
			}
			else
			{
				
				$path = WPSG_PATH_CONTENT.'uploads/wpsg/wpsg_mod_gutscheinprodukt/'.$order_id;
				$htaccess = WPSG_PATH_CONTENT.'uploads/wpsg/wpsg_mod_gutscheinprodukt/.htaccess';
				
				$strReturn = WPSG_PATH_CONTENT.'uploads/wpsg/wpsg_mod_gutscheinprodukt/'.$order_id.'/';
				 
			}
			
			if (!file_exists($path)) 
			{
				
				mkdir($path, 0777, true);
				
			}
			
			/*
 			 * Zur Sicherheit überprüfe ich immer ob die .htaccess existiert
 			 */			
			if (!file_exists($htaccess))
			{
				
				$handle = fopen($htaccess, "w+");
				
				$content = 'Deny from all';
							
				fwrite($handle, $content, strlen($content));
				fclose($handle);
				
			}
			
			return $strReturn;
			
		} // private function getFilePath($order_id)
		
		/**
		 * Gibt true zurück wenn die Bestellung Gutscheinprodukte beinhaltet
		 */
		public function hasVocherProducts($order_id)
		{
			
			$arProdukte = $this->db->fetchAssoc("
				SELECT
					P.*,
					OP.`menge`
				FROM
					`".WPSG_TBL_ORDERPRODUCT."` AS OP
						LEFT JOIN `".WPSG_TBL_PRODUCTS."` AS P ON (OP.`p_id` = P.`id`)
				WHERE
					OP.`o_id` = '".wpsg_q($order_id)."'
			");
			
			foreach ($arProdukte as $p)
			{
					
				if ($p['wpsg_mod_voucherproduct_gs'] == '1')
				{

					return true;
					
				}
				
			}
			
			return false;
			
		} // private function hasVocherProducts($order_id)
		
		/**
		 * Baut die Ansicht für das Backend (Bestellverwaltung)
		 */
		public function order_view_sidebar_content($order_id)
		{
			
			if ($order_id > 0)
			{
			
				$this->shop->view['wpsg_mod_voucherproduct']['data'] = $this->db->fetchAssoc("
					SELECT
						*
					FROM
						`".WPSG_TBL_GUTSCHEIN."`
					WHERE
						`autocreate_order` = '".wpsg_q($order_id)."'
				");
				
				if (wpsg_isSizedArray($this->shop->view['wpsg_mod_voucherproduct']['data']))
				{
					
					foreach ($this->shop->view['wpsg_mod_voucherproduct']['data'] as $k => $v)
					{
					
						$tStart = strtotime($v['start_date']);
						$tEnd = strtotime($v['end_date']);
						
						if ($tStart < time() && $tEnd > time())
						{
							$nStatus = 1; $strStatus = __('Aktiv', 'wpsg');
						}
						else if ($tStart > time())
						{
							$nStatus = 2; $strStatus = __('Wartend', 'wpsg');
						}
						else if ($tEnd < time())
						{
							$nStatus = 3; $strStatus = __('Ausgelaufen', 'wpsg');
						}
						else
						{
							
							// Sollte eignetlich nich auftreten
							$nStatus = 999; $strStatus = __('Ungültig', 'wpsg');
							
						}

						$this->shop->view['wpsg_mod_voucherproduct']['data'][$k]['status'] = $strStatus;
						
					}
					
				}
				
			} 
			
			$this->shop->view['wpsg_mod_voucherproduct']['order_id'] = $order_id;
			
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_voucherproduct/order_view_sidebar_content.phtml', false);
			
		} // public function order_view_sidebar_content($order_id)
		
	} // class wpsg_mod_voucherproduct extends wpsg_mod_basic

?>