first(); // 바로빌 회원사 정보 조회 (담당자 정보 기본값용) $barobillMember = BarobillMember::where('tenant_id', $tenantId)->first(); if (! $setting) { // 설정이 없으면 바로빌 회원사 정보를 기본값으로 사용 return response()->json([ 'success' => true, 'data' => [ 'use_tax_invoice' => false, 'use_bank_account' => false, 'use_card_usage' => false, 'use_hometax' => false, // 바로빌 회원사의 담당자 정보를 기본값으로 제공 'contact_name' => $barobillMember?->manager_name ?? '', 'contact_tel' => $barobillMember?->manager_hp ?? '', 'contact_id' => $barobillMember?->manager_email ?? '', ], ]); } // 설정이 있지만 담당자 정보가 비어있으면 바로빌 회원사 정보로 채움 $data = $setting->toArray(); if (empty($data['contact_name']) && $barobillMember?->manager_name) { $data['contact_name'] = $barobillMember->manager_name; } if (empty($data['contact_tel']) && $barobillMember?->manager_hp) { $data['contact_tel'] = $barobillMember->manager_hp; } if (empty($data['contact_id']) && $barobillMember?->manager_email) { $data['contact_id'] = $barobillMember->manager_email; } return response()->json([ 'success' => true, 'data' => $data, ]); } /** * 바로빌 설정 저장/수정 */ public function store(Request $request): JsonResponse { $tenantId = session('selected_tenant_id', 1); $validated = $request->validate([ 'use_tax_invoice' => 'nullable|boolean', 'use_bank_account' => 'nullable|boolean', 'use_card_usage' => 'nullable|boolean', 'use_hometax' => 'nullable|boolean', 'contact_name' => 'nullable|string|max:50', 'contact_tel' => 'nullable|string|max:20', 'contact_id' => 'nullable|email|max:100', ]); try { $setting = BarobillSetting::updateOrCreate( ['tenant_id' => $tenantId], array_merge($validated, [ 'corp_name' => $validated['corp_name'] ?? '미설정', 'ceo_name' => $validated['ceo_name'] ?? '미설정', 'corp_num' => $validated['corp_num'] ?? '0000000000', ]) ); return response()->json([ 'success' => true, 'message' => '설정이 저장되었습니다.', 'data' => $setting, ]); } catch (\Exception $e) { Log::error('바로빌 설정 저장 실패', ['error' => $e->getMessage()]); return response()->json([ 'success' => false, 'message' => '설정 저장에 실패했습니다.', ], 500); } } /** * 서비스 설정 개별 저장 (체크박스 변경 시 즉시 저장) */ public function updateService(Request $request): JsonResponse { $tenantId = session('selected_tenant_id', 1); $validated = $request->validate([ 'field' => 'required|in:use_tax_invoice,use_bank_account,use_card_usage,use_hometax', 'value' => 'required|boolean', ]); try { $setting = BarobillSetting::updateOrCreate( ['tenant_id' => $tenantId], [ $validated['field'] => $validated['value'], 'corp_name' => '미설정', 'ceo_name' => '미설정', 'corp_num' => '0000000000', ] ); $serviceNames = [ 'use_tax_invoice' => '전자세금계산서', 'use_bank_account' => '계좌조회', 'use_card_usage' => '카드사용내역', 'use_hometax' => '홈텍스매입/매출', ]; $serviceName = $serviceNames[$validated['field']] ?? $validated['field']; $status = $validated['value'] ? '활성화' : '비활성화'; return response()->json([ 'success' => true, 'message' => "{$serviceName} 서비스가 {$status}되었습니다.", ]); } catch (\Exception $e) { Log::error('바로빌 서비스 설정 저장 실패', ['error' => $e->getMessage()]); return response()->json([ 'success' => false, 'message' => '설정 저장에 실패했습니다.', ], 500); } } /** * 서비스 이용 여부 확인 (다른 메뉴에서 참조용) */ public function checkService(string $service): JsonResponse { $tenantId = session('selected_tenant_id', 1); $setting = BarobillSetting::where('tenant_id', $tenantId)->first(); if (! $setting) { return response()->json([ 'success' => true, 'enabled' => false, 'message' => '바로빌 설정이 없습니다.', ]); } $enabled = match ($service) { 'tax_invoice', 'tax-invoice' => $setting->use_tax_invoice, 'bank_account', 'bank-account' => $setting->use_bank_account, 'card_usage', 'card-usage' => $setting->use_card_usage, 'hometax' => $setting->use_hometax, default => false, }; return response()->json([ 'success' => true, 'enabled' => $enabled, 'service' => $service, ]); } }