Files
sembara/app/Services/ProductService.php

68 lines
1.9 KiB
PHP

<?php
namespace App\Services;
use App\Services\InsForge\InsForgeTableService;
class ProductService extends InsForgeTableService
{
protected function table(): string
{
return 'products';
}
protected function searchableColumns(): array
{
return ['name', 'code'];
}
protected function sortableColumns(): array
{
return ['code', 'name', 'created_at', 'stock_minimum', 'cost_price'];
}
public function nextCode(): string
{
$last = $this->client->listRecords($this->table(), [
'order' => 'code.desc.nullslast',
'limit' => 1,
'select' => 'code',
])[0]['code'] ?? null;
preg_match('/(\d+)$/', (string) $last, $matches);
return sprintf('BERAS-%03d', ((int) ($matches[1] ?? 0)) + 1);
}
/** @return array<string, string> id => "CODE — Nama" */
public function optionsForSelect(): array
{
return collect($this->client->listRecords($this->table(), [
'select' => 'id,code,name',
'is_active' => 'eq.true',
'order' => 'code.asc',
'limit' => 200,
]))
->mapWithKeys(fn (array $row): array => [$row['id'] => trim(($row['code'] ?? '').' — '.($row['name'] ?? ''), ' —')])
->all();
}
/** @return array<string, int> */
public function codeMap(): array
{
return collect($this->client->listRecords($this->table(), [
'select' => 'id,code',
'limit' => 200,
]))->mapWithKeys(fn (array $row): array => [$row['id'] => $row['code']])->all();
}
/** @return array<string, int> */
public function costMap(): array
{
return collect($this->client->listRecords($this->table(), [
'select' => 'id,cost_price',
'limit' => 200,
]))->mapWithKeys(fn (array $row): array => [$row['id'] => (int) ($row['cost_price'] ?? 0)])->all();
}
}