<?php

	/**
	 * Bankabgleichsmodul
	 * Ermöglicht den Kontoabgleich über exportierte CSV Dateien
	 * @author Daschmi
	 */
	class wpsg_mod_bankalignment extends wpsg_mod_basic
	{
		
		var $lizenz = 2;
		var $id = 1610;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
				
			parent::__construct();
				
			$this->name = __('Bankabgleich', 'wpsg');
			$this->group = __('Bestellung', 'wpsg');
			$this->desc = __('Ermöglicht den Kontoabgleich über exportierte CSV Dateien.', 'wpsg');
		
		} // public function __construct()
		
		public function produkt_index_head() 
		{ 

			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_bankalignment/produkt_index_head.phtml');
			
		} // public function produkt_index_head()
		
		public function produkt_index_aftersearch() 
		{ 
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_bankalignment/produkt_index_aftersearch.phtml');
			
		} // public function produkt_index_aftersearch()
		
		public function install()
		{
			
			$this->shop->checkDefault('wpsg_mod_bankalignment_separator', ';');
			
		} // public function install()
		
		public function settings_edit()
		{
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_bankalignment/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_mod_bankalignment_separator', $_REQUEST['wpsg_mod_bankalignment_separator']);
			
		} // public function settings_save()
		
		public function order_ajax() 
		{ 
			
			if ($_REQUEST['mod'] != 'wpsg_mod_bankalignment') return;			

			if (isset($_REQUEST['wpsg_mod_bankalignment_submit']))
			{
				
				$nSet = 0;
				
				foreach ($_REQUEST['set'] as $order_id)
				{
					
					if ($order_id > 0)
					{
						
						$order = $this->shop->cache->loadOrder($order_id);
						
						if ($order['status'] == 0)
						{
						
							$this->shop->setOrderStatus($order_id, 100, true);
							$nSet ++;
							
						}
						
					}
					
				}
								
				$this->shop->addBackendMessage(wpsg_translate(__('Es wurden #1# Bestellungen auf "#2#" gesetzt.', 'wpsg'), $nSet, $this->shop->arStatus[100]));
				$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Order');
				
			}
			
			if (!file_exists($_FILES['wpsg_mod_bankalignment_file']['tmp_name']))
			{
				
				$this->shop->addBackendError(__('Bitte geben sie eine Datei an!', 'wpsg'));
				die('<script type="text/javascript">/* <![CDATA[ */ location.href="'.WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Order"; /* ]]> */</script>');				
				
			}
			else if (file_exists($_FILES['wpsg_mod_bankalignment_file']['tmp_name']))
			{
			
				// Alle Bestellungen die noch "Bestellung eingegangen"
				$arOrder = $this->db->fetchAssoc("
					SELECT 
						O.`id` AS o_id, O.`onr` AS onr,
						CONCAT(K.`vname`, ' ', K.`name`, ', ', REPLACE(FORMAT(O.`price_gesamt`, 2), '.', ','), ' ".$this->shop->get_option('wpsg_currency')."') AS okey
					FROM 
						`".WPSG_TBL_ORDER."` AS O
							LEFT JOIN `".WPSG_TBL_KU."` AS K ON (K.`id` = O.`k_id`) 
					WHERE 
						O.`status` = '0'		
				");

				if (sizeof($arOrder) <= 0) 
				{
					
					$this->shop->addBackendError(__('Keine Bestellungen vorhanden, die noch nicht bezahlt sind!', 'wpsg'));
					die('<script type="text/javascript">/* <![CDATA[ */ location.href="'.WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Order"; /* ]]> */</script>');
										
				}
				
				foreach ($arOrder as $k => $v)
				{
					
					if ($this->shop->hasMod('wpsg_mod_prepayment'))
					{
							
						$arOrder[$k]['wpsg_mod_prepayment'] = $this->shop->callMod('wpsg_mod_prepayment', 'get_subject', array($v['o_id']));								

					} 
					
				}
				
				$_SESSION['wpsg']['wpsg_mod_prepayment']['set'] = array();
				
				$h = fopen($_FILES['wpsg_mod_bankalignment_file']['tmp_name'], "r");
				 
				$row = 0;
				while (($data = fgetcsv($h, 1000, $this->shop->get_option('wpsg_mod_bankalignment_separator'))) !== FALSE) 
				{
									 	
					if ($row === 0)
					{
						
						$this->shop->view['colInfo'] = $data;
						
					}
					else
					{
						
						$set = array();
						$set['data'] = $data;
						$set['order_id'] = false;
						$set['onr'] = false;
						$set['okey'] = false;
						
						foreach ($arOrder as $k => $v)
						{
							
							// Betreff aus dem Vorkassemodul
							if ($this->shop->hasMod('wpsg_mod_prepayment') && $this->in_array($v['wpsg_mod_prepayment'], $data))
							{
								
								$set['order_id'] = $v['o_id']; 
								$set['onr'] = $v['onr'];
								$set['okey'] = $v['key'];
								break;
								
							}
 
							
						}
						
						$_SESSION['wpsg']['wpsg_mod_prepayment']['set'][] = $set;
												
					}
									
					$row ++;
					
				}
				 				
				fclose($h);
				
				$this->shop->view['set'] = $_SESSION['wpsg']['wpsg_mod_prepayment']['set'];
				$this->shop->view['arOrder'] = $arOrder;
				
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_bankalignment/order_ajax.phtml');
							
			} 
			 
		} // public function order_ajax()
		
		/**
		 * Hilfsfunktion die wie in_array prüft ob $betreff in $data ist
		 * Prüft aber nicht ganz so restriktiv
		 */
		private function in_array($betreff, $data)
		{
			
			foreach ((array)$data as $k => $v)
			{
				
				if (preg_match('/(.*)'.$v.'(.*)/i', $betreff)) return true; 
				
			}
			
			return false;
			
		} // private function in_array($betreff, $data)
		
	} // class wpsg_mod_bankalignment extends wpsg_mod_basic

?>