Files

103 lines
2.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Modules\ProjectManagement\Models;
use App\Models\User;
use App\Traits\HasPublicIdentifier;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\ApprovalWorkflow\Traits\HasApprovable;
use Modules\ProjectManagement\Enums\ReportStatus;
class WeeklyStatusReport extends Model
{
use HasPublicIdentifier, SoftDeletes, HasApprovable;
protected $fillable = [
'project_id',
'period_start',
'period_end',
'status',
'narrative_status',
'narrative_weather',
'narrative_compliance',
'submitted_by',
'approved_by',
];
protected function casts(): array
{
return [
'status' => ReportStatus::class,
'period_start' => 'date',
'period_end' => 'date',
];
}
// --- Relationships ---
public function project(): BelongsTo
{
return $this->belongsTo(Project::class);
}
public function workforceMetric(): HasOne
{
return $this->hasOne(WorkforceMetric::class);
}
public function hseRecord(): HasOne
{
return $this->hasOne(HseRecord::class);
}
public function submitter(): BelongsTo
{
return $this->belongsTo(User::class, 'submitted_by');
}
public function approver(): BelongsTo
{
return $this->belongsTo(User::class, 'approved_by');
}
// --- State Machine ---
public function transitionTo(ReportStatus $newStatus): void
{
if (!in_array($newStatus, $this->status->allowedTransitions())) {
throw new \InvalidArgumentException(
"Cannot transition report from {$this->status->label()} to {$newStatus->label()}"
);
}
$this->update(['status' => $newStatus]);
}
// --- Scopes ---
public function scopeStatus($query, ReportStatus $status)
{
return $query->where('status', $status);
}
public function scopeApproved($query)
{
return $query->where('status', ReportStatus::Approved);
}
// --- Accessors ---
public function getPeriodLabelAttribute(): string
{
return $this->period_start->format('M d') . ' ' . $this->period_end->format('M d, Y');
}
public function getDaysInPeriodAttribute(): int
{
return $this->period_start->diffInDays($this->period_end) + 1;
}
}