<?php
$datei = fopen('./tmp/' . $_SESSION['TID'] . "_" . PREFIX . '_' . date('Y-m-d'), 'w+');

$content = "<?php\n";

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Angebote;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('alte Angebote konnten nicht verworfen werden! Datenbank-Fehler:'.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('alte Angebote konnten nicht verworfen werden! Fehler beim Zugriff auf die Tabelle '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Angebote (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	KfZ_ID int(11) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	LV_Jahr year(4) NOT NULL,\n";
$content .= "	LV_ID int(11) NOT NULL,\n";
$content .= "	MwSt int(11) NOT NULL,\n";
$content .= "	Text text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Tabelle Angebote konnte nicht erstellt werden. Datenbank-Fehler '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Tabelle Angebote konnte nicht erstellt werden. Fehler beim Ausführen des Befehls: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Angebote ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Angebote (Jahr, ID, Kontakt_ID, KfZ_ID, Admin_ID, LV_Jahr, LV_ID, MwSt, Text, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Angebote konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Angebote konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Angebot_Pos;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Angebotspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Angebotspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Angebot_Pos (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	Angebot_ID int(11) NOT NULL,\n";
$content .= "	Position int(11) NOT NULL,\n";
$content .= "	Anzahl float(7,2) NOT NULL,\n";
$content .= "	Bezeichnung text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Feld1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld3 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld4 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld5 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Angebotspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Angebotspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Angebot_Pos ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Angebot_Pos (ID, Jahr, Angebot_ID, Position, Anzahl, Bezeichnung, Preis, Feld1, Feld2, Feld3, Feld4, Feld5) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Angebotspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Angebotspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Artikel;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Artikel konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Artikel konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Artikel (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Bezeichnung text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	Feld1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld3 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld4 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld5 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Artikel konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Artikel konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Artikel ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Artikel (ID, Bezeichnung, Preis, Datum, Feld1, Feld2, Feld3, Feld4, Feld5) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Artikel konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Artikel konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Auftraege;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Aufträge konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Aufträge konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Auftraege (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	KfZ_ID int(11) NOT NULL,\n";
$content .= "	Angebot_Jahr year(4) NOT NULL,\n";
$content .= "	Angebot_ID int(11) NOT NULL,\n";
$content .= "	KV tinyint(1) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	Preis_aus_Pos tinyint(1) NOT NULL,\n";
$content .= "	Auftrag_Preis float(14,2) NOT NULL,\n";
$content .= "	MwSt int(11) NOT NULL,\n";
$content .= "	Vorkasse tinyint(1) NOT NULL,\n";
$content .= "	Text text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	A_Datum date NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Aufträge konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Aufträge konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Auftraege ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Auftraege (Jahr, ID, Kontakt_ID, KfZ_ID, Angebot_Jahr, Angebot_ID, KV, Admin_ID, Preis_aus_Pos, Auftrag_Preis, MwSt, Vorkasse, Text, A_Datum, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Aufträge konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Aufträge konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Auftrag_Pos;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Auftragspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Auftragspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Auftrag_Pos (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	Auftrag_ID int(11) NOT NULL,\n";
$content .= "	Position int(11) NOT NULL,\n";
$content .= "	Anzahl float(7,2) NOT NULL,\n";
$content .= "	Bezeichnung text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Feld1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld3 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld4 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld5 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Auftragspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Auftragspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Auftrag_Pos ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Auftrag_Pos (ID, Jahr, Auftrag_ID, Position, Anzahl, Bezeichnung, Preis, Feld1, Feld2, Feld3, Feld4, Feld5) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Auftragspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Auftragspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Eingang;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Eingänge konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Eingänge konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Eingang (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Re_Nr text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Re_Datum date NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	MwSt int(11) NOT NULL,\n";
$content .= "	Bezahlt_am date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Eingänge konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Eingänge konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Eingang ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Eingang (Jahr, ID, Kontakt, Re_Nr, Re_Datum, Preis, MwSt, Bezahlt_am) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Eingänge konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Eingänge konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Gutschriften;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Gutschriften konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Gutschriften konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Gutschriften (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	KfZ_ID int(11) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	Rechnung_Jahr year(4) NOT NULL,\n";
$content .= "	Rechnung_ID int(11) NOT NULL,\n";
$content .= "	Gesamt tinyint(1) NOT NULL,\n";
$content .= "	Bezahlt tinyint(1) NOT NULL,\n";
$content .= "	Bezahlt_am date NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	MwSt int(11) NOT NULL,\n";
$content .= "	Text text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Gutschriften konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Gutschriften konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Gutschriften ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Gutschriften (Jahr, ID, Kontakt_ID, KfZ_ID, Admin_ID, Rechnung_Jahr, Rechnung_ID, Gesamt, Bezahlt, Bezahlt_am, Preis, MwSt, Text, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Gutschriften konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Gutschriften konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Gutschrift_Pos;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Gutschriftpositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Gutschriftpositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Gutschrift_Pos (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	Gutschrift_ID int(11) NOT NULL,\n";
$content .= "	Position int(11) NOT NULL,\n";
$content .= "	Anzahl float(7,2) NOT NULL,\n";
$content .= "	Bezeichnung text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Feld1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld3 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld4 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld5 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Gutschriftpositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Gutschriftpositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Gutschrift_Pos ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Gutschrift_Pos (ID, Jahr, Gutschrift_ID, Position, Anzahl, Bezeichnung, Preis, Feld1, Feld2, Feld3, Feld4, Feld5) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Gutschriftpositionen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Gutschriftpositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Kontakte;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Kontakte konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Kontakte konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Kontakte (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Firma text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Anrede text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Vorname text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Nachname text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Strasse text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Nummer text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PLZ text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Ort text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Land text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Geb_Datum date NOT NULL,\n";
$content .= "	Telefon text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Fax text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Mobil text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Email text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Homepage text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Gericht text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	SteuerNr text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Kto_Inhaber text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Kto_Nummer text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	BLZ text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Bank text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	DSchutz tinyint(1) NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=1000;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Kontakte konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Kontakte konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Kontakte ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Kontakte (ID, Firma, Anrede, Vorname, Nachname, Strasse, Nummer, PLZ, Ort, Land, Geb_Datum, Telefon, Fax, Mobil, Email, Homepage, Gericht, SteuerNr, Kto_Inhaber, Kto_Nummer, BLZ, Bank, DSchutz, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Kontakte konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Kontakte konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_liefer;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Lieferscheine konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Lieferscheine konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_liefer (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	KfZ_ID int(11) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	Referenz_Jahr year(4) NOT NULL,\n";
$content .= "	Referenz_ID int(11) NOT NULL,\n";
$content .= "	Auftrag tinyint(1) NOT NULL,\n";
$content .= "	Text text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Lieferscheine konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Lieferscheine konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_liefer ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_liefer (Jahr, ID, Kontakt_ID, KfZ_ID, Admin_ID, Referenz_Jahr, Referenz_ID, Auftrag, Text, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Lieferscheine konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Lieferscheine konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_liefer_Pos;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Lieferscheinpositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Lieferscheinpositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_liefer_Pos (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	liefer_ID int(11) NOT NULL,\n";
$content .= "	Position int(11) NOT NULL,\n";
$content .= "	Anzahl float(7,2) NOT NULL,\n";
$content .= "	Bezeichnung text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Feld1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld3 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld4 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld5 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Lieferscheinpositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Lieferscheinpositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_liefer_Pos ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_liefer_Pos (ID, Jahr, liefer_ID, Position, Anzahl, Bezeichnung, Preis, Feld1, Feld2, Feld3, Feld4, Feld5) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Lieferscheinpositionen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Lieferscheinpositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_LVs;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('LVs konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('LVs konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_LVs (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	KfZ_ID int(11) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	MwSt int(11) NOT NULL,\n";
$content .= "	Text text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('LVs konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('LVs konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_LVs ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_LVs (Jahr, ID, Kontakt_ID, KfZ_ID, Admin_ID, MwSt, Text, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('LVs konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('LVs konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_LV_Pos;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('LV-Positionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('LV-Positionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_LV_Pos (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	LV_ID int(11) NOT NULL,\n";
$content .= "	Position int(11) NOT NULL,\n";
$content .= "	Anzahl float(7,2) NOT NULL,\n";
$content .= "	Bezeichnung text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Feld1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld3 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld4 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld5 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('LV-Positionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('LV-Positionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_LV_Pos ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_LV_Pos (ID, Jahr, LV_ID, Position, Anzahl, Bezeichnung, Preis, Feld1, Feld2, Feld3, Feld4, Feld5) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('LV-Positionen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('LV-Positionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Mahnungen;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Mahnungen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Mahnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Mahnungen (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Nr tinyint(4) NOT NULL,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	KfZ_ID int(11) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	Referenz_Jahr year(4) NOT NULL,\n";
$content .= "	Referenz_ID int(11) NOT NULL,\n";
$content .= "	Z_Bis int(11) NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Mahnungen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Mahnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Mahnungen ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Mahnungen (Jahr, ID, Nr, Kontakt_ID, KfZ_ID, Admin_ID, Referenz_Jahr, Referenz_ID, Z_Bis, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Mahnungen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Mahnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Rechnungen;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Rechnungen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Rechnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Rechnungen (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	KfZ_ID int(11) NOT NULL,\n";
$content .= "	Auftrag_Jahr year(4) NOT NULL,\n";
$content .= "	Auftrag_ID int(11) NOT NULL,\n";
$content .= "	Liefer_Jahr int(11) NOT NULL,\n";
$content .= "	Liefer_ID int(11) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	Vorkasse tinyint(1) NOT NULL,\n";
$content .= "	Schluss_Rechnung tinyint(1) NOT NULL,\n";
$content .= "	Alte_Rechnung tinyint(1) NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	MwSt int(11) NOT NULL,\n";
$content .= "	Rabatt float(5,2) NOT NULL,\n";
$content .= "	Skonto float(5,2) NOT NULL,\n";
$content .= "	Bezahlt tinyint(1) NOT NULL,\n";
$content .= "	Teilzahlungen tinyint(1) NOT NULL,\n";
$content .= "	Bezahlt_am date NOT NULL,\n";
$content .= "	Text text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Rechnungen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Rechnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Rechnungen ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Rechnungen (Jahr, ID, Kontakt_ID, KfZ_ID, Auftrag_Jahr, Auftrag_ID, Liefer_Jahr, Liefer_ID, Admin_ID, Vorkasse, Schluss_Rechnung, Alte_Rechnung, Preis, MwSt, Rabatt, Skonto, Bezahlt, Teilzahlungen, Bezahlt_am, Text, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Rechnungen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Rechnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Rechnung_Pos;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Rechnungspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Rechnungspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Rechnung_Pos (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	Rechnung_ID int(11) NOT NULL,\n";
$content .= "	Position int(11) NOT NULL,\n";
$content .= "	Anzahl float(7,2) NOT NULL,\n";
$content .= "	Bezeichnung text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Feld1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld3 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld4 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld5 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Rechnungspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Rechnungspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Rechnung_Pos ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Rechnung_Pos (ID, Jahr, Rechnung_ID, Position, Anzahl, Bezeichnung, Preis, Feld1, Feld2, Feld3, Feld4, Feld5) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Rechnungspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Rechnungspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Reminder;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Wiederkehrende Rechnungen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Wiederkehrende Rechnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Reminder (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	KfZ_ID int(11) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	MwSt int(11) NOT NULL,\n";
$content .= "	Rabatt float(5,2) NOT NULL,\n";
$content .= "	Skonto float(5,2) NOT NULL,\n";
$content .= "	Text text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Wiederkehrende Rechnungen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Wiederkehrende Rechnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Reminder ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Reminder (Jahr, ID, Kontakt_ID, KfZ_ID, Admin_ID, Preis, MwSt, Rabatt, Skonto, Text, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Wiederkehrende Rechnungen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Wiederkehrende Rechnungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Reminder_Pos;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Wiederkehrende Rechnungspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Wiederkehrende Rechnungspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Reminder_Pos (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	Reminder_ID int(11) NOT NULL,\n";
$content .= "	Position int(11) NOT NULL,\n";
$content .= "	Anzahl float(7,2) NOT NULL,\n";
$content .= "	Bezeichnung text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Feld1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld3 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld4 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Feld5 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Wiederkehrende Rechnungspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Wiederkehrende Rechnungspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Reminder_Pos ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Reminder_Pos (ID, Jahr, Reminder_ID, Position, Anzahl, Bezeichnung, Preis, Feld1, Feld2, Feld3, Feld4, Feld5) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Wiederkehrende Rechnungspositionen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Wiederkehrende Rechnungspositionen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Schrift;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Eigene Briefe konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Eigene Briefe konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Schrift (\n";
$content .= "	Jahr year(4) NOT NULL,\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Kontakt_ID int(11) NOT NULL,\n";
$content .= "	Admin_ID int(11) NOT NULL,\n";
$content .= "	Betreff text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Text text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Datum date NOT NULL,\n";
$content .= "	PRIMARY KEY (Jahr,ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Eigene Briefe konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Eigene Briefe konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Schrift ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Schrift (Jahr, ID, Kontakt_ID, Admin_ID, Betreff, Text, Datum) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Eigene Briefe konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Eigene Briefe konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Teilzahlungen;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Teilzahlungen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Teilzahlungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Teilzahlungen (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	Referenz_Jahr year(4) NOT NULL,\n";
$content .= "	Referenz_ID int(11) NOT NULL,\n";
$content .= "	MwSt int(11) NOT NULL,\n";
$content .= "	Preis float(14,2) NOT NULL,\n";
$content .= "	Bezahlt_am date NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Teilzahlungen konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Teilzahlungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Teilzahlungen ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Teilzahlungen (ID, Referenz_Jahr, Referenz_ID, MwSt, Preis, Bezahlt_am) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Teilzahlungen konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Teilzahlungen konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "\$sql = \"DROP TABLE IF EXISTS " . PREFIX . "_Texte;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Firmentexte konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Firmentexte konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$content .= "\$sql = \"CREATE TABLE " . PREFIX . "_Texte (\n";
$content .= "	ID int(11) NOT NULL AUTO_INCREMENT,\n";
$content .= "	section text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Name text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Text1 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Text2 text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	Betreff text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	EMail text COLLATE latin1_german1_ci NOT NULL,\n";
$content .= "	PRIMARY KEY (ID)\n";
$content .= ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;\";\n";
$content .= "\$stmt = \$db->prepare(\$sql);\n";
$content .= "if (!\$stmt) {\n";
$content .= "	die('Firmentexte konnten nicht gespeichert werden: '.\$db->error);\n";
$content .= "}\n";
$content .= "if (!\$stmt->execute()) {\n";
$content .= "	die('Firmentexte konnten nicht gespeichert werden: '.\$stmt->error);\n";
$content .= "}\n";
$sql = "SELECT * FROM " . PREFIX . "_Texte ORDER BY ID ASC;";
$result = $db->query($sql);
if (0 != $result->num_rows)
{
	$content .= "\$sql = \"INSERT INTO " . PREFIX . "_Texte (ID, section, Name, Text1, Text2, Betreff, EMail) VALUES\n";
	while ($nt = $result->fetch_assoc())
	{
		$content .= "(";
		foreach ($nt AS $wert)
		{
			$content .= "'" . addslashes(stripslashes($wert)) . "', ";
		}
		$content = substr($content, 0, -2);
		$content .= "),\n";
	}
	$content = substr($content, 0, -2);
	$content .= ";\";\n";
	$content .= "\$stmt = \$db->prepare(\$sql);\n";
	$content .= "if (!\$stmt) {\n";
	$content .= "	die('Firmentexte konnten nicht gespeichert werden: '.\$db->error);\n";
	$content .= "}\n";
	$content .= "if (!\$stmt->execute()) {\n";
	$content .= "	die('Firmentexte konnten nicht gespeichert werden: '.\$stmt->error);\n";
	$content .= "}\n";
}

$content .= "echo \"<h1>Datenrücksicherung war erfolgreich!</h1>\";\n";
$content .= "echo \"<h2>Der Datenstand entspricht nun dem bei der Erstellung der Rücksicherung vorliegenden.</h2>\";\n";
$content .= "?>\n";
$content .= "-->Ende_dieser_Datei<--";

$crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(PREFIX), $content, MCRYPT_MODE_CBC);
fwrite($datei, $crypt);