130 lines
3.4 KiB
PHP
130 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class RetributionCalculation extends Model
|
|
{
|
|
protected $fillable = [
|
|
'spatial_planning_id',
|
|
'retribution_formula_id',
|
|
'detected_building_function_id',
|
|
'luas_bangunan',
|
|
'used_parameters',
|
|
'used_formula',
|
|
'calculation_result',
|
|
'calculation_date',
|
|
'calculated_by',
|
|
'notes'
|
|
];
|
|
|
|
protected $casts = [
|
|
'luas_bangunan' => 'decimal:6',
|
|
'calculation_result' => 'decimal:6',
|
|
'used_parameters' => 'array',
|
|
'calculation_date' => 'datetime'
|
|
];
|
|
|
|
/**
|
|
* Spatial planning relationship (1:1)
|
|
*/
|
|
public function spatialPlanning(): BelongsTo
|
|
{
|
|
return $this->belongsTo(SpatialPlanning::class);
|
|
}
|
|
|
|
/**
|
|
* Retribution formula relationship
|
|
*/
|
|
public function retributionFormula(): BelongsTo
|
|
{
|
|
return $this->belongsTo(RetributionFormula::class);
|
|
}
|
|
|
|
/**
|
|
* Detected building function relationship
|
|
*/
|
|
public function detectedBuildingFunction(): BelongsTo
|
|
{
|
|
return $this->belongsTo(BuildingFunction::class, 'detected_building_function_id');
|
|
}
|
|
|
|
/**
|
|
* User who calculated relationship
|
|
*/
|
|
public function calculatedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'calculated_by');
|
|
}
|
|
|
|
/**
|
|
* Scope: Recent calculations
|
|
*/
|
|
public function scopeRecent($query, int $days = 30)
|
|
{
|
|
return $query->where('calculation_date', '>=', now()->subDays($days));
|
|
}
|
|
|
|
/**
|
|
* Scope: By building function
|
|
*/
|
|
public function scopeByBuildingFunction($query, int $buildingFunctionId)
|
|
{
|
|
return $query->where('detected_building_function_id', $buildingFunctionId);
|
|
}
|
|
|
|
/**
|
|
* Get formatted calculation result
|
|
*/
|
|
public function getFormattedResultAttribute(): string
|
|
{
|
|
return number_format($this->calculation_result, 2);
|
|
}
|
|
|
|
/**
|
|
* Get calculation summary
|
|
*/
|
|
public function getCalculationSummary(): array
|
|
{
|
|
return [
|
|
'spatial_planning' => $this->spatialPlanning->name ?? 'N/A',
|
|
'building_function' => $this->detectedBuildingFunction->name ?? 'N/A',
|
|
'luas_bangunan' => $this->luas_bangunan,
|
|
'formula_used' => $this->used_formula,
|
|
'parameters_used' => $this->used_parameters,
|
|
'result' => $this->calculation_result,
|
|
'calculated_date' => $this->calculation_date?->format('Y-m-d H:i:s'),
|
|
'calculated_by' => $this->calculatedBy->name ?? 'System'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Check if calculation is recent (within last 24 hours)
|
|
*/
|
|
public function isRecent(): bool
|
|
{
|
|
return $this->calculation_date && $this->calculation_date->isAfter(now()->subDay());
|
|
}
|
|
|
|
/**
|
|
* Recalculate retribution
|
|
*/
|
|
public function recalculate(): bool
|
|
{
|
|
if (!$this->spatialPlanning || !$this->retributionFormula) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$service = app(\App\Services\RetributionCalculationService::class);
|
|
$service->calculateRetribution($this->spatialPlanning);
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
\Log::error('Recalculation failed: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
}
|