feat: 거래처 통계 및 일괄삭제 API 추가 (Phase 2.2)

- ClientService: stats(), bulkDestroy() 메서드 추가
- ClientController: stats(), bulkDestroy() 액션 추가
- routes/api.php: /clients/stats, /clients/bulk 라우트 추가
- 악성채권 보유 거래처 통계 계산 (BadDebt 연계)
- 주문 있는 거래처는 삭제 건너뜀

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-01-09 16:46:25 +09:00
parent 00f57ce244
commit da2eddead4
3 changed files with 93 additions and 1 deletions

View File

@@ -56,4 +56,32 @@ public function toggle(int $id)
return $this->service->toggle($id);
}, __('message.client.toggled'));
}
/**
* 거래처 통계
*/
public function stats()
{
return ApiResponse::handle(function () {
return $this->service->stats();
}, __('message.fetched'));
}
/**
* 거래처 일괄 삭제
*/
public function bulkDestroy(Request $request)
{
$ids = $request->input('ids', []);
if (empty($ids)) {
return ApiResponse::error(__('error.no_items_selected'), 400);
}
return ApiResponse::handle(function () use ($ids) {
$deletedCount = $this->service->bulkDestroy($ids);
return ['deleted_count' => $deletedCount];
}, __('message.deleted'));
}
}

View File

@@ -9,7 +9,6 @@
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Services\PushNotificationService;
class ClientService extends Service
{
@@ -224,4 +223,67 @@ public function toggle(int $id)
return $client->refresh();
}
/**
* 거래처 통계 조회
*/
public function stats(): array
{
$tenantId = $this->tenantId();
$total = Client::where('tenant_id', $tenantId)->count();
// 거래처 유형별 통계
$typeCounts = Client::where('tenant_id', $tenantId)
->selectRaw('client_type, COUNT(*) as count')
->groupBy('client_type')
->pluck('count', 'client_type')
->toArray();
// 악성채권 보유 거래처 수 (활성 악성채권이 있는 거래처)
$badDebtClientIds = BadDebt::where('tenant_id', $tenantId)
->whereIn('status', [BadDebt::STATUS_COLLECTING, BadDebt::STATUS_LEGAL_ACTION])
->where('is_active', true)
->whereNull('deleted_at')
->distinct('client_id')
->pluck('client_id');
$badDebtCount = Client::where('tenant_id', $tenantId)
->whereIn('id', $badDebtClientIds)
->count();
return [
'total' => $total,
'sales' => $typeCounts['SALES'] ?? 0,
'purchase' => $typeCounts['PURCHASE'] ?? 0,
'both' => $typeCounts['BOTH'] ?? 0,
'badDebt' => $badDebtCount,
'normal' => $total - $badDebtCount,
];
}
/**
* 거래처 일괄 삭제
*/
public function bulkDestroy(array $ids): int
{
$tenantId = $this->tenantId();
$clients = Client::where('tenant_id', $tenantId)
->whereIn('id', $ids)
->get();
$deletedCount = 0;
foreach ($clients as $client) {
// 주문 존재 검사 - 주문 있으면 건너뛰기
if ($client->orders()->exists()) {
continue;
}
$client->delete();
$deletedCount++;
}
return $deletedCount;
}
}

View File

@@ -921,6 +921,8 @@
// Clients (거래처 관리)
Route::prefix('clients')->group(function () {
Route::get('/stats', [ClientController::class, 'stats'])->name('v1.clients.stats'); // 통계
Route::delete('/bulk', [ClientController::class, 'bulkDestroy'])->name('v1.clients.bulk-destroy'); // 일괄 삭제
Route::get('', [ClientController::class, 'index'])->name('v1.clients.index'); // 목록
Route::post('', [ClientController::class, 'store'])->name('v1.clients.store'); // 생성
Route::get('/{id}', [ClientController::class, 'show'])->whereNumber('id')->name('v1.clients.show'); // 단건