<?php
if (isset($_POST['ueber']))
{
	$Frage = " AND DATE_ADD(" . PREFIX . "_Rechnungen.Datum, INTERVAL " . ZAHLBAR . " DAY) < CURDATE()";
	$Typ = "Fälligkeiten";
}
else
{
	$Typ = "offene Posten";
}
$sql = "SELECT " . PREFIX . "_Rechnungen.Jahr, " . PREFIX . "_Rechnungen.ID, " . PREFIX . "_Rechnungen.MwSt, " . PREFIX . "_Rechnungen.Auftrag_ID, " . PREFIX . "_Kontakte.Firma, " . PREFIX . "_Kontakte.Vorname, " . PREFIX . "_Kontakte.Nachname, " . PREFIX . "_Rechnungen.Preis, DATE_FORMAT(" . PREFIX . "_Rechnungen.Datum, '%d.%m.%Y') AS Datum, " . PREFIX . "_Rechnungen.Vorkasse, " . PREFIX . "_Rechnungen.Schluss_Rechnung FROM " . PREFIX . "_Rechnungen LEFT JOIN " . PREFIX . "_Kontakte ON " . PREFIX . "_Rechnungen.Kontakt_ID = " . PREFIX . "_Kontakte.ID WHERE (Bezahlt = \"0\"" . $Frage . ") ORDER BY " . PREFIX . "_Rechnungen.Datum ASC;";
$result = $db->query($sql);
$sql = "SELECT SUM(Preis * (1 + MwSt / 100)) AS Preis FROM " . PREFIX . "_Rechnungen WHERE (Bezahlt =\"0\"" . $Frage . ") GROUP BY Bezahlt;";
$res_sum = $db->query($sql);
$ntsum = $res_sum->fetch_assoc();
$GesamtTeil = 0;


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']);
$pdf->Write(20, "Übersicht " . $Typ . " vom " . date("d.m.Y", time()) . "\n");
$pdf->SetTextColor(0);

while ($nt = $result->fetch_assoc())
{
	$ID = $nt['ID'];
	while (strlen($ID) < $_SESSION['Firma']['Stellen'])
	{
		$ID = "0" . $ID;
	}

	$sql = "SELECT ID, Nr, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Mahnungen WHERE (Referenz_Jahr = \"" . $nt['Jahr'] . "\" AND Referenz_ID = \"" . $nt['ID'] . "\") ORDER BY Nr ASC;";
	$resultm = $db->query($sql);

	$sql = "SELECT *, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Datum FROM " . PREFIX . "_Teilzahlungen WHERE (Referenz_Jahr = \"" . $nt['Jahr'] . "\" AND Referenz_ID = \"" . $nt['ID'] . "\") ORDER BY Bezahlt_am ASC;";
	$resultt = $db->query($sql);

	$pdf->SetFont('Helvetica', 'B', 10);
	$pdf->Write(4, "Rechnung: ");
	$pdf->Write(4, $nt['Jahr'] . "_" . $ID);
	$pdf->Write(4, " vom " . date("d.m.Y", strtotime($nt['Datum'])));
	$pdf->SetX(130);
	$pdf->Cell(70, 4, number_format($nt['Preis'] * (1 + $nt['MwSt'] / 100), '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->Write(4, "\n");
	$pdf->SetFont('Helvetica', '', 8);

	$Summe = 0;
	while ($ntt = $resultt->fetch_assoc())
	{
		$Summe = $Summe + round($ntt['Preis'] * (1 + $ntt['MwSt'] / 100), 2);
		$pdf->SetX(30);
		$pdf->Write(4, "Teilzahlung vom " . $ntt['Datum']);
		$pdf->SetX(130);
		$pdf->Cell(70, 4, number_format($ntt['Preis'] * (1 + $ntt['MwSt'] / 100), '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->Write(4, "\n");
	}
	if ("0" != $resultt->num_rows)
	{
		$pdf->SetX(30);
		$pdf->Write(4, 'Gesamt:');
		$pdf->SetX(130);
		$pdf->Cell(70, 4, number_format($Summe, '2', ',', '.') . " €\n", 0, 0, 'R');
		$pdf->Write(4, "\n");
		$pdf->SetFont('Helvetica', 'B', 10);
		$pdf->SetX(30);
		$pdf->Write(4, "Offener Restbetrag");
		$pdf->SetX(130);
		$pdf->Cell(70, 4, number_format($nt['Preis'] * (1 + $nt['MwSt'] / 100) - $Summe, '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->Write(4, "\n");
		$pdf->SetFont('Helvetica', '', 8);
	}

	$GesamtTeil = $GesamtTeil + $Summe;
	$i = 0;
	while ($ntm = $resultm->fetch_assoc())
	{
		while (strlen($ntm['ID']) < $_SESSION['Firma']['Stellen'])
		{
			$ntm['ID'] = "0" . $ntm['ID'];
		}
		if (0 != $i)
		{
			$pdf->Write(4, ", ");
		}
		else
		{
			$pdf->SetX(30);
		}
		$pdf->Write(4, $ntm['Nr'] . ". Mahnung-Nr. " . $ntm['ID'] . " vom " . $ntm['Datum']);
		$i = 1;
	}
	if (0 != $i)
	{
		$pdf->Write(4, "\n");
	}

	$pdf->SetX(30);
	$pdf->Write(4, $nt['Nachname']);
	$pdf->Write(4, ", " . $nt['Firma'] . "\n");

}


$y = $pdf->GetY();
$pdf->Line(20, $y, 200, $y);
$pdf->SetFont('Helvetica', 'B', 14);
$pdf->Write(10, "Gesamt " . $Typ . ": ");
$pdf->SetX(130);
$pdf->Cell(70, 10, number_format($ntsum['Preis'] - $GesamtTeil, '2', ',', '.') . " €", 0, 0, 'R');

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

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