diff --git a/app/Controllers/Contact.php b/app/Controllers/Contact.php index 3a891e5..6c21553 100644 --- a/app/Controllers/Contact.php +++ b/app/Controllers/Contact.php @@ -16,10 +16,10 @@ class Contact extends Controller { } public function index() { - $sql = $this->db->table('contact'); - $sql->select("*") - ->join("contactdetail", "contact.ContactID=contactdetail.ContactID", "left") - ->join("occupation","contactdetail.OccupationID=occupation.OccupationID", "left"); + $sql = $this->db->table('contact c') + ->select("c.ContactID, cd.ContactCode, c.NameFirst, c.NameLast, c.Specialty") + ->join("contactdetail cd", "c.ContactID=cd.ContactID", "left") + ->join("occupation o","cd.OccupationID=o.OccupationID", "left"); $rows = $sql->get()->getResultArray(); if (empty($rows)) { diff --git a/app/Controllers/PatVisit.php b/app/Controllers/PatVisit.php index dd5d112..488661d 100644 --- a/app/Controllers/PatVisit.php +++ b/app/Controllers/PatVisit.php @@ -10,8 +10,23 @@ class PatVisit extends Controller { public function __construct() { $this->db = \Config\Database::connect(); + $this->visnum_prefix = "DV"; } + private function preparePVID($PVID) { + $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++; + // 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); + } + public function show($PVID = null) { try { $row = $this->db->table('patvisit pv') @@ -66,15 +81,9 @@ class PatVisit extends Controller { $this->db->transComplete(); - if (!empty($dbError['message'])) { - $this->db->transRollback(); - return $this->failServerError('Update failed: ' . $dbError['message']); - } - - if ($this->db->transStatus() === false) { $dbError = $this->db->error(); - return $this->failServerError('Failed to update patient data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown error')); + return $this->failServerError('Failed to create patient data (transaction rolled back): ' . ($dbError ?? 'Unknown error')); } return $this->respond([ @@ -94,6 +103,7 @@ 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']); $dataPatVisit = $this->preparePatVisitData($input); $dataPatDiag = $this->preparePatDiagData($input); $dataPatVisitAdt = $this->preparePatVisitAdtData($input); diff --git a/app/Database/Seeds/CounterSeeder.php b/app/Database/Seeds/CounterSeeder.php new file mode 100644 index 0000000..64f11be --- /dev/null +++ b/app/Database/Seeds/CounterSeeder.php @@ -0,0 +1,16 @@ +1, 'CounterValue'=>'1', 'CounterStart'=>'1', 'CounterEnd'=>'99999', 'CounterDesc'=>'Counter for Order#', 'CounterReset'=>'Y' ], + ['CounterID'=>2, 'CounterValue'=>'1', 'CounterStart'=>'1', 'CounterEnd'=>'9999', 'CounterDesc'=>'Counter for Visit#', 'CounterReset'=>'M' ], + ]; + $this->db->table('counter')->insertBatch($data); + } +} \ No newline at end of file diff --git a/app/Database/Seeds/DBSeeder.php b/app/Database/Seeds/DBSeeder.php index 2f14fff..226e79d 100644 --- a/app/Database/Seeds/DBSeeder.php +++ b/app/Database/Seeds/DBSeeder.php @@ -9,5 +9,6 @@ class DBSeeder extends Seeder { $this->call('MainSeeder'); $this->call('ValueSetSeeder'); $this->call('DummySeeder'); + $this->call('CounterSeeder'); } }