<?php

	/**
	 * Modul für die Verwaltung von Produktgewichten
	 * @author Daschmi
	 */
	class wpsg_mod_weight extends wpsg_mod_basic
	{
		
		var $lizenz = 2;
		var $id = 95;

		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name 	= __('Produktgewicht', 'wpsg');
			$this->group 	= __('Produkte', 'wpsg');
			$this->desc 	= __('Erlaubt die Verwaltung von Gewichten pro Produkt.', 'wpsg');
						
		} // public function __construct()
		
		public function install() 
		{ 
			
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
			
			/**
			 * Posts Tabelle erweitern
			 */ 
			$sql = "CREATE TABLE ".WPSG_TBL_PRODUCTS." (
		   		weight DOUBLE(10,2) NOT NULL
		   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
   	   	 
   			dbDelta($sql); 
   			   			
   			/**
   			 * Tabelle für die bestellten Produkte
			 * Zur Sicherheit speichere ich hier das Gewicht zum Zeitpunkt der Bestellung
   			 */
   			$sql = "CREATE TABLE ".WPSG_TBL_ORDERPRODUCT." (
   				weight DOUBLE(10,2) NOT NULL
   			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
   			
   			dbDelta($sql);
   			
   			/**
   			 * Tabelle für die Bestellung erweitern, hier wird das Gesamtgewicht der Bestellung abgespeichert
   			 */
   			$sql = "CREATE TABLE ".WPSG_TBL_ORDER." (
   				weight DOUBLE(10,2) NOT NULL
   			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
   			
   			dbDelta($sql);
   			
   			$this->shop->checkDefault('wpsg_mod_weight_unit', 'g');
   			$this->shop->checkDefault('wpsg_mod_weight_showProduct', '1');
   			$this->shop->checkDefault('wpsg_mod_weight_showBasket', '1');
   			$this->shop->checkDefault('wpsg_mod_weight_showBasketProduct', '1');
   			$this->shop->checkDefault('wpsg_mod_weight_showOverview', '1');
   			$this->shop->checkDefault('wpsg_mod_weight_showOverviewProduct', '1');
   			
		} // public function install() 
		
		public function settings_edit()
		{
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_weight/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_mod_weight_unit', $_REQUEST['wpsg_mod_weight_unit']);
			$this->shop->update_option('wpsg_mod_weight_showProduct', $_REQUEST['wpsg_mod_weight_showProduct']);			
			$this->shop->update_option('wpsg_mod_weight_showBasket', $_REQUEST['wpsg_mod_weight_showBasket']);
			$this->shop->update_option('wpsg_mod_weight_showBasketProduct', $_REQUEST['wpsg_mod_weight_showBasketProduct']);
			$this->shop->update_option('wpsg_mod_weight_showOverview', $_REQUEST['wpsg_mod_weight_showOverview']);
			$this->shop->update_option('wpsg_mod_weight_showOverviewProduct', $_REQUEST['wpsg_mod_weight_showOverviewProduct']);
			
			if ($this->shop->hasMod('wpsg_mod_request'))
			{
				
				$this->shop->update_option('wpsg_mod_weight_showRequestPage', $_REQUEST['wpsg_mod_weight_showRequestPage']);
				$this->shop->update_option('wpsg_mod_weight_showRequestPageProduct', $_REQUEST['wpsg_mod_weight_showRequestPageProduct']);
				
			}
			
			if ($this->shop->hasMod('wpsg_mod_productindex'))
			{
				$this->shop->update_option('wpsg_mod_weight_showProductindex', $_REQUEST['wpsg_mod_weight_showProductindex']);
			}
			
		} // public function settings_save()
		
		public function basket_row(&$p, $i) 
		{ 

			if ($this->shop->get_option('wpsg_mod_weight_showBasketProduct') != '1') return;
			
			$this->shop->view['weight'] = $p['weight'];

			$this->shop->view['i'] = $i;			
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_weight/basket_row.phtml');
			
		} // public function basket_row(&$p, $i)
		
		public function basket_row_end(&$basket_view) 
		{ 
			
			if ($this->shop->get_option('wpsg_mod_weight_showBasket') != '1') return;

			$this->shop->view['wpsg_mod_weight']['weight'] = $basket_view['basket']['sum']['weight'];
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_weight/basket_row_end.phtml');
			
		} // public function basket_row_end(&$basket)
		
		public function overview_row(&$p, $i) 
		{ 
			
			if ($this->shop->get_option('wpsg_mod_weight_showOverviewProduct') != '1') return;
			
			$this->shop->view['i'] = $i;			
			$this->shop->view['weight'] = $p['weight'];
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_weight/overview_row.phtml');
			
		} // public function overview_row(&$p, $i) 

		public function overview_row_end(&$overview_view) { 
			
			if ($this->shop->get_option('wpsg_mod_weight_showOverview') != '1') return;
			
			$this->shop->view['wpsg_mod_weight']['weight'] = $overview_view['basket']['sum']['weight'];
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_weight/overview_row_end.phtml');			
			
		} // public function overview_row_end(&$overview_view)
		
		public function produkt_edit_allgemein(&$produkt_data) 
		{ 
			
			if (isset($_REQUEST['wpsg_lang'])) return;
			
			$this->shop->view['wpsg_mod_weight']['weight'] = $produkt_data['weight'];
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_weight/produkt_edit_allgemein.phtml');
			
		} // public function produkt_edit_allgemein(&$produkt_data)

		public function produkt_save_before(&$produkt_data)
		{ 
			
			$produkt_data['weight'] = wpsg_tf($_REQUEST['wpsg_mod_weight']['weight']);
			
		} // public function produkt_save_before(&$produkt_data)
		
		public function loadProduktArray(&$product_data)
		{
			
			$product_data['weight'] = $this->getWeight($product_data['id']);
			
		}
						
		public function basket_toArray(&$produkt, $backend = false, $noMwSt = false)
		{
			
			$produkt['weight_one'] = $this->getWeight($produkt['id']);
			$produkt['weight'] = $this->getWeight($produkt['id']) * $produkt['menge'];
		 
		}
		 		
		public function basket_toArray_preshippayment(&$basket, &$arBasket) 
		{  
			
			if ($basket->loadFromSession)
			{
			
				$weight_sum = 0;
				
				foreach ($arBasket['produkte'] as $k => $p)
				{
					
					$weight_sum += $p['weight'];
					
				}
				
				$arBasket['sum']['weight'] = $weight_sum;
				
			}
			else if ($basket->o_id > 0)
			{
				
				$order_data = $this->shop->cache->loadOrder($basket->o_id);
				
				$arBasket['sum']['weight'] = $order_data['weight'];
				
			}
		
		} // public function basket_toArray_final(&$basket, &$arBasket)
				
		public function basket_save_order(&$data, &$checkout, &$arBasket, $finish_order) { 
			
			$data['weight'] = $arBasket['sum']['weight'];
			
		} // public function basket_save_order(&$data, &$checkout, &$arBasket, $finish_order)
		
		public function basket_save_product(&$data, &$product_data) 
		{ 

			$data['weight'] = $product_data['weight'];

		} // public function basket_save_product(&$data, &$product_data)
		
		/* --- */
		
		public function getWeight($product_key)
		{
			 
			$product_data_db = $this->shop->cache->loadProduct($this->shop->getProduktId($product_key));
			
			if ($this->shop->hasMod('wpsg_mod_varianten') && $this->shop->callMod('wpsg_mod_varianten', 'isVariantsProductKey', array($product_key)))
			{
			
				$variInfo = $this->shop->callMod('wpsg_mod_varianten', 'getVariantenInfoArray', array($product_key));
				$weight = $product_data_db['weight'] + $variInfo['weight'];
				
			}
			else
			{
			
				$weight = $product_data_db['weight'];
			
			}
			
			return $weight;
			
		} // public function getWeight($produkt_key)
		 		
	} // class wpsg_mod_weight extends wpsg_mod_basic

?>