add spatial plannings retribution calculations
This commit is contained in:
@@ -10,12 +10,21 @@ 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 $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
'description',
|
||||
'parent_id',
|
||||
'level',
|
||||
'sort_order',
|
||||
'base_tariff'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'level' => 'integer',
|
||||
'sort_order' => 'integer'
|
||||
'sort_order' => 'integer',
|
||||
'base_tariff' => 'decimal:2'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -32,48 +41,41 @@ class BuildingFunction extends Model
|
||||
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
|
||||
public function parameter(): HasOne
|
||||
{
|
||||
return $this->hasOne(BuildingFunctionParameter::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formula relationship (1:1)
|
||||
* Formulas relationship (1:n) - multiple formulas for different floor numbers
|
||||
*/
|
||||
public function formula(): HasOne
|
||||
public function formulas(): HasMany
|
||||
{
|
||||
return $this->hasOne(RetributionFormula::class);
|
||||
return $this->hasMany(RetributionFormula::class)->orderBy('floor_number');
|
||||
}
|
||||
|
||||
/**
|
||||
* Spatial plannings relationship (1:n) - via detected building function
|
||||
* Get appropriate formula based on floor number
|
||||
*/
|
||||
public function spatialPlannings(): HasMany
|
||||
public function getFormulaForFloor(int $floorNumber): ?RetributionFormula
|
||||
{
|
||||
return $this->hasMany(SpatialPlanning::class, 'building_function_id');
|
||||
return $this->formulas()
|
||||
->where('floor_number', $floorNumber)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retribution calculations relationship (1:n) - via detected building function
|
||||
* Retribution proposals relationship (1:n)
|
||||
*/
|
||||
public function retributionCalculations(): HasMany
|
||||
public function retributionProposals(): 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);
|
||||
return $this->hasMany(RetributionProposal::class, 'building_function_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,11 +95,19 @@ class BuildingFunction extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if building function has complete setup (parameters + formula)
|
||||
* Scope: By level
|
||||
*/
|
||||
public function scopeByLevel($query, int $level)
|
||||
{
|
||||
return $query->where('level', $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if building function has complete setup (parameters + at least one formula)
|
||||
*/
|
||||
public function hasCompleteSetup(): bool
|
||||
{
|
||||
return $this->parameters()->exists() && $this->formula()->exists();
|
||||
return $this->parameter()->exists() && $this->formulas()->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,13 +120,48 @@ class BuildingFunction extends Model
|
||||
'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
|
||||
],
|
||||
'level' => $this->level,
|
||||
'sort_order' => $this->sort_order,
|
||||
'base_tariff' => $this->base_tariff,
|
||||
'parameters' => $this->parameter?->getParametersArray(),
|
||||
'formulas' => $this->formulas->map(function ($formula) {
|
||||
return [
|
||||
'id' => $formula->id,
|
||||
'name' => $formula->name,
|
||||
'expression' => $formula->formula_expression,
|
||||
'floor_number' => $formula->floor_number,
|
||||
'floor_description' => $this->getFloorDescription($formula->floor_number)
|
||||
];
|
||||
})->toArray(),
|
||||
'has_complete_setup' => $this->hasCompleteSetup()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get floor description
|
||||
*/
|
||||
public function getFloorDescription(?int $floorNumber): string
|
||||
{
|
||||
if ($floorNumber === null || $floorNumber === 0) {
|
||||
return 'Semua lantai';
|
||||
}
|
||||
|
||||
return "Lantai {$floorNumber}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is a parent function
|
||||
*/
|
||||
public function isParent(): bool
|
||||
{
|
||||
return $this->parent_id === null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is a child function
|
||||
*/
|
||||
public function isChild(): bool
|
||||
{
|
||||
return $this->parent_id !== null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user