<?php



require_once("syncinclude.php");
// Hier wird der Test geschrieben

/////////////////////
//$die_datei = preg_replace('/\/testshop2\/configmain\/load_this\/mods\/jtlwawi_connector\/dbeS\//', '', $_SERVER[PHP_SELF]);
//eS_execute_query("delete from test where aktiviert='" . $die_datei . "'");
//foreach($_POST as $k => $v) {
//    $comma_separated .= "\$_POST[$k] => $k\n"; //echte Werte mit $v
//}
//eS_execute_query("insert into test (aktiviert, item_template_one) values ('" . $die_datei . "', '" . $comma_separated . "')");
////////////////////

$return=3;
if (auth())
{
	if (intval($_POST["action"]) == 1 && intval($_POST['KeyArtikel']))
	{
		$return = 0;
		//hole einstellungen
		$cur_query = eS_execute_query("select * from eazysales_einstellungen");
		$einstellungen = go_db_fetch_object($cur_query);
		
		$artikel->kArtikel = realEscape($_POST["KeyArtikel"]);
		$artikel->cArtNr = realEscape($_POST["ArtikelNo"]);
		$artikel->cName = realEscape($_POST["ArtikelName"]);
		$artikel->cBeschreibung = realEscape($_POST["ArtikelBeschreibung"]);
		$artikel->fVKBrutto = realEscape($_POST["ArtikelVKBrutto"]);
		$artikel->fVKNetto = realEscape($_POST["ArtikelVKNetto"]);
		$artikel->fMwSt = realEscape($_POST["ArtikelMwSt"]);
		$artikel->cAnmerkung = realEscape($_POST["ArtikelAnmerkung"]);
		$artikel->nLagerbestand = max(realEscape($_POST["ArtikelLagerbestand"]),0);
		$artikel->cEinheit = realEscape($_POST["ArtikelEinheit"]);
		$artikel->nMindestbestellmaenge = realEscape($_POST["ArtikelMindBestell"]);
		$artikel->cBarcode = realEscape($_POST["ArtikelBarcode"]);
		$artikel->fVKHaendlerBrutto = realEscape($_POST["ArtikelVKHaendlerBrutto"]);
		$artikel->fVKHaendlerNetto = realEscape($_POST["ArtikelVKHaendlerNetto"]);
		$artikel->cTopArtikel = realEscape($_POST["TopAngebot"]);
		$artikel->fGewicht = realEscape($_POST["Gewicht"]);
		$artikel->cNeu = realEscape($_POST["Neu"]);
		$artikel->cKurzBeschreibung = realEscape($_POST["ArtikelKurzBeschreibung"]);
		$artikel->fUVP = realEscape($_POST["ArtikelUVP"]);
		$artikel->cHersteller = realEscape(htmlentities($_POST["Hersteller"]));
		$startseite=0;
		if ($artikel->cTopArtikel=="Y")
			$startseite=1;
		
		//update oder insert?
		$items_id = getFremdArtikel($artikel->kArtikel);
		if ($items_id>0)
		{
			//update

			//attribute l�schen
			eS_execute_query("delete from items_characteristics where items_id=".$items_id);
			//KategorieArtikel l�schen
			eS_execute_query("delete from items_to_categories where items_id=".$items_id);
			
			//evtl. andere MwSt?
			$items_tax_class_id = holeSteuerId($artikel->fMwSt);
			//evtl. neuer Hersteller?
			$producers_id = holeHerstellerId($artikel->cHersteller);
			//update items
			eS_execute_query("update items set items_model=\"$artikel->cArtNr\", items_price=\"$artikel->fVKNetto\", items_tax_class_id=\"$items_tax_class_id\", items_quantity=\"$artikel->nLagerbestand\", items_weight=\"$artikel->fGewicht\", producers_id=\"$producers_id\", items_status=1, items_last_modified=now() where items_id=".$items_id);
			//update items_description
			eS_execute_query("update items_description set items_name=\"$artikel->cName\", items_description=\"$artikel->cBeschreibung\" where items_id=".$items_id." and language_id=".$einstellungen->languages_id);
			//kundengrp preise
			insertPreise($items_id);
		}
		else 
		{
			//insert
			//hole Mwst classId
			$items_tax_class_id = holeSteuerId($artikel->fMwSt);
			//setze Hersteller, falls es ihn noch nicht gibt
			$producers_id = holeHerstellerId($artikel->cHersteller);
			eS_execute_query("insert into items (items_model, items_price, items_tax_class_id, items_quantity, items_weight, producers_id, items_status, items_date_added) values (\"".$artikel->cArtNr."\",$artikel->fVKNetto,$items_tax_class_id,$artikel->nLagerbestand,$artikel->fGewicht,$producers_id,1,now())");
			//hole id
			$query = eS_execute_query("select LAST_INSERT_ID()");
			$items_id_arr = go_db_fetch_row($query);
			if ($items_id_arr[0]>0)
			{
				$items_id=$items_id_arr[0];
				//m�ssen Preise in spezielle tabellen?
				insertPreise($items_id_arr[0]);
				eS_execute_query("insert into items_description (items_id, items_name, items_description, language_id) values (".$items_id_arr[0].",\"".$artikel->cName."\", \"".$artikel->cBeschreibung."\", $einstellungen->languages_id)");
				setMappingArtikel($artikel->kArtikel,$items_id_arr[0]);
				
				//erstelle leere description f�r alle anderen Sprachen
				$sonstigeSprachen = getSonstigeSprachen($einstellungen->languages_id);
				if (is_array($sonstigeSprachen))
				{
					foreach ($sonstigeSprachen as $sonstigeSprache)
					{
						//eS_execute_query("insert into items_description (items_id, items_name, language_id) values (".$items_id_arr[0].",\"".$artikel->cName."\", $sonstigeSprache)");
						eS_execute_query("insert into items_description (items_id, items_name, items_description, language_id) values (".$items_id_arr[0].",\"".$artikel->cName."\", \"".$artikel->cBeschreibung."\", $sonstigeSprache)");
					}
				}
			}
			else 
			{
				//Fehler aufgetreten
				$return=1;
			}
		}
		
		if ($items_id>0)
		{
			//erstelle Dummy f�r Sonderpreise, falls es keinen Sonderpreis f�r diesen Artikel gibt
			$spe_query = eS_execute_query("select specials_id from specials where items_id=".$items_id);
			$special_obj = go_db_fetch_object($spe_query);
			if (!$special_obj->specials_id)
			{
				eS_execute_query("insert into specials (items_id, status) values ($items_id,0)");
			}
		}
 	}
	else
		$return=5;

	if (intval($_POST["action"]) == 3 && intval($_POST['KeyArtikel']))
	{
		$items_id = getFremdArtikel(intval($_POST['KeyArtikel']));
		if ($items_id>0)
			eS_execute_query("update items set items_status=0 where items_id=".$items_id);
		$return = 0;
	}
}

