diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/model/ShiftsFilter.php | 5 | ||||
-rw-r--r-- | includes/model/UserWorkLog_model.php | 20 | ||||
-rw-r--r-- | includes/model/User_model.php | 30 |
3 files changed, 45 insertions, 10 deletions
diff --git a/includes/model/ShiftsFilter.php b/includes/model/ShiftsFilter.php index fe3bfa56..c3811977 100644 --- a/includes/model/ShiftsFilter.php +++ b/includes/model/ShiftsFilter.php @@ -119,6 +119,11 @@ class ShiftsFilter */ public function setEndTime($endTime) { + $filterMaxDuration = config('filter_max_duration') * 60 * 60; + if ($filterMaxDuration && ($endTime - $this->startTime > $filterMaxDuration)) { + $endTime = $this->startTime + $filterMaxDuration; + } + $this->endTime = $endTime; } diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php index cc7686cf..e785067d 100644 --- a/includes/model/UserWorkLog_model.php +++ b/includes/model/UserWorkLog_model.php @@ -22,14 +22,24 @@ function UserWorkLog($user_worklog_id) /** * Returns all work log entries for a user. * - * @param int $userId + * @param int $userId + * @param Carbon|null $sinceTime * @return array[] */ -function UserWorkLogsForUser($userId) +function UserWorkLogsForUser($userId, Carbon $sinceTime = null) { - return Db::select("SELECT * FROM `UserWorkLog` WHERE `user_id`=? ORDER BY `created_timestamp`", [ - $userId - ]); + return Db::select( + ' + SELECT * + FROM `UserWorkLog` + WHERE `user_id`=? + ' . ($sinceTime ? 'AND work_timestamp >= ' . $sinceTime->getTimestamp() : '') . ' + ORDER BY `created_timestamp` + ', + [ + $userId + ] + ); } /** diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 1b1434bb..0858c0e2 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -236,15 +236,35 @@ function User_get_eligable_voucher_count($user) $start = $voucher_settings['voucher_start'] ? Carbon::createFromFormat('Y-m-d', $voucher_settings['voucher_start'])->setTime(0, 0) : null; - $shifts_done = count(ShiftEntries_finished_by_user($user->id, $start)); - $earned_vouchers = $user->state->got_voucher - $voucher_settings['initial_vouchers']; - $eligable_vouchers = $shifts_done / $voucher_settings['shifts_per_voucher'] - $earned_vouchers; - if ($eligable_vouchers < 0) { + $shifts = ShiftEntries_finished_by_user($user->id, $start); + $worklog = UserWorkLogsForUser($user->id, $start); + $shifts_done = + count($shifts) + + count($worklog); + + $shiftsTime = 0; + foreach ($shifts as $shift){ + $shiftsTime += ($shift['end'] - $shift['start']) / 60 / 60; + } + foreach ($worklog as $entry){ + $shiftsTime += $entry['work_hours']; + } + + $vouchers = $voucher_settings['initial_vouchers']; + if($voucher_settings['shifts_per_voucher']){ + $vouchers += $shifts_done / $voucher_settings['shifts_per_voucher']; + } + if($voucher_settings['hours_per_voucher']){ + $vouchers += $shiftsTime / $voucher_settings['hours_per_voucher']; + } + + $vouchers -= $user->state->got_voucher; + if ($vouchers < 0) { return 0; } - return $eligable_vouchers; + return $vouchers; } /** |