Files
GSB-Construction/Modules/ProjectManagement/app/Models/TaskDelay.php
Christopher Boyles 64d4b331a8
Some checks failed
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled
Tests / PHP 8.5 (push) Has been cancelled
Additional Changes
2026-06-02 22:04:03 +08:00

106 lines
3.4 KiB
PHP

<?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 Modules\ProjectManagement\Enums\DelayReason;
use Modules\ProjectManagement\Enums\WeatherCondition;
class TaskDelay extends Model
{
use HasPublicIdentifier;
protected $fillable = [
'task_id',
'reason_type',
'weather_condition',
'delay_date',
'lost_hours',
'notes',
'reported_by',
];
protected function casts(): array
{
return [
'reason_type' => DelayReason::class,
'weather_condition' => WeatherCondition::class,
'delay_date' => 'date',
'lost_hours' => 'decimal:2',
];
}
public function task(): BelongsTo
{
return $this->belongsTo(Task::class);
}
public function reporter(): BelongsTo
{
return $this->belongsTo(User::class, 'reported_by');
}
protected static function booted(): void
{
static::created(function (TaskDelay $td) {
if (auth()->check() && $td->task) {
$reason = $td->reason_type instanceof DelayReason ? $td->reason_type->label() : $td->reason_type;
$dateStr = $td->delay_date ? $td->delay_date->format('Y-m-d') : 'unknown date';
$td->task->activities()->create([
'user_id' => auth()->id(),
'description' => "logged delay: '{$reason}' on {$dateStr} ({$td->lost_hours}h lost).",
'type' => 'delay_add',
]);
}
});
static::updated(function (TaskDelay $td) {
if (auth()->check() && $td->task) {
$dirty = $td->getDirty();
$changes = [];
if (array_key_exists('lost_hours', $dirty)) {
$changes[] = "changed delay hours to {$td->lost_hours}h";
}
if (array_key_exists('reason_type', $dirty)) {
$reason = $td->reason_type instanceof DelayReason ? $td->reason_type->label() : $td->reason_type;
$changes[] = "changed delay reason to '{$reason}'";
}
if (!empty($changes)) {
$td->task->activities()->create([
'user_id' => auth()->id(),
'description' => "updated delay: " . implode(', ', $changes) . '.',
'type' => 'delay_update',
]);
}
}
});
static::deleted(function (TaskDelay $td) {
if (auth()->check() && $td->task) {
$reason = $td->reason_type instanceof DelayReason ? $td->reason_type->label() : $td->reason_type;
$dateStr = $td->delay_date ? $td->delay_date->format('Y-m-d') : 'unknown date';
$td->task->activities()->create([
'user_id' => auth()->id(),
'description' => "removed delay: '{$reason}' on {$dateStr}.",
'type' => 'delay_remove',
]);
}
});
}
// --- Accessors ---
public function getIsWeatherRelatedAttribute(): bool
{
return $this->reason_type === DelayReason::Weather;
}
public function getLostDaysAttribute(): float
{
return round((float) $this->lost_hours / 8, 1);
}
}