<?php
echo "<h1>Tracking / Auftragsverfolgung</h1>\n";
$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Auftraege ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);

if ("alle" == $_POST['Suche'])
{
	echo "<h2>Übersicht <i>alle Aufträge</i></h2>\n";
	while ($nt = $result->fetch_assoc())
	{
		$sqlk = "SELECT Firma, Anrede, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
		$resultk = $db->query($sqlk);
		$ntk = $resultk->fetch_assoc();
		if ("" != $ntk['Firma'])
		{
			echo "<label>" . $ntk['Firma'] . "</label>\n";
		}
		else
		{
			echo "<label>" . $ntk['Anrede'] . "</label>\n";
		}
		echo "<label class=\"data\">" . $ntk['Vorname'] . " " . $ntk['Nachname'] . "</label>\n";
		echo "<br class=\"fix\" />\n";
		while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
		{
			$nt['ID'] = "0" . $nt['ID'];
		}
		echo "<img class=\"n1\" src=\"img/small_sub.png\" />\n";
		echo "<b><label>Auftrag-Nr.</label>\n";
		echo "<label class=\"s2\">&nbsp;</label>\n";
		echo "<label class=\"short\">" . $nt['Jahr'] . "_" . $nt['ID'] . "</label>\n";
		echo "<label>vom " . $nt['Datum'] . "</label>\n";
		echo "<label class=\"money\">" . number_format($nt['Auftrag_Preis'] * 1.19, '2', ',', '.') . " €</label></b>\n";
		echo "<br class=\"fix\" />\n";
		$Rest = $nt['Auftrag_Preis'];

		$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\") ORDER BY ID ASC;";
		$resultr = $db->query($sqlr);
		while ($ntr = $resultr->fetch_assoc())
		{
			echo "<img class=\"n2\" src=\"img/small_sub.png\" />\n";
			if ($ntr['Vorkasse'])
			{
				echo "<label>Vorkasse";
			}
			elseif ($ntr['Schluss_Rechnung'])
			{
				echo "<label>Schluss";
			}
			else
			{
				echo "<label>Abschlag";
			}
			while (strlen($ntr['ID']) < $_SESSION['Firma']['Stellen'])
			{
				$ntr['ID'] = "0" . $ntr['ID'];
			}
			echo "-Rechnung Nr.</label>\n";
			echo "<label class=\"short\">" . $ntr['Jahr'] . "_" . $ntr['ID'] . "</label>\n";
			echo "<label>vom " . $ntr['Datum'] . "</label>\n";
			echo "<label class=\"money\">" . number_format($ntr['Preis'] * 1.19, '2', ',', '.') . " €</label>\n";
			echo "<br class=\"fix\" />\n";
			$Rest = $Rest - $ntr['Preis'];
		}
		echo "<b><label class=\"data\">Offener Restbetrag:</label>\n";
		echo "<label class=\"s3\">&nbsp;</label>\n";
		echo "<label class=\"money\">" . number_format($Rest * 1.19, '2', ',', '.') . " €</label></b>\n";
		echo "<br />\n";
		echo "<hr />\n";
	}
}
elseif ("fertig" == $_POST['Suche'])
{
	echo "<h2>Übersicht <i>abgeschlossene Aufträge</i></h2>\n";
	while ($nt = $result->fetch_assoc())
	{
		$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\" AND Schluss_Rechnung = \"1\") ORDER BY ID ASC;";
		$resultr = $db->query($sqlr);
		if ($ntr = $resultr->fetch_assoc())
		{
			$sqlk = "SELECT Firma, Anrede, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
			$resultk = $db->query($sqlk);
			$ntk = $resultk->fetch_assoc();
			if ("" != $ntk['Firma'])
			{
				echo "<label>" . $ntk['Firma'] . "</label>\n";
			}
			else
			{
				echo "<label>" . $ntk['Anrede'] . "</label>\n";
			}
			echo "<label class=\"data\">" . $ntk['Vorname'] . " " . $ntk['Nachname'] . "</label>\n";
			echo "<br class=\"fix\" />\n";
			while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
			{
				$nt['ID'] = "0" . $nt['ID'];
			}
			echo "<img class=\"n1\" src=\"img/small_sub.png\" />\n";
			echo "<b><label>Auftrag-Nr.</label>\n";
			echo "<label class=\"s2\">&nbsp;</label>\n";
			echo "<label class=\"short\">" . $nt['Jahr'] . "_" . $nt['ID'] . "</label>\n";
			echo "<label>vom " . $nt['Datum'] . "</label>\n";
			echo "<label class=\"money\">" . number_format($nt['Auftrag_Preis'] * 1.19, '2', ',', '.') . " €</label></b>\n";
			echo "<br class=\"fix\" />\n";
			$Rest = $nt['Auftrag_Preis'];

			$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\") ORDER BY ID ASC;";
			$resultr = $db->query($sqlr);
			while ($ntr = $resultr->fetch_assoc())
			{
				echo "<img class=\"n2\" src=\"img/small_sub.png\" />\n";
				if ($ntr['Vorkasse'])
				{
					echo "<label>Vorkasse";
				}
				elseif ($ntr['Schluss_Rechnung'])
				{
					echo "<label>Schluss";
				}
				else
				{
					echo "<label>Abschlag";
				}
				while (strlen($ntr['ID']) < $_SESSION['Firma']['Stellen'])
				{
					$ntr['ID'] = "0" . $ntr['ID'];
				}
				echo "-Rechnung Nr.</label>\n";
				echo "<label class=\"short\">" . $ntr['Jahr'] . "_" . $ntr['ID'] . "</label>\n";
				echo "<label>vom " . $ntr['Datum'] . "</label>\n";
				echo "<label class=\"money\">" . number_format($ntr['Preis'] * 1.19, '2', ',', '.') . " €</label>\n";
				echo "<br class=\"fix\" />\n";
				$Rest = $Rest - $ntr['Preis'];
			}
			echo "<b><label class=\"data\">Offener Restbetrag:</label>\n";
			echo "<label class=\"s3\">&nbsp;</label>\n";
			echo "<label class=\"money\">" . number_format($Rest * 1.19, '2', ',', '.') . " €</label></b>\n";
			echo "<br />\n";
			echo "<hr />\n";
		}
	}
}
elseif ("offen" == $_POST['Suche'])
{
	echo "<h2>Übersicht <i>noch nicht abgeschossene Aufträge</i></h2>\n";
	while ($nt = $result->fetch_assoc())
	{
		$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\" AND Schluss_Rechnung = \"1\") ORDER BY ID ASC;";
		$resultr = $db->query($sqlr);
		if (!$ntr = $resultr->fetch_assoc())
		{
			$sqlk = "SELECT Firma, Anrede, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
			$resultk = $db->query($sqlk);
			$ntk = $resultk->fetch_assoc();
			if ("" != $ntk['Firma'])
			{
				echo "<label>" . $ntk['Firma'] . "</label>\n";
			}
			else
			{
				echo "<label>" . $ntk['Anrede'] . "</label>\n";
			}
			echo "<label class=\"data\">" . $ntk['Vorname'] . " " . $ntk['Nachname'] . "</label>\n";
			echo "<br class=\"fix\" />\n";
			while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
			{
				$nt['ID'] = "0" . $nt['ID'];
			}
			echo "<img class=\"n1\" src=\"img/small_sub.png\" />\n";
			echo "<b><label>Auftrag-Nr.</label>\n";
			echo "<label class=\"s2\">&nbsp;</label>\n";
			echo "<label class=\"short\">" . $nt['Jahr'] . "_" . $nt['ID'] . "</label>\n";
			echo "<label>vom " . $nt['Datum'] . "</label>\n";
			echo "<label class=\"money\">" . number_format($nt['Auftrag_Preis'] * 1.19, '2', ',', '.') . " €</label></b>\n";
			echo "<br class=\"fix\" />\n";
			$Rest = $nt['Auftrag_Preis'];

			$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\") ORDER BY ID ASC;";
			$resultr = $db->query($sqlr);
			while ($ntr = $resultr->fetch_assoc())
			{
				echo "<img class=\"n2\" src=\"img/small_sub.png\" />\n";
				if ($ntr['Vorkasse'])
				{
					echo "<label>Vorkasse";
				}
				elseif ($ntr['Schluss_Rechnung'])
				{
					echo "<label>Schluss";
				}
				else
				{
					echo "<label>Abschlag";
				}
				while (strlen($ntr['ID']) < $_SESSION['Firma']['Stellen'])
				{
					$ntr['ID'] = "0" . $ntr['ID'];
				}
				echo "-Rechnung Nr.</label>\n";
				echo "<label class=\"short\">" . $ntr['Jahr'] . "_" . $ntr['ID'] . "</label>\n";
				echo "<label>vom " . $ntr['Datum'] . "</label>\n";
				echo "<label class=\"money\">" . number_format($ntr['Preis'] * 1.19, '2', ',', '.') . " €</label>\n";
				echo "<br class=\"fix\" />\n";
				$Rest = $Rest - $ntr['Preis'];
			}
			echo "<b><label class=\"data\">Offener Restbetrag:</label>\n";
			echo "<label class=\"s3\">&nbsp;</label>\n";
			echo "<label class=\"money\">" . number_format($Rest * 1.19, '2', ',', '.') . " €</label></b>\n";
			echo "<br />\n";
			echo "<hr />\n";
		}
	}
}