clqms-be/app/Controllers/Patient.php

323 lines
14 KiB
PHP

<?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();
}
// OK - Done
public function index() {
try {
$PatientID = $this->request->getVar('PatientID');
$AlternatePID = $this->request->getVar('AlternatePID');
$Prefix = $this->request->getVar('Prefix');
$name = $this->request->getVar('name');
$Suffix = $this->request->getVar('Suffix');
$BirthDate = $this->request->getVar('BirthDate');
$startDate = $this->request->getVar('startDate');
$endDate = $this->request->getVar('endDate');
$builder = $this->db->table('patient');
if ($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, $name, 'both');
}
if ($PatientID !== null) { $builder->where('PatientID', $pat_num); }
if ($AlternatePID !== null) { $builder->where('AlternatePID', $pat_altnum); }
if ($BirthDate !== null) { $builder->where('BirthDate', $pat_dob); }
/*
if ($startDate !== null || $endDate !== 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();
// Data pasien tidak ada mengembalikan - success 200
if (empty($filteredPatients)) {
return $this->respond([
'status' => 'success',
'message' => 'No patient records found matching the criteria.',
'data' => []
], 200);
}
// Data pasien ditemukan dan mengembalikan - success 200
return $this->respond([
'status' => 'success',
'message'=> "Patients fetched successfully",
'data' => $filteredPatients,
], 200);
} catch (\Exception $e) {
// Error Server Mengembalikan 500
return $this->failServerError('Something went wrong.');
}
}
// OK - Done
public function show($InternalPID = null) {
try {
$builder = $this->db->table('patient');
$patient = $builder->where('InternalPID', $InternalPID)->get()->getRowArray();
// Data pasien tidak ada mengembalikan - success 200
if (empty($patient)) {
return $this->respond([
'status' => 'success',
'message' => 'Patient with ID ' . $id . ' not found.',
'data' => [],
], 200);
}
// Data pasien ditemukan dan mengembalikan - success 200
return $this->respond([
'status' => 'success',
'message'=> "Patient Show Successfully",
'data' => $patient,
], 200);
} catch (\Exception $e) {
// Error Server Mengembalikan 500
return $this->failServerError('Something went wrong');
}
}
// OK - Done
public function create() {
try {
$input = $this->request->getJSON(true);
$data = [
"PatientID" => $input['patientID'] ?? null,
"AlternatePID" => $input['alternateID'] ?? null,
"Prefix" => $input['title'] ?? null,
"NameFirst" => $input['firstName'] ?? null,
"NameMiddle" => $input['middleName'] ?? null,
"NameMaiden" => $input['maidenName'] ?? null,
"NameLast" => $input['lastName'] ?? null,
"Suffix" => $input['suffixName'] ?? null,
"NameAlias" => null,
"Gender" => ((int) $input['gender']) ?? null, //int
"PlaceOfBirth" => $input['placeOfBirthdate'] ?? null,
"BirthDate" => $input['birthdate'] ?? null,
"Street_1" => $input['street1'] ?? null,
"Street_2" => $input['street2'] ?? null,
"Street_3" => null,
"City" => $input['city'] ?? null,
"Province" => $input['province'] ?? null,
"ZIP" => null,
"CountryID" => null, // int
"EmailAddress1" => $input['email1'] ?? null,
"EmailAddress2" => $input['email2'] ?? null,
"Phone" => $input['phone'] ?? null,
"Mobilephone" => $input['mobile'] ?? null,
// "Mother" => $input['motherName'] ?? null, //int
"Accountnumber" => null, //int
"RaceID" => ((int) $input['race']) ?? null, //int
"MaritalStatus" => $input['maritalStatus'] ?? null,
"ReligionID" => ((int)$input['religion']) ?? null, //int
"EthnicID" => ((int)$input['ethnic']) ?? null, //int
"Citizenship" => null,
"DeathIndicator" => ((int)$input['death']) ?? null, //int
"DeathDateTime" => $input['deathTime'] ?? null,
"CreateDate" => date('Y-m-d H:i:s')
// "LinkTo" => $input['linkTo'] ?? null,
// "PatientComment" => $input['patientComment'] ?? null,
// "IdentityIDType" => $input['identityIDType'] ?? null,
// "IdentityID" => $input['identityID'] ?? null,
];
$rules = [
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]',
'NameFirst' => 'required|min_length[3]|max_length[255]',
'NameMiddle' => 'permit_empty',
'NameMaiden' => 'permit_empty',
'NameLast' => 'permit_empty',
'AlternatePID' => 'permit_empty|max_length[50]',
'Street_1' => 'permit_empty',
'Street_2' => 'permit_empty',
'Street_3' => 'permit_empty',
'City' => 'permit_empty',
];
// Request dari client tidak valid atau tidak bisa diproses oleh server - 400
if (!$this->validateData($data, $rules)) {
return $this->respond([
'status' => 'error',
'message' => 'Validation failed',
'errors' => $this->validator->getErrors()
], 400);
}
$this->db->table('patient')->insert($data);
$newPatientId = $this->db->insertID();
// Sukses & Insert = 201 - Kirim data patient ID
return $this->respondCreated([
'status' => 'success',
'message' => 'Patient created successfully',
'data' => $newPatientId
], 201);
} catch (\Exception $e) {
// Error Server = 500
return $this->failServerError('Something went wrong'.$e);
}
}
// OK
public function update($InternalPID = null) {
try {
$input = $this->request->getJSON(true);
$data = [
"PatientID" => $input['patientID'] ?? null,
"AlternatePID" => $input['alternateID'] ?? null,
"Prefix" => $input['title'] ?? null,
"NameFirst" => $input['firstName'] ?? null,
"NameMiddle" => $input['middleName'] ?? null,
"NameMaiden" => $input['maidenName'] ?? null,
"NameLast" => $input['lastName'] ?? null,
"Suffix" => $input['suffixName'] ?? null,
"NameAlias" => null,
"Gender" => ((int) $input['gender']) ?? null, //int
"PlaceOfBirth" => $input['placeOfBirthdate'] ?? null,
"BirthDate" => $input['birthdate'] ?? null,
"Street_1" => $input['street1'] ?? null,
"Street_2" => $input['street2'] ?? null,
"Street_3" => null,
"City" => $input['city'] ?? null,
"Province" => $input['province'] ?? null,
"ZIP" => null,
"CountryID" => null, // int
"EmailAddress1" => $input['email1'] ?? null,
"EmailAddress2" => $input['email2'] ?? null,
"Phone" => $input['phone'] ?? null,
"Mobilephone" => $input['mobile'] ?? null,
// "Mother" => $input['motherName'] ?? null, //int
"Accountnumber" => null, //int
"RaceID" => ((int) $input['race']) ?? null, //int
"MaritalStatus" => $input['maritalStatus'] ?? null,
"ReligionID" => ((int)$input['religion']) ?? null, //int
"EthnicID" => ((int)$input['ethnic']) ?? null, //int
"Citizenship" => null,
"DeathIndicator" => ((int)$input['death']) ?? null, //int
"DeathDateTime" => $input['deathTime'] ?? null,
"CreateDate" => date('Y-m-d H:i:s')
// "LinkTo" => $input['linkTo'] ?? null,
// "PatientComment" => $input['patientComment'] ?? null,
// "IdentityIDType" => $input['identityIDType'] ?? null,
// "IdentityID" => $input['identityID'] ?? null,
];
$rules = [
'NameFirst' => 'required|min_length[3]|max_length[255]',
'NameMiddle' => 'permit_empty',
'NameMaiden' => 'permit_empty',
'NameLast' => 'permit_empty',
// 'birth_date' => 'permit_empty|valid_date[Y-m-d]|not_in_list[0000-00-00]',
'AlternatePID' => 'permit_empty|max_length[50]',
'Street_1' => 'permit_empty',
'Street_2' => 'permit_empty',
'Street_3' => 'permit_empty',
'City' => 'permit_empty',
];
$existingPatient = $this->db->table('patient')->where('InternalPID', $InternalPID)->get()->getRowArray();
// Mengembalikan 404
if (empty($existingPatient)) {
return $this->failNotFound('Patient with ID ' . $InternalPID . ' not found.');
}
// Request dari client tidak valid atau tidak bisa diproses oleh server - 400
if (!$this->validateData($data, $rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$allowedUpdateFields = [
'NameFirst', 'NameLast', 'NameMiddle',
'PatientID', 'AlternatePID', 'BirthDate', 'PlaceOfBirth',
'Street_1', 'Street_2', 'Street_3', 'City', 'Province', 'ZIP',
'Emailaddress1', 'Emailaddress2', 'Phone', 'Mobilephone', 'Mother', 'AccountNumber'
];
$datas = [];
foreach ($allowedUpdateFields as $field) {
if (isset($data[$field])) {
$datas[$field] = $data[$field];
}
}
if (empty($data)) {
return $this->failValidationError('No data provided for update.');
}
$this->db->table('patient')->where('PatientID', $InternalPID)->update($data);
// Sukses & Insert = 201 - Kirim data patient ID
return $this->respondCreated([
'status' => 'success',
'message' => 'Patient updated successfully',
'data' => $data
], 201);
} catch (\Exception $e) {
// Error Server = 500
return $this->failServerError('Something went wrong '.$e);
}
}
// OK
public function delete($InternalPID = null) {
try {
if (!$InternalPID) {
return $this->failValidationError('Patient ID is required.');
}
// Cari data pasien
$patient = $this->db->table('patient')->where('InternalPID', $InternalPID)->get()->getRow();
if (!$patient) {
return $this->failNotFound("Patient ID with {$InternalPID} not found.");
}
// Hapus data pasien berdasarkan pat_num
$this->db->table('patient')->where('InternalPID', $InternalPID)->delete();
// Mengembalikan 200
return $this->respondDeleted([
'status' => 'success',
'message' => "Patient ID with {$InternalPID} deleted successfully."
]);
} catch (\Exception $e) {
return $this->failServerError("Internal server error: " . $e->getMessage());
}
}
}