Viewing File: /home/assersoft/public_html/doctor-assistant/app/Controllers/AppointmentsController.php
<?php
namespace App\Controllers;
use App\Models\TreatmentsModel;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
class AppointmentsController extends ResourceController
{
protected $modelName = 'App\Models\AppointmentsModel';
protected $format = 'json';
/**
* Return an array of resource objects, themselves in array format.
*
* @return ResponseInterface
*/
public function index()
{
$rules = [
'clinic_id' => 'required|numeric',
'patient_id' => 'required|numeric',
'treatment_id' => 'permit_empty|numeric',
];
if(!$this->validate($rules)){
return $this->fail($this->validator->getErrors());
}
$clinic_id = $this->request->getVar('clinic_id');
$patient_id = $this->request->getVar('patient_id');
$treatment_id = $this->request->getVar('treatment_id') ?? null;
if($treatment_id != null){
$appointments = $this->model->where('clinic_id', $clinic_id)->where('patient_id', $patient_id)->where('treatment_id', $treatment_id)->findAll();
}else{
$appointments = $this->model->where('clinic_id', $clinic_id)->where('patient_id', $patient_id)->findAll();
}
return $this->respond($appointments);
}
/**
* Return the properties of a resource object.
*
* @param int|string|null $id
*
* @return ResponseInterface
*/
public function show($id = null)
{
$data = $this->model->find($id);
if($data == null){
return $this->failNotFound("Appointment not found");
}
return $this->respond($data);
}
/**
* Create a new resource object, from "posted" parameters.
*
* @return ResponseInterface
*/
public function create()
{
$rules = [
'appointment_date' => 'required|valid_date',
'appointment_time' => 'required',
'session_time' => 'required',
'appointment_status' => 'required',
'appointment_type' => 'required|in_list[consultation, treatment]',
'doctor_remarks' => 'permit_empty',
'treatment_id' => 'permit_empty|numeric',
'appointment_fees' => 'permit_empty|numeric',
'patient_id' => 'required|numeric',
'doctor_id' => 'required|numeric',
'receptionist_id' => 'permit_empty|numeric',
'clinic_id' => 'required|numeric',
];
if(!$this->validate($rules)){
return $this->fail($this->validator->getErrors());
}
$data = $this->request->getVar();
$this->model->insert($data);
if(!$this->model->insertID){
return $this->fail("Failed to create appointment");
}
if(property_exists($data, 'treatment_id') && property_exists($data, 'appointment_fees') && property_exists($data, 'appointment_type')){
$treatmentModel = new TreatmentsModel();
$treatment = $treatmentModel->find($data->treatment_id);
if($treatment == null){
return $this->failNotFound("Treatment not found");
}
$treatmentModel->update($data->treatment_id, [
'paid_amount' => $treatment['paid_amount'] + $data->appointment_fees,
'due_amount' => $treatment['due_amount'] - $data->appointment_fees,
]);
}
return $this->respondCreated(['appointment_id' => $this->model->insertID]);
}
/**
* Add or update a model resource, from "posted" properties.
*
* @param int|string|null $id
*
* @return ResponseInterface
*/
public function update($id = null)
{
$rules = [
'appointment_date' => 'permit_empty|valid_date',
'appointment_time' => 'permit_empty',
'session_time' => 'permit_empty',
'appointment_status' => 'permit_empty',
'appointment_type' => 'permit_empty|in_list[consultation, treatment]',
'doctor_remarks' => 'permit_empty',
'treatment_id' => 'permit_empty|numeric',
'appointment_fees' => 'permit_empty|numeric',
'patient_id' => 'permit_empty|numeric',
'doctor_id' => 'permit_empty|numeric',
'receptionist_id' => 'permit_empty|numeric',
];
if(!$this->validate($rules)){
return $this->fail($this->validator->getErrors());
}
$app = $this->model->find($id);
if($app == null){
return $this->failNotFound("Appointment not found");
}
$data = $this->request->getVar();
if(!$this->model->update($id, $data)){
return $this->fail("Failed to update appointment");
}
if(property_exists($data, 'appointment_status') &&
property_exists($data, 'appointment_fees') &&
$app['appointment_type'] == 'treatment' &&
$app['treatment_id'] &&
$data->appointment_status == 'completed'
) {
$treatmentModel = new TreatmentsModel();
$treatment = $treatmentModel->find($app['treatment_id']);
if(!$treatment){
return $this->failNotFound("Treatment not found");
}
$treatmentModel->update($app['treatment_id'], [
'paid_amount' => $treatment['paid_amount'] + $app['appointment_fees'],
'due_amount' => $treatment['due_amount'] - $app['appointment_fees'],
]);
}
return $this->respondUpdated(['appointment_id' => $id]);
}
/**
* Delete the designated resource object from the model.
*
* @param int|string|null $id
*
* @return ResponseInterface
*/
public function delete($id = null)
{
$data = $this->model->find($id);
if($data == null){
return $this->failNotFound("Appointment not found");
}
$this->model->delete($id);
return $this->respondDeleted(['appointment_id' => $id]);
}
public function findByStatus(){
$clinic_id = $this->request->getVar('clinic_id');
$status = $this->request->getVar('status');
$page = $this->request->getVar('page') ?? 1;
$perPage = $this->request->getVar('perPage') ?? 10;
$data = $this->model->where('clinic_id', $clinic_id)->where('appointment_status', $status)->orderBy('appointment_date', 'desc')->paginate($perPage, 'default', $page);
$pager = service('pager');
$pagination = [
'current_page' => $page,
'perPage' => $perPage,
'total_pages' => $pager->getPageCount(),
'total_items' => $pager->getTotal('default'),
];
return $this->respond([
'pagination' => $pagination,
'data' => $data
]);
}
public function findByDate() {
$clinic_id = $this->request->getVar('clinic_id');
$date = $this->request->getVar('date');
if(!$this->validate(['clinic_id' => 'required|numeric', 'date' => 'required|valid_date'])) {
return $this->fail($this->validator->getErrors());
}
$data = $this->model->where('clinic_id', $clinic_id)->where('appointment_date', $date)->findAll();
return $this->respond($data);
}
public function findByDoctor() {
$clinic_id = $this->request->getVar('clinic_id');
$doctor_id = $this->request->getVar('doctor_id');
$page = $this->request->getVar('page') ?? 1;
$perPage = $this->request->getVar('perPage') ?? 10;
$data = $this->model->where('clinic_id', $clinic_id)->where('doctor_id', $doctor_id)->orderBy('appointment_date', 'desc')->paginate($perPage, 'default', $page);
$pager = service('pager');
$pagination = [
'current_page' => $page,
'perPage' => $perPage,
'total_pages' => $pager->getPageCount(),
'total_items' => $pager->getTotal('default'),
];
return $this->respond([
'pagination' => $pagination,
'data' => $data
]);
}
public function findByDoctorAndDate() {
$clinic_id = $this->request->getVar('clinic_id');
$doctor_id = $this->request->getVar('doctor_id');
$date = $this->request->getVar('date');
if(!$this->validate(['date' => 'required|valid_date'])) {
return $this->fail($this->validator->getErrors());
}
$data = $this->model->where('clinic_id', $clinic_id)->where('doctor_id', $doctor_id)->findAll();
return $this->respond($data);
}
public function findByStatusAndDate() {
$clinic_id = $this->request->getVar('clinic_id');
$date = $this->request->getVar('date');
$status = $this->request->getVar('status');
if(!$this->validate(['date' => 'required|valid_date'])) {
return $this->fail($this->validator->getErrors());
}
$data = $this->model->where('clinic_id', $clinic_id)->where('appointment_date', $date)->where('appointment_status', $status)->findAll();
return $this->respond($data);
}
public function findByTreatment() {
$clinic_id = $this->request->getVar('clinic_id');
$treatment_id = $this->request->getVar('treatment_id');
$data = $this->model->where('clinic_id', $clinic_id)->where('treatment_id', $treatment_id)->findAll();
return $this->respond($data);
}
}
Back to Directory
File Manager