restructure retribution calculations table

This commit is contained in:
arifal hidayat
2025-06-18 22:53:44 +07:00
parent df70a47bd1
commit 4c3443c2d6
12 changed files with 1548 additions and 0 deletions

View File

@@ -0,0 +1,263 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Services\RetributionCalculatorService;
use App\Models\BuildingType;
class TestRetributionCalculation extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'retribution:test
{--area= : Luas bangunan dalam m2}
{--floor= : Jumlah lantai (1-6)}
{--type= : ID atau kode building type}
{--all : Test semua building types}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test perhitungan retribusi PBG dengan input luas bangunan dan tinggi lantai';
protected $calculatorService;
public function __construct(RetributionCalculatorService $calculatorService)
{
parent::__construct();
$this->calculatorService = $calculatorService;
}
/**
* Execute the console command.
*/
public function handle()
{
$this->info('🏢 SISTEM TEST PERHITUNGAN RETRIBUSI PBG');
$this->info('=' . str_repeat('=', 50));
// Test all building types if --all flag is used
if ($this->option('all')) {
return $this->testAllBuildingTypes();
}
// Get input parameters
$area = $this->getArea();
$floor = $this->getFloor();
$buildingTypeId = $this->getBuildingType();
if (!$area || !$floor || !$buildingTypeId) {
$this->error('❌ Parameter tidak lengkap!');
return 1;
}
// Perform calculation
$this->performCalculation($buildingTypeId, $floor, $area);
return 0;
}
protected function getArea()
{
$area = $this->option('area');
if (!$area) {
$area = $this->ask('📐 Masukkan luas bangunan (m²)');
}
if (!is_numeric($area) || $area <= 0) {
$this->error('❌ Luas bangunan harus berupa angka positif!');
return null;
}
return (float) $area;
}
protected function getFloor()
{
$floor = $this->option('floor');
if (!$floor) {
$floor = $this->ask('🏗️ Masukkan jumlah lantai (1-6)');
}
if (!is_numeric($floor) || $floor < 1 || $floor > 6) {
$this->error('❌ Jumlah lantai harus antara 1-6!');
return null;
}
return (int) $floor;
}
protected function getBuildingType()
{
$type = $this->option('type');
if (!$type) {
$this->showBuildingTypes();
$type = $this->ask('🏢 Masukkan ID atau kode building type');
}
// Try to find by ID first, then by code
$buildingType = null;
if (is_numeric($type)) {
$buildingType = BuildingType::find($type);
} else {
$buildingType = BuildingType::where('code', strtoupper($type))->first();
}
if (!$buildingType) {
$this->error('❌ Building type tidak ditemukan!');
return null;
}
return $buildingType->id;
}
protected function showBuildingTypes()
{
$this->info('📋 DAFTAR BUILDING TYPES:');
$this->line('');
$buildingTypes = BuildingType::with('indices')
->whereHas('indices') // Only types that have indices
->get();
$headers = ['ID', 'Kode', 'Nama', 'Coefficient', 'Free'];
$rows = [];
foreach ($buildingTypes as $type) {
$rows[] = [
$type->id,
$type->code,
$type->name,
$type->indices ? number_format($type->indices->coefficient, 4) : 'N/A',
$type->is_free ? '✅' : '❌'
];
}
$this->table($headers, $rows);
$this->line('');
}
protected function performCalculation($buildingTypeId, $floor, $area)
{
try {
$result = $this->calculatorService->calculate($buildingTypeId, $floor, $area, false);
$this->displayResults($result, $area, $floor);
} catch (\Exception $e) {
$this->error('❌ Error: ' . $e->getMessage());
return 1;
}
}
protected function displayResults($result, $area, $floor)
{
$this->info('');
$this->info('📊 HASIL PERHITUNGAN RETRIBUSI');
$this->info('=' . str_repeat('=', 40));
// Building info
$this->line('🏢 <fg=cyan>Building Type:</> ' . $result['building_type']['name']);
$this->line('📐 <fg=cyan>Luas Bangunan:</> ' . number_format($area, 0) . ' m²');
$this->line('🏗️ <fg=cyan>Jumlah Lantai:</> ' . $floor);
if (isset($result['building_type']['is_free']) && $result['building_type']['is_free']) {
$this->line('');
$this->info('🎉 GRATIS - Building type ini tidak dikenakan retribusi');
$this->line('💰 <fg=green>Total Retribusi: Rp 0</fg=green>');
return;
}
$this->line('');
// Parameters
$this->info('📋 PARAMETER PERHITUNGAN:');
$indices = $result['indices'];
$this->line('• Coefficient: ' . number_format($indices['coefficient'], 4));
$this->line('• IP Permanent: ' . number_format($indices['ip_permanent'], 4));
$this->line('• IP Complexity: ' . number_format($indices['ip_complexity'], 4));
$this->line('• Locality Index: ' . number_format($indices['locality_index'], 4));
$this->line('• Height Index: ' . number_format($result['input_parameters']['height_index'], 4));
$this->line('');
// Calculation steps
$this->info('🔢 LANGKAH PERHITUNGAN:');
$detail = $result['calculation_detail'];
$this->line('1. H5 Raw: ' . number_format($detail['h5_raw'], 6));
$this->line('2. H5 Rounded: ' . number_format($detail['h5'], 4));
$this->line('3. Main Calculation: Rp ' . number_format($detail['main'], 2));
$this->line('4. Infrastructure (50%): Rp ' . number_format($detail['infrastructure'], 2));
$this->line('');
// Final result
$this->info('💰 <fg=green>TOTAL RETRIBUSI: ' . $result['formatted_amount'] . '</fg=green>');
$this->line('📈 <fg=yellow>Per m²: Rp ' . number_format($result['total_retribution'] / $area, 2) . '</fg=yellow>');
}
protected function testAllBuildingTypes()
{
$area = $this->option('area') ?: 100;
$floor = $this->option('floor') ?: 2;
$this->info("🧪 TESTING SEMUA BUILDING TYPES");
$this->info("📐 Luas: {$area} m² | 🏗️ Lantai: {$floor}");
$this->info('=' . str_repeat('=', 60));
$buildingTypes = BuildingType::with('indices')
->whereHas('indices') // Only types that have indices
->orderBy('level')
->orderBy('name')
->get();
$headers = ['Kode', 'Nama', 'Coefficient', 'Total Retribusi', 'Per m²'];
$rows = [];
foreach ($buildingTypes as $type) {
try {
$result = $this->calculatorService->calculate($type->id, $floor, $area, false);
if ($type->is_free) {
$rows[] = [
$type->code,
$type->name,
'FREE',
'Rp 0',
'Rp 0'
];
} else {
$rows[] = [
$type->code,
$type->name,
number_format($result['indices']['coefficient'], 4),
'Rp ' . number_format($result['total_retribution'], 0),
'Rp ' . number_format($result['total_retribution'] / $area, 0)
];
}
} catch (\Exception $e) {
$rows[] = [
$type->code,
$type->name,
'ERROR',
$e->getMessage(),
'-'
];
}
}
$this->table($headers, $rows);
return 0;
}
}

