Files
PackControl/pirp/public/mahnung_new.php

129 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
require_once __DIR__ . '/../src/config.php';
require_once __DIR__ . '/../src/auth.php';
require_once __DIR__ . '/../src/db.php';
require_once __DIR__ . '/../src/invoice_functions.php';
require_once __DIR__ . '/../src/pdf_functions.php';
require_once __DIR__ . '/../src/icons.php';
require_login();
$invoice_id = isset($_GET['invoice_id']) ? (int)$_GET['invoice_id'] : 0;
if (!$invoice_id) {
header('Location: ' . url_for('invoices.php'));
exit;
}
$inv = get_invoice_with_customer($invoice_id);
if (!$inv || $inv['paid']) {
header('Location: ' . url_for('invoices.php?msg=' . urlencode('Mahnung nur für offene Rechnungen möglich.')));
exit;
}
$pdo = get_db();
// Mahnstufe auto-bestimmen
$stmt = $pdo->prepare("SELECT COUNT(*) FROM mahnungen WHERE invoice_id = :id");
$stmt->execute([':id' => $invoice_id]);
$existing_count = (int)$stmt->fetchColumn();
$next_level = min($existing_count + 1, 3);
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$mahnung_date = $_POST['mahnung_date'] ?? date('Y-m-d');
$level = (int)($_POST['level'] ?? $next_level);
$fee_amount = (float)str_replace(',', '.', $_POST['fee_amount'] ?? '0');
if ($level < 1 || $level > 3) $level = $next_level;
try {
$stmt = $pdo->prepare("INSERT INTO mahnungen (invoice_id, mahnung_date, level, fee_amount)
VALUES (:iid, :mdate, :level, :fee)
RETURNING id");
$stmt->execute([
':iid' => $invoice_id,
':mdate' => $mahnung_date,
':level' => $level,
':fee' => $fee_amount,
]);
$mahnung_id = (int)$stmt->fetchColumn();
archive_mahnung_pdf($mahnung_id);
header('Location: ' . url_for('mahnung_pdf.php?id=' . $mahnung_id));
exit;
} catch (\Exception $e) {
$error = $e->getMessage();
}
}
$level_labels = [1 => 'Mahnung', 2 => '2. Mahnung', 3 => '3. Mahnung (Letzte)'];
?>
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Mahnung <?= htmlspecialchars($inv['invoice_number']) ?></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') ?>" class="active"><?= 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') ?>"><?= 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>
<div class="module-subnav">
<a href="<?= url_for('invoices.php') ?>">← Zurück zu Rechnungen</a>
</div>
<?php if ($error): ?>
<p class="error"><?= htmlspecialchars($error) ?></p>
<?php endif; ?>
<section>
<h2>Mahnung erstellen</h2>
<div style="max-width:460px;">
<table class="list" style="margin-bottom:16px;">
<tr><td style="color:var(--text-muted);width:140px;">Rechnung</td><td><strong><?= htmlspecialchars($inv['invoice_number']) ?></strong></td></tr>
<tr><td style="color:var(--text-muted);">Kunde</td><td><?= htmlspecialchars($inv['customer_name']) ?></td></tr>
<tr><td style="color:var(--text-muted);">Rechnungsdatum</td><td><?= date('d.m.Y', strtotime($inv['invoice_date'])) ?></td></tr>
<tr><td style="color:var(--text-muted);">Betrag</td><td><?= number_format($inv['total_gross'], 2, ',', '.') ?> €</td></tr>
</table>
<form method="post" style="display:flex;flex-direction:column;gap:12px;">
<label>Mahnstufe:
<select name="level">
<?php foreach ($level_labels as $l => $lbl): ?>
<option value="<?= $l ?>" <?= $l == $next_level ? 'selected' : '' ?>><?= $lbl ?></option>
<?php endforeach; ?>
</select>
</label>
<label>Mahndatum:
<input type="date" name="mahnung_date" value="<?= date('Y-m-d') ?>" required>
</label>
<label>Mahngebühr (€):
<input type="text" name="fee_amount" value="0,00" style="width:120px;">
</label>
<div>
<button type="submit">Mahnung erstellen &amp; PDF öffnen</button>
<a href="<?= url_for('invoices.php') ?>" style="margin-left:12px;">Abbrechen</a>
</div>
</form>
</div>
</section>
</main>
<script src="assets/command-palette.js"></script>
</body>
</html>