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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,19 +12,23 @@ class BuildingFunctionParameter extends Model
|
||||
'fungsi_bangunan',
|
||||
'ip_permanen',
|
||||
'ip_kompleksitas',
|
||||
'ip_ketinggian',
|
||||
'indeks_lokalitas',
|
||||
'is_active',
|
||||
'notes'
|
||||
'asumsi_prasarana',
|
||||
'koefisien_dasar',
|
||||
'faktor_penyesuaian',
|
||||
'custom_parameters',
|
||||
'parameter_notes'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'fungsi_bangunan' => 'decimal:6',
|
||||
'ip_permanen' => 'decimal:6',
|
||||
'ip_kompleksitas' => 'decimal:6',
|
||||
'ip_ketinggian' => 'decimal:6',
|
||||
'indeks_lokalitas' => 'decimal:6',
|
||||
'is_active' => 'boolean'
|
||||
'asumsi_prasarana' => 'decimal:6',
|
||||
'koefisien_dasar' => 'decimal:6',
|
||||
'faktor_penyesuaian' => 'decimal:6',
|
||||
'custom_parameters' => 'array'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -52,8 +56,11 @@ class BuildingFunctionParameter extends Model
|
||||
'fungsi_bangunan' => $this->fungsi_bangunan,
|
||||
'ip_permanen' => $this->ip_permanen,
|
||||
'ip_kompleksitas' => $this->ip_kompleksitas,
|
||||
'ip_ketinggian' => $this->ip_ketinggian,
|
||||
'indeks_lokalitas' => $this->indeks_lokalitas
|
||||
'indeks_lokalitas' => $this->indeks_lokalitas,
|
||||
'asumsi_prasarana' => $this->asumsi_prasarana,
|
||||
'koefisien_dasar' => $this->koefisien_dasar,
|
||||
'faktor_penyesuaian' => $this->faktor_penyesuaian,
|
||||
'custom_parameters' => $this->custom_parameters
|
||||
];
|
||||
}
|
||||
|
||||
@@ -66,8 +73,62 @@ class BuildingFunctionParameter extends Model
|
||||
'Fungsi Bangunan' => $this->fungsi_bangunan,
|
||||
'IP Permanen' => $this->ip_permanen,
|
||||
'IP Kompleksitas' => $this->ip_kompleksitas,
|
||||
'IP Ketinggian' => $this->ip_ketinggian,
|
||||
'Indeks Lokalitas' => $this->indeks_lokalitas . '%'
|
||||
'Indeks Lokalitas' => $this->indeks_lokalitas,
|
||||
'Asumsi Prasarana' => $this->asumsi_prasarana,
|
||||
'Koefisien Dasar' => $this->koefisien_dasar,
|
||||
'Faktor Penyesuaian' => $this->faktor_penyesuaian
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate floor result using the main formula
|
||||
*/
|
||||
public function calculateFloorResult(float $ipKetinggian): float
|
||||
{
|
||||
return $this->fungsi_bangunan * (
|
||||
$this->ip_permanen +
|
||||
$this->ip_kompleksitas +
|
||||
(0.5 * $ipKetinggian)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate full retribution for given building area and floor result
|
||||
*/
|
||||
public function calculateRetribution(float $luasBangunan, float $floorResult): float
|
||||
{
|
||||
$baseValue = 70350; // Base retribution value
|
||||
$mainCalculation = 1 * $luasBangunan * ($this->indeks_lokalitas * $baseValue * $floorResult * 1);
|
||||
$additionalCalculation = 0.5 * $mainCalculation;
|
||||
|
||||
return $mainCalculation + $additionalCalculation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply custom parameters if available
|
||||
*/
|
||||
public function getParameterValue(string $key, $default = null)
|
||||
{
|
||||
// First check if it's a standard parameter
|
||||
if (property_exists($this, $key) && $this->$key !== null) {
|
||||
return $this->$key;
|
||||
}
|
||||
|
||||
// Then check custom parameters
|
||||
if ($this->custom_parameters && is_array($this->custom_parameters)) {
|
||||
return $this->custom_parameters[$key] ?? $default;
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set custom parameter
|
||||
*/
|
||||
public function setCustomParameter(string $key, $value): void
|
||||
{
|
||||
$customParams = $this->custom_parameters ?? [];
|
||||
$customParams[$key] = $value;
|
||||
$this->custom_parameters = $customParams;
|
||||
}
|
||||
}
|
||||
|
||||
56
app/Models/FloorHeightIndex.php
Normal file
56
app/Models/FloorHeightIndex.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class FloorHeightIndex extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'floor_number',
|
||||
'ip_ketinggian',
|
||||
'description'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'floor_number' => 'integer',
|
||||
'ip_ketinggian' => 'decimal:6'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get IP ketinggian by floor number
|
||||
*/
|
||||
public static function getIpKetinggianByFloor(int $floorNumber): float
|
||||
{
|
||||
$index = self::where('floor_number', $floorNumber)->first();
|
||||
return $index ? (float) $index->ip_ketinggian : 1.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all IP ketinggian mapping as array
|
||||
*/
|
||||
public static function getAllMapping(): array
|
||||
{
|
||||
return self::orderBy('floor_number')
|
||||
->pluck('ip_ketinggian', 'floor_number')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available floor numbers
|
||||
*/
|
||||
public static function getAvailableFloors(): array
|
||||
{
|
||||
return self::orderBy('floor_number')
|
||||
->pluck('floor_number')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope: By floor number
|
||||
*/
|
||||
public function scopeByFloor($query, int $floorNumber)
|
||||
{
|
||||
return $query->where('floor_number', $floorNumber);
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,17 +11,16 @@ class RetributionFormula extends Model
|
||||
protected $fillable = [
|
||||
'building_function_id',
|
||||
'name',
|
||||
'formula_expression',
|
||||
'description',
|
||||
'is_active'
|
||||
'floor_number',
|
||||
'formula_expression'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean'
|
||||
'floor_number' => 'integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* Building function relationship (1:1)
|
||||
* Building function relationship (n:1)
|
||||
*/
|
||||
public function buildingFunction(): BelongsTo
|
||||
{
|
||||
@@ -29,45 +28,47 @@ class RetributionFormula extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Retribution calculations relationship (1:n)
|
||||
* Retribution proposals relationship (1:n)
|
||||
*/
|
||||
public function retributionCalculations(): HasMany
|
||||
public function retributionProposals(): HasMany
|
||||
{
|
||||
return $this->hasMany(RetributionCalculation::class);
|
||||
return $this->hasMany(RetributionProposal::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope: Active formulas only
|
||||
* Scope: By floor number
|
||||
*/
|
||||
public function scopeActive($query)
|
||||
public function scopeByFloor($query, int $floorNumber)
|
||||
{
|
||||
return $query->where('is_active', true);
|
||||
return $query->where('floor_number', $floorNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute formula calculation
|
||||
* Execute formula calculation with parameters and IP ketinggian
|
||||
*/
|
||||
public function calculate(float $luasBangunan, array $parameters): float
|
||||
public function calculate(float $luasBangunan, array $parameters, float $ipKetinggian): float
|
||||
{
|
||||
// Replace placeholders in formula with actual values
|
||||
$formula = $this->formula_expression;
|
||||
// Extract parameters
|
||||
$fungsi_bangunan = $parameters['fungsi_bangunan'] ?? 0;
|
||||
$ip_permanen = $parameters['ip_permanen'] ?? 0;
|
||||
$ip_kompleksitas = $parameters['ip_kompleksitas'] ?? 0;
|
||||
$indeks_lokalitas = $parameters['indeks_lokalitas'] ?? 0;
|
||||
|
||||
// Calculate H13 (floor coefficient) using Excel formula
|
||||
$h13 = $fungsi_bangunan * ($ip_permanen + $ip_kompleksitas + (0.5 * $ipKetinggian));
|
||||
|
||||
// Calculate full retribution using Excel formula
|
||||
$baseValue = 70350;
|
||||
$additionalFactor = 0.5;
|
||||
|
||||
// Replace luas_bangunan
|
||||
$formula = str_replace('luas_bangunan', $luasBangunan, $formula);
|
||||
// Main calculation: (1*D13*(N13*70350*H13*1))
|
||||
$mainCalculation = 1 * $luasBangunan * ($indeks_lokalitas * $baseValue * $h13 * 1);
|
||||
|
||||
// Replace parameter values
|
||||
foreach ($parameters as $key => $value) {
|
||||
$formula = str_replace($key, $value, $formula);
|
||||
}
|
||||
// Additional calculation: ($O$3*(1*D13*(N13*70350*H13*1)))
|
||||
$additionalCalculation = $additionalFactor * $mainCalculation;
|
||||
|
||||
// Evaluate the mathematical expression
|
||||
// Note: In production, use a safer math expression evaluator
|
||||
try {
|
||||
$result = eval("return $formula;");
|
||||
return (float) $result;
|
||||
} catch (Exception $e) {
|
||||
throw new \Exception("Error calculating formula: " . $e->getMessage());
|
||||
}
|
||||
// Total: main + additional
|
||||
return $mainCalculation + $additionalCalculation;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,19 +88,140 @@ class RetributionFormula extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate formula expression
|
||||
* Check if this formula applies to given floor number
|
||||
*/
|
||||
public function appliesTo(int $floorNumber): bool
|
||||
{
|
||||
// If floor_number is 0, formula applies to all floors
|
||||
if ($this->floor_number === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Otherwise, exact match required
|
||||
return $this->floor_number == $floorNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get human readable floor description
|
||||
*/
|
||||
public function getFloorDescription(): string
|
||||
{
|
||||
if ($this->floor_number === 0) {
|
||||
return 'Semua lantai';
|
||||
}
|
||||
|
||||
return "Lantai {$this->floor_number}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default formula expression
|
||||
*/
|
||||
public static function getDefaultFormula(): string
|
||||
{
|
||||
return '(fungsi_bangunan * (ip_permanen + ip_kompleksitas + (0.5 * ip_ketinggian)))';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate formula expression syntax
|
||||
*/
|
||||
public function validateFormula(): bool
|
||||
{
|
||||
// Basic validation - check if formula contains required elements
|
||||
$requiredElements = ['luas_bangunan'];
|
||||
// Basic validation - check if formula contains required variables
|
||||
$requiredVariables = ['fungsi_bangunan', 'ip_permanen', 'ip_kompleksitas', 'ip_ketinggian'];
|
||||
|
||||
foreach ($requiredElements as $element) {
|
||||
if (strpos($this->formula_expression, $element) === false) {
|
||||
foreach ($requiredVariables as $variable) {
|
||||
if (strpos($this->formula_expression, $variable) === false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get calculation breakdown for debugging
|
||||
*/
|
||||
public function getCalculationBreakdown(float $luasBangunan, array $parameters, float $ipKetinggian): array
|
||||
{
|
||||
// Extract parameters
|
||||
$fungsi_bangunan = $parameters['fungsi_bangunan'] ?? 0;
|
||||
$ip_permanen = $parameters['ip_permanen'] ?? 0;
|
||||
$ip_kompleksitas = $parameters['ip_kompleksitas'] ?? 0;
|
||||
$indeks_lokalitas = $parameters['indeks_lokalitas'] ?? 0;
|
||||
|
||||
// Calculate H13 (floor coefficient)
|
||||
$h13 = $fungsi_bangunan * ($ip_permanen + $ip_kompleksitas + (0.5 * $ipKetinggian));
|
||||
|
||||
// Calculate components
|
||||
$baseValue = 70350;
|
||||
$additionalFactor = 0.5;
|
||||
$mainCalculation = 1 * $luasBangunan * ($indeks_lokalitas * $baseValue * $h13 * 1);
|
||||
$additionalCalculation = $additionalFactor * $mainCalculation;
|
||||
$totalCalculation = $mainCalculation + $additionalCalculation;
|
||||
|
||||
return [
|
||||
'input_parameters' => [
|
||||
'luas_bangunan' => $luasBangunan,
|
||||
'fungsi_bangunan' => $fungsi_bangunan,
|
||||
'ip_permanen' => $ip_permanen,
|
||||
'ip_kompleksitas' => $ip_kompleksitas,
|
||||
'ip_ketinggian' => $ipKetinggian,
|
||||
'indeks_lokalitas' => $indeks_lokalitas,
|
||||
'base_value' => $baseValue,
|
||||
'additional_factor' => $additionalFactor
|
||||
],
|
||||
'calculation_steps' => [
|
||||
'h13_calculation' => [
|
||||
'formula' => 'fungsi_bangunan * (ip_permanen + ip_kompleksitas + (0.5 * ip_ketinggian))',
|
||||
'calculation' => "{$fungsi_bangunan} * ({$ip_permanen} + {$ip_kompleksitas} + (0.5 * {$ipKetinggian}))",
|
||||
'result' => $h13
|
||||
],
|
||||
'main_calculation' => [
|
||||
'formula' => '1 * luas_bangunan * (indeks_lokalitas * base_value * h13 * 1)',
|
||||
'calculation' => "1 * {$luasBangunan} * ({$indeks_lokalitas} * {$baseValue} * {$h13} * 1)",
|
||||
'result' => $mainCalculation
|
||||
],
|
||||
'additional_calculation' => [
|
||||
'formula' => 'additional_factor * main_calculation',
|
||||
'calculation' => "{$additionalFactor} * {$mainCalculation}",
|
||||
'result' => $additionalCalculation
|
||||
],
|
||||
'total_calculation' => [
|
||||
'formula' => 'main_calculation + additional_calculation',
|
||||
'calculation' => "{$mainCalculation} + {$additionalCalculation}",
|
||||
'result' => $totalCalculation
|
||||
]
|
||||
],
|
||||
'formatted_results' => [
|
||||
'h13' => number_format($h13, 6),
|
||||
'main_calculation' => 'Rp ' . number_format($mainCalculation, 2),
|
||||
'additional_calculation' => 'Rp ' . number_format($additionalCalculation, 2),
|
||||
'total_calculation' => 'Rp ' . number_format($totalCalculation, 2)
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this formula has been used in any proposals
|
||||
*/
|
||||
public function hasProposals(): bool
|
||||
{
|
||||
return $this->retributionProposals()->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total amount calculated using this formula
|
||||
*/
|
||||
public function getTotalCalculatedAmount(): float
|
||||
{
|
||||
return $this->retributionProposals()->sum('total_retribution_amount');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count of proposals using this formula
|
||||
*/
|
||||
public function getProposalCount(): int
|
||||
{
|
||||
return $this->retributionProposals()->count();
|
||||
}
|
||||
}
|
||||
|
||||
187
app/Models/RetributionProposal.php
Normal file
187
app/Models/RetributionProposal.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RetributionProposal extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'spatial_planning_id',
|
||||
'building_function_id',
|
||||
'retribution_formula_id',
|
||||
'proposal_number',
|
||||
'floor_number',
|
||||
'floor_area',
|
||||
'total_building_area',
|
||||
'ip_ketinggian',
|
||||
'floor_retribution_amount',
|
||||
'total_retribution_amount',
|
||||
'calculation_parameters',
|
||||
'calculation_breakdown',
|
||||
'notes',
|
||||
'calculated_at'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'floor_area' => 'decimal:6',
|
||||
'total_building_area' => 'decimal:6',
|
||||
'ip_ketinggian' => 'decimal:6',
|
||||
'floor_retribution_amount' => 'decimal:2',
|
||||
'total_retribution_amount' => 'decimal:2',
|
||||
'calculation_parameters' => 'array',
|
||||
'calculation_breakdown' => 'array',
|
||||
'calculated_at' => 'datetime'
|
||||
];
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Relationship with SpatialPlanning
|
||||
*/
|
||||
public function spatialPlanning(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SpatialPlanning::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship with BuildingFunction
|
||||
*/
|
||||
public function buildingFunction(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(BuildingFunction::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship with RetributionFormula
|
||||
*/
|
||||
public function retributionFormula(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(RetributionFormula::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate proposal number
|
||||
*/
|
||||
public static function generateProposalNumber(): string
|
||||
{
|
||||
$year = now()->format('Y');
|
||||
$month = now()->format('m');
|
||||
|
||||
// Use max ID + 1 to avoid duplicates when records are deleted
|
||||
$maxId = static::whereYear('created_at', now()->year)
|
||||
->whereMonth('created_at', now()->month)
|
||||
->max('id') ?? 0;
|
||||
$nextNumber = $maxId + 1;
|
||||
|
||||
// Fallback: if still duplicate, use timestamp
|
||||
$proposalNumber = sprintf('RP-%s%s-%04d', $year, $month, $nextNumber);
|
||||
|
||||
// Check if exists and increment until unique
|
||||
$counter = $nextNumber;
|
||||
while (static::where('proposal_number', $proposalNumber)->exists()) {
|
||||
$counter++;
|
||||
$proposalNumber = sprintf('RP-%s%s-%04d', $year, $month, $counter);
|
||||
}
|
||||
|
||||
return $proposalNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get formatted floor retribution amount
|
||||
*/
|
||||
public function getFormattedFloorAmountAttribute(): string
|
||||
{
|
||||
return 'Rp ' . number_format($this->floor_retribution_amount, 0, ',', '.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get formatted total retribution amount
|
||||
*/
|
||||
public function getFormattedTotalAmountAttribute(): string
|
||||
{
|
||||
return 'Rp ' . number_format($this->total_retribution_amount, 0, ',', '.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get calculation breakdown for specific parameter
|
||||
*/
|
||||
public function getCalculationBreakdownFor(string $parameter)
|
||||
{
|
||||
return $this->calculation_breakdown[$parameter] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parameter value
|
||||
*/
|
||||
public function getParameterValue(string $parameter)
|
||||
{
|
||||
return $this->calculation_parameters[$parameter] ?? null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Scope for filtering by building function
|
||||
*/
|
||||
public function scopeByBuildingFunction($query, int $buildingFunctionId)
|
||||
{
|
||||
return $query->where('building_function_id', $buildingFunctionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope for filtering by spatial planning
|
||||
*/
|
||||
public function scopeBySpatialPlanning($query, int $spatialPlanningId)
|
||||
{
|
||||
return $query->where('spatial_planning_id', $spatialPlanningId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total retribution amount for multiple proposals
|
||||
*/
|
||||
public static function getTotalAmount($proposals = null): float
|
||||
{
|
||||
if ($proposals) {
|
||||
return $proposals->sum('total_retribution_amount');
|
||||
}
|
||||
|
||||
return static::sum('total_retribution_amount');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get basic statistics
|
||||
*/
|
||||
public static function getBasicStats(): array
|
||||
{
|
||||
return [
|
||||
'total_count' => static::count(),
|
||||
'total_amount' => static::sum('total_retribution_amount'),
|
||||
'average_amount' => static::avg('total_retribution_amount'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot method to generate proposal number
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
if (empty($model->proposal_number)) {
|
||||
$model->proposal_number = static::generateProposalNumber();
|
||||
}
|
||||
|
||||
if (empty($model->calculated_at)) {
|
||||
$model->calculated_at = now();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
@@ -44,11 +45,11 @@ class SpatialPlanning extends Model
|
||||
];
|
||||
|
||||
/**
|
||||
* Retribution calculation relationship (1:1)
|
||||
* Retribution proposals relationship (1:many)
|
||||
*/
|
||||
public function retributionCalculation(): HasOne
|
||||
public function retributionProposals(): HasMany
|
||||
{
|
||||
return $this->hasOne(RetributionCalculation::class);
|
||||
return $this->hasMany(RetributionProposal::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,11 +61,27 @@ class SpatialPlanning extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if spatial planning has retribution calculation
|
||||
* Check if spatial planning has retribution proposals
|
||||
*/
|
||||
public function hasRetributionCalculation(): bool
|
||||
public function hasRetributionProposals(): bool
|
||||
{
|
||||
return $this->retributionCalculation()->exists();
|
||||
return $this->retributionProposals()->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get latest retribution proposal
|
||||
*/
|
||||
public function getLatestRetributionProposal()
|
||||
{
|
||||
return $this->retributionProposals()->latest()->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all retribution proposals
|
||||
*/
|
||||
public function getAllRetributionProposals()
|
||||
{
|
||||
return $this->retributionProposals()->get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,18 +101,20 @@ class SpatialPlanning extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope: Without retribution calculation
|
||||
* Scope: Without retribution proposals
|
||||
*/
|
||||
public function scopeWithoutRetributionCalculation($query)
|
||||
public function scopeWithoutRetributionProposals($query)
|
||||
{
|
||||
return $query->whereDoesntHave('retributionCalculation');
|
||||
return $query->whereDoesntHave('retributionProposals');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope: With retribution calculation
|
||||
* Scope: With retribution proposals
|
||||
*/
|
||||
public function scopeWithRetributionCalculation($query)
|
||||
public function scopeWithRetributionProposals($query)
|
||||
{
|
||||
return $query->whereHas('retributionCalculation');
|
||||
return $query->whereHas('retributionProposals');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user