New-Thanasoft/thanasoft-back/app/Repositories/EmployeeRepository.php
2025-11-05 17:09:12 +03:00

128 lines
3.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()->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
{
$paginator = $this->model->newQuery()->paginate($perPage);
return [
'employees' => $paginator->getCollection(),
'pagination' => [
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage(),
'per_page' => $paginator->perPage(),
'total' => $paginator->total(),
],
];
}
/**
* Get employees with their thanatopractitioner data.
*/
public function getWithThanatopractitioner(): Collection
{
return $this->model->newQuery()
->with('thanatopractitioner')
->orderBy('last_name')
->get();
}
/**
* 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(),
];
}
}