<?php
$sql = "SELECT " . PREFIX . "_Mahnungen.*, DATE_FORMAT(" . PREFIX . "_Mahnungen.Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(DATE_ADD(" . PREFIX . "_Mahnungen.Datum, INTERVAL " . PREFIX . "_Mahnungen.Z_Bis DAY), '%d.%m.%Y') AS Z_Bis, " . PREFIX . "_Kontakte.Firma, " . PREFIX . "_Kontakte.Vorname, " . PREFIX . "_Kontakte.Nachname FROM " . PREFIX . "_Mahnungen LEFT JOIN " . PREFIX . "_Kontakte ON " . PREFIX . "_Mahnungen.Kontakt_ID = " . PREFIX . "_Kontakte.ID WHERE Nr = \"" . $_POST['mahnung'] . "\" ORDER BY " . PREFIX . "_Mahnungen.Jahr ASC, " . PREFIX . "_Mahnungen.ID ASC;";
$result = $db->query($sql);
echo "<h2>" . $_POST['mahnung'] . ". Mahnungen</h2>\n";
while ($nt = $result->fetch_assoc())
{
	while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
	{
		$nt['ID'] = "0" . $nt['ID'];
	}
	$sqlr = "SELECT Jahr, ID, Preis, Schluss_Rechnung, Bezahlt, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt_am, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Jahr = \"" . $nt['Referenz_Jahr'] . "\" AND ID = \"" . $nt['Referenz_ID'] . "\");";
	$resultr = $db->query($sqlr);
	$ntr = $resultr->fetch_assoc();
	while (strlen($ntr['ID']) < $_SESSION['Firma']['Stellen'])
	{
		$ntr['ID'] = "0" . $ntr['ID'];
	}
	echo "<b><label class=\"data\">Rechnung-Nr.: " . $ntr['Jahr'] . "_" . $ntr['ID'] . "</label>\n";
	echo "<label class=\"short\">Betrag</label>\n";
	echo "<label class=\"short\">Datum</label>\n";
	echo "<label>Zahlbar bis</label>\n";
	echo "<label class=\"short\">Bezahlt am</label>\n";
	echo "</b><br class=\"fix\" />\n";
	if ("" != $nt['Firma'])
	{
		echo "<label class=\"data\">" . $nt['Firma'] . ", ";
	}
	else
	{
		echo "<label class=\"data\">" . $nt['Vorname'] . " ";
	}
	echo $nt['Nachname'] . "</label>\n";
	if ("0" == $nt['Schluss_Rechnung'])
	{
		echo "<label class=\"short\">" . number_format($ntr['Preis'], '2', ',', '.') . " &euro;</label>\n";
	}
	else
	{
		echo "<label class=\"short\">" . number_format($ntr['Preis'] * 1.19, '2', ',', '.') . " &euro;</label>\n";
	}
	echo "<label class=\"short\">" . $ntr['Datum'] . "</label>\n";
	echo "<label>" . $nt['Z_Bis'] . "</label>\n";
	if ("1" == $ntr['Bezahlt'])
	{
		echo "<label class=\"short\">" . $ntr['Bezahlt_am'] . "</label>\n";
		echo "<span class=\"data\">\n";
		echo "	<form action=\"index.php?" . $_SERVER['QUERY_STRING'] . "\" method=\"post\" >\n";
		echo "		<input type=\"hidden\" name=\"ID\" value=\"" . $nt['Jahr'] . "_" . $nt['ID'] . "\" />\n";
		echo "		<input type=\"submit\" class=\"button_small_del\" name=\"formaction\" value=\"del\" title=\"Der Betrag wurde als bezahlt gekennzeichnet. Soll die Mahnung gelöscht werden?\" />\n";
		echo "	</form>\n";
		echo "</span>\n";
	}
	echo "<br class=\"fix\" />\n";
	echo "<hr />\n";
	echo "<br>\n";
}
