<?php

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->SetLeftMargin(20);
$pdf->SetY(50);
$pdf->SetFont('Helvetica', 'B', 14);
$pdf->SetTextColor($_SESSION['Firma']['F2_Red'], $_SESSION['Firma']['F2_Green'], $_SESSION['Firma']['F2_Blue']);

if ("m" == $_POST['Suche'])
{
	$pdf->Write(20, "bezahlte Eingangsrechnungen / Gutschriften " . $_POST['Monat'] . "/" . $_POST['Jahr'] . "\n");
	$sql = "SELECT *, DATE_FORMAT(Re_Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Eingang WHERE (YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND MONTH(Bezahlt_am) = \"" . $_POST['Monat'] . "\") ORDER BY Bezahlt_am ASC;";
	$sqlust = "SELECT SUM(Preis) AS Preis, MwSt FROM " . PREFIX . "_Eingang WHERE YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND MONTH(Bezahlt_am) = \"" . $_POST['Monat'] . "\" GROUP BY MwSt DESC;";
	$sqlsum = "SELECT SUM(Preis) AS Preis, SUM(Preis * MwSt / 100) AS MwSt, SUM(Preis * (1 + MwSt / 100)) AS Gesamt FROM " . PREFIX . "_Eingang WHERE (YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND MONTH(Bezahlt_am) = \"" . $_POST['Monat'] . "\") ORDER BY Bezahlt_am ASC;";
}
elseif ("q" == $_POST['Suche'])
{
	$pdf->Write(20, "bezahlte Eingangsrechnungen / Gutschriften Q" . $_POST['Quartal'] . "/" . $_POST['Jahr'] . "\n");
	$sql = "SELECT *, DATE_FORMAT(Re_Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Eingang WHERE (YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Bezahlt_am) = \"" . $_POST['Quartal'] . "\") ORDER BY Bezahlt_am ASC;";
	$sqlust = "SELECT SUM(Preis) AS Preis, MwSt FROM " . PREFIX . "_Eingang WHERE YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Bezahlt_am) = \"" . $_POST['Quartal'] . "\" GROUP BY MwSt DESC;";
	$sqlsum = "SELECT SUM(Preis) AS Preis, SUM(Preis * MwSt / 100) AS MwSt, SUM(Preis * (1 + MwSt / 100)) AS Gesamt FROM " . PREFIX . "_Eingang WHERE (YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Bezahlt_am) = \"" . $_POST['Quartal'] . "\") ORDER BY Bezahlt_am ASC;";
}
elseif ("j" == $_POST['Suche'])
{
	$pdf->Write(20, "bezahlte Eingangsrechnungen / Gutschriften " . $_POST['Jahr'] . "\n");
	$sql = "SELECT *, DATE_FORMAT(Re_Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Eingang WHERE YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" ORDER BY Bezahlt_am ASC;";
	$sqlust = "SELECT SUM(Preis) AS Preis, MwSt FROM " . PREFIX . "_Eingang WHERE YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" GROUP BY MwSt DESC;";
	$sqlsum = "SELECT SUM(Preis) AS Preis, SUM(Preis * MwSt / 100) AS MwSt, SUM(Preis * (1 + MwSt / 100)) AS Gesamt FROM " . PREFIX . "_Eingang WHERE (YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\") ORDER BY Bezahlt_am ASC;";
}
$result = $db->query($sql);
$resultust = $db->query($sqlust);
$resultsum = $db->query($sqlsum);

$pdf->SetTextColor(00);
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->Cell(65, 4, "Firma / Name", 0);
$pdf->Cell(20, 4, "Re-Nr.", 0);
$pdf->Cell(20, 4, "Re-Datum", 0);
$pdf->Cell(20, 4, "Netto", 0, 0, 'R');
$pdf->Cell(15, 4, "MwSt", 0, 0, 'R');
$pdf->Cell(20, 4, "Gesamt", 0, 0, 'R');
$pdf->Cell(20, 4, "Bezahlt", 0, 1, 'R');
$pdf->Write(4, "\n");
$y = $pdf->GetY() - 2;
$pdf->Line(20, $y, 200, $y);
$pdf->SetFont('Helvetica', '', 8);

