akaunting/app/Models/Setting/Currency.php

230 lines
5.2 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Setting;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Model;
2020-12-24 01:28:38 +03:00
use App\Models\Document\Document;
2020-08-26 15:14:16 +03:00
use App\Traits\Contacts;
use App\Traits\Transactions;
2020-10-14 17:07:59 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2017-09-14 22:21:00 +03:00
class Currency extends Model
{
2020-10-14 17:07:59 +03:00
use Contacts, HasFactory, Transactions;
2017-09-14 22:21:00 +03:00
protected $table = 'currencies';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2021-06-17 10:59:07 +03:00
protected $fillable = [
'company_id',
'name',
'code',
'rate',
'enabled',
'precision',
'symbol',
'symbol_first',
'decimal_mark',
'thousands_separator',
2021-09-07 10:33:34 +03:00
'created_from',
2021-06-17 10:59:07 +03:00
'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
'rate' => '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
*/
public $sortable = ['name', 'code', 'rate', 'enabled'];
public function accounts()
{
return $this->hasMany('App\Models\Banking\Account', 'currency_code', 'code');
2017-09-14 22:21:00 +03:00
}
2020-12-24 01:28:38 +03:00
public function documents()
{
return $this->hasMany('App\Models\Document\Document', 'currency_code', 'code');
}
2019-11-16 10:21:14 +03:00
public function bills()
{
2022-06-10 19:26:32 +03:00
return $this->documents()->where('documents.type', Document::BILL_TYPE);
2019-11-16 10:21:14 +03:00
}
public function contacts()
{
return $this->hasMany('App\Models\Common\Contact', 'currency_code', 'code');
}
2017-09-14 22:21:00 +03:00
public function customers()
{
2022-06-10 19:26:32 +03:00
return $this->contacts()->whereIn('contacts.type', (array) $this->getCustomerTypes());
2017-09-14 22:21:00 +03:00
}
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
}
public function income_transactions()
{
2022-06-10 19:26:32 +03:00
return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
}
public function invoices()
2017-09-14 22:21:00 +03:00
{
2022-06-10 19:26:32 +03:00
return $this->documents()->where('documents.type', Document::INVOICE_TYPE);
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', 'currency_code', 'code');
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
public function vendors()
2017-09-14 22:21:00 +03:00
{
2022-06-10 19:26:32 +03:00
return $this->contacts()->whereIn('contacts.type', (array) $this->getVendorTypes());
}
2022-06-01 10:15:55 +03:00
/**
* Scope currency by code.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $code
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCode($query, $code)
{
return $query->where($this->qualifyColumn('code'), $code);
}
/**
* Get the current precision.
*
* @return string
*/
public function getPrecisionAttribute($value)
{
2018-10-13 17:15:52 +03:00
if (is_null($value)) {
return config('money.' . $this->code . '.precision');
}
return (int) $value;
}
/**
* Get the current symbol.
*
* @return string
*/
public function getSymbolAttribute($value)
{
2018-10-13 17:15:52 +03:00
if (is_null($value)) {
return config('money.' . $this->code . '.symbol');
}
return $value;
}
/**
* Get the current symbol_first.
*
* @return string
*/
public function getSymbolFirstAttribute($value)
{
2018-10-13 17:15:52 +03:00
if (is_null($value)) {
return config('money.' . $this->code . '.symbol_first');
}
return $value;
}
/**
* Get the current decimal_mark.
*
* @return string
*/
public function getDecimalMarkAttribute($value)
{
2018-10-13 17:15:52 +03:00
if (is_null($value)) {
return config('money.' . $this->code . '.decimal_mark');
}
return $value;
}
/**
* Get the current thousands_separator.
*
* @return string
*/
public function getThousandsSeparatorAttribute($value)
{
2018-10-13 17:15:52 +03:00
if (is_null($value)) {
return config('money.' . $this->code . '.thousands_separator');
}
return $value;
}
2020-02-28 17:54:01 +03:00
/**
2022-06-01 10:15:55 +03:00
* Get the line actions.
2020-02-28 17:54:01 +03:00
*
2022-06-01 10:15:55 +03:00
* @return array
2020-02-28 17:54:01 +03:00
*/
2022-06-01 10:15:55 +03:00
public function getLineActionsAttribute()
{
$actions = [];
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route('currencies.edit', $this->id),
'permission' => 'update-settings-currencies',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-edit-currency-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'route' => 'currencies.destroy',
'permission' => 'delete-settings-currencies',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-delete-currency-' . $this->id,
],
2022-06-01 10:15:55 +03:00
'model' => $this,
];
return $actions;
2020-02-28 17:54:01 +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\Currency::new();
}
2017-09-14 22:21:00 +03:00
}