done restructure calculation retribution
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class RetributionCalculation extends Model
|
||||
@@ -15,23 +16,39 @@ class RetributionCalculation extends Model
|
||||
'building_area',
|
||||
'retribution_amount',
|
||||
'calculation_detail',
|
||||
'calculated_at'
|
||||
'calculated_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'floor_number' => 'integer',
|
||||
'building_area' => 'decimal:2',
|
||||
'retribution_amount' => 'decimal:2',
|
||||
'calculation_detail' => 'array',
|
||||
'calculated_at' => 'datetime'
|
||||
'calculated_at' => 'timestamp',
|
||||
'floor_number' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* Building type relationship
|
||||
* Get the building type
|
||||
*/
|
||||
public function buildingType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(BuildingType::class, 'building_type_id');
|
||||
return $this->belongsTo(BuildingType::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all calculable assignments
|
||||
*/
|
||||
public function calculableRetributions(): HasMany
|
||||
{
|
||||
return $this->hasMany(CalculableRetribution::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get active assignments only
|
||||
*/
|
||||
public function activeAssignments(): HasMany
|
||||
{
|
||||
return $this->hasMany(CalculableRetribution::class)->where('is_active', true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,7 +56,48 @@ class RetributionCalculation extends Model
|
||||
*/
|
||||
public static function generateCalculationId(): string
|
||||
{
|
||||
return 'RTB' . Carbon::now()->format('ymdHis') . rand(10, 99);
|
||||
return 'CALC-' . date('Ymd') . '-' . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot method to auto-generate calculation_id
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
if (empty($model->calculation_id)) {
|
||||
$model->calculation_id = self::generateCalculationId();
|
||||
}
|
||||
if (empty($model->calculated_at)) {
|
||||
$model->calculated_at = now();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if calculation is being used
|
||||
*/
|
||||
public function isInUse(): bool
|
||||
{
|
||||
return $this->activeAssignments()->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get calculation summary
|
||||
*/
|
||||
public function getSummary(): array
|
||||
{
|
||||
return [
|
||||
'calculation_id' => $this->calculation_id,
|
||||
'building_type' => $this->buildingType->name ?? 'Unknown',
|
||||
'floor_number' => $this->floor_number,
|
||||
'building_area' => $this->building_area,
|
||||
'retribution_amount' => $this->retribution_amount,
|
||||
'calculated_at' => $this->calculated_at->format('Y-m-d H:i:s'),
|
||||
'in_use' => $this->isInUse(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user