<?php
$test = explode("_", $_POST['ID']);
$ID = $test['1'];
$Jahr = $test['0'];
$pdf_type = "schrift";

$sql = "SELECT *, DATE_FORMAT(Datum, '%Y%m%d') AS File_Name, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Schrift WHERE (Jahr = \"" . $Jahr . "\" AND ID = \"" . $ID . "\");";
$result = $db->query($sql);
$nt = $result->fetch_assoc();
$sqla = "SELECT * FROM Admin WHERE ID = \"" . $nt['Admin_ID'] . "\";";
$resulta = $db->query($sqla);
$nta = $resulta->fetch_assoc();
$sqlk = "SELECT * FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
$resultk = $db->query($sqlk);
$ntk = $resultk->fetch_assoc();
$sqltext = "SELECT * FROM " . PREFIX . "_Texte WHERE section= \"schrift\";";
$rtext = $db->query($sqltext);
$nttext = $rtext->fetch_assoc();

while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
{
	$nt['ID'] = "0" . $nt['ID'];
}


class PDF extends TCPDF
{

	function Header()
	{
		include __DIR__ . "/header.php";
	}

	function Footer()
	{
		include __DIR__ . "/footer.php";
	}
}

$pdf = new PDF();
$pdf->SetDisplayMode('fullpage');
$pdf->AddPage();

if ("2" == $_SESSION['Firma']['Werbung'])
{
	include __DIR__ . "/header2.php";
}
else
{
	include __DIR__ . "/header" . $_SESSION['Firma']['ASP'] . ".php";
}

$pdf->SetY(95);
$pdf->SetX(20);
$pdf->SetRightMargin(10);
$pdf->SetFont('Helvetica', 'B', 14);
$pdf->SetTextColor($_SESSION['Firma']['F1_Red'], $_SESSION['Firma']['F1_Green'], $_SESSION['Firma']['F1_Blue']);
$pdf->Write(4, $nt['Betreff']);
$pdf->SetTextColor(0);
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetX(138);
$pdf->Cell(15, 4, "Datum:", 0);
$pdf->Cell(47, 4, $nt['Datum'], 0, 1, 'R');
$pdf->Write(4, "\n\n");

$pdf->SetFont('Helvetica', '', 10);

$var = $nt['Text'];
include __DIR__ . "/replace_token.php";
include __DIR__ . "/replace_bi.php";

$pdf->SetFont('Helvetica', '', 10);
$pdf->Write(4, $var);

$var = $_SESSION['Firma']['Gruss'];
include __DIR__ . "/replace_gruss.php";
$pdf->SetFont('Helvetica', '', 10);
$pdf->Write(4, "\n" . $var);

if(!is_dir(__DIR__ . "/" . PREFIX . "/schrift"))
{
	mkdir(__DIR__ . "/" . PREFIX . "/schrift", 0777, true);
}

$file = __DIR__ . "/" . PREFIX . "/schrift/S_" . $nt['File_Name'] . ".pdf";
$pdf->Output($file, "F");

$var = $nttext['EMail'];
include __DIR__ . "/replace_token.php";
include __DIR__ . "/replace_angebot.php";