engelsystem/includes/model/UserWorkLog_model.php

145 lines
3.4 KiB
PHP
Raw Normal View History

2017-12-29 17:19:27 +01:00
<?php
2018-01-14 18:09:34 +01:00
use Carbon\Carbon;
2020-09-06 23:50:36 +02:00
use Engelsystem\Models\Room;
2020-09-12 19:45:25 +02:00
use Engelsystem\Models\Worklog;
use Illuminate\Support\Collection;
2017-12-29 17:19:27 +01:00
/**
* Returns all work log entries for a user.
2018-01-14 18:09:34 +01:00
*
* @param int $userId
* @param Carbon|null $sinceTime
2020-09-12 19:45:25 +02:00
*
* @return Worklog[]|Collection
2017-12-29 17:19:27 +01:00
*/
function UserWorkLogsForUser($userId, Carbon $sinceTime = null)
2017-12-29 18:57:11 +01:00
{
2020-09-12 19:45:25 +02:00
$worklogs = Worklog::whereUserId($userId);
if ($sinceTime) {
$worklogs = $worklogs->whereDate('worked_at', '>=', $sinceTime);
}
return $worklogs->get();
2017-12-29 17:19:27 +01:00
}
/**
* Delete a work log entry.
2018-01-14 18:09:34 +01:00
*
* @param Worklog $worklog
2018-01-14 18:09:34 +01:00
* @return int
2017-12-29 17:19:27 +01:00
*/
function UserWorkLog_delete(Worklog $worklog)
2017-12-29 17:19:27 +01:00
{
$result = $worklog->delete();
2018-01-14 18:09:34 +01:00
engelsystem_log(sprintf(
'Delete work log for %s, %s hours, %s',
User_Nick_render($worklog->user, true),
$worklog->hours,
$worklog->comment
2018-01-14 18:09:34 +01:00
));
2017-12-29 17:19:27 +01:00
return $result;
}
/**
* Create a new work log entry
2018-01-14 18:09:34 +01:00
*
* @param Worklog $worklog
2018-01-14 18:09:34 +01:00
* @return bool
2017-12-29 17:19:27 +01:00
*/
function UserWorkLog_create(Worklog $worklog)
2017-12-29 17:19:27 +01:00
{
2018-10-31 12:48:22 +01:00
$user = auth()->user();
$worklog->creator()->associate($user);
$result = $worklog->save();
2018-01-14 18:09:34 +01:00
2020-09-12 19:45:25 +02:00
engelsystem_log(sprintf(
'Added work log entry for %s, %s hours, %s',
User_Nick_render($worklog->user, true),
$worklog->hours,
$worklog->comment
2020-09-12 19:45:25 +02:00
));
2018-01-14 18:09:34 +01:00
2017-12-29 17:19:27 +01:00
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;
}
2020-09-06 23:50:36 +02:00
$room = Room::find($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'];
}
2020-09-12 19:45:25 +02:00
$worklog = UserWorkLog_new($entry['UID']);
$worklog->hours = (($shift['end'] - $shift['start']) / 60 / 60) * $nightShiftMultiplier;
$worklog->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'))
);
$worklog->worked_at = Carbon::createFromTimestamp($shift['start']);
UserWorkLog_create($worklog);
}
}
2017-12-29 17:19:27 +01:00
/**
* New user work log entry
*
2018-10-09 21:47:31 +02:00
* @param int $userId
2020-09-12 19:45:25 +02:00
* @return Worklog
2017-12-29 17:19:27 +01:00
*/
2018-10-09 21:47:31 +02:00
function UserWorkLog_new($userId)
2017-12-29 17:19:27 +01:00
{
/** @var Carbon $buildup */
$buildup = config('buildup_start');
2020-09-12 19:45:25 +02:00
$work_date = Carbon::today();
if (!empty($buildup)) {
2020-09-12 19:45:25 +02:00
$work_date = $buildup;
2017-12-29 18:57:11 +01:00
}
2020-09-12 19:45:25 +02:00
$work_date
->setHour(0)
->setMinute(0)
->setSecond(0);
$worklog = new Worklog();
$worklog->user_id = $userId;
$worklog->worked_at = $work_date;
return $worklog;
2017-12-29 17:19:27 +01:00
}