diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 726814cf..15f92a9d 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -221,6 +221,7 @@ function shift_delete_controller() // Schicht löschen bestätigt if ($request->hasPostData('delete')) { + UserWorkLog_from_shift($shift_id); Shift_delete($shift_id); engelsystem_log( diff --git a/includes/mailer/shifts_mailer.php b/includes/mailer/shifts_mailer.php index 37b26838..b496a007 100644 --- a/includes/mailer/shifts_mailer.php +++ b/includes/mailer/shifts_mailer.php @@ -94,7 +94,12 @@ function mail_shift_delete($shift) foreach ($users as $user) { $user = (new User())->forceFill($user); if ($user->settings->email_shiftinfo) { - engelsystem_email_to_user($user, __('Your Shift was deleted'), $message, true); + $userMessage = $message; + if ($shift['start'] < time() && !$user['freeloaded']) { + $userMessage .= "\n" . __('Since the deleted shift was already done, we added a worklog entry instead, to keep your work hours correct.') . "\n"; + } + + engelsystem_email_to_user($user, __('Your Shift was deleted'), $userMessage, true); } } } diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 6bb17cf2..01295fbc 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -80,12 +80,15 @@ function Shifts_from_frab() } /** - * @param array $room + * @param array|int $room * @return array[] */ function Shifts_by_room($room) { - return DB::select('SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`', [$room['RID']]); + return DB::select( + 'SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`', + [is_array($room) ? $room['RID'] : $room] + ); } /** @@ -508,8 +511,8 @@ function Shift_delete_by_psid($shift_psid) */ function Shift_delete($shift_id) { - DB::delete('DELETE FROM `Shifts` WHERE `SID`=?', [$shift_id]); mail_shift_delete(Shift($shift_id)); + DB::delete('DELETE FROM `Shifts` WHERE `SID`=?', [$shift_id]); } /** diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php index 7b766c5e..cc7686cf 100644 --- a/includes/model/UserWorkLog_model.php +++ b/includes/model/UserWorkLog_model.php @@ -121,6 +121,56 @@ function UserWorkLog_create($userWorkLog) return $result; } +/** + * @param array|int $shift + */ +function UserWorkLog_from_shift($shift) +{ + $shift = is_array($shift) ? $shift : Shift($shift); + $nightShifts = config('night_shifts'); + + if ($shift['start'] > time()) { + return; + } + + $room = Room($shift['RID']); + foreach ($shift['ShiftEntry'] as $entry) { + if ($entry['freeloaded']) { + continue; + } + + $type = AngelType($entry['TID']); + + $nightShiftMultiplier = 1; + $shiftStart = Carbon::createFromTimestamp($shift['start']); + $shiftEnd = Carbon::createFromTimestamp($shift['end']); + if ( + $nightShifts['enabled'] + && ( + $shiftStart->hour >= $nightShifts['start'] && $shiftStart->hour < $nightShifts['end'] + || $shiftEnd->hour >= $nightShifts['start'] && $shiftEnd->hour < $nightShifts['end'] + ) + ) { + $nightShiftMultiplier = $nightShifts['multiplier']; + } + + UserWorkLog_create([ + 'user_id' => $entry['UID'], + 'work_timestamp' => $shift['start'], + 'work_hours' => (($shift['end'] - $shift['start']) / 60 / 60) * $nightShiftMultiplier, + 'comment' => sprintf( + '%s (%s as %s) in %s, %s-%s', + $shift['name'], + $shift['title'], + $type['name'], + $room['Name'], + Carbon::createFromTimestamp($shift['start'])->format(__('m/d/Y h:i a')), + Carbon::createFromTimestamp($shift['end'])->format(__('m/d/Y h:i a')) + ), + ]); + } +} + /** * New user work log entry * diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index bc0eb651..c55eb227 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -179,9 +179,16 @@ function admin_rooms() ]); } elseif ($request->input('show') == 'delete') { if ($request->hasPostData('ack')) { + $shifts = Shifts_by_room($room_id); + foreach ($shifts as $shift) { + $shift = Shift($shift['SID']); + + UserWorkLog_from_shift($shift); + mail_shift_delete($shift); + } + Room_delete($room_id); - engelsystem_log('Room deleted: ' . $name); success(sprintf(__('Room %s deleted.'), $name)); redirect(page_link_to('admin_rooms')); } diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index d6dc38bb..8dba9b4b 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -674,6 +674,9 @@ msgstr "Eine deiner Schichten wurde gelöscht:" msgid "Your Shift was deleted" msgstr "Deine Schicht wurde gelöscht" +msgid "Since the deleted shift was already done, we added a worklog entry instead, to keep your work hours correct." +msgstr "Da die gelöschte Schicht bereits vergangen ist, haben wir einen entsprechenden Arbeitseinsatz hinzugefügt." + #: includes/mailer/shifts_mailer.php:114 msgid "You have been assigned to a Shift:" msgstr "Du wurdest in eine Schicht eingetragen:"