<?php

	/**
	 * Modul für die Füllmengenberechnung
	 */
	class wpsg_mod_fuellmenge extends wpsg_mod_basic
	{
		
		var $id = 701;		
		var $lizenz = 2;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Füllmengen', 'wpsg');
			$this->group = __('Produkte', 'wpsg');
			$this->desc = __('Erlaubt das angeben der Füllmenge und die Berechnung des Grundpreises. Notwendig bei Flächenprodukten, Flüssigkeiten, o.ä. Produkten.<br />nähere Details siehe <a href="http://www.gesetze-im-internet.de/pangv/index.html" target="">Preisangabenverordnung</a>.', 'wpsg');
						
		} // public function __construct()
		
		public function install() 
		{ 

			if ($this->shop->get_option('wpsg_mod_fuellmenge_einheit') === false || $this->shop->get_option('wpsg_mod_fuellmenge_einheit') == '') $this->shop->update_option('wpsg_mod_fuellmenge_einheit', 'kg,l,m³,m,m²');
			if ($this->shop->get_option('wpsg_mod_fuellmenge_bezug') === false || $this->shop->get_option('wpsg_mod_fuellmenge_bezug') == '') $this->shop->update_option('wpsg_mod_fuellmenge_bezug', '1');
						
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');

			/**
			 * Produkttabelle erweitern
			 */
			$sql = "CREATE TABLE ".WPSG_TBL_PRODUCTS." (
			  	feinheit varchar(50) NOT NULL,
			  	fmenge varchar(50) NOT NULL,
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
			
			dbDelta($sql);
			
		}
		
		public function settings_edit()
		{
						
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_fuellmenge/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save() 
		{
			
			$this->shop->update_option('wpsg_mod_fuellmenge_einheit', $_REQUEST['wpsg_mod_fuellmenge_einheit']);
			$this->shop->update_option('wpsg_mod_fuellmenge_bezug', $_REQUEST['wpsg_mod_fuellmenge_bezug']);
			
			if ($this->shop->hasMod('wpsg_mod_fuellmenge') == '1')
			{
				
				$this->shop->update_option('wpsg_mod_fuellmenge_showProductindex_fmenge', $_REQUEST['wpsg_mod_fuellmenge_showProductindex_fmenge']);
				$this->shop->update_option('wpsg_mod_fuellmenge_showProductindex_grundpreis', $_REQUEST['wpsg_mod_fuellmenge_showProductindex_grundpreis']);
				
			}
			
		} // public function settings_save()
		
		public function produkt_add(&$data)
		{
			
			$data['arFeinheiten'] = explode(',', $this->shop->get_option('wpsg_mod_fuellmenge_einheit'));
			
		} // public function produkt_add(&$data)
		 
		public function produkt_edit(&$data)
		{
			
			$data = wpsg_array_merge($data, $this->db->fetchRow("
				SELECT
					`feinheit`, `fmenge`
				FROM
					`".WPSG_TBL_PRODUCTS."`
				WHERE
					`id` = '".wpsg_q($data['id'])."'
			"));
						
			$data['arFeinheiten'] = explode(',', $this->shop->get_option('wpsg_mod_fuellmenge_einheit'));
			
		} // public function produkt_edit(&$data)
				
		public function produkt_save(&$produkt_id)
		{
			
			$this->db->UpdateQuery(WPSG_TBL_PRODUCTS, array(
				"feinheit" => wpsg_q($_REQUEST['feinheit']),
				"fmenge" => wpsg_q(wpsg_tf($_REQUEST['fmenge']))
			), "`id` = '".wpsg_q($produkt_id)."'");
						
		} // public function produkt_save(&$produkt_id)
		
		public function produkt_edit_sidebar(&$produkt_data)
		{
			
			if (isset($_REQUEST['wpsg_lang'])) return;
			 
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_fuellmenge/produkt_edit_sidebar.phtml');
			
		} // public function produkt_edit_sidebar(&$produkt_data)
		 		
		public function loadProduktArray(&$produkt)
		{
			
			$arFeinheiten = explode(',', $this->shop->get_option('wpsg_mod_fuellmenge_einheit'));
						
			if ($this->shop->hasMod('wpsg_mod_varianten') && $this->shop->callMod('wpsg_mod_varianten', 'isVariantsProductKey', array($produkt['id'])))
			{
				
				$variInfo = $this->shop->callMod('wpsg_mod_varianten', 'getVariantenInfoArray', array($produkt['id']));
				$produkt['fmenge'] += $variInfo['fmenge'];
				
			}
			
			$produkt['feinheit'] = $arFeinheiten[$produkt['feinheit']];
			if ($produkt['fmenge'] > 0) $produkt['fmenge_preis'] = $produkt['preis'] / $produkt['fmenge'] * wpsg_tf($this->shop->get_option('wpsg_mod_fuellmenge_bezug'));
						
		} // public function renderProdukt_data(&$view)
		
		/** Modulfunktionen */
		
		public function renderPriceInfo($price, $fmenge, $feinheit)
		{
			
			if ($price <= 0 || $fmenge <= 0) return;
			
			if (is_numeric($feinheit))
			{
				
				$arFeinheiten = explode(',', $this->shop->get_option('wpsg_mod_fuellmenge_einheit'));
				$feinheit = $arFeinheiten[$feinheit];
				
			}
			
			$this->shop->view['wpsg_mod_fuellmenge'] = array(
				'fmenge' => $fmenge,
				'feinheit' => $feinheit,
				'fmenge_preis' => $price / $fmenge * wpsg_tf($this->shop->get_option('wpsg_mod_fuellmenge_bezug'))
			);
						 			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_fuellmenge/priceinfo.phtml');			
			
		} // public function renderPriceInfo($fmenge, $feinheit)
		
	} // class wpsg_mod_fuellmenge extends wpsg_mod_basic

?>