<?php
if ((!isset($_POST['ok'])) AND ("j" != $_POST['Suche']))
{
	if ("m" == $_POST['Suche'])
	{
		echo "<fieldset>\n";
		echo "	<legend>erstellte Eingangsrechnungen / Gutschriften " . $_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=\"ein_ist\" />\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>erstellte Eingangsrechnungen / Gutschriften " . $_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=\"ein_ist\" />\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 Eingangsrechnungen / Gutschriften " . $_POST['Monat'] . "/" . $_POST['Jahr'] . "</legend>\n";
		$sql = "SELECT *, DATE_FORMAT(Re_Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Eingang WHERE (YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\" AND MONTH(Re_Datum) = \"" . $_POST['Monat'] . "\") ORDER BY Re_Datum ASC;";
		$sqlust = "SELECT SUM(Preis) AS Preis, MwSt FROM " . PREFIX . "_Eingang WHERE YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\" AND MONTH(Re_Datum) = \"" . $_POST['Monat'] . "\" GROUP BY MwSt DESC;";
		$sqlsum = "SELECT SUM(Preis) AS Preis, SUM(Preis * MwSt / 100) AS MwSt, SUM(Preis * (1 + MwSt / 100)) AS Gesamt FROM " . PREFIX . "_Eingang WHERE (YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\" AND MONTH(Re_Datum) = \"" . $_POST['Monat'] . "\") ORDER BY Re_Datum ASC;";
	}
	elseif ("q" == $_POST['Suche'])
	{
		echo "	<legend>erstellte Eingangsrechnungen / Gutschriften Q" . $_POST['Quartal'] . "/" . $_POST['Jahr'] . "</legend>\n";
		$sql = "SELECT *, DATE_FORMAT(Re_Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Eingang WHERE (YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Re_Datum) = \"" . $_POST['Quartal'] . "\") ORDER BY Re_Datum ASC;";
		$sqlust = "SELECT SUM(Preis) AS Preis, MwSt FROM " . PREFIX . "_Eingang WHERE YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Re_Datum) = \"" . $_POST['Quartal'] . "\" GROUP BY MwSt DESC;";
		$sqlsum = "SELECT SUM(Preis) AS Preis, SUM(Preis * MwSt / 100) AS MwSt, SUM(Preis * (1 + MwSt / 100)) AS Gesamt FROM " . PREFIX . "_Eingang WHERE (YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Re_Datum) = \"" . $_POST['Quartal'] . "\") ORDER BY Re_Datum ASC;";
	}
	elseif ("j" == $_POST['Suche'])
	{
		echo "	<legend>erstellte Eingangsrechnungen / Gutschriften " . $_POST['Jahr'] . "</legend>\n";
		$sql = "SELECT *, DATE_FORMAT(Re_Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Eingang WHERE YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\" ORDER BY Re_Datum ASC;";
		$sqlust = "SELECT SUM(Preis) AS Preis, MwSt FROM " . PREFIX . "_Eingang WHERE YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\" GROUP BY MwSt DESC;";
		$sqlsum = "SELECT SUM(Preis) AS Preis, SUM(Preis * MwSt / 100) AS MwSt, SUM(Preis * (1 + MwSt / 100)) AS Gesamt FROM " . PREFIX . "_Eingang WHERE (YEAR(Re_Datum) = \"" . $_POST['Jahr'] . "\") ORDER BY Re_Datum ASC;";
	}
	$result = $db->query($sql);
	$resultust = $db->query($sqlust);
	$resultsum = $db->query($sqlsum);
	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())
	{
		echo "<label class=\"data\">" . $nt['Kontakt'] . "</label>\n";
		echo "	<label class=\"short\">" . $nt['Re_Nr'] . "</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 "	<form action=\"index.php?" . $_SERVER['QUERY_STRING'] . "\" method=\"post\">\n";
		echo "		<input type=\"hidden\" name=\"Jahr\" value=\"" . $nt['Jahr'] . "\" />";
		echo "		<input type=\"hidden\" name=\"ID\" value=\"" . $nt['ID'] . "\" />";
		echo "		<input type=\"submit\" class=\"button_small_edit\" name=\"formaction\" value=\"e_edit\" />\n";
		echo "		<input type=\"submit\" class=\"button_small_del\" name=\"formaction\" value=\"e_del\" />\n";
		echo "	</form>\n";
		echo "<br class=\"fix\"/>\n";
		echo "<hr />\n";
	}
	echo "<hr />\n";
	$ntsum = $resultsum->fetch_assoc();
	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($ntsum['Preis'], '2', ',', '.') . " €</label>\n";
	echo "	<label class=\"money\">" . number_format($ntsum['MwSt'], '2', ',', '.') . " €</label>\n";
	echo "	<label class=\"money\">" . number_format($ntsum['Gesamt'], '2', ',', '.') . " €</label>\n";
	echo "</b>\n";
	echo "<br />\n";
	echo "</fieldset>\n";
	echo "<fieldset>\n";
	echo "	<legend>Beträge aufgeschlüsselt nach Mehrwertsteuersatz</legend>\n";
	$Sonst_Preis = $Sonst_MwSt = 0;
	while ($ntust = $resultust->fetch_assoc())
	{
		if (19 == $ntust['MwSt'])
		{
			echo "	<label class=\"ust\">Eingangsrechnungen / Gutschriften zu 19% MwSt.</label>\n";
			echo "	<label class=\"money\">" . number_format($ntust['Preis'], '2', ',', '.') . " €</label>\n";
			echo "	<label class=\"money\"><b>" . number_format($ntust['Preis'] * 0.19, '2', ',', '.') . " €</b></label>\n";
			echo "	<label class=\"money\">" . number_format($ntust['Preis'] * 1.19, '2', ',', '.') . " €</label>\n";
			echo "	<br class=\"fix\" />\n";
		}
		elseif (7 == $ntust['MwSt'])
		{
			echo "	<label class=\"ust\">Eingangsrechnungen / Gutschriften zu 7% MwSt.</label>\n";
			echo "	<label class=\"money\">" . number_format($ntust['Preis'], '2', ',', '.') . " €</label>\n";
			echo "	<label class=\"money\"><b>" . number_format($ntust['Preis'] * 0.07, '2', ',', '.') . " €</b></label>\n";
			echo "	<label class=\"money\">" . number_format($ntust['Preis'] * 1.07, '2', ',', '.') . " €</label>\n";
			echo "	<br class=\"fix\" />\n";
		}
		elseif (0 == $ntust['MwSt'])
		{
			echo "	<label class=\"ust\">Umsatzsteuerfreie Eingangsrechnungen / Gutschriften</label>\n";
			echo "	<label class=\"money\">" . number_format($ntust['Preis'], '2', ',', '.') . " €</label>\n";
			echo "	<label class=\"money\"><b>" . number_format(0, '2', ',', '.') . " €</b></label>\n";
			echo "	<label class=\"money\">" . number_format($ntust['Preis'], '2', ',', '.') . " €</label>\n";
			echo "	<br class=\"fix\" />\n";
		}
		else
		{
			$Sonst_Preis = $Sonst_Preis + $ntust['Preis'];
			$Sonst_MwSt = $Sonst_MwSt + $ntust['Preis'] * $ntust['MwSt'] / 100;
		}
	}
	if (0 != $Sonst_Preis)
	{
		echo "	<label class=\"ust\">Eingangsrechnungen / Gutschriften 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";
}