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

$sql = "SELECT *, Datum AS Bezug_Datum, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(DATE_ADD(Datum, INTERVAL Z_Bis DAY), '%d.%m.%Y') AS GUELTIG FROM " . PREFIX . "_Mahnungen WHERE (Jahr = \"" . $Jahr . "\" AND ID = \"" . $ID . "\");";
$result = $db->query($sql);
$nt = $result->fetch_assoc();
$sql1 = "SELECT ID, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Mahnungen WHERE (Nr = \"1\" AND Referenz_ID = \"" . $nt['Referenz_ID'] . "\");";
$result1 = $db->query($sql1);
$nt1 = $result1->fetch_assoc();
$sqlr = "SELECT *, DATE_ADD(Datum, INTERVAL " . $_SESSION['Firma']['Z_Bis'] . " DAY) AS R_Datum, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Jahr = \"" . $nt['Referenz_Jahr'] . "\" AND ID=\"" . $nt['Referenz_ID'] . "\");";
$resultr = $db->query($sqlr);
$ntr = $resultr->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 = \"mahn2\";";
$resulttext = $db->query($sqltext);
$nttext = $resulttext->fetch_assoc();

$sqlt = "SELECT SUM(Preis * (1 + MwSt / 100)) AS Preis FROM " . PREFIX . "_Teilzahlungen WHERE (Referenz_Jahr = \"" . $ntr['Jahr'] . "\" AND Referenz_ID = \"" . $ntr['ID'] . "\") ORDER BY Bezahlt_am ASC;";
$resultt = $db->query($sqlt);
$ntsum = $resultt->fetch_assoc();

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

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->Write(4, "Letzte Mahnung " . $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', '', 10);
$pdf->SetTextColor(0);

$var = $nttext['Text1'];
include __DIR__ . "/replace_token.php";
include __DIR__ . "/replace_mahn2.php";
include __DIR__ . "/replace_bi.php";
$pdf->SetFont('Helvetica', '', 10);
$pdf->Write(4, $var . "\n");

include __DIR__ . "/position_mahn2.php";

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

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