tenantId(); return ItemSection::templates() ->where('tenant_id', $tenantId) ->with(['fields', 'bomItems']) ->orderBy('created_at', 'desc') ->get(); } /** * 섹션 템플릿 생성 */ public function store(array $data): ItemSection { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); $template = ItemSection::create([ 'tenant_id' => $tenantId, 'group_id' => 1, 'page_id' => null, 'title' => $data['title'], 'type' => $data['type'], 'order_no' => 0, 'is_template' => true, 'is_default' => $data['is_default'] ?? false, 'description' => $data['description'] ?? null, 'created_by' => $userId, ]); return $template->load(['fields', 'bomItems']); } /** * 섹션 템플릿 수정 */ public function update(int $id, array $data): ItemSection { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); $template = ItemSection::templates() ->where('tenant_id', $tenantId) ->where('id', $id) ->first(); if (! $template) { throw new NotFoundHttpException(__('error.section_not_found')); } $template->update([ 'title' => $data['title'] ?? $template->title, 'type' => $data['type'] ?? $template->type, 'description' => $data['description'] ?? $template->description, 'is_default' => $data['is_default'] ?? $template->is_default, 'updated_by' => $userId, ]); return $template->fresh()->load(['fields', 'bomItems']); } /** * 섹션 템플릿 삭제 */ public function destroy(int $id): void { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); $template = ItemSection::templates() ->where('tenant_id', $tenantId) ->where('id', $id) ->first(); if (! $template) { throw new NotFoundHttpException(__('error.section_not_found')); } $template->update(['deleted_by' => $userId]); $template->delete(); // 하위 필드/BOM도 Soft Delete foreach ($template->fields as $field) { $field->update(['deleted_by' => $userId]); $field->delete(); } foreach ($template->bomItems as $bomItem) { $bomItem->update(['deleted_by' => $userId]); $bomItem->delete(); } } }