<?php
$sql = "SELECT Vorname, Nachname FROM Admin WHERE ID = \"" . $_SESSION['TID'] . "\";";
$result = $db->query($sql);
$nt = $result->fetch_assoc();

$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Reminder WHERE (Datum <= CURDATE()) ORDER BY Datum ASC;";
$resultw = $db->query($sql);

$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Reminder WHERE (Datum > CURDATE() AND DATE_SUB(Datum, INTERVAL 15 DAY) < CURDATE()) ORDER BY Datum ASC;";
$resultwa = $db->query($sql);

$sql = "SELECT Vorname, Nachname, DATE_FORMAT(Geb_Datum, '%d.%m.') AS Geb_Datum FROM " . PREFIX . "_Kontakte WHERE (Geb_Datum != \"00-00-0000\" AND (RIGHT(Geb_Datum, 5) >= RIGHT(CURDATE(), 5)) AND (RIGHT(Geb_Datum, 5) < RIGHT(DATE_ADD(CURDATE(), INTERVAL 15 DAY), 5)) ) ORDER BY RIGHT(Geb_Datum, 5) ASC;";
$resultg = $db->query($sql);

$sql = "SELECT Count(*) AS Anzahl, SUM(Preis) AS Summe, SUM(Preis * (1 + MwSt / 100)) AS Mwst FROM " . PREFIX . "_Rechnungen WHERE Bezahlt = 0;";
$result = $db->query($sql);
$nto = $result->fetch_assoc();

$sql = "SELECT Count(*) AS Anzahl, SUM(Preis) AS Summe, SUM(Preis * (1 + MwSt / 100)) AS Mwst FROM " . PREFIX . "_Rechnungen WHERE (Bezahlt = 0 AND DATE_ADD(Datum, INTERVAL " . $_SESSION['Firma']['Z_Bis'] . " DAY) < CURDATE());";
$result = $db->query($sql);
$ntf = $result->fetch_assoc();

echo "<div id=\"left\">\n";
echo "	<fieldset>\n";
echo "	<legend>Willkommen bei <img style=\"vertical-align: middle; margin-left: 10px;\" src=\"img/startup_logo.png\" /></legend>\n";
echo "	<br class=\"fix\" />\n";
echo "	<label class=\"short\">Logo</label>\n";
if (file_exists("img/" . PREFIX . "_logo.gif"))
{
	echo "	<img style=\"height: 50px; float:right;\" src=\"img/" . PREFIX . "_logo.gif\" />\n";
}
else
{
	echo "	<label class=\"data\"><b>Sie haben noch kein Logo hochgeladen</b></label>\n";
}
echo "	<br class=\"fix\" />\n";
echo "	<label class=\"short\">Kundennummer</label>\n";
echo "	<label class=\"data\" style=\"float:right; text-align:right;\"><b>" . PREFIX . "</b></label>\n";
echo "	<br class=\"fix\" />\n";
echo "	<label class=\"short\">Firma</label>\n";
echo "	<label class=\"data\" style=\"float:right; text-align:right;\"><b>" . $_SESSION['Firma']['Firma_Name'] . "</b></label>\n";
echo "	<br class=\"fix\" />\n";
echo "	<label class=\"short\">Angemeldet als:</label>\n";
echo "	<label class=\"data\" style=\"float:right; text-align:right;\"><b>" . $nt['Vorname'] . " " . $nt['Nachname'] . "</b></label>\n";
echo "<!--[if lte IE 7]>\n";
echo "	<h2>Sie benutzen eine veraltete Browserversion</h2>\n";
echo "	<p>Wir empfehlen Ihnen aus Sicherheitsgründen auf eine neue Browserversion zu aktualisieren:<br />\n";
echo "	<a href=\"http://www.microsoft.com/downloads/details.aspx?familyid=341c2ad5-8c3d-4347-8c03-08cdecd8852b&displaylang=de\" target=\"_blank\">Download Internet Explorer 8 für WindowsXP</a><br />\n";
echo "	<a href=\"http://www.microsoft.com/downloads/details.aspx?FamilyID=79154fb4-c610-4a1e-811d-dfe0f1dd84d1&displayLang=de\" target=\"_blank\">Download Internet Explorer 8 für Vista</a><br />\n";
echo "	<a href=\"http://www.mozilla-europe.org/de/firefox/\" target=\"_blank\">Download Mozilla Firefox 3.6</a>\n";
echo "	</p>\n";
echo "<![endif]-->\n";
echo "	</fieldset>\n";
echo "</div>\n";

