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

144 lines
5.0 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;
use App\Models\BaseModel;
use App\Models\CounterModel;
use App\Models\PatVisit\PatDiagModel;
use App\Models\PatVisit\PatVisitADTModel;
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) {
$row = $this->select("*, patvisit.InternalPID, patvisit.CreateDate as PVCreateDate, patdiag.CreateDate as PDCreateDate, patvisitadt.CreateDate as PVACreateDate")
2025-10-23 11:05:20 +07:00
->join('patdiag', 'patdiag.InternalPVID=patvisit.InternalPVID and patdiag.DelDate is null', 'left')
->join('patvisitadt', 'patvisitadt.InternalPVID=patvisit.InternalPVID', 'left')
->where('patvisit.PVID',$PVID)->first();
return $row;
2025-09-26 13:08:30 +07:00
}
2025-09-26 15:03:11 +07:00
public function showByPatient($InternalPID) {
2025-11-12 16:02:31 +07:00
$rows = $this->select("*, patvisit.InternalPID, patvisit.CreateDate as PVCreateDate, patdiag.CreateDate as PDCreateDate, patvisitadt.CreateDate as PVACreateDate")
2025-10-23 11:05:20 +07:00
->join('patdiag', 'patdiag.InternalPVID=patvisit.InternalPVID and patdiag.DelDate is null', 'left')
2025-10-27 11:13:12 +07:00
->join('(SELECT a1.*
FROM patvisitadt a1
INNER JOIN (
SELECT InternalPVID, MAX(PVADTID) AS MaxID
FROM patvisitadt
GROUP BY InternalPVID
) a2 ON a1.InternalPVID = a2.InternalPVID AND a1.PVADTID = a2.MaxID
) AS patvisitadt',
'patvisitadt.InternalPVID = patvisit.InternalPVID',
'left')
2025-10-23 09:29:20 +07:00
->join('location', 'location.LocationID=patvisitadt.LocationID', 'left')
2025-10-21 10:27:31 +07:00
->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-23 11:05:20 +07:00
$db = $this->db;
$modelPD = new PatDiagModel();
$modelPVA = new PatVisitADTModel();
2025-10-23 11:05:20 +07:00
$db->transBegin();
2025-10-03 10:28:59 +07:00
try{
2025-10-23 11:05:20 +07:00
2025-10-09 14:49:13 +07:00
if (!isset($input['PVID']) || $input['PVID']=='') {
$modelCounter = new CounterModel();
$input['PVID'] = $this->visnum_prefix .$modelCounter->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-23 11:05:20 +07:00
if($InternalPVID === false) { throw new \Exception("Failed to insert main PatVisit record."); }
if( !empty($input['PatDiag']) && ( !empty($input['PatDiag']['DiagCode']) || !empty($input['PatDiag']['Diagnosis']) ) ) {
2025-10-03 10:28:59 +07:00
$input['PatDiag']['InternalPVID'] = $InternalPVID;
2025-10-23 11:05:20 +07:00
$tmp = $modelPD->insert($input['PatDiag']);
if ($tmp === false) { throw new \Exception("Failed to insert PatDiag record."); }
2025-10-03 10:28:59 +07:00
}
2025-10-23 11:05:20 +07:00
if( !empty($input['PatVisitADT']) ) {
2025-10-03 10:28:59 +07:00
$input['PatVisitADT']['InternalPVID'] = $InternalPVID;
2025-10-23 11:05:20 +07:00
$tmp = $modelPVA->insert($input['PatVisitADT']);
if ($tmp === false) {
throw new \Exception("Failed to insert PatVisitADT record.");
}
2025-10-03 10:28:59 +07:00
}
2025-10-16 13:44:22 +07:00
2025-10-23 11:05:20 +07:00
if ($db->transStatus() === FALSE) {
$db->transRollback();
return false;
} else {
$db->transCommit();
$data = [ "PVID" => $input['PVID'], "InternalPVID" => $InternalPVID ];
return $data;
}
} catch (\Exception $e) {
$db->transRollback();
2025-10-03 10:28:59 +07:00
throw $e;
2025-10-23 11:05:20 +07:00
}
2025-10-03 10:28:59 +07:00
}
public function updatePatVisit($input) {
$InternalPVID = $input['InternalPVID'];
2025-10-23 11:05:20 +07:00
$modelPD = new PatDiagModel();
$modelPVA = new PatVisitADTModel();
$db = $this->db;
$db->transBegin();
2025-10-03 10:28:59 +07:00
try{
$this->where('InternalPVID',$InternalPVID)->set($input)->update();
// patdiag
2025-10-23 11:05:20 +07:00
$exist = $modelPD->where('InternalPVID',$InternalPVID)->find();
2025-10-03 10:28:59 +07:00
if($exist) {
2025-10-23 11:05:20 +07:00
if( !empty($input['PatDiag']) && ( !empty($input['PatDiag']['DiagCode']) || !empty($input['PatDiag']['Diagnosis']) ) ) {
$tmp = $modelPD->where('InternalPVID',$InternalPVID)->set($input['PatDiag'])->update();
} else { $tmp = $modelPD->delete($InternalPVID); }
2025-10-03 10:28:59 +07:00
} else {
2025-10-23 11:05:20 +07:00
if( !empty($input['PatDiag']) && ( !empty($input['PatDiag']['DiagCode']) || !empty($input['PatDiag']['Diagnosis']) ) ) {
2025-10-03 10:28:59 +07:00
$input['PatDiag']['InternalPVID'] = $InternalPVID;
2025-10-23 11:05:20 +07:00
$tmp = $modelPD->insert($input['PatDiag']);
}
}
2025-11-13 13:49:24 +07:00
if (isset($tmp) && $tmp === false) {
2025-10-23 11:05:20 +07:00
$error = $db->error();
throw new \Exception("Failed to update PatDiag record. ". $error['message']);
2025-10-03 10:28:59 +07:00
}
2025-10-27 10:37:44 +07:00
if(!empty($input['PatVisitADT'])) {
$adtList = $input['PatVisitADT'];
usort($adtList, fn($a, $b) => $a['sequence'] <=> $b['sequence']);
foreach ($adtList as $adt) {
$adt['InternalPVID'] = $InternalPVID;
$tmp = $modelPVA->insert($adt);
if ($tmp === false) {
$error = $db->error();
throw new \Exception("Failed to update PatVisitADT record. ". $error['message']);
}
}
2025-10-03 10:28:59 +07:00
}
2025-10-23 11:05:20 +07:00
if ($db->transStatus() === FALSE) {
$db->transRollback();
return false;
} else {
$db->transCommit();
$data = [ "PVID" => $input['PVID'], "InternalPVID" => $InternalPVID ];
return $data;
}
2025-10-03 10:28:59 +07:00
} catch (\Exception $e) {
$this->db->transRollback();
throw $e;
}
}
2025-09-26 13:08:30 +07:00
}