<?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, "erstellte Ausgangsrechnungen " . $_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 (YEAR(Datum) = \"" . $_POST['Jahr'] . "\" AND MONTH(Datum) = \"" . $_POST['Monat'] . "\") ORDER BY Datum ASC;";
}
elseif ("q" == $_POST['Suche'])
{
	$pdf->Write(20, "erstellte Ausgangsrechnungen 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 (YEAR(Datum) = \"" . $_POST['Jahr'] . "\" AND QUARTER(Datum) = \"" . $_POST['Quartal'] . "\") ORDER BY Datum ASC;";
}
elseif ("j" == $_POST['Suche'])
{
	$pdf->Write(20, "erstellte Ausgangsrechnungen " . $_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 (YEAR(Datum) = \"" . $_POST['Jahr'] . "\") ORDER BY Datum ASC;";
}

$result = $db->query($sql);
$_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, "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");