Mitarbeiter ohne Buchungen löschbar machen
This commit is contained in:
parent
e3d328f55a
commit
672df3d0a7
7 changed files with 58 additions and 4 deletions
|
|
@ -251,6 +251,29 @@ function admin_employees(): void
|
|||
require_post();
|
||||
$action = post_string('action');
|
||||
$id = (int) post_string('id');
|
||||
|
||||
if ($action === 'delete') {
|
||||
$employee = employee_find($id);
|
||||
if (!$employee) {
|
||||
abort_page(404, 'Mitarbeiter nicht gefunden.');
|
||||
}
|
||||
$stmt = db()->prepare('SELECT COUNT(*) FROM bookings WHERE employee_id = ?');
|
||||
$stmt->execute([$id]);
|
||||
if ((int) $stmt->fetchColumn() > 0) {
|
||||
flash('error', 'Mitarbeiter ' . $employee['name'] . ' kann nicht gelöscht werden, weil Buchungen darauf verweisen. Der Mitarbeiter kann stattdessen deaktiviert werden.');
|
||||
redirect('/admin/employees');
|
||||
}
|
||||
if ((int) $employee['active'] === 1
|
||||
&& (int) db()->query('SELECT COUNT(*) FROM employees WHERE active = 1')->fetchColumn() <= 1
|
||||
) {
|
||||
flash('error', 'Mindestens ein aktiver Mitarbeiter muss bleiben, damit Buchungen zugeordnet werden können.');
|
||||
redirect('/admin/employees');
|
||||
}
|
||||
db()->prepare('DELETE FROM employees WHERE id = ?')->execute([$id]);
|
||||
flash('success', 'Mitarbeiter ' . $employee['name'] . ' wurde gelöscht.');
|
||||
redirect('/admin/employees');
|
||||
}
|
||||
|
||||
$values = [
|
||||
'name' => post_string('name'),
|
||||
'sort_order' => (int) post_string('sort_order'),
|
||||
|
|
@ -280,6 +303,8 @@ function admin_employees(): void
|
|||
$stmt->execute([$id, today_iso()]);
|
||||
if ((int) $stmt->fetchColumn() > 0) {
|
||||
$errors['active'] = 'Der Mitarbeiter hat noch aktive Buchungen und kann nicht deaktiviert werden. Bitte zuerst umbuchen.';
|
||||
} elseif ((int) db()->query('SELECT COUNT(*) FROM employees WHERE active = 1')->fetchColumn() <= 1) {
|
||||
$errors['active'] = 'Mindestens ein aktiver Mitarbeiter muss bleiben, damit Buchungen zugeordnet werden können.';
|
||||
}
|
||||
}
|
||||
if (!$errors) {
|
||||
|
|
@ -315,7 +340,8 @@ function admin_employees(): void
|
|||
|
||||
$rows = db()->query(
|
||||
'SELECT e.*, u.first_name, u.last_name,
|
||||
(SELECT COUNT(*) FROM bookings b WHERE b.employee_id = e.id AND b.status = "active" AND b.end_date >= CURDATE()) AS future_bookings
|
||||
(SELECT COUNT(*) FROM bookings b WHERE b.employee_id = e.id AND b.status = "active" AND b.end_date >= CURDATE()) AS future_bookings,
|
||||
(SELECT COUNT(*) FROM bookings b WHERE b.employee_id = e.id) AS booking_count
|
||||
FROM employees e LEFT JOIN users u ON u.employee_id = e.id
|
||||
ORDER BY e.sort_order, e.name'
|
||||
)->fetchAll();
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ function url(string $path = '/'): string
|
|||
}
|
||||
|
||||
/** Cache-Buster: bei Änderungen an app.css/app.js erhöhen. */
|
||||
const DIC_ASSET_VERSION = '7';
|
||||
const DIC_ASSET_VERSION = '8';
|
||||
|
||||
function asset(string $path): string
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue