First commit
This commit is contained in:
99
app/Models/Document.php
Normal file
99
app/Models/Document.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class Document extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'category_id',
|
||||
'title',
|
||||
'description',
|
||||
'status',
|
||||
'is_active',
|
||||
'effective_date',
|
||||
'expiry_date',
|
||||
'tags',
|
||||
'language',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'effective_date' => 'date',
|
||||
'expiry_date' => 'date',
|
||||
'tags' => 'array',
|
||||
];
|
||||
|
||||
// Status constants untuk kejelasan
|
||||
const STATUS_DRAFT = 'draft';
|
||||
const STATUS_PROCESSING = 'processing';
|
||||
const STATUS_ACTIVE = 'active';
|
||||
const STATUS_INACTIVE = 'inactive';
|
||||
const STATUS_FAILED = 'failed';
|
||||
|
||||
// === Relationships ===
|
||||
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
public function versions(): HasMany
|
||||
{
|
||||
return $this->hasMany(DocumentVersion::class)->orderBy('version_number');
|
||||
}
|
||||
|
||||
public function currentVersion(): HasOne
|
||||
{
|
||||
return $this->hasOne(DocumentVersion::class)->where('is_current', true);
|
||||
}
|
||||
|
||||
public function chunks(): HasMany
|
||||
{
|
||||
return $this->hasMany(DocumentChunk::class);
|
||||
}
|
||||
|
||||
public function creator(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
public function updater(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'updated_by');
|
||||
}
|
||||
|
||||
// === Scopes ===
|
||||
|
||||
public function scopeActive(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_active', true)->where('status', self::STATUS_ACTIVE);
|
||||
}
|
||||
|
||||
public function scopeByCategory(Builder $query, int $categoryId): Builder
|
||||
{
|
||||
return $query->where('category_id', $categoryId);
|
||||
}
|
||||
|
||||
// === Helpers ===
|
||||
|
||||
public function getLatestVersionNumber(): int
|
||||
{
|
||||
return $this->versions()->max('version_number') ?? 0;
|
||||
}
|
||||
|
||||
public function isProcessing(): bool
|
||||
{
|
||||
return $this->status === self::STATUS_PROCESSING;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user