Files
sam-api/app/Services/ClientService.php
hskwon d164bb4c4a feat: [client] 거래처 API 2차 필드 추가 및 견적 계획 업데이트
- 거래처 유형(client_type), 연락처(mobile, fax), 담당자 정보 필드 추가
- 발주처 설정(account_id/password, payment_day) 필드 추가
- 약정 세금(tax_agreement, tax_amount, tax_start/end_date) 필드 추가
- 악성채권(bad_debt 관련 5개 필드) 정보 필드 추가
- Model, Service, FormRequest, Swagger 문서 업데이트
- 견적 API 계획에 문서 발송 API(email/fax/kakao) 요구사항 추가
2025-12-04 21:13:58 +09:00

213 lines
7.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\Orders\Client;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ClientService extends Service
{
/** 목록(검색/페이징) */
public function index(array $params)
{
$tenantId = $this->tenantId();
$page = (int) ($params['page'] ?? 1);
$size = (int) ($params['size'] ?? 20);
$q = trim((string) ($params['q'] ?? ''));
$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) {
$query->where('is_active', $onlyActive ? 'Y' : 'N');
}
$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);
if (! $client) {
throw new NotFoundHttpException(__('error.not_found'));
}
return $client;
}
/** 생성 */
public function store(array $params)
{
$tenantId = $this->tenantId();
$uid = $this->apiUserId();
$v = Validator::make($params, [
'client_code' => 'required|string|max:50',
'name' => 'required|string|max:100',
'client_type' => 'nullable|in:매입,매출,매입매출',
'contact_person' => 'nullable|string|max:50',
'phone' => 'nullable|string|max:30',
'mobile' => 'nullable|string|max:20',
'fax' => 'nullable|string|max:20',
'email' => 'nullable|email|max:80',
'address' => 'nullable|string|max:255',
'manager_name' => 'nullable|string|max:50',
'manager_tel' => 'nullable|string|max:20',
'system_manager' => 'nullable|string|max:50',
'account_id' => 'nullable|string|max:50',
'account_password' => 'nullable|string|max:255',
'purchase_payment_day' => 'nullable|string|max:20',
'sales_payment_day' => 'nullable|string|max:20',
'business_no' => 'nullable|string|max:20',
'business_type' => 'nullable|string|max:50',
'business_item' => 'nullable|string|max:100',
'tax_agreement' => 'nullable|boolean',
'tax_amount' => 'nullable|numeric|min:0',
'tax_start_date' => 'nullable|date',
'tax_end_date' => 'nullable|date',
'bad_debt' => 'nullable|boolean',
'bad_debt_amount' => 'nullable|numeric|min:0',
'bad_debt_receive_date' => 'nullable|date',
'bad_debt_end_date' => 'nullable|date',
'bad_debt_progress' => 'nullable|in:협의중,소송중,회수완료,대손처리',
'memo' => 'nullable|string',
'is_active' => 'nullable|in:Y,N',
]);
if ($v->fails()) {
throw new BadRequestHttpException($v->errors()->first());
}
$data = $v->validated();
// 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;
$data['is_active'] = $data['is_active'] ?? 'Y';
return Client::create($data);
}
/** 수정 */
public function update(int $id, array $params)
{
$tenantId = $this->tenantId();
$uid = $this->apiUserId();
$client = Client::where('tenant_id', $tenantId)->find($id);
if (! $client) {
throw new NotFoundHttpException(__('error.not_found'));
}
$v = Validator::make($params, [
'client_code' => 'sometimes|required|string|max:50',
'name' => 'sometimes|required|string|max:100',
'client_type' => 'nullable|in:매입,매출,매입매출',
'contact_person' => 'nullable|string|max:50',
'phone' => 'nullable|string|max:30',
'mobile' => 'nullable|string|max:20',
'fax' => 'nullable|string|max:20',
'email' => 'nullable|email|max:80',
'address' => 'nullable|string|max:255',
'manager_name' => 'nullable|string|max:50',
'manager_tel' => 'nullable|string|max:20',
'system_manager' => 'nullable|string|max:50',
'account_id' => 'nullable|string|max:50',
'account_password' => 'nullable|string|max:255',
'purchase_payment_day' => 'nullable|string|max:20',
'sales_payment_day' => 'nullable|string|max:20',
'business_no' => 'nullable|string|max:20',
'business_type' => 'nullable|string|max:50',
'business_item' => 'nullable|string|max:100',
'tax_agreement' => 'nullable|boolean',
'tax_amount' => 'nullable|numeric|min:0',
'tax_start_date' => 'nullable|date',
'tax_end_date' => 'nullable|date',
'bad_debt' => 'nullable|boolean',
'bad_debt_amount' => 'nullable|numeric|min:0',
'bad_debt_receive_date' => 'nullable|date',
'bad_debt_end_date' => 'nullable|date',
'bad_debt_progress' => 'nullable|in:협의중,소송중,회수완료,대손처리',
'memo' => 'nullable|string',
'is_active' => 'nullable|in:Y,N',
]);
if ($v->fails()) {
throw new BadRequestHttpException($v->errors()->first());
}
$payload = $v->validated();
// client_code 변경 시 중복 검사
if (isset($payload['client_code']) && $payload['client_code'] !== $client->client_code) {
$exists = Client::where('tenant_id', $tenantId)
->where('client_code', $payload['client_code'])
->exists();
if ($exists) {
throw new BadRequestHttpException(__('error.duplicate_code'));
}
}
$client->update($payload);
return $client->refresh();
}
/** 삭제 */
public function destroy(int $id)
{
$tenantId = $this->tenantId();
$client = Client::where('tenant_id', $tenantId)->find($id);
if (! $client) {
throw new NotFoundHttpException(__('error.not_found'));
}
// 주문 존재 검사
if ($client->orders()->exists()) {
throw new BadRequestHttpException(__('error.has_orders'));
}
$client->delete();
return 'success';
}
/** 활성/비활성 토글 */
public function toggle(int $id)
{
$tenantId = $this->tenantId();
$client = Client::where('tenant_id', $tenantId)->find($id);
if (! $client) {
throw new NotFoundHttpException(__('error.not_found'));
}
// Model에서 is_active가 boolean으로 캐스팅되므로 boolean으로 토글
$client->is_active = ! $client->is_active;
$client->save();
return $client->refresh();
}
}