fix:가망고객 모드에서 계약상품 선택 표시 및 저장 지원

- scenario-step.blade.php: 가망고객 모드에서도 상품 선택 UI 표시
- product-selection.blade.php: 가망고객/테넌트 모드 공통 지원
  - management_id 기반으로 상품 조회/저장
  - isProspect 플래그로 모드 구분
- SalesContractController: prospect_id 지원 추가
  - tenant_id 또는 prospect_id 중 하나로 상품 저장 가능
  - 카테고리별 상품 삭제 후 저장으로 변경

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
김보곤
2026-01-31 20:40:16 +09:00
parent 28f129393d
commit 7d0ead3079
3 changed files with 87 additions and 23 deletions

View File

@@ -16,11 +16,15 @@ class SalesContractController extends Controller
{
/**
* 계약 상품 저장 (전체 교체 방식)
* - tenant_id 또는 prospect_id 중 하나 필수
*/
public function saveProducts(Request $request): JsonResponse
{
$validated = $request->validate([
'tenant_id' => 'required|exists:tenants,id',
'tenant_id' => 'nullable|exists:tenants,id',
'prospect_id' => 'nullable|exists:tenant_prospects,id',
'management_id' => 'nullable|exists:sales_tenant_managements,id',
'category_id' => 'required|exists:sales_product_categories,id',
'products' => 'required|array',
'products.*.product_id' => 'required|exists:sales_products,id',
'products.*.category_id' => 'required|exists:sales_product_categories,id',
@@ -28,15 +32,34 @@ public function saveProducts(Request $request): JsonResponse
'products.*.subscription_fee' => 'required|numeric|min:0',
]);
// tenant_id 또는 prospect_id 중 하나는 필수
if (empty($validated['tenant_id']) && empty($validated['prospect_id'])) {
return response()->json([
'success' => false,
'message' => 'tenant_id 또는 prospect_id가 필요합니다.',
], 422);
}
try {
DB::transaction(function () use ($validated) {
$tenantId = $validated['tenant_id'];
$managementId = null;
DB::transaction(function () use ($validated, &$managementId) {
$tenantId = $validated['tenant_id'] ?? null;
$prospectId = $validated['prospect_id'] ?? null;
$categoryId = $validated['category_id'];
// 영업관리 레코드 조회 (없으면 생성)
$management = SalesTenantManagement::findOrCreateByTenant($tenantId);
if ($tenantId) {
$management = SalesTenantManagement::findOrCreateByTenant($tenantId);
} else {
$management = SalesTenantManagement::findOrCreateByProspect($prospectId);
}
$managementId = $management->id;
// 기존 상품 삭제
SalesContractProduct::where('tenant_id', $tenantId)->delete();
// 해당 카테고리의 기존 상품 삭제
SalesContractProduct::where('management_id', $management->id)
->where('category_id', $categoryId)
->delete();
// 새 상품 저장
foreach ($validated['products'] as $product) {
@@ -51,16 +74,22 @@ public function saveProducts(Request $request): JsonResponse
'created_by' => auth()->id(),
]);
}
// 총 가입비 업데이트
$totalRegistrationFee = SalesContractProduct::where('management_id', $management->id)
->sum('registration_fee');
$management->update(['total_registration_fee' => $totalRegistrationFee]);
});
return response()->json([
'success' => true,
'message' => '계약 상품이 저장되었습니다.',
'management_id' => $managementId,
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => '저장 중 오류가 발생했습니다.',
'message' => '저장 중 오류가 발생했습니다: ' . $e->getMessage(),
], 500);
}
}