<?php
echo "<h1>Übersicht</h1>\n";
if (isset($_POST['ueber']))
{
	$Frage = " AND DATE_ADD(" . PREFIX . "_Rechnungen.Datum, INTERVAL " . $_SESSION['Firma']['Z_Bis'] . " DAY) < CURDATE()";
	$Typ = "Fälligkeiten";
}
elseif (isset($_POST['mahnung']))
{
	include __DIR__ . "/mahn_over.php";
	return (0);
}
else
{
	$Typ = "offene Posten";
	$Frage = "";
}
echo "<h2>" . $Typ . "</h2>\n";

$sql = "SELECT " . PREFIX . "_Rechnungen.Jahr, " . PREFIX . "_Rechnungen.ID, " . PREFIX . "_Rechnungen.MwSt, " . PREFIX . "_Rechnungen.Auftrag_ID, " . PREFIX . "_Rechnungen.Teilzahlungen, " . PREFIX . "_Kontakte.Firma, " . PREFIX . "_Kontakte.Vorname, " . PREFIX . "_Kontakte.Nachname, " . PREFIX . "_Rechnungen.Preis, DATE_FORMAT(" . PREFIX . "_Rechnungen.Datum, '%d.%m.%Y') AS Datum, " . PREFIX . "_Rechnungen.Vorkasse, Schluss_Rechnung FROM " . PREFIX . "_Rechnungen LEFT JOIN " . PREFIX . "_Kontakte ON " . PREFIX . "_Rechnungen.Kontakt_ID = " . PREFIX . "_Kontakte.ID WHERE (Bezahlt = \"0\"" . $Frage . ") ORDER BY " . PREFIX . "_Rechnungen.Jahr ASC, " . PREFIX . "_Rechnungen.ID ASC;";
$result = $db->query($sql);
$sql = "SELECT SUM(Preis * (1 + MwSt / 100)) AS Preis FROM " . PREFIX . "_Rechnungen WHERE (Bezahlt =\"0\"" . $Frage . ") GROUP BY Bezahlt;";
$res_sum = $db->query($sql);
$ntsum = $res_sum->fetch_assoc();

$GesamtTeil = 0;

while ($nt = $result->fetch_assoc())
{
	while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
	{
		$nt['ID'] = "0" . $nt['ID'];
	}

	$sql = "SELECT Jahr, ID, Nr, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Mahnungen WHERE (Referenz_Jahr = \"" . $nt['Jahr'] . "\" AND Referenz_ID = \"" . $nt['ID'] . "\") ORDER BY Nr ASC;";
	$resultm = $db->query($sql);
	$sql = "SELECT *, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Teilzahlungen WHERE (Referenz_Jahr = \"" . $nt['Jahr'] . "\" AND Referenz_ID = \"" . $nt['ID'] . "\") ORDER BY Bezahlt_am ASC;";
	$resultt = $db->query($sql);


	echo "<fieldset>\n";
	echo "	<legend class=\"sub\">" . $nt['Nachname'] . ", " . $nt['Firma'] . "</legend>\n";
	echo "	<b><label class=\"data\">";
	echo "Re-Nr.: " . $nt['Jahr'] . "_" . $nt['ID'] . " vom " . $nt['Datum'] . "</label>\n";
	echo "	<label class=\"money\">Re-Betrag</label>\n";
	echo "	<label class=\"money\">" . number_format($nt['Preis'] * (1 + $nt['MwSt'] / 100), '2', ',', '.') . " €</label>\n";
	echo "	<label class=\"money\">Mahnungen</label></b>\n";
	while ($ntm = $resultm->fetch_assoc())
	{
		while (strlen($ntm['ID']) < $_SESSION['Firma']['Stellen'])
		{
			$ntm['ID'] = "0" . $ntm['ID'];
		}
		echo "	<label style=\"width: 200px; margin-left: 10px;\">";
		echo "#" . $ntm['Nr'] . " " . $ntm['Jahr'] . "_" . $ntm['ID'] . " vom " . $ntm['Datum'];
		echo "</label>\n";
	}

	$Summe = 0;
	while ($ntt = $resultt->fetch_assoc())
	{
		$Summe = $Summe + round($ntt['Preis'] * (1 + $ntt['MwSt'] / 100), 2);
		echo "		<br class=\"fix\" />\n";
		echo "		<label class=\"data\">&nbsp;&nbsp;&nbsp;&nbsp;Teilzahlung vom " . $ntt['Datum'] . "</label>\n";
		echo "		<label class=\"money\">" . number_format($ntt['Preis'] * (1 + $ntt['MwSt'] / 100), '2', ',', '.') . " €</label>\n";
	}
	if ("0" != $resultt->num_rows)
	{
		echo "		<b><label class=\"money\">" . number_format($Summe, '2', ',', '.') . " €</label></b>\n";
		echo "		<br class=\"fix\" />\n";
		echo "		<span style=\"color: red; font-weight: bold;\"><label class=\"data\">Offener Restbetrag</label>\n";
		echo "		<label class=\"money\"></label><label class=\"money\">" . number_format($nt['Preis'] * (1 + $nt['MwSt'] / 100) - $Summe, '2', ',', '.') . " €</label></span>\n";
		echo "		<br class=\"fix\" />\n";
	}
	echo "	<br class=\"fix\" />\n";

	$GesamtTeil = $GesamtTeil + $Summe;

	echo "	<form action=\"index.php?" . $_SERVER['QUERY_STRING'] . "\" method=\"post\" >\n";
	echo "		<input type=\"hidden\" name=\"ID\" value=\"" . $nt['ID'] . "\" />\n";
	echo "		<input type=\"hidden\" name=\"Jahr\" value=\"" . $nt['Jahr'] . "\" />\n";
	echo "		<input type=\"hidden\" name=\"MwSt\" value=\"" . $nt['MwSt'] . "\" />\n";
	echo "		<label class=\"short\">Bezahlt am</label>\n";
	echo "		<input class=\"short\" type=\"text\" name=\"Bezahlt_am\" />\n";
	echo "		<img style=\"float:left;\" src=\"img/small_info.png\" alt=\"TT.MM.JJJJ\" title=\"Datum der Form: <TT.MM.JJJJ>\" />\n";
	echo "		<label style=\"width: 130px;\">&nbsp;</label>\n";
	echo "		<label class=\"money\">Teilbetrag</label>\n";
	echo "		<input class=\"short\" onkeyup=\"if ( window.event.keyCode == '188' || window.event.keyCode == '110' ) { this.value = this.value.replace(',' , '.'); }\" style=\"margin-left: 30px;\" type=\"text\" name=\"Teilsumme\" />\n";
	echo "		<img style=\"float:left; margin-right: 130px;\" src=\"img/small_info.png\" alt=\"Teilzahlung inkl. MwSt!!! Nur ausflüllen, falls eine Teilzahlung erfolgt ist.\" title=\"Teilzahlung inkl. MwSt!!! Nur ausflüllen, falls eine Teilzahlung erfolgt ist.\" />\n";
	echo "		<input type=\"submit\" class=\"button_small_ok\" name=\"formaction\" value=\"save\" title=\"übernehmen\" />\n";
	echo "	</form>\n";
	echo "</fieldset>\n";
}

echo "<h2>Gesamt " . $Typ . ": " . number_format($ntsum['Preis'] - $GesamtTeil, '2', ',', '.') . " €</h2>\n";
echo "<br class=\"fix\" />\n";