Abwesenheiten: gemeinsamer Team-Kalender mit Sperre fuer Buchungen inkl. Vorlaufzeit
This commit is contained in:
parent
9d2154ee2d
commit
e83d6dc606
13 changed files with 533 additions and 2 deletions
136
app/data.php
136
app/data.php
|
|
@ -293,9 +293,145 @@ function booking_validate(array $data, int $excludeId = 0, int $allowTrailerId =
|
|||
}
|
||||
}
|
||||
|
||||
if (!$errors && !isset($errors['start_date']) && !isset($errors['end_date'])) {
|
||||
// Abwesenheiten: Übergabe-Mitarbeiter muss ab dem Erinnerungs-Vorlauf
|
||||
// verfügbar sein, der Rücknahme-Mitarbeiter bis zum Mietende.
|
||||
$handover = booking_absence_conflict((int) $data['employee_id'], $data['start_date'], $data['start_date']);
|
||||
if ($handover) {
|
||||
$errors['employee_id'] = absence_conflict_message($handover, 'Übergabe');
|
||||
}
|
||||
$returnId = (int) ($data['return_employee_id'] ?? 0);
|
||||
if ($returnId > 0 && $returnId !== (int) $data['employee_id']) {
|
||||
$return = booking_absence_conflict($returnId, $data['end_date'], $data['end_date']);
|
||||
if ($return) {
|
||||
$errors['return_employee_id'] = absence_conflict_message($return, 'Rücknahme');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------- Abwesenheiten */
|
||||
|
||||
/** Vorlauf in Tagen, in dem ein Mitarbeiter vor Mietbeginn verfügbar sein muss. */
|
||||
const BOOKING_LEAD_DAYS = 8;
|
||||
|
||||
/** @return array<int,array<string,mixed>> Abwesenheiten im Zeitraum, optional je Mitarbeiter. */
|
||||
function absence_list(string $from, string $to, int $employeeId = 0): array
|
||||
{
|
||||
if ($from > $to) {
|
||||
[$from, $to] = [$to, $from];
|
||||
}
|
||||
$sql = 'SELECT a.*, e.name AS employee_name, e.sort_order AS employee_sort, e.active AS employee_active
|
||||
FROM absences a JOIN employees e ON e.id = a.employee_id
|
||||
WHERE a.start_date <= ? AND a.end_date >= ?';
|
||||
$params = [$to, $from];
|
||||
if ($employeeId > 0) {
|
||||
$sql .= ' AND a.employee_id = ?';
|
||||
$params[] = $employeeId;
|
||||
}
|
||||
$sql .= ' ORDER BY a.start_date, e.sort_order, a.id';
|
||||
$stmt = db()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
function absence_find(int $id): ?array
|
||||
{
|
||||
$stmt = db()->prepare('SELECT a.*, e.name AS employee_name FROM absences a JOIN employees e ON e.id = a.employee_id WHERE a.id = ? LIMIT 1');
|
||||
$stmt->execute([$id]);
|
||||
$row = $stmt->fetch();
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
/** Abwesenheit, die den Mitarbeiter im Zeitraum (inkl. Vorlauf) betrifft. */
|
||||
function booking_absence_conflict(int $employeeId, string $startDate, string $endDate): ?array
|
||||
{
|
||||
if ($employeeId <= 0) {
|
||||
return null;
|
||||
}
|
||||
$from = add_days($startDate, -BOOKING_LEAD_DAYS);
|
||||
$to = $endDate;
|
||||
$stmt = db()->prepare("SELECT a.*, e.name AS employee_name FROM absences a JOIN employees e ON e.id = a.employee_id
|
||||
WHERE a.employee_id = ? AND a.start_date <= ? AND a.end_date >= ?
|
||||
ORDER BY a.start_date LIMIT 1");
|
||||
$stmt->execute([$employeeId, $to, $from]);
|
||||
$row = $stmt->fetch();
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
function absence_conflict_message(array $absence, string $task): string
|
||||
{
|
||||
return sprintf(
|
||||
'%s ist vom %s bis %s abwesend – die Buchung ist für die %s (inkl. %d Tagen Vorlauf) nicht möglich.',
|
||||
(string) $absence['employee_name'],
|
||||
fmt_date((string) $absence['start_date']),
|
||||
fmt_date((string) $absence['end_date']),
|
||||
$task,
|
||||
BOOKING_LEAD_DAYS
|
||||
);
|
||||
}
|
||||
|
||||
function absence_validate(array $data): array
|
||||
{
|
||||
$errors = [];
|
||||
if ((int) $data['employee_id'] <= 0 || !employee_find((int) $data['employee_id'])) {
|
||||
$errors['employee_id'] = 'Bitte einen Mitarbeiter auswählen.';
|
||||
}
|
||||
if (!valid_date((string) $data['start_date'])) {
|
||||
$errors['start_date'] = 'Bitte ein gültiges Startdatum angeben.';
|
||||
}
|
||||
if (!valid_date((string) $data['end_date'])) {
|
||||
$errors['end_date'] = 'Bitte ein gültiges Enddatum angeben.';
|
||||
}
|
||||
if (!isset($errors['start_date']) && !isset($errors['end_date']) && $data['end_date'] < $data['start_date']) {
|
||||
$errors['end_date'] = 'Das Ende darf nicht vor dem Start liegen.';
|
||||
}
|
||||
if (mb_strlen((string) $data['note']) > 160) {
|
||||
$errors['note'] = 'Der Hinweis ist zu lang (max. 160 Zeichen).';
|
||||
}
|
||||
if (!$errors) {
|
||||
$stmt = db()->prepare('SELECT start_date, end_date FROM absences WHERE employee_id = ? AND start_date <= ? AND end_date >= ? AND id <> ?');
|
||||
$stmt->execute([(int) $data['employee_id'], (string) $data['end_date'], (string) $data['start_date'], (int) ($data['id'] ?? 0)]);
|
||||
$overlap = $stmt->fetch();
|
||||
if ($overlap) {
|
||||
$errors['start_date'] = sprintf(
|
||||
'Für diesen Mitarbeiter ist bereits eine Abwesenheit vom %s bis %s eingetragen.',
|
||||
fmt_date((string) $overlap['start_date']),
|
||||
fmt_date((string) $overlap['end_date'])
|
||||
);
|
||||
}
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
function absence_save(?int $id, array $data, int $userId): int
|
||||
{
|
||||
if ($id === null) {
|
||||
$stmt = db()->prepare('INSERT INTO absences (employee_id, start_date, end_date, note, created_by, updated_by, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
$stmt->execute([
|
||||
(int) $data['employee_id'], (string) $data['start_date'], (string) $data['end_date'],
|
||||
(string) $data['note'], $userId, $userId, utc_now(), utc_now(),
|
||||
]);
|
||||
return (int) db()->lastInsertId();
|
||||
}
|
||||
$stmt = db()->prepare('UPDATE absences SET employee_id = ?, start_date = ?, end_date = ?, note = ?, updated_by = ?, updated_at = ? WHERE id = ?');
|
||||
$stmt->execute([
|
||||
(int) $data['employee_id'], (string) $data['start_date'], (string) $data['end_date'],
|
||||
(string) $data['note'], $userId, utc_now(), $id,
|
||||
]);
|
||||
return $id;
|
||||
}
|
||||
|
||||
function absence_delete(int $id): bool
|
||||
{
|
||||
$stmt = db()->prepare('DELETE FROM absences WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
/** Serialisiert Änderungen pro Anhänger, damit parallele Buchungen nicht kollidieren. */
|
||||
function booking_lock_trailers(array $trailerIds): void
|
||||
{
|
||||
|
|
|
|||
140
app/pages.php
140
app/pages.php
|
|
@ -91,6 +91,14 @@ function page_handle(string $method, string $path): void
|
|||
page_availability($user);
|
||||
return;
|
||||
}
|
||||
if ($path === '/absences') {
|
||||
page_absences($user, $method);
|
||||
return;
|
||||
}
|
||||
if (preg_match('#^/absences/(\d+)/delete$#', $path, $m) === 1) {
|
||||
page_absence_delete($user, (int) $m[1]);
|
||||
return;
|
||||
}
|
||||
if ($path === '/bookings/new') {
|
||||
page_booking_new($user, $method);
|
||||
return;
|
||||
|
|
@ -518,6 +526,7 @@ function page_calendar(array $user): void
|
|||
}
|
||||
|
||||
$employees = employees_all();
|
||||
$absences = absence_list($from, $to);
|
||||
render_page('calendar', [
|
||||
'view' => $view,
|
||||
'date' => $date,
|
||||
|
|
@ -534,6 +543,7 @@ function page_calendar(array $user): void
|
|||
'showCancelled' => $showCancelled,
|
||||
'employees' => $employees,
|
||||
'bookingCount' => count($bookings),
|
||||
'absences' => $absences,
|
||||
'urlFor' => static function (string $d) use ($view, $employeeId, $showCancelled): string {
|
||||
return calendar_url($d, $view, $employeeId, $showCancelled);
|
||||
},
|
||||
|
|
@ -986,6 +996,136 @@ function page_reminders(array $user): void
|
|||
], 'Erinnerungen');
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------- Abwesenheiten */
|
||||
|
||||
function page_absences(array $user, string $method): void
|
||||
{
|
||||
$isAdmin = ($user['role'] ?? '') === 'admin';
|
||||
$ownEmployeeId = auth_employee_id($user);
|
||||
$employees = employees_all(false);
|
||||
|
||||
$period = query_string('period', 'weeks');
|
||||
if (!in_array($period, ['weeks', 'month'], true)) {
|
||||
$period = 'weeks';
|
||||
}
|
||||
$today = today_iso();
|
||||
$date = query_string('date');
|
||||
if (!valid_date($date)) {
|
||||
$date = query_string('from') !== '' && valid_date(query_string('from')) ? query_string('from') : $today;
|
||||
}
|
||||
$from = $period === 'month' ? month_start($date) : week_start($date);
|
||||
$to = $period === 'month' ? month_end($date) : add_days($from, CALENDAR_WEEKS * 7 - 1);
|
||||
|
||||
$editId = query_int('edit');
|
||||
$editing = $editId > 0 ? absence_find($editId) : null;
|
||||
if ($editing && !$isAdmin && (int) $editing['employee_id'] !== $ownEmployeeId) {
|
||||
abort_page(403, 'Diese Abwesenheit gehört zu einem anderen Mitarbeiter.');
|
||||
}
|
||||
|
||||
$values = [
|
||||
'id' => $editing ? (int) $editing['id'] : 0,
|
||||
'employee_id' => $editing ? (int) $editing['employee_id'] : ($isAdmin ? 0 : $ownEmployeeId),
|
||||
'start_date' => $editing ? (string) $editing['start_date'] : $from,
|
||||
'end_date' => $editing ? (string) $editing['end_date'] : $from,
|
||||
'note' => $editing ? (string) $editing['note'] : '',
|
||||
];
|
||||
$errors = [];
|
||||
|
||||
if ($method === 'POST') {
|
||||
require_post();
|
||||
$values = [
|
||||
'id' => (int) post_string('id'),
|
||||
'employee_id' => $isAdmin ? (int) post_string('employee_id') : $ownEmployeeId,
|
||||
'start_date' => post_string('start_date'),
|
||||
'end_date' => post_string('end_date'),
|
||||
'note' => trim(post_string('note')),
|
||||
];
|
||||
$errors = absence_validate($values);
|
||||
if (!$isAdmin && $values['employee_id'] !== $ownEmployeeId) {
|
||||
abort_page(403, 'Abwesenheiten anderer Mitarbeiter dürfen nur Admins pflegen.');
|
||||
}
|
||||
if (!$errors) {
|
||||
$id = absence_save($values['id'] > 0 ? $values['id'] : null, $values, (int) $user['id']);
|
||||
$affected = $values['id'] > 0 ? booking_absence_conflict($values['employee_id'], $values['start_date'], $values['end_date']) : null;
|
||||
$message = 'Abwesenheit gespeichert.';
|
||||
if ($affected) {
|
||||
$message .= ' Achtung: Es gibt bereits Buchungen in diesem Zeitraum – bitte Mitarbeiter tauschen.';
|
||||
}
|
||||
flash('success', $message);
|
||||
redirect('/absences?date=' . rawurlencode((string) $values['start_date']) . '&period=' . $period . '#abwesenheit-' . $id);
|
||||
}
|
||||
http_response_code(422);
|
||||
}
|
||||
|
||||
$rows = absence_list($from, $to);
|
||||
$byEmployee = [];
|
||||
foreach ($employees as $employee) {
|
||||
$byEmployee[(int) $employee['id']] = [
|
||||
'employee' => $employee,
|
||||
'rows' => [],
|
||||
];
|
||||
}
|
||||
foreach ($rows as $row) {
|
||||
$byEmployee[(int) $row['employee_id']]['rows'][] = $row;
|
||||
}
|
||||
|
||||
// Buchungen im Zeitraum, die mit einer Abwesenheit kollidieren.
|
||||
$conflicts = [];
|
||||
foreach ($rows as $row) {
|
||||
$windowStart = add_days((string) $row['start_date'], -BOOKING_LEAD_DAYS);
|
||||
foreach (booking_list($windowStart, (string) $row['end_date']) as $booking) {
|
||||
if ($booking['status'] !== 'active') {
|
||||
continue;
|
||||
}
|
||||
if (booking_employee_for_kind($booking, 'start_1') === (int) $row['employee_id']
|
||||
|| booking_employee_for_kind($booking, 'end_1') === (int) $row['employee_id']) {
|
||||
$conflicts[(int) $row['id']][] = $booking;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$prevDate = $period === 'month'
|
||||
? date_obj($from)->modify('-1 month')->format('Y-m-d')
|
||||
: add_days($date, -CALENDAR_WEEKS * 7);
|
||||
$nextDate = $period === 'month'
|
||||
? date_obj($from)->modify('+1 month')->format('Y-m-d')
|
||||
: add_days($date, CALENDAR_WEEKS * 7);
|
||||
|
||||
render_page('absences', [
|
||||
'user' => $user,
|
||||
'isAdmin' => $isAdmin,
|
||||
'ownEmployeeId' => $ownEmployeeId,
|
||||
'employees' => $employees,
|
||||
'byEmployee' => $byEmployee,
|
||||
'conflicts' => $conflicts,
|
||||
'values' => $values,
|
||||
'errors' => $errors,
|
||||
'period' => $period,
|
||||
'date' => $date,
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
'prevDate' => $prevDate,
|
||||
'nextDate' => $nextDate,
|
||||
'today' => $today,
|
||||
], 'Abwesenheiten');
|
||||
}
|
||||
|
||||
function page_absence_delete(array $user, int $id): void
|
||||
{
|
||||
require_post();
|
||||
$absence = absence_find($id);
|
||||
if (!$absence) {
|
||||
abort_page(404, 'Abwesenheit nicht gefunden.');
|
||||
}
|
||||
$isAdmin = ($user['role'] ?? '') === 'admin';
|
||||
if (!$isAdmin && (int) $absence['employee_id'] !== auth_employee_id($user)) {
|
||||
abort_page(403, 'Abwesenheiten anderer Mitarbeiter dürfen nur Admins löschen.');
|
||||
}
|
||||
absence_delete($id);
|
||||
flash('success', 'Abwesenheit gelöscht.');
|
||||
redirect(safe_next(post_string('return_to'), '/absences'));
|
||||
}
|
||||
|
||||
function page_reminder_done(array $user): void
|
||||
{
|
||||
require_post();
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ function url(string $path = '/'): string
|
|||
}
|
||||
|
||||
/** Cache-Buster: bei Änderungen an app.css/app.js erhöhen. */
|
||||
const DIC_ASSET_VERSION = '23';
|
||||
const DIC_ASSET_VERSION = '24';
|
||||
|
||||
function asset(string $path): string
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue