akaunting/app/Models/Common/Company.php

425 lines
10 KiB
PHP
Raw Normal View History

2018-06-10 02:48:51 +03:00
<?php
namespace App\Models\Common;
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;
2019-11-16 10:21:14 +03:00
use App\Traits\Media;
2020-05-02 14:53:06 +03:00
use App\Traits\Tenants;
2020-08-26 15:14:16 +03:00
use App\Traits\Transactions;
2018-06-10 02:48:51 +03:00
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;
2019-11-16 10:21:14 +03:00
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
2018-06-10 02:48:51 +03:00
class Company extends Eloquent
{
2020-08-26 15:14:16 +03:00
use Contacts, Media, SearchString, SoftDeletes, Sortable, Tenants, Transactions;
2018-06-10 02:48:51 +03:00
protected $table = 'companies';
2020-05-02 14:33:41 +03:00
protected $tenantable = false;
2018-06-10 02:48:51 +03:00
protected $dates = ['deleted_at'];
protected $fillable = ['domain', 'enabled'];
2020-07-27 13:08:26 +03:00
protected $casts = [
'enabled' => 'boolean',
];
2021-02-26 17:31:02 +03:00
public $allAttributes = [];
2018-06-10 02:48:51 +03:00
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'domain', 'email', 'enabled', 'created_at'];
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
$this->allAttributes = $attributes;
parent::__construct($attributes);
}
2019-11-16 10:21:14 +03:00
public static function boot()
{
parent::boot();
static::retrieved(function($model) {
$model->setSettings();
});
static::saving(function($model) {
$model->unsetSettings();
});
}
2020-12-24 01:28:38 +03:00
public function documents()
{
return $this->hasMany('App\Models\Document\Document');
}
public function document_histories()
{
return $this->hasMany('App\Models\Document\DocumentHistory');
}
public function document_items()
{
return $this->hasMany('App\Models\Document\DocumentItem');
}
public function document_item_taxes()
{
return $this->hasMany('App\Models\Document\DocumentItemTax');
}
public function document_totals()
{
return $this->hasMany('App\Models\Document\DocumentTotal');
}
2018-06-10 02:48:51 +03:00
public function accounts()
{
return $this->hasMany('App\Models\Banking\Account');
}
2019-12-22 15:58:48 +03:00
public function bills()
{
2020-12-24 01:28:38 +03:00
return $this->documents()->where('type', Document::BILL_TYPE);
2019-12-22 15:58:48 +03:00
}
2018-06-10 02:48:51 +03:00
public function bill_histories()
{
2020-12-24 01:28:38 +03:00
return $this->document_histories()->where('type', Document::BILL_TYPE);
2018-06-10 02:48:51 +03:00
}
public function bill_items()
{
2020-12-24 01:28:38 +03:00
return $this->document_items()->where('type', Document::BILL_TYPE);
2018-06-10 02:48:51 +03:00
}
2019-12-22 15:58:48 +03:00
public function bill_item_taxes()
{
2020-12-24 01:28:38 +03:00
return $this->document_item_taxes()->where('type', Document::BILL_TYPE);
2019-12-22 15:58:48 +03:00
}
public function bill_totals()
2018-06-10 02:48:51 +03:00
{
2020-12-24 01:28:38 +03:00
return $this->document_totals()->where('type', Document::BILL_TYPE);
2018-06-10 02:48:51 +03:00
}
public function categories()
{
return $this->hasMany('App\Models\Setting\Category');
}
2019-11-16 10:21:14 +03:00
public function contacts()
{
return $this->hasMany('App\Models\Common\Contact');
}
2018-06-10 02:48:51 +03:00
public function currencies()
{
return $this->hasMany('App\Models\Setting\Currency');
}
public function customers()
{
2020-08-26 15:14:16 +03:00
return $this->contacts()->whereIn('type', (array) $this->getCustomerTypes());
2019-11-16 10:21:14 +03:00
}
public function dashboards()
{
return $this->hasMany('App\Models\Common\Dashboard');
2018-06-10 02:48:51 +03:00
}
2019-12-22 15:58:48 +03:00
public function email_templates()
{
return $this->hasMany('App\Models\Common\EmailTemplate');
}
public function expense_transactions()
{
2020-08-26 15:14:16 +03:00
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
}
public function income_transactions()
{
2020-08-26 15:14:16 +03:00
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
}
2019-12-22 15:58:48 +03:00
public function invoices()
{
2020-12-24 01:28:38 +03:00
return $this->documents()->where('type', Document::INVOICE_TYPE);
2019-12-22 15:58:48 +03:00
}
2018-06-10 02:48:51 +03:00
public function invoice_histories()
{
2020-12-24 01:28:38 +03:00
return $this->document_histories()->where('type', Document::INVOICE_TYPE);
2018-06-10 02:48:51 +03:00
}
public function invoice_items()
{
2020-12-24 01:28:38 +03:00
return $this->document_items()->where('type', Document::INVOICE_TYPE);
2018-06-10 02:48:51 +03:00
}
2019-12-22 15:58:48 +03:00
public function invoice_item_taxes()
{
2020-12-24 01:28:38 +03:00
return $this->document_item_taxes()->where('type', Document::INVOICE_TYPE);
2019-12-22 15:58:48 +03:00
}
public function invoice_totals()
2018-06-10 02:48:51 +03:00
{
2020-12-24 01:28:38 +03:00
return $this->document_totals()->where('type', Document::INVOICE_TYPE);
2018-06-10 02:48:51 +03:00
}
public function items()
{
return $this->hasMany('App\Models\Common\Item');
}
2019-12-22 15:58:48 +03:00
public function modules()
{
return $this->hasMany('App\Models\Module\Module');
}
public function module_histories()
{
return $this->hasMany('App\Models\Module\ModuleHistory');
}
public function reconciliations()
{
return $this->hasMany('App\Models\Banking\Reconciliation');
}
2018-06-10 02:48:51 +03:00
public function recurring()
{
return $this->hasMany('App\Models\Common\Recurring');
}
2019-12-22 15:58:48 +03:00
public function reports()
{
return $this->hasMany('App\Models\Common\Report');
}
2018-06-10 02:48:51 +03:00
public function settings()
{
return $this->hasMany('App\Models\Setting\Setting');
}
public function taxes()
{
return $this->hasMany('App\Models\Setting\Tax');
}
2019-11-16 10:21:14 +03:00
public function transactions()
{
return $this->hasMany('App\Models\Banking\Transaction');
}
2018-06-10 02:48:51 +03:00
public function transfers()
{
return $this->hasMany('App\Models\Banking\Transfer');
}
public function users()
{
return $this->morphedByMany('App\Models\Auth\User', 'user', 'user_companies', 'company_id', 'user_id');
}
public function vendors()
{
2020-08-26 15:14:16 +03:00
return $this->contacts()->whereIn('type', (array) $this->getVendorTypes());
2018-06-10 02:48:51 +03:00
}
2019-12-22 15:58:48 +03:00
public function widgets()
{
return $this->hasMany('App\Models\Common\Widget');
}
2018-06-10 02:48:51 +03:00
public function setSettings()
{
$settings = $this->settings;
2019-11-16 10:21:14 +03:00
$groups = [
'company',
2020-02-09 00:30:44 +03:00
'default',
2019-11-16 10:21:14 +03:00
];
2018-06-10 02:48:51 +03:00
foreach ($settings as $setting) {
list($group, $key) = explode('.', $setting->getAttribute('key'));
// Load only general settings
2019-11-16 10:21:14 +03:00
if (!in_array($group, $groups)) {
2018-06-10 02:48:51 +03:00
continue;
}
$value = $setting->getAttribute('value');
2019-11-16 10:21:14 +03:00
if (($key == 'logo') && empty($value)) {
2018-06-10 02:48:51 +03:00
$value = 'public/img/company.png';
}
$this->setAttribute($key, $value);
}
// Set default default company logo if empty
2019-11-16 10:21:14 +03:00
if ($this->getAttribute('logo') == '') {
$this->setAttribute('logo', 'public/img/company.png');
2018-06-10 02:48:51 +03:00
}
}
2019-11-16 10:21:14 +03:00
public function unsetSettings()
2018-06-10 02:48:51 +03:00
{
2019-11-16 10:21:14 +03:00
$settings = $this->settings;
2018-06-10 02:48:51 +03:00
2019-11-16 10:21:14 +03:00
$groups = [
'company',
2020-02-09 00:30:44 +03:00
'default',
2019-11-16 10:21:14 +03:00
];
2018-06-10 02:48:51 +03:00
2019-11-16 10:21:14 +03:00
foreach ($settings as $setting) {
list($group, $key) = explode('.', $setting->getAttribute('key'));
// Load only general settings
if (!in_array($group, $groups)) {
continue;
}
$this->offsetUnset($key);
}
2018-06-10 02:48:51 +03:00
2019-11-16 10:21:14 +03:00
$this->offsetUnset('logo');
2018-06-10 02:48:51 +03:00
}
/**
* Scope to get all rows filtered, sorted and paginated.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $sort
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCollect($query, $sort = 'name')
{
$request = request();
2019-11-16 10:21:14 +03:00
$search = $request->get('search');
2020-11-06 00:43:46 +03:00
2021-03-06 11:40:19 +03:00
$query->usingSearchString($search)->sortable($sort);
2020-11-06 00:43:46 +03:00
2021-03-06 11:39:36 +03:00
if ($request->expectsJson() && $request->isNotApi()) {
2020-11-06 00:43:46 +03:00
return $query->get();
}
2019-11-16 10:21:14 +03:00
$limit = $request->get('limit', setting('default.list_limit', '25'));
2018-06-10 02:48:51 +03:00
2020-11-06 00:43:46 +03:00
return $query->paginate($limit);
2018-06-10 02:48:51 +03:00
}
/**
* Scope to only include companies of a given enabled value.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeEnabled($query, $value = 1)
{
return $query->where('enabled', $value);
}
2021-01-29 23:56:25 +03:00
/**
* Scope to only include companies of a given user id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $user_id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUserId($query, $user_id)
{
return $query->whereHas('users', function ($query) use ($user_id) {
$query->where('user_id', $user_id);
});
}
2018-06-10 02:48:51 +03:00
/**
* Sort by company name
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function nameSortable($query, $direction)
{
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
2019-11-16 10:21:14 +03:00
->where('key', 'company.name')
2018-06-10 02:48:51 +03:00
->orderBy('value', $direction)
->select('companies.*');
}
/**
* Sort by company email
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function emailSortable($query, $direction)
{
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
2020-02-09 00:30:44 +03:00
->where('key', 'company.email')
2018-06-10 02:48:51 +03:00
->orderBy('value', $direction)
->select('companies.*');
}
/**
* Scope autocomplete.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $filter
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAutocomplete($query, $filter)
{
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
->where(function ($query) use ($filter) {
foreach ($filter as $key => $value) {
$column = $key;
if (!in_array($key, $this->fillable)) {
$column = 'company.' . $key;
$query->orWhere('key', $column);
$query->Where('value', 'LIKE', "%" . $value . "%");
} else {
$query->orWhere($column, 'LIKE', "%" . $value . "%");
}
}
})
->select('companies.*');
}
2018-06-10 02:48:51 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getCompanyLogoAttribute()
{
return $this->getMedia('company_logo')->last();
}
}