153 lines
4.0 KiB
PHP
153 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\Activitylog\Support\LogOptions;
|
|
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
|
|
|
class Submission extends Model
|
|
{
|
|
use HasFactory, SoftDeletes, LogsActivity;
|
|
|
|
public const STATUSES = [
|
|
'draf' => 'Draf',
|
|
'baru' => 'Baru',
|
|
'dalam_tindakan' => 'Dalam Tindakan',
|
|
'pembetulan_perunding' => 'Pembetulan Perunding',
|
|
'selesai' => 'Selesai',
|
|
];
|
|
|
|
protected $fillable = [
|
|
'data_centre_id',
|
|
'reporting_cycle_id',
|
|
'consultant_id',
|
|
'checklist_template_id',
|
|
'status',
|
|
'is_locked',
|
|
'locked_by',
|
|
'locked_at',
|
|
'lock_reason',
|
|
'assigned_officer_id',
|
|
'hardcopy_received',
|
|
'hardcopy_received_date',
|
|
'hardcopy_sent_date',
|
|
'submitted_at',
|
|
'submitted_by',
|
|
'review_notes',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_locked' => 'boolean',
|
|
'locked_at' => 'datetime',
|
|
'hardcopy_received' => 'boolean',
|
|
'hardcopy_received_date' => 'date',
|
|
'hardcopy_sent_date' => 'date',
|
|
'submitted_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logOnly(['status', 'is_locked', 'assigned_officer_id', 'hardcopy_received', 'hardcopy_sent_date'])
|
|
->logOnlyDirty()
|
|
->dontLogEmptyChanges()
|
|
->useLogName('serahan');
|
|
}
|
|
|
|
public function dataCentre(): BelongsTo
|
|
{
|
|
return $this->belongsTo(DataCentre::class);
|
|
}
|
|
|
|
public function reportingCycle(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ReportingCycle::class);
|
|
}
|
|
|
|
public function consultant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Consultant::class);
|
|
}
|
|
|
|
public function checklistTemplate(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ChecklistTemplate::class);
|
|
}
|
|
|
|
public function assignedOfficer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'assigned_officer_id');
|
|
}
|
|
|
|
public function answers(): HasMany
|
|
{
|
|
return $this->hasMany(SubmissionAnswer::class);
|
|
}
|
|
|
|
public function results(): HasMany
|
|
{
|
|
return $this->hasMany(SubmissionResult::class);
|
|
}
|
|
|
|
public function documents(): HasMany
|
|
{
|
|
return $this->hasMany(SubmissionDocument::class)->orderByDesc('version');
|
|
}
|
|
|
|
public function activeDocument(): HasOne
|
|
{
|
|
return $this->hasOne(SubmissionDocument::class)->where('is_active', true);
|
|
}
|
|
|
|
public function statusHistories(): HasMany
|
|
{
|
|
return $this->hasMany(SubmissionStatusHistory::class)->latest();
|
|
}
|
|
|
|
public function correctionRequests(): HasMany
|
|
{
|
|
return $this->hasMany(SubmissionCorrectionRequest::class)->latest();
|
|
}
|
|
|
|
public function comments(): HasMany
|
|
{
|
|
return $this->hasMany(SubmissionComment::class)->oldest();
|
|
}
|
|
|
|
/** Perunding boleh edit selagi belum dikunci dan belum selesai. */
|
|
public function isEditableByConsultant(): bool
|
|
{
|
|
return ! $this->is_locked && $this->status !== 'selesai';
|
|
}
|
|
|
|
public function isSubmitted(): bool
|
|
{
|
|
return ! in_array($this->status, ['draf'], true);
|
|
}
|
|
|
|
public function statusLabel(): string
|
|
{
|
|
return self::STATUSES[$this->status] ?? $this->status;
|
|
}
|
|
|
|
public function statusBadgeClass(): string
|
|
{
|
|
return [
|
|
'draf' => 'secondary',
|
|
'baru' => 'info',
|
|
'dalam_tindakan' => 'primary',
|
|
'pembetulan_perunding' => 'warning',
|
|
'selesai' => 'success',
|
|
][$this->status] ?? 'secondary';
|
|
}
|
|
}
|