2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Banking;
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Model;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Account extends Model
|
|
|
|
{
|
|
|
|
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'];
|
|
|
|
|
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function expense_transactions()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-11-27 12:08:36 +03:00
|
|
|
return $this->transactions()->where('type', 'expense');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 09:02:25 +01:00
|
|
|
public function income_transactions()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-11-27 12:08:36 +03:00
|
|
|
return $this->transactions()->where('type', 'income');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function transactions()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
return $this->hasMany('App\Models\Banking\Transaction');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:49 +03:00
|
|
|
public function scopeName($query, $name)
|
|
|
|
{
|
|
|
|
return $query->where('name', '=', $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeNumber($query, $number)
|
|
|
|
{
|
|
|
|
return $query->where('number', '=', $number);
|
|
|
|
}
|
|
|
|
|
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
|
2020-03-09 09:02:25 +01:00
|
|
|
$total += $this->income_transactions->sum('amount');
|
2019-04-05 15:13:47 +03:00
|
|
|
|
|
|
|
// Subtract Expenses
|
2019-11-27 12:08:36 +03:00
|
|
|
$total -= $this->expense_transactions->sum('amount');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
}
|