Files
GSB-Construction/Modules/ProjectManagement/app/Models/HseRecord.php

76 lines
2.1 KiB
PHP

<?php
namespace Modules\ProjectManagement\Models;
use App\Traits\HasPublicIdentifier;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class HseRecord extends Model
{
use HasPublicIdentifier;
protected $fillable = [
'weekly_status_report_id',
// Proactive
'toolbox_meetings',
'safety_observations',
// Reactive
'fatalities',
'major_injuries',
'first_aid_cases',
'medical_cases',
'near_misses',
'environmental_damage',
'property_damage',
'fines',
];
protected function casts(): array
{
return [
'fines' => 'decimal:2',
];
}
public function report(): BelongsTo
{
return $this->belongsTo(WeeklyStatusReport::class, 'weekly_status_report_id');
}
// --- Accessors ---
public function getTotalIncidentsAttribute(): int
{
return $this->fatalities
+ $this->major_injuries
+ $this->first_aid_cases
+ $this->medical_cases
+ $this->near_misses
+ $this->environmental_damage
+ $this->property_damage;
}
public function getHasZeroIncidentsAttribute(): bool
{
return $this->total_incidents === 0 && (float) $this->fines === 0.0;
}
/**
* Returns each reactive field as a labeled array for UI rendering.
*/
public function getReactiveBreakdownAttribute(): array
{
return [
['label' => 'Fatalities', 'value' => $this->fatalities],
['label' => 'Major Injuries', 'value' => $this->major_injuries],
['label' => 'First Aid Cases', 'value' => $this->first_aid_cases],
['label' => 'Medical Cases', 'value' => $this->medical_cases],
['label' => 'Near Misses', 'value' => $this->near_misses],
['label' => 'Environmental Damage', 'value' => $this->environmental_damage],
['label' => 'Property Damage', 'value' => $this->property_damage],
['label' => 'Fines/Costs', 'value' => $this->fines, 'isCurrency' => true],
];
}
}