diff --git a/app/Controllers/PatVisit.php b/app/Controllers/PatVisit.php index 488661d..3c1ecf6 100644 --- a/app/Controllers/PatVisit.php +++ b/app/Controllers/PatVisit.php @@ -13,18 +13,19 @@ class PatVisit extends Controller { $this->visnum_prefix = "DV"; } - private function preparePVID($PVID) { + private function preparePVID() { $row = $this->db->table('counter')->select('*')->where('CounterID','2')->get()->getResultArray(); $cValue = $row[0]['CounterValue']; $cStart = $row[0]['CounterStart']; $cEnd = $row[0]['CounterEnd']; $cReset = $row[0]['CounterReset']; - $cPad = strlen((string)$cEnd); - $cValue_next = $cValue++; + $cPad = strlen((string)$cEnd); + if($cValue > $cEnd) { $cValue = $cStart; } + $cnum = $this->visnum_prefix.str_pad($cValue, $cPad, "0", STR_PAD_LEFT); + $cValue_next = $cValue+1; // next value > end, back to start - if($cValue_next > $cEnd) { $cValue_next = $cStart; } - $row = $this->db->table('counter')->set('CounterValue', $cValue_next)->where('CounterID','2'); - return $this->visnum_prefix.str_pad($cValue, $cPad, "0", STR_PAD_LEFT); + $this->db->table('counter')->set('CounterValue', $cValue_next)->where('CounterID','2')->update(); + return $cnum; } public function show($PVID = null) { @@ -103,7 +104,10 @@ class PatVisit extends Controller { $input = $this->request->getJSON(true); if (!$input) { return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400); } - $input['PVID'] = $this->preparePVID($input['PVID']); + if($input['PVID'] =='' || !isset($input['PVID'])) { + $input['PVID'] = $this->preparePVID(); + } + $dataPatVisit = $this->preparePatVisitData($input); $dataPatDiag = $this->preparePatDiagData($input); $dataPatVisitAdt = $this->preparePatVisitAdtData($input); @@ -140,7 +144,6 @@ class PatVisit extends Controller { 'message' => 'Data insert success', 'data' => $dataPatVisit ], 201); - } catch (\Exception $e) { $this->db->transRollback(); return $this->failServerError('Something went wrong: ' . $e->getMessage()); diff --git a/app/Database/Migrations/2025-09-09-155526_Pat_Visit.php b/app/Database/Migrations/2025-09-09-155526_Pat_Visit.php index 4ae0d88..2794ce5 100644 --- a/app/Database/Migrations/2025-09-09-155526_Pat_Visit.php +++ b/app/Database/Migrations/2025-09-09-155526_Pat_Visit.php @@ -19,6 +19,7 @@ class CreatePVTables extends Migration { ]); $this->forge->addField('CreateDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP'); $this->forge->addKey('InternalPVID', true); + $this->forge->addUniqueKey('PVID', true); $this->forge->createTable('patvisit'); // patdiag