<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BankAccount extends Model
{
    protected $guarded = [];

    public function expenses()
    {
        return $this->hasMany(Expense::class);
    }

    public function sentTransactions()
    {
        return $this->hasMany(AccountToAccountTransection::class, 'from');
    }

    // Transactions received by this account
    public function receivedTransactions()
    {
        return $this->hasMany(AccountToAccountTransection::class, 'to');
    }

    // Balance calculation
    public function balance()
    {
        return $this->current_balance;
    }


    protected static function boot() {
        parent::boot();
        static::deleting( function ( $telco ) {
            $relationMethods = [ 'expenses' ,'incomes'];
            foreach ( $relationMethods as $relationMethod ) {
                if ( $telco->$relationMethod()->count() > 0 ) {
                    return false;
                }
            }
        } );
    }
}
