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
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue