akaunting/app/Models/Common/Company.php

689 lines
18 KiB
PHP
Raw Normal View History

2018-06-10 02:48:51 +03:00
<?php
namespace App\Models\Common;
2022-06-01 10:15:55 +03:00
use Akaunting\Sortable\Traits\Sortable;
2021-04-16 00:59:43 +03:00
use App\Events\Common\CompanyForgettingCurrent;
use App\Events\Common\CompanyForgotCurrent;
use App\Events\Common\CompanyMadeCurrent;
use App\Events\Common\CompanyMakingCurrent;
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;
2021-06-17 10:59:07 +03:00
use App\Traits\Owners;
2021-09-07 10:33:34 +03:00
use App\Traits\Sources;
2020-05-02 14:53:06 +03:00
use App\Traits\Tenants;
2020-08-26 15:14:16 +03:00
use App\Traits\Transactions;
2021-04-16 00:59:43 +03:00
use App\Utilities\Overrider;
2022-06-01 10:15:55 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2018-06-10 02:48:51 +03:00
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
2021-06-17 10:59:07 +03:00
use Laratrust\Contracts\Ownable;
2019-11-16 10:21:14 +03:00
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
2018-06-10 02:48:51 +03:00
2021-06-17 10:59:07 +03:00
class Company extends Eloquent implements Ownable
2018-06-10 02:48:51 +03:00
{
2022-06-01 10:15:55 +03:00
use Contacts, HasFactory, Media, Owners, SearchString, SoftDeletes, Sortable, Sources, Tenants, Transactions;
2018-06-10 02:48:51 +03:00
protected $table = 'companies';
2021-09-03 12:53:35 +03:00
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['location'];
2018-06-10 02:48:51 +03:00
protected $dates = ['deleted_at'];
2021-09-07 10:33:34 +03:00
protected $fillable = ['domain', 'enabled', 'created_from', 'created_by'];
2018-06-10 02:48:51 +03:00
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
*/
2022-06-01 10:15:55 +03:00
public $sortable = ['id', 'name', 'domain', 'email', 'enabled', 'created_at', 'tax_number', 'country', 'currency'];
2018-06-10 02:48:51 +03:00
/**
2022-06-01 10:15:55 +03:00
* Fill the model with an array of attributes.
*
* @param array $attributes
2022-06-01 10:15:55 +03:00
* @return $this
*
2022-06-01 10:15:55 +03:00
* @throws \Illuminate\Database\Eloquent\MassAssignmentException
*/
2022-06-01 10:15:55 +03:00
public function fill(array $attributes)
{
$this->allAttributes = $attributes;
2022-06-01 10:15:55 +03:00
return parent::fill($attributes);
}
2019-11-16 10:21:14 +03:00
public static function boot()
{
parent::boot();
2022-09-06 13:54:56 +03:00
try {
// TODO will optimize..
2022-03-02 17:43:54 +03:00
static::retrieved(function($model) {
$model->setCommonSettingsAsAttributes();
});
static::saving(function($model) {
$model->unsetCommonSettingsFromAttributes();
});
} catch(\Throwable $e) {
2022-06-01 10:15:55 +03:00
2022-03-02 17:43:54 +03:00
}
2019-11-16 10:21:14 +03:00
}
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()
{
2022-06-01 10:15:55 +03:00
return $this->hasMany('App\Models\Setting\EmailTemplate');
2019-12-22 15:58:48 +03:00
}
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');
}
2021-06-27 12:18:05 +03:00
public function owner()
{
2022-01-05 18:20:19 +03:00
return $this->belongsTo('App\Models\Auth\User', 'created_by', 'id')->withDefault(['name' => trans('general.na')]);
2021-06-27 12:18:05 +03:00
}
2019-12-22 15:58:48 +03:00
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()
{
2022-03-02 12:19:46 +03:00
return $this->belongsToMany('App\Models\Auth\User', 'App\Models\Auth\UserCompany');
2018-06-10 02:48:51 +03:00
}
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');
}
2021-05-30 20:51:14 +03:00
public function setCommonSettingsAsAttributes()
2018-06-10 02:48:51 +03:00
{
try { // TODO will optimize..
$settings = $this->settings;
2018-06-10 02:48:51 +03:00
$groups = [
'company',
'default',
];
2019-11-16 10:21:14 +03:00
foreach ($settings as $setting) {
list($group, $key) = explode('.', $setting->getAttribute('key'));
2018-06-10 02:48:51 +03:00
// Load only general settings
2022-06-01 10:15:55 +03:00
if (! in_array($group, $groups)) {
continue;
}
2018-06-10 02:48:51 +03:00
$value = $setting->getAttribute('value');
2018-06-10 02:48:51 +03:00
if (($key == 'logo') && empty($value)) {
$value = 'public/img/company.png';
}
2018-06-10 02:48:51 +03:00
$this->setAttribute($key, $value);
}
2018-06-10 02:48:51 +03:00
// Set default default company logo if empty
if ($this->getAttribute('logo') == '') {
$this->setAttribute('logo', 'public/img/company.png');
}
2022-06-01 10:15:55 +03:00
// Set default default company currency if empty
if ($this->getAttribute('currency') == '') {
$this->setAttribute('currency', config('setting.fallback.default.currency'));
}
} catch(\Throwable $e) {
2022-06-01 10:15:55 +03:00
2018-06-10 02:48:51 +03:00
}
}
2021-05-30 20:51:14 +03:00
public function unsetCommonSettingsFromAttributes()
2018-06-10 02:48:51 +03:00
{
try { // TODO will optimize..
$settings = $this->settings;
$groups = [
'company',
'default',
];
2018-06-10 02:48:51 +03:00
foreach ($settings as $setting) {
list($group, $key) = explode('.', $setting->getAttribute('key'));
2018-06-10 02:48:51 +03:00
// Load only general settings
2022-06-01 10:15:55 +03:00
if (! in_array($group, $groups)) {
continue;
}
2019-11-16 10:21:14 +03:00
$this->offsetUnset($key);
2019-11-16 10:21:14 +03:00
}
$this->offsetUnset('logo');
$this->offsetUnset('currency');
} catch(\Throwable $e) {
2022-06-01 10:15:55 +03:00
2019-11-16 10:21:14 +03:00
}
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();
}
$limit = (int) $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.*');
}
2022-06-01 10:15:55 +03:00
/**
* Sort by company tax number
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function taxNumberSortable($query, $direction)
{
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
->where('key', 'company.tax_number')
->orderBy('value', $direction)
->select('companies.*');
}
/**
* Sort by company country
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function countrySortable($query, $direction)
{
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
->where('key', 'company.country')
->orderBy('value', $direction)
->select('companies.*');
}
/**
* Sort by company currency
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function currencySortable($query, $direction)
{
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
->where('key', 'default.currency')
->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()
{
2021-08-12 14:45:27 +03:00
return $this->getMedia('company.logo')->last();
2018-06-10 02:48:51 +03:00
}
2021-04-16 00:59:43 +03:00
2021-09-03 12:53:35 +03:00
public function getLocationAttribute()
{
$location = [];
if (setting('company.city')) {
$location[] = setting('company.city');
}
if (setting('company.zip_code')) {
$location[] = setting('company.zip_code');
}
if (setting('company.state')) {
$location[] = setting('company.state');
}
$country = setting('company.country');
if ($country && in_array($country, trans('countries'))) {
$location[] = trans('countries.' . $country);
2021-09-03 12:53:35 +03:00
}
return implode(', ', $location);
}
2022-06-01 10:15:55 +03:00
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$actions = [];
if ($this->enabled) {
$actions[] = [
'title' => trans('general.switch'),
'icon' => 'settings_ethernet',
'url' => route('companies.switch', $this->id),
'permission' => 'read-common-companies',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-switch-company-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
}
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route('companies.edit', $this->id),
'permission' => 'update-common-companies',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-edit-company-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'route' => 'companies.destroy',
'permission' => 'delete-common-companies',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-delete-company-' . $this->id,
],
2022-06-01 10:15:55 +03:00
'model' => $this,
];
return $actions;
}
2021-04-18 01:39:36 +03:00
public function makeCurrent($force = false)
2021-04-16 00:59:43 +03:00
{
2021-04-18 01:39:36 +03:00
if (!$force && $this->isCurrent()) {
2021-04-16 00:59:43 +03:00
return $this;
}
static::forgetCurrent();
event(new CompanyMakingCurrent($this));
// Bind to container
app()->instance(static::class, $this);
// Load settings
setting()->setExtraColumns(['company_id' => $this->id]);
setting()->forgetAll();
setting()->load(true);
// Override settings and currencies
Overrider::load('settings');
Overrider::load('currencies');
event(new CompanyMadeCurrent($this));
return $this;
}
public function isCurrent()
{
2022-06-01 10:15:55 +03:00
return static::getCurrent()?->id === $this->id;
2021-04-16 00:59:43 +03:00
}
public function isNotCurrent()
{
return !$this->isCurrent();
}
public static function getCurrent()
{
if (!app()->has(static::class)) {
return null;
}
return app(static::class);
}
public static function forgetCurrent()
{
$current = static::getCurrent();
if (is_null($current)) {
return null;
}
event(new CompanyForgettingCurrent($current));
// Remove from container
app()->forgetInstance(static::class);
// Remove settings
setting()->forgetAll();
event(new CompanyForgotCurrent($current));
return $current;
}
public static function hasCurrent()
{
return static::getCurrent() !== null;
}
2021-06-17 10:59:07 +03:00
2021-09-07 10:33:34 +03:00
public function scopeSource($query, $source)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('created_from'), $source);
2021-09-07 10:33:34 +03:00
}
2021-06-17 10:59:07 +03:00
public function scopeIsOwner($query)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('created_by'), user_id());
2021-06-17 10:59:07 +03:00
}
public function scopeIsNotOwner($query)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('created_by'), '<>', user_id());
2021-06-17 10:59:07 +03:00
}
public function ownerKey($owner)
{
if ($this->isNotOwnable()) {
return 0;
}
return $this->created_by;
}
2022-06-01 10:15:55 +03:00
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\Company::new();
}
2018-06-10 02:48:51 +03:00
}