150 lines
4.1 KiB
PHP
150 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\PractitionerDocument;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class PractitionerDocumentRepository extends BaseRepository implements PractitionerDocumentRepositoryInterface
|
|
{
|
|
public function __construct(PractitionerDocument $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
/**
|
|
* Get all practitioner documents with optional filtering.
|
|
*/
|
|
public function getAll(array $filters = []): Collection
|
|
{
|
|
$query = $this->model->newQuery()->with(['thanatopractitioner.employee']);
|
|
|
|
// Apply filters
|
|
if (!empty($filters['search'])) {
|
|
$query->where(function ($q) use ($filters) {
|
|
$q->where('doc_type', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('status', 'like', '%' . $filters['search'] . '%');
|
|
});
|
|
}
|
|
|
|
if (!empty($filters['practitioner_id'])) {
|
|
$query->where('practitioner_id', $filters['practitioner_id']);
|
|
}
|
|
|
|
if (!empty($filters['doc_type'])) {
|
|
$query->ofType($filters['doc_type']);
|
|
}
|
|
|
|
if (isset($filters['valid_only'])) {
|
|
if ($filters['valid_only']) {
|
|
$query->valid();
|
|
} else {
|
|
$query->expired();
|
|
}
|
|
}
|
|
|
|
// Apply sorting
|
|
$sortField = $filters['sort_by'] ?? 'created_at';
|
|
$sortDirection = $filters['sort_direction'] ?? 'desc';
|
|
$query->orderBy($sortField, $sortDirection);
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
/**
|
|
* Find a practitioner document by ID.
|
|
*/
|
|
public function findById(int $id): ?PractitionerDocument
|
|
{
|
|
return $this->model->newQuery()
|
|
->with(['thanatopractitioner.employee'])
|
|
->find($id);
|
|
}
|
|
|
|
/**
|
|
* Get documents by practitioner ID.
|
|
*/
|
|
public function getByPractitionerId(int $practitionerId): Collection
|
|
{
|
|
return $this->model->newQuery()
|
|
->where('practitioner_id', $practitionerId)
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get documents by type.
|
|
*/
|
|
public function getByDocumentType(string $docType): Collection
|
|
{
|
|
return $this->model->newQuery()
|
|
->with(['thanatopractitioner.employee'])
|
|
->ofType($docType)
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get valid documents (not expired).
|
|
*/
|
|
public function getValid(): Collection
|
|
{
|
|
return $this->model->newQuery()
|
|
->with(['thanatopractitioner.employee'])
|
|
->valid()
|
|
->orderBy('expiry_date')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get expired documents.
|
|
*/
|
|
public function getExpired(): Collection
|
|
{
|
|
return $this->model->newQuery()
|
|
->with(['thanatopractitioner.employee'])
|
|
->expired()
|
|
->orderBy('expiry_date', 'desc')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get documents with pagination.
|
|
*/
|
|
public function getPaginated(int $perPage = 10): array
|
|
{
|
|
$paginator = $this->model->newQuery()
|
|
->with(['thanatopractitioner.employee'])
|
|
->paginate($perPage);
|
|
|
|
return [
|
|
'documents' => $paginator->getCollection(),
|
|
'pagination' => [
|
|
'current_page' => $paginator->currentPage(),
|
|
'last_page' => $paginator->lastPage(),
|
|
'per_page' => $paginator->perPage(),
|
|
'total' => $paginator->total(),
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get document statistics.
|
|
*/
|
|
public function getStatistics(): array
|
|
{
|
|
return [
|
|
'total' => $this->model->newQuery()->count(),
|
|
'valid' => $this->model->newQuery()->valid()->count(),
|
|
'expired' => $this->model->newQuery()->expired()->count(),
|
|
'by_type' => $this->model->newQuery()
|
|
->selectRaw('doc_type, count(*) as count')
|
|
->groupBy('doc_type')
|
|
->pluck('count', 'doc_type')
|
|
->toArray(),
|
|
];
|
|
}
|
|
}
|