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;
|
2017-12-29 17:19:27 +01:00
|
|
|
use Engelsystem\Database\Db;
|
2018-10-10 03:10:28 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2017-12-29 17:19:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a single work log entry.
|
2018-01-14 18:09:34 +01:00
|
|
|
*
|
|
|
|
* @param int $user_worklog_id
|
2018-01-14 23:07:34 +01:00
|
|
|
* @return array|null
|
2017-12-29 17:19:27 +01:00
|
|
|
*/
|
2017-12-29 18:57:11 +01:00
|
|
|
function UserWorkLog($user_worklog_id)
|
|
|
|
{
|
2018-01-14 23:07:34 +01:00
|
|
|
$workLog = Db::selectOne("SELECT * FROM `UserWorkLog` WHERE `id`=?", [
|
2017-12-29 18:57:11 +01:00
|
|
|
$user_worklog_id
|
|
|
|
]);
|
2018-01-14 23:07:34 +01:00
|
|
|
|
|
|
|
return empty($workLog) ? null : $workLog;
|
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
|
2018-01-14 18:09:34 +01:00
|
|
|
* @return array[]
|
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
|
|
|
{
|
2019-12-27 05:24:32 +01:00
|
|
|
return Db::select(
|
|
|
|
'
|
2020-05-13 18:26:32 +02:00
|
|
|
SELECT *
|
|
|
|
FROM `UserWorkLog`
|
|
|
|
WHERE `user_id`=?
|
|
|
|
' . ($sinceTime ? 'AND work_timestamp >= ' . $sinceTime->getTimestamp() : '') . '
|
|
|
|
ORDER BY `created_timestamp`
|
|
|
|
',
|
2019-12-27 05:24:32 +01:00
|
|
|
[
|
|
|
|
$userId
|
|
|
|
]
|
|
|
|
);
|
2017-12-29 17:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a work log entry.
|
2018-01-14 18:09:34 +01:00
|
|
|
*
|
|
|
|
* @param $userWorkLog
|
|
|
|
* @return int
|
2017-12-29 17:19:27 +01:00
|
|
|
*/
|
|
|
|
function UserWorkLog_delete($userWorkLog)
|
|
|
|
{
|
2018-10-10 03:10:28 +02:00
|
|
|
$user_source = User::find($userWorkLog['user_id']);
|
2017-12-29 17:19:27 +01:00
|
|
|
$result = Db::delete("DELETE FROM `UserWorkLog` WHERE `id`=?", [
|
|
|
|
$userWorkLog['id']
|
|
|
|
]);
|
2018-01-14 18:09:34 +01:00
|
|
|
|
|
|
|
engelsystem_log(sprintf(
|
|
|
|
'Delete work log for %s, %s hours, %s',
|
2019-05-31 04:03:19 +02:00
|
|
|
User_Nick_render($user_source, true),
|
2018-01-14 18:09:34 +01:00
|
|
|
$userWorkLog['work_hours'],
|
|
|
|
$userWorkLog['comment']
|
|
|
|
));
|
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update work log entry (only work hours and comment)
|
2018-01-14 18:09:34 +01:00
|
|
|
*
|
|
|
|
* @param $userWorkLog
|
|
|
|
* @return int
|
2017-12-29 17:19:27 +01:00
|
|
|
*/
|
|
|
|
function UserWorkLog_update($userWorkLog)
|
|
|
|
{
|
2018-10-10 03:10:28 +02:00
|
|
|
$user_source = User::find($userWorkLog['user_id']);
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
$result = Db::update("UPDATE `UserWorkLog` SET
|
2017-12-29 18:57:11 +01:00
|
|
|
`work_timestamp`=?,
|
2017-12-29 17:19:27 +01:00
|
|
|
`work_hours`=?,
|
|
|
|
`comment`=?
|
|
|
|
WHERE `id`=?", [
|
2017-12-29 18:57:11 +01:00
|
|
|
$userWorkLog['work_timestamp'],
|
2017-12-29 17:19:27 +01:00
|
|
|
$userWorkLog['work_hours'],
|
2017-12-29 18:57:11 +01:00
|
|
|
$userWorkLog['comment'],
|
|
|
|
$userWorkLog['id']
|
|
|
|
]);
|
2018-01-14 18:09:34 +01:00
|
|
|
|
|
|
|
engelsystem_log(sprintf(
|
|
|
|
'Updated work log for %s, %s hours, %s',
|
2019-05-31 04:03:19 +02:00
|
|
|
User_Nick_render($user_source, true),
|
2018-01-14 18:09:34 +01:00
|
|
|
$userWorkLog['work_hours'],
|
|
|
|
$userWorkLog['comment'])
|
|
|
|
);
|
|
|
|
|
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 $userWorkLog
|
|
|
|
* @return bool
|
2017-12-29 17:19:27 +01:00
|
|
|
*/
|
|
|
|
function UserWorkLog_create($userWorkLog)
|
|
|
|
{
|
2018-10-31 12:48:22 +01:00
|
|
|
$user = auth()->user();
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2018-10-10 03:10:28 +02:00
|
|
|
$user_source = User::find($userWorkLog['user_id']);
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
$result = Db::insert("INSERT INTO `UserWorkLog` (
|
|
|
|
`user_id`,
|
2017-12-29 18:57:11 +01:00
|
|
|
`work_timestamp`,
|
2017-12-29 17:19:27 +01:00
|
|
|
`work_hours`,
|
|
|
|
`comment`,
|
|
|
|
`created_user_id`,
|
|
|
|
`created_timestamp`
|
|
|
|
)
|
2017-12-29 18:57:11 +01:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?)", [
|
|
|
|
$userWorkLog['user_id'],
|
|
|
|
$userWorkLog['work_timestamp'],
|
2017-12-29 17:19:27 +01:00
|
|
|
$userWorkLog['work_hours'],
|
2017-12-29 18:57:11 +01:00
|
|
|
$userWorkLog['comment'],
|
2018-10-08 21:15:56 +02:00
|
|
|
$user->id,
|
2017-12-29 18:57:11 +01:00
|
|
|
time()
|
|
|
|
]);
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2019-05-31 04:03:19 +02:00
|
|
|
engelsystem_log(sprintf('Added work log entry for %s, %s hours, %s', User_Nick_render($user_source, true),
|
2018-01-14 18:09:34 +01:00
|
|
|
$userWorkLog['work_hours'], $userWorkLog['comment']));
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$room = Room($shift['RID']);
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
|
2019-09-07 00:41:48 +02:00
|
|
|
UserWorkLog_create([
|
|
|
|
'user_id' => $entry['UID'],
|
|
|
|
'work_timestamp' => $shift['start'],
|
2019-10-13 20:28:34 +02:00
|
|
|
'work_hours' => (($shift['end'] - $shift['start']) / 60 / 60) * $nightShiftMultiplier,
|
2019-09-07 00:41:48 +02:00
|
|
|
'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'))
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
/**
|
|
|
|
* New user work log entry
|
|
|
|
*
|
2018-10-09 21:47:31 +02:00
|
|
|
* @param int $userId
|
2018-01-14 18:09:34 +01:00
|
|
|
* @return array
|
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
|
|
|
{
|
2017-12-29 18:57:11 +01:00
|
|
|
$work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time()));
|
2018-09-25 17:33:31 +02:00
|
|
|
|
|
|
|
/** @var Carbon $buildup */
|
2018-10-28 12:59:49 +01:00
|
|
|
$buildup = config('buildup_start');
|
2018-09-25 17:33:31 +02:00
|
|
|
if (!empty($buildup)) {
|
|
|
|
$work_date = $buildup->format('Y-m-d H:i');
|
2017-12-29 18:57:11 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
return [
|
2018-10-09 21:47:31 +02:00
|
|
|
'user_id' => $userId,
|
2017-12-29 18:57:11 +01:00
|
|
|
'work_timestamp' => $work_date,
|
2018-01-14 18:09:34 +01:00
|
|
|
'work_hours' => 0,
|
|
|
|
'comment' => ''
|
2017-12-29 17:19:27 +01:00
|
|
|
];
|
|
|
|
}
|