<?php
$type = "Abschlags-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 < \"" . $nt['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 Nachname ASC";
$resultan = $db->query($sqlan);
$sqlab = "SELECT Auftrag_Preis, 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();

$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 width=\"15%\" />\n";
echo "		<col width=\"60%\" />\n";
echo "		<col width=\"25%\" />\n";
echo "	</colgroup>\n";
echo "	<tr>\n";
echo "		<th>Pos.</th>\n";
echo "		<th>Beschreibung</th>\n";
echo "		<th>Preis</th>\n";
echo "	</tr>\n";
echo "	<tr>\n";
echo "		<td>1</td>\n";
echo "		<td>Vereinbarte Abschlagzahlung</td>\n";
echo "		<td>" . number_format($nt['Preis'], '2', ',', '.') . " €</td>\n";
echo "	</tr>\n";
echo "	<tr>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td>Gesamtpreis zzgl. " . $nt['MwSt'] . "% gesetzlicher MwSt.</td>\n";
echo "		<td>" . number_format($nt['Preis'], '2', ',', '.') . " €</td>\n";
echo "	</tr>\n";
$Summe = $nt['Preis'];
if (0 != $nt['Rabatt'])
{
	echo "	<tr>\n";
	echo "		<td style=\"border: none;\">&nbsp;</td>\n";
	echo "		<td>- " . number_format($nt['Rabatt'], '2', ',', '.') . "% Rabatt auf " . number_format($nt['Preis'], '2', ',', '.') . " €</td>\n";
	$Rabatt = $nt['Preis'] * $nt['Rabatt'] / 100;
	$Summe = $nt['Preis'] * (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>+ " . $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><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['UStG_13'] = $UStG;

include __DIR__ . '/sv_saved.php';