337 lines
13 KiB
PHP
337 lines
13 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../src/config.php';
|
|
require_once __DIR__ . '/../src/auth.php';
|
|
require_once __DIR__ . '/../src/db.php';
|
|
require_once __DIR__ . '/../src/journal_functions.php';
|
|
require_once __DIR__ . '/../src/icons.php';
|
|
require_login();
|
|
|
|
$pdo = get_db();
|
|
|
|
// Jahr bestimmen
|
|
$years = get_journal_years();
|
|
$current_cal_year = (int)date('Y');
|
|
|
|
$year = isset($_GET['year']) ? (int)$_GET['year'] : $current_cal_year;
|
|
|
|
// Journal-Jahr finden (oder automatisch erstellen bei Bedarf)
|
|
$journal_year = get_journal_year_by_year($year);
|
|
|
|
// Fehlende Buchungen nachholen (Quick-Fix)
|
|
$fix_msg = '';
|
|
$fix_errors = [];
|
|
if (isset($_GET['fix_missing']) && $journal_year) {
|
|
$fixed_inv = 0;
|
|
$fixed_exp = 0;
|
|
$consistency = check_journal_consistency();
|
|
|
|
foreach ($consistency['unbooked_invoice_list'] as $inv) {
|
|
try {
|
|
create_journal_entry_from_invoice((int)$inv['id']);
|
|
$fixed_inv++;
|
|
} catch (Exception $e) {
|
|
$fix_errors[] = 'Rechnung ' . ($inv['invoice_number'] ?? '#' . $inv['id']) . ': ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
foreach ($consistency['unbooked_expense_list'] as $exp) {
|
|
try {
|
|
create_journal_entry_from_expense((int)$exp['id']);
|
|
$fixed_exp++;
|
|
} catch (Exception $e) {
|
|
$fix_errors[] = 'Ausgabe "' . ($exp['description'] ?? '#' . $exp['id']) . '": ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
$fix_msg = $fixed_inv . ' Rechnungen und ' . $fixed_exp . ' Ausgaben nachgebucht.';
|
|
if ($fix_errors) {
|
|
$fix_msg .= ' ' . count($fix_errors) . ' Fehler aufgetreten.';
|
|
}
|
|
}
|
|
|
|
// CSV-Export Journal
|
|
if (isset($_GET['csv_journal']) && $journal_year) {
|
|
$euer = generate_journal_euer((int)$journal_year['id']);
|
|
|
|
header('Content-Type: text/csv; charset=utf-8');
|
|
header('Content-Disposition: attachment; filename="eur_' . $year . '.csv"');
|
|
|
|
$out = fopen('php://output', 'w');
|
|
fwrite($out, "\xEF\xBB\xBF");
|
|
|
|
fputcsv($out, ['EÜR - ' . $year], ';');
|
|
fputcsv($out, [], ';');
|
|
|
|
fputcsv($out, ['Position', 'Betrag'], ';');
|
|
fputcsv($out, [], ';');
|
|
|
|
fputcsv($out, ['EINNAHMEN'], ';');
|
|
foreach ($euer['erloese_detail'] as $row) {
|
|
fputcsv($out, [$row['name'], number_format((float)$row['total'], 2, ',', '.')], ';');
|
|
}
|
|
if ($euer['sonstiges_einnahmen'] > 0) {
|
|
fputcsv($out, ['Sonstige Einnahmen', number_format($euer['sonstiges_einnahmen'], 2, ',', '.')], ';');
|
|
}
|
|
fputcsv($out, ['Einnahmen gesamt', number_format($euer['einnahmen_total'], 2, ',', '.')], ';');
|
|
fputcsv($out, [], ';');
|
|
|
|
fputcsv($out, ['AUSGABEN'], ';');
|
|
if ($euer['wareneingang'] > 0) {
|
|
fputcsv($out, ['Wareneingang', number_format($euer['wareneingang'], 2, ',', '.')], ';');
|
|
}
|
|
foreach ($euer['aufwand_detail'] as $row) {
|
|
fputcsv($out, [$row['name'], number_format((float)$row['total'], 2, ',', '.')], ';');
|
|
}
|
|
if ($euer['sonstiges_ausgaben'] > 0) {
|
|
fputcsv($out, ['Sonstige Ausgaben', number_format($euer['sonstiges_ausgaben'], 2, ',', '.')], ';');
|
|
}
|
|
fputcsv($out, ['Ausgaben gesamt', number_format($euer['ausgaben_total'], 2, ',', '.')], ';');
|
|
fputcsv($out, [], ';');
|
|
|
|
fputcsv($out, ['STEUER'], ';');
|
|
fputcsv($out, ['MwSt (eingenommen)', number_format($euer['mwst'], 2, ',', '.')], ';');
|
|
fputcsv($out, ['VorSt (gezahlt)', number_format($euer['vorst'], 2, ',', '.')], ';');
|
|
fputcsv($out, ['Steuer-Saldo', number_format($euer['steuer_saldo'], 2, ',', '.')], ';');
|
|
fputcsv($out, [], ';');
|
|
|
|
if ($euer['privat_entnahmen'] > 0 || $euer['privat_einlagen'] > 0) {
|
|
fputcsv($out, ['PRIVATKONTEN'], ';');
|
|
if ($euer['privat_einlagen'] > 0) {
|
|
fputcsv($out, ['Privateinlagen', number_format($euer['privat_einlagen'], 2, ',', '.')], ';');
|
|
}
|
|
if ($euer['privat_entnahmen'] > 0) {
|
|
fputcsv($out, ['Privatentnahmen', number_format($euer['privat_entnahmen'], 2, ',', '.')], ';');
|
|
}
|
|
fputcsv($out, [], ';');
|
|
}
|
|
|
|
fputcsv($out, ['GEWINN', number_format($euer['gewinn'], 2, ',', '.')], ';');
|
|
|
|
fclose($out);
|
|
exit;
|
|
}
|
|
|
|
// Journal-Daten laden
|
|
$journal_euer = null;
|
|
if ($journal_year) {
|
|
$journal_euer = generate_journal_euer((int)$journal_year['id']);
|
|
}
|
|
|
|
// Konsistenz-Check: Fehlende Buchungen
|
|
$consistency = check_journal_consistency();
|
|
$has_missing = ($consistency['unbooked_invoices'] > 0 || $consistency['unbooked_expenses'] > 0);
|
|
|
|
// Verfügbare Jahre sammeln (aus Journal + Rechnungen + Ausgaben)
|
|
$available_years = [];
|
|
foreach ($years as $y) {
|
|
$available_years[(int)$y['year']] = true;
|
|
}
|
|
$stmt = $pdo->query("SELECT DISTINCT EXTRACT(YEAR FROM invoice_date)::int AS y FROM invoices WHERE paid = TRUE ORDER BY y DESC");
|
|
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
|
$available_years[$row['y']] = true;
|
|
}
|
|
try {
|
|
$stmt = $pdo->query("SELECT DISTINCT EXTRACT(YEAR FROM expense_date)::int AS y FROM expenses WHERE paid = TRUE ORDER BY y DESC");
|
|
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
|
$available_years[$row['y']] = true;
|
|
}
|
|
} catch (PDOException $e) {}
|
|
krsort($available_years);
|
|
$available_years = array_keys($available_years);
|
|
if (empty($available_years)) {
|
|
$available_years = [$current_cal_year];
|
|
}
|
|
?>
|
|
<!doctype html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>EÜR <?= $year ?></title>
|
|
<link rel="stylesheet" href="assets/style.css">
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1>PIRP</h1>
|
|
<nav>
|
|
<a href="<?= url_for('index.php') ?>"><?= icon_dashboard() ?>Dashboard</a>
|
|
<a href="<?= url_for('invoices.php') ?>"><?= icon_invoices() ?>Rechnungen</a>
|
|
<a href="<?= url_for('customers.php') ?>"><?= icon_customers() ?>Kunden</a>
|
|
<a href="<?= url_for('expenses.php') ?>"><?= icon_expenses() ?>Ausgaben</a>
|
|
<a href="<?= url_for('belegarchiv.php') ?>"><?= icon_archive() ?>Belege</a>
|
|
<a href="<?= url_for('journal.php') ?>"><?= icon_journal() ?>Journal</a>
|
|
<a href="<?= url_for('euer.php') ?>" class="active"><?= icon_euer() ?>EÜR</a>
|
|
<a href="<?= url_for('settings.php') ?>"><?= icon_settings() ?>Einstellungen</a>
|
|
<a href="<?= url_for('logout.php') ?>"><?= icon_logout() ?>Logout (<?= htmlspecialchars($_SESSION['username'] ?? '') ?>)</a>
|
|
<span class="cmd-k-hint" onclick="document.dispatchEvent(new KeyboardEvent('keydown',{key:'k',ctrlKey:true}))"><kbd>Ctrl+K</kbd></span>
|
|
</nav>
|
|
</header>
|
|
<main>
|
|
<h2>Einnahmen-Überschuss-Rechnung <?= $year ?></h2>
|
|
|
|
<!-- Jahr-Auswahl -->
|
|
<form method="get" style="margin-bottom:16px;">
|
|
<div class="flex-row">
|
|
<label>Jahr:
|
|
<select name="year" onchange="this.form.submit();">
|
|
<?php foreach ($available_years as $y): ?>
|
|
<option value="<?= $y ?>" <?= $y == $year ? 'selected' : '' ?>><?= $y ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</label>
|
|
</div>
|
|
</form>
|
|
|
|
<?php if ($fix_msg): ?>
|
|
<p class="success"><?= htmlspecialchars($fix_msg) ?></p>
|
|
<?php if (!empty($fix_errors)): ?>
|
|
<div class="gobd-warning">
|
|
<h3>Fehler beim Nachbuchen</h3>
|
|
<ul>
|
|
<?php foreach ($fix_errors as $fe): ?>
|
|
<li><?= htmlspecialchars($fe) ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<p style="font-size:11px;">Bitte die betroffenen Belege manuell prüfen und ggf. eine Aufwandskategorie zuweisen.</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($has_missing): ?>
|
|
<div class="gobd-warning">
|
|
<h3>Fehlende Journalbuchungen</h3>
|
|
<p>Es gibt bezahlte Belege ohne Journaleintrag. Die EÜR ist dadurch unvollständig.</p>
|
|
<ul>
|
|
<?php if ($consistency['unbooked_invoices'] > 0): ?>
|
|
<li><strong><?= $consistency['unbooked_invoices'] ?></strong> bezahlte Rechnung(en) ohne Journalbuchung</li>
|
|
<?php endif; ?>
|
|
<?php if ($consistency['unbooked_expenses'] > 0): ?>
|
|
<li><strong><?= $consistency['unbooked_expenses'] ?></strong> bezahlte Ausgabe(n) ohne Journalbuchung</li>
|
|
<?php endif; ?>
|
|
</ul>
|
|
<a href="<?= url_for('euer.php?year=' . $year . '&fix_missing=1') ?>"
|
|
class="button" onclick="return confirm('Fehlende Journalbuchungen jetzt automatisch erstellen?');">
|
|
Fehlende Buchungen nachholen
|
|
</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($journal_euer): ?>
|
|
<section class="euer-section">
|
|
<p class="euer-desc">Basierend auf Journalbuchungen (Zufluss-/Abflussprinzip)</p>
|
|
|
|
<h4>Einnahmen</h4>
|
|
<table class="list">
|
|
<tbody>
|
|
<?php foreach ($journal_euer['erloese_detail'] as $row): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($row['name']) ?></td>
|
|
<td style="text-align:right;"><?= number_format((float)$row['total'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if ($journal_euer['sonstiges_einnahmen'] > 0): ?>
|
|
<tr>
|
|
<td>Sonstige Einnahmen</td>
|
|
<td style="text-align:right;"><?= number_format($journal_euer['sonstiges_einnahmen'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<tr>
|
|
<td><strong>Einnahmen gesamt</strong></td>
|
|
<td style="text-align:right;"><strong><?= number_format($journal_euer['einnahmen_total'], 2, ',', '.') ?> €</strong></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<h4>Ausgaben</h4>
|
|
<table class="list">
|
|
<tbody>
|
|
<?php if ($journal_euer['wareneingang'] > 0): ?>
|
|
<tr>
|
|
<td>Wareneingang</td>
|
|
<td style="text-align:right;"><?= number_format($journal_euer['wareneingang'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($journal_euer['aufwand_detail'] as $row): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($row['name']) ?></td>
|
|
<td style="text-align:right;"><?= number_format((float)$row['total'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if ($journal_euer['sonstiges_ausgaben'] > 0): ?>
|
|
<tr>
|
|
<td>Sonstige Ausgaben</td>
|
|
<td style="text-align:right;"><?= number_format($journal_euer['sonstiges_ausgaben'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<tr>
|
|
<td><strong>Ausgaben gesamt</strong></td>
|
|
<td style="text-align:right;"><strong><?= number_format($journal_euer['ausgaben_total'], 2, ',', '.') ?> €</strong></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<h4>Steuer</h4>
|
|
<table class="list">
|
|
<tbody>
|
|
<tr>
|
|
<td>MwSt (eingenommen)</td>
|
|
<td style="text-align:right;"><?= number_format($journal_euer['mwst'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<tr>
|
|
<td>VorSt (gezahlt)</td>
|
|
<td style="text-align:right;"><?= number_format($journal_euer['vorst'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Steuer-Saldo</strong></td>
|
|
<td style="text-align:right;"><strong><?= number_format($journal_euer['steuer_saldo'], 2, ',', '.') ?> €</strong></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<?php if ($journal_euer['privat_entnahmen'] > 0 || $journal_euer['privat_einlagen'] > 0): ?>
|
|
<h4>Privatkonten</h4>
|
|
<table class="list">
|
|
<tbody>
|
|
<?php if ($journal_euer['privat_einlagen'] > 0): ?>
|
|
<tr>
|
|
<td>Privateinlagen</td>
|
|
<td style="text-align:right;"><?= number_format($journal_euer['privat_einlagen'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<?php if ($journal_euer['privat_entnahmen'] > 0): ?>
|
|
<tr>
|
|
<td>Privatentnahmen</td>
|
|
<td style="text-align:right;"><?= number_format($journal_euer['privat_entnahmen'], 2, ',', '.') ?> €</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
|
|
<h4>Ergebnis</h4>
|
|
<table class="list">
|
|
<tbody>
|
|
<tr class="euer-result <?= $journal_euer['gewinn'] < 0 ? 'negative' : '' ?>">
|
|
<td><strong>Gewinn / Verlust</strong></td>
|
|
<td style="text-align:right;"><strong><?= number_format($journal_euer['gewinn'], 2, ',', '.') ?> €</strong></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<div style="margin-top:8px;">
|
|
<a href="<?= url_for('euer.php?year=' . $year . '&csv_journal=1') ?>" class="button-secondary">CSV Export</a>
|
|
</div>
|
|
</section>
|
|
<?php else: ?>
|
|
<section class="euer-section">
|
|
<p class="euer-desc">Kein Journal für <?= $year ?> vorhanden.</p>
|
|
<?php if ($has_missing): ?>
|
|
<p>Klicke oben auf "Fehlende Buchungen nachholen" um das Journal automatisch zu erstellen.</p>
|
|
<?php else: ?>
|
|
<p><a href="<?= url_for('settings.php?tab=journal') ?>">Jahr im Journal anlegen</a></p>
|
|
<?php endif; ?>
|
|
</section>
|
|
<?php endif; ?>
|
|
</main>
|
|
<script src="assets/command-palette.js"></script>
|
|
</body>
|
|
</html>
|