255 lines
6.8 KiB
PHP
Raw Permalink 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
2022-06-01 10:15:55 +03:00
use Akaunting\Sortable\Traits\Sortable;
use App\Events\Common\SearchStringApplied;
use App\Events\Common\SearchStringApplying;
use App\Traits\DateTime;
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:33:41 +03:00
use App\Traits\Tenants;
2020-01-18 13:03:07 +03:00
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
2022-06-01 10:15:55 +03:00
use Illuminate\Database\Eloquent\Builder;
2017-09-14 22:21:00 +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;
2017-09-14 22:21:00 +03:00
2021-06-17 10:59:07 +03:00
abstract class Model extends Eloquent implements Ownable
2017-09-14 22:21:00 +03:00
{
2021-09-07 10:33:34 +03:00
use Cachable, DateTime, Owners, SearchString, SoftDeletes, Sortable, Sources, Tenants;
2017-09-14 22:21:00 +03:00
2020-05-02 15:13:49 +03:00
protected $tenantable = true;
2020-07-27 13:08:26 +03:00
protected $casts = [
2023-03-16 16:36:13 +03:00
'amount' => 'double',
'enabled' => 'boolean',
'deleted_at' => 'datetime',
2020-07-27 13:08:26 +03:00
];
2021-02-26 17:31:02 +03:00
public $allAttributes = [];
/**
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);
}
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
}
2021-06-27 12:16:47 +03:00
/**
* Owner relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function owner()
{
2021-06-28 12:21:56 +03:00
return $this->belongsTo('App\Models\Auth\User', 'created_by', 'id')->withDefault(['name' => trans('general.na')]);
2021-06-27 12:16:47 +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)
{
2021-09-10 09:47:45 +03:00
return $query->where($this->qualifyColumn('company_id'), '=', $company_id);
2017-09-14 22:21:00 +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();
/**
* Modules that use the sort parameter in CRUD operations cause an error,
* so this sort parameter set back to old value after the query is executed.
2023-03-16 16:36:13 +03:00
*
* for Custom Fields module
*/
$request_sort = $request->get('sort');
$query->usingSearchString()->sortable($sort);
2020-11-06 00:43:46 +03:00
2021-01-19 14:12:54 +03:00
if ($request->expectsJson() && $request->isNotApi()) {
2020-11-06 00:43:46 +03:00
return $query->get();
}
$request->merge(['sort' => $request_sort]);
// This line disabled because broken sortable issue.
//$request->offsetUnset('direction');
$limit = (int) $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
}
2022-06-28 14:43:14 +03:00
public function scopeUsingSearchString(Builder $query, string|null $string = null)
{
event(new SearchStringApplying($query));
2022-06-28 14:43:14 +03:00
$string = $string ?: request('search');
$this->getSearchStringManager()->updateBuilder($query, $string);
event(new SearchStringApplied($query));
}
2021-06-27 11:40:46 +03:00
/**
* Scope to export the rows of the current page filtered and sorted.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $ids
* @param $sort
* @param $id_field
*
* @return \Illuminate\Support\LazyCollection
*/
public function scopeCollectForExport($query, $ids = [], $sort = 'name', $id_field = 'id')
{
$request = request();
if (!empty($ids)) {
$query->whereIn($id_field, (array) $ids);
}
$search = $request->get('search');
$query->usingSearchString($search)->sortable($sort);
$page = (int) $request->get('page');
$limit = (int) $request->get('limit', setting('default.list_limit', '25'));
$offset = $page ? ($page - 1) * $limit : 0;
$query->offset($offset)->limit($limit);
return $query->cursor();
}
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)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('enabled'), 1);
2017-09-14 22:21:00 +03:00
}
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)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('enabled'), 0);
2018-06-30 12:37:19 +03:00
}
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)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('reconciled'), $value);
2018-10-27 17:57:40 +03:00
}
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
}
2021-09-10 09:41:15 +03:00
return $query->whereIn($this->qualifyColumn('account_id'), (array) $accounts);
2018-11-01 16:58:31 +03:00
}
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
}
2021-09-10 09:41:15 +03:00
return $query->whereIn($this->qualifyColumn('contact_id'), (array) $contacts);
2018-11-01 16:58:31 +03:00
}
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
}
2022-06-01 10:15:55 +03:00
public function scopeIsRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'like', '%-recurring');
}
public function scopeIsNotRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'not like', '%-recurring');
}
2021-06-17 10:59:07 +03:00
public function ownerKey($owner)
{
if ($this->isNotOwnable()) {
return 0;
}
return $this->created_by;
}
2017-09-14 22:21:00 +03:00
}