Files
rkm-kmh/app/Models/UnitSupportTimesheet.php
T

146 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class UnitSupportTimesheet extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'timesheet_no',
'vehicle_id',
'operator_id',
'date',
'shift',
'meter_type',
'start_time',
'end_time',
'start_meter',
'end_meter',
'operating_hours',
'standby_hours',
'breakdown_hours',
'location',
'activity_type',
'task_description',
'fuel_liters',
'status',
'notes',
'created_by_user_id',
];
protected $casts = [
'date' => 'date',
'start_meter' => 'decimal:2',
'end_meter' => 'decimal:2',
'operating_hours' => 'decimal:2',
'standby_hours' => 'decimal:2',
'breakdown_hours' => 'decimal:2',
'fuel_liters' => 'decimal:2',
];
protected $appends = [
'effective_end_meter',
'effective_operating_hours',
];
public function vehicle(): BelongsTo
{
return $this->belongsTo(Vehicle::class);
}
public function operator(): BelongsTo
{
return $this->belongsTo(Employee::class, 'operator_id')->withTrashed();
}
public function createdByUser(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by_user_id');
}
/**
* Dapatkan meter akhir efektif.
* Jika end_meter sudah diisi, gunakan nilainya.
* Jika belum, cari dari start_meter entri berikutnya untuk unit yang sama.
*/
public function getEffectiveEndMeterAttribute(): ?float
{
if ($this->end_meter !== null && (float) $this->end_meter > 0) {
return (float) $this->end_meter;
}
$next = static::where('vehicle_id', $this->vehicle_id)
->where(function ($q) {
$q->where('date', '>', $this->date)
->orWhere(function ($sq) {
$sq->where('date', $this->date)
->where('id', '>', $this->id);
});
})
->orderBy('date', 'asc')
->orderBy('id', 'asc')
->first();
if ($next && $next->start_meter !== null && (float) $next->start_meter >= (float) $this->start_meter) {
return (float) $next->start_meter;
}
return null;
}
/**
* Dapatkan jam kerja operasional efektif (1 HM = 1 Jam).
*/
public function getEffectiveOperatingHoursAttribute(): ?float
{
if ($this->operating_hours !== null && (float) $this->operating_hours > 0) {
return (float) $this->operating_hours;
}
$end = $this->effective_end_meter;
if ($end !== null && $this->start_meter !== null) {
return max(0.0, (float) $end - (float) $this->start_meter);
}
return null;
}
/**
* Generate nomor timesheet unik harian (Format: TS-US-YYYYMMDD-0001).
* Dilengkapi mitigasi tabrakan concurrency dan pengecekan soft deleted records.
*/
public static function generateTimesheetNo(?Carbon $date = null): string
{
$d = $date ?: Carbon::today();
$dateStr = $d->format('Ymd');
$prefix = "TS-US-{$dateStr}-";
$last = self::withTrashed()
->where('timesheet_no', 'like', "{$prefix}%")
->orderByDesc('id')
->first();
$seq = 1;
if ($last && preg_match('/-(\d+)$/', $last->timesheet_no, $matches)) {
$seq = ((int) $matches[1]) + 1;
}
$candidate = $prefix . str_pad((string) $seq, 4, '0', STR_PAD_LEFT);
// Mitigasi bentrokan concurrency: cek apakah candidate sudah pernah digunakan (termasuk soft deletes)
while (self::withTrashed()->where('timesheet_no', $candidate)->exists()) {
$seq++;
$candidate = $prefix . str_pad((string) $seq, 4, '0', STR_PAD_LEFT);
}
return $candidate;
}
}