2025-07-02 16:10:37 +07:00
|
|
|
<?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();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 22:18:45 +07:00
|
|
|
// OK - Done
|
2025-07-02 16:10:37 +07:00
|
|
|
public function index() {
|
2025-07-23 11:03:46 +07:00
|
|
|
try {
|
2025-08-01 13:37:13 +07:00
|
|
|
$PatientID = $this->request->getVar('PatientID');
|
|
|
|
|
$AlternatePID = $this->request->getVar('AlternatePID');
|
|
|
|
|
$Prefix = $this->request->getVar('Prefix');
|
2025-08-04 11:01:03 +07:00
|
|
|
$name = $this->request->getVar('Name');
|
2025-08-01 13:37:13 +07:00
|
|
|
$Suffix = $this->request->getVar('Suffix');
|
|
|
|
|
$BirthDate = $this->request->getVar('BirthDate');
|
2025-08-04 11:01:03 +07:00
|
|
|
$startDate = $this->request->getVar('StartDate');
|
|
|
|
|
$endDate = $this->request->getVar('EndDate');
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-08-01 22:26:27 +07:00
|
|
|
$builder = $this->db->table('patient');
|
2025-07-23 11:03:46 +07:00
|
|
|
|
2025-08-01 13:37:13 +07:00
|
|
|
if ($name !== null) {
|
2025-07-23 11:03:46 +07:00
|
|
|
$sql = "LOWER(CONCAT_WS(' ', IFNULL(prefix,''), IFNULL(name_first,''), IFNULL(name_middle,''), IFNULL(name_last,''), IFNULL(name_maiden,''), IFNULL(suffix,'')))";
|
|
|
|
|
$rawSql = new RawSql($sql);
|
2025-08-01 13:37:13 +07:00
|
|
|
$builder->like($rawSql, $name, 'both');
|
2025-07-23 11:03:46 +07:00
|
|
|
}
|
2025-08-01 13:37:13 +07:00
|
|
|
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) {
|
2025-07-23 11:03:46 +07:00
|
|
|
$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'); }
|
|
|
|
|
}
|
2025-08-01 13:37:13 +07:00
|
|
|
*/
|
2025-07-23 11:03:46 +07:00
|
|
|
|
|
|
|
|
$filteredPatients = $builder->get()->getResultArray();
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
// Data pasien tidak ada mengembalikan - success 200
|
|
|
|
|
if (empty($filteredPatients)) {
|
|
|
|
|
return $this->respond([
|
2025-07-16 09:19:47 +07:00
|
|
|
'status' => 'success',
|
2025-07-23 11:03:46 +07:00
|
|
|
'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
|
2025-08-01 22:33:41 +07:00
|
|
|
return $this->failServerError('Something went wrong.'.$e->getMessage());
|
2025-07-23 11:03:46 +07:00
|
|
|
}
|
2025-07-02 16:10:37 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-01 22:18:45 +07:00
|
|
|
// OK - Done
|
|
|
|
|
public function show($InternalPID = null) {
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
try {
|
|
|
|
|
|
2025-08-01 22:26:27 +07:00
|
|
|
$builder = $this->db->table('patient');
|
2025-08-01 22:33:41 +07:00
|
|
|
$patient = $builder->where('InternalPID', ((int) $InternalPID))->get()->getRowArray();
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
// Data pasien tidak ada mengembalikan - success 200
|
|
|
|
|
if (empty($patient)) {
|
|
|
|
|
return $this->respond([
|
2025-07-16 09:19:47 +07:00
|
|
|
'status' => 'success',
|
2025-08-01 22:34:33 +07:00
|
|
|
'message' => 'Patient with ID ' . $InternalPID . ' not found.',
|
2025-07-23 11:03:46 +07:00
|
|
|
'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
|
2025-08-01 22:33:41 +07:00
|
|
|
return $this->failServerError('Something went wrong'.$e->getMessage());
|
2025-07-23 11:03:46 +07:00
|
|
|
}
|
2025-07-16 09:19:47 +07:00
|
|
|
|
2025-07-02 16:10:37 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-01 22:18:45 +07:00
|
|
|
// OK - Done
|
2025-07-02 16:10:37 +07:00
|
|
|
public function create() {
|
|
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
try {
|
2025-08-01 22:18:45 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
|
|
|
|
|
$data = [
|
2025-08-04 10:26:26 +07:00
|
|
|
"PatientID" => $input['PatientID'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"AlternatePID" => $input['AlternatePID'] ?? null,
|
|
|
|
|
"Prefix" => $input['Prefix'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"NameFirst" => $input['NameFirst'] ?? null,
|
|
|
|
|
"NameMiddle" => $input['NameMiddle'] ?? null,
|
|
|
|
|
"NameMaiden" => $input['NameMaiden'] ?? null,
|
|
|
|
|
"NameLast" => $input['NameLast'] ?? null,
|
|
|
|
|
"Suffix" => $input['Suffix'] ?? null,
|
|
|
|
|
"NameAlias" => null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"Gender" => isset($input['Gender']) ? (int) $input['Gender'] : null,
|
|
|
|
|
"PlaceOfBirth" => $input['PlaceOfBirth'] ?? null,
|
|
|
|
|
"BirthDate" => $input['BirthDate'] ?? null,
|
|
|
|
|
"Street_1" => $input['Street_1'] ?? null,
|
|
|
|
|
"Street_2" => $input['Street_2'] ?? null,
|
|
|
|
|
"Street_3" => null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"City" => $input['City'] ?? null,
|
|
|
|
|
"Province" => $input['Province'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"ZIP" => $input['ZIP'] ?? null,
|
|
|
|
|
"CountryID" => isset($input['CountryID']) ? (int) $input['CountryID'] : null,
|
|
|
|
|
"Emailaddress1" => $input['Emailaddress1'] ?? null,
|
|
|
|
|
"Emailaddress2" => $input['Emailaddress2'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"Phone" => $input['Phone'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"Mobilephone" => $input['Mobilephone'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"Mother" => $input['Mother'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"AccountNumber" => isset($input['AccountNumber']) ? (int) $input['AccountNumber'] : null,
|
|
|
|
|
"RaceID" => isset($input['RaceID']) ? (int) $input['RaceID'] : null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"MaritalStatus" => $input['MaritalStatus'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"ReligionID" => isset($input['ReligionID']) ? (int) $input['ReligionID'] : null,
|
|
|
|
|
"EthnicID" => isset($input['EthnicID']) ? (int) $input['EthnicID'] : null,
|
|
|
|
|
"Citizenship" => $input['Citizenship'] ?? null,
|
|
|
|
|
"DeathIndicator" => isset($input['DeathIndicator']) ? (int) $input['DeathIndicator'] : null,
|
|
|
|
|
"DeathDateTime" => $input['DeathDateTime'] ?? null,
|
|
|
|
|
"LinkTo" => $input['LinkTo'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"CreateDate" => date('Y-m-d H:i:s'),
|
2025-08-04 11:01:03 +07:00
|
|
|
"DelDate" => null,
|
2025-08-04 10:26:26 +07:00
|
|
|
|
2025-08-04 11:01:03 +07:00
|
|
|
// Field tambahan dari struktur sebelumnya (bisa dihapus jika tidak dipakai)
|
2025-08-04 10:26:26 +07:00
|
|
|
"PatientComment" => $input['PatientComment'] ?? null,
|
|
|
|
|
"IdentityIDType" => $input['IdentityIDType'] ?? null,
|
|
|
|
|
"IdentityID" => $input['IdentityID'] ?? null
|
2025-07-23 11:03:46 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$rules = [
|
2025-08-01 22:18:45 +07:00
|
|
|
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]',
|
2025-08-01 13:37:13 +07:00
|
|
|
'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',
|
2025-07-23 11:03:46 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 22:18:45 +07:00
|
|
|
$this->db->table('patient')->insert($data);
|
2025-07-23 11:03:46 +07:00
|
|
|
$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) {
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
// Error Server = 500
|
2025-08-01 22:33:41 +07:00
|
|
|
return $this->failServerError('Something went wrong'.$e->getMessage());
|
2025-07-23 11:03:46 +07:00
|
|
|
}
|
2025-07-02 16:10:37 +07:00
|
|
|
}
|
|
|
|
|
|
2025-08-01 22:33:41 +07:00
|
|
|
// OK - Done
|
2025-08-01 22:18:45 +07:00
|
|
|
public function update($InternalPID = null) {
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
try {
|
2025-08-01 22:33:41 +07:00
|
|
|
|
|
|
|
|
$InternalPID = (int) $InternalPID;
|
|
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
$data = [
|
2025-08-04 11:01:03 +07:00
|
|
|
"InternalPID" => $input['InternalPID'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"PatientID" => $input['PatientID'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"AlternatePID" => $input['AlternatePID'] ?? null,
|
|
|
|
|
"Prefix" => $input['Prefix'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"NameFirst" => $input['NameFirst'] ?? null,
|
|
|
|
|
"NameMiddle" => $input['NameMiddle'] ?? null,
|
|
|
|
|
"NameMaiden" => $input['NameMaiden'] ?? null,
|
|
|
|
|
"NameLast" => $input['NameLast'] ?? null,
|
|
|
|
|
"Suffix" => $input['Suffix'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"NameAlias" => null,
|
|
|
|
|
"Gender" => isset($input['Gender']) ? (int) $input['Gender'] : null,
|
|
|
|
|
"PlaceOfBirth" => $input['PlaceOfBirth'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"BirthDate" => $input['BirthDate'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"Street_1" => $input['Street_1'] ?? null,
|
|
|
|
|
"Street_2" => $input['Street_2'] ?? null,
|
|
|
|
|
"Street_3" => null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"City" => $input['City'] ?? null,
|
|
|
|
|
"Province" => $input['Province'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"ZIP" => $input['ZIP'] ?? null,
|
|
|
|
|
"CountryID" => isset($input['CountryID']) ? (int) $input['CountryID'] : null,
|
|
|
|
|
"Emailaddress1" => $input['Emailaddress1'] ?? null,
|
|
|
|
|
"Emailaddress2" => $input['Emailaddress2'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"Phone" => $input['Phone'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"Mobilephone" => $input['Mobilephone'] ?? null,
|
|
|
|
|
"Mother" => $input['Mother'] ?? null,
|
|
|
|
|
"AccountNumber" => isset($input['AccountNumber']) ? (int) $input['AccountNumber'] : null,
|
|
|
|
|
"RaceID" => isset($input['RaceID']) ? (int) $input['RaceID'] : null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"MaritalStatus" => $input['MaritalStatus'] ?? null,
|
2025-08-04 11:01:03 +07:00
|
|
|
"ReligionID" => isset($input['ReligionID']) ? (int) $input['ReligionID'] : null,
|
|
|
|
|
"EthnicID" => isset($input['EthnicID']) ? (int) $input['EthnicID'] : null,
|
|
|
|
|
"Citizenship" => $input['Citizenship'] ?? null,
|
|
|
|
|
"DeathIndicator" => isset($input['DeathIndicator']) ? (int) $input['DeathIndicator'] : null,
|
|
|
|
|
"DeathDateTime" => $input['DeathDateTime'] ?? null,
|
|
|
|
|
"LinkTo" => $input['LinkTo'] ?? null,
|
2025-08-04 10:26:26 +07:00
|
|
|
"CreateDate" => date('Y-m-d H:i:s'),
|
2025-08-04 11:01:03 +07:00
|
|
|
"DelDate" => null,
|
2025-08-04 10:26:26 +07:00
|
|
|
|
2025-08-04 11:01:03 +07:00
|
|
|
// Field tambahan dari struktur sebelumnya (bisa dihapus jika tidak dipakai)
|
2025-08-04 10:26:26 +07:00
|
|
|
"PatientComment" => $input['PatientComment'] ?? null,
|
|
|
|
|
"IdentityIDType" => $input['IdentityIDType'] ?? null,
|
|
|
|
|
"IdentityID" => $input['IdentityID'] ?? null
|
2025-07-23 11:03:46 +07:00
|
|
|
];
|
2025-08-04 11:01:03 +07:00
|
|
|
// $data = [
|
|
|
|
|
// "PatientID" => $input['PatientID'] ?? null,
|
|
|
|
|
// "AlternatePID" => $input['AlternateID'] ?? null,
|
|
|
|
|
// "Prefix" => $input['Title'] ?? null,
|
|
|
|
|
// "NameFirst" => $input['NameFirst'] ?? null,
|
|
|
|
|
// "NameMiddle" => $input['NameMiddle'] ?? null,
|
|
|
|
|
// "NameMaiden" => $input['NameMaiden'] ?? null,
|
|
|
|
|
// "NameLast" => $input['NameLast'] ?? null,
|
|
|
|
|
// "Suffix" => $input['Suffix'] ?? null,
|
|
|
|
|
// "NameAlias" => null,
|
|
|
|
|
// "Gender" => ((int) $input['Gender']) ?? null, //int
|
|
|
|
|
// "PlaceOfBirth" => $input['PlaceOfBirthdate'] ?? null,
|
|
|
|
|
// "BirthDate" => $input['BirthDate'] ?? null,
|
|
|
|
|
// "Street1" => $input['Street1'] ?? null,
|
|
|
|
|
// "Street2" => $input['Street2'] ?? null,
|
|
|
|
|
// "Street3" => 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" => ((int) $input['Mother']) ?? 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
|
|
|
|
|
// ];
|
2025-08-04 10:26:26 +07:00
|
|
|
|
2025-08-01 13:37:13 +07:00
|
|
|
$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',
|
|
|
|
|
];
|
|
|
|
|
|
2025-08-01 22:26:27 +07:00
|
|
|
$existingPatient = $this->db->table('patient')->where('InternalPID', $InternalPID)->get()->getRowArray();
|
2025-07-23 11:03:46 +07:00
|
|
|
|
|
|
|
|
// Mengembalikan 404
|
|
|
|
|
if (empty($existingPatient)) {
|
2025-08-01 22:18:45 +07:00
|
|
|
return $this->failNotFound('Patient with ID ' . $InternalPID . ' not found.');
|
2025-07-02 16:10:37 +07:00
|
|
|
}
|
|
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
// Request dari client tidak valid atau tidak bisa diproses oleh server - 400
|
|
|
|
|
if (!$this->validateData($data, $rules)) {
|
|
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allowedUpdateFields = [
|
2025-08-01 13:37:13 +07:00
|
|
|
'NameFirst', 'NameLast', 'NameMiddle',
|
|
|
|
|
'PatientID', 'AlternatePID', 'BirthDate', 'PlaceOfBirth',
|
|
|
|
|
'Street_1', 'Street_2', 'Street_3', 'City', 'Province', 'ZIP',
|
|
|
|
|
'Emailaddress1', 'Emailaddress2', 'Phone', 'Mobilephone', 'Mother', 'AccountNumber'
|
2025-07-23 11:03:46 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$datas = [];
|
|
|
|
|
foreach ($allowedUpdateFields as $field) {
|
|
|
|
|
if (isset($data[$field])) {
|
|
|
|
|
$datas[$field] = $data[$field];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($data)) {
|
|
|
|
|
return $this->failValidationError('No data provided for update.');
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 23:15:19 +07:00
|
|
|
$this->db->table('patient')->where('InternalPID', $InternalPID)->update($data);
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
// Sukses & Insert = 201 - Kirim data patient ID
|
|
|
|
|
return $this->respondCreated([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Patient updated successfully',
|
|
|
|
|
'data' => $data
|
|
|
|
|
], 201);
|
2025-07-02 16:10:37 +07:00
|
|
|
|
2025-07-23 11:03:46 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
// Error Server = 500
|
2025-08-01 22:33:41 +07:00
|
|
|
return $this->failServerError('Something went wrong '.$e->getMessage());
|
2025-07-23 11:03:46 +07:00
|
|
|
}
|
2025-07-02 16:10:37 +07:00
|
|
|
}
|
2025-07-23 11:03:46 +07:00
|
|
|
|
2025-08-01 22:33:41 +07:00
|
|
|
// OK - Done
|
2025-08-01 22:18:45 +07:00
|
|
|
public function delete($InternalPID = null) {
|
2025-07-23 11:03:46 +07:00
|
|
|
|
|
|
|
|
try {
|
2025-08-01 22:33:41 +07:00
|
|
|
|
|
|
|
|
$InternalPID = (int) $InternalPID;
|
2025-07-23 11:03:46 +07:00
|
|
|
|
2025-08-01 22:18:45 +07:00
|
|
|
if (!$InternalPID) {
|
2025-07-23 11:03:46 +07:00
|
|
|
return $this->failValidationError('Patient ID is required.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cari data pasien
|
2025-08-01 22:26:27 +07:00
|
|
|
$patient = $this->db->table('patient')->where('InternalPID', $InternalPID)->get()->getRow();
|
2025-07-23 11:03:46 +07:00
|
|
|
|
|
|
|
|
if (!$patient) {
|
2025-08-01 22:18:45 +07:00
|
|
|
return $this->failNotFound("Patient ID with {$InternalPID} not found.");
|
2025-07-23 11:03:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hapus data pasien berdasarkan pat_num
|
2025-08-01 22:26:27 +07:00
|
|
|
$this->db->table('patient')->where('InternalPID', $InternalPID)->delete();
|
2025-07-23 11:03:46 +07:00
|
|
|
|
|
|
|
|
// Mengembalikan 200
|
|
|
|
|
return $this->respondDeleted([
|
|
|
|
|
'status' => 'success',
|
2025-08-01 22:18:45 +07:00
|
|
|
'message' => "Patient ID with {$InternalPID} deleted successfully."
|
2025-07-23 11:03:46 +07:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError("Internal server error: " . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 16:10:37 +07:00
|
|
|
}
|