akaunting/app/Models/Common/Company.php

328 lines
7.6 KiB
PHP
Raw Normal View History

2018-06-10 02:48:51 +03:00
<?php
namespace App\Models\Common;
2019-11-16 10:21:14 +03:00
use App\Traits\Media;
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
{
2019-11-16 10:21:14 +03:00
use Media, SearchString, SoftDeletes, Sortable;
2018-06-10 02:48:51 +03:00
protected $table = 'companies';
protected $dates = ['deleted_at'];
protected $fillable = ['domain', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'domain', 'email', 'enabled', 'created_at'];
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();
});
}
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()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\Bill');
2019-12-22 15:58:48 +03:00
}
2018-06-10 02:48:51 +03:00
public function bill_histories()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\BillHistory');
2018-06-10 02:48:51 +03:00
}
public function bill_items()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\BillItem');
2018-06-10 02:48:51 +03:00
}
2019-12-22 15:58:48 +03:00
public function bill_item_taxes()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\BillItemTax');
2019-12-22 15:58:48 +03:00
}
public function bill_totals()
2018-06-10 02:48:51 +03:00
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\BillTotal');
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()
{
2019-11-16 10:21:14 +03:00
return $this->contacts()->where('type', 'customer');
}
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()
{
return $this->transactions()->where('type', 'expense');
}
public function income_transactions()
{
return $this->transactions()->where('type', 'income');
}
2019-12-22 15:58:48 +03:00
public function invoices()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\Invoice');
2019-12-22 15:58:48 +03:00
}
2018-06-10 02:48:51 +03:00
public function invoice_histories()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\InvoiceHistory');
2018-06-10 02:48:51 +03:00
}
public function invoice_items()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\InvoiceItem');
2018-06-10 02:48:51 +03:00
}
2019-12-22 15:58:48 +03:00
public function invoice_item_taxes()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\InvoiceItemTax');
2019-12-22 15:58:48 +03:00
}
public function invoice_totals()
2018-06-10 02:48:51 +03:00
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\InvoiceTotal');
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()
{
2019-11-16 10:21:14 +03:00
return $this->contacts()->where('type', 'vendor');
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');
$limit = $request->get('limit', setting('default.list_limit', '25'));
2018-06-10 02:48:51 +03:00
return user()->companies()->usingSearchString($search)->sortable($sort)->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);
}
/**
* 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.*');
}
/**
* Get the current balance.
*
* @return string
*/
public function getCompanyLogoAttribute()
{
return $this->getMedia('company_logo')->last();
}
}