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

$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(DATE_ADD(Datum, INTERVAL " . GUELTIG . " DAY), '%d.%m.%Y') AS GUELTIG, DATE_FORMAT(A_Datum, '%d.%m.%Y') AS A_Datum FROM " . PREFIX . "_Auftraege WHERE (Jahr = \"" . $Jahr . "\" AND ID = \"" . $ID . "\");";
$result = $db->query($sql);
$nt = $result->fetch_assoc();
$sqlp = "SELECT * FROM " . PREFIX . "_Auftrag_Pos WHERE (Jahr = \"" . $Jahr . "\" AND Auftrag_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= \"bestaet\";";
$rtext = $db->query($sqltext);
$nttext = $rtext->fetch_assoc();

while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
{
	$nt['ID'] = "0" . $nt['ID'];
}
while (strlen($nt['Angebot_ID']) < $_SESSION['Firma']['Stellen'])
{
	$nt['Angebot_ID'] = "0" . $nt['Angebot_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);
$pdf->Write(4, "Auftrag-Nr.:");
$pdf->SetX(45);
$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');
if ((0 != $nt['Angebot_Jahr']) AND (0 != $nt['Angebot_ID']))
{
	if ("1" == $nt['KV'])
	{
		$pdf->Write(4, "KV-Nr.:");
		$pdf->SetX(45);
		$pdf->Write(4, $nt['Angebot_Jahr'] . "_" . $nt['Angebot_ID']);
	}
	else
	{
		$pdf->Write(4, "Angebot-Nr.:");
		$pdf->SetX(45);
		$pdf->Write(4, $nt['Angebot_Jahr'] . "_" . $nt['Angebot_ID']);
	}
}
$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']);
$pdf->Write(10, "Auftragsbestätigung\n");
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetTextColor(0);

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

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

include __DIR__ . "/position_full.php";

include __DIR__ . "/berechnung.php";

$var = $nttext['Text2'];
include __DIR__ . "/replace_token.php";
include __DIR__ . "/replace_bestaet.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 . "/bestaet"))
{
	mkdir(__DIR__ . "/" . PREFIX . "/bestaet", 0777, true);
}

$file = __DIR__ . "/" . PREFIX . "/bestaet/B_" . $nt['Jahr'] . "_" . $nt['ID'] . ".pdf";
$pdf->Output($file, "F");

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