122 lines
3.5 KiB
PHP
122 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\DeceasedDocument;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class DeceasedDocumentRepository extends BaseRepository implements DeceasedDocumentRepositoryInterface
|
|
{
|
|
public function __construct(DeceasedDocument $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
/**
|
|
* Get paginated deceased documents with optional filters
|
|
*/
|
|
public function paginate(int $perPage = 15, array $filters = []): LengthAwarePaginator
|
|
{
|
|
$query = $this->model->newQuery()->with(['deceased', 'file']);
|
|
|
|
// Apply filters
|
|
if (!empty($filters['search'])) {
|
|
$query->where(function ($q) use ($filters) {
|
|
$q->where('doc_type', 'like', '%' . $filters['search'] . '%')
|
|
->orWhereHas('deceased', function ($q) use ($filters) {
|
|
$q->where('first_name', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('last_name', 'like', '%' . $filters['search'] . '%');
|
|
});
|
|
});
|
|
}
|
|
|
|
if (!empty($filters['deceased_id'])) {
|
|
$query->where('deceased_id', $filters['deceased_id']);
|
|
}
|
|
|
|
if (!empty($filters['doc_type'])) {
|
|
$query->where('doc_type', $filters['doc_type']);
|
|
}
|
|
|
|
if (!empty($filters['file_id'])) {
|
|
$query->where('file_id', $filters['file_id']);
|
|
}
|
|
|
|
// Apply sorting
|
|
$sortField = $filters['sort_by'] ?? 'created_at';
|
|
$sortDirection = $filters['sort_direction'] ?? 'desc';
|
|
$query->orderBy($sortField, $sortDirection);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* Get documents by deceased ID
|
|
*/
|
|
public function getByDeceasedId(int $deceasedId): Collection
|
|
{
|
|
return $this->model->newQuery()
|
|
->with(['deceased', 'file'])
|
|
->where('deceased_id', $deceasedId)
|
|
->orderBy('generated_at', 'desc')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get documents by document type
|
|
*/
|
|
public function getByDocType(string $docType): Collection
|
|
{
|
|
return $this->model->newQuery()
|
|
->with(['deceased', 'file'])
|
|
->where('doc_type', $docType)
|
|
->orderBy('generated_at', 'desc')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get documents by file ID
|
|
*/
|
|
public function getByFileId(int $fileId): Collection
|
|
{
|
|
return $this->model->newQuery()
|
|
->with(['deceased', 'file'])
|
|
->where('file_id', $fileId)
|
|
->orderBy('generated_at', 'desc')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Search documents by various criteria
|
|
*/
|
|
public function search(array $criteria): Collection
|
|
{
|
|
$query = $this->model->newQuery()->with(['deceased', 'file']);
|
|
|
|
if (!empty($criteria['deceased_id'])) {
|
|
$query->where('deceased_id', $criteria['deceased_id']);
|
|
}
|
|
|
|
if (!empty($criteria['doc_type'])) {
|
|
$query->where('doc_type', $criteria['doc_type']);
|
|
}
|
|
|
|
if (!empty($criteria['file_id'])) {
|
|
$query->where('file_id', $criteria['file_id']);
|
|
}
|
|
|
|
if (!empty($criteria['generated_from'])) {
|
|
$query->where('generated_at', '>=', $criteria['generated_from']);
|
|
}
|
|
|
|
if (!empty($criteria['generated_to'])) {
|
|
$query->where('generated_at', '<=', $criteria['generated_to']);
|
|
}
|
|
|
|
return $query->orderBy('generated_at', 'desc')->get();
|
|
}
|
|
}
|