From 20350db5bdd3a132db96ad407ead50fc8a3dbd29 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Wed, 15 Oct 2025 11:01:52 +0700 Subject: [PATCH] refactor contact occupation counter location to model --- app/Config/Routes.php | 4 +- app/Controllers/Contact/Contact.php | 112 +++-------- app/Controllers/Contact/Occupation.php | 12 +- app/Controllers/Counter.php | 133 ++----------- app/Controllers/Location.php | 199 ++----------------- app/Models/Contact/ContactModel.php | 8 +- app/Models/Location/LocationAddressModel.php | 22 ++ app/Models/Location/LocationModel.php | 86 ++++++++ app/Models/{ => Patient}/PatientModel.php | 7 +- app/Models/Specimen/ContainerDefModel.php | 2 +- 10 files changed, 193 insertions(+), 392 deletions(-) create mode 100644 app/Models/Location/LocationAddressModel.php create mode 100644 app/Models/Location/LocationModel.php rename app/Models/{ => Patient}/PatientModel.php (98%) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 1c36a5c..383edb7 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -57,8 +57,8 @@ $routes->delete('/api/location', 'Location::delete'); $routes->get('/api/contact', 'Contact::index'); $routes->get('/api/contact/(:num)', 'Contact::show/$1'); -$routes->post('/api/contact', 'Contact::save'); -$routes->patch('/api/contact', 'Contact::save'); +$routes->post('/api/contact', 'Contact::create'); +$routes->patch('/api/contact', 'Contact::update'); $routes->delete('/api/contact', 'Contact::delete'); $routes->get('/api/occupation', 'Occupation::index'); diff --git a/app/Controllers/Contact/Contact.php b/app/Controllers/Contact/Contact.php index 5d2ba68..4a8cfe8 100644 --- a/app/Controllers/Contact/Contact.php +++ b/app/Controllers/Contact/Contact.php @@ -2,43 +2,33 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; -use CodeIgniter\Controller; +use App\Controllers\BaseController; use App\Models\Contact\ContactModel; -use App\Models\Contact\ContactDetailModel; -class Contact extends Controller { +class Contact extends BaseController { use ResponseTrait; protected $db; protected $model; - protected $rule; + protected $rules; public function __construct() { $this->db = \Config\Database::connect(); $this->model = new ContactModel(); - $this->rule = [ 'NameFirst' => 'required' ]; + $this->rules = [ 'NameFirst' => 'required' ]; } public function index() { $ContactName = $this->request->getVar('ContactName'); $Specialty = $this->request->getVar('Specialty'); $rows = $this->model->getContacts($ContactName, $Specialty); - //$rows = $model->getContacts(); if (empty($rows)) { - return $this->respond([ - 'status' => 'success', - 'message' => "no Data.", - 'data' => $rows, - ], 200); + return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } - return $this->respond([ - 'status' => 'success', - 'message'=> "fetch success", - 'data' => $rows, - ], 200); + return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200); } public function show($ContactID = null) { @@ -46,90 +36,44 @@ class Contact extends Controller { $rows = $model->getContactWithDetail($ContactID); if (empty($rows)) { - return $this->respond([ - 'status' => 'success', - 'message' => "Data not found.", - 'data' => [], - ], 200); + return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } - return $this->respond([ - 'status' => 'success', - 'message'=> "Data fetched successfully", - 'data' => $rows, - ], 200); + return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200); } public function delete() { try { $input = $this->request->getJSON(true); $ContactID = $input["ContactID"]; - if (!$ContactID) { - return $this->failValidationErrors('ContactID is required.'); - } - - $contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow(); - if (!$contact) { - return $this->failNotFound("data with {$ContactID} not found."); - } - - $this->db->table('contact')->where('ContactID', $ContactID)->update(['EndDate' => NOW()]); - - return $this->respondDeleted([ - 'status' => 'success', - 'message' => "Contact with {$ContactID} deleted successfully." - ]); - + if (!$ContactID) { return $this->failValidationErrors('ContactID is required.'); } + $this->model->delete($ContactID); + return $this->respondDeleted([ 'status' => 'success', 'message' => "Contact with {$ContactID} deleted successfully."]); } catch (\Throwable $e) { - // Ensure rollback if something goes wrong - if ($this->db->transStatus() !== false) { - $this->db->transRollback(); - } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } - public function save() { + public function create() { $input = $this->request->getJSON(true); - $contactModel = new ContactModel(); - $detailModel = new ContactDetailModel(); - $db = \Config\Database::connect(); - - $db->transStart(); - + if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } try { - if (!empty($input['ContactID'])) { - $ContactID = $input['ContactID']; - if (!$contactModel->update($ContactID, $input)) { throw new \RuntimeException('Failed to update contact'); } - } else { - $ContactID = $contactModel->insert($input, true); - if (!$ContactID) { throw new \RuntimeException('Failed to insert contact'); } - } - - if(isset($input['Details'])) { - $result = $detailModel->syncDetails($ContactID, $input['Details']); - if ($result['status'] !== 'success') { - throw new \RuntimeException('Failed to sync details: ' . $result['message']); - } - } - - $db->transComplete(); - - if ($db->transStatus() === false) { - throw new \RuntimeException('Transaction failed'); - } - - return $this->respondCreated([ - 'status' => 'success', - 'ContactID' => $ContactID, - ]); + $id = $this->model->saveContact($input,true); + return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201); } catch (\Throwable $e) { - $db->transRollback(); - log_message('error', 'saveContact error: ' . $e->getMessage()); - return $this->fail([ - 'status' => 'error', - 'message' => $e->getMessage(), - ], 500); + return $this->failServerError('Something went wrong: ' . $e->getMessage()); + } + } + + public function update() { + $input = $this->request->getJSON(true); + if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } + try { + $this->model->saveContact($input); + $id = $input['ContactID']; + return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201); + } catch (\Throwable $e) { + return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } } \ No newline at end of file diff --git a/app/Controllers/Contact/Occupation.php b/app/Controllers/Contact/Occupation.php index f3cdd01..b986e2f 100644 --- a/app/Controllers/Contact/Occupation.php +++ b/app/Controllers/Contact/Occupation.php @@ -2,7 +2,7 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; -use CodeIgniter\BaseController; +use App\Controllers\BaseController; use App\Models\Contact\OccupationModel; class Occupation extends BaseController { @@ -40,8 +40,9 @@ class Occupation extends BaseController { public function create() { $input = $this->request->getJSON(true); try { - $insert = $this->model->insert($input); - return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $input ], 201); + $this->model->insert($input); + $id = $this->model->getInsertID(); + return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201); } catch (\Throwable $e) { $this->db->transRollback(); return $this->failServerError('Exception : ' . $e->getMessage()); @@ -51,10 +52,9 @@ class Occupation extends BaseController { public function update() { $input = $this->request->getJSON(true); try { - $this->modelOccupation->update($input['OccupationID'], $input); - return $this->respondCreated([ 'status' => 'success', 'message' => 'Data updated successfully', 'data' => $input ], 201); + $this->model->update($input['OccupationID'], $input); + return $this->respondCreated([ 'status' => 'success', 'message' => 'Data updated successfully', 'data' => $input['OccupationID'] ], 201); } catch (\Throwable $e) { - $this->db->transRollback(); return $this->failServerError('Exception : ' . $e->getMessage()); } } diff --git a/app/Controllers/Counter.php b/app/Controllers/Counter.php index 025ca27..dbc04a8 100644 --- a/app/Controllers/Counter.php +++ b/app/Controllers/Counter.php @@ -2,157 +2,64 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; -use CodeIgniter\Controller; -use CodeIgniter\Database\RawSql; +use App\Controllers\BaseController; +use App\Models\CounterModel; -class Counter extends Controller { +class Counter extends BaseController { use ResponseTrait; - protected $db; protected $model; public function __construct() { - $this->db = \Config\Database::connect(); + $this->model = new CounterModel(); } public function index() { - $rows = $this->db->table('counter')->select("*")->get()->getResultArray(); + $rows = $this->model->findAll(); - if (empty($rows)) { - return $this->respond([ - 'status' => 'success', - 'message' => "No Data.", - 'data' => [], - ], 200); + if (empty($rows)) { + return $this->respond([ 'status' => 'success', 'message' => "No Data.", 'data' => [] ], 200); } - return $this->respond([ - 'status' => 'success', - 'message'=> "Data fetched successfully", - 'data' => $rows, - ], 200); + return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); } public function show($CounterID = null) { - $rows = $this->db->table('counter')->select("*")->where('CounterID', (int) $CounterID)->get()->getResultArray(); + $rows = $this->model->find($CounterID); - if (empty($rows)) { - return $this->respond([ - 'status' => 'success', - 'message' => "Data not found.", - 'data' => [], - ], 200); + if (empty($rows)) { + return $this->respond([ 'status' => 'success', 'message' => "No Data.", 'data' => [] ], 200); } - return $this->respond([ - 'status' => 'success', - 'message'=> "Data fetched successfully", - 'data' => $rows, - ], 200); + return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); } public function create() { $input = $this->request->getJSON(true); - $dataCounter = $this->prepareCounterData($input); - try { - $this->db->transStart(); - $this->db->table('counter')->insert($dataCounter); - $this->db->transComplete(); - - if ($this->db->transStatus() === false) { - $dbError = $this->db->error(); - return $this->failServerError( - 'Failed to create data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') - ); - } - - return $this->respondCreated([ - 'status' => 'success', - 'message' => 'Data created successfully', - 'data' => $dataCounter, - ], 201); - + $id = $this->model->insert($input,true); + return $this->respondCreated([ 'status' => 'success', 'message' => 'Data created successfully', 'data' => $id ], 201); } catch (\Throwable $e) { - // Ensure rollback if something goes wrong - if ($this->db->transStatus() !== false) { - $this->db->transRollback(); - } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function update() { + $input = $this->request->getJSON(true); try { - $input = $this->request->getJSON(true); - $dataCounter = $this->prepareCounterData($input); - - $this->db->transStart(); - $this->db->table('counter')->where('CounterID', $dataCounter["CounterID"])->update($dataCounter); - $this->db->transComplete(); - - if ($this->db->transStatus() === false) { - $dbError = $this->db->error(); - return $this->failServerError( - 'Failed to update data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') - ); - } - - return $this->respondCreated([ - 'status' => 'success', - 'message' => 'Data updated successfully', - 'data' => $dataCounter, - ], 201); - + $this->model->update($input['CounterID'], $input); + return $this->respondCreated([ 'status' => 'success', 'message' => 'Data updated successfully', 'data' => $input['CounterID'] ], 201); } catch (\Throwable $e) { - // Ensure rollback if something goes wrong - if ($this->db->transStatus() !== false) { - $this->db->transRollback(); - } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function delete() { + $input = $this->request->getJSON(true); try { - $input = $this->request->getJSON(true); - $CounterID = $input["CounterID"]; - if (!$CounterID) { - return $this->failValidationErrors('CounterID is required.'); - } - - - $location = $this->db->table('counter')->where('CounterID', $CounterID)->get()->getRow(); - if (!$location) { - return $this->failNotFound("Data not found."); - } - - $this->db->table('counter')->where('CounterID', $CounterID)->update(['EndDate' => NOW()]); - - return $this->respondDeleted([ - 'status' => 'success', - 'message' => "Counter deleted successfully." - ]); - + $this->model->delete($input['CounterID'], $input); + return $this->respondCreated([ 'status' => 'success', 'message' => 'Data deleted successfully', 'data' => $input['CounterID'] ], 201); } catch (\Throwable $e) { - // Ensure rollback if something goes wrong - if ($this->db->transStatus() !== false) { - $this->db->transRollback(); - } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } - - private function prepareCounterData(array $input): array { - $data = [ - "CounterValue" => $input['CounterValue'] ?? null, - "CounterStart" => $input['CounterStart'] ?? null, - "CounterEnd" => $input['CounterEnd'] ?? null, - "CounterDesc" => $input['CounterDesc'] ?? null, - "CounterReset" => $input['CounterReset'] ?? null, - ]; - - if(!empty($input["CounterID"])) { $data["CounterID"] = $input["CounterID"]; } - - return $data; - } } \ No newline at end of file diff --git a/app/Controllers/Location.php b/app/Controllers/Location.php index fe447dd..3257e56 100644 --- a/app/Controllers/Location.php +++ b/app/Controllers/Location.php @@ -2,16 +2,17 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; -use CodeIgniter\Controller; +use App\Controllers\BaseController; +use App\Models\Location\LocationModel; -class Location extends Controller { +class Location extends BaseController { use ResponseTrait; - protected $db; + protected $model; protected $rules; public function __construct() { - $this->db = \Config\Database::connect(); + $this->model = new LocationModel(); $this->rules = [ 'LocCode' => 'required|max_length[6]', 'LocFull' => 'required', @@ -21,210 +22,54 @@ class Location extends Controller { public function index() { $LocName = $this->request->getVar('LocName'); $LocCode = $this->request->getVar('LocCode'); - - $sql = $this->db->table('location l') - ->select("l.LocationID, LocCode, Parent, LocFull, LocType, v.VDesc ") - ->join("locationaddress la", "l.LocationID=la.LocationID", 'left') - ->join("valueset v", "v.VSetID=12 and v.VValue=l.loctype", 'left'); - - if($LocName != '') { $sql->like('LocFull', $LocName, 'both'); } - if($LocCode != '') { $sql->like('LocCode', $LocCode, 'both'); } - - $rows = $sql->get()->getResultArray(); - + $rows = $this->model->getLocations($LocCode,$LocName); if (empty($rows)) { - return $this->respond([ - 'status' => 'success', - 'message' => "Location no Data.", - 'data' => [], - ], 200); + return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } - return $this->respond([ - 'status' => 'success', - 'message'=> "Locations fetched successfully", - 'data' => $rows, - ], 200); + return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200); } - - public function show($LocationID = null) { - $rows = $this->db->table('location l') - ->select("l.*, la.*, v.*") - ->join("locationaddress la", "l.LocationID=la.LocationID", "left") - ->join("valueset v", "v.VSetID=12 and v.VValue=l.loctype", "left") - ->where('l.LocationID', (int) $LocationID) - ->get()->getResultArray(); + public function show($LocationID = null) { + $rows = $this->model->getLocation($LocationID); if (empty($rows)) { - return $this->respond([ - 'status' => 'success', - 'message' => "Data not found.", - 'data' => [], - ], 200); + return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } - return $this->respond([ - 'status' => 'success', - 'message'=> "Locations fetched successfully", - 'data' => $rows, - ], 200); + return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200); } public function create() { $input = $this->request->getJSON(true); - $dataLocation = $this->prepareLocationData($input); - $dataLocationAddress = $this->prepareLocationAddressData($input); - if (!$this->validateData($dataLocation, $this->rules)) { - return $this->failValidationErrors($this->validator->getErrors()); - } - + if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } try { - $this->db->transStart(); - $this->db->table('location')->insert($dataLocation); - $newLocationID = $this->db->insertID(); - - if (!empty($dataLocationAddress)) { - $dataLocationAddress['LocationID'] = $newLocationID; - $this->db->table('locationaddress')->insert($dataLocationAddress); - } - $this->db->transComplete(); - - if ($this->db->transStatus() === false) { - $dbError = $this->db->error(); - return $this->failServerError( - 'Failed to create location data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') - ); - } - - return $this->respondCreated([ - 'status' => 'success', - 'message' => 'Location created successfully', - 'data' => $dataLocation, - ], 201); - + $data = $this->model->saveLocation($input); + return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $data['LocationID'] ], 201); } catch (\Throwable $e) { - // Ensure rollback if something goes wrong - if ($this->db->transStatus() !== false) { - $this->db->transRollback(); - } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function update() { + $input = $this->request->getJSON(true); try { - $input = $this->request->getJSON(true); - - // Prepare data - $dataLocation = $this->prepareLocationData($input); - $dataLocationAddress = $this->prepareLocationAddressData($input); - - if (!$this->validateData($dataLocation, $this->rules)) { - return $this->failValidationErrors( $this->validator->getErrors()); - } - - // Start transaction - $this->db->transStart(); - - // Insert location - $this->db->table('location')->where('LocationID', $dataLocation["LocationID"])->update($dataLocation); - - // Insert address if available - if (!empty($dataLocationAddress)) { - $dataLocationAddress['LocationID'] = $input["LocationID"]; - $this->db->table('locationaddress')->upsert($dataLocationAddress); - } - - // Complete transaction - $this->db->transComplete(); - - if ($this->db->transStatus() === false) { - $dbError = $this->db->error(); - return $this->failServerError( - 'Failed to update location data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') - ); - } - - return $this->respondCreated([ - 'status' => 'success', - 'message' => 'Location updated successfully', - 'data' => $dataLocation, - ], 201); - + if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors()); } + $this->model->saveLocation($input); + return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $input['LocationID'] ], 201); } catch (\Throwable $e) { - // Ensure rollback if something goes wrong - if ($this->db->transStatus() !== false) { - $this->db->transRollback(); - } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function delete() { + $input = $this->request->getJSON(true); try { - $input = $this->request->getJSON(true); $LocationID = $input["LocationID"]; - if (!$LocationID) { - return $this->failValidationErrors('LocationID is required.'); - } - - - $location = $this->db->table('location')->where('LocationID', $LocationID)->get()->getRow(); - if (!$location) { - return $this->failNotFound("LocationID with {$LocationID} not found."); - } - - $this->db->table('location')->where('LocationID', $LocationID)->update(['DelDate' => NOW()]); - - return $this->respondDeleted([ - 'status' => 'success', - 'message' => "Location with {$LocationID} deleted successfully." - ]); - + $this->model->deleteLocation($LocationID); + return $this->respondDeleted([ 'status' => 'success', 'message' => "Location with {$LocationID} deleted successfully." ]); } catch (\Throwable $e) { - // Ensure rollback if something goes wrong - if ($this->db->transStatus() !== false) { - $this->db->transRollback(); - } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } - private function prepareLocationData(array $input): array { - $LinkTo = null; - if (!empty($input['LinkTo'])) { - $ids = array_column($input['LinkTo'], 'InternalPID'); - $LinkTo = implode(',', $ids); - } - - $data = [ - "LocCode" => $input['LocCode'] ?? null, - "Parent" => $input['Parent'] ?? null, - "LocFull" => $input['LocFull'] ?? null, - "LocType" => $input['LocType'] ?? null, - "Description" => $input['Description'] ?? null, - ]; - - if(!empty($input["LocationID"])) { $data["LocationID"] = $input["LocationID"]; } - - return $data; - } - - private function prepareLocationAddressData(array $input): array { - $data = [ - "LocationID" => $input['LocationID'] ?? null, - "Street1" => $input['Street1'] ?? null, - "Street2" => $input['Street2'] ?? null, - "City" => $input['City'] ?? null, - "Province" => $input['Province'] ?? null, - "PostCode" => $input['PostCode'] ?? null, - "GeoLocationSystem" => $input['GeoLocationSystem'] ?? null, - "GeoLocationData" => $input['GeoLocationData'] ?? null, - "Email" => $input['Email'] ?? null, - "Phone" => $input['Phone'] ?? null, - "Mobile" => $input['Mobile'] ?? null, - ]; - - return $data; - } } \ No newline at end of file diff --git a/app/Models/Contact/ContactModel.php b/app/Models/Contact/ContactModel.php index 1e91c6f..41acb30 100644 --- a/app/Models/Contact/ContactModel.php +++ b/app/Models/Contact/ContactModel.php @@ -77,7 +77,7 @@ class ContactModel extends BaseModel { return $contact; } - public function saveWithDetails(array $data): array { + public function saveContact(array $data): array { $db = \Config\Database::connect(); $db->transStart(); @@ -94,8 +94,8 @@ class ContactModel extends BaseModel { } if (!empty($data['Details'])) { - $detailModel = new \App\Models\ContactDetailModel(); - $result = $detailModel->syncDetails($contactId, $data['Details']); + $modelDetail = new \App\Models\Contact\ContactDetailModel(); + $result = $modelDetail->syncDetails($contactId, $data['Details']); if ($result['status'] !== 'success') { throw new \RuntimeException('SyncDetails failed: ' . $result['message']); @@ -111,8 +111,6 @@ class ContactModel extends BaseModel { } catch (\Throwable $e) { $db->transRollback(); - log_message('error', 'saveWithDetails error: ' . $e->getMessage()); - return [ 'status' => 'error', 'message' => $e->getMessage(), diff --git a/app/Models/Location/LocationAddressModel.php b/app/Models/Location/LocationAddressModel.php new file mode 100644 index 0000000..4065e1c --- /dev/null +++ b/app/Models/Location/LocationAddressModel.php @@ -0,0 +1,22 @@ +select("LocationID, LocCode, Parent, LocFull, LocType, v.VDesc ") + ->join("valueset v", "v.VSetID=12 and v.VValue=location.loctype", 'left'); + if($LocName != '') { $sql->like('LocFull', $LocName, 'both'); } + if($LocCode != '') { $sql->like('LocCode', $LocCode, 'both'); } + $rows = $sql->get()->getResultArray(); + + return $rows; + } + + public function getLocation($LocationID) { + $rows = $this->select("location.*, la.*, v.*") + ->join("locationaddress la", "location.LocationID=la.LocationID", "left") + ->join("valueset v", "v.VSetID=12 and v.VValue=location.loctype", "left") + ->where('location.LocationID', (int) $LocationID) + ->get()->getResultArray(); + return $rows; + } + + public function saveLocation(array $data): array { + $modelAddress = new \App\Models\Location\LocationAddressModel(); + $db = \Config\Database::connect(); + $db->transBegin(); + try { + if (!empty($data['LocationID'])) { + $LocationID = $data['LocationID']; + $this->update($LocationID, $data); + $modelAddress->update($LocationID, $data); + } else { + $LocationID = $this->insert($data, true); + $data['LocationID'] = $LocationID; + $modelAddress->insert($data); + } + if ($db->transStatus() === false) { + $db->transRollback(); + throw new \Exception('Transaction failed'); + } + $db->transCommit(); + return [ 'status' => 'success', 'LocationID' => $LocationID ]; + } catch (\Throwable $e) { + $db->transRollback(); + return [ 'status' => 'error', 'message' => $e->getMessage() ]; + } + } + + public function deleteLocation(array $data): array { + $modelAddress = new \App\Models\Location\LocationAddressModel(); + $db = \Config\Database::connect(); + $db->transBegin(); + try { + $LocationID = $data['LocationID']; + $this->delete($LocationID); + $modelAddress->delete($LocationID); + if ($db->transStatus() === false) { + $db->transRollback(); + throw new \Exception('Transaction failed'); + } + $db->transCommit(); + return [ 'status' => 'success', 'LocationID' => $LocationID ]; + } catch (\Throwable $e) { + $db->transRollback(); + return [ 'status' => 'error', 'message' => $e->getMessage() ]; + } + } +} diff --git a/app/Models/PatientModel.php b/app/Models/Patient/PatientModel.php similarity index 98% rename from app/Models/PatientModel.php rename to app/Models/Patient/PatientModel.php index af0cee8..8f7dd73 100644 --- a/app/Models/PatientModel.php +++ b/app/Models/Patient/PatientModel.php @@ -1,7 +1,7 @@ select("InternalPID, PatientID, $qname as FullName, Gender, Birthdate, EmailAddress1 as Email, MobilePhone"); if (!empty($filters['Name'])) { - $rawSql = new RawSql($qname); - $builder->like($rawSql, $filters['Name'], 'both'); + $builder->like($qname, $filters['Name'], 'both'); } if (!empty($filters['InternalPID'])) { diff --git a/app/Models/Specimen/ContainerDefModel.php b/app/Models/Specimen/ContainerDefModel.php index 03ba649..73a62b4 100644 --- a/app/Models/Specimen/ContainerDefModel.php +++ b/app/Models/Specimen/ContainerDefModel.php @@ -1,7 +1,7 @@