fix edit patatt

This commit is contained in:
mahdahar 2025-09-08 15:56:38 +07:00
parent 1305b2bc3e
commit 0fb7caa32f

View File

@ -57,181 +57,128 @@ class Patient extends Controller {
}
}
// OK - Done
public function show($InternalPID = null) {
try {
$rows = $this->db->table('patient')
->select("
patient.*,
country.Country as CountryName,
race.Race as RaceName,
religion.Religion as ReligionName,
ethnic.Ethnic as EthnicName,
patcom.Comment as Comment,
patidt.IdentifierType,
patidt.Identifier,
patatt.Address
")
->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')
->join('patcom', 'patcom.InternalPID = patient.InternalPID', 'left')
->join('patidt', 'patidt.InternalPID = patient.InternalPID', 'left')
->join('patatt', 'patatt.InternalPID = patient.InternalPID', 'left')
->where('patient.InternalPID', (int) $InternalPID)
->get()
->getResultArray();
$patient = $this->db->table('patient')
->select('patient.*, country.Country as CountryName, race.Race as RaceName, religion.Religion as ReligionName, ethnic.Ethnic as EthnicName, patcom.Comment as Comment')
->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')
->join('patcom', 'patcom.InternalPID = patient.InternalPID', 'left')
->where('patient.InternalPID', (int) $InternalPID)
->get()
->getRowArray();
// Data pasien tidak ada mengembalikan - success 200
if (empty($patient)) {
if (empty($rows)) {
return $this->respond([
'status' => 'success',
'message' => 'Patient with ID ' . $InternalPID . ' not found.',
'data' => [],
'status' => 'success',
'message' => "Patient with ID {$InternalPID} not found.",
'data' => [],
], 200);
} else {
$patient["Age"] = $this->calculateAgeFromBirthdate($patient["Birthdate"]);
$patient["Birthdate"] = $this->formatedDate($patient["Birthdate"]);
$patient["CreateDate"] = $this->formatedDate($patient["CreateDate"]);
$patient["DelDate"] = $this->formatedDate($patient["DelDate"]);
$patient["DeathDateTime"] = $this->formatedDate($patient["DeathDateTime"]);
$patient["BirthdateConversion"] = $this->formatedDateForDisplay($patient["Birthdate"]);
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();
$patient["LinkTo"] = $patientLinkTo == [] ? null : $patientLinkTo;
}
if ($patient['Custodian'] != null) {
$custodianId = (int) $patient['Custodian'];
$patientCustodian = $this->db->table('patient')
->select('InternalPID, PatientID')
->where('InternalPID', $custodianId)
->get()
->getResultArray();
$patient["Custodian"] = $patientCustodian == [] ? null : $patientCustodian[0];
}
$patidt = $this->db->table('patidt')
->select('IdentifierType, Identifier')
->where('InternalPID', (int) $InternalPID)
->get()
->getResultArray();
$patient['Identity'] = $patidt == [] ? null : $patidt[0];
$patatt = $this->db->table('patatt')
->select('Address')
->where('InternalPID', (int) $InternalPID)
->get()
->getResultArray();
$patient['Attachments'] = $patatt == [] ? null : $patatt;
}
// Data pasien ditemukan dan mengembalikan - success 200
// Use first row as base patient data
$patient = $rows[0];
$patient = $this->transformPatientData($patient);
$patient['Identity'] = null;
$patient['Attachments'] = [];
foreach ($rows as $row) {
if ($row['IdentifierType'] && $row['Identifier'] && !$patient['Identity']) {
$patient['Identity'] = [
'IdentifierType' => $row['IdentifierType'],
'Identifier' => $row['Identifier'],
];
}
if ($row['Address']) {
$patient['Attachments'][] = ['Address' => $row['Address']];
}
}
if (empty($patient['Identity'])) { $patient['Identity'] = null; }
if (empty($patient['Attachments'])) { $patient['Attachments'] = null; }
return $this->respond([
'status' => 'success',
'message'=> "Patient Show Successfully",
'data' => $patient,
], 200);
'status' => 'success',
'message' => "Patient Show Successfully",
'data' => $patient,
], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
// Error Server Mengembalikan 500
return $this->failServerError('Something went wrong'.$e->getMessage());
private function transformPatientData(array $patient): array {
$patient["Age"] = $this->calculateAgeFromBirthdate($patient["Birthdate"]);
$patient["Birthdate"] = $this->formatedDate($patient["Birthdate"]);
$patient["CreateDate"] = $this->formatedDate($patient["CreateDate"]);
$patient["DelDate"] = $this->formatedDate($patient["DelDate"]);
$patient["DeathDateTime"] = $this->formatedDate($patient["DeathDateTime"]);
$patient["BirthdateConversion"] = $this->formatedDateForDisplay($patient["Birthdate"]);
$patient["LinkTo"] = $this->getLinkedPatients($patient['LinkTo']);
$patient["Custodian"] = $this->getCustodian($patient['Custodian']);
return $patient;
}
private function getLinkedPatients(?string $linkTo): ?array {
if (empty($linkTo)) {
return null;
}
$ids = array_filter(explode(',', $linkTo));
return $this->db->table('patient')
->select('InternalPID, PatientID')
->whereIn('InternalPID', $ids)
->get()
->getResultArray() ?: null;
}
private function getCustodian($custodianId): ?array {
if (empty($custodianId)) {
return null;
}
return $this->db->table('patient')
->select('InternalPID, PatientID')
->where('InternalPID', (int) $custodianId)
->get()
->getRowArray() ?: null;
}
public function create() {
try {
$input = $this->request->getJSON(true);
$now = date('Y-m-d H:i:s');
$LinkTo = null;
if (!empty($input['LinkTo'])) {
$ids = []; // Temporary
foreach ($input['LinkTo'] as $row) {
$ids[] = $row['InternalPID'];
}
$LinkTo = implode(',', $ids);
}
$now = date('Y-m-d H:i:s');
// =========================
// 1. Data untuk tabel patient
// =========================
// $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
// ];
$dataPatient = [
"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,
"Custodian" => ($input['Custodian'] ?? '') !== '' ? (int)$input['Custodian'] : null,
"CreateDate" => $now,
"DelDate" => null,
"LinkTo" => $LinkTo
];
// Prepare data
$dataPatient = $this->preparePatientData($input, $now, 'create');
$dataPatidt = $this->preparePatidtData($input, $now, 'create');
$dataPatatt = $this->preparePatattData($input, $now, 'create');
$dataPatcom = $this->preparePatcomData($input, $now, 'create');
$rulesDataPatient = [
// Validation rules
$rulesPatient = [
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]',
'AlternatePID' => 'permit_empty|max_length[50]',
'NameFirst' => 'required|min_length[1]|max_length[255]',
@ -239,70 +186,45 @@ class Patient extends Controller {
'DeathIndicator' => 'required',
'Gender' => 'required'
];
$rulesPatidt = ['Identifier' => 'required|is_unique[patidt.Identifier]'];
//$rulesPatatt = ['Address' => 'required|is_unique[patatt.Address]'];
$dataPatidt = [
"IdentifierType" => $input['Identity']['IdentifierType'] === '' ? null : ($input['Identity']['IdentifierType'] ?? null),
"Identifier" => $input['Identity']['Identifier'] === '' ? null : ($input['Identity']['Identifier'] ?? null),
"CreateDate" => $now,
];
$rulesDataPatidt = [
'Identifier' => 'required|is_unique[patidt.Identifier]',
];
// Validate patient
if (!$this->validateData($dataPatient, $rulesPatient)) {
return $this->validationError('patient', $this->validator->getErrors());
}
// Validate patidt
if (!$this->validateData($dataPatidt, $rulesPatidt)) {
return $this->validationError('patidt', $this->validator->getErrors());
}
/* Validate patatt (if exists, validate only the first row)
if (!empty($dataPatatt) && !$this->validateData($dataPatatt[0], $rulesPatatt)) {
return $this->validationError('patatt', $this->validator->getErrors());
}
*/
foreach($input['Attachments'] as $attachment) {
$dataPatatt[] = [
"Address" => $attachment['Address'],
"CreateDate" => $now
];
}
$rulesDataPatatt = [
'Address' => 'required|is_unique[patatt.Address]',
];
$dataPatcom = [ "Comment" => $input['Comment'] === '' ? null : ($input['Comment'] ?? null) ];
// =========================
// Validasi semua sebelum insert
// =========================
if (!$this->validateData($dataPatient, $rulesDataPatient)) {
return $this->respond([
'status' => 'error',
'message' => 'Validation failed (patient)',
'errors' => $this->validator->getErrors()
], 400);
}
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();
$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);
}
if($dataPatcom['Comment'] != '') {
if (!empty($dataPatatt)) {
foreach ($dataPatatt as &$row) {
$row['InternalPID'] = $newInternalPatientId;
}
$this->db->table('patatt')->upsertBatch($dataPatatt);
}
if (!empty($dataPatcom['Comment'])) {
$dataPatcom['InternalPID'] = $newInternalPatientId;
$dataPatcom['CreateDate'] = $now;
$this->db->table('patcom')->insert($dataPatcom);
}
@ -315,7 +237,7 @@ class Patient extends Controller {
$this->db->transComplete();
if ($this->db->transStatus() === false) {
$dbError = $this->db->error();
$dbError = $this->db->error();
return $this->failServerError(
'Failed to create patient data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
);
@ -333,104 +255,125 @@ class Patient extends Controller {
}
}
private function preparePatientData(array $input, string $now, string $mode = 'create'): array {
$LinkTo = null;
if (!empty($input['LinkTo'])) {
$ids = array_column($input['LinkTo'], 'InternalPID');
$LinkTo = implode(',', $ids);
}
$data = [
"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" => !empty($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,
"Custodian" => isset($input['Custodian']) ? (int)$input['Custodian'] : null,
"DelDate" => null,
"LinkTo" => $LinkTo
];
// Only set CreateDate when creating
if ($mode === 'create') {
$data["CreateDate"] = $now;
}
return $data;
}
private function preparePatidtData(array $input, string $now, string $mode = 'create'): array {
$data = [
"IdentifierType" => $input['Identity']['IdentifierType'] ?? null,
"Identifier" => $input['Identity']['Identifier'] ?? null,
];
if ($mode === 'create') {
$data["CreateDate"] = $now;
}
return $data;
}
private function preparePatattData(array $input, string $now, string $mode = 'create'): array {
if (empty($input['Attachments'])) {
return [];
}
return array_map(function ($attachment) use ($now, $mode) {
$row = [
"Address" => $attachment['Address'] ?? null,
];
if ($mode === 'create') {
$row["CreateDate"] = $now;
}
return $row;
}, $input['Attachments']);
}
private function preparePatcomData(array $input, string $now, string $mode = 'create'): array {
$data = [
"Comment" => $input['Comment'] ?? null,
];
if ($mode === 'create') {
$data["CreateDate"] = $now;
}
return $data;
}
private function validationError(string $context, array $errors)
{
return $this->respond([
'status' => 'error',
'message' => "Validation failed ({$context})",
'errors' => $errors
], 400);
}
// OK - Done
public function update($InternalPID = null) {
try {
if (!$InternalPID || !is_numeric($InternalPID)) {
return $this->respond(['status' => 'error', 'message' => 'Invalid or missing InternalPID'], 400);
}
$now = $this->now;
if (!$InternalPID || !is_numeric($InternalPID)) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing InternalPID'], 400); }
$input = $this->request->getJSON(true);
if (!$input) {
return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400);
}
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);
}
if (!$patient) { return $this->respond(['status' => 'error', 'message' => 'Patient not found'], 404); }
$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
// $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,
// ];
$dataPatient = [
"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),
// "Custodian" => ($input['Custodian'] ?? null) === '' ? null : ($input['Custodian'] ?? null),
"Custodian" => ($input['Custodian'] ?? null) === '' ? null : (isset($input['Custodian']) ? (int)$input['Custodian'] : null),
"LinkTo" => $LinkTo,
];
$dataPatient = $this->preparePatientData($input, $now, 'update');
$dataPatidt = $this->preparePatidtData($input, $now, 'update');
$dataPatcom = $this->preparePatcomData($input, $now, 'update');
$dataPatatt = $this->preparePatattData($input, $now, 'update');
// Atur aturan validasi dengan pengecualian is_unique untuk InternalPID ini
$rulesDataPatient = [
@ -438,28 +381,13 @@ class Patient extends Controller {
'AlternatePID' => 'permit_empty|max_length[50]',
'NameFirst' => 'required|min_length[1]|max_length[255]',
'EmailAddress1' => "required|is_unique[patient.EmailAddress1,InternalPID,{$InternalPID}]",
'DeathIndicator' => 'required',
'Gender' => 'required'
];
// Ambil data patidt
$patidt = $this->db->table('patidt')->where('InternalPID', $InternalPID)->get()->getRowArray();
$dataPatidt = [
"IdentifierType" => $input['Identity']['IdentifierType'] === '' ? null : ($input['Identity']['IdentifierType'] ?? null),
"Identifier" => $input['Identity']['Identifier'] === '' ? null : ($input['Identity']['Identifier'] ?? null),
];
$rulesDataPatidt = [
'Identifier' => "required|is_unique[patidt.Identifier,InternalPID,{$InternalPID}]"
];
$dataPatcom = [ "Comment" => $input['Comment'] === '' ? null : ($input['Comment'] ?? null) ];
if($dataPatcom['Comment'] != '') {
$dataPatcom['InternalPID'] = $InternalPID;
$dataPatcom['CreateDate'] = $this->now;
$this->db->table('patcom')->upsert($dataPatcom);
}
// Validasi
if (!$this->validateData($dataPatient, $rulesDataPatient)) {
return $this->respond([
@ -477,7 +405,6 @@ class Patient extends Controller {
], 400);
}
// Transaksi update
$this->db->transStart();
$this->db->table('patient')->where('InternalPID', $InternalPID)->update($dataPatient);
@ -494,6 +421,21 @@ class Patient extends Controller {
return $this->failServerError('Update patidt failed: ' . $dbError['message']);
}
if (!empty($dataPatatt)) {
foreach ($dataPatatt as &$row) {
$row['InternalPID'] = $InternalPID;
}
$this->db->table('patatt')->upsertBatch($dataPatatt);
$addresses = array_column($dataPatatt, 'Address');
$this->db->table('patatt')->where('InternalPID', $InternalPID)->WhereNotIn('Address', $addresses)->delete();
}
if(!empty($dataPatcom['Comment'])) {
$dataPatcom['InternalPID'] = $InternalPID;
$dataPatcom['CreateDate'] = $this->now;
$this->db->table('patcom')->upsert($dataPatcom);
}
$this->db->transComplete();
if ($this->db->transStatus() === false) {