Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| UserVouchers | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
10 | |
100.00% |
1 / 1 |
| eligibleVoucherCount | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
10 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Helpers; |
| 6 | |
| 7 | use Carbon\Carbon; |
| 8 | use Engelsystem\Models\User\User; |
| 9 | |
| 10 | class UserVouchers |
| 11 | { |
| 12 | public static function eligibleVoucherCount(User $user): int |
| 13 | { |
| 14 | if (!config('enable_voucher')) { |
| 15 | return 0; |
| 16 | } |
| 17 | $voucherSettings = config('voucher_settings'); |
| 18 | $start = $voucherSettings['voucher_start'] |
| 19 | ? Carbon::createFromFormat('Y-m-d', $voucherSettings['voucher_start'])->setTime(0, 0) |
| 20 | : null; |
| 21 | |
| 22 | $shiftEntries = $user->shiftEntries() |
| 23 | ->join('shifts', 'shift_entries.shift_id', '=', 'shifts.id') |
| 24 | ->where('shifts.end', '<', Carbon::now()) |
| 25 | ->where('shifts.start', '>=', $start ?: 0) |
| 26 | ->whereNull('freeloaded_by') |
| 27 | ->get() |
| 28 | ->load('shift'); |
| 29 | $worklogs = $user->worklogs() |
| 30 | ->where('worked_at', '>=', $start ?: 0) |
| 31 | ->where('worked_at', '<=', Carbon::now()) |
| 32 | ->get(); |
| 33 | $shiftsCount = |
| 34 | $shiftEntries->count() |
| 35 | + $worklogs->count(); |
| 36 | |
| 37 | $shiftsTime = 0; |
| 38 | foreach ($shiftEntries as $shiftEntry) { |
| 39 | $shiftsTime += $shiftEntry->shift->start->diffInHours($shiftEntry->shift->end); |
| 40 | } |
| 41 | foreach ($worklogs as $worklog) { |
| 42 | $shiftsTime += $worklog->hours; |
| 43 | } |
| 44 | |
| 45 | $vouchers = $voucherSettings['initial_vouchers']; |
| 46 | if ($voucherSettings['shifts_per_voucher']) { |
| 47 | $vouchers += $shiftsCount / $voucherSettings['shifts_per_voucher']; |
| 48 | } |
| 49 | if ($voucherSettings['hours_per_voucher']) { |
| 50 | $vouchers += $shiftsTime / $voucherSettings['hours_per_voucher']; |
| 51 | } |
| 52 | |
| 53 | $vouchers -= $user->state->got_voucher; |
| 54 | $vouchers = floor($vouchers); |
| 55 | if ($vouchers <= 0) { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | return (int) $vouchers; |
| 60 | } |
| 61 | } |