while ($nt = $result->fetch_assoc())
{
	$pdf->Cell(65, 4, $nt['Kontakt'], 0);
	while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
	{
		$nt['ID'] = "0" . $nt['ID'];
	}
	$pdf->Cell(20, 4, $nt['Re_Nr'], 0);
	$pdf->Cell(20, 4, $nt['Datum'], 0);
	$pdf->Cell(20, 4, number_format($nt['Preis'], '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->Cell(15, 4, number_format($nt['Preis'] * $nt['MwSt'] / 100, '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->Cell(20, 4, number_format($nt['Preis'] * (1 + $nt['MwSt'] / 100), '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->Cell(20, 4, $nt['Bezahlt'], 0, 1, 'R');
}
$y = $pdf->GetY() + 2;
$pdf->Line(20, $y, 200, $y);
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->Write(4, "\n");

$pdf->SetFont('Helvetica', 'B', 14);
$pdf->SetTextColor($_SESSION['Firma']['F2_Red'], $_SESSION['Firma']['F2_Green'], $_SESSION['Firma']['F2_Blue']);

$pdf->Write(20, "Beträge aufgeschlüsselt nach Mehrwertsteuersatz\n");
$Sonst_Preis = $Sonst_MwSt = 0;
$pdf->SetTextColor(00);
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->Cell(105, 4, "", 0);
$pdf->Cell(25, 4, "Netto", 0, 0, 'R');
$pdf->Cell(25, 4, "MwSt", 0, 0, 'R');
$pdf->Cell(25, 4, "Gesamt", 0, 1, 'R');
$pdf->Write(4, "\n");
$y = $pdf->GetY() - 2;
$pdf->Line(20, $y, 200, $y);
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetFont('Helvetica', '', 10);

while ($ntust = $resultust->fetch_assoc())
{
	if (19 == $ntust['MwSt'])
	{
		$pdf->Cell(105, 4, "Eingangsrechnungen / Gutschriften zu 19% MwSt.");
		$pdf->Cell(25, 4, number_format($ntust['Preis'], '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->SetFont('Helvetica', 'B', 10);
		$pdf->Cell(25, 4, number_format($ntust['Preis'] * 0.19, '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->SetFont('Helvetica', '', 10);
		$pdf->Cell(25, 4, number_format($ntust['Preis'] * 1.19, '2', ',', '.') . " €", 0, 1, 'R');
	}
	elseif (7 == $ntust['MwSt'])
	{
		$pdf->Cell(105, 4, "Eingangsrechnungen / Gutschriften zu 7% MwSt.");
		$pdf->Cell(25, 4, number_format($ntust['Preis'], '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->SetFont('Helvetica', 'B', 10);
		$pdf->Cell(25, 4, number_format($ntust['Preis'] * 0.07, '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->SetFont('Helvetica', '', 10);
		$pdf->Cell(25, 4, number_format($ntust['Preis'] * 1.07, '2', ',', '.') . " €", 0, 1, 'R');
	}
	elseif (0 == $ntust['MwSt'])
	{
		$pdf->Cell(105, 4, "Umsatzsteuerfreie Eingangsrechnungen / Gutschriften");
		$pdf->Cell(25, 4, number_format($ntust['Preis'], '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->SetFont('Helvetica', 'B', 10);
		$pdf->Cell(25, 4, number_format(0, '2', ',', '.') . " €", 0, 0, 'R');
		$pdf->SetFont('Helvetica', '', 10);
		$pdf->Cell(25, 4, number_format($ntust['Preis'], '2', ',', '.') . " €", 0, 1, 'R');
	}
	else
	{
		$Sonst_Preis = $Sonst_Preis + $ntust['Preis'];
		$Sonst_MwSt = $Sonst_MwSt + $ntust['Preis'] * $ntust['MwSt'] / 100;
	}
}
if (0 != $Sonst_Preis)
{
	$pdf->Cell(105, 4, "Eingangsrechnungen / Gutschriften zu anderen Mehrwertsteuersätzen");
	$pdf->Cell(25, 4, number_format($Sonst_Preis, '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->SetFont('Helvetica', 'B', 10);
	$pdf->Cell(25, 4, number_format($Sonst_MwSt, '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->SetFont('Helvetica', '', 10);
	$pdf->Cell(25, 4, number_format($Sonst_Preis + $Sonst_MwSt, '2', ',', '.') . " €", 0, 1, 'R');
}
$ntsum = $resultsum->fetch_assoc();
$pdf->Cell(105, 4, "Gesamt", 0);
$pdf->Cell(25, 4, number_format($ntsum['Preis'], '2', ',', '.') . " €", 0, 0, 'R');
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->Cell(25, 4, number_format($ntsum['MwSt'], '2', ',', '.') . " €", 0, 0, 'R');
$pdf->SetFont('Helvetica', '', 10);
$pdf->Cell(25, 4, number_format($ntsum['Gesamt'], '2', ',', '.') . " €", 0, 1, 'R');

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

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