update retribution calculation spatial plannings

This commit is contained in:
arifal
2025-06-17 17:58:37 +07:00
parent 236b6f9bfc
commit 6946fa7074
14 changed files with 1069 additions and 1 deletions

View File

@@ -0,0 +1,122 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
class BuildingFunction extends Model
{
protected $table = 'building_functions';
protected $fillable = ['code', 'name', 'description', 'parent_id', 'is_active', 'level', 'sort_order'];
protected $casts = [
'is_active' => '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()
];
}
}