diff --git a/app/Http/Controllers/Api/Admin/Quote/QuoteFormulaRangeController.php b/app/Http/Controllers/Api/Admin/Quote/QuoteFormulaRangeController.php new file mode 100644 index 00000000..287eeb24 --- /dev/null +++ b/app/Http/Controllers/Api/Admin/Quote/QuoteFormulaRangeController.php @@ -0,0 +1,151 @@ +rangeService->getRangesByFormula($formulaId); + + return response()->json([ + 'success' => true, + 'data' => $ranges, + ]); + } + + /** + * 범위 상세 조회 + */ + public function show(int $formulaId, int $rangeId): JsonResponse + { + // 수식 소속 확인 + if (! $this->rangeService->belongsToFormula($rangeId, $formulaId)) { + return response()->json([ + 'success' => false, + 'message' => '해당 수식에 속하지 않는 범위입니다.', + ], 404); + } + + $range = $this->rangeService->getRangeById($rangeId); + + return response()->json([ + 'success' => true, + 'data' => $range, + ]); + } + + /** + * 범위 생성 + */ + public function store(Request $request, int $formulaId): JsonResponse + { + $validated = $request->validate([ + 'min_value' => 'nullable|numeric', + 'max_value' => 'nullable|numeric', + 'condition_variable' => 'required|string|max:50', + 'result_value' => 'required|string', + 'result_type' => 'nullable|in:fixed,formula', + 'sort_order' => 'nullable|integer|min:0', + ]); + + $range = $this->rangeService->createRange($formulaId, $validated); + + return response()->json([ + 'success' => true, + 'message' => '범위가 추가되었습니다.', + 'data' => $range, + ]); + } + + /** + * 범위 수정 + */ + public function update(Request $request, int $formulaId, int $rangeId): JsonResponse + { + // 수식 소속 확인 + if (! $this->rangeService->belongsToFormula($rangeId, $formulaId)) { + return response()->json([ + 'success' => false, + 'message' => '해당 수식에 속하지 않는 범위입니다.', + ], 404); + } + + $validated = $request->validate([ + 'min_value' => 'nullable|numeric', + 'max_value' => 'nullable|numeric', + 'condition_variable' => 'nullable|string|max:50', + 'result_value' => 'nullable|string', + 'result_type' => 'nullable|in:fixed,formula', + ]); + + $range = $this->rangeService->updateRange($rangeId, $validated); + + return response()->json([ + 'success' => true, + 'message' => '범위가 수정되었습니다.', + 'data' => $range, + ]); + } + + /** + * 범위 삭제 + */ + public function destroy(int $formulaId, int $rangeId): JsonResponse + { + // 수식 소속 확인 + if (! $this->rangeService->belongsToFormula($rangeId, $formulaId)) { + return response()->json([ + 'success' => false, + 'message' => '해당 수식에 속하지 않는 범위입니다.', + ], 404); + } + + $this->rangeService->deleteRange($rangeId); + + return response()->json([ + 'success' => true, + 'message' => '범위가 삭제되었습니다.', + ]); + } + + /** + * 순서 변경 + */ + public function reorder(Request $request, int $formulaId): JsonResponse + { + $validated = $request->validate([ + 'range_ids' => 'required|array', + 'range_ids.*' => 'integer', + ]); + + // 모든 범위가 해당 수식에 속하는지 확인 + foreach ($validated['range_ids'] as $rangeId) { + if (! $this->rangeService->belongsToFormula($rangeId, $formulaId)) { + return response()->json([ + 'success' => false, + 'message' => '유효하지 않은 범위 ID가 포함되어 있습니다.', + ], 400); + } + } + + $this->rangeService->reorder($validated['range_ids']); + + return response()->json([ + 'success' => true, + 'message' => '순서가 변경되었습니다.', + ]); + } +} diff --git a/app/Services/Quote/QuoteFormulaRangeService.php b/app/Services/Quote/QuoteFormulaRangeService.php new file mode 100644 index 00000000..c4a98000 --- /dev/null +++ b/app/Services/Quote/QuoteFormulaRangeService.php @@ -0,0 +1,95 @@ +orderBy('sort_order') + ->get(); + } + + /** + * 범위 상세 조회 + */ + public function getRangeById(int $id): ?QuoteFormulaRange + { + return QuoteFormulaRange::find($id); + } + + /** + * 범위 생성 + */ + public function createRange(int $formulaId, array $data): QuoteFormulaRange + { + // 순서 자동 설정 + if (! isset($data['sort_order'])) { + $maxOrder = QuoteFormulaRange::where('formula_id', $formulaId)->max('sort_order') ?? 0; + $data['sort_order'] = $maxOrder + 1; + } + + return QuoteFormulaRange::create([ + 'formula_id' => $formulaId, + 'min_value' => $data['min_value'] ?? null, + 'max_value' => $data['max_value'] ?? null, + 'condition_variable' => $data['condition_variable'], + 'result_value' => $data['result_value'], + 'result_type' => $data['result_type'] ?? 'fixed', + 'sort_order' => $data['sort_order'], + ]); + } + + /** + * 범위 수정 + */ + public function updateRange(int $rangeId, array $data): QuoteFormulaRange + { + $range = QuoteFormulaRange::findOrFail($rangeId); + + $range->update([ + 'min_value' => array_key_exists('min_value', $data) ? $data['min_value'] : $range->min_value, + 'max_value' => array_key_exists('max_value', $data) ? $data['max_value'] : $range->max_value, + 'condition_variable' => $data['condition_variable'] ?? $range->condition_variable, + 'result_value' => $data['result_value'] ?? $range->result_value, + 'result_type' => $data['result_type'] ?? $range->result_type, + ]); + + return $range->fresh(); + } + + /** + * 범위 삭제 + */ + public function deleteRange(int $rangeId): void + { + QuoteFormulaRange::destroy($rangeId); + } + + /** + * 순서 변경 + */ + public function reorder(array $rangeIds): void + { + foreach ($rangeIds as $order => $id) { + QuoteFormulaRange::where('id', $id)->update(['sort_order' => $order + 1]); + } + } + + /** + * 범위가 수식에 속하는지 확인 + */ + public function belongsToFormula(int $rangeId, int $formulaId): bool + { + return QuoteFormulaRange::where('id', $rangeId) + ->where('formula_id', $formulaId) + ->exists(); + } +} diff --git a/resources/views/quote-formulas/edit.blade.php b/resources/views/quote-formulas/edit.blade.php index cffe2df6..1532b1ea 100644 --- a/resources/views/quote-formulas/edit.blade.php +++ b/resources/views/quote-formulas/edit.blade.php @@ -3,12 +3,15 @@ @section('title', '수식 수정') @section('content') -
수식 정보를 수정합니다.
++ + +
데이터를 불러오는 중...