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

$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(DATE_ADD(Datum, INTERVAL " . GUELTIG . " DAY), '%d.%m.%Y') AS GUELTIG FROM " . PREFIX . "_LVs WHERE (Jahr = \"" . $Jahr . "\" AND ID = \"" . $ID . "\");";
$result = $db->query($sql);
$nt = $result->fetch_assoc();
$sqlp = "SELECT * FROM " . PREFIX . "_LV_Pos WHERE (Jahr = \"" . $Jahr . "\" AND LV_ID = \"" . $ID . "\") ORDER BY Position ASC;";
$resultp = $db->query($sqlp);
$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= \"lv\";";
$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();
$pdf->SetLeftMargin(20);
$pdf->SetAutoPageBreak(true, 30);

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

$pdf->SetY(95);
if ($_SESSION['Firma']['KfZ'])
{
	$pdf->Write(4, "Kostenvoranschlag-Nr.:");
}
else
{
	$pdf->Write(4, "Leistungsverzeichnis-Nr.:");
}
$pdf->SetX(65);
$pdf->Write(4, $nt['Jahr'] . "_" . $nt['ID']);
$pdf->SetX(138);
$pdf->Cell(15, 4, "Datum:", 0);
$pdf->Cell(47, 4, $nt['Datum'], 0, 1, 'R');
$pdf->SetRightMargin(10);
$pdf->SetY(107);
$pdf->SetFont('Helvetica', 'B', 14);
$pdf->SetTextColor($_SESSION['Firma']['F1_Red'], $_SESSION['Firma']['F1_Green'], $_SESSION['Firma']['F1_Blue']);
if ($_SESSION['Firma']['KfZ'])
{
	$pdf->Write(10, "Kostenvoranschlag\n");
}
else
{
	$pdf->Write(10, "Leistungsverzeichnis\n");
}
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetTextColor(0);

$var = $nttext['Text1'];
include __DIR__ . "/replace_token.php";
include __DIR__ . "/replace_lv.php";
include __DIR__ . "/replace_bi.php";

$pdf->SetFont('Helvetica', '', 10);
$pdf->Write(4, $var . "\n");

if ($_SESSION['Firma']['KfZ'])
{
	include __DIR__ . "/position_full.php";
	include __DIR__ . "/berechnung.php";
}
else
{
	include __DIR__ . "/position_lv.php";
}

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

$var = $nttext['Text2'];
include __DIR__ . "/replace_token.php";
include __DIR__ . "/replace_lv.php";
include __DIR__ . "/replace_bi.php";

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

if ("" != $nt['Text'])
{
	$pdf->Write(4, "\n" . $nt['Text'] . "\n");
}

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

if(!is_dir(__DIR__ . "/" . PREFIX . "/lv"))
{
	mkdir(__DIR__ . "/" . PREFIX . "/lv", 0777, true);
}
$file = __DIR__ . "/" . PREFIX . "/lv/LV_" . $nt['Jahr'] . "_" . $nt['ID'] . ".pdf";
$pdf->Output($file, "F");

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