<?php
if ("1" == $_POST['art_take'])
{
	include __DIR__ . "/art_save.php";
}
if ("1" == $_POST['pos_save'])
{
	include __DIR__ . "/pos_save.php";
}
if ("1" == $_POST['art_pos_save'])
{
	include __DIR__ . "/pos_save.php";
	include __DIR__ . "/art_pos_save.php";
}
if ("1" == $_POST['pos_change'])
{
	include __DIR__ . "/pos_change.php";
}
if ("1" == $_POST['art_pos_change'])
{
	include __DIR__ . "/pos_change.php";
	include __DIR__ . "/art_pos_save.php";
}
if ("1" == $_POST['pos_delete'])
{
	include __DIR__ . "/pos_delete.php";
}

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);
	}
}

$Max_Pos = 0;
$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();
$sqlp = "SELECT * FROM " . PREFIX . "_Rechnung_Pos WHERE (Jahr = \"" . $Jahr . "\" AND Rechnung_ID =\"" . $ID . "\") ORDER BY Position ASC;";
$resultp = $db->query($sqlp);
$sqls = "SELECT SUM(Preis * Anzahl) AS Summe FROM " . PREFIX . "_Rechnung_Pos WHERE (Jahr = \"" . $Jahr . "\" AND Rechnung_ID = \"" . $ID . "\") GROUP BY Rechnung_ID;";
$results = $db->query($sqls);
$nts = $results->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);

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

include __DIR__ . "/settings.php";

include __DIR__ . "/positions.php";

echo "	<tr>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td style=\"border: none;\">&nbsp;</td>\n";
echo "		<td colspan=\"2\">Gesamtpreis zzgl. " . $nt['MwSt'] . "% gesetzlicher MwSt.</td>\n";
echo "		<td>" . number_format(($nts['Summe']), '2', ',', '.') . " &euro;</td>\n";
echo "	</tr>\n";
$Summe = $nts['Summe'];
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($nts['Summe'], '2', ',', '.') . " €</td>\n";
	$Rabatt = $nts['Summe'] * $nt['Rabatt'] / 100;
	$Summe = $nts['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'] . "% gesetzliche MwSt.</td>\n";
echo "		<td>" . number_format(($Summe * $nt['MwSt'] / 100), '2', ',', '.') . " &euro;</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', ',', '.') . " &euro;</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";

include __DIR__ . "/art_take.php";

$nt['Preis'] = $nts['Summe'];
$nt['UStG_13'] = $UStG;

include __DIR__ . "/sv_save.php";