2022-05-19 00:32:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. 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;
|
|
|
|
|
2023-03-18 09:06:32 +01:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\BaseModel;
|
|
|
|
use App\Models\RecurringInvoice;
|
2023-01-13 02:43:38 +01:00
|
|
|
use App\Services\Scheduler\SchedulerService;
|
2022-05-25 23:25:54 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2022-05-19 00:32:40 +02:00
|
|
|
|
|
|
|
/**
|
2023-03-08 08:33:42 +01:00
|
|
|
* App\Models\Scheduler
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property bool $is_deleted
|
|
|
|
* @property int|null $created_at
|
|
|
|
* @property int|null $updated_at
|
|
|
|
* @property int|null $deleted_at
|
|
|
|
* @property array|null $parameters
|
|
|
|
* @property int $company_id
|
|
|
|
* @property bool $is_paused
|
|
|
|
* @property int|null $frequency_id
|
2023-07-26 04:59:36 +02:00
|
|
|
* @property \Carbon\Carbon|\Illuminate\Support\Carbon|null $next_run_client
|
|
|
|
* @property \Carbon\Carbon|\Illuminate\Support\Carbon|null $next_run
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property int $user_id
|
|
|
|
* @property string $name
|
|
|
|
* @property string $template
|
|
|
|
* @property int|null $remaining_cycles
|
|
|
|
* @property-read \App\Models\Company $company
|
|
|
|
* @property-read mixed $hashed_id
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel company()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
|
|
|
|
* @method static \Database\Factories\SchedulerFactory factory($count = null, $state = [])
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Scheduler filter(\App\Filters\QueryFilters $filters)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Scheduler newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Scheduler newQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Scheduler onlyTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Scheduler query()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Scheduler withTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Scheduler withoutTrashed()
|
2023-04-13 07:39:12 +02:00
|
|
|
* @property-read \App\Models\User $user
|
2023-03-08 08:33:42 +01:00
|
|
|
* @mixin \Eloquent
|
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
|
|
|
{
|
2023-01-13 02:43:38 +01:00
|
|
|
use SoftDeletes;
|
2023-02-26 08:21:23 +01:00
|
|
|
use Filterable;
|
|
|
|
|
2022-05-19 00:32:40 +02:00
|
|
|
protected $fillable = [
|
2023-01-13 10:16:17 +01:00
|
|
|
'name',
|
|
|
|
'frequency_id',
|
|
|
|
'next_run',
|
2023-01-17 09:42:34 +01:00
|
|
|
'next_run_client',
|
2023-01-13 10:16:17 +01:00
|
|
|
'template',
|
|
|
|
'is_paused',
|
2022-05-30 20:45:38 +02:00
|
|
|
'parameters',
|
2023-02-16 23:34:50 +01:00
|
|
|
'remaining_cycles',
|
2022-05-19 00:32:40 +02:00
|
|
|
];
|
2022-05-27 05:10:32 +02:00
|
|
|
|
2022-05-25 23:25:54 +02:00
|
|
|
protected $casts = [
|
2023-01-13 02:43:38 +01:00
|
|
|
'next_run' => 'datetime',
|
2023-01-17 09:42:34 +01:00
|
|
|
'next_run_client' => 'datetime',
|
2022-05-27 02:50:03 +02:00
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2023-01-13 10:16:17 +01:00
|
|
|
'is_paused' => 'boolean',
|
2022-05-26 04:16:19 +02:00
|
|
|
'is_deleted' => 'boolean',
|
2022-05-30 20:45:38 +02:00
|
|
|
'parameters' => 'array',
|
2022-05-25 23:25:54 +02:00
|
|
|
];
|
2022-05-30 20:45:38 +02:00
|
|
|
|
2023-01-13 02:43:38 +01:00
|
|
|
protected $appends = [
|
|
|
|
'hashed_id',
|
|
|
|
];
|
2022-05-23 01:08:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Service entry points.
|
|
|
|
*/
|
2023-01-13 02:43:38 +01:00
|
|
|
public function service(): SchedulerService
|
2022-05-23 01:08:08 +02:00
|
|
|
{
|
2023-01-13 02:43:38 +01:00
|
|
|
return new SchedulerService($this);
|
2022-05-23 01:08:08 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 12:40:40 +01:00
|
|
|
public function company()
|
2022-05-25 23:25:54 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Company::class);
|
|
|
|
}
|
2023-02-16 23:34:50 +01:00
|
|
|
|
2023-04-13 07:39:12 +02:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2023-02-16 23:34:50 +01:00
|
|
|
/**
|
|
|
|
* remainingCycles
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function remainingCycles() : int
|
|
|
|
{
|
|
|
|
if ($this->remaining_cycles == 0) {
|
|
|
|
return 0;
|
|
|
|
} elseif ($this->remaining_cycles == -1) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return $this->remaining_cycles - 1;
|
|
|
|
}
|
|
|
|
}
|
2023-03-18 09:06:32 +01:00
|
|
|
|
|
|
|
public function calculateNextRun()
|
|
|
|
{
|
|
|
|
if (! $this->next_run) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset = $this->company->timezone_offset();
|
|
|
|
|
|
|
|
switch ($this->frequency_id) {
|
2023-05-18 01:26:57 +02:00
|
|
|
case 0: //used only for email entities
|
2023-06-06 10:19:47 +02:00
|
|
|
$next_run = $this->next_run;
|
2023-05-18 01:26:57 +02:00
|
|
|
break;
|
2023-03-18 09:06:32 +01:00
|
|
|
case RecurringInvoice::FREQUENCY_DAILY:
|
|
|
|
$next_run = now()->startOfDay()->addDay();
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_WEEKLY:
|
|
|
|
$next_run = now()->startOfDay()->addWeek();
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
|
|
|
|
$next_run = now()->startOfDay()->addWeeks(2);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
|
|
|
|
$next_run = now()->startOfDay()->addWeeks(4);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_MONTHLY:
|
|
|
|
$next_run = now()->startOfDay()->addMonthNoOverflow();
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
|
|
|
|
$next_run = now()->startOfDay()->addMonthsNoOverflow(2);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
|
|
|
|
$next_run = now()->startOfDay()->addMonthsNoOverflow(3);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
|
|
|
|
$next_run = now()->startOfDay()->addMonthsNoOverflow(4);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
|
|
|
|
$next_run = now()->startOfDay()->addMonthsNoOverflow(6);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_ANNUALLY:
|
|
|
|
$next_run = now()->startOfDay()->addYear();
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_YEARS:
|
|
|
|
$next_run = now()->startOfDay()->addYears(2);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_THREE_YEARS:
|
|
|
|
$next_run = now()->startOfDay()->addYears(3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$next_run = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->next_run_client = $next_run ?: null;
|
|
|
|
$this->next_run = $next_run ? $next_run->copy()->addSeconds($offset) : null;
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
2023-06-25 04:38:15 +02:00
|
|
|
public function adjustOffset(): void
|
|
|
|
{
|
|
|
|
if (! $this->next_run) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset = $this->company->timezone_offset();
|
|
|
|
|
|
|
|
$this->next_run = $this->next_run->copy()->addSeconds($offset);
|
|
|
|
$this->save();
|
2023-03-18 09:06:32 +01:00
|
|
|
|
2023-06-25 04:38:15 +02:00
|
|
|
}
|
2022-05-19 00:32:40 +02:00
|
|
|
}
|