clqms-be/app/Models/PatVisitModel.php
2025-10-13 11:08:26 +07:00

132 lines
4.1 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
use App\Models\CounterModel;
class PatVisitModel extends Model {
protected $table = 'patvisit';
protected $primaryKey = 'InternalPVID';
protected $allowedFields = ['PVID', 'InternalPID', 'EpisodeID', 'CreateDate', 'EndDate'];
protected $db;
protected $visnum_prefix;
protected $useTimestamps = true;
protected $createdField = 'CreateDate';
protected $beforeInsert = ['normalizeDatesToUTC'];
protected $beforeUpdate = ['normalizeDatesToUTC'];
protected $afterFind = ['convertDatesToUTCISO'];
protected $afterInsert = ['convertDatesToUTCISO'];
protected $afterUpdate = ['convertDatesToUTCISO'];
public function __construct() {
$this->db = \Config\Database::connect();
$this->visnum_prefix = "DV";
}
public function show($PVID) {
$rows = $this->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left')
->join('patvisitadt pva', 'pva.InternalPVID=patvisit.InternalPVID', 'left')
->where('patvisit.PVID',$PVID)->get()->getResultArray();
return $rows;
}
public function showByPatient($InternalPID) {
$rows = $this->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)->get()->getResultArray();
return $rows;
}
public function createPatVisit($input) {
try{
$input = $this->transformPatVisit($input);
if (!isset($input['PVID']) || $input['PVID']=='') {
$counter = new CounterModel();
$input['PVID'] = $this->visnum_prefix .$counter->use(2);
}
$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;
}
}
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;
}
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;
}
}
}