Update Patient Tabel Migration & datetime 0000-00-00 & detail messages error

This commit is contained in:
mikael-zakaria 2025-10-06 00:24:55 +07:00
parent c667243b88
commit 082e5d4d1c
2 changed files with 54 additions and 14 deletions

View File

@ -78,7 +78,7 @@ class CreatePatientRegTables extends Migration {
'Religion' => ['type' => 'INT', 'constraint' => 11, 'null' => true], 'Religion' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
'Ethnic' => ['type' => 'INT', 'constraint' => 11, 'null' => true], 'Ethnic' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
'Citizenship' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true], 'Citizenship' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
'DeathIndicator'=> ['type' => 'TINYINT', 'null' => true], 'DeathIndicator'=> ['type' => 'INT', 'constraint' => 11, 'null' => true],
'DeathDateTime' => ['type' => 'DATETIME', 'null' => true], 'DeathDateTime' => ['type' => 'DATETIME', 'null' => true],
'LinkTo' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true], 'LinkTo' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
'CreateDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP', 'CreateDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP',

View File

@ -98,7 +98,7 @@ class PatientModel extends Model {
} }
if ($row['Comment']) { if ($row['Comment']) {
$patient['PatCom'][] = ['Comment' => $row['Comment']]; $patient['PatCom'] = $row['Comment'];
} }
if ($row['Address']) { if ($row['Address']) {
@ -116,23 +116,28 @@ class PatientModel extends Model {
$db = \Config\Database::connect(); $db = \Config\Database::connect();
$patidt = $input['PatIdt'] ?? []; $patidt = $input['PatIdt'] ?? [];
$patatt = $input['PatAtt'] ?? []; $patatt = $input['PatAtt'] ?? [];
$patcom = $input['PatCom'] ?? []; $patcom['Comment'] = $input['PatCom'] ?? [];
$input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo']; $input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo'];
$input['Birthdate'] = $this->isValidDateTime($input['Birthdate']);
$input['DeathDateTime'] = $this->isValidDateTime($input['DeathDateTime']);
try { try {
$db->transStart(); $db->transStart();
$this->insert($input); $this->insert($input);
$newInternalPID = $this->getInsertID(); $newInternalPID = $this->getInsertID();
$this->checkDbError($db, 'Insert patient');
if (!empty($patidt)) { if (!empty($patidt)) {
$patidt['InternalPID'] = $newInternalPID; $patidt['InternalPID'] = $newInternalPID;
$db->table('patidt')->insert($patidt); $db->table('patidt')->insert($patidt);
$this->checkDbError($db, 'Insert patidt');
} }
if (!empty($patcom)) { if (!empty($patcom)) {
$patcom['InternalPID'] = $newInternalPID; $patcom['InternalPID'] = $newInternalPID;
$db->table('patcom')->insert($patcom); $db->table('patcom')->insert($patcom);
$this->checkDbError($db, 'Insert patcom');
} }
if (!empty($patatt)) { if (!empty($patatt)) {
@ -140,18 +145,17 @@ class PatientModel extends Model {
$row['InternalPID'] = $newInternalPID; $row['InternalPID'] = $newInternalPID;
} }
$db->table('patatt')->upsertBatch($patatt); $db->table('patatt')->upsertBatch($patatt);
$this->checkDbError($db, 'Insert patatt');
} }
$db->transComplete();
$db->transComplete();
if ($db->transStatus() === false) { if ($db->transStatus() === false) {
$error = $db->error(); throw new \Exception("Failed to sync patient relations");
throw new \Exception('Transaction failed: ' . ($error['message'] ?? 'Unknown DB error'));
} }
return $newInternalPID; return $newInternalPID;
} catch (\Exception $e) { } catch (\Exception $e) {
$db->transRollback(); // $db->transRollback();
throw $e; throw $e;
} }
} }
@ -160,14 +164,17 @@ class PatientModel extends Model {
$db = \Config\Database::connect(); $db = \Config\Database::connect();
$patidt = $input['PatIdt'] ?? []; $patidt = $input['PatIdt'] ?? [];
$patatt = $input['PatAtt'] ?? []; $patatt = $input['PatAtt'] ?? [];
$patcom = $input['PatCom'] ?? []; $patcom['Comment'] = $input['PatCom'] ?? [];
$input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo']; $input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo'];
$input['Birthdate'] = $this->isValidDateTime($input['Birthdate']);
$input['DeathDateTime'] = $this->isValidDateTime($input['DeathDateTime']);
try { try {
$db->transStart(); $db->transStart();
$InternalPID = $input['InternalPID']; $InternalPID = $input['InternalPID'];
$this->where('InternalPID',$InternalPID)->set($input)->update(); $this->where('InternalPID',$InternalPID)->set($input)->update();
$this->checkDbError($db, 'Update patient');
$now = date('Y-m-d H:i:s'); $now = date('Y-m-d H:i:s');
if (!empty($patidt)) { if (!empty($patidt)) {
@ -177,9 +184,11 @@ class PatientModel extends Model {
} else { } else {
$patidt['InternalPID'] = $InternalPID; $patidt['InternalPID'] = $InternalPID;
$db->table('patidt')->insert($patidt); $db->table('patidt')->insert($patidt);
$this->checkDbError($db, 'Update patidt');
} }
} else { } else {
$db->table('patidt')->where('InternalPID', $InternalPID)->delete(); $db->table('patidt')->where('InternalPID', $InternalPID)->delete();
$this->checkDbError($db, 'Update patidt');
} }
if (!empty($patcom)) { if (!empty($patcom)) {
@ -189,9 +198,11 @@ class PatientModel extends Model {
} else { } else {
$patcom['InternalPID'] = $InternalPID; $patcom['InternalPID'] = $InternalPID;
$db->table('patcom')->insert($patcom); $db->table('patcom')->insert($patcom);
$this->checkDbError($db, 'Update patcom');
} }
} else { } else {
$db->table('patcom')->where('InternalPID', $InternalPID)->delete(); $db->table('patcom')->where('InternalPID', $InternalPID)->delete();
$this->checkDbError($db, 'Update patcom');
} }
if (!empty($patatt)) { if (!empty($patatt)) {
@ -223,6 +234,7 @@ class PatientModel extends Model {
->whereIn('Address', $removed) ->whereIn('Address', $removed)
->set('DelDate', $now) ->set('DelDate', $now)
->update(); ->update();
$this->checkDbError($db, 'Update/Delete patatt');
} }
// 2) Tambahkan yang baru // 2) Tambahkan yang baru
@ -238,30 +250,33 @@ class PatientModel extends Model {
$builder->where('InternalPID', $InternalPID) $builder->where('InternalPID', $InternalPID)
->where('Address', $addr) ->where('Address', $addr)
->where('(DelDate IS NOT NULL)', null, false) ->where('DelDate IS NOT NULL', null, false)
->orderBy('PatAttID', 'DESC') ->orderBy('PatAttID', 'DESC')
->limit(1) ->limit(1)
->update(); ->update();
$this->checkDbError($db, 'Update/Insert patatt');
if ($db->affectedRows() === 0) { if ($db->affectedRows() === 0) {
// Tidak ada baris soft-deleted untuk alamat ini → INSERT baru // Tidak ada baris soft-deleted untuk alamat ini → INSERT baru
$db->table('patatt')->insert($data); $db->table('patatt')->insert($data);
$this->checkDbError($db, 'Update/Insert patatt');
} }
} }
} else { } else {
// Input kosong → semua yang masih aktif di-soft delete // Input kosong → semua yang masih aktif di-soft delete
$db->table('patatt')->where('InternalPID', $InternalPID)->where('DelDate', null)->set('DelDate', $now)->update(); $db->table('patatt')->where('InternalPID', $InternalPID)->where('DelDate', null)->set('DelDate', $now)->update();
$this->checkDbError($db, 'Update/Delete patatt');
} }
$db->transComplete(); $db->transComplete();
if ($db->transStatus() === false) { if ($db->transStatus() === false) {
throw new \Exception('Failed to sync patient relations'); throw new \Exception('Failed to sync patient relations');
} }
return $InternalPID; return $InternalPID;
} catch (\Exception $e) { } catch (\Exception $e) {
$db->transRollback(); // $db->transRollback();
throw $e; throw $e;
} }
} }
@ -360,4 +375,29 @@ class PatientModel extends Model {
return $date->format('j M Y'); return $date->format('j M Y');
} }
// Check Error and Send Spesific Messages
private function checkDbError($db, string $context) {
$error = $db->error();
if (!empty($error['code'])) {
throw new \Exception(
"{$context} failed: {$error['code']} - {$error['message']}"
);
}
}
// Preventif 0000-00-00
private function isValidDateTime($datetime) {
// Check datetime format
$formats = ['Y-m-d', 'Y-m-d H:i:s'];
foreach ($formats as $format) {
$dt = \DateTime::createFromFormat($format, $datetime);
if ($dt !== false && $dt->format($format) === $datetime) {
return $datetime;
}
}
return null;
}
} }