2022-05-19 00:32:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-05-26 04:16:19 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-05-19 00:32:40 +02:00
|
|
|
*
|
2022-05-26 04:16:19 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2022-05-19 00:32:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-05-23 01:08:08 +02:00
|
|
|
use App\Services\TaskScheduler\TaskSchedulerService;
|
2022-05-19 00:32:40 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2022-05-25 23:25:54 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2022-05-24 01:42:43 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2022-05-19 00:32:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property boolean paused
|
2022-05-26 04:16:19 +02:00
|
|
|
* @property boolean is_deleted
|
2022-05-19 00:32:40 +02:00
|
|
|
* @property \Carbon\Carbon|mixed start_from
|
|
|
|
* @property string repeat_every
|
|
|
|
* @property \Carbon\Carbon|mixed scheduled_run
|
2022-05-23 01:33:33 +02:00
|
|
|
* @property mixed job
|
2022-05-23 21:58:05 +02:00
|
|
|
* @property integer company_id
|
2022-05-25 23:25:54 +02:00
|
|
|
* @property integer updated_at
|
|
|
|
* @property integer created_at
|
|
|
|
* @property integer deleted_at
|
2022-05-19 00:32:40 +02:00
|
|
|
*/
|
2022-05-25 23:25:54 +02:00
|
|
|
class Scheduler extends BaseModel
|
2022-05-19 00:32:40 +02:00
|
|
|
{
|
2022-05-25 23:25:54 +02:00
|
|
|
use HasFactory, SoftDeletes;
|
2022-05-19 00:32:40 +02:00
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'start_from',
|
|
|
|
'paused',
|
|
|
|
'repeat_every',
|
|
|
|
'scheduled_run',
|
2022-05-23 21:58:05 +02:00
|
|
|
'company_id'
|
2022-05-19 00:32:40 +02:00
|
|
|
];
|
2022-05-25 23:25:54 +02:00
|
|
|
protected $casts = [
|
|
|
|
'start_from' => 'timestamp',
|
|
|
|
'scheduled_run' => 'timestamp',
|
2022-05-27 02:50:03 +02:00
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2022-05-25 23:25:54 +02:00
|
|
|
'paused' => 'boolean',
|
2022-05-26 04:16:19 +02:00
|
|
|
'is_deleted' => 'boolean',
|
2022-05-25 23:25:54 +02:00
|
|
|
];
|
2022-05-23 01:08:08 +02:00
|
|
|
protected $appends = ['linked_job'];
|
|
|
|
|
2022-05-19 00:32:40 +02:00
|
|
|
const DAILY = 'DAY';
|
|
|
|
const WEEKLY = 'WEEK';
|
2022-05-27 02:50:03 +02:00
|
|
|
const BIWEEKLY = 'BIWEEKLY';
|
2022-05-19 00:32:40 +02:00
|
|
|
const MONTHLY = 'MONTH';
|
|
|
|
const QUARTERLY = '3MONTHS';
|
|
|
|
const ANNUALLY = 'YEAR';
|
|
|
|
|
2022-05-23 01:08:08 +02:00
|
|
|
public function getLinkedJobAttribute()
|
|
|
|
{
|
|
|
|
return $this->job ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Service entry points.
|
|
|
|
*/
|
|
|
|
public function service(): TaskSchedulerService
|
|
|
|
{
|
|
|
|
return new TaskSchedulerService($this);
|
|
|
|
}
|
|
|
|
|
2022-05-19 00:32:40 +02:00
|
|
|
public function job(): \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
{
|
|
|
|
return $this->hasOne(ScheduledJob::class, 'scheduler_id', 'id');
|
|
|
|
}
|
2022-05-24 01:42:43 +02:00
|
|
|
|
2022-05-25 23:25:54 +02:00
|
|
|
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Company::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function nextScheduledDate(): ?Carbon
|
2022-05-24 01:42:43 +02:00
|
|
|
{
|
|
|
|
|
2022-05-25 23:25:54 +02:00
|
|
|
$offset = 0;
|
|
|
|
|
|
|
|
$entity_send_time = $this->company->settings->entity_send_time;
|
|
|
|
|
|
|
|
if ($entity_send_time != 0) {
|
|
|
|
$timezone = $this->company->timezone();
|
|
|
|
|
|
|
|
$offset -= $timezone->utc_offset;
|
|
|
|
$offset += ($entity_send_time * 3600);
|
|
|
|
}
|
|
|
|
|
2022-05-24 01:42:43 +02:00
|
|
|
/*
|
|
|
|
As we are firing at UTC+0 if our offset is negative it is technically firing the day before so we always need
|
|
|
|
to add ON a day - a day = 86400 seconds
|
|
|
|
*/
|
2022-05-25 23:25:54 +02:00
|
|
|
|
|
|
|
if ($offset < 0)
|
|
|
|
$offset += 86400;
|
2022-05-24 01:42:43 +02:00
|
|
|
|
|
|
|
switch ($this->repeat_every) {
|
|
|
|
case self::DAILY:
|
|
|
|
return Carbon::parse($this->scheduled_run)->startOfDay()->addDay()->addSeconds($offset);
|
|
|
|
case self::WEEKLY:
|
|
|
|
return Carbon::parse($this->scheduled_run)->startOfDay()->addWeek()->addSeconds($offset);
|
2022-05-27 02:50:03 +02:00
|
|
|
case self::BIWEEKLY:
|
|
|
|
return Carbon::parse($this->scheduled_run)->startOfDay()->addWeeks(2)->addSeconds($offset);
|
2022-05-24 01:42:43 +02:00
|
|
|
case self::MONTHLY:
|
2022-05-27 02:50:03 +02:00
|
|
|
return Carbon::parse($this->scheduled_run)->startOfDay()->addMonthNoOverflow()->addSeconds($offset);
|
2022-05-24 01:42:43 +02:00
|
|
|
case self::QUARTERLY:
|
2022-05-27 02:50:03 +02:00
|
|
|
return Carbon::parse($this->scheduled_run)->startOfDay()->addMonthsNoOverflow(3)->addSeconds($offset);
|
2022-05-24 01:42:43 +02:00
|
|
|
case self::ANNUALLY:
|
2022-05-27 02:50:03 +02:00
|
|
|
return Carbon::parse($this->scheduled_run)->startOfDay()->addYearNoOverflow()->addSeconds($offset);
|
2022-05-24 01:42:43 +02:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-05-19 00:32:40 +02:00
|
|
|
}
|