New-Thanasoft/thanasoft-back/app/Repositories/EmployeeRepository.php
2026-04-29 08:30:15 +03:00

156 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Repositories;
use App\Models\Employee;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
class EmployeeRepository extends BaseRepository implements EmployeeRepositoryInterface
{
public function __construct(Employee $model)
{
parent::__construct($model);
}
/**
* Get all employees with optional filtering.
*/
public function getAll(array $filters = []): Collection
{
$query = $this->model->newQuery();
// Apply filters
if (!empty($filters['search'])) {
$query->search($filters['search']);
}
if (isset($filters['active'])) {
if ($filters['active']) {
$query->active();
} else {
$query->inactive();
}
}
// Apply sorting
$sortField = $filters['sort_by'] ?? 'last_name';
$sortDirection = $filters['sort_direction'] ?? 'asc';
$query->orderBy($sortField, $sortDirection);
return $query->get();
}
/**
* Find an employee by ID.
*/
public function findById(int $id): ?Employee
{
return $this->model->newQuery()->with(['thanatopractitioner', 'user'])->find($id);
}
/**
* Find an employee by email.
*/
public function findByEmail(string $email): ?Employee
{
return $this->model->newQuery()->where('email', $email)->first();
}
/**
* Get active employees only.
*/
public function getActive(): Collection
{
return $this->model->newQuery()->active()->get();
}
/**
* Get inactive employees only.
*/
public function getInactive(): Collection
{
return $this->model->newQuery()->inactive()->get();
}
/**
* Search employees by term.
*/
public function search(string $term): Collection
{
return $this->model->newQuery()->search($term)->get();
}
/**
* Get employees with pagination.
*/
public function getPaginated(int $perPage = 10, array $filters = []): array
{
$query = $this->model->newQuery()->with(['thanatopractitioner', 'user']);
if (!empty($filters['search'])) {
$query->search($filters['search']);
}
if (array_key_exists('active', $filters)) {
if (filter_var($filters['active'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) {
$query->active();
} else {
$query->inactive();
}
}
$sortField = $filters['sort_by'] ?? 'last_name';
$sortDirection = $filters['sort_direction'] ?? 'asc';
$paginator = $query
->orderBy($sortField, $sortDirection)
->paginate($perPage);
return [
'employees' => $paginator->getCollection(),
'pagination' => [
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage(),
'per_page' => $paginator->perPage(),
'total' => $paginator->total(),
'from' => $paginator->firstItem(),
'to' => $paginator->lastItem(),
],
];
}
/**
* Get employees with their thanatopractitioner data.
*/
public function getWithThanatopractitioner(): Collection
{
return $this->model->newQuery()
->with(['thanatopractitioner', 'user'])
->orderBy('last_name')
->get();
}
public function find(int|string $id, array $columns = ['*']): ?\Illuminate\Database\Eloquent\Model
{
return $this->model->newQuery()
->with(['thanatopractitioner', 'user'])
->find($id, $columns);
}
/**
* Get employee statistics.
*/
public function getStatistics(): array
{
return [
'total' => $this->model->newQuery()->count(),
'active' => $this->model->newQuery()->active()->count(),
'inactive' => $this->model->newQuery()->inactive()->count(),
'with_thanatopractitioner' => $this->model->newQuery()->has('thanatopractitioner')->count(),
];
}
}