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

$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(DATE_ADD(Datum, INTERVAL " . ZAHLBAR . " DAY), '%d.%m.%Y') AS GUELTIG FROM " . PREFIX . "_Rechnungen WHERE (Jahr= \"" . $Jahr . "\" AND ID = \"" . $ID . "\");";
$result = $db->query($sql);
$nt = $result->fetch_assoc();
$sqlp = "SELECT * FROM " . PREFIX . "_Rechnung_Pos WHERE (Rechnung_ID = \"" . $ID . "\" AND Jahr = \"" . $Jahr . "\") 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();
$sqlab = "SELECT Preis_aus_Pos, Auftrag_Preis, DATE_FORMAT(A_Datum, '%d.%m.%Y') AS A_Datum FROM " . PREFIX . "_Auftraege WHERE Jahr = \"" . $nt['Auftrag_Jahr'] . "\" AND ID = \"" . $nt['Auftrag_ID'] . "\";";
$resultab = $db->query($sqlab);
$ntab = $resultab->fetch_assoc();
if ((0 == $nt['Auftrag_Jahr']) AND (0 == $nt['Auftrag_ID']))
{
	$sqltext = "SELECT * FROM " . PREFIX . "_Texte WHERE Name = \"Rechnungen ohne Auftragsbestätigung\";";
	$table = "full";
}
else
{
	if ($nt['Vorkasse'])
	{
		$sqltext = "SELECT * FROM " . PREFIX . "_Texte WHERE Name = \"Vorkasse-Rechnungen\";";
		$table = "VR";
	}
	elseif ($nt['Schluss_Rechnung'])
	{
		$sqltext = "SELECT * FROM " . PREFIX . "_Texte WHERE Name = \"Schlußrechnungen\";";
		$table = "SR";
		$sqlr = "SELECT Jahr, ID, Vorkasse, Preis, Rabatt, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Auftrag_Jahr'] . "\" AND Auftrag_ID = \"" . $nt['Auftrag_ID'] . "\" AND Schluss_Rechnung = \"0\") ORDER BY Vorkasse DESC, ID ASC;";
		$resultr = $db->query($sqlr);
	}
	else
	{
		$sqltext = "SELECT * FROM " . PREFIX . "_Texte WHERE Name = \"Abschlags-Rechnungen\";";
		$table = "AR";
	}
}

$resulttext = $db->query($sqltext);
$nttext = $resulttext->fetch_assoc();
$sqlfirma = 'SELECT * FROM Firma WHERE ID = "' . PREFIX . '";';
$resultfirma = $db->query($sqlfirma);
$ntfirma = $resultfirma->fetch_assoc();

while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
{
	$nt['ID'] = "0" . $nt['ID'];
}
while (strlen($nt['Auftrag_ID']) < $_SESSION['Firma']['Stellen'])
{
	$nt['Auftrag_ID'] = "0" . $nt['Auftrag_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->Write(4, "Rechnung-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['Auftrag_Jahr']) AND (0 != $nt['Auftrag_ID']))
{
	$pdf->Write(4, "Auftrag-Nr:");
	$pdf->SetX(45);
	$pdf->Write(4, $nt['Auftrag_Jahr'] . "_" . $nt['Auftrag_ID'] . "\n");
}
if ($_SESSION['Firma']['KfZ'])
{
	include __DIR__ . "/kfz_header.php";
}
else
{
	$pdf->SetY(107);
}
$pdf->SetRightMargin(10);
$pdf->SetFont('Helvetica', 'B', 14);
$pdf->SetTextColor($_SESSION['Firma']['F1_Red'], $_SESSION['Firma']['F1_Green'], $_SESSION['Firma']['F1_Blue']);
$pdf->Write(10, "Rechnung\n");
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetTextColor(0);

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

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

if (("VR" == $table) OR ("AR" == $table))
{
	$pdf->SetFont('Helvetica', 'B', 10);
	$pdf->Cell(10, 4, "Pos.", 1);
	$pdf->Cell(10, 4, "Anz.", 1);
	$pdf->Cell(110, 4, "Beschreibung", 1);
	$pdf->Cell(25, 4, "E-Preis", 1, 0, 'R');
	$pdf->Cell(25, 4, "G-Preis", 1, 1, 'R');
	$pdf->SetFont('Helvetica', '', 10);
	$pdf->Cell(10, 4, "1", 1);
	$pdf->Cell(10, 4, "1", 1);
	if ("VR" == $table)
	{
		$pdf->Cell(110, 4, "vereinbarter Vorkassebetrag", 1);
	}
	elseif ("AR" == $table)
	{
		$pdf->Cell(110, 4, "vereinbarte Abschlagzahlung", 1);
	}
	$pdf->Cell(25, 4, number_format($nt['Preis'], '2', ',', '.') . " €", 1, 0, 'R');
	$pdf->Cell(25, 4, number_format($nt['Preis'], '2', ',', '.') . " €", 1, 1, 'R');
	$Gesamtpreis = $nt['Preis'];
	$take_pos = 1;
}
else
{
	include __DIR__ . "/position_full.php";
}


include __DIR__ . "/berechnung.php";

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

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

if (0 != $nt['Skonto'])
{
	$var = $_SESSION['Firma']['Skonto'];
	include __DIR__ . "/replace_skonto.php";
	$pdf->SetFont('Helvetica', '', 10);
	$pdf->Write(4, $var . "\n");
}


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

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

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

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

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

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