81 lines
2.0 KiB
PHP
81 lines
2.0 KiB
PHP
<?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 ?? [];
|
|
}
|
|
}
|