devsofttech_hms_ci4/scratch/enqueue_func.txt

215 lines
20 KiB
Plaintext


> private function enqueueIpdDischargeFhirSync(int $ipdId, int $patientId, array $ipdRow, array $patientRow): void
{
if ($ipdId <= 0 || $patientId <= 0) {
return;
}
$hfrId = trim($this->getHospitalSettingValue('ABDM_HFR_ID'));
if ($hfrId === '') {
return;
}
$lastName = trim((string) ($patientRow['p_lname'] ?? ''));
if (! $this->isMeaningfulDischargeValue($lastName)) {
$lastName = '';
}
$patientName = trim(trim((string) ($patientRow['p_fname'] ?? '')) . ' ' . $lastName);
if ($patientName === '') {
$patientName = trim((string) ($ipdRow['P_name'] ?? ''));
}
$abhaIdRaw = '';
foreach (['abha_id', 'abha_no', 'abha'] as $field) {
$value = trim((string) ($patientRow[$field] ?? ''));
if ($value !== '') {
$abhaIdRaw = $value;
break;
}
}
if ($abhaIdRaw === '') {
$abhaIdRaw = trim((string) ($patientRow['abha_address'] ?? ''));
}
$abhaDigits = preg_replace('/\D/', '', $abhaIdRaw);
$abhaDigits = is_string($abhaDigits) ? $abhaDigits : '';
$abhaAddress = trim((string) ($patientRow['abha_address'] ?? ''));
if ($abhaAddress === '' && strpos($abhaIdRaw, '@') !== false) {
$abhaAddress = $abhaIdRaw;
}
$admissionRaw = trim((string) ($ipdRow['register_date'] ?? ''));
$dischargeRaw = trim((string) ($ipdRow['discharge_date'] ?? ''));
$admissionIso = $this->toIsoDateTimeOrNow($admissionRaw);
$dischargeIso = $this->toIsoDateTimeOrNow($dischargeRaw);
$visitDate = $dischargeRaw !== ''
? date('Y-m-d', strtotime($dischargeRaw))
: ($admissionRaw !== '' ? date('Y-m-d', strtotime($admissionRaw)) : date('Y-m-d'));
$diagnosisRows = $this->byIpdRows('ipd_discharge_diagnosis', ['comp_report'], 'id ASC', $ipdId);
$conditionRows = [];
foreach ($diagnosisRows as $row) {
$text = trim((string) ($row['comp_report'] ?? ''));
if ($text !== '') {
$conditionRows[] = ['text' => $text, 'code' => ''];
}
}
$chiefComplaintsList = [];
if ($this->tableHasColumns('ipd_discharge_complaint', ['ipd_id', 'comp_report'])) {
$complaintRows = $this->byIpdRows('ipd_discharge_complaint', ['comp_report', 'comp_remark'], 'id ASC',
$ipdId);
foreach ($complaintRows as $row) {
$term = trim((string) ($row['comp_report'] ?? ''));
$duration = trim((string) ($row['comp_remark'] ?? ''));
if ($term !== '') {
$text = $term . ($duration !== '' ? (' - ' . $duration) : '');
$chiefComplaintsList[] = ['text' => $text, 'code' => ''];
}
}
}
$complaintRemarkText = '';
if ($this->tableHasColumns('ipd_discharge_complaint_remark', ['ipd_id', 'comp_remark'])) {
$complaintRemarkRow = $this->firstRowByIpd('ipd_discharge_complaint_remark', $ipdId);
$complaintRemarkText = trim((string) ($complaintRemarkRow['comp_remark'] ?? ''));
}
if (empty($chiefComplaintsList) && $complaintRemarkText !== '') {
$chiefComplaintsList[] = ['text' => $complaintRemarkText, 'code' => ''];
}
$procedureRows = [];
foreach ($this->byIpdRows('ipd_discharge_surgery', ['surgery_name', 'surgery_date'], 'id ASC', $ipdId) as
$row) {
$text = trim((string) ($row['surgery_name'] ?? ''));
if ($text !== '') {
$procedureRows[] = [
'text' => $text,
'code' => '',
'performed_at' => $this->toIsoDateTimeOrNow(trim((string) ($row['surgery_date'] ?? ''))),
];
}
}
foreach ($this->byIpdRows('ipd_discharge_procedure', ['procedure_name', 'procedure_date'], 'id ASC', $ipdId)
as $row) {
$text = trim((string) ($row['procedure_name'] ?? ''));
if ($text !== '') {
$procedureRows[] = [
'text' => $text,
'code' => '',
'performed_at' => $this->toIsoDateTimeOrNow(trim((string) ($row['procedure_date'] ?? ''))),
];
}
}
$medicationRows = [];
$legacyMeds = $this->byIpdRows('ipd_discharge_prescrption_prescribed', ['med_name', 'dosage', 'dosage_when',
'dosage_freq', 'no_of_days', 'remark'], 'id ASC', $ipdId);
if (empty($legacyMeds)) {
$legacyMeds = $this->byIpdRows('ipd_discharge_prescription_prescribed', ['med_name', 'dosage',
'dosage_when', 'dosage_freq', 'no_of_days', 'remark'], 'id ASC', $ipdId);
}
foreach ($legacyMeds as $row) {
$name = trim((string) ($row['med_name'] ?? ''));
if (! $this->isMeaningfulDischargeValue($name)) {
continue;
}
$dose = $this->formatDischargeMedDirections(
$row['dosage'] ?? '',
$row['dosage_when'] ?? '',
$row['dosage_freq'] ?? '',
(string) ($row['no_of_days'] ?? ''),
(string) ($row['remark'] ?? '')
);
$medicationRows[] = ['name' => $name, 'dosage' => $dose];
}
if (empty($medicationRows)) {
foreach ($this->byIpdRows('ipd_discharge_drug', ['drug_name', 'drug_dose', 'drug_day'], 'id ASC',
$ipdId) as $row) {
$name = trim((string) ($row['drug_name'] ?? ''));
if (! $this->isMeaningfulDischargeValue($name)) {
continue;
}
$dose = trim(implode(' ', array_filter([
(string) ($row['drug_dose'] ?? ''),
(string) ($row['drug_day'] ?? ''),
], fn ($v) => $this->isMeaningfulDischargeValue((string) $v))));
$medicationRows[] = ['name' => $name, 'dosage' => $dose];
}
}
$observationRows = [];
if ($this->tableHasColumns('ipd_discharge_1_b', ['ipd_d_id'])) {
$cols = $this->db->getFieldNames('ipd_discharge_1_b') ?? [];
$sel = in_array('short_head', $cols, true) ? 'short_head,rdata' : '*';
foreach ($this->db->table('ipd_discharge_1_b')->select($sel)->where('ipd_d_id',
$ipdId)->get()->getResultArray() as $row) {
$label = trim((string) ($row['short_head'] ?? ''));
$value = trim((string) ($row['rdata'] ?? ''));
if ($label === '' || ! $this->isMeaningfulDischargeValue($value)) {
continue;
}
$observationRows[] = [
'text' => $label,
'value' => $value,
'category' => 'Condition on Admission Time',
'category_code' => 'vital-signs',
'effective_at' => $admissionIso,
];
}
}
if ($this->tableHasColumns('ipd_discharge_1_b_final', ['ipd_d_id'])) {
$cols = $this->db->getFieldNames('ipd_discharge_1_b_final') ?? [];
$sel = in_array('short_head', $cols, true) ? 'short_head,rdata' : '*';
foreach ($this->db->table('ipd_discharge_1_b_final')->select($sel)->where('ipd_d_id',
$ipdId)->get()->getResultArray() as $row) {
$label = trim((string) ($row['short_head'] ?? ''));
$value = trim((string) ($row['rdata'] ?? ''));
if ($label === '' || ! $this->isMeaningfulDischargeValue($value)) {
continue;
}
$observationRows[] = [
'text' => $label,
'value' => $value,
'category' => 'Condition on Discharge Time',
'category_code' => 'vital-signs',
'effective_at' => $dischargeIso,
];
}
}
$investigationRows = [];
if ($this->tableHasColumns('ipd_discharge_1_d', ['ipd_d_id'])) {
$cols = $this->db->getFieldNames('ipd_discharge_1_d') ?? [];
$sel = in_array('short_head', $cols, true) ? 'short_head,rdata' : '*';
foreach ($this->db->table('ipd_discharge_1_d')->select($sel)->where('ipd_d_id',
$ipdId)->get()->getResultArray() as $row) {
$label = trim((string) ($row['short_head'] ?? ''));
$value = trim((string) ($row['rdata'] ?? ''));
if ($label === '' || ! $this->isMeaningfulDischargeValue($value)) {
continue;
}
$investigationRows[] = [
'text' => $label . ': ' . $value,
'authored_on' => $admissionIso,
];
}
}
if ($this->tableHasColumns('ipd_discharge_1_e', ['ipd_d_id'])) {
$cols = $this->db->getFieldNames('ipd_discharge_1_e') ?? [];
$sel = in_array('short_head', $cols, true) ? 'short_head,rdata' : '*';
foreach ($this->db->table('ipd_discharge_1_e')->select($sel)->where('ipd_d_id',
$ipdId)->get()->getResultArray() as $row) {
$label = trim((string) ($row['short_head'] ?? ''));
$value = trim((string) ($row['rdata'] ?? ''));
if ($label === '' || ! $this->isMeaningfulDischargeValue($value)) {
continue;
}
$investigationRows[] = [
'text' => $label . ': ' . $value,
'authored_on' => $admissionIso,
];