akaunting/app/Models/Banking/Account.php

106 lines
2.4 KiB
PHP
Raw Normal View History

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;
2020-08-26 15:14:16 +03:00
use App\Traits\Transactions;
2020-10-14 17:07:59 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2021-08-03 17:56:52 +03:00
use Bkwld\Cloner\Cloneable;
2017-09-14 22:21:00 +03:00
class Account extends Model
{
2021-08-03 17:56:52 +03:00
use Cloneable, HasFactory, Transactions;
2020-08-26 15:14:16 +03:00
2017-09-14 22:21:00 +03:00
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
*/
2021-06-17 10:59:07 +03:00
protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled', 'created_by'];
2017-09-14 22:21:00 +03:00
2020-11-13 15:15:27 +03:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'opening_balance' => 'double',
'enabled' => 'boolean',
];
2017-09-14 22:21:00 +03:00
/**
* 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()
2017-09-14 22:21:00 +03:00
{
2020-08-26 15:14:16 +03:00
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
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
{
2020-08-26 15:14:16 +03:00
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
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-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
$total -= $this->expense_transactions->sum('amount');
2017-09-14 22:21:00 +03:00
return $total;
}
2020-10-14 17:07:59 +03:00
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\Account::new();
}
2017-09-14 22:21:00 +03:00
}