Files
sam-manage/app/Services/FormulaApiService.php
권혁성 f271f8bdc3 feat:품목관리 3-Panel 페이지 신규 구현 + FormulaEvaluatorService 연동
- 품목관리 3-Panel 레이아웃 (좌:목록, 중:BOM/수식산출, 우:상세)
- FormulaApiService로 API 견적수식 엔진 연동
- FG 품목 선택 시 기본값(W:1000, H:1000, QTY:1) 자동 산출
- 수식 산출 결과 트리 렌더링 (그룹별/소계/합계)
- 중앙 패널 클릭 시 우측 상세만 변경 (skipCenterUpdate)
- API 인증 버튼 전역 헤더로 이동 (모든 페이지에서 사용 가능)
- FormulaApiService에 Bearer 토큰 지원 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 10:50:24 +09:00

84 lines
3.1 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class FormulaApiService
{
/**
* API 서버의 FormulaEvaluatorService를 HTTP로 호출하여 BOM 산출
*
* Docker 내부 통신 패턴:
* - URL: https://nginx/api/v1/quotes/calculate/bom (Docker nginx 컨테이너)
* - Host 헤더: api.sam.kr (nginx가 올바른 서버 블록으로 라우팅)
* - SSL 우회: withoutVerifying() (내부 자체 서명 인증서)
* - 인증: X-API-KEY 헤더 (FLOW_TESTER_API_KEY 환경변수)
*
* @param string $finishedGoodsCode 완제품 코드 (예: FG-KQTS01)
* @param array $variables 입력 변수 ['W0' => 3000, 'H0' => 3000, 'QTY' => 1]
* @param int $tenantId 테넌트 ID
* @return array 성공 시 API 응답, 실패 시 ['success' => false, 'error' => '...']
*/
public function calculateBom(string $finishedGoodsCode, array $variables, int $tenantId): array
{
try {
$apiKey = config('api-explorer.default_environments.0.api_key')
?: env('FLOW_TESTER_API_KEY', '');
// Bearer token: 세션에 저장된 API 토큰 사용 (헤더 인증 UI에서 발급)
$bearerToken = session('api_explorer_token');
$headers = [
'Host' => 'api.sam.kr',
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-API-KEY' => $apiKey,
'X-TENANT-ID' => (string) $tenantId,
];
$http = Http::timeout(30)->withoutVerifying()->withHeaders($headers);
if ($bearerToken) {
$http = $http->withToken($bearerToken);
}
// API의 QuoteBomCalculateRequest는 W0, H0, QTY 등을 최상위 레벨에서 기대
$payload = array_merge(
['finished_goods_code' => $finishedGoodsCode],
$variables // W0, H0, QTY 등을 풀어서 전송
);
$response = $http->post('https://nginx/api/v1/quotes/calculate/bom', $payload);
if ($response->successful()) {
$json = $response->json();
// ApiResponse::handle()는 {success, message, data} 구조로 래핑
return $json['data'] ?? $json;
}
Log::warning('FormulaApiService: API 호출 실패', [
'status' => $response->status(),
'body' => $response->body(),
'code' => $finishedGoodsCode,
]);
return [
'success' => false,
'error' => 'API 응답 오류: HTTP ' . $response->status(),
];
} catch (\Exception $e) {
Log::error('FormulaApiService: 예외 발생', [
'message' => $e->getMessage(),
'code' => $finishedGoodsCode,
]);
return [
'success' => false,
'error' => '수식 계산 서버 연결 실패: ' . $e->getMessage(),
];
}
}
}