<?php


require_once("syncinclude.php");

$return=3;
if (auth())
{
	$return=5;
	if (intval($_POST['KeyBestellung']))
	{
		//glob einstellungen
		$cur_query = eS_execute_query("select versandMwst,tax_zone_id from eazysales_einstellungen");
		$einstellungen = go_db_fetch_object($cur_query);
		
		$return = 0;		
		//hole orderposes
		$cur_query = eS_execute_query("select * from orders_items where orders_id=".intval($_POST['KeyBestellung'])." order by orders_items_id");
		while ($BestellungPos = go_db_fetch_object($cur_query))
		{
			//mappe bestellpos
			$kBestellPos = setMappingBestellPos($BestellungPos->orders_items_id);
			echo(CSVkonform($kBestellPos).';');
			echo(CSVkonform(intval($_POST['KeyBestellung'])).';');
			echo(CSVkonform(getEsArtikel($BestellungPos->items_id)).';');
			echo(CSVkonform($BestellungPos->items_name).';');
			echo(CSVkonform($BestellungPos->items_price*(100+$BestellungPos->items_tax)/100).';');
			echo(CSVkonform($BestellungPos->items_tax).';');
			echo(CSVkonform($BestellungPos->items_quantity).';');
			echo("\n");
		}
		//letzte Positionen wie Versand, Mindermengenzuschlag, Rabatt, Kupon etc.
		$cur_query = eS_execute_query("select * from orders_total where (class=\"ot_shipping\" OR class=\"ot_cod_fee\" OR class=\"ot_coupon\" OR class=\"ot_discount\" OR class=\"ot_gv\" OR class=\"ot_loworderfee\" OR class=\"ot_ps_fee\") and orders_id=".intval($_POST['KeyBestellung'])." order by sort_order");
		while ($total_pos = go_db_fetch_object($cur_query))
		{
			if ($total_pos->class=="ot_shipping" || $total_pos->value!=0)
			{
				//mappe bestellpos
				$kBestellPos = setMappingBestellPos(0);
	
				$steuersatz = 0;
				switch ($total_pos->class)
				{
					case 'ot_shipping':
						//hole versand mwst aus einstellungen 
						$steuersatz = $einstellungen->versandMwst;
						break;
					case 'ot_cod_fee':
						$tax_query = eS_execute_query("select set_it_up_value from set_it_up where set_it_up_key=\"MODULE_ORDER_TOTAL_COD_FEE_TAX_CLASS\"");
						$tax_class = go_db_fetch_object($tax_query);
						if ($tax_class->set_it_up_value>0)
							$steuersatz = get_tax($tax_class->set_it_up_value);
						break;
					case 'ot_coupon':
						//$tax_query = eS_execute_query("select set_it_up_value from set_it_up where set_it_up_key=\"MODULE_ORDER_TOTAL_COUPON_TAX_CLASS\"");
						//$tax_class = go_db_fetch_object($tax_query);
						//if ($tax_class->set_it_up_value>0)
						//	$steuersatz = get_tax($tax_class->set_it_up_value);
						$steuersatz = $einstellungen->versandMwst;
						$total_pos->value*=-1;
						break;
					case 'ot_gv':
						$tax_query = eS_execute_query("select set_it_up_value from set_it_up where set_it_up_key=\"MODULE_ORDER_TOTAL_GV_TAX_CLASS\"");
						$tax_class = go_db_fetch_object($tax_query);
						if ($tax_class->set_it_up_value>0)
							$steuersatz = get_tax($tax_class->set_it_up_value);
						break;
					case 'ot_loworderfee':
						$tax_query = eS_execute_query("select set_it_up_value from set_it_up where set_it_up_key=\"MODULE_ORDER_TOTAL_LOWORDERFEE_TAX_CLASS\"");
						$tax_class = go_db_fetch_object($tax_query);
						if ($tax_class->set_it_up_value>0)
							$steuersatz = get_tax($tax_class->set_it_up_value);
						break;				
					case 'ot_ps_fee':
						$tax_query = eS_execute_query("select set_it_up_value from set_it_up where set_it_up_key=\"MODULE_ORDER_TOTAL_PS_FEE_TAX_CLASS\"");
						$tax_class = go_db_fetch_object($tax_query);
						if ($tax_class->set_it_up_value>0)
							$steuersatz = get_tax($tax_class->set_it_up_value);
						break;
				}
				echo(CSVkonform($kBestellPos).';');
				echo(CSVkonform(intval($_POST['KeyBestellung'])).';');
				echo(CSVkonform("0").';');
				echo(CSVkonform(unhtmlentities($total_pos->title)).';');
				echo(CSVkonform(unhtmlentities($total_pos->value)).';');
				echo(CSVkonform($steuersatz).';');
				echo(CSVkonform("1").';');
				echo("\n");
			}
		}
	}
}
echo($return);
logge($return);
?>