<?php
$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Auftraege ORDER BY Jahr ASC, ID ASC;";
$result = $db->query($sql);

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->SetRightMargin(10);
$pdf->SetY(50);
$pdf->SetFont('Helvetica', 'B', 14);
$pdf->SetTextColor($_SESSION['Firma']['F1_Red'], $_SESSION['Firma']['F1_Green'], $_SESSION['Firma']['F1_Blue']);

if ("alle" == $_POST['Suche'])
{
	$pdf->Write(20, "Tracking alle Aufträge - Übersicht vom " . date("d.m.Y", time()) . "\n");
	$pdf->SetTextColor(0);
	while ($nt = $result->fetch_assoc())
	{
		$pdf->SetFont('Helvetica', '', 10);
		$sqlk = "SELECT Firma, Anrede, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
		$resultk = $db->query($sqlk);
		$ntk = $resultk->fetch_assoc();
		if ("" != $ntk['Firma'])
		{
			$pdf->Write(4, $ntk['Firma'] . ",");
		}
		else
		{
			$pdf->Write(4, $ntk['Anrede']);
		}
		$pdf->Write(4, " " . $ntk['Vorname'] . " " . $ntk['Nachname'] . "\n");
		while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
		{
			$nt['ID'] = "0" . $nt['ID'];
		}
		$pdf->SetFont('Helvetica', 'B', 10);
		$pdf->SetX(25);
		$pdf->Write(4, "-> Auftrag-Nr.");
		$pdf->SetX(80);
		$pdf->Write(4, $nt['Jahr'] . "_" . $nt['ID']);
		$pdf->SetX(110);
		$pdf->Write(4, "vom " . $nt['Datum']);
		$pdf->SetX(135);
		$pdf->Cell(60, 4, number_format($nt['Auftrag_Preis'] * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->Write(4, "\n");

		$Rest = $nt['Auftrag_Preis'];

		$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\") ORDER BY ID ASC;";
		$resultr = $db->query($sqlr);
		while ($ntr = $resultr->fetch_assoc())
		{
			$pdf->SetFont('Helvetica', '', 10);
			$pdf->SetX(30);
			if ($ntr['Vorkasse'])
			{
				$pdf->Write(4, "-> Vorkasse");
			}
			elseif ($ntr['Schluss_Rechnung'])
			{
				$pdf->Write(4, "-> Schluss");
			}
			else
			{
				$pdf->Write(4, "-> Abschlag");
			}
			while (strlen($ntr['ID']) < $_SESSION['Firma']['Stellen'])
			{
				$ntr['ID'] = "0" . $ntr['ID'];
			}
			$pdf->Write(4, "-Rechnung Nr.");
			$pdf->SetX(80);
			$pdf->Write(4, $ntr['Jahr'] . "_" . $ntr['ID']);
			$pdf->SetX(110);
			$pdf->Write(4, "vom " . $ntr['Datum']);
			$pdf->SetX(135);
			$pdf->Cell(60, 4, number_format($ntr['Preis'] * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
			$pdf->Write(4, "\n");
			$Rest = $Rest - $ntr['Preis'];
		}
		$pdf->SetFont('Helvetica', 'B', 10);
		$pdf->SetX(20);
		$pdf->Write(4, "Offener Restbetrag");
		$pdf->SetX(135);
		$pdf->Cell(60, 4, number_format($Rest * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->Write(4, "\n\n");
		$y = $pdf->GetY() - 2;
		$pdf->Line(20, $y, 195, $y);

	}
}
elseif ("fertig" == $_POST['Suche'])
{
	$pdf->Write(20, "Tracking alle abgeschlossenen Aufträge - Übersicht vom " . date("d.m.Y", time()) . "\n");
	$pdf->SetTextColor(0);
	while ($nt = $result->fetch_assoc())
	{
		$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\" AND Schluss_Rechnung = \"1\") ORDER BY ID ASC;";
		$resultr = $db->query($sqlr);
		if ($ntr = $resultr->fetch_assoc())
		{
			$pdf->SetFont('Helvetica', '', 10);
			$sqlk = "SELECT Firma, Anrede, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
			$resultk = $db->query($sqlk);
			$ntk = $resultk->fetch_assoc();
			if ("" != $ntk['Firma'])
			{
				$pdf->Write(4, $ntk['Firma'] . ",");
			}
			else
			{
				$pdf->Write(4, $ntk['Anrede']);
			}
			$pdf->Write(4, " " . $ntk['Vorname'] . " " . $ntk['Nachname'] . "\n");
			while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
			{
				$nt['ID'] = "0" . $nt['ID'];
			}
			$pdf->SetFont('Helvetica', 'B', 10);
			$pdf->SetX(25);
			$pdf->Write(4, "-> Auftrag-Nr.");
			$pdf->SetX(80);
			$pdf->Write(4, $nt['Jahr'] . "_" . $nt['ID']);
			$pdf->SetX(110);
			$pdf->Write(4, "vom " . $nt['Datum']);
			$pdf->SetX(135);
			$pdf->Cell(60, 4, number_format($nt['Auftrag_Preis'] * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
			$pdf->Write(4, "\n");


			$Rest = $nt['Auftrag_Preis'];

			$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\") ORDER BY ID ASC;";
			$resultr = $db->query($sqlr);
			while ($ntr = $resultr->fetch_assoc())
			{
				$pdf->SetFont('Helvetica', '', 10);
				$pdf->SetX(30);
				if ($ntr['Vorkasse'])
				{
					$pdf->Write(4, "-> Vorkasse");
				}
				elseif ($ntr['Schluss_Rechnung'])
				{
					$pdf->Write(4, "-> Schluss");
				}
				else
				{
					$pdf->Write(4, "-> Abschlag");
				}
				while (strlen($ntr['ID']) < $_SESSION['Firma']['Stellen'])
				{
					$ntr['ID'] = "0" . $ntr['ID'];
				}
				$pdf->Write(4, "-Rechnung Nr.");
				$pdf->SetX(80);
				$pdf->Write(4, $ntr['Jahr'] . "_" . $ntr['ID']);
				$pdf->SetX(110);
				$pdf->Write(4, "vom " . $ntr['Datum']);
				$pdf->SetX(135);
				$pdf->Cell(60, 4, number_format($ntr['Preis'] * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
				$pdf->Write(4, "\n");
				$Rest = $Rest - $ntr['Preis'];
			}
			$pdf->SetFont('Helvetica', 'B', 10);
			$pdf->SetX(20);
			$pdf->Write(4, "Offener Restbetrag");
			$pdf->SetX(135);
			$pdf->Cell(60, 4, number_format($Rest * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
			$pdf->Write(4, "\n\n");
			$y = $pdf->GetY() - 2;
			$pdf->Line(20, $y, 195, $y);
		}
	}
}
elseif ("offen" == $_POST['Suche'])
{
	$pdf->Write(20, "Tracking alle nicht abgeschlossenen Aufträge - Übersicht vom " . date("d.m.Y", time()) . "\n");
	$pdf->SetTextColor(0);
	while ($nt = $result->fetch_assoc())
	{
		$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\" AND Schluss_Rechnung = \"1\") ORDER BY ID ASC;";
		$resultr = $db->query($sqlr);
		if (!$ntr = $resultr->fetch_assoc())
		{
			$pdf->SetFont('Helvetica', '', 10);
			$sqlk = "SELECT Firma, Anrede, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
			$resultk = $db->query($sqlk);
			$ntk = $resultk->fetch_assoc();
			if ("" != $ntk['Firma'])
			{
				$pdf->Write(4, $ntk['Firma'] . ",");
			}
			else
			{
				$pdf->Write(4, $ntk['Anrede']);
			}
			$pdf->Write(4, " " . $ntk['Vorname'] . " " . $ntk['Nachname'] . "\n");
			while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
			{
				$nt['ID'] = "0" . $nt['ID'];
			}
			$pdf->SetFont('Helvetica', 'B', 10);
			$pdf->SetX(25);
			$pdf->Write(4, "-> Auftrag-Nr.");
			$pdf->SetX(80);
			$pdf->Write(4, $nt['Jahr'] . "_" . $nt['ID']);
			$pdf->SetX(110);
			$pdf->Write(4, "vom " . $nt['Datum']);
			$pdf->SetX(135);
			$pdf->Cell(60, 4, number_format($nt['Auftrag_Preis'] * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
			$pdf->Write(4, "\n");


			$Rest = $nt['Auftrag_Preis'];

			$sqlr = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Rechnungen WHERE (Auftrag_Jahr = \"" . $nt['Jahr'] . "\" AND Auftrag_ID = \"" . $nt['ID'] . "\") ORDER BY ID ASC;";
			$resultr = $db->query($sqlr);
			while ($ntr = $resultr->fetch_assoc())
			{
				$pdf->SetFont('Helvetica', '', 10);
				$pdf->SetX(30);
				if ($ntr['Vorkasse'])
				{
					$pdf->Write(4, "-> Vorkasse");
				}
				elseif ($ntr['Schluss_Rechnung'])
				{
					$pdf->Write(4, "-> Schluss");
				}
				else
				{
					$pdf->Write(4, "-> Abschlag");
				}
				while (strlen($ntr['ID']) < $_SESSION['Firma']['Stellen'])
				{
					$ntr['ID'] = "0" . $ntr['ID'];
				}
				$pdf->Write(4, "-Rechnung Nr.");
				$pdf->SetX(80);
				$pdf->Write(4, $ntr['Jahr'] . "_" . $ntr['ID']);
				$pdf->SetX(110);
				$pdf->Write(4, "vom " . $ntr['Datum']);
				$pdf->SetX(135);
				$pdf->Cell(60, 4, number_format($ntr['Preis'] * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
				$pdf->Write(4, "\n");
				$Rest = $Rest - $ntr['Preis'];
			}
			$pdf->SetFont('Helvetica', 'B', 10);
			$pdf->SetX(20);
			$pdf->Write(4, "Offener Restbetrag");
			$pdf->SetX(135);
			$pdf->Cell(60, 4, number_format($Rest * 1.19, '2', ',', '.') . " €", 0, 0, 'R');
			$pdf->Write(4, "\n\n");
			$y = $pdf->GetY() - 2;
			$pdf->Line(20, $y, 195, $y);
		}
	}
}

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

$file = __DIR__ . "/" . PREFIX . "/tracking/T_" . date('Ymd') . ".pdf";
$pdf->Output($file, "F");