<?php

	/**
	 * Dieses Modul erlaubt die Erstellung und Verwendung von Gutscheinen
	 */
	class wpsg_mod_gutschein extends wpsg_mod_basic
	{
		
		var $id = 160;		
		var $lizenz = 1;
		var $hilfeURL = 'http://wpshopgermany.de/?p=455';
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Gutscheine', 'wpsg');
			$this->group = __('Sonstiges', 'wpsg');
			$this->desc = __('Erlaubt das Erstellen von Gutscheinen.', 'wpsg');
						
		} // public function __construct()
		
		public function install() 
		{ 
 	
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');

			/**
			 * Tabelle für die Gutscheine
			 */			
			$sql = "CREATE TABLE ".WPSG_TBL_GUTSCHEIN." (
		   		id mediumint(9) NOT NULL AUTO_INCREMENT,
		   		code VARCHAR(255) NOT NULL,
		   		value DOUBLE(10,2) NOT NULL,
		   		o_id INT(11) NOT NULL,
		   		calc_typ ENUM('w', 'p') NOT NULL,
		   		cdate datetime NOT NULL,
		   		start_date datetime NOT NULL,
		   		end_date datetime NOT NULL, 
		   		multi INT(11) NOT NULL,
		   		comment TEXT NOT NULL,
		   		autocreate_order INT(11) NOT NULL,
		   		autocreate_product INT(11) NOT NULL,
				autocreate_order_product INT(11) NOT NULL,
		   		minvalue DOUBLE(10,2) NOT NULL,
				productgroups TEXT NOT NULL,
				products TEXT NOT NULL,
		   		PRIMARY KEY  (`id`),
		   		KEY o_id (o_id),
		   		KEY autocreate_order (autocreate_order),
		   		KEY autocreate_product (autocreate_product)
		   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
			
			dbDelta($sql);
			
			$this->shop->checkDefault('wpsg_mod_gutschein_size', '10');
			$this->shop->checkDefault('wpsg_mod_gutschein_perPage', '25');
						
		} // public function install() 
		
		public function init()
		{
			
			$role_object = get_role('administrator');
			$role_object->add_cap('wpsg_voucher');
			
		} // public function init()
		
		public function dispatch() 
		{
			
			if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'add')
			{
				
				$this->addAction();
				
			}
			else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'import')
			{
				
				$this->importAction();
				
			}
			else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'save')
			{
				
				$this->saveAction();
				
			}
			else 
			{
				
				$this->indexAction();
				
			}
			
		} // public function dispatch()
		 
		public function admin_setcapabilities() { 
			
			$this->render(WPSG_PATH_VIEW.'/mods/mod_gutschein/admin_setcapabilities.phtml');
			
		} // public function admin_setcapabilities()
		
		public function wpsg_add_pages()
		{
						
			add_submenu_page('wpsg-Admin', __("Gutscheine", "wpsg"), __("Gutscheine", "wpsg"), 'wpsg_voucher', 'wpsg-Voucher', array($this, 'dispatch'));
			
		} // public function wpsg_add_pages()
		
		/**
		 * Maske für einen neuen Gutschein
		 */
		public function addAction()
		{
			
			$this->shop->view['mod_gutschein'] = array();
			
			if (isset($_SESSION['wpsg']['mod_gutschein_error']))
			{

				$this->shop->view['mod_gutschein']['value'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_value'];
				$this->shop->view['mod_gutschein']['calc'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_calc'];
				$this->shop->view['mod_gutschein']['start'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_start'];
				$this->shop->view['mod_gutschein']['end'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_end'];
				$this->shop->view['mod_gutschein']['count'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_count'];
				$this->shop->view['mod_gutschein']['gen'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_gen'];
				$this->shop->view['mod_gutschein']['code'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_code'];
				$this->shop->view['mod_gutschein']['comment'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_comment'];
				$this->shop->view['mod_gutschein']['multi'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_multi'];
				if (isset($_REQUEST['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_minvalue'])) $this->shop->view['mod_gutschein']['minvalue'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_minvalue'];				
				if (isset($_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_products'])) $this->shop->view['mod_gutschein']['products'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_products'];
				if (isset($_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_productgroups'])) $this->shop->view['mod_gutschein']['productgroups'] = $_SESSION['wpsg']['mod_gutschein_error']['wpsg_mod_gutschein_productgroups'];
				
				unset($_SESSION['wpsg']['mod_gutschein_error']);
				
			}
			else
			{
				
				$this->shop->view['mod_gutschein']['value'] = 10;
				$this->shop->view['mod_gutschein']['calc'] = 'w';				
				$this->shop->view['mod_gutschein']['count'] = '1';
				$this->shop->view['mod_gutschein']['multi'] = '0';	
				$this->shop->view['mod_gutschein']['productgroups'] = '-1';			
				
			}
			
			if ($this->shop->hasMod('wpsg_mod_productgroups'))
			{
				
				$this->shop->view['wpsg_mod_gutschein']['arProductGroups'] = $this->shop->callMod('wpsg_mod_productgroups', 'getAllProductGroups');
				
			}
			
			$this->shop->view['wpsg_mod_gutschein']['arProducts'] = $this->shop->getAllProductsForSelect();			
			
			if (!isset($this->shop->view['mod_gutschein']['start']) || !is_int($this->view['mod_gutschein']['start']) || $this->view['mod_gutschein']['start'] <= 0)
				$this->shop->view['mod_gutschein']['start'] = wpsg_date('d.m.Y H:i');
				
			if (!isset($this->shop->view['mod_gutschein']['end']) || !is_int($this->view['mod_gutschein']['end']) || $this->view['mod_gutschein']['end'] <= 0)
				$this->shop->view['mod_gutschein']['end'] = wpsg_date('d.m.').(wpsg_date('Y') + 1).' '.wpsg_date('H:i');
			
			$this->render(WPSG_PATH_VIEW.'/mods/mod_gutschein/add.phtml');
			
		} // public function addAction()
		
		/**
		 * CSV Import von Gutscheinen
		 */
		public function importAction()
		{
			
			if (isset($_REQUEST['submit']))
			{
					
				// Datei angegeben?
				if (!file_exists($_FILES['wpsg_file']['tmp_name']) || !preg_match('/(.*)\.csv$/i', $_FILES['wpsg_file']['name']))
				{
						
					$this->shop->addBackendError(__('Bitte eine CSV Datei angeben!', 'wpsg'));
					$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Voucher&action=import');
						
				}
			
				$arData = array();
			
				if (($handle = fopen($_FILES['wpsg_file']['tmp_name'], "r")) !== false)
				{
						
					while (($data = fgetcsv($handle, 1000, ";")) !== false)
					{
						 
						$arData[] = $data;
			
					}
			
					fclose($handle);
			
				}
			
				if (!wpsg_isSizedArray($arData))
				{
						
					$this->shop->addBackendError(__('CSV Datei konnte nicht geladen werden!', 'wpsg'));
					$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Voucher&action=import');
						
				}
								
				// Spalten identifizieren
				$arCol = array_flip($arData[0]);
				$arColPflicht = array('code', 'value', 'start_date', 'end_date');
				$bColError = false;
				
				foreach ($arColPflicht as $v)
				{
				
					if (!isset($arCol[$v]))
					{
					
						$this->shop->addBackendError(wpsg_translate(
							__('CSV Datei ungültig. (Spalte "#1#" muss vorhanden sein)', 'wpsg'),
							$v)
						);
						
						$bColError = true;
					
					}
					
				}
				
				if ($bColError === true)
				{
					
					$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Voucher&action=import');
					
				}
				
				$nImport = 0;
				 				
				// Daten importieren / Erste Zeile sind die Schlüssel
				for ($i = 1; $i < sizeof($arData); $i ++)
				{
					
					// Leere Zeilen überspringen
					if (!wpsg_isSizedArray($arData[$i])) continue;
					
					if (!preg_match('/\d{4}\-\d{2}\-\d{2}\040\d{2}\:\d{2}\:\d{2}/', $arData[$i][$arCol['start_date']]))
					{
												
						$this->shop->addBackendError(wpsg_translate(
							__('Spalte "#1#" in Zeile #2# enthält kein gültiges Datum -> Zeile wurde übersprungen', 'wpsg'),
							'start_date',
							$i + 1)
						);
						
						continue;
						
					} 
					
					if (!preg_match('/\d{4}\-\d{2}\-\d{2}\040\d{2}\:\d{2}\:\d{2}/', $arData[$i][$arCol['end_date']]))
					{
					
						$this->shop->addBackendError(wpsg_translate(
							__('Spalte "#1#" in Zeile #2# enthält kein gültiges Datum -> Zeile wurde übersprungen', 'wpsg'),
							'end_date',
							$i + 1)
						);
						
						continue;
					
					}
					
					if (strlen($arData[$i][$arCol['code']]) < $this->shop->get_option('wpsg_mod_gutschein_size'))
					{
						
						$this->shop->addBackendError(wpsg_translate(
							__('Ungültiger Gutscheincode in Zeile #1# (zu Kurz) -> Zeile wurde übersprungen', 'wpsg'),		
							$i + 1)
						);
						
						continue;
						
					}
					
					if (doubleval($arData[$i][$arCol['value']]) <= 0)
					{
						
						$this->shop->addBackendError(wpsg_translate(
							__('Ungültiger Gutscheinwert in Zeile #1# (kleiner gleich 0) -> Zeile wurde übersprungen', 'wpsg'),
							$i + 1)
						);
						
						continue;
						
					}
					
					// Existiert schon ein Gutschein mit dem Code
					$bExists = $this->db->fetchOne("SELECT G.`id` FROM `".WPSG_TBL_GUTSCHEIN."` AS G WHERE G.`code` = '".wpsg_q($arData[$i][$arCol['code']])."'");
					
					$doImport = false;
					
					if ($bExists > 0)
					{
						
						// Existiert schon
						if ($_REQUEST['gs_exists'] == '0')
						{
							
							$this->shop->addBackendMessage(wpsg_translate(
								__('Code aus Zeile #1# existierte schon und wurde überschrieben.', 'wpsg'),	
								$i + 1)
							);
							
							$doImport = true;
							
						}
						else
						{
							
							$this->shop->addBackendMessage(wpsg_translate(
								__('Code (#1#) aus Zeile #2# existiert schon -> Zeile wurde übersprungen.', 'wpsg'),
								$arData[$i][$arCol['code']],
								$i + 1)
							);
														
						}
						
					}
					else
					{
						
						$nImport ++;
						$doImport = true;
						
					}
					
					if ($doImport === true)
					{
						
						if ($bExists > 0) $this->db->Query("DELETE FROM `".WPSG_TBL_GUTSCHEIN."` WHERE `id` = '".wpsg_q($bExists)."'");
						
						$data = array(
							'code' => wpsg_q($arData[$i][$arCol['code']]),
							'value' => wpsg_q(wpsg_tf($arData[$i][$arCol['value']])),
							'start_date' => wpsg_q($arData[$i][$arCol['start_date']]),
							'end_date' => wpsg_q($arData[$i][$arCol['end_date']])								
						);
						
						if ($arData[$i][$arCol['calc_typ']] == 'p') $data['calc_typ'] = 'p'; else $data['calc_typ'] = 'w';
						if ($arData[$i][$arCol['multi']] == '1') $data['multi'] = '1'; else $data['multi'] = '0';
						if (isset($arCol['products']) && wpsg_isSizedString($arData[$i][$arCol['products']])) $data['products'] = $arData[$i][$arCol['products']]; else $data['products'] = '';
						if (isset($arCol['productgroups']) && wpsg_isSizedString($arData[$i][$arCol['productgroups']])) $data['productgroups'] = $arData[$i][$arCol['productgroups']]; else $data['productgroups'] = '';
						if (isset($arCol['comment']) && wpsg_isSizedString($arData[$i][$arCol['comment']])) $data['comment'] = $arData[$i][$arCol['comment']]; else $data['comment'] = '';
	
						$this->db->ImportQuery(WPSG_TBL_GUTSCHEIN, $data);
						
					}
					
				} // for data
 
				if ($nImport == 1) $this->shop->addBackendMessage(__('Ein Gutschein erfolgreich importiert.', 'wpsg'));
				else if ($nImport > 1) $this->shop->addBackendMessage(wpsg_translate(__('#1# Gutscheine erfolgreich importiert.', 'wpsg'), $nImport));
				else $this->shop->addBackendMessage(__('Keine Gutscheine importiert.', 'wpsg'));
					
				$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Voucher');
				
			}
			
			$this->render(WPSG_PATH_VIEW.'/mods/mod_gutschein/import.phtml');
			
		} // public function importAction()
		
		/**
		 * Speichert einen Gutschein
		 */
		public function saveAction()
		{
			
			$bError = false;
			$tStart = wpsg_fieldarray_todate($_REQUEST['wpsg_mod_gutschein_start']);
			$tEnd = wpsg_fieldarray_todate($_REQUEST['wpsg_mod_gutschein_end']);
			
			// Validierung 
			if ($_REQUEST['wpsg_mod_gutschein_count'] <= 0)
			{
				
				$this->shop->addBackendError(__('Eingabe im Feld "Menge" ist ungültig.', 'wpsg'));
				$bError = true;
				
			}
						
			if ($tStart <= 0)
			{
				
				$this->shop->addBackendError(__('Eingabe im Feld "Gültig ab" überprüfen.', 'wpsg'));
				$bError = true;
				
			}
			
			if ($tEnd <= 0)
			{
				
				$this->shop->addBackendError(__('Eingabe im Feld "Gültig bis" überprüfen.', 'wpsg'));
				$bError = true;
				
			}
			
			if ($_REQUEST['wpsg_mod_gutschein_value'] <= 0)
			{
				
				$this->shop->addBackendError(__('Eingabe im Feld "Wert" überprüfen.', 'wpsg'));
				$bError = true;
				
			}
			
			if ($bError)
			{
				
				$_SESSION['wpsg']['mod_gutschein_error'] = wpsg_xss($_REQUEST);
				
				$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Voucher&action=add');
				
			}
			else
			{
				 	
				for ($i = 0; $j < $_REQUEST['wpsg_mod_gutschein_count']; $j ++)
				{
					
					$arProductGroups = array(); if (wpsg_isSizedArray($_REQUEST['wpsg_mod_gutschein_productgroups'])) $arProductGroups = $_REQUEST['wpsg_mod_gutschein_productgroups'];
					$arProducts = array(); if (wpsg_isSizedArray($_REQUEST['wpsg_mod_gutschein_products'])) $arProducts = $_REQUEST['wpsg_mod_gutschein_products'];
					 			 
					if ($_REQUEST['wpsg_mod_gutschein_gen'] == '1')
					{
						
						$setCode = $_REQUEST['wpsg_mod_gutschein_code'];
						
					}
					else
					{
						
						$setCode = false;
						
					}
					
					$this->genGS(
						wpsg_tf($_REQUEST['wpsg_mod_gutschein_value']), 
						$_REQUEST['wpsg_mod_gutschein_calc'], 
						$tStart, 
						$tEnd, 
						$_REQUEST['wpsg_mod_gutschein_multi'], 
						$this->shop->get_option('wpsg_mod_gutschein_size'), 
						$_REQUEST['wpsg_mod_gutschein_comment'],
						0,
						0,
						0,
						$arProductGroups,
						$arProducts,						
						$_REQUEST['wpsg_mod_gutschein_minvalue'],
						$setCode
					);
					
				}
				
				if ($_REQUEST['wpsg_mod_gutschein_count'] > 1)
				{
					$this->shop->addBackendMessage(__('Gutscheine wurden erfolgreich angelegt.', 'wpsg'));
				}
				else
				{
					$this->shop->addBackendMessage(__('Gutschein wurde erfolgreich angelegt.', 'wpsg'));
				}
				
				$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Voucher&action=index');				
				
			}
			
		} // public function saveAction()
						
		public function basket_row_end(&$basket_view) 
		{ 
			
			if ($this->shop->get_option('wpsg_mod_gutschein_hideInsert') == '1') return;
			
			$this->view['error'] = $basket_view['error'];
			
			$this->render(WPSG_PATH_VIEW.'/mods/mod_gutschein/basket_row_end.phtml');
			
		} // public function basket_row_end(&$basket)

		public function basket_preInsert() 
		{
			
			if (!wpsg_isSizedArray($_SESSION['wpsg']['gs'])) return;
			
			$gs_db = $this->db->fetchRow("SELECT G.`id`, G.`productgroups`, G.`products` FROM `".WPSG_TBL_GUTSCHEIN."` AS G WHERE G.`id` = '".wpsg_q($_SESSION['wpsg']['gs']['id'])."'");
			
			if ($gs_db['id'] != $_SESSION['wpsg']['gs']['id'])
			{
				
				$this->shop->throwErrorCode('160_1');
				return;
				
			}
			
			$product_id = $this->shop->getProduktID($_REQUEST['wpsg']['produkt_id']);
			
			$product_allowed = wpsg_explode(',', $gs_db['products']);
					
			if (wpsg_isSizedArray(wpsg_explode(',', $gs_db['productgroups'])))
			{

				$ProductGroupIDs = wpsg_explode(',', $gs_db['productgroups']);
				$product_allowed = array_merge($product_allowed, $this->db->fetchAssocField("
					SELECT P.`id` FROM `".WPSG_TBL_PRODUCTS."` AS P WHERE P.`pgruppe` IN (".wpsg_implode(',', $ProductGroupIDs).")
				"));
				
			}
			 
			$product_allowed = wpsg_trim($product_allowed);
			
			if (wpsg_isSizedArray($product_allowed) && !in_array($product_id, $product_allowed))
			{
				
				$product_data = $this->shop->loadProduktArray($product_id);
				$this->shop->addFrontendError(wpsg_translate(__('Gutschein wurde entfernt, da er nicht mit Produkt #1# verwendet werden kann', 'wpsg'), $product_data['name']));
				unset($_SESSION['wpsg']['gs']);
				
			}
			
		} // public function basket_preInsert() 

		public function basket_afterRemove()
		{
			
			$arBasket = $this->shop->cache->getShopBasketArray();
			
			$this->_checkBasket($this->shop->cache->getShopBasketArray());
			
		} // public function basket_afterRemove()
		
		public function basket_afterUpdate(&$bError) 
		{ 
			
			$this->_checkBasket($this->shop->cache->getShopBasketArray());
						
		} // public function basket_afterUpdate(&$bError)

		public function checkCheckout(&$state, &$error, &$arCheckout) 
		{ 

			if (!$this->_checkBasket($this->shop->cache->getShopBasketArray())) $error = true;
						
		} // public function checkCheckout(&$state, &$error, &$arCheckout)
		
		/* Modulfunktionen */
		
		/**
		 * Gutscheinberechnung im Warenkorb
		 */
		public function basket_toArray_gs(&$basket, &$arReturn)
		{
			 		
			// Analog Rabatt
			
			if ($basket->gs_value !== false)
			{
				
				$arReturn['gs'] = array(
					'value' => $basket->gs_value,
					'calc' => $basket->gs_calc,
					'code' => $basket->gs_code
				);
				
				$gs_value = $basket->gs_value;
				$gs_tax = 0;
				
				// Bruttopreis des Gutscheins bestimmen
				if ($basket->gs_calc == 'p')
				{
											
					$gs_value = $arReturn['sum']['preis_gesamt_brutto'] / 100 * $basket->gs_value;
					
					if ($this->shop->get_option('wpsg_kleinunternehmer') != '1')
					{
												 					
						$gs_tax = $this->shop->subMwSt($arReturn, $gs_value);
						
					}
											
				}
				else 
				{
					
					if ($this->shop->get_option('wpsg_preisangaben') == WPSG_NETTO)
					{
						
						if ($this->shop->get_option('wpsg_kleinunternehmer') != '1')
						{
						
							$gs_tax = $this->shop->addMwSt($arReturn, $basket->gs_value);
							 
						} 
						
						$gs_value = $basket->gs_value + $gs_tax;
						
					}
					else
					{
						
						$gs_value = $basket->gs_value;
						
						if ($this->shop->get_option('wpsg_kleinunternehmer') != '1')
						{
							
							$gs_tax = $this->shop->subMwSt($arReturn, $basket->gs_value);
							
						}
						//die($gs_tax."=".$gs_value);
					}
										
				}
								 				 
				$gs_brutto = $gs_value;
				$gs_netto = $gs_value - $gs_tax; 
 				
				$arReturn['sum']['preis_brutto'] -= $gs_brutto;
				$arReturn['sum']['preis_netto'] -= $gs_netto;
				$arReturn['sum']['preis_gesamt_brutto'] -= $gs_brutto;
				$arReturn['sum']['preis_gesamt_netto'] -= $gs_netto;
												
				if ($this->shop->getFrontendTaxview() == WPSG_NETTO)
				{

					$arReturn['sum']['gs'] = $gs_netto;
					$arReturn['sum']['gs_netto'] = $gs_netto;
					$arReturn['sum']['gs_brutto'] = $gs_brutto;
					$arReturn['sum']['preis'] = $arReturn['sum']['preis_netto'];
					$arReturn['sum']['preis_gesamt'] = $arReturn['sum']['preis_gesamt_netto'];
					
				}
				else if ($this->shop->getFrontendTaxview() == WPSG_BRUTTO)
				{
					
					$arReturn['sum']['gs'] = $gs_brutto;
					$arReturn['sum']['gs_netto'] = $gs_netto;
					$arReturn['sum']['gs_brutto'] = $gs_brutto;
					$arReturn['sum']['preis'] = $arReturn['sum']['preis_brutto'];
					$arReturn['sum']['preis_gesamt'] = $arReturn['sum']['preis_gesamt_brutto'];
					
				} 
				 
			} // gs_value 
			
		} // public function basket_toArray_gs(&$basket, &$arReturn)
		
		/**
		 * Prüft den Basket und entfernt den Gutschein, sollte irgendetwas nicht passen
		 */
		private function _checkBasket($arBasket)
		{
			
			if (!wpsg_isSizedArray($arBasket['gs'])) return true;
			if (!wpsg_isSizedInt($_SESSION['wpsg']['gs']['id'])) { $this->shop->throwErrorCode('160_2'); return true; }			
			
			$gs_db = $this->db->fetchRow("SELECT G.`id`, G.`minvalue` FROM `".WPSG_TBL_GUTSCHEIN."` AS G WHERE G.`id` = '".wpsg_q($_SESSION['wpsg']['gs']['id'])."'");

			if ($gs_db['id'] != $_SESSION['wpsg']['gs']['id']) { $this->shop->throwErrorCode('160_1'); return true; }
			
			if (wpsg_tf($gs_db['minvalue']) > 0 && ($arBasket['sum']['preis'] + $arBasket['sum']['gs']) < $gs_db['minvalue'])
			{
				
				$this->shop->addFrontendError(wpsg_translate(__('Gutschein wurde entfernt, da der Mindestbestellwert von #1# unterschritten wurden.', 'wpsg'), wpsg_ff($gs_db['minvalue'], $this->shop->get_option('wpsg_currency'))));
				unset($_SESSION['wpsg']['gs']);
				
				return false;
				
			}
			
			return true;
			 			
		} // private function _checkBasket($arBasket)
		
		/**
		 * Generiert einen Gutschein in der Datenbank
		 * 
		 * @param double $value der Wert des Gutscheins
		 * @param varchar $calc_typ Typ 'p' = Prozentual, 'w' = Absoluter Wert
		 * @param int $tStart Timestamp Start der Gültigkeit
		 * @param int $tEnd Timestamp Ende der Gültigkeit
		 * @param int $multi 1 = Mehrfach, 0 = Einmalig
		 * @param int $laenge Länge des Gutscheins
		 * @param String $comment Kommentar
		 * @param int $autocreate_order ID Der Bestellung aus der der Gutschein generiert wurde
		 * @param int $autocreate_product ID Des Produkts aus dem der Gutschein generiert wurde
		 * @param <int>Array $productgroups Array mit IDs von Produktgruppen für die der Gutschein gültig ist
		 * @param <int>Array $products Array mit IDs von Produkten für die der Gutschein gültig ist 
		 * 
		 * @return Der generierte Code
		 */
		public function genGS($value, $calc_typ, $tStart, $tEnd, $multi, $laenge, $comment, $autocreate_order = 0, $autocreate_product = 0, $autocreate_order_product, $productgroups = array(), $products = array(), $minValue = false, $code = false)
		{
			
			$value = wpsg_tf($value);
			
			if (wpsg_isSizedString($code))
			{
				
				$strCode = $code;
				
			}
			else
			{
			 
				$laenge = get_option('wpsg_mod_gutschein_size');
				if ($laenge <= 0) $size = 10;
				
				$zeichen = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
				
				while (true)
				{
					 
					$arCode = array();
					
					for ($i = 1; $i <= $laenge; $i++) { $arCode[] = $zeichen[rand(0, (strlen($zeichen) - 1))]; }
					
					$strCode = implode('', @$arCode);
	
					$bExists = $this->db->fetchOne("SELECT COUNT(*) FROM `".WPSG_TBL_GUTSCHEIN."` WHERE `code` = '".wpsg_q($strCode)."'");
					
					if ($bExists <= 0) break;
					break;
										
				}
				
			}
			
			$this->db->ImportQuery(WPSG_TBL_GUTSCHEIN, array(
				'code' => wpsg_q($strCode),
				'value' => wpsg_q($value),
				'minvalue' => wpsg_q(wpsg_tf($minValue)),
				'calc_typ' => wpsg_q($calc_typ),
				'cdate' => 'NOW()',
				'start_date' => wpsg_q(date('Y-m-d H:i:00', $tStart)),
				'end_date' => wpsg_q(date('Y-m-d H:i:00', $tEnd)),
				'multi' => wpsg_q($multi),
				'comment' => wpsg_q($comment),
				'autocreate_order' => wpsg_q($autocreate_order),
			 	'autocreate_product' => wpsg_q($autocreate_product),
				'autocreate_order_product' => wpsg_q($autocreate_order_product),
				'productgroups' => wpsg_q(wpsg_implode(',', $productgroups)),
				'products' => wpsg_q(wpsg_implode(',', $products))
			));
			 
			return $strCode;
			
		} // public function genGS($value, $calc_typ, $tStart, $tEnd, $multi, $laenge)
		
		/**
		 * Ist für die Übersicht der Gutscheine zuständig
		 */
		public function indexAction()
		{
			 
			if (isset($_REQUEST['wpshopgermany-submit-gs_del']))
			{
				
				if (sizeof($_REQUEST['wpsg_gs_cb']) > 0)
				{
					
					$this->db->Query("DELETE FROM `".WPSG_TBL_GUTSCHEIN."` WHERE `id` IN (".implode(",", array_values($_REQUEST['wpsg_gs_cb'])).")");
					
				}
				
				$this->shop->addBackendMessage(__('Gutscheine erfolgreich gelöscht.', 'wpsg'));
				$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Voucher');
				
			}
			
			// Suchstring aufbauen
			$strQueryWHERE = "";
			
			$this->shop->view['filter'] = array(
				'search' => ''
			);
			
			if (isset($_REQUEST['wpsg_search']))
			{
				
				$this->shop->view['filter']['search'] = $_REQUEST['wpsg_search'];
				$strQueryWHERE .= " AND `code` LIKE '%".wpsg_q($_REQUEST['wpsg_search'])."%' ";
				
			} 
			
			// Gefilterten Status für das View definieren
			if (isset($_REQUEST['wpsg_status']) && $_REQUEST['wpsg_status'] >= 0)
			{	
				$this->shop->view['filter']['status'] = $_REQUEST['wpsg_status'];
			}
						
			$strQueryORDER = '';
				
			if (!isset($_REQUEST['filter']['order']))
			{
			
				$this->shop->view['filter']['order'] = 'id';
			
			}
			else
			{
			
				$this->shop->view['filter']['order'] = $_REQUEST['filter']['order'];
			
			}
			
			switch ($this->shop->view['filter']['order'])
			{
			  
				case 'code': $strQueryORDER = "G.`code`"; break;
				case 'start_date': $strQueryORDER = "G.`start_date`"; break;
				case 'end_date': $strQueryORDER = "G.`end_date`"; break;
				case 'value': $strQueryORDER = "G.`value`"; break;
				default: $strQueryORDER = "G.`id`"; break;
			
			}
			
			if (!isset($_REQUEST['filter']['ascdesc']))
			{
			
				$this->shop->view['filter']['ascdesc'] = 'asc';
			
			}
			else
			{
			
				$this->shop->view['filter']['ascdesc'] = $_REQUEST['filter']['ascdesc'];
			
			}
			 
			// Alle gefilterten holen
			$arDataAll = $this->db->fetchAssoc("
				SELECT
					G.*
				FROM
					`".WPSG_TBL_GUTSCHEIN."` AS G
				WHERE
					1
					".$strQueryWHERE."
				ORDER BY 
					".wpsg_q($strQueryORDER)." ".wpsg_q($this->shop->view['filter']['ascdesc'])."
			");
			 
			$this->shop->view['arStatus'] = array();
			$this->shop->view['countAll'] = sizeof($arDataAll);
			
			foreach ($arDataAll as $k => $v)
			{
					
				$tStart = strtotime($v['start_date']);
				$tEnd = strtotime($v['end_date']);

				if ($tStart < wpsg_time() && $tEnd > wpsg_time())
				{
					$nStatus = 1; $strStatus = __('Aktiv', 'wpsg');
				}
				else if ($tStart > wpsg_time())
				{
					$nStatus = 2; $strStatus = __('Wartend', 'wpsg');
				}
				else if ($tEnd < wpsg_time())
				{
					$nStatus = 3; $strStatus = __('Ausgelaufen', 'wpsg');
				}
				else
				{
					
					// Sollte eignetlich nich auftreten
					$nStatus = 999; $strStatus = __('Ungültig', 'wpsg');
					
				}
				
				if ($v['multi'] == '0' && $v['o_id'] > 0)
				{
					
					$nStatus = 4; $strStatus = __('Verbraucht', 'wpsg');
					
				}
				else
				{
					
					$arDataAll[$k]['ordered'] = $this->db->fetchOne("SELECT COUNT(*) FROM `".WPSG_TBL_ORDER."` WHERE `gs_id` = '".wpsg_q($v['id'])."'");
					
				}
				
				if (array_key_exists($nStatus, $this->shop->view['arStatus']))
				{
					$this->shop->view['arStatus'][$nStatus][1] ++;
				}
				else 
				{
					$this->shop->view['arStatus'][$nStatus] = array($strStatus, 1);
				}
				
				if (isset($this->shop->view['filter']['status']) && $this->shop->view['filter']['status'] > 0 && $nStatus != $this->shop->view['filter']['status'])
				{
					unset($arDataAll[$k]);
				}
				else
				{					
					$arDataAll[$k]['status'] = $strStatus;					
				}
				 
			}
			
			if (isset($_REQUEST['wpsg_voucher_export']) && $_REQUEST['wpsg_voucher_export'] == '1')
			{
				
				$strCSV  = "code;value;start_date;end_date;comment;calc_typ;multi;products";				
				if ($this->shop->hasMod('wpsg_mod_productgroups')) $strCSV .= ";productgroups";
				$strCSV .= "\r\n";
				
				$arData = array();
				
				
				
				foreach ($arDataAll as $d)
				{
					
					$strCSV .= $d['code'].';'.$d['value'].';'.$d['start_date'].';'.$d['end_date'].';'.$d['comment'].';'.$d['calc_typ'].';'.$d['multi'].';'.$d['products'];
					if ($this->shop->hasMod('wpsg_mod_productgroups')) $strCSV .= ';'.$d['productgroups'];					
					$strCSV .= "\r\n";
										
				}
		 
				header("Content-Type: text/csv"); 
				header("Content-Disposition: attachment; filename=gutscheine.csv"); 
				header("Content-Description: csv File"); 
				header("Pragma: no-cache"); 
				header("Expires: 0"); 
				
				die($strCSV);
				
			}
			
			// Anzahl an Elementen
			$this->shop->view['count'] = sizeof($arDataAll);
			
			// Pro Seite / Fallback auf 25 wenn was falsches eingegeben wurde
			$perPage = ((intval($this->shop->get_option('wpsg_mod_gutschein_perPage')) > 0)?intval($this->shop->get_option('wpsg_mod_gutschein_perPage')):25);
			
			// Anzahl an Seiten
			$this->shop->view['pages'] = ceil($this->shop->view['count'] / $perPage);
			
			// Seite bestimmen
			if (isset($_REQUEST['seite']) && intval($_REQUEST['seite']) > 0) $this->shop->view['page'] = intval($_REQUEST['seite']); else $this->shop->view['page'] = 1;
			
			// Aktuele Seite aus den Datensätzen herausnehmen
			$this->shop->view['data'] = array_splice($arDataAll, (($this->shop->view['page'] - 1) * $perPage), $perPage);
			
			if ($this->shop->view['filter']['order'] == 'status')
			{
				
				wpsg_array_csort($this->shop->view['data'], 'status');
				
			}
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_gutschein/index.phtml');
			
		} // public function indexAction()
				
		public function settings_edit()
		{
			
			$this->render(WPSG_PATH_VIEW.'/mods/mod_gutschein/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_mod_gutschein_size', $_REQUEST['wpsg_mod_gutschein_size']);
			$this->shop->update_option('wpsg_mod_gutschein_perPage', $_REQUEST['wpsg_mod_gutschein_perPage']);
			
		} // public function settings_save()
				
		public function admin_presentation() 
		{ 

			echo wpsg_drawForm_Checkbox('wpsg_mod_gutschein_hideInsert', __('Gutschein einfügen im Warenkorb verbergen', 'wpsg'), $this->shop->get_option('wpsg_mod_gutschein_hideInsert'), array('help' => 'wpsg_mod_gutschein_hideInsert'));
			
		} // public function admin_presentation()
		
		public function admin_presentation_submit() 
		{ 
			
			$this->shop->update_option('wpsg_mod_gutschein_hideInsert', $_REQUEST['wpsg_mod_gutschein_hideInsert']);
			
		} // public function admin_presentation_submit()
		
		/* Modulfunktionen */
			 
	} // class wpsg_mod_gutschein extends wpsg_mod_basic

?>