2025-10-13 21:52:34 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Models\Orders\Client;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
|
|
|
|
|
|
class ClientService extends Service
|
|
|
|
|
{
|
|
|
|
|
/** 목록(검색/페이징) */
|
|
|
|
|
public function index(array $params)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
|
2025-11-06 17:45:49 +09:00
|
|
|
$page = (int) ($params['page'] ?? 1);
|
|
|
|
|
$size = (int) ($params['size'] ?? 20);
|
|
|
|
|
$q = trim((string) ($params['q'] ?? ''));
|
2025-10-13 21:52:34 +09:00
|
|
|
$onlyActive = $params['only_active'] ?? null;
|
|
|
|
|
|
|
|
|
|
$query = Client::query()->where('tenant_id', $tenantId);
|
|
|
|
|
|
|
|
|
|
if ($q !== '') {
|
|
|
|
|
$query->where(function ($qq) use ($q) {
|
|
|
|
|
$qq->where('name', 'like', "%{$q}%")
|
|
|
|
|
->orWhere('client_code', 'like', "%{$q}%")
|
|
|
|
|
->orWhere('contact_person', 'like', "%{$q}%");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($onlyActive !== null) {
|
2025-12-08 20:25:38 +09:00
|
|
|
$query->where('is_active', (bool) $onlyActive);
|
2025-10-13 21:52:34 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query->orderBy('client_code')->orderBy('id');
|
|
|
|
|
|
|
|
|
|
return $query->paginate($size, ['*'], 'page', $page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 단건 */
|
|
|
|
|
public function show(int $id)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
$client = Client::where('tenant_id', $tenantId)->find($id);
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $client) {
|
2025-10-13 21:52:34 +09:00
|
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
|
|
|
}
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-10-13 21:52:34 +09:00
|
|
|
return $client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 생성 */
|
2025-12-08 20:25:38 +09:00
|
|
|
public function store(array $data)
|
2025-10-13 21:52:34 +09:00
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
|
|
|
|
|
// client_code 중복 검사
|
|
|
|
|
$exists = Client::where('tenant_id', $tenantId)
|
|
|
|
|
->where('client_code', $data['client_code'])
|
|
|
|
|
->exists();
|
|
|
|
|
if ($exists) {
|
|
|
|
|
throw new BadRequestHttpException(__('error.duplicate_code'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data['tenant_id'] = $tenantId;
|
2025-12-08 20:25:38 +09:00
|
|
|
$data['is_active'] = $data['is_active'] ?? true;
|
2025-10-13 21:52:34 +09:00
|
|
|
|
|
|
|
|
return Client::create($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 수정 */
|
2025-12-08 20:25:38 +09:00
|
|
|
public function update(int $id, array $data)
|
2025-10-13 21:52:34 +09:00
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
|
|
|
|
|
$client = Client::where('tenant_id', $tenantId)->find($id);
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $client) {
|
2025-10-13 21:52:34 +09:00
|
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// client_code 변경 시 중복 검사
|
2025-12-08 20:25:38 +09:00
|
|
|
if (isset($data['client_code']) && $data['client_code'] !== $client->client_code) {
|
2025-10-13 21:52:34 +09:00
|
|
|
$exists = Client::where('tenant_id', $tenantId)
|
2025-12-08 20:25:38 +09:00
|
|
|
->where('client_code', $data['client_code'])
|
2025-10-13 21:52:34 +09:00
|
|
|
->exists();
|
|
|
|
|
if ($exists) {
|
|
|
|
|
throw new BadRequestHttpException(__('error.duplicate_code'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 20:25:38 +09:00
|
|
|
$client->update($data);
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-10-13 21:52:34 +09:00
|
|
|
return $client->refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 삭제 */
|
|
|
|
|
public function destroy(int $id)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
|
|
|
|
|
$client = Client::where('tenant_id', $tenantId)->find($id);
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $client) {
|
2025-10-13 21:52:34 +09:00
|
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 주문 존재 검사
|
|
|
|
|
if ($client->orders()->exists()) {
|
|
|
|
|
throw new BadRequestHttpException(__('error.has_orders'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$client->delete();
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-10-13 21:52:34 +09:00
|
|
|
return 'success';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 활성/비활성 토글 */
|
|
|
|
|
public function toggle(int $id)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
$client = Client::where('tenant_id', $tenantId)->find($id);
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $client) {
|
2025-10-13 21:52:34 +09:00
|
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 15:40:24 +09:00
|
|
|
$client->is_active = ! $client->is_active;
|
2025-10-13 21:52:34 +09:00
|
|
|
$client->save();
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-10-13 21:52:34 +09:00
|
|
|
return $client->refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|