engelsystem/includes/model/UserWorkLog_model.php

96 lines
1.9 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-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;
}
/**
* 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');
/** @var Carbon $event */
$event = config('event_start');
2020-09-12 19:45:25 +02:00
$work_date = Carbon::today();
if (!empty($buildup) && (empty($event) || $event->lessThan(Carbon::now()))) {
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
}