summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-12-25 16:26:59 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2019-12-25 16:26:59 +0100
commit25cdf1cac8c66c55a7c2a354c1be51de261da5b5 (patch)
tree241b3c25e618cbbcbb36772f6e79483316a93dbb
parent45d13ac998217a20939400fa5367382d03c482af (diff)
Voucher: Added calculation start time
-rw-r--r--config/config.default.php2
-rw-r--r--includes/model/ShiftEntry_model.php7
-rw-r--r--includes/model/User_model.php5
3 files changed, 11 insertions, 3 deletions
diff --git a/config/config.default.php b/config/config.default.php
index 7fdd3b2e..06f8201f 100644
--- a/config/config.default.php
+++ b/config/config.default.php
@@ -138,6 +138,8 @@ return [
'voucher_settings' => [
'initial_vouchers' => 0,
'shifts_per_voucher' => 1,
+ // 'Y-m-d' formatted
+ 'voucher_start' => null,
],
// Available locales in /locale/
diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php
index 5dd11421..ea025604 100644
--- a/includes/model/ShiftEntry_model.php
+++ b/includes/model/ShiftEntry_model.php
@@ -1,5 +1,6 @@
<?php
+use Carbon\Carbon;
use Engelsystem\Database\DB;
use Engelsystem\Models\User\User;
@@ -201,10 +202,11 @@ function ShiftEntries_upcoming_for_user($userId)
/**
* Returns shifts completed by the given user.
*
- * @param int $userId
+ * @param int $userId
+ * @param Carbon|null $sinceTime
* @return array
*/
-function ShiftEntries_finished_by_user($userId)
+function ShiftEntries_finished_by_user($userId, Carbon $sinceTime = null)
{
return DB::select('
SELECT *
@@ -214,6 +216,7 @@ function ShiftEntries_finished_by_user($userId)
WHERE `ShiftEntry`.`UID` = ?
AND `Shifts`.`end` < ?
AND `ShiftEntry`.`freeloaded` = 0
+ ' . ($sinceTime ? 'AND Shifts.start >= ' . $sinceTime->getTimestamp() : '') . '
ORDER BY `Shifts`.`end` desc
',
[
diff --git a/includes/model/User_model.php b/includes/model/User_model.php
index 681e70aa..1b1434bb 100644
--- a/includes/model/User_model.php
+++ b/includes/model/User_model.php
@@ -233,7 +233,10 @@ function User_reset_api_key($user, $log = true)
function User_get_eligable_voucher_count($user)
{
$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'];
$eligable_vouchers = $shifts_done / $voucher_settings['shifts_per_voucher'] - $earned_vouchers;