clqms-be/app/Controllers/Patient.php

174 lines
6.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
use CodeIgniter\Database\RawSql;
class Patient extends Controller {
use ResponseTrait;
public function __construct() {
$this->db = \Config\Database::connect();
}
public function index() {
$pat_num = $this->request->getVar('pat_num');
$pat_altnum = $this->request->getVar('pat_altnum');
$pat_name = $this->request->getVar('pat_name');
$pat_dob = $this->request->getVar('pat_dob');
$start_date = $this->request->getVar('start_date');
$end_date = $this->request->getVar('end_date');
$builder = $this->db->table('patients');
if ($pat_name !== null) {
$sql = "LOWER(CONCAT_WS(' ', IFNULL(prefix,''), IFNULL(name_first,''), IFNULL(name_middle,''), IFNULL(name_last,''), IFNULL(name_maiden,''), IFNULL(suffix,'')))";
$rawSql = new RawSql($sql);
$builder->like($rawSql, $pat_name, 'both');
}
if ($pat_num !== null) { $builder->where('pat_num', $pat_num); }
if ($pat_altnum !== null) { $builder->where('pat_altnum', $pat_altnum); }
if ($pat_dob !== null) { $builder->where('pat_dob', $pat_dob); }
if ($start_date !== null || $end_date !== null) {
$builder->join('requests', 'pat_id=patients.pat_id','left');
if ($start_date !== null) { $builder->where('requests.req_date >=', $start_date . ' 00:00:00'); }
if ($end_date !== null) { $builder->where('requests.req_date <=', $end_date . ' 23:59:00'); }
}
$filteredPatients = $builder->get()->getResultArray();
if (empty($filteredPatients)) {
return $this->failNotFound('No patient records found matching the criteria.');
}
return $this->respond($filteredPatients);
}
public function show($id = null) {
$builder = $this->db->table('patients');
$patient = $builder->where('pat_num', $id)->get()->getRowArray();
if (empty($patient)) {
return $this->failNotFound('Patient with ID ' . $id . ' not found.');
}
return $this->respond($patient);
}
public function create() {
$rules = [
'pat_num' => 'required|is_unique[patients.pat_num]|max_length[50]',
'name_first' => 'required|min_length[3]|max_length[255]',
'name_middle' => 'permit_empty',
'name_maiden' => 'permit_empty',
'name_last' => 'permit_empty',
'birth_date' => 'permit_empty|valid_date[Y-m-d]',
'pat_altnum' => 'permit_empty|max_length[50]',
'address_1' => 'permit_empty',
'address_2' => 'permit_empty',
'address_3' => 'permit_empty',
'city' => 'permit_empty',
];
$data = $this->request->getJSON(true);
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$datas = [
'name_first' => $data['name_first'],
'name_last' => $data['name_last'],
'name_middle' => $data['name_middle'] ?? null,
'name_maiden' => $data['name_maiden'] ?? null,
'pat_num' => $data['pat_num'],
'prefix' => $data['prefix'] ?? null,
'suffix' => $data['suffix'] ?? null,
'birth_date' => $data['pat_dob'] ?? null,
'pat_altnum' => $data['pat_altnum'] ?? null,
'address_1' => $data['address_1'] ?? null,
'address_2' => $data['address_2'] ?? null,
'address_3' => $data['address_3'] ?? null,
'city' => $data['city'] ?? null,
'province' => $data['province'] ?? null,
'zip' => $data['zip'] ?? null,
'email_1' => $data['email_1'] ?? null,
'email_2' => $data['email_2'] ?? null,
'phone' => $data['phone'] ?? null,
'mobile_phone' => $data['mobile_phone'] ?? null,
'mother' => $data['mother'] ?? null,
'account_number' => $data['account_number'] ?? null,
'marital_status' => $data['marital_status'] ?? null,
'country_id' => $data['country_id'] ?? null,
'race_id' => $data['race_id'] ?? null,
'religion_id' => $data['religion_id'] ?? null,
'ethnic_id' => $data['ethnic_id'] ?? null,
'citizenship' => $data['citizenship'] ?? null,
'death' => $data['death'] ?? null,
'death_date' => $data['death_date'] ?? null,
'create_date' => date('Y-m-d H:i:s'),
];
$this->db->table('patients')->insert($datas);
$newPatientId = $this->db->insertID();
return $this->respondCreated([
'message' => 'Patient created successfully',
'pat_id' => $newPatientId
]);
}
public function update($pat_id = null) {
$data = $this->request->getJSON(true);
$existingPatient = $this->db->table('patients')->where('pat_id', $pat_id)->get()->getRowArray();
if (empty($existingPatient)) {
return $this->failNotFound('Patient with ID ' . $pat_id . ' not found.');
}
$rules = [
'pat_num' => 'required|max_length[50]',
'name_first' => 'required|min_length[3]|max_length[255]',
'name_middle' => 'permit_empty',
'name_maiden' => 'permit_empty',
'name_last' => 'permit_empty',
'birth_date' => 'permit_empty|valid_date[Y-m-d]',
'pat_altnum' => 'permit_empty|max_length[50]',
'address_1' => 'permit_empty',
'address_2' => 'permit_empty',
'address_3' => 'permit_empty',
'city' => 'permit_empty',
];
// Validate the input data
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$allowedUpdateFields = [
'name_first', 'name_last', 'name_middle',
'pat_num', 'pat_altnum', 'birth_date', 'birth_place',
'address_1', 'address_2', 'address_3', 'city', 'province', 'zip',
'email_1', 'email_2', 'phone', 'mobile_phone', 'mother', 'account_number'
];
$datas = [];
foreach ($allowedUpdateFields as $field) {
if (isset($data[$field])) {
$datas[$field] = $data[$field];
}
}
if (empty($datas)) {
return $this->failValidationError('No data provided for update.');
}
$this->db->table('patients')->where('pat_id', $pat_id)->update($datas);
return $this->respond([
'message' => 'Patient updated successfully',
'pat_id' => $pat_id
]);
}
}