akaunting/app/Models/Banking/Account.php
Denis Duliçi 1ba8835a2d laravel 8
2020-10-14 17:07:59 +03:00

106 lines
2.4 KiB
PHP

<?php
namespace App\Models\Banking;
use App\Abstracts\Model;
use App\Traits\Transactions;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Account extends Model
{
use HasFactory, Transactions;
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');
}
public function expense_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
}
public function income_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
}
public function transactions()
{
return $this->hasMany('App\Models\Banking\Transaction');
}
public function scopeName($query, $name)
{
return $query->where('name', '=', $name);
}
public function scopeNumber($query, $number)
{
return $query->where('number', '=', $number);
}
/**
* Convert opening balance to double.
*
* @param string $value
* @return void
*/
public function setOpeningBalanceAttribute($value)
{
$this->attributes['opening_balance'] = (double) $value;
}
/**
* Get the current balance.
*
* @return string
*/
public function getBalanceAttribute()
{
// Opening Balance
$total = $this->opening_balance;
// Sum Incomes
$total += $this->income_transactions->sum('amount');
// Subtract Expenses
$total -= $this->expense_transactions->sum('amount');
return $total;
}
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\Account::new();
}
}