'boolean', 'level' => 'integer', 'sort_order' => 'integer' ]; /** * Parent relationship (self-referencing) */ public function parent(): BelongsTo { return $this->belongsTo(BuildingFunction::class, 'parent_id'); } /** * Children relationship (self-referencing) */ public function children(): HasMany { return $this->hasMany(BuildingFunction::class, 'parent_id') ->where('is_active', true) ->orderBy('sort_order'); } /** * Parameters relationship (1:1) */ public function parameters(): HasOne { return $this->hasOne(BuildingFunctionParameter::class); } /** * Formula relationship (1:1) */ public function formula(): HasOne { return $this->hasOne(RetributionFormula::class); } /** * Spatial plannings relationship (1:n) - via detected building function */ public function spatialPlannings(): HasMany { return $this->hasMany(SpatialPlanning::class, 'building_function_id'); } /** * Retribution calculations relationship (1:n) - via detected building function */ public function retributionCalculations(): HasMany { return $this->hasMany(RetributionCalculation::class, 'detected_building_function_id'); } /** * Scope: Active building functions only */ public function scopeActive($query) { return $query->where('is_active', true); } /** * Scope: Parent functions only */ public function scopeParents($query) { return $query->whereNull('parent_id'); } /** * Scope: Children functions only */ public function scopeChildren($query) { return $query->whereNotNull('parent_id'); } /** * Check if building function has complete setup (parameters + formula) */ public function hasCompleteSetup(): bool { return $this->parameters()->exists() && $this->formula()->exists(); } /** * Get building function with all related data */ public function getCompleteData(): array { return [ 'id' => $this->id, 'code' => $this->code, 'name' => $this->name, 'description' => $this->description, 'parameters' => $this->parameters?->getParametersArray(), 'formula' => [ 'name' => $this->formula?->name, 'expression' => $this->formula?->formula_expression, 'description' => $this->formula?->description ], 'has_complete_setup' => $this->hasCompleteSetup() ]; } }