refactor contact occupation counter location to model
This commit is contained in:
parent
3f656bfa87
commit
20350db5bd
@ -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');
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
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);
|
||||
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() {
|
||||
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);
|
||||
|
||||
try {
|
||||
$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() {
|
||||
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."
|
||||
]);
|
||||
|
||||
try {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
|
||||
$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() {
|
||||
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);
|
||||
|
||||
try {
|
||||
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() {
|
||||
try {
|
||||
$input = $this->request->getJSON(true);
|
||||
try {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
@ -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(),
|
||||
|
||||
22
app/Models/Location/LocationAddressModel.php
Normal file
22
app/Models/Location/LocationAddressModel.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace App\Models\Location;
|
||||
use App\Models\BaseModel;
|
||||
|
||||
class LocationAddressModel extends BaseModel {
|
||||
protected $table = 'locationaddress';
|
||||
protected $primaryKey = 'LocationID';
|
||||
protected $allowedFields = ['Street1', 'Street2', 'City', 'Province', 'PostCode', 'GeoLocationSystem', 'GeoLocationData', 'Phone', 'Mobile', 'Email', 'CreateDate', 'EndDate'];
|
||||
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'CreateDate';
|
||||
protected $updatedField = '';
|
||||
protected $useSoftDeletes = true;
|
||||
protected $deletedField = 'EndDate';
|
||||
|
||||
protected $beforeInsert = ['normalizeDatesToUTC'];
|
||||
protected $beforeUpdate = ['normalizeDatesToUTC'];
|
||||
protected $afterFind = ['convertDatesToUTCISO'];
|
||||
protected $afterInsert = ['convertDatesToUTCISO'];
|
||||
protected $afterUpdate = ['convertDatesToUTCISO'];
|
||||
|
||||
}
|
||||
86
app/Models/Location/LocationModel.php
Normal file
86
app/Models/Location/LocationModel.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
namespace App\Models\Location;
|
||||
use App\Models\BaseModel;
|
||||
|
||||
class LocationModel extends BaseModel {
|
||||
protected $table = 'location';
|
||||
protected $primaryKey = 'LocationID';
|
||||
protected $allowedFields = ['SiteID', 'LocCode', 'Parent', 'LocFull', 'Description', 'LocType', 'CreateDate', 'EndDate'];
|
||||
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'CreateDate';
|
||||
protected $updatedField = '';
|
||||
protected $useSoftDeletes = true;
|
||||
protected $deletedField = 'EndDate';
|
||||
|
||||
protected $beforeInsert = ['normalizeDatesToUTC'];
|
||||
protected $beforeUpdate = ['normalizeDatesToUTC'];
|
||||
protected $afterFind = ['convertDatesToUTCISO'];
|
||||
protected $afterInsert = ['convertDatesToUTCISO'];
|
||||
protected $afterUpdate = ['convertDatesToUTCISO'];
|
||||
|
||||
public function getLocations($LocCode, $LocName) {
|
||||
$sql = $this->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() ];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
namespace App\Models\Patient;
|
||||
|
||||
use CodeIgniter\Database\RawSql;
|
||||
use App\Models\BaseModel;
|
||||
|
||||
class PatientModel extends BaseModel {
|
||||
protected $table = 'patient';
|
||||
@ -23,8 +23,7 @@ class PatientModel extends BaseModel {
|
||||
$builder->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'])) {
|
||||
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace App\Models\Specimen;
|
||||
|
||||
use App\Models\BaseUtcModel;
|
||||
use App\Models\BaseModel;
|
||||
|
||||
class ContainerDefModel extends BaseModel {
|
||||
protected $table = 'containerdef';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user