2025-09-26 13:08:30 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
2025-10-03 10:28:59 +07:00
|
|
|
use App\Models\CounterModel;
|
2025-09-26 13:08:30 +07:00
|
|
|
|
|
|
|
|
class PatVisitModel extends Model {
|
2025-10-09 13:04:05 +07:00
|
|
|
protected $table = 'patvisit';
|
|
|
|
|
protected $primaryKey = 'InternalPVID';
|
|
|
|
|
protected $allowedFields = ['PVID', 'InternalPID', 'EpisodeID', 'EndDate'];
|
2025-10-03 10:28:59 +07:00
|
|
|
protected $db;
|
|
|
|
|
protected $visnum_prefix;
|
2025-09-26 13:08:30 +07:00
|
|
|
|
2025-10-03 10:28:59 +07:00
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
$this->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)->get()->getResultArray();
|
2025-09-26 13:08:30 +07:00
|
|
|
return $rows;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-26 15:03:11 +07:00
|
|
|
public function showByPatient($InternalPID) {
|
|
|
|
|
$rows = $this->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left')
|
|
|
|
|
->join('patvisitadt pva', 'pd.InternalPVID=pva.InternalPVID', 'left')
|
2025-10-08 16:26:41 +07:00
|
|
|
->join('location l', 'l.LocationID=pva.LocationID', 'left')
|
2025-09-26 15:03:11 +07:00
|
|
|
->where('patvisit.InternalPID',$InternalPID)->get()->getResultArray();
|
|
|
|
|
return $rows;
|
2025-09-26 13:08:30 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-03 10:28:59 +07:00
|
|
|
public function createPatVisit($input) {
|
|
|
|
|
try{
|
2025-10-09 14:49:13 +07:00
|
|
|
|
|
|
|
|
$input = $this->transformPatVisit($input);
|
|
|
|
|
|
|
|
|
|
if (!isset($input['PVID']) || $input['PVID']=='') {
|
2025-10-03 10:28:59 +07:00
|
|
|
$counter = new CounterModel();
|
2025-10-09 14:49:13 +07:00
|
|
|
$input['PVID'] = $this->visnum_prefix .$counter->use(2);
|
2025-10-03 10:28:59 +07:00
|
|
|
}
|
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
|
|
$this->insert($input);
|
|
|
|
|
$InternalPVID = $this->getInsertID();
|
|
|
|
|
|
|
|
|
|
if(!empty($input['PatDiag'])) {
|
|
|
|
|
$input['PatDiag']['InternalPVID'] = $InternalPVID;
|
|
|
|
|
$this->db->table('patdiag')->insert($input['PatDiag']);
|
|
|
|
|
}
|
|
|
|
|
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-10-09 14:49:13 +07:00
|
|
|
private function transformPatVisit(array $input): array {
|
|
|
|
|
|
|
|
|
|
// Ubah jadi null saat string kosong
|
|
|
|
|
$fields = ['LocationID','AttDoc', 'RefDoc', 'AdmDoc', 'CnsDoc'];
|
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
|
if (isset($input['PatVisitADT'][$field]) && $input['PatVisitADT'][$field] === '') {
|
|
|
|
|
$input['PatVisitADT'][$field] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $input;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 10:28:59 +07:00
|
|
|
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
|
|
|
}
|