<?php
$type = "Schluss-Rechnung";

if ("settings" == $_POST['action'])
{
	$sql = 'UPDATE ' . PREFIX . '_Rechnungen SET
				Kontakt_ID			= "' . $_POST['Kontakt_ID'] . '",
				Admin_ID			= "' . $_POST['Admin_ID'] . '",
				KfZ_ID				= "' . $_POST['KfZ_ID'] . '",
				Preis				= "' . $_POST['Preis'] . '",
				MwSt				= "' . $_POST['MwSt'] . '",
				Rabatt				= "' . $_POST['Rabatt'] . '",
				Skonto				= "' . $_POST['Skonto'] . '"
			WHERE
				(Jahr 		= "' . $Jahr . '"
			AND
				ID 			= "' . $ID . '");';
	$stmt = $db->prepare($sql);
	if (!$stmt)
	{
		die ('Datensatz konnte nicht gespeichert werden: ' . $db->error);
	}
	if (!$stmt->execute())
	{
		die ('Datensatz konnte nicht gespeichert werden: ' . $stmt->error);
	}
}
$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Jahr = \"" . $Jahr . "\" AND ID = \"" . $ID . "\");";
$result = $db->query($sql);
$nt = $result->fetch_assoc();
$sqlvk = "SELECT Preis, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Auftrag_Jahr'] . "\" AND Auftrag_ID = \"" . $nt['Auftrag_ID'] . "\" AND Vorkasse = \"1\");";
$resultvk = $db->query($sqlvk);
$sqlaz = "SELECT SUM(Preis) AS Preis FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Auftrag_Jahr'] . "\" AND Auftrag_ID = \"" . $nt['Auftrag_ID'] . "\" AND Vorkasse =\"0\" AND ID < \"" . $ID . "\") GROUP BY Auftrag_ID;";
$resultaz = $db->query($sqlaz);
$ntaz = $resultaz->fetch_assoc();
$sqlkn = "SELECT * FROM " . PREFIX . "_Kontakte ORDER BY Nachname ASC, Firma ASC";
$resultkn = $db->query($sqlkn);
$sqlan = "SELECT ID, Vorname, Nachname FROM Admin WHERE Firma_ID = \"" . PREFIX . "\" ORDER BY ID ASC";
$resultan = $db->query($sqlan);
$sqlab = "SELECT Auftrag_Preis, Preis_aus_Pos, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Auftraege WHERE (Jahr = \"" . $nt['Auftrag_Jahr'] . "\" AND ID = \"" . $nt['Auftrag_ID'] . "\");";
$resultab = $db->query($sqlab);
$ntab = $resultab->fetch_assoc();
$sqlrp = "SELECT * FROM " . PREFIX . "_Rechnung_Pos WHERE (Jahr = \"" . $nt['Jahr'] . "\" AND Rechnung_ID = \"" . $nt['ID'] . "\") ORDER BY Position ASC;";
$resultrp = $db->query($sqlrp);
$sqlr = "SELECT ID, Preis, Vorkasse, Rabatt, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Auftrag_Jahr'] . "\" AND Auftrag_ID = \"" . $nt['Auftrag_ID'] . "\" AND (Jahr < \"" . $Jahr . "\" OR (Jahr = \"" . $Jahr . "\" AND ID < \"" . $nt['ID'] . "\"))) ORDER BY Jahr ASC, ID ASC;";
$resultr = $db->query($sqlr);

$Text = $nt['Text'];
$UStG = $nt['UStG_13'];

include __DIR__ . "/settings.php";

