<?php

	/**
	 * Dieses Module ermöglicht die Zahlungsart "Vorkasse"
	 */
	class wpsg_mod_prepayment extends wpsg_mod_basic
	{
		
		var $lizenz = 2;
		var $id = 1;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Vorkasse', 'wpsg');
			$this->group = __('Zahlungsarten', 'wpsg');
			$this->desc = __('Ermöglicht die Zahlungsart Vorkasse.', 'wpsg');
									
		} // public function __construct()
		
		public function install()
		{
			
			$this->shop->checkDefault('wpsg_mod_prepayment_bezeichnung', $this->name, false, true);
			$this->shop->checkDefault('wpsg_mod_prepayment_aktiv', '1');
			$this->shop->checkDefault('wpsg_mod_prepayment_hint', __('Zahlen Sie die Bestellung mittels Überweisung. Der Betreff wird ihnen in der Bestellbestätigung mitgeteilt.', 'wpsg'), false, true);
			$this->shop->checkDefault('wpsg_mod_prepayment_subject', 'O%order_id% - K%kunde_id%');
			$this->shop->checkDefault('wpsg_mod_prepayment_mwst', '0');
			$this->shop->checkDefault('wpsg_mod_prepayment_mwstland', '0');
			
			$this->shop->checkDefault('wpsg_mod_prepayment_kinhaber', '%shopinfo_accountowner%');
			$this->shop->checkDefault('wpsg_mod_prepayment_bank', '%shopinfo_bankname%');
			$this->shop->checkDefault('wpsg_mod_prepayment_iban', '%shopinfo_iban%');
			$this->shop->checkDefault('wpsg_mod_prepayment_swift', '%shopinfo_bic%');
			
		} // public function install()
		
		public function settings_edit()
		{
			 		
			$this->render(WPSG_PATH_VIEW.'/mods/mod_prepayment/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_mod_prepayment_bezeichnung', $_REQUEST['wpsg_mod_prepayment_bezeichnung']);
			$this->shop->update_option('wpsg_mod_prepayment_aktiv', $_REQUEST['wpsg_mod_prepayment_aktiv']);
			$this->shop->update_option('wpsg_mod_prepayment_hint', $_REQUEST['wpsg_mod_prepayment_hint']);
			$this->shop->update_option('wpsg_mod_prepayment_gebuehr', wpsg_tf($_REQUEST['wpsg_mod_prepayment_gebuehr'], true));
			$this->shop->update_option('wpsg_mod_prepayment_mwst', $_REQUEST['wpsg_mod_prepayment_mwst']);
			$this->shop->update_option('wpsg_mod_prepayment_mwstland', $_REQUEST['wpsg_mod_prepayment_mwstland']);
			$this->shop->update_option('wpsg_mod_prepayment_kinhaber', $_REQUEST['wpsg_mod_prepayment_kinhaber']);		
			$this->shop->update_option('wpsg_mod_prepayment_bank', $_REQUEST['wpsg_mod_prepayment_bank']);
			$this->shop->update_option('wpsg_mod_prepayment_iban', $_REQUEST['wpsg_mod_prepayment_iban']);
			$this->shop->update_option('wpsg_mod_prepayment_swift', $_REQUEST['wpsg_mod_prepayment_swift']);
			$this->shop->update_option('wpsg_mod_prepayment_subject', $_REQUEST['wpsg_mod_prepayment_subject']);
			
			$this->shop->addTranslationString('wpsg_mod_prepayment_bezeichnung', $_REQUEST['wpsg_mod_prepayment_bezeichnung']);
			$this->shop->addTranslationString('wpsg_mod_prepayment_hint', $_REQUEST['wpsg_mod_prepayment_hint']);
			
		} // public function settings_save()
		
		public function checkGeneralBackendError() 
		{ 

			if ($this->shop->get_option('wpsg_mod_prepayment_aktiv') === '1')
			{
				
				if (	!wpsg_isSizedString($this->shop->get_option('wpsg_mod_prepayment_bank')) ||
						!wpsg_isSizedString($this->shop->get_option('wpsg_mod_prepayment_iban')) ||
						!wpsg_isSizedString($this->shop->get_option('wpsg_mod_prepayment_swift')) || 
						!wpsg_isSizedString($this->shop->get_option('wpsg_mod_prepayment_kinhaber'))	)
				{
					
					$this->shop->addBackendError('nohspc_'.wpsg_translate(
						wpsg_translate(
							__('Die Zahlungsart "Vorkasse" ist aktiv, es wurden aber nur unvollständige Kontodaten angegeben. Überprüfen Sie die <a href="#1#">Moduleinstellungen</a>.', 'wpsg'),
							WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Admin&action=module&modul=wpsg_mod_prepayment'
						)
					));
					
				}
				
			}
			
		} // public function checkGeneralBackendError()
		
		public function addPayment(&$arPayment) 
		{ 
 
			if (!is_admin() && $this->shop->get_option('wpsg_mod_prepayment_aktiv') != '1') return;
			
			$mwst_value = $this->getTaxValue($this->shop->get_option('wpsg_mod_prepayment_mwst'), $this->shop->getDefaultCountry());
			
			$arPayment[$this->id] = array(
				'id' => $this->id,
				'name' => __($this->shop->get_option('wpsg_mod_prepayment_bezeichnung'), 'wpsg'),
				'preis' => $this->shop->get_option('wpsg_mod_prepayment_gebuehr'),
				'mwst' => $this->shop->get_option('wpsg_mod_prepayment_mwst'),
				'mwst_value' => $mwst_value,
				'mwst_null' => $this->shop->get_option('wpsg_mod_prepayment_mwstland'),
				'hint' => $this->shop->get_option('wpsg_mod_prepayment_hint')
			);
			
			if (isset($_REQUEST['wpsg_checkout2']))
			{

				$arBasket = $this->shop->basket->toArray();
				
				if ($this->shop->get_option('wpsg_preisangaben') == WPSG_NETTO || $arBasket['noMwSt'])
				{
						
					$payment_netto = $this->getPreis($this->shop->get_option('wpsg_mod_prepayment_gebuehr'), $arBasket['sum']['preis']);
					$payment_brutto = wpsg_calculatePreis($payment_netto, WPSG_BRUTTO, $mwst_value);
						
				}
				else
				{
												
					$payment_brutto = $this->getPreis($this->shop->get_option('wpsg_mod_prepayment_gebuehr'), $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]['price'] = $payment_netto;
					
				}
				else 
				{
					
					$arPayment[$this->id]['price'] = $payment_brutto;
					
				}
				
			}
			
		} // public function addPayment(&$arShipping)
		
		public function calcPayment(&$arBasket) 
		{ 
			
			if (wpsg_isSizedInt($arBasket['checkout']['payment'], $this->id))
			{

				$payment_price = $this->getPreis($this->shop->get_option('wpsg_mod_prepayment_gebuehr'), $arBasket['sum']['preis']);
				
				if ($this->shop->get_option('wpsg_mod_prepayment_mwstland') == '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, $this->shop->get_option('wpsg_mod_prepayment_mwst'));

				}
								
			}
			
		} // public function calcPayment(&$arBasket)
 		
		public function order_done(&$order_id, &$done_view) 
		{ 

			// Bestellungen mit 0 geben nix aus
			if ($done_view['basket']['sum']['preis_gesamt_brutto'] <= 0) return;
			
			if ($this->shop->view['basket']['checkout']['payment'] == $this->id)
			{
				
				$this->shop->view['wpsg_mod_prepayment']['subject'] = $this->get_subject($order_id);
				
				echo $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_prepayment/order_done.phtml', false);

			}
						
		} // public function order_done(&$order_id)
		
		public function mail_payment() 
		{ 

			if ($this->shop->view['basket']['checkout']['payment'] != $this->id) return;
			
			$this->shop->view['mod_prepayment']['subject'] = $this->get_subject($this->shop->view['o_id']);
			
			if ($this->shop->htmlMail === true)
			{

				echo $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_prepayment/mail_html.phtml');
				
			}
			else
			{
			
				echo $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_prepayment/mail.phtml');
				
			}
			
		} // public function mail_payment()
				
		/**
		 * Ersetzt die Platzhalter im Betreff
		 */
		public function get_subject($order_id)
		{
			
			return $this->shop->replaceUniversalPlatzhalter($this->shop->get_option('wpsg_mod_prepayment_subject'), $order_id);
			
		} // private function get_subject($order_id)
		
	} // class wpsg_mod_prepayment extends wpsg_mod_basic

?>