Files
InvoiceShelf/app/Domains/Metadata/Models/CustomField.php
T
Darko Gjorgjijoski 5ef7804e60 refactor: adopt modular domain architecture (#747)
* refactor: stabilize model identities for domain migration

* refactor: extract module platform context

* refactor: assign models to domain contexts

* refactor: extract ai platform context

* refactor: extract storage platform context

* refactor: extract mail platform context

* refactor: extract pdf platform context

* refactor: extract operations platform context

* refactor: move installation into operations platform

* refactor: extract money domain context

* refactor: extract taxation domain context

* refactor: extract catalog domain context

* refactor: extract metadata domain context

* refactor: extract reporting domain context

* refactor: extract purchases domain context

* refactor: extract receivables domain context

* refactor: extract accounts domain context

* refactor: complete reporting statement boundary

* refactor: extract contacts domain context

* refactor: extract sales domain context

* refactor: remove legacy application layers

* fix: migrate legacy bouncer role identities
2026-08-05 17:40:03 +02:00

111 lines
2.5 KiB
PHP

<?php
namespace App\Domains\Metadata\Models;
use App\Domains\Accounts\Models\Company;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class CustomField extends Model
{
protected $table = 'custom_fields';
use HasFactory;
protected $guarded = [
'id',
];
protected $dates = [
'date_answer',
'date_time_answer',
];
protected $appends = [
'defaultAnswer',
];
protected function casts(): array
{
return [
'options' => 'array',
];
}
public function setTimeAnswerAttribute(mixed $value): void
{
if ($value && $value != null) {
$this->attributes['time_answer'] = date('H:i:s', strtotime($value));
}
}
public function setOptionsAttribute(mixed $value): void
{
$this->attributes['options'] = json_encode($value);
}
public function getDefaultAnswerAttribute()
{
$value_type = getCustomFieldValueKey($this->type);
return $this->$value_type;
}
public function getInUseAttribute()
{
return $this->customFieldValues()->exists();
}
public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}
public function customFieldValues(): HasMany
{
return $this->hasMany(CustomFieldValue::class);
}
public function scopeWhereCompany($query)
{
return $query->where('custom_fields.company_id', request()->header('company'));
}
public function scopeWhereSearch($query, $search)
{
$query->where(function ($query) use ($search) {
$query->where('label', 'LIKE', '%'.$search.'%')
->orWhere('name', 'LIKE', '%'.$search.'%');
});
}
public function scopePaginateData($query, $limit)
{
if ($limit == 'all') {
return $query->get();
}
return $query->paginate($limit);
}
public function scopeApplyFilters($query, array $filters)
{
$filters = collect($filters);
if ($filters->get('type')) {
$query->whereType($filters->get('type'));
}
if ($filters->get('search')) {
$query->whereSearch($filters->get('search'));
}
}
public function scopeWhereType($query, $type)
{
$query->where('custom_fields.model_type', $type);
}
}