forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrugService.php
209 lines (192 loc) · 8.28 KB
/
DrugService.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* DrugService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Yash Bothra <yashrajbothra786gmail.com>
* @copyright Copyright (c) 2020 Yash Bothra <yashrajbothra786gmail.com>
* @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\Database\SqlQueryException;
use OpenEMR\Common\Logging\SystemLogger;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Services\Search\FhirSearchWhereClauseBuilder;
use OpenEMR\Services\Search\ISearchField;
use OpenEMR\Services\Search\ReferenceSearchField;
use OpenEMR\Services\Search\ReferenceSearchValue;
use OpenEMR\Services\Search\SearchFieldException;
use OpenEMR\Services\Search\SearchModifier;
use OpenEMR\Services\Search\StringSearchField;
use OpenEMR\Services\Search\TokenSearchField;
use OpenEMR\Services\Search\TokenSearchValue;
use OpenEMR\Validators\ProcessingResult;
class DrugService extends BaseService
{
private const DRUG_TABLE = "drugs";
/**
* Default constructor.
*/
public function __construct()
{
parent::__construct(self::DRUG_TABLE);
UuidRegistry::createMissingUuidsForTables([self::DRUG_TABLE]);
}
public function getUuidFields(): array
{
return ['uuid'];
}
/**
* Returns a list of drugs matching optional search criteria.
* Search criteria is conveyed by array where key = field/column name, value = field value.
* If no search criteria is provided, all records are returned.
*
* @param $search search array parameters
* @param $isAndCondition specifies if AND condition is used for multiple criteria. Defaults to true.
* @param $puuidBind - Patient uuid to return drug resources that are only visible to the current patient
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function getAll($search = array(), $isAndCondition = true, $puuidBind = null)
{
$newSearch = [];
foreach ($search as $key => $value) {
if (!$value instanceof ISearchField) {
$newSearch[] = new StringSearchField($key, [$value], SearchModifier::EXACT);
} else {
$newSearch[$key] = $value;
}
}
// so if we have a puuid we need to make sure we only return drugs that are connected to the current patient.
if (isset($puuidBind)) {
$newSearch['puuid'] = new TokenSearchField('puuid', $puuidBind, true);
}
return $this->search($newSearch, $isAndCondition);
}
/**
* Returns a single drug record by id.
* @param $uuid - The drug uuid identifier in string format.
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function getOne($uuid)
{
$search = [
'uuid' => new TokenSearchField('uuid', [new TokenSearchValue($uuid, null, false)])
];
// so if we have a puuid we need to make sure we only return drugs that are connected to the current patient.
if (isset($puuid)) {
$search['puuid'] = new ReferenceSearchField('puuid', [new ReferenceSearchValue($puuid, 'Patient', true)]);
}
return $this->search($search);
}
public function search($search, $isAndCondition = true)
{
$sql = "SELECT
drug_table.drug_id,
drug_table.uuid,
drug_table.name,
drug_table.ndc_number,
drug_table.form,
drug_table.size,
drug_table.unit,
drug_table.route,
drug_table.related_code,
drug_table.active,
drug_table.drug_code,
IF(drug_prescriptions.rxnorm_drugcode!=''
,drug_prescriptions.rxnorm_drugcode
,IF(drug_table.drug_code IS NULL, '', drug_table.drug_code)
) AS 'rxnorm_drugcode',
drug_inventory.manufacturer,
drug_inventory.lot_number,
drug_inventory.expiration,
drug_table.drug_last_updated,
drug_table.drug_date_created
FROM (
select
drug_id,
uuid,
name,
ndc_number,
form,
size,
unit,
route,
related_code,
active,
drug_code,
last_updated AS drug_last_updated,
date_created AS drug_date_created
FROM
drugs
) drug_table
LEFT JOIN drug_inventory
ON drug_table.drug_id = drug_inventory.drug_id
LEFT JOIN (
select
uuid AS prescription_uuid
,rxnorm_drugcode
,drug_id
,patient_id as prescription_patient_id
FROM
prescriptions
) drug_prescriptions
ON drug_prescriptions.drug_id = drug_table.drug_id
LEFT JOIN (
select uuid AS puuid
,pid
FROM patient_data
) patient
ON patient.pid = drug_prescriptions.prescription_patient_id";
$processingResult = new ProcessingResult();
try {
$whereClause = FhirSearchWhereClauseBuilder::build($search, $isAndCondition);
$sql .= $whereClause->getFragment();
$sqlBindArray = $whereClause->getBoundValues();
$statementResults = QueryUtils::sqlStatementThrowException($sql, $sqlBindArray);
while ($row = sqlFetchArray($statementResults)) {
$resultRecord = $this->createResultRecordFromDatabaseResult($row);
$processingResult->addData($resultRecord);
}
} catch (SqlQueryException $exception) {
// we shouldn't hit a query exception
(new SystemLogger())->error($exception->getMessage(), ['trace' => $exception->getTraceAsString()]);
$processingResult->addInternalError("Error selecting data from database");
} catch (SearchFieldException $exception) {
(new SystemLogger())->error($exception->getMessage(), ['trace' => $exception->getTraceAsString(), 'field' => $exception->getField()]);
$processingResult->setValidationMessages([$exception->getField() => $exception->getMessage()]);
}
return $processingResult;
}
protected function createResultRecordFromDatabaseResult($row)
{
$record = parent::createResultRecordFromDatabaseResult($row);
if ($record['rxnorm_drugcode'] != "") {
// removed the RXCUI concatenation out of the db query and into the code here
// some parts of OpenEMR adds the RXCUI designation in the drug_code such as the inventory/dispensary module
// and this causes the FHIR medication resource to not get the actual RXCUI code.
if ($row['drug_code'] == $record['rxnorm_drugcode'] && strpos($row['drug_code'], ':') === false) {
$codes = $this->addCoding("RXCUI:" . $row['drug_code']);
} else {
$codes = $this->addCoding($row['rxnorm_drugcode']);
}
$updatedCodes = [];
foreach ($codes as $code => $codeValues) {
if (empty($codeValues['description'])) {
// use the drug name if for some reason we have no rxnorm description from the lookup
$codeValues['description'] = $row['drug'];
}
$updatedCodes[$code] = $codeValues;
}
$record['drug_code'] = $updatedCodes;
}
// TODO: @adunsulag this looks odd... why modify the original row...? look at removing this.
if ($row['rxnorm_drugcode'] != "") {
$row['drug_code'] = $this->addCoding($row['drug_code']);
}
return $record;
}
}