Files
PackControl/pirp/public/recurring_generate.php

159 lines
6.3 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/recurring_functions.php';
require_once __DIR__ . '/../src/icons.php';
require_login();
$msg = '';
$error = '';
$generated = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$selected = $_POST['template_ids'] ?? [];
if (empty($selected)) {
$error = 'Bitte mindestens eine Vorlage auswählen.';
} else {
$success = 0;
$failed = 0;
foreach ($selected as $tid) {
$tid = (int)$tid;
$invoice_id = generate_invoice_from_template($tid);
if ($invoice_id) {
$success++;
$generated[] = $invoice_id;
} else {
$failed++;
}
}
if ($success > 0) {
$msg = "$success Rechnung(en) erfolgreich generiert.";
}
if ($failed > 0) {
$error = "$failed Rechnung(en) konnten nicht generiert werden.";
}
}
}
$pending = get_pending_recurring_invoices();
?>
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Abo-Rechnungen generieren</title>
<link rel="stylesheet" href="assets/style.css">
<script>
function toggleAll(checkbox) {
document.querySelectorAll('input[name="template_ids[]"]').forEach(cb => {
cb.checked = checkbox.checked;
});
}
</script>
</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') ?>">Übersicht</a>
<a href="<?= url_for('invoice_new.php') ?>">Neue Rechnung</a>
<a href="<?= url_for('recurring.php') ?>" class="active">Abo-Rechnungen</a>
</div>
<?php if ($msg): ?><p class="success"><?= htmlspecialchars($msg) ?></p><?php endif; ?>
<?php if ($error): ?><p class="error"><?= htmlspecialchars($error) ?></p><?php endif; ?>
<?php if (!empty($generated)): ?>
<section>
<h2>Generierte Rechnungen</h2>
<div>
<ul>
<?php foreach ($generated as $inv_id): ?>
<li><a href="<?= url_for('invoice_pdf.php?id=' . $inv_id) ?>" target="_blank">Rechnung #<?= $inv_id ?> anzeigen</a></li>
<?php endforeach; ?>
</ul>
</div>
</section>
<?php endif; ?>
<section>
<h2>Fällige Abo-Rechnungen</h2>
<div>
<?php if (empty($pending)): ?>
<p class="success">Keine fälligen Abo-Rechnungen. Alles erledigt!</p>
<p><a href="<?= url_for('recurring.php') ?>" class="button-secondary">Zurück zur Übersicht</a></p>
<?php else: ?>
<form method="post">
<table class="list">
<thead>
<tr>
<th style="width:30px"><input type="checkbox" onclick="toggleAll(this)" checked></th>
<th>Vorlage</th>
<th>Kunde</th>
<th>Intervall</th>
<th>Fällig seit</th>
</tr>
</thead>
<tbody>
<?php foreach ($pending as $p): ?>
<?php
$items = get_recurring_template_items($p['id']);
$total = 0;
foreach ($items as $item) {
$total += $item['quantity'] * $item['unit_price'];
}
?>
<tr>
<td><input type="checkbox" name="template_ids[]" value="<?= $p['id'] ?>" checked></td>
<td>
<?= htmlspecialchars($p['template_name']) ?>
<br><small>ca. <?= number_format($total, 2, ',', '.') ?> € netto</small>
</td>
<td>
<?= htmlspecialchars($p['customer_name']) ?>
<?php if ($p['customer_number']): ?>
<br><small><?= htmlspecialchars($p['customer_number']) ?></small>
<?php endif; ?>
</td>
<td>
<span class="interval-badge interval-<?= $p['interval_type'] ?>">
<?= get_interval_label($p['interval_type']) ?>
</span>
</td>
<td><?= date('d.m.Y', strtotime($p['next_due_date'])) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p style="margin-top:15px">
<button type="submit">Ausgewählte Rechnungen generieren</button>
<a href="<?= url_for('recurring.php') ?>" class="button-secondary">Abbrechen</a>
</p>
</form>
<?php endif; ?>
</div>
</section>
</main>
<script src="assets/command-palette.js"></script>
</body>
</html>