<?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 Ausgangsrechnungen " . $_POST['Monat'] . "/" . $_POST['Jahr'] . "\n");
	$teil = "bezahlte Teilzahlungen " . $_POST['Monat'] . "/" . $_POST['Jahr'] . "\n";
	$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Rechnungen WHERE (Bezahlt = \"1\" AND YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND MONTH(Bezahlt_am) = \"" . $_POST['Monat'] . "\" AND Teilzahlungen = \"0\") ORDER BY Bezahlt_am ASC;";
	$sqlt = "SELECT *, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Teilzahlungen 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 Ausgangsrechnungen Q" . $_POST['Quartal'] . "/" . $_POST['Jahr'] . "\n");
	$teil = "bezahlte Teilzahlungen Q" . $_POST['Quartal'] . "/" . $_POST['Jahr'] . "\n";
	$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Rechnungen WHERE (Bezahlt = \"1\" AND YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Bezahlt_am) = \"" . $_POST['Quartal'] . "\" AND Teilzahlungen = \"0\") ORDER BY Bezahlt_am ASC;";
	$sqlt = "SELECT *, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Teilzahlungen 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 Ausgangsrechnungen " . $_POST['Jahr'] . "\n");
	$teil = "bezahlte Teilzahlungen " . $_POST['Jahr'] . "\n";
	$sql = "SELECT *, DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Rechnungen WHERE (Bezahlt = \"1\" AND YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\" AND Teilzahlungen = \"0\") ORDER BY Bezahlt_am ASC;";
	$sqlt = "SELECT *, DATE_FORMAT(Bezahlt_am, '%d.%m.%Y') AS Bezahlt FROM " . PREFIX . "_Teilzahlungen WHERE (YEAR(Bezahlt_am) = \"" . $_POST['Jahr'] . "\") ORDER BY Bezahlt_am ASC;";
}

$result = $db->query($sql);
$resultt = $db->query($sqlt);
$_19 = $_19_MwSt = 0;
$_7 = $_7_MwSt = 0;
$_0 = 0;
$Sonst_Preis = $Sonst_MwSt = 0;

