Files
sam-manage/app/Http/Controllers/Api/Admin/ItemManagementApiController.php
김보곤 bb4f4cd191 feat: [item-management] 품목 삭제 및 이력 조회 기능 추가
- 삭제: soft delete, 사용 중 품목 삭제 차단 (BOM/수주/견적/입고/LOT/작업지시 참조 체크)
- 이력: audit_logs 기반 생성/수정/삭제 이력 조회 모달
- 상세 패널에 이력/삭제 액션 버튼 추가
- API: DELETE /{id}, GET /{id}/history 엔드포인트 추가
2026-03-18 14:27:14 +09:00

119 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin;
use App\Http\Controllers\Controller;
use App\Models\Items\Item;
use App\Services\FormulaApiService;
use App\Services\ItemManagementService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class ItemManagementApiController extends Controller
{
public function __construct(
private readonly ItemManagementService $service
) {}
/**
* 품목 목록 (HTML partial - 좌측 패널)
*/
public function index(Request $request): View
{
$items = $this->service->getItemList([
'search' => $request->input('search'),
'item_type' => $request->input('item_type'),
'per_page' => $request->input('per_page', 50),
]);
return view('item-management.partials.item-list', compact('items'));
}
/**
* BOM 재귀 트리 (JSON - 중앙 패널, JS 렌더링)
*/
public function bomTree(int $id, Request $request): JsonResponse
{
$maxDepth = $request->input('max_depth', 10);
$tree = $this->service->getBomTree($id, $maxDepth);
return response()->json($tree);
}
/**
* 품목 상세 (HTML partial - 우측 패널)
*/
public function detail(int $id): View
{
$data = $this->service->getItemDetail($id);
return view('item-management.partials.item-detail', [
'item' => $data['item'],
'bomChildren' => $data['bom_children'],
]);
}
/**
* 품목 삭제 (Soft Delete, 사용 중 체크)
*/
public function destroy(int $id): JsonResponse
{
$result = $this->service->deleteItem($id);
return response()->json($result, $result['success'] ? 200 : 422);
}
/**
* 품목 이력 조회 (audit_logs 기반)
*/
public function history(int $id): JsonResponse
{
$history = $this->service->getItemHistory($id);
return response()->json($history);
}
/**
* 수식 기반 BOM 산출 (API 서버의 FormulaEvaluatorService HTTP 호출)
*/
public function calculateFormula(Request $request, int $id): JsonResponse
{
$item = Item::withoutGlobalScopes()
->where('tenant_id', session('selected_tenant_id'))
->findOrFail($id);
$width = (int) $request->input('width', 1000);
$height = (int) $request->input('height', 1000);
$qty = (int) $request->input('qty', 1);
$mp = $request->input('mp', 'single');
$variables = [
'W0' => $width,
'H0' => $height,
'QTY' => $qty,
'MP' => in_array($mp, ['single', 'three']) ? $mp : 'single',
];
// 제품모델/설치타입/마감타입 (입력값이 있으면 전달)
if ($request->filled('product_model')) {
$variables['product_model'] = $request->input('product_model');
}
if ($request->filled('installation_type')) {
$variables['installation_type'] = $request->input('installation_type');
}
if ($request->filled('finishing_type')) {
$variables['finishing_type'] = $request->input('finishing_type');
}
$formulaService = new FormulaApiService;
$result = $formulaService->calculateBom(
$item->code,
$variables,
(int) session('selected_tenant_id')
);
return response()->json($result);
}
}