fix occupation, create occupation model
This commit is contained in:
parent
7b093aefb3
commit
a6c5538b4c
@ -3,7 +3,6 @@ namespace App\Controllers;
|
||||
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
use CodeIgniter\Controller;
|
||||
use CodeIgniter\Database\RawSql;
|
||||
|
||||
class Location extends Controller {
|
||||
use ResponseTrait;
|
||||
|
||||
@ -3,11 +3,14 @@ namespace App\Controllers;
|
||||
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
use CodeIgniter\Controller;
|
||||
use CodeIgniter\Database\RawSql;
|
||||
use App\Models\OccupationModel;
|
||||
|
||||
class Occupation extends Controller {
|
||||
use ResponseTrait;
|
||||
|
||||
protected $db;
|
||||
protected $rulesOccupation;
|
||||
|
||||
public function __construct() {
|
||||
$this->db = \Config\Database::connect();
|
||||
$this->rulesOccupation = [
|
||||
@ -17,7 +20,8 @@ class Occupation extends Controller {
|
||||
}
|
||||
|
||||
public function index() {
|
||||
$rows = $this->db->table('occupation')->select("*")->get()->getResultArray();
|
||||
$model = new OccupationModel();
|
||||
$rows = $model->get()->getResultArray();
|
||||
|
||||
if (empty($rows)) {
|
||||
return $this->respond([
|
||||
@ -35,7 +39,9 @@ class Occupation extends Controller {
|
||||
}
|
||||
|
||||
public function show($OccupationID = null) {
|
||||
$rows=$this->db->table('occupation')->select("*")->where('occupationID', (int) $OccupationID)->get()->getResultArray();
|
||||
$model = new OccupationModel();
|
||||
$rows = $model->where('occupationID', (int) $OccupationID)->get()->getResultArray();
|
||||
//$rows=$this->db->table('occupation')->select("*")->where('occupationID', (int) $OccupationID)->get()->getResultArray();
|
||||
|
||||
if (empty($rows)) {
|
||||
return $this->respond([
|
||||
|
||||
@ -35,10 +35,11 @@ class DummySeeder extends Seeder {
|
||||
];
|
||||
$this->db->table('contactdetail')->insertBatch($data);
|
||||
$data = [
|
||||
['OccupationID'=>1, 'AbbTex'=>'OC0001', 'FullText'=>'Medical Doctor', 'Description'=>'Diagnoses and treats, injuries and illnesses' ],
|
||||
['OccupationID'=>2, 'AbbTex'=>'OC0002', 'FullText'=>'Trainee Medical Technician', 'Description'=>'Performing basic laboratory task' ],
|
||||
['OccupationID'=>3, 'AbbTex'=>'OC0003', 'FullText'=>'Medical Laboratory Technician', 'Description'=>'Perform routine laboratory tests' ]
|
||||
['OccupationID'=>1, 'AbbTex'=>'OC001', 'FullText'=>'Medical Doctor', 'Description'=>'Diagnoses and treats, injuries and illnesses' ],
|
||||
['OccupationID'=>2, 'AbbTex'=>'OC002', 'FullText'=>'Trainee Medical Technician', 'Description'=>'Performing basic laboratory task' ],
|
||||
['OccupationID'=>3, 'AbbTex'=>'OC003', 'FullText'=>'Medical Laboratory Technician', 'Description'=>'Perform routine laboratory tests' ]
|
||||
];
|
||||
$this->db->table('occupation')->insertBatch($data);
|
||||
|
||||
// patient
|
||||
$data = [
|
||||
|
||||
@ -4,110 +4,8 @@ namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class ContactModel extends Model {
|
||||
class OccupationModel extends Model {
|
||||
protected $table = 'occupation';
|
||||
protected $primaryKey = 'OccupationID';
|
||||
protected $allowedFields = ['AbbTex', 'FullText', 'Description'];
|
||||
|
||||
public function getContactsWithDetail() {
|
||||
$rows = $this->select("contact.ContactID, cd.SiteID, cd.ContactCode, NameFirst, NameLast, Specialty")
|
||||
->join("contactdetail cd", "contact.ContactID=cd.ContactID", "left")
|
||||
->join("occupation o","cd.OccupationID=o.OccupationID", "left")
|
||||
->get()->getResultArray();
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function getContacts($ContactName, $Specialty) {
|
||||
$sql = $this->select("ContactID, NameFirst, NameLast, Title, Initial, Specialty");
|
||||
if($ContactName !='') { $sql->like('NameFirst',$ContactName,'both')->orLike('NameLast',$ContactName,'both'); }
|
||||
if($Specialty != '') { $sql->like('Specialty',$Specialty); }
|
||||
$rows = $sql->get()->getResultArray();
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function getContactWithDetail($ContactID) {
|
||||
$rows = $this->where('contact.ContactID', $ContactID)->join('contactdetail cd', 'contact.ContactID=cd.ContactID','left')->get()->getResultArray();
|
||||
$contact = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
if(empty($contact['NameFirst'])) {
|
||||
$contact = [
|
||||
'ContactID' => $ContactID,
|
||||
'NameFirst' => $row['NameFirst'] ?? null,
|
||||
'NameLast' => $row['NameLast'] ?? null,
|
||||
'Title' => $row['Title'] ?? null,
|
||||
'Initial' => $row['Initial'] ?? null,
|
||||
'Birthdate' => $row['Birthdate'] ?? null,
|
||||
'EmailAddress1' => $row['EmailAddress1'] ?? null,
|
||||
'EmailAddress2' => $row['EmailAddress2'] ?? null,
|
||||
'Phone' => $row['Phone'] ?? null,
|
||||
'MobilePhone1' => $row['MobilePhone1'] ?? null,
|
||||
'MobilePhone2' => $row['MobilePhone2'] ?? null,
|
||||
'Specialty' => $row['Specialty'] ?? null,
|
||||
'SubSpecialty' => $row['SubSpecialty'] ?? null,
|
||||
'Details' => []
|
||||
];
|
||||
}
|
||||
if (!empty($row['ContactDetID'])) {
|
||||
$contact['Details'][] = [
|
||||
'SiteID' => $row['SiteID'] ?? null,
|
||||
'ContactDetID' => $row['ContactDetID'],
|
||||
'ContactCode' => $row['ContactCode'] ?? null,
|
||||
'ContactEmail' => $row['ContactEmail'] ?? null,
|
||||
'OccupationID' => $row['OccupationID'] ?? null,
|
||||
'JobTitle' => $row['JobTitle'] ?? null,
|
||||
'Department' => $row['Department'] ?? null,
|
||||
'ContactStartDate' => $row['ContactStartDate'] ?? null,
|
||||
'ContactEndDate' => $row['ContactEndDate'] ?? null
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $contact;
|
||||
}
|
||||
|
||||
public function saveWithDetails(array $data): array {
|
||||
$db = \Config\Database::connect();
|
||||
$db->transStart();
|
||||
|
||||
try {
|
||||
if (!empty($data['ContactID'])) {
|
||||
$contactId = $data['ContactID'];
|
||||
$this->update($contactId, $data);
|
||||
} else {
|
||||
$contactId = $this->insert($data, true);
|
||||
}
|
||||
|
||||
if (!$contactId) {
|
||||
throw new \RuntimeException('Failed to save contact');
|
||||
}
|
||||
|
||||
if (!empty($data['Details'])) {
|
||||
$detailModel = new \App\Models\ContactDetailModel();
|
||||
$result = $detailModel->syncDetails($contactId, $data['Details']);
|
||||
|
||||
if ($result['status'] !== 'success') {
|
||||
throw new \RuntimeException('SyncDetails failed: ' . $result['message']);
|
||||
}
|
||||
}
|
||||
|
||||
$db->transComplete();
|
||||
|
||||
return [
|
||||
'status' => 'success',
|
||||
'ContactID' => $contactId,
|
||||
];
|
||||
} catch (\Throwable $e) {
|
||||
$db->transRollback();
|
||||
|
||||
log_message('error', 'saveWithDetails error: ' . $e->getMessage());
|
||||
|
||||
return [
|
||||
'status' => 'error',
|
||||
'message' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user