echo "<fieldset>\n";
echo "	<legend>" . $type . ": Positionen</legend>\n";
while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
{
	$nt['ID'] = "0" . $nt['ID'];
}
echo "<p style=\"float: left;\">Rechnung-Nr.: " . $nt['Jahr'] . "_" . $nt['ID'] . "<br />\n";
while (strlen($nt['Auftrag_ID']) < $_SESSION['Firma']['Stellen'])
{
	$nt['Auftrag_ID'] = "0" . $nt['Auftrag_ID'];
}
echo "	Auftrag-Nr.: " . $nt['Auftrag_Jahr'] . "_" . $nt['Auftrag_ID'] . "\n";
echo "&nbsp;</p>\n";
echo "<p class=\"right\">Datum: " . $nt['Datum'] . "</p>\n";
echo "<br class=\"fix\" />\n";
echo "<table>\n";
echo "	<colgroup>\n";
echo "		<col span=\"2\" width=\"5%\" />\n";
echo "		<col width=\"60%\" />\n";
echo "		<col span=\"2\" width=\"15%\" />\n";
echo "	</colgroup>\n";
echo "	<tr>\n";
echo "		<th>Pos.</th>\n";
echo "		<th>Anz.</th>\n";
echo "		<th>Beschreibung</th>\n";
echo "		<th>E-Preis</th>\n";
echo "		<th>G-Preis</th>\n";
echo "	</tr>\n";
while ($ntrp = $resultrp->fetch_assoc())
{
	echo "	<tr>\n";
	echo "		<td>" . $ntrp['Position'] . "</td>\n";
	echo "		<td>" . $ntrp['Anzahl'] . "</td>\n";
	echo "		<td>" . nl2br($ntrp['Bezeichnung']);
	if (("" != $_SESSION['Firma']['Feld1_Name']) AND (preg_match("/" . $_GET['section'] . ",/", $_SESSION['Firma']['Feld1'])))
	{
		echo "<p class=\"Feld1\">" . nl2br(stripslashes($ntrp['Feld1'])) . "</p>\n";
	}
	if (("" != $_SESSION['Firma']['Feld2_Name']) AND (preg_match("/" . $_GET['section'] . ",/", $_SESSION['Firma']['Feld2'])))
	{
		echo "<p class=\"Feld\">" . $_SESSION['Firma']['Feld2_Name'] . ": " . stripslashes($ntrp['Feld2']) . "</p>\n";
	}
	if (("" != $_SESSION['Firma']['Feld3_Name']) AND (preg_match("/" . $_GET['section'] . ",/", $_SESSION['Firma']['Feld3'])))
	{
		echo "<p class=\"Feld\">" . $_SESSION['Firma']['Feld3_Name'] . ": " . stripslashes($ntrp['Feld3']) . "</p>\n";
	}
	if (("" != $_SESSION['Firma']['Feld4_Name']) AND (preg_match("/" . $_GET['section'] . ",/", $_SESSION['Firma']['Feld4'])))
	{
		echo "<p class=\"Feld\">" . $_SESSION['Firma']['Feld4_Name'] . ": " . stripslashes($ntrp['Feld4']) . "</p>\n";
	}
	if (("" != $_SESSION['Firma']['Feld5_Name']) AND (preg_match("/" . $_GET['section'] . ",/", $_SESSION['Firma']['Feld5'])))
	{
		echo "<p class=\"Feld\">" . $_SESSION['Firma']['Feld5_Name'] . ": " . stripslashes($ntrp['Feld5']) . "</p>\n";
	}
	echo "</td>\n";
	if ($ntab['Preis_aus_Pos'])
	{
		echo "		<td>" . number_format($ntrp['Preis'], '2', ',', '.') . " €</td>\n";
		echo "		<td>" . number_format($ntrp['Preis'] * $ntrp['Anzahl'], '2', ',', '.') . " €</td>\n";
	}
	else
	{
		echo "		<td>-/-</td>\n";
		echo "		<td>-/-</td>\n";
	}
	echo "	</tr>\n";
}
echo "	<tr>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td colspan=\"2\"><b>Gesamtpreis zzgl. " . $nt['MwSt'] . "% gesetzlicher MwSt.</b></td>\n";
echo "		<td><b>" . number_format($ntab['Auftrag_Preis'], '2', ',', '.') . " €</b></td>\n";
echo "	</tr>\n";
$Pos = $ntab['Auftrag_Preis'];
while ($ntr = $resultr->fetch_assoc())
{
	echo "	<tr>\n";
	while (strlen($ntr['ID']) < $_SESSION['Firma']['Stellen'])
	{
		$ntr['ID'] = "0" . $ntr['ID'];
	}
	echo "		<td style=\"border: none;\">&nbsp;</td>\n";
	echo "		<td style=\"border: none;\">&nbsp;</td>\n";
	if ($ntr['Vorkasse'])
	{
		echo "		<td colspan=\"2\">- Vorkasse aus Rechnungs-Nr. " . $ntr['ID'] . " vom " . $ntr['Datum'] . "</td>\n";
	}
	else
	{
		echo "		<td colspan=\"2\">- Abschlag aus Rechnungs-Nr. " . $ntr['ID'] . " vom " . $ntr['Datum'] . "</td>\n";
	}
	echo "		<td>" . number_format($ntr['Preis'] * (1 - $nt['Rabatt'] / 100), '2', ',', '.') . " €</td>\n";
	$Pos = $Pos - $ntr['Preis'] * (1 - $nt['Rabatt'] / 100);
	echo "	</tr>\n";
}
echo "	<tr>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td colspan=\"2\"><b>Zwischensumme zzgl. " . $nt['MwSt'] . "% gesetzlicher MwSt.</b></td>\n";
echo "		<td><b>" . number_format($Pos, '2', ',', '.') . " €</b></td>\n";
echo "	</tr>\n";
$Summe = $Pos;
if (0 != $nt['Rabatt'])
{
	echo "	<tr>\n";
	echo "		<td style=\"border: none;\">&nbsp;</td>\n";
	echo "		<td style=\"border: none;\">&nbsp;</td>\n";
	echo "		<td colspan=\"2\">- " . number_format($nt['Rabatt'], '2', ',', '.') . "% Rabatt auf " . number_format($Summe, '2', ',', '.') . " €</td>\n";
	$Rabatt = $Summe * $nt['Rabatt'] / 100;
	$Summe = $Summe * (1 - $nt['Rabatt'] / 100);
	echo "		<td>" . number_format($Rabatt, '2', ',', '.') . " €</td>\n";
	echo "	</tr>\n";
}
echo "	<tr>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td colspan=\"2\">+ " . $nt['MwSt'] . "% gesetzlicher MwSt.</td>\n";
echo "		<td>" . number_format($Summe * $nt['MwSt'] / 100, '2', ',', '.') . " €</td>\n";
echo "	</tr>\n";
echo "	<tr>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td colspan=\"2\"><b>Gesamtpreis inkl. " . $nt['MwSt'] . "% gesetzlicher MwSt.</b></td>\n";
echo "		<td><b>" . number_format($Summe * (1 + $nt['MwSt'] / 100), '2', ',', '.') . " €</b></td>\n";
echo "	</tr>\n";
echo "</table>\n";
echo "<br />\n";
if (0 != $nt['Skonto'])
{
	$Skonto = date("d.m.Y", strtotime("+" . $_SESSION['Firma']['S_Bis'] . " day", strtotime($nt['Datum'])));
	echo "<p>Bei Bezahlung bis zum " . $Skonto . " erhalten Sie " . number_format($nt['Skonto'], '2', ',', '.') . "% Skonto! Dies entspricht einem Betrag von " . number_format($Summe * (1 + $nt['MwSt'] / 100) * ($nt['Skonto'] / 100), '2', ',', '.') . " €.</p>\n";
}
echo "</fieldset>\n";

$nt['Preis'] = $Pos;
$nt['UStG_13'] = $UStG;

include __DIR__ . '/sv_saved.php';