<?php

	/**
	 * Modul zur verwaltung von Preisstaffeln
	 * Die Konfiguration der Preisstaffeln erfolgt nach Aktivierung im Modul Produke
	 * Ist nur für die Lizenztypen Pro und Enterprise verfügbar!
	 * @author daniel
	 */
	class wpsg_mod_scaleprice extends wpsg_mod_basic 
	{
				
		var $lizenz = 1;
		var $id = 97;
		var $hilfeURL = 'http://wpshopgermany.maennchen1.de/?p=3672';
		
		const TYP_QUANTITY = 0;
		const TYP_WEIGHT = 1;
		
		const CALC_REPLACE = 0;
		const CALC_ADD = 1;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
		
			parent::__construct();
		
			$this->name = __('Staffelpreise', 'wpsg');
			$this->group = __('Produkte', 'wpsg');
			$this->desc = __('Ermöglicht eine gestaffelte Preisgestaltung', 'wpsg');
		
		} // public function __construct()
		
		public function install()
		{
				
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
				
			/**
			 * Produkttabelle erweitern
			*/
			$sql = "CREATE TABLE ".WPSG_TBL_PRODUCTS." (
			  	wpsg_mod_scaleprice_activ int(1) DEFAULT 0 NOT NULL,
				wpsg_mod_scaleprice_typ int(11) DEFAULT 0 NOT NULL,				
				wpsg_mod_scaleprice_calc int(11) DEFAULT 0 NOT NULL
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
					
			dbDelta($sql);
			
			/**
			 * Tabelle für die Staffelpreise
			 */
			$sql = "CREATE TABLE ".WPSG_TBL_SCALEPRICE." (
				id int(11) NOT NULL AUTO_INCREMENT,
				product_id int(11) DEFAULT 0 NOT NULL,
				scale double(10,2) DEFAULT 0 NOT NULL,
				value double(10,2) DEFAULT 0 NOT NULL,
				PRIMARY KEY  (id),
				KEY product_id (product_id)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
		 
			dbDelta($sql);
			
			$this->shop->checkDefault('wpsg_mod_scaleprice_showProductInfo', '1'); 
			
		} // public function install()
		
		public function produkt_edit_sidebar(&$produkt_data) 
		{ 
			
			if (!isset($produkt_data['id'])) return;
			
			$this->shop->view['wpsg_mod_scaleprice']['product'] = $produkt_data; 
			
			$this->shop->view['wpsg_mod_scaleprice']['arTyp'] = array(
				wpsg_mod_scaleprice::TYP_QUANTITY => __('Menge', 'wpsg')	
			);
			
			if ($this->shop->hasMod('wpsg_mod_weight'))
			{
				
				$this->shop->view['wpsg_mod_scaleprice']['arTyp'][wpsg_mod_scaleprice::TYP_WEIGHT] = __('Gewicht', 'wpsg');
				
			}
			
			$this->shop->view['wpsg_mod_scaleprice']['list'] = $this->scaleList($produkt_data['id']);
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_scaleprice/produkt_edit_sidebar.phtml');
			
		} // public function produkt_edit_sidebar(&$produkt_data)
		
		public function produkt_save_before(&$produkt_data) 
		{ 
			
			$produkt_data['wpsg_mod_scaleprice_activ'] = wpsg_q($_REQUEST['wpsg_mod_scaleprice_activ']);
			$produkt_data['wpsg_mod_scaleprice_typ'] = wpsg_q($_REQUEST['wpsg_mod_scaleprice_typ']);
			$produkt_data['wpsg_mod_scaleprice_calc'] = wpsg_q($_REQUEST['wpsg_mod_scaleprice_calc']);
			
		} // public function produkt_save_before(&$produkt_data)
		
		public function produkt_ajax()
		{
				
			if ($_REQUEST['cmd'] == 'add')
			{
				 
				$this->db->ImportQuery(WPSG_TBL_SCALEPRICE, array(
					'product_id' => wpsg_q($_REQUEST['edit_id']),
					'scale' => wpsg_q($_REQUEST['scale']),
					'value' => wpsg_q(wpsg_tf($_REQUEST['value']))	
				));
				
				die($this->scaleList($_REQUEST['edit_id']));
				
			}
			else if ($_REQUEST['cmd'] == 'refresh')
			{
				
				$this->db->UpdateQuery(WPSG_TBL_PRODUCTS, array(
					'wpsg_mod_scaleprice_activ' => '1',
					'wpsg_mod_scaleprice_typ' => wpsg_q($_REQUEST['typ']),
					'wpsg_mod_scaleprice_calc' => wpsg_q($_REQUEST['calc'])
				), "`id` = '".wpsg_q($_REQUEST['edit_id'])."'");
				
				die($this->scaleList($_REQUEST['edit_id'], $_REQUEST['typ']));
				
			}
			else if ($_REQUEST['cmd'] == 'ajaxSave')
			{
				
				$this->db->UpdateQuery(WPSG_TBL_PRODUCTS, array(
					'wpsg_mod_scaleprice_activ' => '1',
					'wpsg_mod_scaleprice_typ' => wpsg_q($_REQUEST['typ']),
					'wpsg_mod_scaleprice_calc' => wpsg_q($_REQUEST['calc'])
				), "`id` = '".wpsg_q($_REQUEST['edit_id'])."'");
				
				die("1");
				
			}
			else if ($_REQUEST['cmd'] == 'remove')
			{
				
				$this->db->Query("DELETE FROM `".WPSG_TBL_SCALEPRICE."` WHERE `id` = '".wpsg_q($_REQUEST['scale_id'])."'");
				
				die("1");
				
			}
			else if ($_REQUEST['cmd'] == 'inlineEdit')
			{
				
				if ($_REQUEST['field'] == 'scale')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_SCALEPRICE, array('scale' => wpsg_q(wpsg_tf($_REQUEST['value']))), "`id` = '".wpsg_q($_REQUEST['scale_id'])."'");
					
					die(wpsg_ff(wpsg_tf($_REQUEST['value'])));
					
				}
				else if ($_REQUEST['field'] == 'value')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_SCALEPRICE, array('value' => wpsg_q(wpsg_tf($_REQUEST['value']))), "`id` = '".wpsg_q($_REQUEST['scale_id'])."'");
					
					die(wpsg_ff(wpsg_tf($_REQUEST['value'])));
					
				}
				
			}
			
		} // public function produkt_ajax()		 

		public function admin_presentation()
		{
			
			echo wpsg_drawForm_Checkbox('wpsg_mod_scaleprice_showProductInfo', __('Preisstaffel im Produkttemplate anzeigen', 'wpsg'), $this->shop->get_option('wpsg_mod_scaleprice_showProductInfo'));
			
		} // public function admin_presentation()
		
		public function admin_presentation_submit()
		{
			
			$this->shop->update_option('wpsg_mod_scaleprice_showProductInfo', $_REQUEST['wpsg_mod_scaleprice_showProductInfo']);
			
		} // public function admin_presentation_submit()
		
		public function product_bottom(&$produkt_id, $template_index) 
		{ 
			
			if ($this->shop->get_option('wpsg_mod_scaleprice_showProductInfo') != '1') return;
				
			$product_data = $this->shop->loadProduktArray($produkt_id);
	 
			if ($product_data['wpsg_mod_scaleprice_activ'] != '1') return;
			 
			if ($this->shop->getFrontendTaxview() == WPSG_BRUTTO) $product_price = $product_data['preis'];
			else $product_price = $product_data['preis_netto'];
			
			$this->shop->view['wpsg_mod_scaleprice']['base'] = $product_price;
			
			$this->shop->view['wpsg_mod_scaleprice']['scale'] = $this->db->fetchAssoc("
				SELECT
					S.`scale`, S.`value`
				FROM
					`".WPSG_TBL_SCALEPRICE."` AS S
				WHERE
					S.`product_id` = '".wpsg_q($produkt_id)."'
				ORDER BY
					S.`scale` ASC
			");
			
			foreach ((array)$this->shop->view['wpsg_mod_scaleprice']['scale'] as $k => $v)
			{
				
				$backend_value = $v['value'];

				if ($this->shop->getBackendTaxView() === WPSG_NETTO && $this->shop->getFrontendTaxView() === WPSG_BRUTTO)
				{
					
					$this->shop->view['wpsg_mod_scaleprice']['scale'][$k]['value'] = wpsg_calculatePreis(
						$backend_value, 
						WPSG_BRUTTO,
						$this->shop->getCalcTaxValue($product_data['mwst_key'])
					);
					
				}
				else if ($this->shop->getBackendTaxView() === WPSG_BRUTTO && $this->shop->getFrontendTaxView() === WPSG_NETTO)
				{
					
					$this->shop->view['wpsg_mod_scaleprice']['scale'][$k]['value'] = wpsg_calculatePreis(
						$backend_value,
						WPSG_NETTO,
						$this->shop->getCalcTaxValue($product_data['mwst_key'])
					);
					
				}
				
				// Eventuell rabattierten Staffelpreis berechnen
				if ($this->shop->hasMod('wpsg_mod_discount'))
				{
				
					$discount_backend_value = $this->shop->callMod('wpsg_mod_discount', 'getDiscountPrice', array($produkt_id, $backend_value));
					
					if ($this->shop->getBackendTaxView() === WPSG_NETTO && $this->shop->getFrontendTaxView() === WPSG_BRUTTO)
					{
						
						$this->shop->view['wpsg_mod_scaleprice']['scale'][$k]['value'] = wpsg_calculatePreis(
							$discount_backend_value, 
							WPSG_BRUTTO, 
							$this->shop->getCalcTaxValue($product_data['mwst_key'])
						);
						
					}
					else if ($this->shop->getBackendTaxView() === WPSG_BRUTTO && $this->shop->getFrontendTaxView() === WPSG_NETTO)
					{
						
						$this->shop->view['wpsg_mod_scaleprice']['scale'][$k]['value'] = wpsg_calculatePreis(
							$discount_backend_value,
							WPSG_NETTO,
							$this->shop->getCalcTaxValue($product_data['mwst_key'])
						);
						
					}
					else
					{
						
						$this->shop->view['wpsg_mod_scaleprice']['scale'][$k]['value'] = $discount_backend_value;
						
					}
					
				}
				
			}
			 
			if (!wpsg_isSizedArray($this->shop->view['wpsg_mod_scaleprice']['scale'])) return;
			
			$this->shop->view['wpsg_mod_scaleprice']['typ'] = $product_data['wpsg_mod_scaleprice_typ'];
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_scaleprice/product_bottom.phtml');
			
		} // public function product_bottom(&$produkt_id, $template_index)
		
		public function loadProduktArray(&$product_data)
		{
						
			$product_key = $product_data['id'];
			$product_id = $this->shop->getProduktId($product_key);
			$product_data_db = $this->shop->cache->loadProduct($product_id);
			 
			if ($product_data_db['wpsg_mod_scaleprice_activ'] == '1')
			{
				
				if ($product_data_db['wpsg_mod_scaleprice_typ'] == wpsg_mod_scaleprice::TYP_QUANTITY)
				{
				
					$scale = $product_data['menge'];
				
				}
				else if ($this->shop->hasMod('wpsg_mod_weight') && $product_data_db['wpsg_mod_scaleprice_typ'] == wpsg_mod_scaleprice::TYP_WEIGHT)
				{
				
					$scale = $this->shop->callMod('wpsg_mod_weight', 'getWeight', array($product_key));
				
				}
				else
				{
				
					$this->shop->throwErrorCode('1640_4');
					return;
				
				}
				
				if ($this->shop->get_option('wpsg_preisangaben') == WPSG_NETTO)
				{
					
					$preis_netto = $this->getScalePrice($product_id, $product_data['preis_netto'], $scale);
					$preis_brutto = wpsg_calculatePreis($preis_netto, WPSG_BRUTTO, $product_data['mwst_value']);
					
				}
				else
				{
					
					$preis_brutto = $this->getScalePrice($product_id, $product_data['preis_brutto'], $scale);
					$preis_netto = wpsg_calculatePreis($preis_brutto, WPSG_NETTO, $product_data['mwst_value']);
					
				}
				
				$product_data['preis_brutto'] = $preis_brutto;
				$product_data['preis_netto'] = $preis_netto;
								
				$product_data['preis_defaultLand_brutto'] = $preis_brutto;
				$product_data['preis_defaultLand_netto'] = $preis_netto;
				
				if ($this->shop->getFrontendTaxview() == WPSG_NETTO)
				{
					
					$product_data['preis'] = $product_data['preis_netto'];
					$product_data['preis_defaultLand'] = $product_data['preis_netto'];
										
				}
				else
				{
					
					$product_data['preis'] = $product_data['preis_brutto'];
					$product_data['preis_defaultLand'] = $product_data['preis_brutto'];
					
				}
				
				$product_data['min_preis'] = $product_data['preis'];
				$product_data['ax_preis'] = $product_data['preis'];
				
			}
			
			//wpsg_debug("Staffel:loadProduktArray = ".$product_data['preis']);
			
		} // public function loadProduktArray(&$product_data)
		 		
		/* Modulfunktionen */
		
		private function getScalePrice($product_id, $price, $scale)
		{
			
			$arScale = $this->db->fetchAssoc("SELECT * FROM `".WPSG_TBL_SCALEPRICE."` WHERE `product_id` = '".wpsg_q($product_id)."' ORDER BY `scale` ASC");
			$produkt_data = $this->shop->cache->loadProduct($product_id);
			
			$set_scale = false;
			
			foreach ($arScale as $s)
			{
				 
				if (wpsg_tf($scale) >= wpsg_tf($s['scale']))
				{
					
					$set_scale = wpsg_tf($s['value']);
					//break;
					
				}
				
			}
			
			if ($set_scale !== false)
			{
				
				if ($produkt_data['wpsg_mod_scaleprice_calc'] == wpsg_mod_scaleprice::CALC_REPLACE)
				{ 
					
					return $set_scale;
					
				}
				else if ($produkt_data['wpsg_mod_scaleprice_calc'] == wpsg_mod_scaleprice::CALC_ADD)
				{
					
					return $price + $set_scale;
					
				}
				else
				{
					
					$this->shop->throwErrorCode('1640_3');
					return $price;
					
				}
				
			}
			else
			{
			
				return $price;
				
			}
			
		} // private function getStaffel($product_id)
		
		private function scaleList($product_id, $typ = false)
		{
			
			if ($typ === false)
			{
			
				$this->shop->view['wpsg_mod_scaleprice']['typ'] = $this->db->fetchOne("SELECT `wpsg_mod_scaleprice_typ` FROM `".WPSG_TBL_PRODUCTS."` WHERE `id` = '".wpsg_q($product_id)."'");
				
			}
			else
			{
				
				$this->shop->view['wpsg_mod_scaleprice']['typ'] = $typ;
				
			}
			
			$this->shop->view['wpsg_mod_scaleprice']['arScale'] = $this->db->fetchAssoc("
				SELECT
					*
				FROM
					`".WPSG_TBL_SCALEPRICE."`
				WHERE
					`product_id` = '".wpsg_q($product_id)."'
				ORDER BY
					`scale` ASC
			");
			 
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_scaleprice/scaleList.phtml', false);
			
		} // private function scaleList($product_id)
		
	} // class wpsg_mod_scaleprice extends wpsg_mod_basic

?>