| Current Path : /var/www/html/LLCF-APP/app/Models/ |
| Current File : /var/www/html/LLCF-APP/app/Models/BankTransaction.php |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class BankTransaction extends Model
{
use HasFactory;
protected $table = 'bank_transactions';
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'bank_account_id',
'transaction_date',
'description',
'debit',
'credit',
'balance',
'cheque_no',
'reference_no',
];
/**
* The attributes that should be cast to native types.
*/
protected $casts = [
'transaction_date' => 'date',
'debit' => 'decimal:2',
'credit' => 'decimal:2',
'balance' => 'decimal:2',
];
/**
* Relationship: A transaction belongs to a bank account.
*/
public function account()
{
return $this->belongsTo(BankAccount::class, 'bank_account_id');
}
/**
* Accessor: Get transaction type (Credit or Debit).
*/
public function getTransactionTypeAttribute(): string
{
if (!is_null($this->credit) && $this->credit > 0) {
return 'credit';
} elseif (!is_null($this->debit) && $this->debit > 0) {
return 'debit';
}
return 'neutral';
}
/**
* Scope: Filter transactions between two dates.
*/
public function scopeBetweenDates($query, $from, $to)
{
return $query->whereBetween('transaction_date', [$from, $to]);
}
}