82 lines
2.1 KiB
PHP
82 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Registries;
|
|
|
|
use App\Models\Asset;
|
|
use App\Support\HasObjectsTrait;
|
|
use App\Support\RelationValuesTrait;
|
|
use App\Support\UuidScopeTrait;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Entry extends Model {
|
|
use UuidScopeTrait, SoftDeletes, HasObjectsTrait, RelationValuesTrait;
|
|
|
|
protected $table = 'registry_entries';
|
|
|
|
protected $dates = [
|
|
'active_since',
|
|
'active_till',
|
|
'suspended_since',
|
|
'suspended_till',
|
|
'cancelled_at'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'uuid',
|
|
'registry_id',
|
|
'category_id',
|
|
'asset_id',
|
|
'number',
|
|
'name',
|
|
'active_since',
|
|
'active_till',
|
|
'suspended_since',
|
|
'suspended_till',
|
|
'cancelled_at'
|
|
];
|
|
|
|
protected $hidden = [
|
|
'id'
|
|
];
|
|
|
|
|
|
public function registry(): BelongsTo {
|
|
return $this->belongsTo(Registry::class);
|
|
}
|
|
|
|
public function category(): BelongsTo {
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function asset(): BelongsTo {
|
|
return $this->belongsTo(Asset::class);
|
|
}
|
|
|
|
public function operations(): MorphToMany {
|
|
return $this->objectsByGroup('operations')->reorder()->applyOrders(['order-date' => 'desc']);
|
|
}
|
|
|
|
|
|
public function getPropertiesAttribute(): ?Model {
|
|
return ($type = $this->registry->parsedType['options']['properties'] ?? null) ? $this->getObject($type, 'properties') : null;
|
|
}
|
|
|
|
public function getStateAttribute(): ?array {
|
|
$state = null;
|
|
if ($this->active_since) {
|
|
$state = ($this->active_since <= now()) ? EntryState::ACTIVE : EntryState::AWAITING;
|
|
if ($this->active_till && ($this->active_till <= now())) $state = EntryState::EXPIRED;
|
|
elseif ($this->suspended_since && ($this->suspended_since <= now())) {
|
|
|
|
}
|
|
}
|
|
return $state ? ['name' => $state, 'title' => EntryState::TITLES[$state] ?? null] : null;
|
|
}
|
|
|
|
|
|
|
|
}
|