echo "<div id=\"right\">\n";
echo "	<fieldset>\n";
echo "		<legend>Wiederkehrende Rechnungen</legend>\n";
echo "		<fieldset>\n";
echo "			<legend class=\"sub\">Fällig:</legend>\n";
while ($ntw = $resultw->fetch_assoc())
{
	$sql = "SELECT Nachname, Firma FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $ntw['Kontakt_ID'] . "\";";
	$resultk = $db->query($sql);
	$ntk = $resultk->fetch_assoc();
	while (strlen($ntw['ID']) < $_SESSION['Firma']['Stellen'])
	{
		$ntw['ID'] = "0" . $ntw['ID'];
	}
	echo "			<label style=\"width: 100px;\" >" . $ntw['Jahr'] . "_" . $ntw['ID'] . "</label>\n";
	echo "			<label style=\"width: 300px;\" >" . $ntk['Nachname'] . ", " . $ntk['Firma'] . "</label>\n";
	echo "			<label style=\"width: 50px; text-align:right;\" ><b>" . $ntw['Datum'] . "</b></label>\n";
	echo "			<br class=\"fix\" />\n";
}
echo "		</fieldset>\n";
echo "		<fieldset>\n";
echo "			<legend class=\"sub\">Anstehend:</legend>\n";
while ($ntwa = $resultwa->fetch_assoc())
{
	$sql = "SELECT Nachname, Firma FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $ntwa['Kontakt_ID'] . "\";";
	$resultk = $db->query($sql);
	$ntk = $resultk->fetch_assoc();
	while (strlen($ntwa['ID']) < $_SESSION['Firma']['Stellen'])
	{
		$ntwa['ID'] = "0" . $ntwa['ID'];
	}
	echo "			<label style=\"width: 100px;\" >" . $ntwa['Jahr'] . "_" . $ntwa['ID'] . "</label>\n";
	echo "			<label style=\"width: 300px;\" >" . $ntk['Nachname'] . ", " . $ntk['Firma'] . "</label>\n";
	echo "			<label style=\"width: 50px; text-align:right;\" ><b>" . $ntwa['Datum'] . "</b></label>\n";
	echo "			<br class=\"fix\" />\n";
}
echo "		</fieldset>\n";
echo "	</fieldset>\n";
echo "	<fieldset>\n";
echo "		<legend>Informationen</legend>\n";
echo "		<fieldset>\n";
echo "			<legend class=\"sub\">Geburtstage</legend>\n";
while ($ntgeb = $resultg->fetch_assoc())
{
	echo "			<label style=\"width: 410px;\" >" . $ntgeb['Vorname'] . " " . $ntgeb['Nachname'] . "</label>\n";
	echo "			<label style=\"width: 50px; text-align:right;\" ><b>" . $ntgeb['Geb_Datum'] . "</b></label>\n";
	echo "			<br class=\"fix\" />\n";
}
echo "		</fieldset>\n";
echo "		<fieldset>\n";
echo "			<legend class=\"sub\">offene Posten</legend>\n";
echo "			<label style=\"width: 160px;\">" . $nto['Anzahl'] . "</label>\n";
echo "			<label style=\"width: 150px; text-align:right;\">" . number_format($nto['Summe'], '2', ',', '.') . " €</label>\n";
echo "			<label style=\"width: 150px; text-align:right;\"><b>" . number_format($nto['Mwst'], '2', ',', '.') . " €</b></label>\n";
echo "		</fieldset>\n";
echo "		<fieldset>\n";
echo "			<legend class=\"sub\">Fälligkeiten</legend>\n";
echo "			<label style=\"width: 160px;\">" . $ntf['Anzahl'] . "</label>\n";
echo "			<label style=\"width: 150px; text-align:right;\">" . number_format($ntf['Summe'], '2', ',', '.') . " €</label>\n";
echo "			<label style=\"width: 150px; text-align:right;\"><b>" . number_format($ntf['Mwst'], '2', ',', '.') . " €</b></label>\n";
echo "		</fieldset>\n";
echo "	</fieldset>\n";
echo "</div>\n";