Make AlertSchedule::isActive work

This commit is contained in:
Tony Murray
2020-04-10 13:23:06 -05:00
parent eac7838de9
commit a68be76015
2 changed files with 11 additions and 16 deletions

View File

@@ -40,7 +40,8 @@ class AlertSchedule extends Model
public function scopeIsActive($query)
{
return $query->where(function ($query) {
$now = CarbonImmutable::now();
$now = CarbonImmutable::now('UTC');
$query->where(function ($query) use ($now) {
// Non recurring simply between start and end
$query->where('recurring', 0)
@@ -49,22 +50,14 @@ class AlertSchedule extends Model
})->orWhere(function ($query) use ($now) {
$query->where('recurring', 1)
// Check the time is after the start date and before the end date, or end date is not set
->where(function ($query) use ($now) {
$query->where('start_recurring_dt', '<=', $now->format('%Y-%m-%d'))
->where(function ($query) use ($now) {
$query->where('end_recurring_dt', '>=', $now->format('%Y-%m-%d'))
->orWhereNull('end_recurring_dt')
->orWhere('end_recurring_dt', '0000-00-00')
->orWhere('end_recurring_dt', '');
});
})
// Check the time is between the start and end hour/minutes/seconds
->where('start_recurring_hr', '<=', $now->format('%H:%i:%s'))
->where('end_recurring_hr', '>=', $now->format('%H:%i:%s'))
->where('start', '<=', $now)
->where('end', '>=', $now)
->whereTime('start', '<=', $now->toTimeString())
->whereTime('end', '>=', $now->toTimeString())
// Check we are on the correct day of the week
->where(function ($query) use ($now) {
/** @var Builder $query */
$query->where('recurring_day', 'like', '%' . $now->format('%w') . '%')
$query->where('recurring_day', 'like', $now->format('%N%'))
->orWhereNull('recurring_day')
->orWhere('recurring_day', '');
});

View File

@@ -12,9 +12,10 @@ class AlertScheduleUtc extends Migration
*/
public function up()
{
DB::table('alert_schedule')->where('recurring', 1)->update([
DB::table('alert_schedule')->update([
'start' => DB::raw("CONVERT_TZ(IF(`recurring` = 1, STR_TO_DATE(CONCAT(start_recurring_dt, ' ', start_recurring_hr), '%Y-%m-%d %H:%i:%s'), start), @@global.time_zone, '+00:00')"),
'end' => DB::raw("CONVERT_TZ(IF(`recurring` = 1, STR_TO_DATE(CONCAT(end_recurring_dt, ' ', end_recurring_hr), '%Y-%m-%d %H:%i:%s'), end), @@global.time_zone, '+00:00')"),
'end' => DB::raw("CONVERT_TZ(IF(`recurring` = 1, STR_TO_DATE(CONCAT(IFNULL(end_recurring_dt, '9000-9-9'), ' ', end_recurring_hr), '%Y-%m-%d %H:%i:%s'), end), @@global.time_zone, '+00:00')"),
'recurring_day' => DB::raw('REPLACE(recurring_day, 0, 7)'), // convert to RFC N date format
]);
Schema::table('alert_schedule', function (Blueprint $table) {
@@ -43,6 +44,7 @@ class AlertScheduleUtc extends Migration
'start_recurring_hr' => DB::raw("TIME(CONVERT_TZ(start, '+00:00', @@global.time_zone))"),
'end_recurring_dt' => DB::raw("DATE(CONVERT_TZ(end, '+00:00', @@global.time_zone))"),
'end_recurring_hr' => DB::raw("TIME(CONVERT_TZ(end, '+00:00', @@global.time_zone))"),
'recurring_day' => DB::raw('REPLACE(recurring_day, 7, 0)'),
]);
}
}