<?php
echo "<h1>Kundenexport</h1>\n";
$sql = "SELECT * FROM " . PREFIX . "_Kontakte ORDER BY ID ASC;";
$result = $db->query($sql);
chdir('tmp');
unlink($_SESSION['TID'] . "_" . PREFIX . "_cont.*");
$datei = fopen($_SESSION['TID'] . "_" . PREFIX . "_cont.csv", 'w');
fwrite($datei, "\"Kundennummer\";\"Firma\";\"Anrede\";\"Vorname\";\"Nachname\";\"Strasse\";\"Hausnummer\";\"PLZ\";\"Ort\";\"Land\";\"Telefon\";\"Fax\";\"Mobil\";\"Email\";\"Homepage\";\"Registergericht\";\"Steuer-Nr.\";\"Bank_Inhaber\";\"Bank_KontoNr\";\"BLZ\";\"Bank_Name\";\"Datum\"");
while ($nt = $result->fetch_assoc())
{
	fwrite($datei, "\n\"" . $nt['ID'] . "\";\"" . $nt['Firma'] . "\";\"" . $nt['Anrede'] . "\";\"" . $nt['Vorname'] . "\";\"" . $nt['Nachname'] . "\";\"" . $nt['Strasse'] . "\";\"" . $nt['Nummer'] . "\";\"" . $nt['PLZ'] . "\";\"" . $nt['Ort'] . "\";\"" . $nt['Land'] . "\";\"" . $nt['Telefon'] . "\";\"" . $nt['Fax'] . "\";\"" . $nt['Mobil'] . "\";\"" . $nt['Email'] . "\";\"" . $nt['Homepage'] . "\";\"" . $nt['Gericht'] . "\";\"" . $nt['SteuerNr'] . "\";\"" . $nt['Kto_Inhaber'] . "\";\"" . $nt['Kto_Nummer'] . "\";\"" . $nt['BLZ'] . "\";\"" . $nt['Bank'] . "\";\"" . $nt['Datum'] . "\"");
}
fclose($datei);
echo "<p>Die Kunden wurden exportiert</p>\n";

$zip = new ZipArchive();
$A_name = $_SESSION['TID'] . "_" . PREFIX . "_cont.zip";
if ($zip->open($A_name, ZipArchive::CREATE) !== true)
{
	exit("cannot open <$A_name>\n");
}
$zip->addFile($_SESSION['TID'] . "_" . PREFIX . "_cont.csv");
$zip->close();
echo "<p>Das Zip-Archiv wurde erstellt</p>\n";
echo "<a href=\"tmp/" . $_SESSION['TID'] . "_" . PREFIX . "_cont.zip\" type=\"application/zip\">Download</a>\n";