- 세션 키 수정: tenant_id → selected_tenant_id - 설정이 없거나 담당자 정보가 비어있을 때 바로빌 회원사 정보를 기본값으로 사용 - 담당자명, 연락처, 이메일 자동 매핑 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
137 lines
4.7 KiB
PHP
137 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Barobill;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Barobill\BarobillMember;
|
|
use App\Models\Barobill\BarobillSetting;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class BarobillSettingController extends Controller
|
|
{
|
|
/**
|
|
* 현재 테넌트의 바로빌 설정 조회
|
|
*
|
|
* 설정이 없거나 담당자 정보가 없으면 바로빌 회원사 정보를 기본값으로 사용
|
|
*/
|
|
public function show(): JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id', 1);
|
|
|
|
$setting = BarobillSetting::where('tenant_id', $tenantId)->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 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,
|
|
]);
|
|
}
|
|
}
|