'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', ]; 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'); } /** * 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; } }