$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())
{
	if (19 == $nt['MwSt'])
	{
		$_19 = $_19 + $nt['Preis'];
		$_19_MwSt = $_19_MwSt + round($nt['Preis'] * $nt['MwSt'] / 100, 2);
	}
	elseif (7 == $nt['MwSt'])
	{
		$_7 = $_7 + $nt['Preis'];
		$_7_MwSt = $_7_MwSt + round($nt['Preis'] * $nt['MwSt'] / 100, 2);
	}
	elseif (0 == $nt['MwSt'])
	{
		$_0 = $_0 + $nt['Preis'];
	}
	else
	{
		$Sonst_Preis = $Sonst_Preis + $nt['Preis'];
		$Sonst_MwSt = $Sonst_MwSt + round($nt['Preis'] * $nt['MwSt'] / 100, 2);
	}
	$sqlk = "SELECT Firma, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $nt['Kontakt_ID'] . "\";";
	$resultk = $db->query($sqlk);
	$ntk = $resultk->fetch_assoc();
	$pdf->Cell(65, 4, $ntk['Nachname'] . ", " . $ntk['Firma'], 0);
	while (strlen($nt['ID']) < $_SESSION['Firma']['Stellen'])
	{
		$nt['ID'] = "0" . $nt['ID'];
	}
	$pdf->Cell(20, 4, $_POST['Jahr'] . "_" . $nt['ID'], 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->Write(4, "\n");

$Rech = $_19 + $_7 + $_0 + $Sonst_Preis;
$Rech_MwSt = $_19_MwSt + $_7_MwSt + $_0_MwSt + $Sonst_MwSt;

$pdf->Cell(105, 4, "Gesamt", 0);
$pdf->Cell(20, 4, number_format($Rech, '2', ',', '.') . " €", 0, 0, 'R');
$pdf->Cell(15, 4, number_format($Rech_MwSt, '2', ',', '.') . " €", 0, 0, 'R');
$pdf->Cell(20, 4, number_format($Rech + $Rech_MwSt, '2', ',', '.') . " €", 0, 0, 'R');
$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, $teil);

$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 ($ntt = $resultt->fetch_assoc())
{
	if (19 == $ntt['MwSt'])
	{
		$_19 = $_19 + $ntt['Preis'];
		$_19_MwSt = $_19_MwSt + round($ntt['Preis'] * $ntt['MwSt'] / 100, 2);
	}
	elseif (7 == $ntt['MwSt'])
	{
		$_7 = $_7 + $ntt['Preis'];
		$_7_MwSt = $_7_MwSt + round($ntt['Preis'] * $ntt['MwSt'] / 100, 2);
	}
	elseif (0 == $ntt['MwSt'])
	{
		$_0 = $_0 + $ntt['Preis'];
	}
	else
	{
		$Sonst_Preis = $Sonst_Preis + $ntt['Preis'];
		$Sonst_MwSt = $Sonst_MwSt + round($ntt['Preis'] * $ntt['MwSt'] / 100, 2);
	}
	$sqlr = "SELECT DATE_FORMAT(Datum, '%d.%m.%Y') AS Datum, Kontakt_ID FROM " . PREFIX . "_Rechnungen WHERE (Jahr = \"" . $ntt['Referenz_Jahr'] . "\" AND ID = \"" . $ntt['Referenz_ID'] . "\");";
	$resultr = $db->query($sqlr);
	$ntr = $resultr->fetch_assoc();
	$sqlk = "SELECT Firma, Vorname, Nachname FROM " . PREFIX . "_Kontakte WHERE ID = \"" . $ntr['Kontakt_ID'] . "\";";
	$resultk = $db->query($sqlk);
	$ntk = $resultk->fetch_assoc();
	$pdf->Cell(65, 4, $ntk['Nachname'] . ", " . $ntk['Firma'], 0);
	while (strlen($ntt['Referenz_ID']) < $_SESSION['Firma']['Stellen'])
	{
		$ntt['Referenz_ID'] = "0" . $ntt['Referenz_ID'];
	}
	$pdf->Cell(20, 4, $ntt['Referenz_Jahr'] . "_" . $ntt['Referenz_ID'], 0);
	$pdf->Cell(20, 4, $ntr['Datum'], 0);
	$pdf->Cell(20, 4, number_format($ntt['Preis'], '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->Cell(15, 4, number_format($ntt['Preis'] * $ntt['MwSt'] / 100, '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->Cell(20, 4, number_format($ntt['Preis'] * (1 + $ntt['MwSt'] / 100), '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->Cell(20, 4, $ntt['Bezahlt'], 0, 1, 'R');
}
$y = $pdf->GetY() + 2;
$pdf->Line(20, $y, 200, $y);
$pdf->Write(4, "\n");

$Teil = $_19 + $_7 + $_0 + $Sonst_Preis - $Rech;
$Teil_MwSt = $_19_MwSt + $_7_MwSt + $_0_MwSt + $Sonst_MwSt - $Rech_MwSt;

$pdf->Cell(105, 4, "Gesamt", 0);
$pdf->Cell(20, 4, number_format($Teil, '2', ',', '.') . " €", 0, 0, 'R');
$pdf->Cell(15, 4, number_format($Teil_MwSt, '2', ',', '.') . " €", 0, 0, 'R');
$pdf->Cell(20, 4, number_format($Teil + $Teil_MwSt, '2', ',', '.') . " €", 0, 0, 'R');
$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");
$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);

if (0 != $_19)
{
	$pdf->Cell(105, 4, "Umsätze zu 19% MwSt.");
	$pdf->Cell(25, 4, number_format($_19, '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->SetFont('Helvetica', 'B', 10);
	$pdf->Cell(25, 4, number_format($_19_MwSt, '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->SetFont('Helvetica', '', 10);
	$pdf->Cell(25, 4, number_format($_19 + $_19_MwSt, '2', ',', '.') . " €", 0, 1, 'R');
}
if (0 != $_7)
{
	$pdf->Cell(105, 4, "Umsätze zu 7% MwSt.");
	$pdf->Cell(25, 4, number_format($_7, '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->SetFont('Helvetica', 'B', 10);
	$pdf->Cell(25, 4, number_format($_7_MwSt, '2', ',', '.') . " €", 0, 0, 'R');
	$pdf->SetFont('Helvetica', '', 10);
	$pdf->Cell(25, 4, number_format($_7 + $_7_MwSt, '2', ',', '.') . " €", 0, 1, 'R');
}
if (0 != $_0)
{
	$pdf->Cell(105, 4, "Umsatzsteuerfreie Verkäufe");
	$pdf->Cell(25, 4, number_format($_0, '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($_0, '2', ',', '.') . " €", 0, 1, 'R');
}
if (0 != $Sonst_Preis)
{
	$pdf->Cell(105, 4, "Umsätze 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');
}

$pdf->Cell(105, 4, "Gesamt", 0);
$pdf->Cell(25, 4, number_format(($_19 + $_7 + $_0 + $Sonst_Preis), '2', ',', '.')." €", 0, 0, 'R');
$pdf->SetFont('Helvetica','B', 10);
$pdf->Cell(25, 4, number_format(($_19_MwSt + $_7_MwSt + $Sonst_MwSt), '2', ',', '.')." €", 0, 0, 'R');
$pdf->SetFont('Helvetica','', 10);
$pdf->Cell(25, 4, number_format(($_19 + $_7 + $_0 + $Sonst_Preis + $_19_MwSt + $_7_MwSt + $Sonst_MwSt), '2', ',', '.')." €", 0, 0, 'R');

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

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