2026-10-20 20:46:48 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-10-28 09:47:16 +07:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2026-10-20 20:46:48 +07:00
|
|
|
|
|
|
|
|
class UnitSupportTimesheet extends Model
|
|
|
|
|
{
|
2026-10-28 09:47:16 +07:00
|
|
|
use HasFactory, SoftDeletes;
|
2026-10-20 20:46:48 +07:00
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
];
|
|
|
|
|
|
2026-11-01 22:28:12 +07:00
|
|
|
protected $appends = [
|
|
|
|
|
'effective_end_meter',
|
|
|
|
|
'effective_operating_hours',
|
|
|
|
|
];
|
|
|
|
|
|
2026-10-20 20:46:48 +07:00
|
|
|
public function vehicle(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Vehicle::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function operator(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Employee::class, 'operator_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createdByUser(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'created_by_user_id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-01 22:28:12 +07:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 20:46:48 +07:00
|
|
|
/**
|
|
|
|
|
* Generate nomor timesheet unik harian (Format: TS-US-YYYYMMDD-0001).
|
2026-10-28 15:37:01 +07:00
|
|
|
* Dilengkapi mitigasi tabrakan concurrency dan pengecekan soft deleted records.
|
2026-10-20 20:46:48 +07:00
|
|
|
*/
|
|
|
|
|
public static function generateTimesheetNo(?Carbon $date = null): string
|
|
|
|
|
{
|
|
|
|
|
$d = $date ?: Carbon::today();
|
|
|
|
|
$dateStr = $d->format('Ymd');
|
|
|
|
|
$prefix = "TS-US-{$dateStr}-";
|
|
|
|
|
|
2026-10-28 15:37:01 +07:00
|
|
|
$last = self::withTrashed()
|
|
|
|
|
->where('timesheet_no', 'like', "{$prefix}%")
|
2026-10-20 20:46:48 +07:00
|
|
|
->orderByDesc('id')
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
$seq = 1;
|
|
|
|
|
if ($last && preg_match('/-(\d+)$/', $last->timesheet_no, $matches)) {
|
|
|
|
|
$seq = ((int) $matches[1]) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 15:37:01 +07:00
|
|
|
$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;
|
2026-10-20 20:46:48 +07:00
|
|
|
}
|
|
|
|
|
}
|