forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatientIssuesService.php
195 lines (168 loc) · 7.27 KB
/
PatientIssuesService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* PatientIssuesService.php
* @package openemr
* @link http://www.open-emr.org
* @author Stephen Nielson <[email protected]>
* @copyright Copyright (c) 2021 Stephen Nielson <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Services\Search\FhirSearchWhereClauseBuilder;
use OpenEMR\Services\Search\TokenSearchField;
use OpenEMR\Validators\ProcessingResult;
class PatientIssuesService extends BaseService
{
const ISSUES_TABLE_NAME = "lists";
public function __construct()
{
parent::__construct(self::ISSUES_TABLE_NAME);
UuidRegistry::createMissingUuidsForTables([self::ISSUES_TABLE_NAME]);
}
public function getUuidFields(): array
{
return ['uuid'];
}
public function getOneById($issueId)
{
$results = $this->search(['id' => new TokenSearchField('id', [$issueId])]);
if (!empty($results->getData())) {
$data_results = $results->getData();
return array_pop($data_results);
}
return null;
}
public function getAllIssues()
{
$results = $this->search();
return $results->getData();
}
public function createIssue($issueRecord)
{
$this->validateIssueType($issueRecord['type']);
if (empty($issueRecord['pid'])) {
throw new \InvalidArgumentException("issue pid cannot be empty for create");
}
if (!empty($issueRecord['id'])) {
throw new \InvalidArgumentException("Cannot insert record with existing id");
}
$whiteListDict = $this->filterData($issueRecord);
$insert = $this->buildInsertColumns($whiteListDict, ['null_value' => null]);
$sql = "INSERT INTO lists SET " . $insert['set'];
$list_id = QueryUtils::sqlInsert($sql, $insert['bind']);
if ($issueRecord['type'] == "medication" && !empty($issueRecord['medication'])) {
$medication = $issueRecord['medication'] ?? [];
$medication['list_id'] = $list_id;
$medicationIssueService = new MedicationPatientIssueService();
$medicationIssueService->createIssue($medication);
}
}
public function updateIssue($issueRecord)
{
$this->validateIssueType($issueRecord['type']);
if (empty($issueRecord['id'])) {
throw new \InvalidArgumentException("issue id cannot be empty for update");
}
if (empty($issueRecord['pid'])) {
throw new \InvalidArgumentException("issue pid cannot be empty for update");
}
$whiteListDict = $this->filterData($issueRecord);
$update = $this->buildUpdateColumns($whiteListDict, ['null_value' => null]);
$values = $update['bind'];
$sql = "UPDATE lists SET " . $update['set'] . " WHERE id = ? AND pid = ? ";
$values[] = $issueRecord['id'];
$values[] = $issueRecord['pid'];
QueryUtils::sqlStatementThrowException($sql, $values);
// now do any specific type updates here...
// TODO: @adunsulag we should trigger a dispatch event here so people can do any post processing on the list insertion
$endDate = $issueRecord['enddate'] ?? null;
$title = $issueRecord['title'] ?? null;
if ($issueRecord['type'] == "medication") {
$medication = $issueRecord['medication'] ?? [];
if (!empty($medication)) {
$medicationIssueService = new MedicationPatientIssueService();
$medication['list_id'] = $issueRecord['id'];
$existingMedication = $medicationIssueService->getRecordByIssueListId($issueRecord['id']);
if (!empty($existingMedication)) {
foreach ($medication as $key => $value) {
$existingMedication[$key] = $value;
}
$medicationIssueService->updateIssue($existingMedication);
} else {
$medicationIssueService->createIssue($medication);
}
}
// sync the prescriptions
// TODO: is this prescription update even used anymore? Is this something for WENO or NewCrop or is this just legacy
if ($endDate != null && $title != null) {
QueryUtils::sqlStatementThrowException(
'UPDATE prescriptions SET '
. 'medication = 0 where patient_id = ? '
. " and upper(trim(drug)) = ? "
. ' and medication = 1',
array($issueRecord['pid'], strtoupper($title))
);
}
}
}
private function validateIssueType($type)
{
$value = QueryUtils::fetchSingleValue("select type FROM issue_types WHERE type = ? ", 'type', $type);
if (empty($value)) {
throw new \InvalidArgumentException("Invalid issue type sent");
}
}
public function search($search, $isAndCondition = true)
{
$sql = "SELECT lists.*
,medications.lists_medication_id
,medications.list_id
,medications.usage_category
,medications.usage_category_title
,medications.drug_dosage_instructions
,medications.request_intent
,medications.request_intent_title
FROM lists
LEFT JOIN (
SELECT
id AS lists_medication_id
,list_id
,usage_category
,usage_category_title
,drug_dosage_instructions
,request_intent
,request_intent_title
FROM lists_medication
) medications ON medications.list_id = lists.id";
$whereClause = FhirSearchWhereClauseBuilder::build($search, $isAndCondition);
$sql .= $whereClause->getFragment() . " ORDER BY lists.begdate ";
$sqlBindArray = $whereClause->getBoundValues();
$statementResults = QueryUtils::sqlStatementThrowException($sql, $sqlBindArray);
$processingResult = new ProcessingResult();
while ($row = sqlFetchArray($statementResults)) {
$resultRecord = $this->createResultRecordFromDatabaseResult($row);
$processingResult->addData($resultRecord);
}
return $processingResult;
}
protected function createResultRecordFromDatabaseResult($row)
{
$record = parent::createResultRecordFromDatabaseResult($row);
if (!empty($record['lists_medication_id'])) {
$extractKeys = ['usage_category', 'usage_category_title', 'request_intent', 'request_intent_title', 'drug_dosage_instructions'];
$record['medication'] = [
'id' => $row['lists_medication_id']
,'erx_source' => $row['erx_source']
,'erx_uploaded' => $row['erx_uploaded']
];
foreach ($extractKeys as $key) {
$record['medication'][$key] = $row[$key];
unset($row[$key]);
}
unset($record['list_medication_id']);
}
return $record;
}
}