Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| UserVouchers | |
100.00% |
33 / 33 |
|
100.00% |
1 / 1 |
9 | |
100.00% |
1 / 1 |
| eligibleVoucherCount | |
100.00% |
33 / 33 |
|
100.00% |
1 / 1 |
9 | |||
| 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 | |
| 20 | $shiftEntries = $user->shiftEntries() |
| 21 | ->join('shifts', 'shift_entries.shift_id', '=', 'shifts.id') |
| 22 | ->where('shifts.end', '<', Carbon::now()) |
| 23 | ->where('shifts.start', '>=', $start ?: 0) |
| 24 | ->whereNull('freeloaded_by') |
| 25 | ->get() |
| 26 | ->load('shift'); |
| 27 | $worklogs = $user->worklogs() |
| 28 | ->where('worked_at', '>=', $start ?: 0) |
| 29 | ->where('worked_at', '<=', Carbon::now()) |
| 30 | ->get(); |
| 31 | $shiftsCount = |
| 32 | $shiftEntries->count() |
| 33 | + $worklogs->count(); |
| 34 | |
| 35 | $shiftsTime = 0; |
| 36 | foreach ($shiftEntries as $shiftEntry) { |
| 37 | $shiftsTime += $shiftEntry->shift->start->diffInHours($shiftEntry->shift->end); |
| 38 | } |
| 39 | foreach ($worklogs as $worklog) { |
| 40 | $shiftsTime += $worklog->hours; |
| 41 | } |
| 42 | |
| 43 | $vouchers = $voucherSettings['initial_vouchers']; |
| 44 | if ($voucherSettings['shifts_per_voucher']) { |
| 45 | $vouchers += $shiftsCount / $voucherSettings['shifts_per_voucher']; |
| 46 | } |
| 47 | if ($voucherSettings['hours_per_voucher']) { |
| 48 | $vouchers += $shiftsTime / $voucherSettings['hours_per_voucher']; |
| 49 | } |
| 50 | |
| 51 | $vouchers -= $user->state->got_voucher; |
| 52 | $vouchers = floor($vouchers); |
| 53 | if ($vouchers <= 0) { |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | return (int) $vouchers; |
| 58 | } |
| 59 | } |