55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\ProjectManagement\Models;
|
|
|
|
use App\Traits\HasPublicIdentifier;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class WorkforceMetric extends Model
|
|
{
|
|
use HasPublicIdentifier;
|
|
|
|
protected $fillable = [
|
|
'weekly_status_report_id',
|
|
'active_workforce',
|
|
'period_man_hours',
|
|
'cumulative_man_hours',
|
|
'logistics_km',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'period_man_hours' => 'decimal:2',
|
|
'cumulative_man_hours' => 'decimal:2',
|
|
'logistics_km' => 'decimal:2',
|
|
];
|
|
}
|
|
|
|
public function report(): BelongsTo
|
|
{
|
|
return $this->belongsTo(WeeklyStatusReport::class, 'weekly_status_report_id');
|
|
}
|
|
|
|
/**
|
|
* Calculate cumulative man-hours from all previous approved reports + current period.
|
|
*/
|
|
public static function calculateCumulative(int $projectId, string $periodStart, float $currentPeriodHours, ?int $excludeReportId = null): float
|
|
{
|
|
$query = self::whereHas('report', fn ($q) => $q
|
|
->where('project_id', $projectId)
|
|
->where('status', 'approved')
|
|
->where('period_end', '<', $periodStart)
|
|
);
|
|
|
|
if ($excludeReportId) {
|
|
$query->where('weekly_status_report_id', '!=', $excludeReportId);
|
|
}
|
|
|
|
$previousTotal = $query->sum('period_man_hours');
|
|
|
|
return (float) $previousTotal + $currentPeriodHours;
|
|
}
|
|
}
|