Update Patient Tabel Migration & datetime 0000-00-00 & detail messages error
This commit is contained in:
parent
c667243b88
commit
082e5d4d1c
@ -78,7 +78,7 @@ class CreatePatientRegTables extends Migration {
|
||||
'Religion' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
|
||||
'Ethnic' => ['type' => 'INT', 'constraint' => 11, '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],
|
||||
'LinkTo' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
|
||||
'CreateDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP',
|
||||
|
||||
@ -98,7 +98,7 @@ class PatientModel extends Model {
|
||||
}
|
||||
|
||||
if ($row['Comment']) {
|
||||
$patient['PatCom'][] = ['Comment' => $row['Comment']];
|
||||
$patient['PatCom'] = $row['Comment'];
|
||||
}
|
||||
|
||||
if ($row['Address']) {
|
||||
@ -116,23 +116,28 @@ class PatientModel extends Model {
|
||||
$db = \Config\Database::connect();
|
||||
$patidt = $input['PatIdt'] ?? [];
|
||||
$patatt = $input['PatAtt'] ?? [];
|
||||
$patcom = $input['PatCom'] ?? [];
|
||||
$patcom['Comment'] = $input['PatCom'] ?? [];
|
||||
$input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo'];
|
||||
|
||||
$input['Birthdate'] = $this->isValidDateTime($input['Birthdate']);
|
||||
$input['DeathDateTime'] = $this->isValidDateTime($input['DeathDateTime']);
|
||||
|
||||
try {
|
||||
$db->transStart();
|
||||
|
||||
$this->insert($input);
|
||||
$newInternalPID = $this->getInsertID();
|
||||
$this->checkDbError($db, 'Insert patient');
|
||||
|
||||
if (!empty($patidt)) {
|
||||
$patidt['InternalPID'] = $newInternalPID;
|
||||
$db->table('patidt')->insert($patidt);
|
||||
$this->checkDbError($db, 'Insert patidt');
|
||||
}
|
||||
|
||||
if (!empty($patcom)) {
|
||||
$patcom['InternalPID'] = $newInternalPID;
|
||||
$db->table('patcom')->insert($patcom);
|
||||
$this->checkDbError($db, 'Insert patcom');
|
||||
}
|
||||
|
||||
if (!empty($patatt)) {
|
||||
@ -140,18 +145,17 @@ class PatientModel extends Model {
|
||||
$row['InternalPID'] = $newInternalPID;
|
||||
}
|
||||
$db->table('patatt')->upsertBatch($patatt);
|
||||
$this->checkDbError($db, 'Insert patatt');
|
||||
}
|
||||
|
||||
$db->transComplete();
|
||||
|
||||
$db->transComplete();
|
||||
if ($db->transStatus() === false) {
|
||||
$error = $db->error();
|
||||
throw new \Exception('Transaction failed: ' . ($error['message'] ?? 'Unknown DB error'));
|
||||
throw new \Exception("Failed to sync patient relations");
|
||||
}
|
||||
return $newInternalPID;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$db->transRollback();
|
||||
// $db->transRollback();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@ -160,14 +164,17 @@ class PatientModel extends Model {
|
||||
$db = \Config\Database::connect();
|
||||
$patidt = $input['PatIdt'] ?? [];
|
||||
$patatt = $input['PatAtt'] ?? [];
|
||||
$patcom = $input['PatCom'] ?? [];
|
||||
$patcom['Comment'] = $input['PatCom'] ?? [];
|
||||
$input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo'];
|
||||
$input['Birthdate'] = $this->isValidDateTime($input['Birthdate']);
|
||||
$input['DeathDateTime'] = $this->isValidDateTime($input['DeathDateTime']);
|
||||
|
||||
try {
|
||||
$db->transStart();
|
||||
$InternalPID = $input['InternalPID'];
|
||||
$this->where('InternalPID',$InternalPID)->set($input)->update();
|
||||
|
||||
$this->checkDbError($db, 'Update patient');
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
|
||||
if (!empty($patidt)) {
|
||||
@ -177,9 +184,11 @@ class PatientModel extends Model {
|
||||
} else {
|
||||
$patidt['InternalPID'] = $InternalPID;
|
||||
$db->table('patidt')->insert($patidt);
|
||||
$this->checkDbError($db, 'Update patidt');
|
||||
}
|
||||
} else {
|
||||
$db->table('patidt')->where('InternalPID', $InternalPID)->delete();
|
||||
$this->checkDbError($db, 'Update patidt');
|
||||
}
|
||||
|
||||
if (!empty($patcom)) {
|
||||
@ -189,9 +198,11 @@ class PatientModel extends Model {
|
||||
} else {
|
||||
$patcom['InternalPID'] = $InternalPID;
|
||||
$db->table('patcom')->insert($patcom);
|
||||
$this->checkDbError($db, 'Update patcom');
|
||||
}
|
||||
} else {
|
||||
$db->table('patcom')->where('InternalPID', $InternalPID)->delete();
|
||||
$this->checkDbError($db, 'Update patcom');
|
||||
}
|
||||
|
||||
if (!empty($patatt)) {
|
||||
@ -223,6 +234,7 @@ class PatientModel extends Model {
|
||||
->whereIn('Address', $removed)
|
||||
->set('DelDate', $now)
|
||||
->update();
|
||||
$this->checkDbError($db, 'Update/Delete patatt');
|
||||
}
|
||||
|
||||
// 2) Tambahkan yang baru
|
||||
@ -238,30 +250,33 @@ class PatientModel extends Model {
|
||||
|
||||
$builder->where('InternalPID', $InternalPID)
|
||||
->where('Address', $addr)
|
||||
->where('(DelDate IS NOT NULL)', null, false)
|
||||
->where('DelDate IS NOT NULL', null, false)
|
||||
->orderBy('PatAttID', 'DESC')
|
||||
->limit(1)
|
||||
->update();
|
||||
$this->checkDbError($db, 'Update/Insert patatt');
|
||||
|
||||
if ($db->affectedRows() === 0) {
|
||||
// Tidak ada baris soft-deleted untuk alamat ini → INSERT baru
|
||||
$db->table('patatt')->insert($data);
|
||||
$this->checkDbError($db, 'Update/Insert patatt');
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// Input kosong → semua yang masih aktif di-soft delete
|
||||
$db->table('patatt')->where('InternalPID', $InternalPID)->where('DelDate', null)->set('DelDate', $now)->update();
|
||||
$this->checkDbError($db, 'Update/Delete patatt');
|
||||
}
|
||||
|
||||
$db->transComplete();
|
||||
|
||||
if ($db->transStatus() === false) {
|
||||
throw new \Exception('Failed to sync patient relations');
|
||||
throw new \Exception('Failed to sync patient relations');
|
||||
}
|
||||
return $InternalPID;
|
||||
} catch (\Exception $e) {
|
||||
$db->transRollback();
|
||||
// $db->transRollback();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@ -360,4 +375,29 @@ class PatientModel extends Model {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user