<?php

	/**
	 * Erlaubt die Definition und Anzeige von Lieferzeiten
	 * @author Daschmi (daniel@maennchen1.de)
	 */
	class wpsg_mod_funding extends wpsg_mod_basic
	{
	
		var $lizenz = 1;
		var $id = 2050;  
		var $version = "9.9.9"; 
		
		var $free = true;
		
		var $arStateLabel;
		
		const STATE_COMPLETED = 1;
		const STATE_RUNNING = 2;
		const STATE_CLOSED = 3;
		const STATE_NOTSTARTET = 4;
	
		/**
		 * Costructor
		 */
		public function __construct()
		{
	
			parent::__construct();
	
			$this->name = __('Crowdfunding', 'wpsg');
			$this->group = __('Produkte', 'wpsg');
			$this->desc = __('Erlaubt den Verkauf von Produkten die durch mehrere Käufer finanziert werden.', 'wpsg');
	
			$this->arStateLabel = array(
				self::STATE_COMPLETED => __('Erfolgreich Abgeschlossen', 'wpsg'),
				self::STATE_RUNNING => __('Läuft', 'wpsg'),
				self::STATE_CLOSED => __('Zeitraum abgelaufen', 'wpsg'),
				self::STATE_NOTSTARTET => __('Noch nicht gestartet', 'wpsg')
			);
			
		} // public function __construct()
	
		public function install()
		{
			
			
			
		} // public function install()
		
		public function settings_edit()
		{
		
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_funding/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			
			
		} // public function settings_save()
		
		public function productForSale($product_key) 
		{
			 
			$product_id = $this->shop->getProduktID($product_key);
						
			if ($this->isFundingProduct($product_id) && $this->getFundingState($product_id) != self::STATE_RUNNING) return -2;
						
		} // public function productForSale($product_key) 
		
		public function product_bottom(&$produkt_id, $template_index) 
		{ 
		
			$this->shop->view['wpsg_mod_funding']['state'] = $this->getFundingState($produkt_id);
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_funding/product_bottom.phtml');
			
		} // public function product_bottom(&$produkt_id, $template_index)
		
		public function basket_produkttosession($product_key, &$menge, &$ses_data) 
		{ 
			
			$product_id = $this->shop->getProduktID($product_key);
			
			if ($this->isFundingProduct($product_id))
			{
				
				// Anzahl schon im Warenkorb
				$nBasket = $this->shop->basket->getBasketAmount($product_key);
				
				if ($nBasket + $menge + $this->getCurrentFundingCount($product_id) > $this->getMaxFundingCount($product_id))
				{
					
					$nMengeAllowed = $this->getMaxFundingCount($product_id) - $this->getCurrentFundingCount($product_id)- $nBasket;
					
					if ($nMengeAllowed > 0) 
					{
						
						$menge = $nMengeAllowed;
						$this->shop->addFrontendError(wpsg_translate(
							__('Produkt "#1#" kann nur noch maximal #2# mal erworben werden.', 'wpsg'),
							$this->shop->getProductName($product_id),
							$nMengeAllowed
						));
																		
					}
					else
					{
				
						$this->shop->addFrontendError(wpsg_translate(
							__('Produkt "#1#" kann nicht mehr erworben werden.', 'wpsg'),
							$this->shop->getProductName($product_id)
						));
						
						return -2;
						
					}
					 
				}
				
			}
			
		} // public function basket_produkttosession($produkt_key, &$menge, &$ses_data)
		
		public function product_top_afterheadline($product_id, $template_index) 
		{ 
		
			if (!$this->isFundingProduct($product_id)) return;
			
			$this->shop->view['wpsg_mod_funding'] = array(
				'currentFundingCount' => $this->getCurrentFundingCount($product_id),
				'maxFundingCount' => $this->getMaxFundingCount($product_id),
				'stateLabel' => $this->arStateLabel[$this->getFundingState($product_id)]
			);
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_funding/product_top_afterheadline.phtml');
			
		} // public function product_top_afterheadline($product_id, $template_index)
		
		public function wp_foot() 
		{ 
			
			echo '<script type="text/javascript">/* <![CDATA[ */
			
				function wpsg_mod_funding_scroll()
				{
					
					var scroll_y = jQuery(window).scrollTop();
					
					jQuery(".wpsg_mod_funding_state_value[data-wpsg-funding-width]").each(function() {
					
						if (scroll_y > jQuery(this).offset().top - jQuery(window).height())
						{
					
							jQuery(this).animate( {
								width: "+=" + jQuery(this).attr("data-wpsg-funding-width") + "%",
							}, 750);	
						
							jQuery(this).removeAttr("data-wpsg-funding-width");
						
						}
					
					} );
					
				}
					
				jQuery(window).scroll(wpsg_mod_funding_scroll).ready(wpsg_mod_funding_scroll); 
					
			/* ]]> */</script>';
			
		} // public function wp_foot()
		
		public function produkt_edit_sidebar(&$produkt_data)
		{
		
			if (isset($_REQUEST['wpsg_lang'])) return;
			
			$oProduct = wpsg_product::getInstance($produkt_data['id']);
			
			$this->shop->view['wpsg_mod_funding'] = array();
			
			if ($produkt_data['id'] > 0)
			{
		 
				$this->shop->view['wpsg_mod_funding']['activ'] = $oProduct->getMeta('wpsg_mod_funding_activ');
				$this->shop->view['wpsg_mod_funding']['limit'] = $oProduct->getMeta('wpsg_mod_funding_limit');
				$this->shop->view['wpsg_mod_funding']['start'] = $oProduct->getMeta('wpsg_mod_funding_start');
				$this->shop->view['wpsg_mod_funding']['end'] = $oProduct->getMeta('wpsg_mod_funding_end');
				
			}
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_funding/product_edit_sidebar.phtml');
				
		} // public function produkt_edit_sidebar(&$produkt_data)
		
		public function produkt_save(&$produkt_id)
		{
			
			if (wpsg_isSizedInt($_REQUEST['wpsg_mod_funding_save']))
			{
				
				$oProduct = wpsg_product::getInstance($produkt_id);
				
				foreach ($_REQUEST['wpsg_mod_funding'] as $k => $v)
				{
					
					$oProduct->setMeta('wpsg_mod_funding_'.$k, $v);
					
				}
							
			}
			
		} // public function produkt_save(&$produkt_id)
				
		/* Modulfunktionen */
		
		/**
		 * Gibt den Status der Finanzierung zurück
		 */
		public function getFundingState($product_id)
		{
			
			$oProduct = wpsg_product::getInstance($product_id);
			
			$nCurrentFunding = $this->getCurrentFundingCount($product_id);
			$nMaxFunding = $this->getMaxFundingCount($product_id);
			$start = strtotime($oProduct->getMeta('wpsg_mod_funding_start'));
			$end = strtotime($oProduct->getMeta('wpsg_mod_funding_end'));
			
			if ($nCurrentFunding >= $nMaxFunding) return self::STATE_COMPLETED;			
			if ($end > 0 && time() > $end) return self::STATE_CLOSED;
			if (time() > $start) return self::STATE_RUNNING;
			
			return self::STATE_NOTSTARTET;
			
			
		} // public function getFundingState($product_id)
		
		/**
		 * Gibt true zurück, wenn das Produkt ein Finanzierungsprodukt ist 
		 * @param Int $product_id
		 */
		public function isFundingProduct($product_id)
		{
			
			$oProduct = wpsg_product::getInstance($product_id);
			
			if (wpsg_isSizedInt($oProduct->getMeta('wpsg_mod_funding_activ')) && wpsg_isSizedInt($oProduct->getMeta('wpsg_mod_funding_limit'))) return true;
			else return false;
			
		} // public function isFundingProduct($product_id)
		
		/**
		 * Gibt die derzeitigen Käufe eines Produktes zurück
		 * @param unknown $product_id
		 */
		public function getCurrentFundingCount($product_id)
		{
			
			$oProduct = wpsg_product::getInstance($product_id);
			
			$arFilter = array('product_id' => $product_id, 'NOTstatus' => array(wpsg_ShopController::STATUS_UNVOLLSTAENDIG, wpsg_ShopController::STATUS_STORNIERT));
			if (strtotime($oProduct->getMeta('wpsg_mod_funding_start')) > 0) $arFilter['cdate_from'] = strtotime($oProduct->getMeta('wpsg_mod_funding_start')); 
			
			$arOrder = wpsg_order::find($arFilter);
			$nSum = 0;
			
			foreach ($arOrder as $oOrder)
			{
				
				$nSum += $oOrder->getProductCount($product_id);
								
			}
			
			return $nSum;
			
		} // public function getCurrentFundingCount($product_id)
		
		/**
		 * Gibt die maximale Anzahl an Käufen für ein Produkt zurück
		 * @param unknown $product_id
		 */
		public function getMaxFundingCount($product_id)
		{
			
			$oProduct = wpsg_product::getInstance($product_id);
			
			return $oProduct->getMeta('wpsg_mod_funding_limit');
			
		} // public function getMaxFundingCount($product_id)
		
	} // class wpsg_mod_funding extends wpsg_mod_basic

?>