echo($return);
logge($return);

function insertPreise($items_id)
{
	/*
	$personalOfferTable = "personal_offers_by_attendees_status_";
	$endKunden_arr = explode(";",$GLOBALS['einstellungen']->mappingEndkunde);
	foreach ($endKunden_arr as $attendees_status_id)
	{
		$table = $personalOfferTable.$attendees_status_id;
		eS_execute_query("delete from $table where items_id=".$items_id);
		eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,1,".floatval($_POST["ArtikelVKNetto"]).")");
		if (intval($_POST["PAnz1"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["PAnz1"]).",".floatval($_POST["PPreis1"]).")");
		if (intval($_POST["PAnz2"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["PAnz2"]).",".floatval($_POST["PPreis2"]).")");
		if (intval($_POST["PAnz3"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["PAnz3"]).",".floatval($_POST["PPreis3"]).")");
		if (intval($_POST["PAnz4"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["PAnz4"]).",".floatval($_POST["PPreis4"]).")");
		if (intval($_POST["PAnz5"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["PAnz5"]).",".floatval($_POST["PPreis5"]).")");
	}
	$haendlerKunden_arr = explode(";",$GLOBALS['einstellungen']->mappingHaendlerkunde);
	foreach ($haendlerKunden_arr as $attendees_status_id)
	{
		$table = $personalOfferTable.$attendees_status_id;
		eS_execute_query("delete from $table where items_id=".$items_id);
		eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,1,".floatval($_POST["ArtikelVKHaendlerNetto"]).")");
		if (intval($_POST["HAnz1"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["HAnz1"]).",".floatval($_POST["HPreis1"]).")");
		if (intval($_POST["HAnz2"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["HAnz2"]).",".floatval($_POST["HPreis2"]).")");
		if (intval($_POST["HAnz3"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["HAnz3"]).",".floatval($_POST["HPreis3"]).")");
		if (intval($_POST["HAnz4"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["HAnz4"]).",".floatval($_POST["HPreis4"]).")");
		if (intval($_POST["HAnz5"])>0)
			eS_execute_query("insert into $table (items_id, quantity, personal_offer) values ($items_id,".intval($_POST["HAnz5"]).",".floatval($_POST["HPreis5"]).")");
	}
	*/
}

function holeHerstellerId($cHersteller)
{
	if (strlen($cHersteller)>0)
	{
		//ex. dieser Hersteller?
		$cur_query = eS_execute_query("select producers_id from producers where producers_name=\"".$cHersteller."\"");
		$manu = go_db_fetch_object($cur_query);
		if ($manu->producers_id>0)
			return $manu->producers_id;
		else 
		{
			//erstelle diesen Hersteller
			eS_execute_query("insert into producers (producers_name, date_added) values (\"".$cHersteller."\", now())");
			$query = eS_execute_query("select LAST_INSERT_ID()");
			$manu_id_arr = go_db_fetch_row($query);
			eS_execute_query("insert into producers_info (producers_id, languages_id) values (".$manu_id_arr[0].", ".$GLOBALS['einstellungen']->languages_id.")");
			return $manu_id_arr[0];
		}
	}
	return 0;
}

function holeSteuerId($MwSt)
{
	//existiert so ein Steuersatz ?
	$cur_query = eS_execute_query("select tax_class_id from tax_rates where tax_zone_id=".$GLOBALS['einstellungen']->tax_zone_id." and tax_rate=".$MwSt);
	$tax = go_db_fetch_object($cur_query);
	if ($tax->tax_class_id>0)
		return $tax->tax_class_id;
	else 
	{
		//erstelle klasse
		eS_execute_query("insert into tax_class (tax_class_title, date_added) values (\"JTL-Wawi Steuerklasse ".$MwSt."%\", now())");
		$query = eS_execute_query("select LAST_INSERT_ID()");
		$tax_class_id_arr = go_db_fetch_row($query);
		//f�ge diesen Steuersatz ein
		eS_execute_query("insert into tax_rates (tax_zone_id, tax_class_id, tax_priority, tax_rate, date_added) values (".$GLOBALS['einstellungen']->tax_zone_id.",".$tax_class_id_arr[0].", ".$GLOBALS['einstellungen']->tax_priority.", ".$MwSt.", now())");
		return $tax_class_id_arr[0];
	}
}
?>