104 lines
3.8 KiB
PHP
104 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\MisMigrationPatient;
|
|
use App\Models\MedicalHistory;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class CurrentPatientService
|
|
{
|
|
const PLAN_STATUSES = [1];
|
|
|
|
const EMERGENCY_STATUSES = [2, 3];
|
|
|
|
public function getCurrentMedicalHistoryIds(string $type, int $branchId, DateRange $dateRange, bool $fillableAuto = false)
|
|
{
|
|
if (! $fillableAuto && $this->useMaterializedViews()) {
|
|
return MedicalHistory::query()
|
|
->when($type === 'plan', fn ($query) => $query->urgency(1))
|
|
->when($type === 'emergency', fn ($query) => $query->whereIn('urgency_id', self::EMERGENCY_STATUSES))
|
|
->whereHas('migrations', fn ($query) => $query
|
|
->where('stationar_branch_id', $branchId)
|
|
->current($dateRange))
|
|
->pluck('original_id')
|
|
->filter()
|
|
->map(fn ($id) => (int) $id)
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
if ($fillableAuto) {
|
|
return $this->getHistoricalCurrentMedicalHistoryIds($type, $branchId, $dateRange);
|
|
}
|
|
|
|
$typeIds = match ($type) {
|
|
'plan' => self::PLAN_STATUSES,
|
|
'emergency' => self::EMERGENCY_STATUSES,
|
|
default => null
|
|
};
|
|
|
|
return MisMigrationPatient::currentlyInTreatment($branchId)
|
|
->whereHas('medicalHistory', function ($q) use ($typeIds) {
|
|
$q->whereIn('rf_EmerSignID', $typeIds);
|
|
})
|
|
->pluck('rf_MedicalHistoryID')
|
|
->toArray();
|
|
}
|
|
|
|
public function getHistoricalCurrentMedicalHistoryIds(?string $type, int $branchId, DateRange $dateRange): array
|
|
{
|
|
$endAt = $dateRange->endSql();
|
|
|
|
$typeIds = match ($type) {
|
|
'plan' => self::PLAN_STATUSES,
|
|
'emergency' => self::EMERGENCY_STATUSES,
|
|
default => null
|
|
};
|
|
|
|
$lastMovementSubquery = DB::table('stt_migrationpatient as mp')
|
|
->selectRaw('
|
|
mp."rf_MedicalHistoryID",
|
|
mp."rf_StationarBranchID",
|
|
mp."DateIngoing",
|
|
mp."SystemDate",
|
|
mp."MigrationPatientID",
|
|
row_number() over (
|
|
partition by mp."rf_MedicalHistoryID"
|
|
order by mp."DateIngoing" desc, mp."SystemDate" desc, mp."MigrationPatientID" desc
|
|
) as rn
|
|
')
|
|
->where('mp.rf_MedicalHistoryID', '<>', 0)
|
|
->where('mp.DateIngoing', '<', $endAt);
|
|
|
|
return DB::query()
|
|
->fromSub($lastMovementSubquery, 'last_mp')
|
|
->leftJoin('stt_medicalhistory as mh', 'mh.MedicalHistoryID', '=', 'last_mp.rf_MedicalHistoryID')
|
|
->where('last_mp.rn', 1)
|
|
->where('last_mp.rf_StationarBranchID', $branchId)
|
|
->where(function ($q) use ($endAt) {
|
|
$q->whereNull('mh.DateExtract')
|
|
->orWhere('mh.DateExtract', '>=', $endAt)
|
|
->orWhereDate('mh.DateExtract', '1900-01-01')
|
|
->orWhereDate('mh.DateExtract', '2222-01-01');
|
|
})
|
|
->where(function ($q) use ($endAt) {
|
|
$q->whereNull('mh.DateDeath')
|
|
->orWhere('mh.DateDeath', '>=', $endAt)
|
|
->orWhereDate('mh.DateDeath', '1900-01-01')
|
|
->orWhereDate('mh.DateDeath', '2222-01-01');
|
|
})
|
|
->when($typeIds, function ($q) use ($typeIds) {
|
|
$q->whereIn('mh.rf_EmerSignID', $typeIds);
|
|
})
|
|
->pluck('last_mp.rf_MedicalHistoryID')
|
|
->toArray();
|
|
}
|
|
|
|
private function useMaterializedViews(): bool
|
|
{
|
|
return Schema::hasTable('mv_medicalhistory_summary') && Schema::hasTable('mv_migrationpatient_details');
|
|
}
|
|
}
|