Update Patient create, penambahan LinkTo

This commit is contained in:
mikael-zakaria 2025-08-11 14:12:07 +07:00
parent d5862a37a5
commit 04eb38177e

View File

@ -110,6 +110,15 @@ class Patient extends Controller {
try {
$input = $this->request->getJSON(true);
$LinkTo = null;
if (!empty($input['LinkTo'])) {
$ids = []; // Temporary
foreach ($input['LinkTo'] as $row) {
$ids[] = $row['InternalPID'];
}
$LinkTo = implode(',', $ids);
}
// =========================
// 1. Data untuk tabel patient
// =========================
@ -125,7 +134,7 @@ class Patient extends Controller {
"NameAlias" => $input['NameAlias'] ?? null,
"Gender" => isset($input['Gender']) ? (int) $input['Gender'] : null,
"PlaceOfBirth" => $input['PlaceOfBirth'] ?? null,
"Birthdate" => $input['Birthdate'] ?? null,
"Birthdate" => $input['Birthdate'] ?: null,
"Street_1" => $input['Street_1'] ?? null,
"Street_2" => $input['Street_2'] ?? null,
"Street_3" => $input['Street_3'] ?? null,
@ -146,7 +155,7 @@ class Patient extends Controller {
'DeathDateTime' => $input['DeathDateTime'] ?: null,
"CreateDate" => date('Y-m-d H:i:s'),
"DelDate" => null,
// Linkto
"LinkTo" => $LinkTo
// Mother
// AccountNumber
];
@ -237,8 +246,7 @@ class Patient extends Controller {
$input = $this->request->getJSON(true);
$data = [
"InternalPID" => $input['InternalPID'] ?? null,
$dataPatient = [
"PatientID" => $input['PatientID'] ?? null,
"AlternatePID" => $input['AlternatePID'] ?? null,
"Prefix" => $input['Prefix'] ?? null,
@ -247,91 +255,52 @@ class Patient extends Controller {
"NameMaiden" => $input['NameMaiden'] ?? null,
"NameLast" => $input['NameLast'] ?? null,
"Suffix" => $input['Suffix'] ?? null,
"NameAlias" => null,
"NameAlias" => $input['NameAlias'] ?? null,
"Gender" => isset($input['Gender']) ? (int) $input['Gender'] : null,
"PlaceOfBirth" => $input['PlaceOfBirth'] ?? null,
"Birthdate" => $input['Birthdate'] ?? null,
"Birthdate" => $input['Birthdate'] ?: null,
"Street_1" => $input['Street_1'] ?? null,
"Street_2" => $input['Street_2'] ?? null,
"Street_3" => null,
"Street_3" => $input['Street_3'] ?? null,
"City" => $input['City'] ?? null,
"Province" => $input['Province'] ?? null,
"ZIP" => $input['ZIP'] ?? null,
"CountryID" => isset($input['CountryID']) ? (int) $input['CountryID'] : null,
"EmailAddress1" => $input['EmailAddress1'] ?? null,
"EmailAddress2" => $input['EmailAddress2'] ?? null,
"Phone" => $input['Phone'] ?? null,
"MobilePhone" => $input['MobilePhone'] ?? null,
"Mother" => $input['Mother'] ?? null,
"AccountNumber" => isset($input['AccountNumber']) ? (int) $input['AccountNumber'] : 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,
// "LinkTo" => $input['LinkTo'] ?? null,
'DeathDateTime' => $input['DeathDateTime'] ?: null,
"CreateDate" => date('Y-m-d H:i:s'),
"DelDate" => null,
// Field tambahan dari struktur sebelumnya (bisa dihapus jika tidak dipakai)
// "PatientComment" => $input['PatientComment'] ?? null,
// "IdentityIDType" => $input['IdentityIDType'] ?? null,
// "IdentityID" => $input['IdentityID'] ?? null
// Linkto
// Mother
// AccountNumber
];
// $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
// ];
$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'
];
$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',
$dataPatidt = [
"IdentifierType" => $input['IdentifierType'] ?? null,
"Identifier" => $input['Identifier'] ?? null,
"CreateDate" => date('Y-m-d H:i:s'),
];
$rulesDataPatidt = [
'Identifier' => 'required|is_unique[patidt.Identifier]',
];
$existingPatient = $this->db->table('patient')->where('InternalPID', $InternalPID)->get()->getRowArray();
@ -342,7 +311,7 @@ class Patient extends Controller {
}
// Request dari client tidak valid atau tidak bisa diproses oleh server - 400
if (!$this->validateData($data, $rules)) {
if (!$this->validateData($dataPatient, $rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
@ -355,22 +324,22 @@ class Patient extends Controller {
$datas = [];
foreach ($allowedUpdateFields as $field) {
if (isset($data[$field])) {
$datas[$field] = $data[$field];
if (isset($dataPatient[$field])) {
$datas[$field] = $dataPatient[$field];
}
}
if (empty($data)) {
if (empty($dataPatient)) {
return $this->failValidationError('No data provided for update.');
}
$this->db->table('patient')->where('InternalPID', $InternalPID)->update($data);
$this->db->table('patient')->where('InternalPID', $InternalPID)->update($dataPatient);
// Sukses & Insert = 201 - Kirim data patient ID
return $this->respondCreated([
'status' => 'success',
'message' => 'Patient updated successfully',
'data' => $data
'data' => $dataPatient
], 201);
} catch (\Exception $e) {