70 lines
1.9 KiB
PHP
70 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Client;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class ClientRepository extends BaseRepository implements ClientRepositoryInterface
|
|
{
|
|
public function __construct(Client $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
|
|
/**
|
|
* Get paginated clients
|
|
*/
|
|
public function paginate(int $perPage = 15, array $filters = []): LengthAwarePaginator
|
|
{
|
|
$query = $this->model->newQuery();
|
|
|
|
// Apply filters
|
|
if (!empty($filters['search'])) {
|
|
$query->where(function ($q) use ($filters) {
|
|
$q->where('name', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('email', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('vat_number', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('siret', 'like', '%' . $filters['search'] . '%');
|
|
});
|
|
}
|
|
|
|
if (isset($filters['is_active'])) {
|
|
$query->where('is_active', $filters['is_active']);
|
|
}
|
|
|
|
if (!empty($filters['group_id'])) {
|
|
$query->where('group_id', $filters['group_id']);
|
|
}
|
|
|
|
if (!empty($filters['client_category_id'])) {
|
|
$query->where('client_category_id', $filters['client_category_id']);
|
|
}
|
|
|
|
// Apply sorting
|
|
$sortField = $filters['sort_by'] ?? 'created_at';
|
|
$sortDirection = $filters['sort_direction'] ?? 'desc';
|
|
$query->orderBy($sortField, $sortDirection);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
|
|
public function searchByName(string $name, int $perPage = 15, bool $exactMatch = false)
|
|
{
|
|
$query = $this->model->newQuery();
|
|
|
|
if ($exactMatch) {
|
|
$query->where('name', $name);
|
|
} else {
|
|
$query->where('name', 'like', '%' . $name . '%');
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
}
|