mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-02 21:31:01 +00:00
* 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
78 lines
1.9 KiB
PHP
78 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Catalog\Models;
|
|
|
|
use App\Domains\Accounts\Models\Company;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Unit extends Model
|
|
{
|
|
protected $table = 'units';
|
|
|
|
use HasFactory;
|
|
|
|
protected $fillable = ['name', 'company_id'];
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(Item::class);
|
|
}
|
|
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class);
|
|
}
|
|
|
|
public function scopeWhereCompany(Builder $query): void
|
|
{
|
|
$query->where('company_id', request()->header('company'));
|
|
}
|
|
|
|
public function scopeWhereUnit(Builder $query, int $unit_id): void
|
|
{
|
|
$query->orWhere('id', $unit_id);
|
|
}
|
|
|
|
public function scopeWhereSearch(Builder $query, string $search): Builder
|
|
{
|
|
return $query->where('name', 'LIKE', '%'.$search.'%');
|
|
}
|
|
|
|
public function scopeApplyFilters(Builder $query, array $filters): Builder
|
|
{
|
|
$filters = collect($filters);
|
|
|
|
if ($filters->get('search')) {
|
|
$query->whereSearch($filters->get('search'));
|
|
}
|
|
|
|
if ($filters->get('unit_id')) {
|
|
$query->whereUnit($filters->get('unit_id'));
|
|
}
|
|
|
|
if ($filters->get('company_id')) {
|
|
$query->whereCompany($filters->get('company_id'));
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* @return Collection|LengthAwarePaginator
|
|
*/
|
|
public function scopePaginateData(Builder $query, string $limit)
|
|
{
|
|
if ($limit == 'all') {
|
|
return $query->get();
|
|
}
|
|
|
|
return $query->paginate($limit);
|
|
}
|
|
}
|