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

116 lines
4.0 KiB
PHP

<?php
namespace App\Models\PatVisit;
use App\Models\BaseModel;
use App\Models\CounterModel;
use App\Models\PatVisit\PatDiagModel;
use App\Models\PatVisit\PatVisitADTModel;
class PatVisitModel extends BaseModel {
protected $table = 'patvisit';
protected $primaryKey = 'InternalPVID';
protected $allowedFields = ['PVID', 'InternalPID', 'EpisodeID', 'CreateDate', 'EndDate', 'ArchivedDate', 'DelDate'];
protected $useTimestamps = true;
protected $createdField = 'CreateDate';
protected $updatedField = '';
protected $useSoftDeletes = true;
protected $deletedField = 'EndDate';
protected $visnum_prefix = "DV";
public function show($PVID) {
$rows = $this->select("*, patvisit.CreateDate as PVCreateDate, patdiag.CreateDate as PDCreateDate, patvisitadt.CreateDate as PVACreateDate")
->join('patdiag', 'patdiag.InternalPVID=patvisit.InternalPVID', 'left')
->join('patvisitadt', 'patvisitadt.InternalPVID=patvisit.InternalPVID', 'left')
->where('patvisit.PVID',$PVID)->findAll();
return $rows;
}
public function showByPatient($InternalPID) {
$rows = $this->select("*, patvisit.CreateDate as PVCreateDate, patdiag.CreateDate as PDCreateDate, patvisitadt.CreateDate as PVACreateDate")
->join('patdiag', 'patdiag.InternalPVID=patvisit.InternalPVID', 'left')
->join('patvisitadt', 'patvisitadt.InternalPVID=patvisit.InternalPVID', 'left')
->join('location', 'location.LocationID=patvisitadt.LocationID', 'left')
->where('patvisit.InternalPID',$InternalPID)->findAll();
return $rows;
}
public function createPatVisit($input) {
$db = \Config\Database::connect();
$modelPD = new PatDiagModel();
$modelPVA = new PatVisitADTModel();
try{
$db->transStart();
if (!isset($input['PVID']) || $input['PVID']=='') {
$modelCounter = new CounterModel();
$input['PVID'] = $this->visnum_prefix .$modelCounter->use(2);
}
$InternalPVID = $this->insert($input, true);
if(!empty($input['PatDiag'])) {
$input['PatDiag']['InternalPVID'] = $InternalPVID;
//$db->table('patdiag')->insert($input['PatDiag']);
$modelPD->insert($input['PatDiag']);
}
if(!empty($input['PatVisitADT'])) {
$input['PatVisitADT']['InternalPVID'] = $InternalPVID;
//$db->table('patvisitadt')->insert($input['PatVisitADT']);
$modelPVA->insert($input['PatVisitADT']);
}
$db->transComplete();
$data = [ "PVID"=>$input['PVID'], "InternalPVID"=>$InternalPVID ];
return $data;
} catch (\Exception $e) {
$db->transRollback();
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;
}
}
}