2017-12-29 17:19:27 +01:00
|
|
|
<?php
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2018-09-25 17:33:31 +02:00
|
|
|
use Carbon\Carbon;
|
2020-09-06 23:50:36 +02:00
|
|
|
use Engelsystem\Models\Room;
|
2018-10-10 03:10:28 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
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
|
|
|
*
|
2019-12-27 05:24:32 +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
|
|
|
*/
|
2019-12-27 05:24:32 +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
|
|
|
*
|
2020-09-12 19:45:25 +02:00
|
|
|
* @param Worklog $userWorkLog
|
2018-01-14 18:09:34 +01:00
|
|
|
* @return int
|
2017-12-29 17:19:27 +01:00
|
|
|
*/
|
2020-09-12 19:45:25 +02:00
|
|
|
function UserWorkLog_delete(Worklog $userWorkLog)
|
2017-12-29 17:19:27 +01:00
|
|
|
{
|
2020-09-12 19:45:25 +02:00
|
|
|
$result = $userWorkLog->delete();
|
2018-01-14 18:09:34 +01:00
|
|
|
|
|
|
|
engelsystem_log(sprintf(
|
|
|
|
'Delete work log for %s, %s hours, %s',
|
2020-09-12 19:45:25 +02:00
|
|
|
User_Nick_render($userWorkLog->user, true),
|
|
|
|
$userWorkLog->hours,
|
|
|
|
$userWorkLog->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
|
|
|
*
|
2020-09-12 19:45:25 +02:00
|
|
|
* @param Worklog $userWorkLog
|
2018-01-14 18:09:34 +01:00
|
|
|
* @return bool
|
2017-12-29 17:19:27 +01:00
|
|
|
*/
|
2020-09-12 19:45:25 +02:00
|
|
|
function UserWorkLog_create(Worklog $userWorkLog)
|
2017-12-29 17:19:27 +01:00
|
|
|
{
|
2018-10-31 12:48:22 +01:00
|
|
|
$user = auth()->user();
|
2020-09-12 19:45:25 +02:00
|
|
|
$userWorkLog->creator()->associate($user);
|
|
|
|
$result = $userWorkLog->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($userWorkLog->user, true),
|
|
|
|
$userWorkLog->hours,
|
|
|
|
$userWorkLog->comment
|
|
|
|
));
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-09-07 00:41:48 +02:00
|
|
|
/**
|
|
|
|
* @param array|int $shift
|
|
|
|
*/
|
|
|
|
function UserWorkLog_from_shift($shift)
|
|
|
|
{
|
|
|
|
$shift = is_array($shift) ? $shift : Shift($shift);
|
2019-10-13 20:28:34 +02:00
|
|
|
$nightShifts = config('night_shifts');
|
2019-09-07 00:41:48 +02:00
|
|
|
|
|
|
|
if ($shift['start'] > time()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-06 23:50:36 +02:00
|
|
|
$room = Room::find($shift['RID']);
|
2019-09-07 00:41:48 +02:00
|
|
|
foreach ($shift['ShiftEntry'] as $entry) {
|
|
|
|
if ($entry['freeloaded']) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-13 20:28:34 +02:00
|
|
|
|
2019-09-07 00:41:48 +02:00
|
|
|
$type = AngelType($entry['TID']);
|
|
|
|
|
2019-10-13 20:28:34 +02:00
|
|
|
$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);
|
2019-09-07 00:41:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-09-25 17:33:31 +02:00
|
|
|
/** @var Carbon $buildup */
|
2018-10-28 12:59:49 +01:00
|
|
|
$buildup = config('buildup_start');
|
2020-09-12 19:45:25 +02:00
|
|
|
|
|
|
|
$work_date = Carbon::today();
|
2018-09-25 17:33:31 +02:00
|
|
|
if (!empty($buildup)) {
|
2020-09-12 19:45:25 +02:00
|
|
|
$work_date = $buildup;
|
2017-12-29 18:57:11 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02: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
|
|
|
}
|