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-09-04 11:05:13 +07:00
$this -> now = date ( 'Y-m-d H:i:s' );
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 index () {
2025-07-23 11:03:46 +07:00
try {
2025-08-04 13:21:13 +07:00
$InternalPID = $this -> request -> getVar ( 'InternalPID' );
2025-08-01 13:37:13 +07:00
$PatientID = $this -> request -> getVar ( 'PatientID' );
2025-08-04 13:21:13 +07:00
$Name = $this -> request -> getVar ( 'Name' );
2025-08-07 09:47:59 +07:00
$Birthdate = $this -> request -> getVar ( 'Birthdate' );
2025-08-05 10:34:58 +07:00
$qname = " LOWER(CONCAT_WS(' ', IFNULL(Prefix,''), IFNULL(NameFirst,''), IFNULL(NameMiddle,''), IFNULL(NameLast,''), IFNULL(NameMaiden,''), IFNULL(Suffix,''))) " ;
2025-08-01 22:26:27 +07:00
$builder = $this -> db -> table ( 'patient' );
2025-08-07 09:47:59 +07:00
$builder -> select ( " InternalPID, PatientID, $qname as FullName, Gender, Birthdate, EmailAddress1 as Email, MobilePhone " );
2025-08-04 13:21:13 +07:00
if ( $Name !== null ) {
2025-08-05 10:34:58 +07:00
$sql = $qname ;
2025-07-23 11:03:46 +07:00
$rawSql = new RawSql ( $sql );
2025-08-04 13:21:13 +07:00
$builder -> like ( $rawSql , $Name , 'both' );
2025-07-23 11:03:46 +07:00
}
2025-08-04 13:21:13 +07:00
if ( $InternalPID !== null ) { $builder -> where ( 'InternalPID' , $InternalPID ); }
2025-08-05 11:38:11 +07:00
if ( $PatientID !== null ) { $builder -> like ( 'PatientID' , $PatientID , 'both' ); }
2025-08-07 09:47:59 +07:00
if ( $Birthdate !== null ) { $builder -> where ( 'Birthdate' , $Birthdate ); }
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-11 12:50:57 +07:00
$patient = $this -> db -> table ( 'patient' )
2025-09-04 11:05:13 +07:00
-> select ( 'patient.*, country.Country as CountryName, race.Race as RaceName, religion.Religion as ReligionName, ethnic.Ethnic as EthnicName, patcom.Comment as Comment' )
2025-08-12 08:27:25 +07:00
-> join ( 'country' , 'country.IntCountryID = patient.IntCountryID' , 'left' )
-> join ( 'race' , 'race.RaceID = patient.RaceID' , 'left' )
-> join ( 'religion' , 'religion.ReligionID = patient.ReligionID' , 'left' )
-> join ( 'ethnic' , 'ethnic.EthnicID = patient.EthnicID' , 'left' )
2025-09-04 11:05:13 +07:00
-> join ( 'patcom' , 'patcom.InternalPID = patient.InternalPID' , 'left' )
-> where ( 'patient.InternalPID' , ( int ) $InternalPID )
2025-08-11 12:50:57 +07:00
-> get ()
-> getRowArray ();
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 );
2025-08-11 12:50:57 +07:00
} else {
2025-08-11 16:14:26 +07:00
$patient [ " Age " ] = $this -> calculateAgeFromBirthdate ( $patient [ " Birthdate " ]);
2025-08-12 11:31:29 +07:00
$patient [ " Birthdate " ] = $this -> formatedDate ( $patient [ " Birthdate " ]);
$patient [ " CreateDate " ] = $this -> formatedDate ( $patient [ " CreateDate " ]);
2025-08-12 16:02:02 +07:00
$patient [ " DelDate " ] = $this -> formatedDate ( $patient [ " DelDate " ]);
2025-08-12 11:31:29 +07:00
$patient [ " DeathDateTime " ] = $this -> formatedDate ( $patient [ " DeathDateTime " ]);
$patient [ " BirthdateConversion " ] = $this -> formatedDateForDisplay ( $patient [ " Birthdate " ]);
2025-08-12 11:03:03 +07:00
2025-08-11 14:37:47 +07:00
if ( $patient [ 'LinkTo' ] != null ) {
$ids = explode ( ',' , $patient [ 'LinkTo' ]); // dari string jadi array
$patientLinkTo = $this -> db -> table ( 'patient' )
-> select ( 'InternalPID, PatientID' )
-> whereIn ( 'InternalPID' , $ids )
-> get ()
-> getResultArray ();
2025-08-12 11:52:22 +07:00
$patient [ " LinkTo " ] = $patientLinkTo == [] ? null : $patientLinkTo ;
2025-08-11 14:37:47 +07:00
}
2025-08-25 09:02:10 +07:00
if ( $patient [ 'Custodian' ] != null ) {
2025-08-25 10:35:05 +07:00
$custodianId = ( int ) $patient [ 'Custodian' ];
2025-08-25 09:02:10 +07:00
$patientCustodian = $this -> db -> table ( 'patient' )
-> select ( 'InternalPID, PatientID' )
2025-08-25 10:35:05 +07:00
-> where ( 'InternalPID' , $custodianId )
2025-08-25 09:02:10 +07:00
-> get ()
-> getResultArray ();
$patient [ " Custodian " ] = $patientCustodian == [] ? null : $patientCustodian [ 0 ];
}
2025-08-11 12:50:57 +07:00
$patidt = $this -> db -> table ( 'patidt' )
2025-08-11 14:43:11 +07:00
-> select ( 'IdentifierType, Identifier' )
2025-08-11 12:50:57 +07:00
-> where ( 'InternalPID' , ( int ) $InternalPID )
-> get ()
2025-08-11 12:55:43 +07:00
-> getResultArray ();
2025-08-11 21:13:01 +07:00
$patient [ 'Identity' ] = $patidt == [] ? null : $patidt [ 0 ];
2025-09-02 13:52:02 +07:00
$patatt = $this -> db -> table ( 'patatt' )
-> select ( 'Address' )
-> where ( 'InternalPID' , ( int ) $InternalPID )
-> get ()
-> getResultArray ();
$patient [ 'Attachments' ] = $patatt == [] ? null : $patatt ;
2025-07-23 11:03:46 +07:00
}
// 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
}
public function create () {
2025-07-23 11:03:46 +07:00
try {
$input = $this -> request -> getJSON ( true );
2025-09-02 13:44:59 +07:00
$now = date ( 'Y-m-d H:i:s' );
2025-08-11 14:12:07 +07:00
$LinkTo = null ;
if ( ! empty ( $input [ 'LinkTo' ])) {
$ids = []; // Temporary
foreach ( $input [ 'LinkTo' ] as $row ) {
$ids [] = $row [ 'InternalPID' ];
}
$LinkTo = implode ( ',' , $ids );
}
2025-08-08 13:49:18 +07:00
// =========================
// 1. Data untuk tabel patient
// =========================
2025-08-12 11:31:29 +07:00
// $dataPatient = [
// "PatientID" => $input['PatientID'] ?? null,
// "AlternatePID" => $input['AlternatePID'] ?? null,
// "Prefix" => $input['Prefix'] ?? null,
// "NameFirst" => $input['NameFirst'] ?? null,
// "NameMiddle" => $input['NameMiddle'] ?? null,
// "NameMaiden" => $input['NameMaiden'] ?? null,
// "NameLast" => $input['NameLast'] ?? null,
// "Suffix" => $input['Suffix'] ?? null,
// "NameAlias" => $input['NameAlias'] ?? null,
// "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" => $input['Street_3'] ?? null,
// "City" => $input['City'] ?? null,
// "Province" => $input['Province'] ?? null,
// "ZIP" => $input['ZIP'] ?? null,
// "EmailAddress1" => $input['EmailAddress1'] ?? null,
// "EmailAddress2" => $input['EmailAddress2'] ?? null,
// "Phone" => $input['Phone'] ?? null,
// "MobilePhone" => $input['MobilePhone'] ?? null,
// "RaceID" => isset($input['RaceID']) ? (int) $input['RaceID'] : null,
// "IntCountryID" => isset($input['IntCountryID']) ? (int) $input['IntCountryID'] : null,
// "MaritalStatus" => $input['MaritalStatus'] ?? null,
// "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,
// "CreateDate" => date('Y-m-d H:i:s'),
// "DelDate" => null,
// "LinkTo" => $LinkTo
// ];
2025-08-08 13:49:18 +07:00
$dataPatient = [
2025-08-12 11:31:29 +07:00
" PatientID " => ( $input [ 'PatientID' ] ? ? '' ) !== '' ? $input [ 'PatientID' ] : null ,
" AlternatePID " => ( $input [ 'AlternatePID' ] ? ? '' ) !== '' ? $input [ 'AlternatePID' ] : null ,
" Prefix " => ( $input [ 'Prefix' ] ? ? '' ) !== '' ? $input [ 'Prefix' ] : null ,
" NameFirst " => ( $input [ 'NameFirst' ] ? ? '' ) !== '' ? $input [ 'NameFirst' ] : null ,
" NameMiddle " => ( $input [ 'NameMiddle' ] ? ? '' ) !== '' ? $input [ 'NameMiddle' ] : null ,
" NameMaiden " => ( $input [ 'NameMaiden' ] ? ? '' ) !== '' ? $input [ 'NameMaiden' ] : null ,
" NameLast " => ( $input [ 'NameLast' ] ? ? '' ) !== '' ? $input [ 'NameLast' ] : null ,
" Suffix " => ( $input [ 'Suffix' ] ? ? '' ) !== '' ? $input [ 'Suffix' ] : null ,
" NameAlias " => ( $input [ 'NameAlias' ] ? ? '' ) !== '' ? $input [ 'NameAlias' ] : null ,
" Gender " => ( $input [ 'Gender' ] ? ? '' ) !== '' ? ( int ) $input [ 'Gender' ] : null ,
" PlaceOfBirth " => ( $input [ 'PlaceOfBirth' ] ? ? '' ) !== '' ? $input [ 'PlaceOfBirth' ] : null ,
" Birthdate " => ( $input [ 'Birthdate' ] ? ? '' ) !== '' ? $input [ 'Birthdate' ] : null ,
" Street_1 " => ( $input [ 'Street_1' ] ? ? '' ) !== '' ? $input [ 'Street_1' ] : null ,
" Street_2 " => ( $input [ 'Street_2' ] ? ? '' ) !== '' ? $input [ 'Street_2' ] : null ,
" Street_3 " => ( $input [ 'Street_3' ] ? ? '' ) !== '' ? $input [ 'Street_3' ] : null ,
" City " => ( $input [ 'City' ] ? ? '' ) !== '' ? $input [ 'City' ] : null ,
" Province " => ( $input [ 'Province' ] ? ? '' ) !== '' ? $input [ 'Province' ] : null ,
" ZIP " => ( $input [ 'ZIP' ] ? ? '' ) !== '' ? $input [ 'ZIP' ] : null ,
" EmailAddress1 " => ( $input [ 'EmailAddress1' ] ? ? '' ) !== '' ? $input [ 'EmailAddress1' ] : null ,
" EmailAddress2 " => ( $input [ 'EmailAddress2' ] ? ? '' ) !== '' ? $input [ 'EmailAddress2' ] : null ,
" Phone " => ( $input [ 'Phone' ] ? ? '' ) !== '' ? $input [ 'Phone' ] : null ,
" MobilePhone " => ( $input [ 'MobilePhone' ] ? ? '' ) !== '' ? $input [ 'MobilePhone' ] : null ,
" RaceID " => ( $input [ 'RaceID' ] ? ? '' ) !== '' ? ( int ) $input [ 'RaceID' ] : null ,
" IntCountryID " => ( $input [ 'IntCountryID' ] ? ? '' ) !== '' ? ( int ) $input [ 'IntCountryID' ] : null ,
" MaritalStatus " => ( $input [ 'MaritalStatus' ] ? ? '' ) !== '' ? $input [ 'MaritalStatus' ] : null ,
" ReligionID " => ( $input [ 'ReligionID' ] ? ? '' ) !== '' ? ( int ) $input [ 'ReligionID' ] : null ,
" EthnicID " => ( $input [ 'EthnicID' ] ? ? '' ) !== '' ? ( int ) $input [ 'EthnicID' ] : null ,
" Citizenship " => ( $input [ 'Citizenship' ] ? ? '' ) !== '' ? $input [ 'Citizenship' ] : null ,
" DeathIndicator " => ( $input [ 'DeathIndicator' ] ? ? '' ) !== '' ? ( int ) $input [ 'DeathIndicator' ] : null ,
" DeathDateTime " => ( $input [ 'DeathDateTime' ] ? ? '' ) !== '' ? $input [ 'DeathDateTime' ] : null ,
2025-08-25 10:31:50 +07:00
" Custodian " => ( $input [ 'Custodian' ] ? ? '' ) !== '' ? ( int ) $input [ 'Custodian' ] : null ,
2025-09-02 13:44:59 +07:00
" CreateDate " => $now ,
2025-08-12 11:31:29 +07:00
" DelDate " => null ,
" LinkTo " => $LinkTo
2025-08-08 13:49:18 +07:00
];
2025-08-04 10:26:26 +07:00
2025-08-08 13:49:18 +07:00
$rulesDataPatient = [
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]' ,
'AlternatePID' => 'permit_empty|max_length[50]' ,
'NameFirst' => 'required|min_length[1]|max_length[255]' ,
'EmailAddress1' => 'required|is_unique[patient.EmailAddress1]' ,
'DeathIndicator' => 'required' ,
'Gender' => 'required'
2025-07-23 11:03:46 +07:00
];
2025-08-08 13:49:18 +07:00
$dataPatidt = [
2025-08-12 11:31:29 +07:00
" IdentifierType " => $input [ 'Identity' ][ 'IdentifierType' ] === '' ? null : ( $input [ 'Identity' ][ 'IdentifierType' ] ? ? null ),
" Identifier " => $input [ 'Identity' ][ 'Identifier' ] === '' ? null : ( $input [ 'Identity' ][ 'Identifier' ] ? ? null ),
2025-09-02 13:44:59 +07:00
" CreateDate " => $now ,
2025-07-23 11:03:46 +07:00
];
2025-08-08 13:49:18 +07:00
$rulesDataPatidt = [
'Identifier' => 'required|is_unique[patidt.Identifier]' ,
];
2025-09-02 13:44:59 +07:00
foreach ( $input [ 'Attachments' ] as $attachment ) {
$dataPatatt [] = [
" Address " => $attachment [ 'Address' ],
" CreateDate " => $now
];
}
$rulesDataPatatt = [
'Address' => 'required|is_unique[patatt.Address]' ,
];
2025-09-04 11:05:13 +07:00
$dataPatcom = [ " Comment " => $input [ 'Comment' ] === '' ? null : ( $input [ 'Comment' ] ? ? null ) ];
2025-08-08 13:49:18 +07:00
// =========================
// Validasi semua sebelum insert
// =========================
if ( ! $this -> validateData ( $dataPatient , $rulesDataPatient )) {
2025-07-23 11:03:46 +07:00
return $this -> respond ([
2025-08-08 13:49:18 +07:00
'status' => 'error' ,
'message' => 'Validation failed (patient)' ,
'errors' => $this -> validator -> getErrors ()
2025-07-23 11:03:46 +07:00
], 400 );
}
2025-08-08 13:49:18 +07:00
if ( ! $this -> validateData ( $dataPatidt , $rulesDataPatidt )) {
return $this -> respond ([
'status' => 'error' ,
'message' => 'Validation failed (patidt)' ,
'errors' => $this -> validator -> getErrors ()
], 400 );
}
// =========================
// Mulai transaksi
// =========================
$this -> db -> transStart ();
$this -> db -> table ( 'patient' ) -> insert ( $dataPatient );
$newInternalPatientId = $this -> db -> insertID ();
2025-09-02 13:44:59 +07:00
$dataPatidt [ 'InternalPID' ] = $newInternalPatientId ;
$this -> db -> table ( 'patidt' ) -> insert ( $dataPatidt );
if ( isset ( $dataPatatt )) {
for ( $i = 0 ; $i < count ( $dataPatatt ); $i ++ ) {
$dataPatatt [ $i ][ 'InternalPID' ] = $newInternalPatientId ;
}
$this -> db -> table ( 'patatt' ) -> insertBatch ( $dataPatatt );
}
2025-09-04 11:05:13 +07:00
if ( $dataPatcom [ 'Comment' ] != '' ) {
$dataPatcom [ 'InternalPID' ] = $newInternalPatientId ;
$dataPatcom [ 'CreateDate' ] = $now ;
$this -> db -> table ( 'patcom' ) -> insert ( $dataPatcom );
}
2025-08-08 14:02:56 +07:00
$dbError = $this -> db -> error ();
if ( ! empty ( $dbError [ 'message' ])) {
$this -> db -> transRollback ();
return $this -> failServerError ( 'Insert patient failed: ' . $dbError [ 'message' ]);
}
2025-08-08 13:49:18 +07:00
$this -> db -> transComplete ();
if ( $this -> db -> transStatus () === false ) {
2025-08-08 15:41:25 +07:00
$dbError = $this -> db -> error ();
2025-08-08 13:59:24 +07:00
return $this -> failServerError (
2025-08-08 14:02:56 +07:00
'Failed to create patient data (transaction rolled back): ' . ( $dbError [ 'message' ] ? ? 'Unknown database error' )
2025-08-08 13:59:24 +07:00
);
2025-08-08 13:49:18 +07:00
}
2025-07-23 11:03:46 +07:00
return $this -> respondCreated ([
2025-08-08 13:49:18 +07:00
'status' => 'success' ,
2025-07-23 11:03:46 +07:00
'message' => 'Patient created successfully' ,
2025-08-08 13:49:18 +07:00
'data' => $newInternalPatientId
2025-07-23 11:03:46 +07:00
], 201 );
} catch ( \Exception $e ) {
2025-08-08 13:49:18 +07:00
$this -> db -> transRollback ();
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-23 11:03:46 +07:00
try {
2025-08-12 09:19:10 +07:00
if ( ! $InternalPID || ! is_numeric ( $InternalPID )) {
return $this -> respond ([ 'status' => 'error' , 'message' => 'Invalid or missing InternalPID' ], 400 );
}
2025-08-01 22:33:41 +07:00
2025-07-23 11:03:46 +07:00
$input = $this -> request -> getJSON ( true );
2025-08-12 09:19:10 +07:00
if ( ! $input ) {
return $this -> respond ([ 'status' => 'error' , 'message' => 'Invalid JSON input' ], 400 );
}
// Cek apakah data patient ada
$patient = $this -> db -> table ( 'patient' ) -> where ( 'InternalPID' , $InternalPID ) -> get () -> getRowArray ();
if ( ! $patient ) {
return $this -> respond ([ 'status' => 'error' , 'message' => 'Patient not found' ], 404 );
}
2025-07-02 16:10:37 +07:00
2025-08-12 09:19:10 +07:00
$LinkTo = null ;
if ( ! empty ( $input [ 'LinkTo' ]) && is_array ( $input [ 'LinkTo' ])) {
$ids = [];
foreach ( $input [ 'LinkTo' ] as $row ) {
if ( isset ( $row [ 'InternalPID' ]) && is_numeric ( $row [ 'InternalPID' ])) {
$ids [] = ( int ) $row [ 'InternalPID' ];
}
}
$LinkTo = implode ( ',' , $ids );
}
// Data untuk update patient
2025-08-12 11:31:29 +07:00
// $dataPatient = [
// "PatientID" => $input['PatientID'] ?? $patient['PatientID'],
// "AlternatePID" => $input['AlternatePID'] ?? $patient['AlternatePID'],
// "Prefix" => $input['Prefix'] ?? $patient['Prefix'],
// "NameFirst" => $input['NameFirst'] ?? $patient['NameFirst'],
// "NameMiddle" => $input['NameMiddle'] ?? $patient['NameMiddle'],
// "NameMaiden" => $input['NameMaiden'] ?? $patient['NameMaiden'],
// "NameLast" => $input['NameLast'] ?? $patient['NameLast'],
// "Suffix" => $input['Suffix'] ?? $patient['Suffix'],
// "NameAlias" => $input['NameAlias'] ?? $patient['NameAlias'],
// "Gender" => isset($input['Gender']) && is_numeric($input['Gender']) ? (int)$input['Gender'] : $patient['Gender'],
// "PlaceOfBirth" => $input['PlaceOfBirth'] ?? $patient['PlaceOfBirth'],
// "Birthdate" => $input['Birthdate'] ?: $patient['Birthdate'],
// "Street_1" => $input['Street_1'] ?? $patient['Street_1'],
// "Street_2" => $input['Street_2'] ?? $patient['Street_2'],
// "Street_3" => $input['Street_3'] ?? $patient['Street_3'],
// "City" => $input['City'] ?? $patient['City'],
// "Province" => $input['Province'] ?? $patient['Province'],
// "ZIP" => $input['ZIP'] ?? $patient['ZIP'],
// "EmailAddress1" => $input['EmailAddress1'] ?? $patient['EmailAddress1'],
// "EmailAddress2" => $input['EmailAddress2'] ?? $patient['EmailAddress2'],
// "Phone" => $input['Phone'] ?? $patient['Phone'],
// "MobilePhone" => $input['MobilePhone'] ?? $patient['MobilePhone'],
// "RaceID" => isset($input['RaceID']) && is_numeric($input['RaceID']) ? (int)$input['RaceID'] : $patient['RaceID'],
// "IntCountryID" => isset($input['IntCountryID']) && is_numeric($input['IntCountryID']) ? (int)$input['IntCountryID'] : $patient['IntCountryID'],
// "MaritalStatus" => $input['MaritalStatus'] ?? $patient['MaritalStatus'],
// "ReligionID" => isset($input['ReligionID']) && is_numeric($input['ReligionID']) ? (int)$input['ReligionID'] : $patient['ReligionID'],
// "EthnicID" => isset($input['EthnicID']) && is_numeric($input['EthnicID']) ? (int)$input['EthnicID'] : $patient['EthnicID'],
// "Citizenship" => $input['Citizenship'] ?? $patient['Citizenship'],
// "DeathIndicator" => isset($input['DeathIndicator']) && is_numeric($input['DeathIndicator']) ? (int)$input['DeathIndicator'] : $patient['DeathIndicator'],
// 'DeathDateTime' => $input['DeathDateTime'] ?: $patient['DeathDateTime'],
// "LinkTo" => $LinkTo,
// ];
2025-08-11 14:12:07 +07:00
$dataPatient = [
2025-08-12 11:31:29 +07:00
" PatientID " => ( $input [ 'PatientID' ] ? ? null ) === '' ? null : ( $input [ 'PatientID' ] ? ? null ),
" AlternatePID " => ( $input [ 'AlternatePID' ] ? ? null ) === '' ? null : ( $input [ 'AlternatePID' ] ? ? null ),
" Prefix " => ( $input [ 'Prefix' ] ? ? null ) === '' ? null : ( $input [ 'Prefix' ] ? ? null ),
" NameFirst " => ( $input [ 'NameFirst' ] ? ? null ) === '' ? null : ( $input [ 'NameFirst' ] ? ? null ),
" NameMiddle " => ( $input [ 'NameMiddle' ] ? ? null ) === '' ? null : ( $input [ 'NameMiddle' ] ? ? null ),
" NameMaiden " => ( $input [ 'NameMaiden' ] ? ? null ) === '' ? null : ( $input [ 'NameMaiden' ] ? ? null ),
" NameLast " => ( $input [ 'NameLast' ] ? ? null ) === '' ? null : ( $input [ 'NameLast' ] ? ? null ),
" Suffix " => ( $input [ 'Suffix' ] ? ? null ) === '' ? null : ( $input [ 'Suffix' ] ? ? null ),
" NameAlias " => ( $input [ 'NameAlias' ] ? ? null ) === '' ? null : ( $input [ 'NameAlias' ] ? ? null ),
" Gender " => ( $input [ 'Gender' ] ? ? null ) === '' ? null : ( isset ( $input [ 'Gender' ]) ? ( int ) $input [ 'Gender' ] : null ),
" PlaceOfBirth " => ( $input [ 'PlaceOfBirth' ] ? ? null ) === '' ? null : ( $input [ 'PlaceOfBirth' ] ? ? null ),
" Birthdate " => ( $input [ 'Birthdate' ] ? ? null ) === '' ? null : ( $input [ 'Birthdate' ] ? ? null ),
" Street_1 " => ( $input [ 'Street_1' ] ? ? null ) === '' ? null : ( $input [ 'Street_1' ] ? ? null ),
" Street_2 " => ( $input [ 'Street_2' ] ? ? null ) === '' ? null : ( $input [ 'Street_2' ] ? ? null ),
" Street_3 " => ( $input [ 'Street_3' ] ? ? null ) === '' ? null : ( $input [ 'Street_3' ] ? ? null ),
" City " => ( $input [ 'City' ] ? ? null ) === '' ? null : ( $input [ 'City' ] ? ? null ),
" Province " => ( $input [ 'Province' ] ? ? null ) === '' ? null : ( $input [ 'Province' ] ? ? null ),
" ZIP " => ( $input [ 'ZIP' ] ? ? null ) === '' ? null : ( $input [ 'ZIP' ] ? ? null ),
" EmailAddress1 " => ( $input [ 'EmailAddress1' ] ? ? null ) === '' ? null : ( $input [ 'EmailAddress1' ] ? ? null ),
" EmailAddress2 " => ( $input [ 'EmailAddress2' ] ? ? null ) === '' ? null : ( $input [ 'EmailAddress2' ] ? ? null ),
" Phone " => ( $input [ 'Phone' ] ? ? null ) === '' ? null : ( $input [ 'Phone' ] ? ? null ),
" MobilePhone " => ( $input [ 'MobilePhone' ] ? ? null ) === '' ? null : ( $input [ 'MobilePhone' ] ? ? null ),
" RaceID " => ( $input [ 'RaceID' ] ? ? null ) === '' ? null : ( isset ( $input [ 'RaceID' ]) ? ( int ) $input [ 'RaceID' ] : null ),
" IntCountryID " => ( $input [ 'IntCountryID' ] ? ? null ) === '' ? null : ( isset ( $input [ 'IntCountryID' ]) ? ( int ) $input [ 'IntCountryID' ] : null ),
" MaritalStatus " => ( $input [ 'MaritalStatus' ] ? ? null ) === '' ? null : ( $input [ 'MaritalStatus' ] ? ? null ),
" ReligionID " => ( $input [ 'ReligionID' ] ? ? null ) === '' ? null : ( isset ( $input [ 'ReligionID' ]) ? ( int ) $input [ 'ReligionID' ] : null ),
" EthnicID " => ( $input [ 'EthnicID' ] ? ? null ) === '' ? null : ( isset ( $input [ 'EthnicID' ]) ? ( int ) $input [ 'EthnicID' ] : null ),
" Citizenship " => ( $input [ 'Citizenship' ] ? ? null ) === '' ? null : ( $input [ 'Citizenship' ] ? ? null ),
" DeathIndicator " => ( $input [ 'DeathIndicator' ] ? ? null ) === '' ? null : ( isset ( $input [ 'DeathIndicator' ]) ? ( int ) $input [ 'DeathIndicator' ] : null ),
" DeathDateTime " => ( $input [ 'DeathDateTime' ] ? ? null ) === '' ? null : ( $input [ 'DeathDateTime' ] ? ? null ),
2025-08-25 10:31:50 +07:00
// "Custodian" => ($input['Custodian'] ?? null) === '' ? null : ($input['Custodian'] ?? null),
" Custodian " => ( $input [ 'Custodian' ] ? ? null ) === '' ? null : ( isset ( $input [ 'Custodian' ]) ? ( int ) $input [ 'Custodian' ] : null ),
2025-08-12 09:19:10 +07:00
" LinkTo " => $LinkTo ,
2025-08-11 14:12:07 +07:00
];
2025-08-04 10:26:26 +07:00
2025-08-12 09:19:10 +07:00
// Atur aturan validasi dengan pengecualian is_unique untuk InternalPID ini
2025-08-11 14:12:07 +07:00
$rulesDataPatient = [
2025-08-12 09:19:10 +07:00
'PatientID' => " required|max_length[50]|is_unique[patient.PatientID,InternalPID, { $InternalPID } ] " ,
2025-08-11 14:12:07 +07:00
'AlternatePID' => 'permit_empty|max_length[50]' ,
'NameFirst' => 'required|min_length[1]|max_length[255]' ,
2025-08-12 09:19:10 +07:00
'EmailAddress1' => " required|is_unique[patient.EmailAddress1,InternalPID, { $InternalPID } ] " ,
2025-08-11 14:12:07 +07:00
'DeathIndicator' => 'required' ,
'Gender' => 'required'
];
2025-08-12 09:19:10 +07:00
// Ambil data patidt
$patidt = $this -> db -> table ( 'patidt' ) -> where ( 'InternalPID' , $InternalPID ) -> get () -> getRowArray ();
2025-08-11 14:12:07 +07:00
$dataPatidt = [
2025-08-12 11:31:29 +07:00
" IdentifierType " => $input [ 'Identity' ][ 'IdentifierType' ] === '' ? null : ( $input [ 'Identity' ][ 'IdentifierType' ] ? ? null ),
" Identifier " => $input [ 'Identity' ][ 'Identifier' ] === '' ? null : ( $input [ 'Identity' ][ 'Identifier' ] ? ? null ),
2025-07-23 11:03:46 +07:00
];
2025-08-11 14:12:07 +07:00
$rulesDataPatidt = [
2025-08-12 09:19:10 +07:00
'Identifier' => " required|is_unique[patidt.Identifier,InternalPID, { $InternalPID } ] "
2025-08-01 13:37:13 +07:00
];
2025-08-12 09:19:10 +07:00
2025-09-04 11:05:13 +07:00
$dataPatcom = [ " Comment " => $input [ 'Comment' ] === '' ? null : ( $input [ 'Comment' ] ? ? null ) ];
if ( $dataPatcom [ 'Comment' ] != '' ) {
$dataPatcom [ 'InternalPID' ] = $InternalPID ;
$dataPatcom [ 'CreateDate' ] = $this -> now ;
$this -> db -> table ( 'patcom' ) -> upsert ( $dataPatcom );
}
2025-08-12 09:19:10 +07:00
// Validasi
if ( ! $this -> validateData ( $dataPatient , $rulesDataPatient )) {
return $this -> respond ([
'status' => 'error' ,
'message' => 'Validation failed (patient)' ,
'errors' => $this -> validator -> getErrors ()
], 400 );
2025-07-02 16:10:37 +07:00
}
2025-08-12 09:19:10 +07:00
if ( ! $this -> validateData ( $dataPatidt , $rulesDataPatidt )) {
return $this -> respond ([
'status' => 'error' ,
'message' => 'Validation failed (patidt)' ,
'errors' => $this -> validator -> getErrors ()
], 400 );
2025-07-23 11:03:46 +07:00
}
2025-08-12 09:19:10 +07:00
// Transaksi update
$this -> db -> transStart ();
2025-07-23 11:03:46 +07:00
2025-08-12 09:19:10 +07:00
$this -> db -> table ( 'patient' ) -> where ( 'InternalPID' , $InternalPID ) -> update ( $dataPatient );
$dbError = $this -> db -> error ();
if ( ! empty ( $dbError [ 'message' ])) {
$this -> db -> transRollback ();
return $this -> failServerError ( 'Update patient failed: ' . $dbError [ 'message' ]);
2025-07-23 11:03:46 +07:00
}
2025-08-12 09:19:10 +07:00
$this -> db -> table ( 'patidt' ) -> where ( 'InternalPID' , $InternalPID ) -> update ( $dataPatidt );
$dbError = $this -> db -> error ();
if ( ! empty ( $dbError [ 'message' ])) {
$this -> db -> transRollback ();
return $this -> failServerError ( 'Update patidt failed: ' . $dbError [ 'message' ]);
2025-07-23 11:03:46 +07:00
}
2025-08-12 09:19:10 +07:00
$this -> db -> transComplete ();
2025-07-02 16:10:37 +07:00
2025-08-12 09:19:10 +07:00
if ( $this -> db -> transStatus () === false ) {
$dbError = $this -> db -> error ();
return $this -> failServerError ( 'Failed to update patient data (transaction rolled back): ' . ( $dbError [ 'message' ] ? ? 'Unknown error' ));
}
return $this -> respond ([
'status' => 'success' ,
2025-07-23 11:03:46 +07:00
'message' => 'Patient updated successfully' ,
2025-08-12 09:19:10 +07:00
'data' => $InternalPID
], 200 );
2025-07-02 16:10:37 +07:00
2025-07-23 11:03:46 +07:00
} catch ( \Exception $e ) {
2025-08-12 09:19:10 +07:00
$this -> db -> transRollback ();
return $this -> failServerError ( 'Something went wrong: ' . $e -> getMessage ());
}
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
}
2025-08-05 10:03:33 +07:00
// Update kolom DelDate sebagai soft delete
$this -> db -> table ( 'patient' ) -> where ( 'InternalPID' , $InternalPID ) -> update ([ 'DelDate' => date ( 'Y-m-d H:i:s' )]);
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-08-14 15:28:16 +07:00
// OK - Done
public function patientCheck () {
2025-08-14 09:17:15 +07:00
try {
2025-08-14 15:28:16 +07:00
$PatientID = $this -> request -> getVar ( 'PatientID' );
$EmailAddress1 = $this -> request -> getVar ( 'EmailAddress1' );
if ( $PatientID != null ){
$tableName = 'PatientID' ;
$searchName = $PatientID ;
}
if ( $EmailAddress1 != null ){
$tableName = 'EmailAddress1' ;
$searchName = $EmailAddress1 ;
2025-08-14 09:17:15 +07:00
}
$patient = $this -> db -> table ( 'patient' )
2025-08-14 15:28:16 +07:00
-> where ( $tableName , $searchName )
2025-08-14 09:17:15 +07:00
-> get ()
-> getRowArray ();
if ( ! $patient ) {
return $this -> respond ([
'status' => 'success' ,
2025-08-14 15:28:16 +07:00
'message' => " $tableName not found. " ,
2025-08-14 09:17:15 +07:00
'data' => true ,
], 200 );
}
return $this -> respond ([
'status' => 'success' ,
2025-08-14 15:28:16 +07:00
'message' => " $tableName already exists. " ,
2025-08-14 09:17:15 +07:00
'data' => false ,
], 200 );
2025-08-14 15:28:16 +07:00
} catch ( \Exception $e ) {
// Error Server Mengembalikan 500
return $this -> failServerError ( 'Something went wrong.' . $e -> getMessage ());
2025-08-14 09:17:15 +07:00
}
}
2025-08-12 11:31:29 +07:00
// Ubah ke format Years Months Days
2025-08-11 16:14:26 +07:00
private function calculateAgeFromBirthdate ( $birthdate ) {
2025-08-11 16:15:40 +07:00
$dob = new \DateTime ( $birthdate );
$today = new \DateTime ();
2025-08-11 16:14:26 +07:00
$diff = $today -> diff ( $dob );
2025-08-11 20:58:31 +07:00
$formattedAge = " " ;
if ( $diff -> y > 0 ){
2025-08-12 11:03:03 +07:00
$formattedAge .= " { $diff -> y } Years " ;
2025-08-11 20:58:31 +07:00
}
if ( $diff -> m > 0 ){
2025-08-12 11:03:03 +07:00
$formattedAge .= " { $diff -> m } Months " ;
2025-08-11 20:58:31 +07:00
}
if ( $diff -> d > 0 ){
2025-08-12 11:03:03 +07:00
$formattedAge .= " { $diff -> d } Days " ;
2025-08-11 20:58:31 +07:00
}
2025-08-11 16:14:26 +07:00
return $formattedAge ;
}
2025-08-12 11:31:29 +07:00
// Ubah ke format Y-m-d H:i
private function formatedDate ( $dateString ) {
2025-08-12 11:03:03 +07:00
$date = \DateTime :: createFromFormat ( 'Y-m-d H:i' , $dateString );
if ( ! $date ) {
$timestamp = strtotime ( $dateString );
if ( $timestamp ) {
return date ( 'Y-m-d H:i' , $timestamp );
}
return null ;
}
return $date -> format ( 'Y-m-d H:i' );
}
2025-08-12 11:31:29 +07:00
// Ubah ke format j M Y
private function formatedDateForDisplay ( $dateString ) {
2025-08-12 11:03:03 +07:00
$date = \DateTime :: createFromFormat ( 'Y-m-d H:i' , $dateString );
if ( ! $date ) {
$timestamp = strtotime ( $dateString );
if ( $timestamp ) {
return date ( 'j M Y' , $timestamp );
}
return null ;
}
return $date -> format ( 'j M Y' );
}
2025-07-02 16:10:37 +07:00
}