View File

@@ -0,0 +1,291 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\BuildingType;
use App\Models\RetributionIndex;
use App\Models\HeightIndex;
use App\Models\RetributionConfig;
use App\Models\RetributionCalculation;
use App\Services\RetributionCalculatorService;
class TestRetributionData extends Command
{
protected $signature = 'retribution:data
{--save : Save calculation results to database}
{--show : Show existing data and relations}
{--clear : Clear calculation history}';
protected $description = 'Test retribution data storage and display database relations';
protected $calculatorService;
public function __construct(RetributionCalculatorService $calculatorService)
{
parent::__construct();
$this->calculatorService = $calculatorService;
}
public function handle()
{
$this->info('🗄️ SISTEM TEST DATA & RELASI RETRIBUSI PBG');
$this->info('=' . str_repeat('=', 55));
if ($this->option('clear')) {
$this->clearCalculationHistory();
return;
}
if ($this->option('show')) {
$this->showExistingData();
return;
}
if ($this->option('save')) {
$this->saveTestCalculations();
}
$this->showDatabaseStructure();
$this->showSampleData();
$this->showRelations();
}
protected function saveTestCalculations()
{
$this->info('💾 MENYIMPAN SAMPLE CALCULATIONS...');
$this->line('');
$testCases = [
['type_code' => 'KEAGAMAAN', 'area' => 200, 'floor' => 1],
['type_code' => 'SOSBUDAYA', 'area' => 150, 'floor' => 2],
['type_code' => 'CAMP_KECIL', 'area' => 1, 'floor' => 1],
['type_code' => 'UMKM', 'area' => 100, 'floor' => 2],
['type_code' => 'HUN_SEDH', 'area' => 80, 'floor' => 1],
['type_code' => 'USH_BESAR', 'area' => 500, 'floor' => 3],
];
foreach ($testCases as $case) {
$buildingType = BuildingType::where('code', $case['type_code'])->first();
if (!$buildingType) {
$this->warn("⚠️ Building type {$case['type_code']} not found");
continue;
}
$result = $this->calculatorService->calculate(
$buildingType->id,
$case['floor'],
$case['area']
);
// Save to database
RetributionCalculation::create([
'calculation_id' => 'TST' . now()->format('ymdHis') . rand(10, 99),
'building_type_id' => $buildingType->id,
'floor_number' => $case['floor'],
'building_area' => $case['area'],
'retribution_amount' => $result['total_retribution'],
'calculation_detail' => json_encode($result),
'calculated_at' => now()
]);
$this->info("✅ Saved: {$buildingType->name} - {$case['area']}m² - {$case['floor']} lantai - Rp " . number_format($result['total_retribution']));
}
$this->line('');
$this->info('💾 Sample calculations saved successfully!');
$this->line('');
}
protected function showExistingData()
{
$this->info('📊 DATA YANG TERSIMPAN DI DATABASE');
$this->info('=' . str_repeat('=', 40));
$calculations = RetributionCalculation::with('buildingType')
->orderBy('created_at', 'desc')
->limit(10)
->get();
if ($calculations->isEmpty()) {
$this->warn('❌ Tidak ada data calculation yang tersimpan');
$this->info('💡 Gunakan --save untuk menyimpan sample data');
return;
}
$headers = ['ID', 'Building Type', 'Area', 'Floor', 'Amount', 'Created'];
$rows = [];
foreach ($calculations as $calc) {
$rows[] = [
substr($calc->calculation_id, -8),
$calc->buildingType->name ?? 'N/A',
$calc->building_area . ' m²',
$calc->floor_number,
'Rp ' . number_format($calc->retribution_amount),
$calc->created_at->format('d/m H:i')
];
}
$this->table($headers, $rows);
}
protected function clearCalculationHistory()
{
$count = RetributionCalculation::count();
if ($count === 0) {
$this->info(' Tidak ada data calculation untuk dihapus');
return;
}
if ($this->confirm("🗑️ Hapus {$count} calculation records?")) {
RetributionCalculation::truncate();
$this->info("{$count} calculation records berhasil dihapus");
}
}
protected function showDatabaseStructure()
{
$this->info('🏗️ STRUKTUR DATABASE RETRIBUSI');
$this->info('=' . str_repeat('=', 35));
$tables = [
'building_types' => 'Hierarki dan metadata building types',
'retribution_indices' => 'Parameter perhitungan (coefficient, IP, dll)',
'height_indices' => 'Koefisien tinggi berdasarkan lantai',
'retribution_configs' => 'Konfigurasi global (base value, dll)',
'retribution_calculations' => 'History perhitungan dan hasil'
];
foreach ($tables as $table => $description) {
$this->line("📋 <info>{$table}</info>: {$description}");
}
$this->line('');
}
protected function showSampleData()
{
$this->info('📋 SAMPLE DATA DARI SETIAP TABEL');
$this->info('=' . str_repeat('=', 35));
// Building Types
$this->line('<comment>🏢 BUILDING TYPES:</comment>');
$buildingTypes = BuildingType::select('id', 'code', 'name', 'level', 'is_free')
->orderBy('level')
->orderBy('name')
->get();
$headers = ['ID', 'Code', 'Name', 'Level', 'Free'];
$rows = [];
foreach ($buildingTypes->take(5) as $type) {
$rows[] = [
$type->id,
$type->code,
substr($type->name, 0, 25) . '...',
$type->level,
$type->is_free ? '✅' : '❌'
];
}
$this->table($headers, $rows);
// Retribution Indices
$this->line('<comment>📊 RETRIBUTION INDICES:</comment>');
$indices = RetributionIndex::with('buildingType')
->select('building_type_id', 'coefficient', 'ip_permanent', 'ip_complexity', 'locality_index')
->get();
$headers = ['Building Type', 'Coefficient', 'IP Permanent', 'IP Complexity', 'Locality'];
$rows = [];
foreach ($indices->take(5) as $index) {
$rows[] = [
$index->buildingType->code ?? 'N/A',
number_format($index->coefficient, 4),
number_format($index->ip_permanent, 4),
number_format($index->ip_complexity, 4),
number_format($index->locality_index, 4)
];
}
$this->table($headers, $rows);
// Height Indices
$this->line('<comment>🏗️ HEIGHT INDICES:</comment>');
$heights = HeightIndex::orderBy('floor_number')->get();
$headers = ['Floor', 'Height Index'];
$rows = [];
foreach ($heights as $height) {
$rows[] = [
$height->floor_number,
number_format($height->height_index, 4)
];
}
$this->table($headers, $rows);
// Retribution Configs
$this->line('<comment>⚙️ RETRIBUTION CONFIGS:</comment>');
$configs = RetributionConfig::all();
$headers = ['Key', 'Value', 'Description'];
$rows = [];
foreach ($configs as $config) {
$rows[] = [
$config->key,
$config->value,
substr($config->description ?? '', 0, 30) . '...'
];
}
$this->table($headers, $rows);
}
protected function showRelations()
{
$this->info('🔗 RELASI ANTAR TABEL');
$this->info('=' . str_repeat('=', 25));
// Test relations dengan sample data
$buildingType = BuildingType::with(['indices', 'calculations'])
->where('code', 'UMKM')
->first();
if (!$buildingType) {
$this->warn('⚠️ Sample building type tidak ditemukan');
return;
}
$this->line("<comment>🏢 Building Type: {$buildingType->name}</comment>");
$this->line(" 📋 Code: {$buildingType->code}");
$this->line(" 📊 Level: {$buildingType->level}");
$this->line(" 🆓 Free: " . ($buildingType->is_free ? 'Ya' : 'Tidak'));
// Show indices relation
if ($buildingType->indices) {
$index = $buildingType->indices;
$this->line(" <comment>📊 Retribution Index:</comment>");
$this->line(" 💰 Coefficient: " . number_format($index->coefficient, 4));
$this->line(" 🏗️ IP Permanent: " . number_format($index->ip_permanent, 4));
$this->line(" 🔧 IP Complexity: " . number_format($index->ip_complexity, 4));
$this->line(" 📍 Locality Index: " . number_format($index->locality_index, 4));
}
// Show calculations relation
$calculationsCount = $buildingType->calculations()->count();
$this->line(" <comment>📈 Calculations: {$calculationsCount} records</comment>");
if ($calculationsCount > 0) {
$latestCalc = $buildingType->calculations()->latest()->first();
$this->line(" 📅 Latest: " . $latestCalc->created_at->format('d/m/Y H:i'));
$this->line(" 💰 Amount: Rp " . number_format($latestCalc->retribution_amount));
}
$this->line('');
$this->info('🎯 KESIMPULAN RELASI:');
$this->line(' • BuildingType hasOne RetributionIndex');
$this->line(' • BuildingType hasMany RetributionCalculations');
$this->line(' • RetributionCalculation belongsTo BuildingType');
$this->line(' • HeightIndex independent (digunakan berdasarkan floor_number)');
$this->line(' • RetributionConfig global settings');
}
}

