Files
sembara/app/Services/CustomerService.php

49 lines
1.3 KiB
PHP

<?php
namespace App\Services;
use App\Services\InsForge\InsForgeTableService;
class CustomerService extends InsForgeTableService
{
protected function table(): string
{
return 'customers';
}
protected function searchableColumns(): array
{
return ['name', 'customer_code', 'phone'];
}
protected function sortableColumns(): array
{
return ['customer_code', 'name', 'created_at'];
}
public function nextCode(): string
{
$last = $this->client->listRecords($this->table(), [
'order' => 'customer_code.desc.nullslast',
'limit' => 1,
'select' => 'customer_code',
])[0]['customer_code'] ?? null;
preg_match('/(\d+)$/', (string) $last, $matches);
return sprintf('CUST-%04d', ((int) ($matches[1] ?? 0)) + 1);
}
/** @return array<string, string> id => "CUST-0001 — Nama" */
public function optionsForSelect(): array
{
return collect($this->client->listRecords($this->table(), [
'select' => 'id,customer_code,name',
'order' => 'customer_code.asc',
'limit' => 500,
]))
->mapWithKeys(fn (array $row): array => [$row['id'] => trim(($row['customer_code'] ?? '').' — '.($row['name'] ?? ''), ' —')])
->all();
}
}