<?php

	/**
	 * Ermgöglicht das Festlegen und Zahlen mit einem Kundenbudget
	 */
	class wpsg_mod_customerbudget extends wpsg_mod_basic 
	{
		
		var $lizenz = 1;
		var $id = 1001;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Kundenbudget', 'wpsg');
			$this->group = __('Sonstiges', 'wpsg');
			$this->desc = __('Ermöglicht das Verwalten von Kundenbudgets und die Zahlung mit dem Budget.', 'wpsg');
									
		} // public function __construct()
		
		public function install()
		{
			
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
			
			/**
			 * Kundentabelle erweitern
			 */ 
			$sql = "CREATE TABLE ".WPSG_TBL_KU." (
		   		budget double(10,2) NOT NULL,
		   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
   	   	 
   			dbDelta($sql);
			
   			if ($this->shop->get_option('wpsg_mod_customerbudget_name') === false || trim($this->shop->get_option('wpsg_mod_customerbudget_name')) == '')
   			{
   				
   				$default_name = __('Kundenbudget', 'wpsg');
   				$this->shop->update_option('wpsg_mod_customerbudget_name', $default_name);
   				$this->shop->addTranslationString('wpsg_mod_customerbudget_name', $default_name);
   				
   			}
   			
			if ($this->shop->get_option('wpsg_mod_customerbudget_hint') === false || trim($this->shop->get_option('wpsg_mod_customerbudget_hint')) == '')
   			{
   				
   				$default_hint = __('Zahlen Sie ihre Rechnung mit ihrem Kundenbudget.', 'wpsg');
   				$this->shop->update_option('wpsg_mod_customerbudget_hint', $default_hint);
   				$this->shop->addTranslationString('wpsg_mod_customerbudget_hint', $default_hint);
   				
   			}
   			
		} // public function install()
		
		public function settings_edit() {
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_customerbudget/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save() {
		
			$this->shop->update_option('wpsg_mod_customerbudget_name', $_REQUEST['wpsg_mod_customerbudget_name']);
			$this->shop->addTranslationString('wpsg_mod_customerbudget_name', $_REQUEST['wpsg_mod_customerbudget_name']);
			
			$this->shop->update_option('wpsg_mod_customerbudget_hint', $_REQUEST['wpsg_mod_customerbudget_hint']);
			$this->shop->addTranslationString('wpsg_mod_customerbudget_hint', $_REQUEST['wpsg_mod_customerbudget_hint']);
			
		} // public function settings_save()
				
		public function wpsg_mod_customer_sidebar(&$customer_data)
		{
			
			$this->shop->view['mod_customerbudget']['budget'] = wpsg_ff($customer_data['budget']);
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_customerbudget/customer_sidebar.phtml');
			
		} // public function wpsg_mod_customer_sidebar(&$customer_data)
		
		public function wpsg_mod_customer_save(&$customer_data) { 
			
			$customer_data['budget'] = wpsg_tf($_REQUEST['wpsg_mod_customerbudget_budget']);
			
		} // public function wpsg_mod_customer_save(&$customer_data)
				
		public function addPayment(&$arPayment) 
		{ 
			
			$arPayment[$this->id] = array(
				'id' 			=> $this->id,
				'name' 			=> __($this->shop->get_option('wpsg_mod_customerbudget_name'), 'wpsg'),
				'preis' 		=> 0,
				'preis_calc' 	=> 0,
				'mwst' 			=> 0,
				'mwst_value' 	=> 0,
				'hint' 			=> __($this->shop->get_option('wpsg_mod_customerbudget_hint'), 'wpsg') 
			);
			 
		} // public function addPayment(&$arShipping)
		
		public function checkCheckout(&$state, &$error, &$arCheckout) { 
			
			if ($arCheckout['payment'] == $this->id)
			{

				if ($arCheckout['id'] <= 0) 
				{
					
					$this->shop->addFrontendError(__('Das Kundenbudget ist nur für eingelogte Kunden verfügbar!', 'wpsg'));
					$error = true; return;
					
				}
				
				$kunde = $this->shop->cache->loadKunden($arCheckout['id']);
				
				$arBasket = $this->shop->basket->toArray();
				
				if ($kunde['budget'] <= 0 || $kunde['budget'] < $arBasket['sum']['preis_gesamt_brutto'])
				{
					
					$this->shop->addFrontendError(__('Ihr Budget reicht für diese Bestellung leider nicht mehr aus!', 'wpsg'));
					$error = true; return;
					
				}
				
			}			
			
		} // public function checkCheckout(&$state, &$error, &$arCheckout)
		
		public function basket_save_order(&$data, &$checkout, &$arBasket, $finish_order) 
		{ 
			
			if ($finish_order !== true) return;
			
			if ($checkout['payment'] == $this->id)
			{
				
				$kunde = $this->shop->cache->LoadKunden($checkout['id']);
				
				$this->db->UpdateQuery(WPSG_TBL_KU, array(
					'budget' => wpsg_q($kunde['budget'] - $data['price_gesamt'])
				), "`id` = '".wpsg_q($checkout['id'])."'");
				
			}
			
		} // public function basket_save_order(&$data, &$checkout, &$arBasket, $finish_order)
		
		public function calcPayment(&$arBasket) { 
			
			
			if ($arBasket['checkout']['payment'] == $this->id && $this->shop->callMod('wpsg_mod_kundenverwaltung', 'isLoggedIn') && !is_admin() && isset($_REQUEST['wpsg_checkout2']))
			{
				
				$kunde = $this->shop->cache->loadKunden($_SESSION['wpsg']['checkout']['id']);
				
				if (trim($this->shop->arPayment[$this->id]['hint']) != '') $this->shop->arPayment[$this->id]['hint'] .= '<br />';
				
				$this->shop->arPayment[$this->id]['hint'] .= wpsg_translate(__('Kontostand: #1#', 'wpsg'), wpsg_ff($kunde['budget'], $this->shop->get_option('wpsg_currency')));				
				
			}		
			 
		} // public function calcPayment(&$arBasket)
		
	} // class wpsg_mod_customerbudget extends wpsg_mod_basic

?>