91 lines
2.0 KiB
PHP
91 lines
2.0 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 ChartOfAccount extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public const TYPE_INCOME = 'income';
|
|
public const TYPE_EXPENSE = 'expense';
|
|
public const TYPE_ASSET = 'asset';
|
|
public const TYPE_LIABILITY = 'liability';
|
|
public const TYPE_NET_ASSET = 'net_asset';
|
|
|
|
protected $fillable = [
|
|
'account_code',
|
|
'account_name_zh',
|
|
'account_name_en',
|
|
'account_type',
|
|
'category',
|
|
'parent_account_id',
|
|
'is_active',
|
|
'display_order',
|
|
'description',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
'display_order' => 'integer',
|
|
];
|
|
|
|
// Relationships
|
|
|
|
public function parentAccount(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ChartOfAccount::class, 'parent_account_id');
|
|
}
|
|
|
|
public function childAccounts(): HasMany
|
|
{
|
|
return $this->hasMany(ChartOfAccount::class, 'parent_account_id')->orderBy('display_order');
|
|
}
|
|
|
|
public function budgetItems(): HasMany
|
|
{
|
|
return $this->hasMany(BudgetItem::class);
|
|
}
|
|
|
|
public function transactions(): HasMany
|
|
{
|
|
return $this->hasMany(Transaction::class);
|
|
}
|
|
|
|
// Helper methods
|
|
|
|
public function getFullNameAttribute(): string
|
|
{
|
|
return "{$this->account_code} - {$this->account_name_zh}";
|
|
}
|
|
|
|
public function isIncome(): bool
|
|
{
|
|
return $this->account_type === 'income';
|
|
}
|
|
|
|
public function isExpense(): bool
|
|
{
|
|
return $this->account_type === 'expense';
|
|
}
|
|
|
|
public function isAsset(): bool
|
|
{
|
|
return $this->account_type === 'asset';
|
|
}
|
|
|
|
public function isLiability(): bool
|
|
{
|
|
return $this->account_type === 'liability';
|
|
}
|
|
|
|
public function isNetAsset(): bool
|
|
{
|
|
return $this->account_type === 'net_asset';
|
|
}
|
|
}
|