feat:고객사 등록 사업자등록증 OCR 자동입력 기능 추가
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,8 +4,10 @@
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Finance\Customer;
|
||||
use App\Services\TradingPartnerOcrService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CustomerController extends Controller
|
||||
{
|
||||
@@ -90,4 +92,78 @@ public function destroy(int $id): JsonResponse
|
||||
Customer::forTenant($tenantId)->findOrFail($id)->delete();
|
||||
return response()->json(['success' => true, 'message' => '고객사가 삭제되었습니다.']);
|
||||
}
|
||||
|
||||
public function ocr(Request $request, TradingPartnerOcrService $ocrService): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'image' => [
|
||||
'required',
|
||||
'string',
|
||||
function ($attribute, $value, $fail) {
|
||||
if (strlen($value) > 7 * 1024 * 1024) {
|
||||
$fail('이미지 크기는 5MB 이하여야 합니다.');
|
||||
}
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
try {
|
||||
$result = $ocrService->extractFromImage($request->input('image'));
|
||||
|
||||
// raw_response에서 원본 OCR 데이터 파싱 (address, biz_type 등 직접 접근)
|
||||
$raw = json_decode($result['raw_response'], true) ?? [];
|
||||
|
||||
// 고객사 폼 필드에 맞게 매핑
|
||||
$data = [
|
||||
'name' => trim($raw['company_name'] ?? $result['data']['name'] ?? ''),
|
||||
'bizNo' => $result['data']['bizNo'] ?? '',
|
||||
'ceo' => trim($raw['ceo_name'] ?? $result['data']['manager'] ?? ''),
|
||||
'contact' => $result['data']['contact'] ?? '',
|
||||
'email' => $result['data']['email'] ?? '',
|
||||
'address' => trim($raw['address'] ?? ''),
|
||||
'industry' => $this->matchIndustry($raw['biz_type'] ?? '', $raw['biz_item'] ?? ''),
|
||||
'memo' => $this->buildCustomerMemo($raw),
|
||||
];
|
||||
|
||||
return response()->json(['ok' => true, 'data' => $data]);
|
||||
} catch (\RuntimeException $e) {
|
||||
return response()->json(['ok' => false, 'message' => $e->getMessage()], 500);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('고객사 OCR 예상치 못한 오류', ['error' => $e->getMessage()]);
|
||||
return response()->json(['ok' => false, 'message' => 'OCR 처리 중 오류가 발생했습니다.'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
private function matchIndustry(string $bizType, string $bizItem): string
|
||||
{
|
||||
$text = $bizType . ' ' . $bizItem;
|
||||
|
||||
$keywords = [
|
||||
'IT/소프트웨어' => ['소프트웨어', 'IT', '정보통신', '전산', '컴퓨터', '인터넷', '데이터', '프로그램'],
|
||||
'제조업' => ['제조', '생산', '가공', '조립'],
|
||||
'서비스업' => ['서비스', '용역', '컨설팅', '대행'],
|
||||
'유통업' => ['유통', '도매', '소매', '판매', '무역', '수출', '수입', '상업'],
|
||||
'금융업' => ['금융', '보험', '은행', '증권', '투자'],
|
||||
];
|
||||
|
||||
foreach ($keywords as $industry => $kws) {
|
||||
foreach ($kws as $kw) {
|
||||
if (mb_strpos($text, $kw) !== false) {
|
||||
return $industry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '기타';
|
||||
}
|
||||
|
||||
private function buildCustomerMemo(array $raw): string
|
||||
{
|
||||
$parts = [];
|
||||
$bizType = trim($raw['biz_type'] ?? '');
|
||||
$bizItem = trim($raw['biz_item'] ?? '');
|
||||
if ($bizType) $parts[] = "[업태] {$bizType}";
|
||||
if ($bizItem) $parts[] = "[종목] {$bizItem}";
|
||||
return implode(' / ', $parts);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user