88 lines
1.9 KiB
PHP
88 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class Transaction extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'budget_item_id',
|
|
'chart_of_account_id',
|
|
'transaction_date',
|
|
'amount',
|
|
'transaction_type',
|
|
'description',
|
|
'reference_number',
|
|
'finance_document_id',
|
|
'membership_payment_id',
|
|
'created_by_user_id',
|
|
'notes',
|
|
];
|
|
|
|
protected $casts = [
|
|
'transaction_date' => 'date',
|
|
'amount' => 'decimal:2',
|
|
];
|
|
|
|
// Relationships
|
|
|
|
public function budgetItem(): BelongsTo
|
|
{
|
|
return $this->belongsTo(BudgetItem::class);
|
|
}
|
|
|
|
public function chartOfAccount(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ChartOfAccount::class);
|
|
}
|
|
|
|
public function financeDocument(): BelongsTo
|
|
{
|
|
return $this->belongsTo(FinanceDocument::class);
|
|
}
|
|
|
|
public function membershipPayment(): BelongsTo
|
|
{
|
|
return $this->belongsTo(MembershipPayment::class);
|
|
}
|
|
|
|
public function createdBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by_user_id');
|
|
}
|
|
|
|
// Helper methods
|
|
|
|
public function isIncome(): bool
|
|
{
|
|
return $this->transaction_type === 'income';
|
|
}
|
|
|
|
public function isExpense(): bool
|
|
{
|
|
return $this->transaction_type === 'expense';
|
|
}
|
|
|
|
// Scopes
|
|
|
|
public function scopeIncome($query)
|
|
{
|
|
return $query->where('transaction_type', 'income');
|
|
}
|
|
|
|
public function scopeExpense($query)
|
|
{
|
|
return $query->where('transaction_type', 'expense');
|
|
}
|
|
|
|
public function scopeForPeriod($query, $startDate, $endDate)
|
|
{
|
|
return $query->whereBetween('transaction_date', [$startDate, $endDate]);
|
|
}
|
|
}
|