Voucher: Added calculation start time

main
Igor Scheller 5 years ago
parent 45d13ac998
commit 25cdf1cac8

@ -138,6 +138,8 @@ return [
'voucher_settings' => [ 'voucher_settings' => [
'initial_vouchers' => 0, 'initial_vouchers' => 0,
'shifts_per_voucher' => 1, 'shifts_per_voucher' => 1,
// 'Y-m-d' formatted
'voucher_start' => null,
], ],
// Available locales in /locale/ // Available locales in /locale/

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
@ -201,10 +202,11 @@ function ShiftEntries_upcoming_for_user($userId)
/** /**
* Returns shifts completed by the given user. * Returns shifts completed by the given user.
* *
* @param int $userId * @param int $userId
* @param Carbon|null $sinceTime
* @return array * @return array
*/ */
function ShiftEntries_finished_by_user($userId) function ShiftEntries_finished_by_user($userId, Carbon $sinceTime = null)
{ {
return DB::select(' return DB::select('
SELECT * SELECT *
@ -214,6 +216,7 @@ function ShiftEntries_finished_by_user($userId)
WHERE `ShiftEntry`.`UID` = ? WHERE `ShiftEntry`.`UID` = ?
AND `Shifts`.`end` < ? AND `Shifts`.`end` < ?
AND `ShiftEntry`.`freeloaded` = 0 AND `ShiftEntry`.`freeloaded` = 0
' . ($sinceTime ? 'AND Shifts.start >= ' . $sinceTime->getTimestamp() : '') . '
ORDER BY `Shifts`.`end` desc ORDER BY `Shifts`.`end` desc
', ',
[ [

@ -233,7 +233,10 @@ function User_reset_api_key($user, $log = true)
function User_get_eligable_voucher_count($user) function User_get_eligable_voucher_count($user)
{ {
$voucher_settings = config('voucher_settings'); $voucher_settings = config('voucher_settings');
$shifts_done = count(ShiftEntries_finished_by_user($user->id)); $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']; $earned_vouchers = $user->state->got_voucher - $voucher_settings['initial_vouchers'];
$eligable_vouchers = $shifts_done / $voucher_settings['shifts_per_voucher'] - $earned_vouchers; $eligable_vouchers = $shifts_done / $voucher_settings['shifts_per_voucher'] - $earned_vouchers;

Loading…
Cancel
Save