77 lines
1.7 KiB
PHP
77 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class BudgetItem extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'budget_id',
|
|
'chart_of_account_id',
|
|
'budgeted_amount',
|
|
'actual_amount',
|
|
'notes',
|
|
];
|
|
|
|
protected $casts = [
|
|
'budgeted_amount' => 'decimal:2',
|
|
'actual_amount' => 'decimal:2',
|
|
];
|
|
|
|
// Relationships
|
|
|
|
public function budget(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Budget::class);
|
|
}
|
|
|
|
public function chartOfAccount(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ChartOfAccount::class);
|
|
}
|
|
|
|
public function transactions(): HasMany
|
|
{
|
|
return $this->hasMany(Transaction::class);
|
|
}
|
|
|
|
// Helper methods
|
|
|
|
public function getVarianceAttribute(): float
|
|
{
|
|
return $this->actual_amount - $this->budgeted_amount;
|
|
}
|
|
|
|
public function getVariancePercentageAttribute(): float
|
|
{
|
|
if ($this->budgeted_amount == 0) {
|
|
return 0;
|
|
}
|
|
return ($this->variance / $this->budgeted_amount) * 100;
|
|
}
|
|
|
|
public function getRemainingBudgetAttribute(): float
|
|
{
|
|
return $this->budgeted_amount - $this->actual_amount;
|
|
}
|
|
|
|
public function isOverBudget(): bool
|
|
{
|
|
return $this->actual_amount > $this->budgeted_amount;
|
|
}
|
|
|
|
public function getUtilizationPercentageAttribute(): float
|
|
{
|
|
if ($this->budgeted_amount == 0) {
|
|
return 0;
|
|
}
|
|
return ($this->actual_amount / $this->budgeted_amount) * 100;
|
|
}
|
|
}
|