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;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Currency extends Model
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $table = 'currencies';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-12-09 16:24:18 +03:00
|
|
|
protected $fillable = ['company_id', 'name', 'code', 'rate', 'enabled', 'precision', 'symbol', 'symbol_first', 'decimal_mark', 'thousands_separator'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sortable columns.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sortable = ['name', 'code', 'rate', 'enabled'];
|
|
|
|
|
|
|
|
public function accounts()
|
|
|
|
{
|
2017-09-26 14:59:17 +03:00
|
|
|
return $this->hasMany('App\Models\Banking\Account', 'currency_code', 'code');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function bills()
|
|
|
|
{
|
2019-12-31 15:49:09 +03:00
|
|
|
return $this->hasMany('App\Models\Purchase\Bill', 'currency_code', 'code');
|
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()
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
return $this->contacts()->where('type', 'customer');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function income_transactions()
|
2018-02-09 16:18:56 +03:00
|
|
|
{
|
2019-11-27 12:08:36 +03:00
|
|
|
return $this->transactions()->where('type', 'income');
|
2018-02-09 16:18:56 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function invoices()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-12-31 15:49:09 +03:00
|
|
|
return $this->hasMany('App\Models\Sale\Invoice', 'currency_code', 'code');
|
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
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
return $this->contacts()->where('type', 'vendor');
|
2017-09-26 14:59:17 +03:00
|
|
|
}
|
|
|
|
|
2017-10-04 01:25:03 +03:00
|
|
|
/**
|
2017-10-21 14:23:57 +03:00
|
|
|
* Convert rate to double.
|
2017-10-04 01:25:03 +03:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setRateAttribute($value)
|
|
|
|
{
|
2017-10-21 14:23:57 +03:00
|
|
|
$this->attributes['rate'] = (double) $value;
|
2017-10-04 01:25:03 +03:00
|
|
|
}
|
2018-09-06 11:11:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current precision.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPrecisionAttribute($value)
|
|
|
|
{
|
2018-10-13 17:15:52 +03:00
|
|
|
if (is_null($value)) {
|
2018-09-06 11:11:53 +03:00
|
|
|
return config('money.' . $this->code . '.precision');
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:39:15 +03:00
|
|
|
return (int) $value;
|
2018-09-06 11:11:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current symbol.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSymbolAttribute($value)
|
|
|
|
{
|
2018-10-13 17:15:52 +03:00
|
|
|
if (is_null($value)) {
|
2018-09-06 11:11:53 +03:00
|
|
|
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)) {
|
2018-09-06 11:11:53 +03:00
|
|
|
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)) {
|
2018-09-06 11:11:53 +03:00
|
|
|
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)) {
|
2018-09-06 11:11:53 +03:00
|
|
|
return config('money.' . $this->code . '.thousands_separator');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2020-02-28 17:54:01 +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->table . '.code', $code);
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|