mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +00:00
Complete the type modernization across all models. Adds Builder-typed $query parameters and return types to all scope methods, typed parameters on accessors, and PHPDoc on scopePaginateData/scopeApplyFilters. Models updated: Address, EstimateItem, Expense, ExpenseCategory, InvoiceItem, Item, Note, Tax, TaxType, Unit. 5 models needed no changes (Country, Currency, ImpersonationLog, Module, UserSetting) as they had no untyped public methods.
99 lines
2.5 KiB
PHP
99 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
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 TaxType extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $guarded = [
|
|
'id',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'percent' => 'float',
|
|
'fixed_amount' => 'integer',
|
|
'compound_tax' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public const TYPE_GENERAL = 'GENERAL';
|
|
|
|
public const TYPE_MODULE = 'MODULE';
|
|
|
|
public function taxes(): HasMany
|
|
{
|
|
return $this->hasMany(Tax::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 scopeWhereTaxType(Builder $query, int $tax_type_id): void
|
|
{
|
|
$query->orWhere('id', $tax_type_id);
|
|
}
|
|
|
|
public function scopeApplyFilters(Builder $query, array $filters): void
|
|
{
|
|
$filters = collect($filters);
|
|
|
|
if ($filters->get('tax_type_id')) {
|
|
$query->whereTaxType($filters->get('tax_type_id'));
|
|
}
|
|
|
|
if ($filters->get('company_id')) {
|
|
$query->whereCompany($filters->get('company_id'));
|
|
}
|
|
|
|
if ($filters->get('search')) {
|
|
$query->whereSearch($filters->get('search'));
|
|
}
|
|
|
|
if ($filters->get('orderByField') || $filters->get('orderBy')) {
|
|
$field = $filters->get('orderByField') ? $filters->get('orderByField') : 'payment_number';
|
|
$orderBy = $filters->get('orderBy') ? $filters->get('orderBy') : 'asc';
|
|
$query->whereOrder($field, $orderBy);
|
|
}
|
|
}
|
|
|
|
public function scopeWhereOrder(Builder $query, string $orderByField, string $orderBy): void
|
|
{
|
|
$query->orderBy($orderByField, $orderBy);
|
|
}
|
|
|
|
public function scopeWhereSearch(Builder $query, string $search): void
|
|
{
|
|
$query->where('name', 'LIKE', '%'.$search.'%');
|
|
}
|
|
|
|
/**
|
|
* @return Collection|LengthAwarePaginator
|
|
*/
|
|
public function scopePaginateData(Builder $query, string $limit)
|
|
{
|
|
if ($limit == 'all') {
|
|
return $query->get();
|
|
}
|
|
|
|
return $query->paginate($limit);
|
|
}
|
|
}
|