Files
financial/public/api/category_suggest.php
2026-06-07 00:33:58 +09:00

61 lines
1.8 KiB
PHP

<?php
require_once __DIR__ . '/../../app/lib/auth.php';
require_once __DIR__ . '/../../app/lib/merchant_pattern_service.php';
check_auth();
header('Content-Type: application/json; charset=UTF-8');
header('Cache-Control: no-store');
$merchant = trim((string)($_GET['merchant_name'] ?? ''));
$transactionType = trim((string)($_GET['transaction_type'] ?? ''));
$allowedTypes = ['income', 'expense', 'transfer'];
if ($merchant === '' || $transactionType === '' || !in_array($transactionType, $allowedTypes, true)) {
echo json_encode([
'ok' => true,
'found' => false,
], JSON_UNESCAPED_UNICODE);
exit;
}
if (mb_strlen($merchant, 'UTF-8') < 2) {
echo json_encode([
'ok' => true,
'found' => false,
], JSON_UNESCAPED_UNICODE);
exit;
}
try {
$suggested = suggest_category_from_merchant(user_id(), $merchant, $transactionType);
if (!$suggested) {
echo json_encode([
'ok' => true,
'found' => false,
], JSON_UNESCAPED_UNICODE);
exit;
}
echo json_encode([
'ok' => true,
'found' => true,
'category_id' => (int)$suggested['category_id'],
'category_name' => (string)$suggested['category_name'],
'category_type' => (string)$suggested['category_type'],
'pattern_text' => (string)($suggested['pattern_text'] ?? ''),
'keyword' => (string)($suggested['keyword'] ?? $suggested['pattern_text'] ?? ''),
'match_type' => (string)($suggested['match_type'] ?? ''),
'priority' => (int)($suggested['priority'] ?? 0),
'confidence' => (float)($suggested['confidence'] ?? 0),
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'ok' => false,
'found' => false,
'message' => '자동추천 조회 실패',
], JSON_UNESCAPED_UNICODE);
}