2012-12-26 14:02:27 +01:00
|
|
|
<?php
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2018-09-25 17:33:31 +02:00
|
|
|
use Carbon\Carbon;
|
2021-11-30 23:29:51 +01:00
|
|
|
use Engelsystem\Database\Db;
|
2022-11-09 00:02:30 +01:00
|
|
|
use Engelsystem\Models\AngelType;
|
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;
|
2019-05-31 04:03:19 +02:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2020-09-12 19:45:25 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2014-09-28 14:50:08 +02:00
|
|
|
|
2014-05-13 15:51:45 +02:00
|
|
|
/**
|
|
|
|
* User model
|
|
|
|
*/
|
|
|
|
|
2017-12-27 12:24:55 +01:00
|
|
|
/**
|
|
|
|
* Returns the tshirt score (number of hours counted for tshirt).
|
|
|
|
* Accounts only ended shifts.
|
2018-01-14 18:09:34 +01:00
|
|
|
*
|
2018-10-09 21:47:31 +02:00
|
|
|
* @param int $userId
|
2018-01-14 18:09:34 +01:00
|
|
|
* @return int
|
2017-12-27 12:24:55 +01:00
|
|
|
*/
|
2018-10-09 21:47:31 +02:00
|
|
|
function User_tshirt_score($userId)
|
2018-01-14 18:09:34 +01:00
|
|
|
{
|
2018-09-17 12:33:15 +02:00
|
|
|
$shift_sum_formula = User_get_shifts_sum_query();
|
2022-06-16 22:50:52 +02:00
|
|
|
$result_shifts = Db::selectOne(sprintf('
|
2018-10-11 01:26:34 +02:00
|
|
|
SELECT ROUND((%s) / 3600, 2) AS `tshirt_score`
|
2023-01-18 13:02:11 +01:00
|
|
|
FROM `users` LEFT JOIN `shift_entries` ON `users`.`id` = `shift_entries`.`user_id`
|
|
|
|
LEFT JOIN `shifts` ON `shift_entries`.`shift_id` = `shifts`.`id`
|
2018-10-11 01:26:34 +02:00
|
|
|
WHERE `users`.`id` = ?
|
2023-01-03 22:19:03 +01:00
|
|
|
AND `shifts`.`end` < NOW()
|
2018-10-11 01:26:34 +02:00
|
|
|
GROUP BY `users`.`id`
|
|
|
|
', $shift_sum_formula), [
|
2023-02-05 18:03:00 +01:00
|
|
|
$userId,
|
2017-12-27 12:24:55 +01:00
|
|
|
]);
|
2018-08-29 23:10:05 +02:00
|
|
|
if (!isset($result_shifts['tshirt_score'])) {
|
|
|
|
$result_shifts = ['tshirt_score' => 0];
|
|
|
|
}
|
|
|
|
|
2020-09-12 19:45:25 +02:00
|
|
|
$worklogHours = Worklog::query()
|
|
|
|
->where('user_id', $userId)
|
|
|
|
->where('worked_at', '<=', Carbon::Now())
|
|
|
|
->sum('hours');
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2020-09-12 19:45:25 +02:00
|
|
|
return $result_shifts['tshirt_score'] + $worklogHours;
|
2017-12-27 12:24:55 +01:00
|
|
|
}
|
|
|
|
|
2014-05-13 15:51:45 +02:00
|
|
|
/**
|
|
|
|
* Returns all users that are not member of given angeltype.
|
|
|
|
*
|
2022-12-03 00:57:04 +01:00
|
|
|
* @param AngelType $angeltype Angeltype
|
2020-09-12 19:45:25 +02:00
|
|
|
*
|
|
|
|
* @return User[]|Collection
|
2014-05-13 15:51:45 +02:00
|
|
|
*/
|
2022-12-03 00:57:04 +01:00
|
|
|
function Users_by_angeltype_inverted(AngelType $angeltype)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2018-10-11 01:26:34 +02:00
|
|
|
return User::query()
|
2018-10-17 01:30:10 +02:00
|
|
|
->select('users.*')
|
2022-12-03 00:57:04 +01:00
|
|
|
->leftJoin('user_angel_type', function ($query) use ($angeltype) {
|
2018-10-11 01:26:34 +02:00
|
|
|
/** @var JoinClause $query */
|
|
|
|
$query
|
2022-12-03 00:57:04 +01:00
|
|
|
->on('users.id', '=', 'user_angel_type.user_id')
|
|
|
|
->where('user_angel_type.angel_type_id', '=', $angeltype->id);
|
2018-10-11 01:26:34 +02:00
|
|
|
})
|
2022-12-03 00:57:04 +01:00
|
|
|
->whereNull('user_angel_type.id')
|
2018-12-27 14:16:09 +01:00
|
|
|
->orderBy('users.name')
|
2018-10-17 01:30:10 +02:00
|
|
|
->get();
|
2013-12-29 15:08:21 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param User $user
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return float
|
|
|
|
*/
|
2018-10-09 21:47:31 +02:00
|
|
|
function User_get_eligable_voucher_count($user)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-01-21 23:07:20 +01:00
|
|
|
$voucher_settings = config('voucher_settings');
|
2019-12-25 16:26:59 +01:00
|
|
|
$start = $voucher_settings['voucher_start']
|
|
|
|
? Carbon::createFromFormat('Y-m-d', $voucher_settings['voucher_start'])->setTime(0, 0)
|
|
|
|
: null;
|
2019-12-27 05:24:32 +01:00
|
|
|
|
2023-01-18 13:02:11 +01:00
|
|
|
$shiftEntries = ShiftEntries_finished_by_user($user, $start);
|
2023-12-23 00:38:44 +01:00
|
|
|
$worklog = $user->worklogs()
|
|
|
|
->whereDate('worked_at', '>=', $start ?: 0)
|
|
|
|
->with(['user', 'creator'])
|
|
|
|
->get();
|
2019-12-27 05:24:32 +01:00
|
|
|
$shifts_done =
|
2023-01-18 13:02:11 +01:00
|
|
|
count($shiftEntries)
|
2020-09-12 19:45:25 +02:00
|
|
|
+ $worklog->count();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2019-12-27 06:07:48 +01:00
|
|
|
$shiftsTime = 0;
|
2023-01-18 13:02:11 +01:00
|
|
|
foreach ($shiftEntries as $shiftEntry) {
|
|
|
|
$shiftsTime += ($shiftEntry->shift->end->timestamp - $shiftEntry->shift->start->timestamp) / 60 / 60;
|
2019-12-27 06:07:48 +01:00
|
|
|
}
|
2022-10-18 19:15:22 +02:00
|
|
|
foreach ($worklog as $entry) {
|
2020-09-12 19:45:25 +02:00
|
|
|
$shiftsTime += $entry->hours;
|
2019-12-27 06:07:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$vouchers = $voucher_settings['initial_vouchers'];
|
2022-10-18 19:15:22 +02:00
|
|
|
if ($voucher_settings['shifts_per_voucher']) {
|
2022-11-09 00:02:30 +01:00
|
|
|
$vouchers += $shifts_done / $voucher_settings['shifts_per_voucher'];
|
2019-12-27 06:07:48 +01:00
|
|
|
}
|
2022-10-18 19:15:22 +02:00
|
|
|
if ($voucher_settings['hours_per_voucher']) {
|
2022-11-09 00:02:30 +01:00
|
|
|
$vouchers += $shiftsTime / $voucher_settings['hours_per_voucher'];
|
2019-12-27 06:07:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$vouchers -= $user->state->got_voucher;
|
2020-10-18 00:52:27 +02:00
|
|
|
$vouchers = floor($vouchers);
|
2019-12-27 06:07:48 +01:00
|
|
|
if ($vouchers < 0) {
|
2017-01-02 03:57:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2019-12-27 06:07:48 +01:00
|
|
|
return $vouchers;
|
2016-02-05 22:57:57 +01:00
|
|
|
}
|
2018-09-17 12:33:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the query to sum night shifts
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function User_get_shifts_sum_query()
|
|
|
|
{
|
|
|
|
$nightShifts = config('night_shifts');
|
|
|
|
if (!$nightShifts['enabled']) {
|
2023-01-03 22:19:03 +01:00
|
|
|
return 'COALESCE(SUM(UNIX_TIMESTAMP(shifts.end) - UNIX_TIMESTAMP(shifts.start)), 0)';
|
2018-09-17 12:33:15 +02:00
|
|
|
}
|
|
|
|
|
2024-02-18 02:18:31 +01:00
|
|
|
/* @see \Engelsystem\Helpers\Shifts::isNightShift to keep it in sync */
|
2022-10-18 19:15:22 +02:00
|
|
|
return sprintf(
|
|
|
|
'
|
2018-12-27 19:53:48 +01:00
|
|
|
COALESCE(SUM(
|
2020-05-13 18:26:32 +02:00
|
|
|
(1 + (
|
2024-04-06 16:14:30 +02:00
|
|
|
/* Starts during night */
|
2024-02-18 12:53:53 +01:00
|
|
|
HOUR(shifts.start) >= %1$d AND HOUR(shifts.start) < %2$d
|
2024-04-06 16:14:30 +02:00
|
|
|
/* Ends during night */
|
2024-02-18 12:53:53 +01:00
|
|
|
OR (
|
|
|
|
HOUR(shifts.end) > %1$d
|
|
|
|
|| HOUR(shifts.end) = %1$d AND MINUTE(shifts.end) > 0
|
|
|
|
) AND HOUR(shifts.end) <= %2$d
|
2024-04-06 16:14:30 +02:00
|
|
|
/* Starts before and ends after night */
|
2024-02-18 02:18:31 +01:00
|
|
|
OR HOUR(shifts.start) <= %1$d AND HOUR(shifts.end) >= %2$d
|
2020-05-13 18:26:32 +02:00
|
|
|
))
|
2023-01-03 22:19:03 +01:00
|
|
|
* (UNIX_TIMESTAMP(shifts.end) - UNIX_TIMESTAMP(shifts.start))
|
2023-01-18 13:02:11 +01:00
|
|
|
* (1 - (%3$d + 1) * `shift_entries`.`freeloaded`)
|
2018-12-27 19:53:48 +01:00
|
|
|
), 0)
|
2018-09-17 12:33:15 +02:00
|
|
|
',
|
|
|
|
$nightShifts['start'],
|
|
|
|
$nightShifts['end'],
|
|
|
|
$nightShifts['multiplier']
|
|
|
|
);
|
|
|
|
}
|