<?php 

	/**
	 * Modul für die Eingabe einer Versandadresse während der Bestellung
	 * @author daniel
	 *
	 */
	class wpsg_mod_shippingadress extends wpsg_mod_basic 
	{
		 
		var $lizenz = 2;
		var $id = 135;

		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Lieferadresse', 'wpsg');
			$this->group = __('Versand', 'wpsg');
			$this->desc = __('Erlaubt das Angeben einer Lieferadresse, unabhängig von der Rechnungsadresse.', 'wpsg');
						
		} // public function __construct()
		 
		/**
		 * zeigt das Formular zu den Einstellungen des Moduls im BE an
		 * 
		 */
		public function settings_edit()
		{
									
		} // public function settings_edit()
				
		/**
		 * speichert die Einstellungen zum Modul
		 * 
		 */
		public function settings_save()
		{
		
		} // public function settings_save()
				
		public function order_ajax()
		{
				
			parse_str($_REQUEST['form_data'], $form_data);
			 
			$this->db->UpdateQuery(WPSG_TBL_ORDER, array(
				'shipping_firma' => wpsg_q($form_data['dialog_shipping_firma']),
				'shipping_title' => wpsg_q($form_data['dialog_shipping_title']),
				'shipping_vname' => wpsg_q($form_data['dialog_shipping_vname']),
				'shipping_name' => wpsg_q($form_data['dialog_shipping_name']),
				'shipping_strasse' => wpsg_q($form_data['dialog_shipping_strasse']),
				'shipping_plz' => wpsg_q($form_data['dialog_shipping_plz']),
				'shipping_ort' => wpsg_q($form_data['dialog_shipping_ort']),
				'shipping_land' => wpsg_q($form_data['dialog_shipping_land'])
			), " `id` = '".wpsg_q($_REQUEST['edit_id'])."' ");
			
			die("1");
				
		} // public function order_ajax()
		
		/**
		 * fügt dem checkout zusätzliche Felder für eine seperate Lieferdresse ein
		 * 
		 */
		public function checkout_inner_prebutton(&$checkout_view)
		{
			
			$this->shop->view['data'] = $_SESSION['wpsg']['checkout'];
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_shippingadress/checkout_inner_prebutton.phtml');
			
		} // public function checkout_inner_prebutton(&$checkout_view)
		
		public function basket_save_order(&$data, &$checkout, &$arBasket, $finish_order)
		{ 
 
			if ($checkout['diff_shippingadress'] == '1')
			{

				$data['shipping_title'] 	= wpsg_q($checkout['shipping_title']);
				$data['shipping_vname'] 	= wpsg_q($checkout['shipping_vname']);
				$data['shipping_name'] 		= wpsg_q($checkout['shipping_name']);
				$data['shipping_strasse'] 	= wpsg_q($checkout['shipping_strasse']); 
				$data['shipping_plz'] 		= wpsg_q($checkout['shipping_plz']);
				$data['shipping_ort'] 		= wpsg_q($checkout['shipping_ort']);
				$data['shipping_firma'] 	= wpsg_q($checkout['shipping_firma']);
				$data['shipping_land'] 		= wpsg_q($checkout['shipping_land']);
				 
			}
			else 
			{
				
				$data['shipping_title']		= wpsg_q($checkout['title']);
				$data['shipping_vname'] 	= wpsg_q($checkout['vname']);
				$data['shipping_name'] 		= wpsg_q($checkout['name']);
				$data['shipping_strasse'] 	= wpsg_q($checkout['strasse']); 
				$data['shipping_plz'] 		= wpsg_q($checkout['plz']);
				$data['shipping_ort'] 		= wpsg_q($checkout['ort']);
				$data['shipping_firma'] 	= wpsg_q($checkout['firma']);
				$data['shipping_land'] 		= wpsg_q($checkout['land']);				
				
			}

		} // public function basket_save_order(&$data, &$checkout, &$arBasket, $finish_order)
		
		public function checkCheckout(&$state, &$error, &$arCheckout)  
		{
			
			if (isset($_REQUEST['wpsg_checkout']))
			{
		 
				if ($_REQUEST['wpsg']['checkout']['diff_shippingadress'] == '1')
				{
	
					$arCheckout['diff_shippingadress'] = 1;
					($arCheckout['shipping_title'] == '')?$_SESSION['wpsg']['errorFields'][] = 'shipping_title':'';
					($arCheckout['shipping_plz'] == '')?$_SESSION['wpsg']['errorFields'][] = 'shipping_plz':'';
					($arCheckout['shipping_vname'] == '')?$_SESSION['wpsg']['errorFields'][] = 'shipping_vname':'';
					($arCheckout['shipping_name'] == '')?$_SESSION['wpsg']['errorFields'][] = 'shipping_name':'';
					($arCheckout['shipping_strasse'] == '')?$_SESSION['wpsg']['errorFields'][] = 'shipping_strasse':'';
					($arCheckout['shipping_ort'] == '')?$_SESSION['wpsg']['errorFields'][] = 'shipping_ort':'';
					(!wpsg_isSizedInt($arCheckout['shipping_land']))?$_SESSION['wpsg']['errorFields'][] = 'shipping_land':'';
					
					if (count($_SESSION['wpsg']['errorFields']) > 0)
					{
						
						$this->shop->addFrontendError(__('Bitte überprüfen Sie die Eingaben bei der Lieferadresse.', 'wpsg'));
						
						$error = true;
						
					}
					
				}
				else
				{
	
					unset($_SESSION['wpsg']['checkout']['shipping_title']); unset($arCheckout['shipping_title']);
					unset($_SESSION['wpsg']['checkout']['shipping_plz']); unset($arCheckout['shipping_plz']);
					unset($_SESSION['wpsg']['checkout']['shipping_vname']); unset($arCheckout['shipping_vname']);
					unset($_SESSION['wpsg']['checkout']['shipping_name']); unset($arCheckout['shipping_name']);				
					unset($_SESSION['wpsg']['checkout']['shipping_strasse']); unset($arCheckout['shipping_strasse']);
					unset($_SESSION['wpsg']['checkout']['shipping_ort']); unset($arCheckout['shipping_ort']);
					unset($_SESSION['wpsg']['checkout']['shipping_land']); unset($arCheckout['shipping_land']);
					unset($_SESSION['wpsg']['checkout']['shipping_firma']); unset($arCheckout['shipping_firma']);
					unset($_SESSION['wpsg']['checkout']['shipping_land']); unset($arCheckout['shipping_land']);
					unset($_SESSION['wpsg']['checkout']['diff_shippingadress']); unset($arCheckout['diff_shippingadress']);
									
				}
				
			}

		} // public function checkCheckout(&$state, &$error, &$arCheckout)
				
		public function wpsg_order_view_customerdata(&$order_id) 
		{ 
			
			$order_data = $this->shop->cache->loadOrder($order_id);
			
			if ($this->check_different_shippingadress($order_data['k_id'], $order_id) === true)
			{

				$this->shop->view['wpsg_mod_shippingadress'] = $order_data;
				
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_shippingadress/wpsg_order_view_customerdata.phtml');
				
			}
			
		} // public function wpsg_order_view_customerdata(&$order_id)

		// Modulfunktionen
		public function check_different_shippingadress($k_id, $o_id)
		{	
			
			$arrAdrKunde = $this->db->fetchRow("SELECT 
													`name`, `vname`, `firma`, `strasse`, `plz`, `ort`, `land` 
												FROM
													`".WPSG_TBL_KU."`
												WHERE
													`id` = '".wpsg_q($k_id)."'");
	
			$arrAdrShipping = $this->db->fetchRow("SELECT
													`shipping_name` AS name,
													`shipping_vname` AS vname,
													`shipping_firma` AS firma,
													`shipping_strasse` AS strasse,
													`shipping_plz` AS plz,
													`shipping_ort` AS ort,
													`shipping_land` AS land
												FROM
													`".WPSG_TBL_ORDER."`
												WHERE
													`id` = '".wpsg_q($o_id)."'");
	
			if (wpsg_isSizedArray($arrAdrShipping))
			{
				$arrDiff = array_diff((array)$arrAdrKunde, $arrAdrShipping);
			}

			if (wpsg_isSizedArray($arrDiff))
			{
				return true;
			}
			else 
			{
				return false;
			}
			
		} // public function check_different_shippingadress()
		
	}
	
?>