diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index ceb87ae..784b614 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -10,6 +10,7 @@ class Patient extends Controller { public function __construct() { $this->db = \Config\Database::connect(); + $this->now = date('Y-m-d H:i:s'); } // OK - Done @@ -62,12 +63,13 @@ class Patient extends Controller { try { $patient = $this->db->table('patient') - ->select('patient.*, country.Country as CountryName, race.Race as RaceName, religion.Religion as ReligionName, ethnic.Ethnic as EthnicName') + ->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') - ->where('InternalPID', (int) $InternalPID) + ->join('patcom', 'patcom.InternalPID = patient.InternalPID', 'left') + ->where('patient.InternalPID', (int) $InternalPID) ->get() ->getRowArray(); @@ -141,7 +143,6 @@ class Patient extends Controller { } - // OK - Done public function create() { try { $input = $this->request->getJSON(true); @@ -260,6 +261,8 @@ class Patient extends Controller { 'Address' => 'required|is_unique[patatt.Address]', ]; + $dataPatcom = [ "Comment" => $input['Comment'] === '' ? null : ($input['Comment'] ?? null) ]; + // ========================= // Validasi semua sebelum insert // ========================= @@ -297,6 +300,12 @@ class Patient extends Controller { $this->db->table('patatt')->insertBatch($dataPatatt); } + if($dataPatcom['Comment'] != '') { + $dataPatcom['InternalPID'] = $newInternalPatientId; + $dataPatcom['CreateDate'] = $now; + $this->db->table('patcom')->insert($dataPatcom); + } + $dbError = $this->db->error(); if (!empty($dbError['message'])) { $this->db->transRollback(); @@ -444,6 +453,13 @@ class Patient extends Controller { '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([ diff --git a/app/Database/Migrations/2025-09-02-070826_Init.php b/app/Database/Migrations/2025-09-02-070826_Init.php index 0e5bfc1..b765920 100644 --- a/app/Database/Migrations/2025-09-02-070826_Init.php +++ b/app/Database/Migrations/2025-09-02-070826_Init.php @@ -54,6 +54,7 @@ class CreateInitialTables extends Migration { 'EndDate' => ['type' => 'DATETIME', 'null' => true], ]); $this->forge->addKey('PatComID', true); + $this->forge->addUniqueKey('InternalPID', true); $this->forge->createTable('patcom'); // patdiag @@ -249,8 +250,7 @@ class CreateInitialTables extends Migration { $this->forge->createTable('religion'); } - public function down() - { + public function down() { $this->forge->dropTable('country', true); $this->forge->dropTable('ethnic', true); $this->forge->dropTable('patatt', true);