New-Thanasoft/thanasoft-back/app/Repositories/InterventionRepository.php
2025-11-21 17:26:43 +03:00

157 lines
4.2 KiB
PHP

<?php
namespace App\Repositories;
use App\Models\Intervention;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
class InterventionRepository implements InterventionRepositoryInterface
{
/**
* Get all interventions with optional filtering and pagination
*
* @param array $filters
* @param int $perPage
* @return LengthAwarePaginator
*/
public function getAllPaginated(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = Intervention::query();
// Apply filters
if (!empty($filters['client_id'])) {
$query->where('client_id', $filters['client_id']);
}
if (!empty($filters['deceased_id'])) {
$query->where('deceased_id', $filters['deceased_id']);
}
if (!empty($filters['status'])) {
$query->where('status', $filters['status']);
}
if (!empty($filters['type'])) {
$query->where('type', $filters['type']);
}
// Date range filters
if (!empty($filters['start_date'])) {
$query->where('scheduled_at', '>=', $filters['start_date']);
}
if (!empty($filters['end_date'])) {
$query->where('scheduled_at', '<=', $filters['end_date']);
}
// Apply sorting
$sortBy = $filters['sort_by'] ?? 'created_at';
$sortOrder = $filters['sort_order'] ?? 'desc';
$query->orderBy($sortBy, $sortOrder);
// Eager load related models
$query->with([
'client',
'deceased',
'location',
'assignedPractitioner'
]);
return $query->paginate($perPage);
}
/**
* Find an intervention by ID
*
* @param int $id
* @return Intervention
*/
public function findById(int $id): Intervention
{
return Intervention::with([
'client',
'deceased',
'location',
'practitioners',
'attachments',
'notifications'
])->findOrFail($id);
}
/**
* Create a new intervention record
*
* @param array $data
* @return Intervention
*/
public function create(array $data): Intervention
{
return DB::transaction(function () use ($data) {
return Intervention::create($data);
});
}
/**
* Update an existing intervention record
*
* @param Intervention $intervention
* @param array $data
* @return Intervention
*/
public function update(Intervention $intervention, array $data): Intervention
{
return DB::transaction(function () use ($intervention, $data) {
$intervention->update($data);
return $intervention;
});
}
/**
* Delete an intervention record
*
* @param Intervention $intervention
* @return bool
*/
public function delete(Intervention $intervention): bool
{
return DB::transaction(function () use ($intervention) {
return $intervention->delete();
});
}
/**
* Change the status of an intervention
*
* @param Intervention $intervention
* @param string $status
* @return Intervention
*/
public function changeStatus(Intervention $intervention, string $status): Intervention
{
return DB::transaction(function () use ($intervention, $status) {
$intervention->update(['status' => $status]);
return $intervention;
});
}
/**
* Get interventions for a specific month
*
* @param int $year
* @param int $month
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getByMonth(int $year, int $month): \Illuminate\Database\Eloquent\Collection
{
$startDate = sprintf('%04d-%02d-01', $year, $month);
$endDate = date('Y-m-t', strtotime($startDate));
return Intervention::query()
->whereBetween('scheduled_at', [$startDate . ' 00:00:00', $endDate . ' 23:59:59'])
->with(['client', 'deceased', 'location', 'assignedPractitioner'])
->orderBy('scheduled_at', 'asc')
->get();
}
}