akaunting/app/Models/Banking/Account.php

103 lines
2.2 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Banking;
use App\Models\Model;
use Sofa\Eloquence\Eloquence;
class Account extends Model
{
use Eloquence;
protected $table = 'accounts';
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['balance'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'number', 'opening_balance', 'enabled'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'number' => 10,
'bank_name' => 10,
'bank_phone' => 5,
'bank_address' => 2,
];
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function invoice_payments()
{
return $this->hasMany('App\Models\Income\InvoicePayment');
}
public function revenues()
{
return $this->hasMany('App\Models\Income\Revenue');
}
public function bill_payments()
{
return $this->hasMany('App\Models\Expense\BillPayment');
}
public function payments()
{
return $this->hasMany('App\Models\Expense\Payment');
}
2017-10-04 01:25:03 +03:00
/**
2017-10-21 14:23:57 +03:00
* Convert opening balance to double.
2017-10-04 01:25:03 +03:00
*
* @param string $value
* @return void
*/
public function setOpeningBalanceAttribute($value)
{
2017-10-21 14:23:57 +03:00
$this->attributes['opening_balance'] = (double) $value;
2017-10-04 01:25:03 +03:00
}
2017-09-14 22:21:00 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getBalanceAttribute()
{
// Opening Balance
$total = $this->opening_balance;
2019-04-05 15:13:47 +03:00
// Sum Incomes
$total += $this->invoice_payments()->sum('amount') + $this->revenues()->sum('amount');
// Subtract Expenses
$total -= $this->bill_payments()->sum('amount') + $this->payments()->sum('amount');
2017-09-14 22:21:00 +03:00
return $total;
}
}