You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
engelsystem/includes/model/UserWorkLog_model.php

146 lines
3.5 KiB
PHTML

<?php
7 years ago
use Carbon\Carbon;
4 years ago
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User;
use Engelsystem\Models\Worklog;
use Illuminate\Support\Collection;
/**
* Returns all work log entries for a user.
7 years ago
*
* @param int $userId
* @param Carbon|null $sinceTime
*
* @return Worklog[]|Collection
*/
function UserWorkLogsForUser($userId, Carbon $sinceTime = null)
{
$worklogs = Worklog::whereUserId($userId);
if ($sinceTime) {
$worklogs = $worklogs->whereDate('worked_at', '>=', $sinceTime);
}
return $worklogs->get();
}
/**
* Delete a work log entry.
7 years ago
*
* @param Worklog $userWorkLog
7 years ago
* @return int
*/
function UserWorkLog_delete(Worklog $userWorkLog)
{
$result = $userWorkLog->delete();
7 years ago
engelsystem_log(sprintf(
'Delete work log for %s, %s hours, %s',
User_Nick_render($userWorkLog->user, true),
$userWorkLog->hours,
$userWorkLog->comment
7 years ago
));
return $result;
}
/**
* Create a new work log entry
7 years ago
*
* @param Worklog $userWorkLog
7 years ago
* @return bool
*/
function UserWorkLog_create(Worklog $userWorkLog)
{
$user = auth()->user();
$userWorkLog->creator()->associate($user);
$result = $userWorkLog->save();
7 years ago
engelsystem_log(sprintf(
'Added work log entry for %s, %s hours, %s',
User_Nick_render($userWorkLog->user, true),
$userWorkLog->hours,
$userWorkLog->comment
));
7 years ago
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;
}
4 years ago
$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'];
}
$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);
}
}
/**
* New user work log entry
*
* @param int $userId
* @return Worklog
*/
function UserWorkLog_new($userId)
{
/** @var Carbon $buildup */
$buildup = config('buildup_start');
$work_date = Carbon::today();
if (!empty($buildup)) {
$work_date = $buildup;
}
$work_date
->setHour(0)
->setMinute(0)
->setSecond(0);
$worklog = new Worklog();
$worklog->user_id = $userId;
$worklog->worked_at = $work_date;
return $worklog;
}