<?php
if ((!isset($_POST['ok'])) AND ("j" != $_POST['Suche']))
{
	if ("m" == $_POST['Suche'])
	{
		echo "<fieldset>\n";
		echo "	<legend>Ausgangsrechnungen " . $_POST['Jahr'] . "</legend>\n";
		echo "	<form action=\"index.php?" . $_SERVER['QUERY_STRING'] . "\" method=\"post\">\n";
		echo "		<input type=\"hidden\" name=\"Suche\" value=\"m\" />\n";
		echo "		<input type=\"hidden\" name=\"Jahr\" value=\"" . $_POST['Jahr'] . "\" />\n";
		echo "		<input type=\"hidden\" name=\"formaction\" value=\"" . $_POST['formaction'] . "\" />\n";
		echo "		<label>Monat für Übersicht</label>\n";
		echo "		<select name=\"Monat\">\n";
		echo "			<option value=\"1\">Januar</option>\n";
		echo "			<option value=\"2\">Februar</option>\n";
		echo "			<option value=\"3\">März</option>\n";
		echo "			<option value=\"4\">April</option>\n";
		echo "			<option value=\"5\">Mai</option>\n";
		echo "			<option value=\"6\">Juni</option>\n";
		echo "			<option value=\"7\">Juli</option>\n";
		echo "			<option value=\"8\">August</option>\n";
		echo "			<option value=\"9\">September</option>\n";
		echo "			<option value=\"10\">Oktober</option>\n";
		echo "			<option value=\"11\">November</option>\n";
		echo "			<option value=\"12\">Dezember</option>\n";
		echo "		</select>\n";
		echo "		<br />\n";
		echo "		<input type=\"submit\" class=\"button_ok\" name=\"ok\" value=\"1\" title=\"Übernehmen\" />\n";
		echo "	</form>\n";
		echo "</fieldset>\n";
	}
	elseif ("q" == $_POST['Suche'])
	{
		echo "<fieldset>\n";
		echo "	<legend>Ausgangsrechnungen " . $_POST['Jahr'] . "</legend>\n";
		echo "	<form action=\"index.php?" . $_SERVER['QUERY_STRING'] . "\" method=\"post\">\n";
		echo "		<input type=\"hidden\" name=\"Suche\" value=\"q\" />\n";
		echo "		<input type=\"hidden\" name=\"Jahr\" value=\"" . $_POST['Jahr'] . "\" />\n";
		echo "		<input type=\"hidden\" name=\"formaction\" value=\"" . $_POST['formaction'] . "\" />\n";
		echo "		<label>Quartal für Übersicht</label>\n";
		echo "		<select name=\"Quartal\">\n";
		for ($count = 1; $count <= 4; $count++)
		{
			echo "			<option value=\"" . $count . "\">" . $count . ". Quartal - " . $_POST['Jahr'] . "</option>\n";
		}
		echo "		</select>\n";
		echo "		<br />\n";
		echo "		<input type=\"submit\" class=\"button_ok\" name=\"ok\" value=\"1\" title=\"Übernehmen\" />\n";
		echo "	</form>\n";
		echo "</fieldset>\n";
	}
}
else
{
	echo "<fieldset>\n";
	if ("m" == $_POST['Suche'])
	{
		echo "	<legend>erstellte Ausgangsrechnungen " . $_POST['Monat'] . "/" . $_POST['Jahr'] . "</legend>\n";
		$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Rechnungen WHERE (YEAR(Datum) = \"" . $_POST['Jahr'] . "\" AND MONTH(Datum) = \"" . $_POST['Monat'] . "\") ORDER BY ID ASC;";
	}
	elseif ("q" == $_POST['Suche'])
	{
		echo "	<legend>erstellte Ausgangsrechnungen Q" . $_POST['Quartal'] . "/" . $_POST['Jahr'] . "</legend>\n";
		$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Rechnungen WHERE (YEAR(Datum) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Datum) = \"" . $_POST['Quartal'] . "\") ORDER BY ID ASC;";
	}
	elseif ("j" == $_POST['Suche'])
	{
		echo "	<legend>erstellte Ausgangsrechnungen " . $_POST['Jahr'] . "</legend>\n";
		$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Rechnungen WHERE (YEAR(Datum) = \"" . $_POST['Jahr'] . "\") ORDER BY ID ASC;";
	}
	$result = $db->query($sql);
	$_19 = $_19_MwSt = 0;
	$_7 = $_7_MwSt = 0;
	$_0 = 0;
	$Sonst_Preis = $Sonst_MwSt = 0;
	echo "<b>\n";
	echo "	<label class=\"data\">Firma / Name</label>\n";
	echo "	<label class=\"short\">Re-Nr.</label>\n";
	echo "	<label class=\"short\">Re-Datum</label>\n";
	echo "	<label class=\"money\">Netto</label>\n";
	echo "	<label class=\"money\">MwSt.</label>\n";
	echo "	<label class=\"money\">Gesamt</label>\n";
	echo "	<label class=\"money\">&nbsp;</label>\n";
	echo "</b>\n";
	echo "<br class=\"fix\" />\n";
	while ($nt = $result->fetch_assoc())
	{
		if (19 == $nt['MwSt'])
		{
			$_19 = $_19 + $nt['Preis'];
			$_19_MwSt = $_19_MwSt + round($nt['Preis'] * $nt['MwSt'] / 100, 2);
		}
		elseif (7 == $nt['MwSt'])
		{
			$_7 = $_7 + $nt['Preis'];
			$_7_MwSt = $_7_MwSt + round($nt['Preis'] * $nt['MwSt'] / 100, 2);
		}
		elseif (0 == $nt['MwSt'])
		{
			$_0 = $_0 + $nt['Preis'];
		}
		else
		{
			$Sonst_Preis = $Sonst_Preis + $nt['Preis'];
			$Sonst_MwSt = $Sonst_MwSt + round($nt['Preis'] * $nt['MwSt'] / 100, 2);
		}
		$sqlk = "SELECT Firma, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
		$resultk = $db->query($sqlk);
		$ntk = $resultk->fetch_assoc();
		if ("" != $ntk['Firma'])
		{
			echo "<label class=\"data\">" . $ntk['Firma'] . "</label>\n";
		}
		else
		{
			echo "<label class=\"data\">" . $ntk['Vorname'] . " " . $ntk['Nachname'] . "</label>\n";
		}
		while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
		{
			$nt['ID'] = "0" . $nt['ID'];
		}
		echo "	<label class=\"short\">" . $nt['Jahr'] . "_" . $nt['ID'] . "</label>\n";
		echo "	<label class=\"short\">" . $nt['Datum'] . "</label>\n";
		echo "	<label class=\"money\">" . number_format($nt['Preis'], '2', ',', '.') . " €</label>\n";
		echo "	<label class=\"money\">" . number_format($nt['Preis'] * $nt['MwSt'] / 100, '2', ',', '.') . " €</label>\n";
		echo "	<label class=\"money\">" . number_format($nt['Preis'] * (1 + $nt['MwSt'] / 100), '2', ',', '.') . " €</label>\n";
		echo "	<label class=\"money\">&nbsp;</label>\n";
		echo "<br class=\"fix\"/>\n";
		echo "<hr />\n";
	}
	echo "<hr />\n";
	$Rech = $_19 + $_7 + $_0 + $Sonst_Preis;
	$Rech_MwSt = $_19_MwSt + $_7_MwSt + $_0_MwSt + $Sonst_MwSt;
	echo "<b>\n";
	echo "	<label class=\"data\">Gesamt</label>\n";
	echo "	<label class=\"short\">&nbsp;</label>\n";
	echo "	<label class=\"short\">&nbsp;</label>\n";
	echo "	<label class=\"money\">" . number_format($Rech, '2', ',', '.') . " €</label>\n";
	echo "	<label class=\"money\">" . number_format($Rech_MwSt, '2', ',', '.') . " €</label>\n";
	echo "	<label class=\"money\">" . number_format($Rech + $Rech_MwSt, '2', ',', '.') . " €</label>\n";
	echo "</b>\n";
	echo "<br class=\"fix\" />\n";
	echo "</fieldset>\n";


	echo "<fieldset>\n";
	echo "	<legend>Beträge aufgeschlüsselt nach Mehrwertsteuersatz</legend>\n";
	if (0 != $_19)
	{
		echo "	<label class=\"ust\">Umsätze zu 19% MwSt.</label>\n";
		echo "	<label class=\"money\">" . number_format($_19, '2', ',', '.') . " €</label>\n";
		echo "	<label class=\"money\"><b>" . number_format($_19_MwSt, '2', ',', '.') . " €</b></label>\n";
		echo "	<label class=\"money\">" . number_format($_19 + $_19_MwSt, '2', ',', '.') . " €</label>\n";
		echo "	<br class=\"fix\" />\n";
	}
	if (0 != $_7)
	{
		echo "	<label class=\"ust\">Umsätze zu 7% MwSt.</label>\n";
		echo "	<label class=\"money\">" . number_format($_7, '2', ',', '.') . " €</label>\n";
		echo "	<label class=\"money\"><b>" . number_format($_7_MwSt, '2', ',', '.') . " €</b></label>\n";
		echo "	<label class=\"money\">" . number_format($_7 + $_7_MwSt, '2', ',', '.') . " €</label>\n";
		echo "	<br class=\"fix\" />\n";
	}
	if (0 != $_0)
	{
		echo "	<label class=\"ust\">Umsatzsteuerfreie Verkäufe</label>\n";
		echo "	<label class=\"money\">" . number_format($_0, '2', ',', '.') . " €</label>\n";
		echo "	<label class=\"money\"><b>" . number_format(0, '2', ',', '.') . " €</b></label>\n";
		echo "	<label class=\"money\">" . number_format($_0, '2', ',', '.') . " €</label>\n";
		echo "	<br class=\"fix\" />\n";
	}
	if (0 != $Sonst_Preis)
	{
		echo "	<label class=\"ust\">Umsätze zu anderen Mehrwertsteuersätzen</label>\n";
		echo "	<label class=\"money\">" . number_format($Sonst_Preis, '2', ',', '.') . " €</label>\n";
		echo "	<label class=\"money\"><b>" . number_format($Sonst_MwSt, '2', ',', '.') . " €</b></label>\n";
		echo "	<label class=\"money\">" . number_format($Sonst_Preis + $Sonst_MwSt, '2', ',', '.') . " €</label>\n";
		echo "	<br class=\"fix\" />\n";
	}
	echo "</fieldset>\n";
}