50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Services\InsForge\InsForgeTableService;
|
|
|
|
class DriverService extends InsForgeTableService
|
|
{
|
|
protected function table(): string
|
|
{
|
|
return 'drivers';
|
|
}
|
|
|
|
protected function searchableColumns(): array
|
|
{
|
|
return ['name', 'driver_code', 'phone'];
|
|
}
|
|
|
|
protected function sortableColumns(): array
|
|
{
|
|
return ['driver_code', 'name', 'created_at'];
|
|
}
|
|
|
|
public function nextCode(): string
|
|
{
|
|
$last = $this->client->listRecords($this->table(), [
|
|
'order' => 'driver_code.desc.nullslast',
|
|
'limit' => 1,
|
|
'select' => 'driver_code',
|
|
])[0]['driver_code'] ?? null;
|
|
|
|
preg_match('/(\d+)$/', (string) $last, $matches);
|
|
|
|
return sprintf('DRV-%04d', ((int) ($matches[1] ?? 0)) + 1);
|
|
}
|
|
|
|
/** @return array<string, string> id => "DRV-0001 — Nama" (hanya aktif) */
|
|
public function optionsForSelect(): array
|
|
{
|
|
return collect($this->client->listRecords($this->table(), [
|
|
'select' => 'id,driver_code,name',
|
|
'is_active' => 'eq.true',
|
|
'order' => 'driver_code.asc',
|
|
'limit' => 200,
|
|
]))
|
|
->mapWithKeys(fn (array $row): array => [$row['id'] => trim(($row['driver_code'] ?? '').' — '.($row['name'] ?? ''), ' —')])
|
|
->all();
|
|
}
|
|
}
|