<?php


require_once("admininclude.php");
require_once("adminTemplates.php");

$adminsession = new AdminSession();

if ($_SESSION["loggedIn"]!=1)
{
	header('Location: index.php');
	exit;
}
if ($_POST['update']==1)
updateKonfig();

zeigeKopf();
zeigeLinks($_SESSION["loggedIn"]);
zeigeKonfigForm();
zeigeFuss();

function zeigeKonfigForm()
{
	//hole einstellungen
	$cur_query = eS_execute_query("select * from eazysales_einstellungen");
	$einstellungen = go_db_fetch_object($cur_query);	
	
	$cur_query = eS_execute_query("select set_it_up_value from set_it_up where set_it_up_key=\"CURRENT_TEMPLATE\"");
	$cur_template = go_db_fetch_object($cur_query);
	
	//defaultwerte setzen
	if (!$einstellungen->shopURL)
		$einstellungen->shopURL = HTTP_SERVER;
	if (!$einstellungen->tax_priority)
		$einstellungen->tax_priority = 1;
	if (!$einstellungen->languages_id)
	{
		$cur_query = eS_execute_query("select set_it_up_value from set_it_up where set_it_up_key=\"DEFAULT_LANGUAGE\"");
		$def_lang = go_db_fetch_object($cur_query);
		if ($def_lang->set_it_up_value)
		{
			$cur_query = eS_execute_query("select languages_id from languages where code=\"".$def_lang->set_it_up_value."\"");
			$langID = go_db_fetch_object($cur_query);			
			$einstellungen->languages_id = $langID->languages_id;
		}
		else 
		{
			//erstbeste Lang
			$cur_query = eS_execute_query("select languages_id from languages");
			$langID = go_db_fetch_object($cur_query);			
			$einstellungen->languages_id = $langID->languages_id;
		}
	}
	echo('
						<td bgcolor="#ffffff" style="border-color:#222222; border-width:1px; border-style:solid; border-top-width:0px; border-bottom-width:0px; border-left-width:0px;" valign="top" align="center" height="400"><br>
							<table cellspacing="0" cellpadding="0" width="96%">
								<tr><td class="content_header" align="center"><h3>Konfiguration vom JTL-Wawi Connector</h3></td></tr>
								<tr><td class="content" align="center"><br>
										F�r den reibungslosen Im-/ und Export von Daten zwischen <a href="http://www.jtl-software.de">JTL-Wawi</a> und Ihrem Shop, m�ssen einige Einstellungen als Standard gesetzt sein.<br><br>
										<table cellspacing="0" cellpadding="0" width="580">
											<tr>
												<td class="unter_content_header">&nbsp;<b>Einstellungen</b></td>
											</tr>
											<tr>
												<td class="content" align="center">
													Hilfe zu den einzelnen Einstellungm�glichkeiten finden Sie unter <a href="http://www.jtl-software.de/jtlwawi_connector.php" target="_blank">JTL-Wawi Connector Konfigurationshilfe</a>.<br>
													<form action="konfiguration.php" method="post" name="konfig">
													<input type="hidden" name="update" value="1">
													<table cellspacing="0" cellpadding="10" width="100%">
														<tr>
															<td><b>Shop URL</b></td><td><input type="text" name="shopurl" size="50" class="konfig" value="'.$einstellungen->shopURL.'"></td>
														</tr>
														<tr>
															<td><b>Standardw�hrung</b></td><td><select name="waehrung">
	');
	$cur_query = eS_execute_query("select * from currencies");
	while ($currency = go_db_fetch_object($cur_query))
	{
		echo('<option value="'.$currency->currencies_id.'" ');if ($currency->currencies_id==$einstellungen->currencies_id) echo('selected'); echo('>'.$currency->title.'</option>');
	}
	echo('</select></td>
														</tr>
														<tr>
															<td><b>Standardsprache</b></td><td><select name="sprache">
	');
	$cur_query = eS_execute_query("select * from languages");
	while ($lang = go_db_fetch_object($cur_query))
	{
		echo('<option value="'.$lang->languages_id.'" ');if ($lang->languages_id==$einstellungen->languages_id) echo('selected'); echo('>'.$lang->name.'</option>');
	}
	echo('
															</select>														
															</td>
														</tr>
														<tr>
															<td>Umsatzsteuer</td><td>&nbsp;</td>
														</tr>
														<tr>
															<td><b>Standard Steuerzone</b></td><td><select name="steuerzone">
	');
	$cur_query = eS_execute_query("select * from geo_zones");
	while ($zone = go_db_fetch_object($cur_query))
	{
		echo('<option value="'.$zone->geo_zone_id.'" ');if ($zone->geo_zone_id==$einstellungen->tax_zone_id) echo('selected'); echo('>'.$zone->geo_zone_name.'</option>');
	}
	echo('
															</select>														
															</td>
														</tr>
														<tr>
															<td><b>Standard Steuerklasse*</b></td><td><select name="steuerklasse">
	');
	$cur_query = eS_execute_query("select * from tax_class");
	while ($klasse = go_db_fetch_object($cur_query))
	{
		echo('<option value="'.$klasse->tax_class_id.'" ');if ($klasse->tax_class_id==$einstellungen->tax_class_id) echo('selected'); echo('>'.$klasse->tax_class_title.'</option>');
	}
	echo('
															</select>														
															</td>
														</tr>
														<tr>
															<td><b>Standard Steuersatzpriorit�t</b></td><td><input type="text" name="prioritaet" size="50" class="konfig" style="width:30px;" value="'.$einstellungen->tax_priority.'"></td>
														</tr>
														<tr>
															<td><b>Steuersatz f�r Versandkosten</b></td><td><input type="text" name="versandMwst" size="50" class="konfig" style="width:30px;" value="'.$einstellungen->versandMwst.'"> %</td>
														</tr>
														<tr>
															<td>Bestellstatus�nderungen</td><td>&nbsp;</td>
														</tr>
														<tr>
															<td><b>Sobald Bestellung erfolgreich in JTL-Wawi �bernommen wird, Status setzen auf:</b></td><td><select name="StatusAbgeholt"><option value="0">Status nicht �ndern</option>
	');
	$cur_query = eS_execute_query("select * from orders_status where language_id=".$einstellungen->languages_id." order by orders_status_id");
	while ($status = go_db_fetch_object($cur_query))
	{
		echo('<option value="'.$status->orders_status_id.'" ');if ($status->orders_status_id==$einstellungen->StatusAbgeholt) echo('selected'); echo('>'.$status->orders_status_name.'</option>');
	}
	echo('
															</select>														
															</td>
														</tr>
														<tr>
															<td><b>Sobald Bestellung in JTL-Wawi versandt wird, Status setzen auf</b></td><td><select name="StatusVersendet"><option value="0">Status nicht �ndern</option>
	');
	$cur_query = eS_execute_query("select * from orders_status where language_id=".$einstellungen->languages_id." order by orders_status_id");
	while ($status = go_db_fetch_object($cur_query))
	{
		echo('<option value="'.$status->orders_status_id.'" ');if ($status->orders_status_id==$einstellungen->StatusVersendet) echo('selected'); echo('>'.$status->orders_status_name.'</option>');
	}
	echo('
															</select>														
															</td>
														</tr>
														<tr>
															<td valign="top"><b>Bestellungen <=> JTL-Wawi</b></td><td>
															Folgende Bestellungen und dazugeh�rige Kundendaten werden hiermit als "bereits zu JTL-Wawi versandt" markiert und bei einem Webshopabgleich nicht nach JTL-Wawi geholt. M�chten Sie auch alle Bestellungen und zugeh�rige Kundendaten in JTL-Wawi importieren, so kreuzen Sie nichts an:<br><br>
		');
	$cur_query = eS_execute_query("select * from orders_status where language_id=".$einstellungen->languages_id." order by orders_status_id");
	while ($status = go_db_fetch_object($cur_query))
	{
		echo('<input type="checkbox" name="bestellungen_bestellt[]" value="'.$status->orders_status_id.'">'.$status->orders_status_name);
	}
	echo('
															</td>
														</tr>	
													</table><br>
													<input type="submit" value="Einstellungen speichern">
													</form>
												</td>
											</tr>
										</table><br>
								</td></tr>
							</table><br>
						</td>
	');
}

function updateKonfig()
{
	$mappingEndkunde="";
	$mappingHaendlerkunde="";
	if (is_array($_POST['endkunde']))
		$mappingEndkunde = implode(";",$_POST['endkunde']);
	if (is_array($_POST['haendlerkunde']))
		$mappingHaendlerkunde = implode(";",$_POST['haendlerkunde']);
	
	$shopurl = $_POST['shopurl']; if (!$shopurl) $shopurl="";
	$waehrung = $_POST['waehrung']; if (!$waehrung) $waehrung=0;
	$sprache = $_POST['sprache']; if (!$sprache) $sprache=0;
	$liefertermin = $_POST['liefertermin']; if (!$liefertermin) $liefertermin=0;
	$steuerzone = $_POST['steuerzone']; if (!$steuerzone) $steuerzone=0;
	$steuerklasse = $_POST['steuerklasse']; if (!$steuerklasse) $steuerklasse=0;
	$prioritaet = $_POST['prioritaet']; if (!$prioritaet) $prioritaet=0;
	$versandMwst = floatval($_POST['versandMwst']); if (!$versandMwst) $versandMwst=0;
	$cat_listing = $_POST['cat_listing']; if (!$cat_listing) $cat_listing="";
	$cat_template = $_POST['cat_template']; if (!$cat_template) $cat_template="";
	$cat_sorting = $_POST['cat_sorting']; if (!$cat_sorting) $cat_sorting="";
	$cat_sorting2 = $_POST['cat_sorting2']; if (!$cat_sorting2) $cat_sorting2="";
	$item_template = $_POST['item_template']; if (!$item_template) $item_template="";
	$option_template = $_POST['option_template']; if (!$option_template) $option_template="";
	$statusAbgeholt = $_POST['StatusAbgeholt']; if (!$statusAbgeholt) $statusAbgeholt=0;
	$statusVersandt = $_POST['StatusVersendet']; if (!$statusVersandt) $statusVersandt=0;


	eS_execute_query("delete from eazysales_einstellungen");
	eS_execute_query("insert into eazysales_einstellungen (StatusAbgeholt, StatusVersendet, currencies_id, languages_id, mappingEndkunde, mappingHaendlerkunde, shopURL, tax_class_id, tax_zone_id, tax_priority, shipping_status_id, versandMwst,cat_listing_template,cat_category_template,cat_sorting,cat_sorting2,prod_item_template,prod_options_template) values ($statusAbgeholt, $statusVersandt, $waehrung,$sprache,\"$mappingEndkunde\",\"$mappingHaendlerkunde\",\"$shopurl\",$steuerklasse,$steuerzone,$prioritaet,$liefertermin, ".floatval($versandMwst).",\"$cat_listing\",\"$cat_template\",\"$cat_sorting\",\"$cat_sorting2\",\"$item_template\",\"$option_template\")");
}

function getTemplateArray($cur_template, $module)
{
	$files=array();
	if ($dir= opendir(FOLDER_ABSOLUT_CATALOG.'templates/'.$cur_template->set_it_up_value.'/module/'.$module.'/'))
	{
		while  (($file = readdir($dir)) !==false) 
		{
			if (is_file( FOLDER_ABSOLUT_CATALOG.'templates/'.$cur_template->set_it_up_value.'/module/'.$module.'/'.$file) and ($file !="index.html"))
			{
				$files[]=array('id' => $file,'text' => $file);
			}
		}
		closedir($dir);
	}	
	return $files;
}
?>

