Initial commit
This commit is contained in:
121
app/Models/Budget.php
Normal file
121
app/Models/Budget.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?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 Budget extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const STATUS_DRAFT = 'draft';
|
||||
public const STATUS_SUBMITTED = 'submitted';
|
||||
public const STATUS_APPROVED = 'approved';
|
||||
public const STATUS_ACTIVE = 'active';
|
||||
public const STATUS_CLOSED = 'closed';
|
||||
|
||||
protected $fillable = [
|
||||
'fiscal_year',
|
||||
'name',
|
||||
'period_type',
|
||||
'period_start',
|
||||
'period_end',
|
||||
'status',
|
||||
'created_by_user_id',
|
||||
'approved_by_user_id',
|
||||
'approved_at',
|
||||
'notes',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'fiscal_year' => 'integer',
|
||||
'period_start' => 'date',
|
||||
'period_end' => 'date',
|
||||
'approved_at' => 'datetime',
|
||||
];
|
||||
|
||||
// Relationships
|
||||
|
||||
public function createdBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by_user_id');
|
||||
}
|
||||
|
||||
public function approvedBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'approved_by_user_id');
|
||||
}
|
||||
|
||||
public function budgetItems(): HasMany
|
||||
{
|
||||
return $this->hasMany(BudgetItem::class);
|
||||
}
|
||||
|
||||
public function financialReports(): HasMany
|
||||
{
|
||||
return $this->hasMany(FinancialReport::class);
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
|
||||
public function isDraft(): bool
|
||||
{
|
||||
return $this->status === self::STATUS_DRAFT;
|
||||
}
|
||||
|
||||
public function isApproved(): bool
|
||||
{
|
||||
return $this->status === self::STATUS_APPROVED;
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->status === self::STATUS_ACTIVE;
|
||||
}
|
||||
|
||||
public function isClosed(): bool
|
||||
{
|
||||
return $this->status === self::STATUS_CLOSED;
|
||||
}
|
||||
|
||||
public function canBeEdited(): bool
|
||||
{
|
||||
return in_array($this->status, [self::STATUS_DRAFT, self::STATUS_SUBMITTED]);
|
||||
}
|
||||
|
||||
public function canBeApproved(): bool
|
||||
{
|
||||
return $this->status === self::STATUS_SUBMITTED;
|
||||
}
|
||||
|
||||
public function getTotalBudgetedIncomeAttribute(): float
|
||||
{
|
||||
return $this->budgetItems()
|
||||
->whereHas('chartOfAccount', fn($q) => $q->where('account_type', 'income'))
|
||||
->sum('budgeted_amount');
|
||||
}
|
||||
|
||||
public function getTotalBudgetedExpenseAttribute(): float
|
||||
{
|
||||
return $this->budgetItems()
|
||||
->whereHas('chartOfAccount', fn($q) => $q->where('account_type', 'expense'))
|
||||
->sum('budgeted_amount');
|
||||
}
|
||||
|
||||
public function getTotalActualIncomeAttribute(): float
|
||||
{
|
||||
return $this->budgetItems()
|
||||
->whereHas('chartOfAccount', fn($q) => $q->where('account_type', 'income'))
|
||||
->sum('actual_amount');
|
||||
}
|
||||
|
||||
public function getTotalActualExpenseAttribute(): float
|
||||
{
|
||||
return $this->budgetItems()
|
||||
->whereHas('chartOfAccount', fn($q) => $q->where('account_type', 'expense'))
|
||||
->sum('actual_amount');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user