orderBy('environment') ->orderBy('is_active', 'desc') ->orderBy('created_at', 'desc') ->get(); // HTMX 요청 시 HTML 반환 if ($request->header('HX-Request')) { return response( view('barobill.config.partials.table', compact('configs'))->render(), 200, ['Content-Type' => 'text/html'] ); } return response()->json([ 'success' => true, 'data' => $configs, ]); } /** * 설정 등록 */ public function store(Request $request): JsonResponse { $validated = $request->validate([ 'name' => 'required|string|max:50', 'environment' => 'required|in:test,production', 'cert_key' => 'required|string|max:100', 'corp_num' => 'nullable|string|max:20', 'base_url' => 'required|url|max:255', 'description' => 'nullable|string|max:500', 'is_active' => 'nullable|boolean', ], [ 'name.required' => '설정 이름을 입력해주세요.', 'environment.required' => '환경을 선택해주세요.', 'cert_key.required' => '인증키(CERTKEY)를 입력해주세요.', 'base_url.required' => '서버 URL을 입력해주세요.', 'base_url.url' => '올바른 URL 형식을 입력해주세요.', ]); DB::beginTransaction(); try { // is_active가 true이면 같은 환경의 다른 설정들은 비활성화 if ($validated['is_active'] ?? false) { BarobillConfig::where('environment', $validated['environment']) ->update(['is_active' => false]); } $config = BarobillConfig::create($validated); DB::commit(); return response()->json([ 'success' => true, 'message' => '바로빌 설정이 등록되었습니다.', 'data' => $config, ], 201); } catch (\Exception $e) { DB::rollBack(); Log::error('바로빌 설정 등록 실패', ['error' => $e->getMessage()]); return response()->json([ 'success' => false, 'message' => '설정 등록 중 오류가 발생했습니다.', ], 500); } } /** * 설정 상세 조회 */ public function show(int $id): JsonResponse { $config = BarobillConfig::find($id); if (!$config) { return response()->json([ 'success' => false, 'message' => '설정을 찾을 수 없습니다.', ], 404); } return response()->json([ 'success' => true, 'data' => $config, ]); } /** * 설정 수정 */ public function update(Request $request, int $id): JsonResponse { $config = BarobillConfig::find($id); if (!$config) { return response()->json([ 'success' => false, 'message' => '설정을 찾을 수 없습니다.', ], 404); } $validated = $request->validate([ 'name' => 'required|string|max:50', 'environment' => 'required|in:test,production', 'cert_key' => 'required|string|max:100', 'corp_num' => 'nullable|string|max:20', 'base_url' => 'required|url|max:255', 'description' => 'nullable|string|max:500', 'is_active' => 'nullable|boolean', ]); DB::beginTransaction(); try { // is_active가 true이면 같은 환경의 다른 설정들은 비활성화 if ($validated['is_active'] ?? false) { BarobillConfig::where('environment', $validated['environment']) ->where('id', '!=', $id) ->update(['is_active' => false]); } $config->update($validated); DB::commit(); return response()->json([ 'success' => true, 'message' => '바로빌 설정이 수정되었습니다.', 'data' => $config->fresh(), ]); } catch (\Exception $e) { DB::rollBack(); Log::error('바로빌 설정 수정 실패', ['error' => $e->getMessage()]); return response()->json([ 'success' => false, 'message' => '설정 수정 중 오류가 발생했습니다.', ], 500); } } /** * 설정 삭제 */ public function destroy(int $id): JsonResponse { $config = BarobillConfig::find($id); if (!$config) { return response()->json([ 'success' => false, 'message' => '설정을 찾을 수 없습니다.', ], 404); } if ($config->is_active) { return response()->json([ 'success' => false, 'message' => '활성화된 설정은 삭제할 수 없습니다. 먼저 비활성화해주세요.', ], 422); } $config->delete(); return response()->json([ 'success' => true, 'message' => '바로빌 설정이 삭제되었습니다.', ]); } /** * 설정 활성화/비활성화 토글 */ public function toggleActive(int $id): JsonResponse { $config = BarobillConfig::find($id); if (!$config) { return response()->json([ 'success' => false, 'message' => '설정을 찾을 수 없습니다.', ], 404); } DB::beginTransaction(); try { if (!$config->is_active) { // 활성화하려면 같은 환경의 다른 설정들 비활성화 BarobillConfig::where('environment', $config->environment) ->where('id', '!=', $id) ->update(['is_active' => false]); } $config->update(['is_active' => !$config->is_active]); DB::commit(); return response()->json([ 'success' => true, 'message' => $config->is_active ? '설정이 활성화되었습니다.' : '설정이 비활성화되었습니다.', 'data' => $config->fresh(), ]); } catch (\Exception $e) { DB::rollBack(); Log::error('바로빌 설정 토글 실패', ['error' => $e->getMessage()]); return response()->json([ 'success' => false, 'message' => '설정 변경 중 오류가 발생했습니다.', ], 500); } } /** * barobill_companies에서 barobill_members로 동기화 * chandj DB(sales 레거시)에서 데이터를 가져와 samdb(MNG)로 동기화 */ public function syncCompanies(): JsonResponse { DB::beginTransaction(); try { // chandj DB의 barobill_companies 테이블에서 데이터 조회 $companies = DB::connection('chandj') ->table('barobill_companies') ->where('is_active', 1) ->whereNotNull('barobill_user_id') ->get(); $synced = 0; $skipped = 0; $errors = []; foreach ($companies as $company) { // 이미 존재하는지 확인 (사업자번호 기준) $existing = BarobillMember::where('biz_no', $company->corp_num)->first(); if ($existing) { // 기존 데이터 업데이트 (비밀번호 제외) $existing->update([ 'corp_name' => $company->company_name, 'ceo_name' => $company->ceo_name ?? $existing->ceo_name, 'barobill_id' => $company->barobill_user_id, ]); $skipped++; } else { // 새로 생성 BarobillMember::create([ 'tenant_id' => 1, // 기본 테넌트 'biz_no' => $company->corp_num, 'corp_name' => $company->company_name, 'ceo_name' => $company->ceo_name ?? '', 'barobill_id' => $company->barobill_user_id, 'barobill_pwd' => '', // 비밀번호는 별도로 입력 필요 'status' => 'active', ]); $synced++; } } DB::commit(); return response()->json([ 'success' => true, 'message' => "동기화 완료: 신규 {$synced}건, 업데이트 {$skipped}건", 'data' => [ 'synced' => $synced, 'updated' => $skipped, 'total' => $companies->count(), ], ]); } catch (\Exception $e) { DB::rollBack(); Log::error('바로빌 회원사 동기화 실패', ['error' => $e->getMessage()]); return response()->json([ 'success' => false, 'message' => '동기화 중 오류가 발생했습니다: ' . $e->getMessage(), ], 500); } } /** * barobill_companies 목록 조회 (chandj DB) */ public function getCompanies(): JsonResponse { try { $companies = DB::connection('chandj') ->table('barobill_companies') ->select('id', 'company_name', 'corp_num', 'barobill_user_id', 'ceo_name', 'is_active', 'memo', 'created_at') ->orderBy('id') ->get(); return response()->json([ 'success' => true, 'data' => $companies, ]); } catch (\Exception $e) { Log::error('chandj DB 연결 실패', ['error' => $e->getMessage()]); return response()->json([ 'success' => false, 'message' => 'chandj DB 연결에 실패했습니다: ' . $e->getMessage(), 'data' => [], ]); } } }