<?php

	/**
	 * Dieses Modul erlaubt dynamische Zahlungsvarianten
	 * @author daniel
	 */
	class wpsg_mod_userpayment extends wpsg_mod_basic 
	{
		
		var $lizenz = 2;
		var $id = 10;
		var $inline = true;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Zahlvarianten', 'wpsg');
			$this->group = __('Zahlungsarten', 'wpsg');
			$this->desc = __('Erlaubt das anlegen von benutzerdefinierten Zahlungsarten.', 'wpsg');
									
		} // public function __construct()
						
		public function install()
		{
			 
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
			
			/**
			 * Tabelle für die Zahlvarianten
			 */ 
			$sql = "CREATE TABLE ".WPSG_TBL_ZV." (
		   		id mediumint(9) NOT NULL AUTO_INCREMENT,
		   		name varchar(255) NOT NULL,
		   		rabgeb varchar(255) NOT NULL,
		   		hint TEXT NOT NULL,		   
				mwst_key VARCHAR(1) NOT NULL,		  
		   		mwst_laender int(1) NOT NULL,
		   		aktiv int(1) NOT NULL,
		   		PRIMARY KEY  (id)
		   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
   	   	 
   			dbDelta($sql);
			
		} // public function install()
		
		public function settings_edit()
		{
 
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_userpayment/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save() {
			
		} // public function settings_save()
		
		public function be_ajax()
		{
			
			if ($_REQUEST['do'] == 'add')
			{
				
				$new_name = __('Anklicken um den Namen der Zahlvariante zu ändern ...', 'wpsg');
				
				// Versandzone in Datenbank eintragen
				$zv_id = $this->db->ImportQuery(WPSG_TBL_ZV, array(
					'name' => wpsg_q($new_name),
					'aktiv' => '1',
					'mwst_key' => 'c',
					'rabgeb' => 0
				));
				
				$this->shop->addTranslationString('wpsg_mod_userpayment_'.$zv_id, $new_name);
				
				die($this->zv_list());
				
			}
			else if ($_REQUEST['do'] == 'remove')
			{
				
				$this->db->Query("DELETE FROM `".WPSG_TBL_ZV."` WHERE `id` = '".wpsg_q($_REQUEST['zv_id'])."'");
				
				die($this->zv_list());
				
			}
			else if ($_REQUEST['do'] == 'inlinedit')
			{
				
				if ($_REQUEST['field'] == 'name')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_ZV, array(
						'name' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['zv_id'])."'");
					
					$this->shop->addTranslationString('wpsg_mod_userpayment_'.$_REQUEST['zv_id'], $_REQUEST['value']);
					
					die($_REQUEST['value']);
					
				} 
				else if ($_REQUEST['field'] == 'hint')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_ZV, array(
						'hint' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['zv_id'])."'");
					
					$this->shop->addTranslationString('wpsg_mod_userpayment_hint_'.$_REQUEST['zv_id'], $_REQUEST['value']);
					
					die($_REQUEST['value']);
					
				}
				else if ($_REQUEST['field'] == 'mwst_key')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_ZV, array(
						'mwst_key' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['zv_id'])."'");
					
					$tax_groups = wpsg_tax_groups();
					die(wpsg_hspc($tax_groups[$_REQUEST['value']]));
					
				}
				else if ($_REQUEST['field'] == 'mwst_laender')
				{
				
					$this->db->UpdateQuery(WPSG_TBL_ZV, array(
						'mwst_laender' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['zv_id'])."'");

					die();
					
				}
				else if ($_REQUEST['field'] == 'rabgeb')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_ZV, array(
						'rabgeb' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['zv_id'])."'");
					 
					die(wpsg_ff($_REQUEST['value']));
					
				}
				else if ($_REQUEST['field'] == 'aktiv')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_ZV, array(
						'aktiv' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['zv_id'])."'");

					die();
					
				}
				
			}
			
		} // public function be_ajax()
				
		public function addPayment(&$arPayment) 
		{ 

			if (isset($_REQUEST['wpsg_checkout2']))
			{

				$arBasket = $this->shop->basket->toArray();
				
			}
			
			// Im Checkout2 wird der Wert berechnet und eventuell nicht verfügbare ZV ausgeschlossen
			$arZV = $this->db->fetchAssoc("
				SELECT * FROM `".WPSG_TBL_ZV."` ORDER BY `name` ASC
			");
				  
			foreach ((array)$arZV as $zv)
			{
				
				if (!is_admin() && $zv['aktiv'] != '1') continue;
				
				$mwst_value = $this->getTaxValue($zv['mwst_key'], $this->shop->getDefaultCountry());
				
				$hint = $zv['hint'];
				
				$arPayment[$this->id.'_'.$zv['id']] = array(
					'id' => $this->id.'_'.$zv['id'],
					'name' => __($zv['name'], 'wpsg'),
					'hint' => __($hint, 'wpsg'),
					'preis' => $zv['rabgeb'],
					'mwst_key' => $zv['mwst_key'],
					'mwst_value' => $mwst_value,
					'mwst_null' => $zv['mwst_laender'],
					'deleted' => $zv['deleted']
				);	
				
				if (isset($_REQUEST['wpsg_checkout2']))
				{
				
					if ($this->shop->get_option('wpsg_preisangaben') == WPSG_NETTO || $arBasket['noMwSt'])
					{
							
						$payment_netto = $this->getPreis($zv['rabgeb'], $arBasket['sum']['preis']); 
						$payment_brutto = wpsg_calculatePreis($payment_netto, WPSG_BRUTTO, $mwst_value);
							
					}
					else
					{
													
						$payment_brutto = $this->getPreis($zv['rabgeb'], $arBasket['sum']['preis']);																							
						$payment_netto = wpsg_calculatePreis($payment_brutto, WPSG_NETTO, $mwst_value);						
							
					}
					 
					// Entsprechenden Preis anzeigen
					if ($this->shop->getFrontendTaxview() == WPSG_NETTO)
					{
						
						$arPayment[$this->id.'_'.$zv['id']]['price'] = $payment_netto;
						
					}
					else 
					{
						
						$arPayment[$this->id.'_'.$zv['id']]['price'] = $payment_brutto;
						
					} 
					
				}
				
			}
			
		} // public function addPayment(&$arPayment)
		
		public function calcPayment(&$arBasket) 
		{ 
			
			if (preg_match('/'.$this->id.'_\d+/', $arBasket['checkout']['payment']))
			{
			
				$zv = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_ZV."` WHERE `id` = '".wpsg_q(preg_replace('/^\d*_/', '', $arBasket['checkout']['payment']))."'");

				$payment_price = $this->getPreis($zv['rabgeb'], $arBasket['sum']['preis']);
				
				if ($va['mwst_akt'] != '1' && $arBasket['noMwSt'])
				{
					
					// Keine MwSt. berechnen !
					$arBasket['sum']['preis_payment'] = $payment_price;
					$arBasket['sum']['preis_payment_brutto'] = $payment_price;
					$arBasket['sum']['preis_payment_netto'] = $payment_price;
					
					$arBasket['payment']['mwst'] = 0;
					$this->shop->arPayment[$arBasket['checkout']['payment']]['mwst_value'] = 0;
						
					$arBasket['payment']['tax_rata'] = false;
					
				}
				else
				{
					
					// MwSt berechnen (Angaben im Backend sind immer Brutto)
					$this->setPaymentKosten($arBasket, $payment_price, $zv['mwst_key']);

				}
								
			}
			
		} // public function calcPayment(&$arBasket) 
		
		/* ---- */
				
		public function zv_list()
		{
			
			$this->shop->view['wpsg_mod_userpayment']['data'] = $this->db->fetchAssoc("
				SELECT
					ZA.*
				FROM
					`".WPSG_TBL_ZV."` AS ZA				
			");
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_userpayment/list.phtml');
			
		} // public function zv_list()
		
	} // class wpsg_mod_userpayment extends mod_basic

?>