218 lines
5.2 KiB
PHP
Raw Permalink 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;
2021-08-03 17:56:52 +03:00
use Bkwld\Cloner\Cloneable;
2022-06-01 10:15:55 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
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
*/
2022-06-06 15:55:14 +03:00
protected $appends = ['balance', 'title'];
2017-09-14 22:21:00 +03:00
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2022-06-01 10:15:55 +03:00
protected $fillable = ['company_id', 'type', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled', 'created_from', '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 = [
2023-03-16 16:36:13 +03:00
'opening_balance' => 'double',
'enabled' => 'boolean',
'deleted_at' => 'datetime',
2020-11-13 15:15:27 +03:00
];
2017-09-14 22:21:00 +03:00
/**
* Sortable columns.
*
* @var array
*/
2022-06-01 10:15:55 +03:00
public $sortable = ['name', 'number', 'balance', 'bank_name', 'bank_phone'];
2017-09-14 22:21:00 +03:00
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
{
2022-06-10 19:26:32 +03:00
return $this->transactions()->whereIn('transactions.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
{
2022-06-10 19:26:32 +03:00
return $this->transactions()->whereIn('transactions.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
}
public function reconciliations()
{
return $this->hasMany('App\Models\Banking\Reconciliation');
}
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);
}
2022-06-01 10:15:55 +03:00
/**
* Sort by balance
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function balanceSortable($query, $direction)
{
return $query//->join('transactions', 'transactions.account_id', '=', 'accounts.id')
->orderBy('balance', $direction)
->select(['accounts.*', 'accounts.opening_balance as balance']);
}
2022-06-06 15:55:14 +03:00
/**
* Get the name with currency.
*
* @return string
*/
public function getTitleAttribute()
{
if (! empty($this->currency) && ! empty($this->currency->symbol)) {
2022-06-06 15:55:14 +03:00
return $this->name . ' (' . $this->currency->symbol . ')';
}
return $this->name;
}
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
2021-08-04 17:51:44 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getIncomeBalanceAttribute()
{
// Opening Balance
//$total = $this->opening_balance;
$total = 0;
// Sum Incomes
$total += $this->income_transactions->sum('amount');
return $total;
}
/**
* Get the current balance.
*
* @return string
*/
public function getExpenseBalanceAttribute()
{
// Opening Balance
//$total = $this->opening_balance;
$total = 0;
// Subtract Expenses
$total += $this->expense_transactions->sum('amount');
return $total;
}
2022-06-01 10:15:55 +03:00
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$actions = [];
$actions[] = [
'title' => trans('general.show'),
'icon' => 'visibility',
'url' => route('accounts.show', $this->id),
'permission' => 'read-banking-accounts',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-show-account-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route('accounts.edit', $this->id),
'permission' => 'update-banking-accounts',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-edit-account-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'route' => 'accounts.destroy',
'permission' => 'delete-banking-accounts',
'model' => $this,
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-delete-account-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
return $actions;
}
2021-08-04 17:51:44 +03:00
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
}