chore: update document approval workflow and bug fixes
This commit is contained in:
54
Modules/ProjectManagement/app/Models/WorkforceMetric.php
Normal file
54
Modules/ProjectManagement/app/Models/WorkforceMetric.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user