diff --git a/app/Services/Quote/QuoteService.php b/app/Services/Quote/QuoteService.php index 495ee33..021b3f1 100644 --- a/app/Services/Quote/QuoteService.php +++ b/app/Services/Quote/QuoteService.php @@ -177,6 +177,17 @@ public function show(int $id): Quote throw new NotFoundHttpException(__('error.quote_not_found')); } + // 역방향 참조 보정: order_id가 null인데 Order.quote_id로 연결된 수주가 있는 경우 + if (! $quote->order_id) { + $linkedOrder = Order::where('quote_id', $quote->id) + ->where('tenant_id', $tenantId) + ->first(); + if ($linkedOrder) { + $quote->update(['order_id' => $linkedOrder->id]); + $quote->refresh()->load(['items', 'revisions', 'client', 'creator', 'updater', 'finalizer', 'siteBriefing.partner']); + } + } + // BOM 자재 데이터 계산 및 추가 $bomMaterials = $this->calculateBomMaterials($quote); if (! empty($bomMaterials)) { @@ -444,8 +455,19 @@ public function update(int $id, array $data): Quote $quote->refresh()->load(['items', 'revisions', 'client']); - // 연결된 수주가 있으면 동기화 - if ($quote->order_id) { + // 연결된 수주가 있으면 동기화 (역방향 참조도 포함) + $syncOrderId = $quote->order_id; + if (! $syncOrderId) { + $linkedOrder = Order::where('quote_id', $quote->id) + ->where('tenant_id', $tenantId) + ->first(); + if ($linkedOrder) { + $quote->update(['order_id' => $linkedOrder->id]); + $quote->refresh()->load(['items', 'revisions', 'client']); + $syncOrderId = $linkedOrder->id; + } + } + if ($syncOrderId) { try { $this->orderService->setContext($tenantId, $userId); $this->orderService->syncFromQuote($quote, $quote->current_revision); @@ -453,7 +475,7 @@ public function update(int $id, array $data): Quote // 수주 동기화 실패는 로그만 남기고 견적 수정은 성공 처리 Log::warning('Failed to sync order from quote', [ 'quote_id' => $quote->id, - 'order_id' => $quote->order_id, + 'order_id' => $syncOrderId, 'error' => $e->getMessage(), ]); }