akaunting/app/Abstracts/Model.php

171 lines
4.1 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-11-16 10:21:14 +03:00
namespace App\Abstracts;
2017-09-14 22:21:00 +03:00
use App\Traits\DateTime;
2020-05-02 14:33:41 +03:00
use App\Traits\Tenants;
2020-01-18 13:03:07 +03:00
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
2017-09-14 22:21:00 +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;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
abstract class Model extends Eloquent
2017-09-14 22:21:00 +03:00
{
use Cachable, DateTime, SearchString, SoftDeletes, Sortable, Tenants;
2017-09-14 22:21:00 +03:00
2020-05-02 15:13:49 +03:00
protected $tenantable = true;
2017-09-14 22:21:00 +03:00
protected $dates = ['deleted_at'];
2020-07-27 13:08:26 +03:00
protected $casts = [
'enabled' => 'boolean',
];
2021-02-26 17:31:02 +03:00
public $allAttributes = [];
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
$this->allAttributes = $attributes;
parent::__construct($attributes);
}
/**
* Update the model in the database.
*
* @param array $attributes
* @param array $options
* @return bool
*/
public function update(array $attributes = [], array $options = [])
{
$this->allAttributes = $attributes;
return parent::update($attributes, $options);
}
2020-12-24 01:28:38 +03:00
public static function observe($classes)
{
parent::observe($classes);
}
2017-09-14 22:21:00 +03:00
/**
* Global company relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function company()
{
2018-06-10 02:48:51 +03:00
return $this->belongsTo('App\Models\Common\Company');
2017-09-14 22:21:00 +03:00
}
2020-12-08 18:47:32 +03:00
/**
* Scope to only include company data.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAllCompanies($query)
{
2021-01-28 18:20:41 +03:00
return $query->withoutGlobalScope('App\Scopes\Company');
2020-12-08 18:47:32 +03:00
}
2017-09-14 22:21:00 +03:00
/**
* Scope to only include company data.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $company_id
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCompanyId($query, $company_id)
{
return $query->where($this->table . '.company_id', '=', $company_id);
}
/**
* 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
$query->usingSearchString($search)->sortable($sort);
2021-01-19 14:12:54 +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'));
2017-09-14 22:21:00 +03:00
2020-11-06 00:43:46 +03:00
return $query->paginate($limit);
2017-09-14 22:21:00 +03:00
}
/**
2018-06-30 12:37:19 +03:00
* Scope to only include active models.
2017-09-14 22:21:00 +03:00
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeEnabled($query)
{
return $query->where('enabled', 1);
}
2018-06-30 12:37:19 +03:00
/**
* Scope to only include passive models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeDisabled($query)
{
return $query->where('enabled', 0);
}
2018-10-27 17:57:40 +03:00
/**
* Scope to only include reconciled models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeReconciled($query, $value = 1)
{
return $query->where('reconciled', $value);
}
2018-11-01 16:58:31 +03:00
public function scopeAccount($query, $accounts)
{
if (empty($accounts)) {
2019-11-16 10:21:14 +03:00
return $query;
2018-11-01 16:58:31 +03:00
}
return $query->whereIn('account_id', (array) $accounts);
}
2019-11-16 10:21:14 +03:00
public function scopeContact($query, $contacts)
2018-11-01 16:58:31 +03:00
{
2019-11-16 10:21:14 +03:00
if (empty($contacts)) {
return $query;
2018-11-01 16:58:31 +03:00
}
2019-11-16 10:21:14 +03:00
return $query->whereIn('contact_id', (array) $contacts);
2018-11-01 16:58:31 +03:00
}
2017-09-14 22:21:00 +03:00
}