131
app/Models/BuildingType.php Normal file
View File

@@ -0,0 +1,131 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
class BuildingType extends Model
{
protected $fillable = [
'code',
'name',
'parent_id',
'level',
'is_free',
'is_active'
];
protected $casts = [
'level' => 'integer',
'is_free' => 'boolean',
'is_active' => 'boolean'
];
/**
* Parent relationship
*/
public function parent(): BelongsTo
{
return $this->belongsTo(BuildingType::class, 'parent_id');
}
/**
* Children relationship
*/
public function children(): HasMany
{
return $this->hasMany(BuildingType::class, 'parent_id')
->where('is_active', true);
}
/**
* Retribution indices relationship
*/
public function indices(): HasOne
{
return $this->hasOne(RetributionIndex::class, 'building_type_id');
}
/**
* Calculations relationship
*/
public function calculations(): HasMany
{
return $this->hasMany(RetributionCalculation::class, 'building_type_id');
}
/**
* Scope: Active only
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
/**
* Scope: Parents only
*/
public function scopeParents($query)
{
return $query->whereNull('parent_id');
}
/**
* Scope: Children only
*/
public function scopeChildren($query)
{
return $query->whereNotNull('parent_id');
}
/**
* Scope: Non-free types
*/
public function scopeChargeable($query)
{
return $query->where('is_free', false);
}
/**
* Check if building type is free
*/
public function isFree(): bool
{
return $this->is_free;
}
/**
* Check if this is a parent type
*/
public function isParent(): bool
{
return $this->parent_id === null;
}
/**
* Check if this is a child type
*/
public function isChild(): bool
{
return $this->parent_id !== null;
}
/**
* Get complete data for calculation
*/
public function getCalculationData(): array
{
return [
'id' => $this->id,
'code' => $this->code,
'name' => $this->name,
'coefficient' => $this->coefficient,
'is_free' => $this->is_free,
'indices' => $this->indices?->toArray(),
'parent' => $this->parent?->only(['id', 'code', 'name'])
];
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class HeightIndex extends Model
{
protected $fillable = [
'floor_number',
'height_index'
];
protected $casts = [
'floor_number' => 'integer',
'height_index' => 'decimal:6'
];
/**
* Get height index by floor number
*/
public static function getByFloor(int $floorNumber): ?HeightIndex
{
return self::where('floor_number', $floorNumber)->first();
}
/**
* Get height index value by floor number
*/
public static function getHeightIndexByFloor(int $floorNumber): float
{
$index = self::getByFloor($floorNumber);
return $index ? (float) $index->height_index : 1.0;
}
/**
* Get all height indices as array
*/
public static function getAllMapping(): array
{
return self::orderBy('floor_number')
->pluck('height_index', 'floor_number')
->toArray();
}
/**
* Get available floor numbers
*/
public static function getAvailableFloors(): array
{
return self::orderBy('floor_number')
->pluck('floor_number')
->toArray();
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Carbon\Carbon;
class RetributionCalculation extends Model
{
protected $fillable = [
'calculation_id',
'building_type_id',
'floor_number',
'building_area',
'retribution_amount',
'calculation_detail',
'calculated_at'
];
protected $casts = [
'floor_number' => 'integer',
'building_area' => 'decimal:2',
'retribution_amount' => 'decimal:2',
'calculation_detail' => 'array',
'calculated_at' => 'datetime'
];
/**
* Building type relationship
*/
public function buildingType(): BelongsTo
{
return $this->belongsTo(BuildingType::class, 'building_type_id');
}
/**
* Generate unique calculation ID
*/
public static function generateCalculationId(): string
{
return 'RTB' . Carbon::now()->format('ymdHis') . rand(10, 99);
}
/**
* Create new calculation
*/
public static function createCalculation(
int $buildingTypeId,
int $floorNumber,
float $buildingArea,
float $retributionAmount,
array $calculationDetail
): self {
return self::create([
'calculation_id' => self::generateCalculationId(),
'building_type_id' => $buildingTypeId,
'floor_number' => $floorNumber,
'building_area' => $buildingArea,
'retribution_amount' => $retributionAmount,
'calculation_detail' => $calculationDetail,
'calculated_at' => Carbon::now()
]);
}
/**
* Get formatted retribution amount
*/
public function getFormattedAmount(): string
{
return 'Rp ' . number_format($this->retribution_amount, 2, ',', '.');
}
/**
* Get calculation breakdown
*/
public function getCalculationBreakdown(): array
{
return $this->calculation_detail ?? [];
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class RetributionConfig extends Model
{
protected $fillable = [
'key',
'value',
'description',
'is_active'
];
protected $casts = [
'value' => 'decimal:2',
'is_active' => 'boolean'
];
/**
* Get config value by key
*/
public static function getValue(string $key, float $default = 0.0): float
{
$config = self::where('key', $key)->where('is_active', true)->first();
return $config ? (float) $config->value : $default;
}
/**
* Get all active configs as array
*/
public static function getAllActive(): array
{
return self::where('is_active', true)
->pluck('value', 'key')
->toArray();
}
/**
* Update config value
*/
public static function updateValue(string $key, float $value): bool
{
return self::updateOrCreate(
['key' => $key],
['value' => $value, 'is_active' => true]
);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class RetributionIndex extends Model
{
protected $fillable = [
'building_type_id',
'coefficient',
'ip_permanent',
'ip_complexity',
'locality_index',
'infrastructure_factor',
'is_active'
];
protected $casts = [
'coefficient' => 'decimal:4',
'ip_permanent' => 'decimal:4',
'ip_complexity' => 'decimal:4',
'locality_index' => 'decimal:4',
'infrastructure_factor' => 'decimal:4',
'is_active' => 'boolean'
];
/**
* Building type relationship
*/
public function buildingType(): BelongsTo
{
return $this->belongsTo(BuildingType::class, 'building_type_id');
}
/**
* Scope: Active only
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
/**
* Get all indices as array
*/
public function getIndicesArray(): array
{
return [
'ip_permanent' => $this->ip_permanent,
'ip_complexity' => $this->ip_complexity,
'locality_index' => $this->locality_index,
'infrastructure_factor' => $this->infrastructure_factor
];
}
}

View File

@@ -0,0 +1,243 @@
<?php
namespace App\Services;
use App\Models\BuildingType;
use App\Models\HeightIndex;
use App\Models\RetributionConfig;
use App\Models\RetributionCalculation;
class RetributionCalculatorService
{
/**
* Calculate retribution for given parameters
*/
public function calculate(
int $buildingTypeId,
int $floorNumber,
float $buildingArea,
bool $saveResult = true
): array {
// Get building type with indices
$buildingType = BuildingType::with('indices')->findOrFail($buildingTypeId);
// Check if building type is free
if ($buildingType->isFree()) {
return $this->createFreeResult($buildingType, $floorNumber, $buildingArea, $saveResult);
}
// Get height index
$heightIndex = HeightIndex::getHeightIndexByFloor($floorNumber);
// Get configuration values
$baseValue = RetributionConfig::getValue('BASE_VALUE', 70350);
$infrastructureMultiplier = RetributionConfig::getValue('INFRASTRUCTURE_MULTIPLIER', 0.5);
$heightMultiplier = RetributionConfig::getValue('HEIGHT_MULTIPLIER', 0.5);
// Get indices
$indices = $buildingType->indices;
if (!$indices) {
throw new \Exception("Indices not found for building type: {$buildingType->name}");
}
// Calculate using Excel formula
$result = $this->executeCalculation(
$buildingType,
$indices,
$heightIndex,
$baseValue,
$infrastructureMultiplier,
$heightMultiplier,
$floorNumber,
$buildingArea
);
// Save result if requested
if ($saveResult) {
$calculation = RetributionCalculation::createCalculation(
$buildingTypeId,
$floorNumber,
$buildingArea,
$result['total_retribution'],
$result['calculation_detail']
);
$result['calculation_id'] = $calculation->calculation_id;
}
return $result;
}
/**
* Execute the main calculation logic
*/
protected function executeCalculation(
BuildingType $buildingType,
$indices,
float $heightIndex,
float $baseValue,
float $infrastructureMultiplier,
float $heightMultiplier,
int $floorNumber,
float $buildingArea
): array {
// Step 1: Calculate H5 coefficient (Excel formula: RUNDOWN(($E5*($F5+$G5+(0.5*H$3))),4))
// H5 = coefficient * (ip_permanent + ip_complexity + (height_multiplier * height_index))
$h5Raw = $indices->coefficient * (
$indices->ip_permanent +
$indices->ip_complexity +
($heightMultiplier * $heightIndex)
);
// Apply RUNDOWN (floor to 4 decimal places)
$h5 = floor($h5Raw * 10000) / 10000;
// Step 2: Main calculation (Excel: 1*D5*(N5*base_value*H5*1))
// Main = building_area * locality_index * base_value * h5
$mainCalculation = $buildingArea * $indices->locality_index * $baseValue * $h5;
// Step 3: Infrastructure calculation (Excel: O3*(1*D5*(N5*base_value*H5*1)))
// Additional = infrastructure_multiplier * main_calculation
$infrastructureCalculation = $infrastructureMultiplier * $mainCalculation;
// Step 4: Total retribution (Main + Infrastructure)
$totalRetribution = $mainCalculation + $infrastructureCalculation;
return [
'building_type' => [
'id' => $buildingType->id,
'code' => $buildingType->code,
'name' => $buildingType->name,
'is_free' => $buildingType->is_free
],
'input_parameters' => [
'building_area' => $buildingArea,
'floor_number' => $floorNumber,
'height_index' => $heightIndex,
'base_value' => $baseValue,
'infrastructure_multiplier' => $infrastructureMultiplier,
'height_multiplier' => $heightMultiplier
],
'indices' => [
'coefficient' => $indices->coefficient,
'ip_permanent' => $indices->ip_permanent,
'ip_complexity' => $indices->ip_complexity,
'locality_index' => $indices->locality_index,
'infrastructure_factor' => $indices->infrastructure_factor
],
'calculation_steps' => [
'h5_coefficient' => [
'formula' => 'RUNDOWN((coefficient * (ip_permanent + ip_complexity + (height_multiplier * height_index))), 4)',
'calculation' => "RUNDOWN(({$indices->coefficient} * ({$indices->ip_permanent} + {$indices->ip_complexity} + ({$heightMultiplier} * {$heightIndex}))), 4)",
'raw_result' => $h5Raw,
'result' => $h5
],
'main_calculation' => [
'formula' => 'building_area * locality_index * base_value * h5',
'calculation' => "{$buildingArea} * {$indices->locality_index} * {$baseValue} * {$h5}",
'result' => $mainCalculation
],
'infrastructure_calculation' => [
'formula' => 'infrastructure_multiplier * main_calculation',
'calculation' => "{$infrastructureMultiplier} * {$mainCalculation}",
'result' => $infrastructureCalculation
],
'total_calculation' => [
'formula' => 'main_calculation + infrastructure_calculation',
'calculation' => "{$mainCalculation} + {$infrastructureCalculation}",
'result' => $totalRetribution
]
],
'total_retribution' => $totalRetribution,
'formatted_amount' => 'Rp ' . number_format($totalRetribution, 2, ',', '.'),
'calculation_detail' => [
'h5_raw' => $h5Raw,
'h5' => $h5,
'main' => $mainCalculation,
'infrastructure' => $infrastructureCalculation,
'total' => $totalRetribution
]
];
}
/**
* Create result for free building types
*/
protected function createFreeResult(
BuildingType $buildingType,
int $floorNumber,
float $buildingArea,
bool $saveResult
): array {
$result = [
'building_type' => [
'id' => $buildingType->id,
'code' => $buildingType->code,
'name' => $buildingType->name,
'is_free' => true
],
'input_parameters' => [
'building_area' => $buildingArea,
'floor_number' => $floorNumber
],
'total_retribution' => 0.0,
'formatted_amount' => 'Rp 0 (Gratis)',
'calculation_detail' => [
'reason' => 'Building type is free of charge',
'total' => 0.0
]
];
if ($saveResult) {
$calculation = RetributionCalculation::createCalculation(
$buildingType->id,
$floorNumber,
$buildingArea,
0.0,
$result['calculation_detail']
);
$result['calculation_id'] = $calculation->calculation_id;
}
return $result;
}
/**
* Get calculation by ID
*/
public function getCalculationById(string $calculationId): ?RetributionCalculation
{
return RetributionCalculation::with('buildingType')
->where('calculation_id', $calculationId)
->first();
}
/**
* Get all available building types for calculation
*/
public function getAvailableBuildingTypes(): array
{
return BuildingType::with('indices')
->active()
->children() // Only child types can be used for calculation
->get()
->map(function ($type) {
return [
'id' => $type->id,
'code' => $type->code,
'name' => $type->name,
'is_free' => $type->is_free,
'has_indices' => $type->indices !== null,
'coefficient' => $type->indices ? $type->indices->coefficient : null
];
})
->toArray();
}
/**
* Get all available floor numbers
*/
public function getAvailableFloors(): array
{
return HeightIndex::getAvailableFloors();
}
}