clqms-be/app/Models/PatVisit/PatVisitModel.php

109 lines
3.5 KiB
PHP
Raw Normal View History

2025-09-26 13:08:30 +07:00
<?php
2025-10-16 12:55:55 +07:00
namespace App\Models\PatVisit;
2025-10-03 10:28:59 +07:00
use App\Models\CounterModel;
2025-10-16 12:55:55 +07:00
use App\Models\BaseModel;
2025-09-26 13:08:30 +07:00
2025-10-14 18:53:06 +07:00
class PatVisitModel extends BaseModel {
protected $table = 'patvisit';
protected $primaryKey = 'InternalPVID';
2025-10-16 12:55:55 +07:00
protected $allowedFields = ['PVID', 'InternalPID', 'EpisodeID', 'CreateDate', 'EndDate', 'ArchivedDate', 'DelDate'];
2025-10-13 11:08:26 +07:00
2025-10-13 15:09:55 +07:00
protected $useTimestamps = true;
2025-10-13 12:28:09 +07:00
protected $createdField = 'CreateDate';
protected $updatedField = '';
protected $useSoftDeletes = true;
protected $deletedField = 'EndDate';
2025-10-16 13:44:22 +07:00
protected $visnum_prefix = "DV";
2025-09-26 13:08:30 +07:00
public function show($PVID) {
2025-09-26 15:03:11 +07:00
$rows = $this->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left')
->join('patvisitadt pva', 'pva.InternalPVID=patvisit.InternalPVID', 'left')
->where('patvisit.PVID',$PVID)->findAll();
2025-09-26 13:08:30 +07:00
return $rows;
}
2025-09-26 15:03:11 +07:00
public function showByPatient($InternalPID) {
2025-10-21 09:55:28 +07:00
$rows = $this
2025-10-21 10:27:31 +07:00
->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left')
->join('patvisitadt pva', 'pd.InternalPVID=pva.InternalPVID', 'left')
->join('location l', 'l.LocationID=pva.LocationID', 'left')
->where('patvisit.InternalPID',$InternalPID)->findAll();
2025-09-26 15:03:11 +07:00
return $rows;
2025-09-26 13:08:30 +07:00
}
2025-10-03 10:28:59 +07:00
public function createPatVisit($input) {
2025-10-16 12:55:55 +07:00
$db = \Config\Database::connect();
2025-10-03 10:28:59 +07:00
try{
2025-10-16 12:55:55 +07:00
$db->transStart();
2025-10-09 14:49:13 +07:00
if (!isset($input['PVID']) || $input['PVID']=='') {
2025-10-03 10:28:59 +07:00
$counter = new CounterModel();
2025-10-13 12:28:09 +07:00
$input['PVID'] = $this->visnum_prefix .$counter->use(2);
2025-10-03 10:28:59 +07:00
}
2025-10-16 12:55:55 +07:00
$InternalPVID = $this->insert($input, true);
2025-10-16 13:44:22 +07:00
2025-10-03 10:28:59 +07:00
if(!empty($input['PatDiag'])) {
$input['PatDiag']['InternalPVID'] = $InternalPVID;
2025-10-16 12:55:55 +07:00
$db->table('patdiag')->insert($input['PatDiag']);
2025-10-03 10:28:59 +07:00
}
if(!empty($input['PatVisitADT'])) {
$input['PatVisitADT']['InternalPVID'] = $InternalPVID;
2025-10-16 12:55:55 +07:00
$db->table('patvisitadt')->insert($input['PatVisitADT']);
2025-10-03 10:28:59 +07:00
}
2025-10-16 13:44:22 +07:00
2025-10-16 12:55:55 +07:00
$db->transComplete();
2025-10-16 13:44:22 +07:00
$data = [ "PVID"=>$input['PVID'], "InternalPVID"=>$InternalPVID ];
2025-10-13 13:59:52 +07:00
return $data;
2025-10-03 10:28:59 +07:00
} catch (\Exception $e) {
2025-10-16 12:55:55 +07:00
$db->transRollback();
2025-10-03 10:28:59 +07:00
throw $e;
}
}
public function updatePatVisit($input) {
$InternalPVID = $input['InternalPVID'];
try{
$this->db->transStart();
$this->where('InternalPVID',$InternalPVID)->set($input)->update();
// patdiag
$exist = $this->db->table('patdiag')->where('InternalPVID',$InternalPVID)->get()->getRow();
if($exist) {
if(!empty($input['PatDiag'])) {
$input['PatDiag']['InternalPVID'] = $InternalPVID;
$this->db->table('patdiag')->where('InternalPVID',$InternalPVID)->set($input['PatDiag'])->update();
} else { $this->db->table('patdiag')->where('InternalPVID',$InternalPVID)->delete(); }
} else {
if(!empty($input['PatDiag'])) {
$input['PatDiag']['InternalPVID'] = $InternalPVID;
$this->db->table('patdiag')->insert($input['PatDiag']);
}
}
// patvisitadt
$exist = $this->db->table('patvisitadt')->where('InternalPVID',$InternalPVID)->get()->getRow();
if($exist) {
if(!empty($input['PatVisitADT'])) {
$input['PatVisitADT']['InternalPVID'] = $InternalPVID;
$this->db->table('patvisitadt')->where('InternalPVID',$InternalPVID)->set($input['PatVisitADT'])->update();
} else { $this->db->table('patvisitadt')->where('InternalPVID',$InternalPVID)->delete(); }
} else {
if(!empty($input['PatVisitADT'])) {
$input['PatVisitADT']['InternalPVID'] = $InternalPVID;
$this->db->table('patvisitadt')->insert($input['PatVisitADT']);
}
}
$this->db->transComplete();
return $input['PVID'];
} catch (\Exception $e) {
$this->db->transRollback();
throw $e;
}
}
2025-09-26 13:08:30 +07:00
}