120 lines
3.1 KiB
PHP
120 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Deceased;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DeceasedRepository implements DeceasedRepositoryInterface
|
|
{
|
|
/**
|
|
* Get all deceased with optional filtering and pagination
|
|
*
|
|
* @param array $filters
|
|
* @param int $perPage
|
|
* @return LengthAwarePaginator
|
|
*/
|
|
public function getAllPaginated(array $filters = [], int $perPage = 15): LengthAwarePaginator
|
|
{
|
|
$query = Deceased::query();
|
|
|
|
// Apply filters
|
|
if (!empty($filters['search'])) {
|
|
$query->where(function($q) use ($filters) {
|
|
$q->where('last_name', 'LIKE', "%{$filters['search']}%")
|
|
->orWhere('first_name', 'LIKE', "%{$filters['search']}%");
|
|
});
|
|
}
|
|
|
|
// Apply date range filters
|
|
if (!empty($filters['start_date'])) {
|
|
$query->where('death_date', '>=', $filters['start_date']);
|
|
}
|
|
|
|
if (!empty($filters['end_date'])) {
|
|
$query->where('death_date', '<=', $filters['end_date']);
|
|
}
|
|
|
|
// Apply sorting
|
|
$sortBy = $filters['sort_by'] ?? 'created_at';
|
|
$sortOrder = $filters['sort_order'] ?? 'desc';
|
|
$query->orderBy($sortBy, $sortOrder);
|
|
|
|
// Eager load related counts
|
|
$query->withCount(['documents', 'interventions']);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* Find a deceased by ID
|
|
*
|
|
* @param int $id
|
|
* @return Deceased
|
|
*/
|
|
public function findById(int $id): Deceased
|
|
{
|
|
return Deceased::findOrFail($id);
|
|
}
|
|
|
|
/**
|
|
* Create a new deceased record
|
|
*
|
|
* @param array $data
|
|
* @return Deceased
|
|
*/
|
|
public function create(array $data): Deceased
|
|
{
|
|
return DB::transaction(function () use ($data) {
|
|
return Deceased::create($data);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Update an existing deceased record
|
|
*
|
|
* @param Deceased $deceased
|
|
* @param array $data
|
|
* @return Deceased
|
|
*/
|
|
public function update(Deceased $deceased, array $data): Deceased
|
|
{
|
|
return DB::transaction(function () use ($deceased, $data) {
|
|
$deceased->update($data);
|
|
return $deceased;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Delete a deceased record
|
|
*
|
|
* @param Deceased $deceased
|
|
* @return bool
|
|
*/
|
|
public function delete(Deceased $deceased): bool
|
|
{
|
|
return DB::transaction(function () use ($deceased) {
|
|
return $deceased->delete();
|
|
});
|
|
}
|
|
/**
|
|
* Search deceased by name
|
|
*
|
|
* @param string $name
|
|
* @return Collection
|
|
*/
|
|
public function searchByName(string $name): Collection
|
|
{
|
|
return Deceased::where(function($query) use ($name) {
|
|
$query->where('last_name', 'LIKE', "%{$name}%")
|
|
->orWhere('first_name', 'LIKE', "%{$name}%")
|
|
->orWhere(DB::raw("CONCAT(last_name, ' ', first_name)"), 'LIKE', "%{$name}%");
|
|
})
|
|
->orderBy('last_name', 'asc')
|
|
->orderBy('first_name', 'asc')
|
|
->get();
|
|
}
|
|
}
|