<?php

	class wpsg_mod_invoice_type extends wpsg_mod_basic 
	{

		var $lizenz = 2;
		var $id = 4;

		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Rechnung', 'wpsg');
			$this->group = __('Zahlungsarten', 'wpsg');
			$this->desc = __('Ermöglicht die Zahlungsart Rechnung.', 'wpsg');
						
		} // public function __construct()
		
		public function install() {
			
			$this->shop->checkDefault('wpsg_mod_invoice_type_bezeichnung', $this->name, false, true);
			$this->shop->checkDefault('wpsg_mod_invoice_type_hint', __('Zahlen Sie die Bestellung auf Rechnung. Die Ware wird dann nach Erhalt des Rechnungsbetrags versendet.', 'wpsg'), false, true);
			$this->shop->checkDefault('wpsg_mod_invoice_type_gebuehr', '0');
			$this->shop->checkDefault('wpsg_mod_invoice_type_mwst', '0');
			$this->shop->checkDefault('wpsg_mod_invoice_type_mwstland', '0');
			
		} // public function install()
		
		public function settings_edit()
		{
									
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_invoice_type/settings_edit.phtml');
			
		} // public function settings_edit()
				
		public function settings_save()
		{
		
			$this->shop->update_option('wpsg_mod_invoice_type_bezeichnung', $_REQUEST['wpsg_mod_invoice_type_bezeichnung']);
			$this->shop->update_option('wpsg_mod_invoide_type_aktiv', $_REQUEST['wpsg_mod_invoide_type_aktiv']);
			$this->shop->update_option('wpsg_mod_invoice_type_hint', $_REQUEST['wpsg_mod_invoice_type_hint']);			
			$this->shop->update_option('wpsg_mod_invoice_type_gebuehr', wpsg_tf($_REQUEST['wpsg_mod_invoice_type_gebuehr'], true));
			$this->shop->update_option('wpsg_mod_invoice_type_mwst', $_REQUEST['wpsg_mod_invoice_type_mwst']);
			$this->shop->update_option('wpsg_mod_invoice_type_mwstland', $_REQUEST['wpsg_mod_invoice_type_mwstland']);			
			
			$this->shop->addTranslationString('wpsg_mod_invoice_type_bezeichnung', $_REQUEST['wpsg_mod_invoice_type_bezeichnung']);
			$this->shop->addTranslationString('wpsg_mod_invoice_type_hint', $_REQUEST['wpsg_mod_invoice_type_hint']);
			 
		} // public function settings_save()
		
		public function addPayment(&$arPayment) 
		{ 

			if (!is_admin() && $this->shop->get_option('wpsg_mod_invoide_type_aktiv') != '1') return;
			
			$mwst_value = $this->getTaxValue($this->shop->get_option('wpsg_mod_invoice_type_mwst'), $this->shop->getDefaultCountry());
			
			$arPayment[$this->id] = array(
				'id' 			=> $this->id,
				'name' 			=> __($this->shop->get_option('wpsg_mod_invoice_type_bezeichnung'), 'wpsg'),
				'hint' => __($this->shop->get_option('wpsg_mod_invoice_type_hint'), 'wpsg'),
				'preis' 		=> $this->shop->get_option('wpsg_mod_invoice_type_gebuehr'),
				'mwst' 			=> $this->shop->get_option('wpsg_mod_invoice_type_mwst'),
				'mwst_value' 	=> $mwst_value,
				'mwst_null' 	=> $this->shop->get_option('wpsg_mod_invoice_type_mwstland')
			);
			
			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_invoice_type_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_invoice_type_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(&$arPayment)
		
		public function calcPayment(&$arBasket)
		{
			
			if ($arBasket['checkout']['payment'] == $this->id)
			{
				
				$payment_price = $this->getPreis($this->shop->get_option('wpsg_mod_invoice_type_gebuehr'), $arBasket['sum']['preis']);
				
				if ($this->shop->get_option('wpsg_mod_invoice_type_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_invoice_type_mwst'));

				}
									
			}
				
		}
		
	}

?>