forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatientTransactionService.php
306 lines (264 loc) · 9.09 KB
/
PatientTransactionService.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* PatientTransaction Service
* DAL to be used for Transactions
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Jonathan Moore <[email protected]>
* @copyright Copyright (c) 2022 Jonathan Moore <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use MongoDB\Driver\Query;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Services\Search\DateSearchField;
use OpenEMR\Services\Search\TokenSearchField;
use OpenEMR\Services\Search\TokenSearchValue;
use OpenEMR\Validators\ProcessingResult;
use Particle\Validator\Exception\InvalidValueException;
use Particle\Validator\Validator;
use OpenEMR\Validators\BaseValidator;
class PatientTransactionService extends BaseService
{
const TABLE_NAME = "transactions";
const _formPredicate = 'd.form_id';
const _transactionPredicate = 't.pid';
public function __construct()
{
parent::__construct(self::TABLE_NAME);
}
public function getSelectStatement($predicateColumnName)
{
$criteriaItemsWhitelist = [
self::_formPredicate,
self::_transactionPredicate
];
return
"
SELECT
d.form_id,
t.title,
t.groupname,
t.authorized,
MAX(
CASE WHEN d.field_id = 'body' THEN d.field_value ELSE ''
END
) AS body,
MAX(
CASE WHEN d.field_id = 'refer_date' THEN d.field_value ELSE ''
END
) AS refer_date,
MAX(
CASE WHEN d.field_id = 'refer_diag' THEN d.field_value ELSE ''
END
) AS refer_diag,
MAX(
CASE WHEN d.field_id = 'refer_from' THEN d.field_value ELSE ''
END
) AS refer_from,
MAX(
CASE WHEN d.field_id = 'refer_to' THEN d.field_value ELSE ''
END
) AS refer_to,
MAX(
CASE WHEN d.field_id = 'refer_risk_level' THEN d.field_value ELSE ''
END
) AS refer_risk_level,
MAX(
CASE WHEN d.field_id = 'refer_vitals' THEN d.field_value ELSE ''
END
) AS refer_vitals,
MAX(
CASE WHEN d.field_id = 'refer_authorization' THEN d.field_value ELSE ''
END
) AS refer_authorization,
MAX(
CASE WHEN d.field_id = 'refer_visits' THEN d.field_value ELSE ''
END
) AS refer_visits,
MAX(
CASE WHEN d.field_id = 'refer_validFrom' THEN d.field_value ELSE ''
END
) AS refer_validFrom,
MAX(
CASE WHEN d.field_id = 'refer_validThrough' THEN d.field_value ELSE ''
END
) AS refer_validThrough
FROM
transactions t
LEFT JOIN lbt_data d ON
d.form_id = t.id
WHERE " . escape_identifier($predicateColumnName, $criteriaItemsWhitelist, true) . " = ?
GROUP BY
d.form_id,
t.groupname,
t.title;
";
}
private function getOneFromDb($tid)
{
$sqlBindArray = array();
$sql = $this->getSelectStatement(self::_formPredicate);
array_push($sqlBindArray, $tid);
$records = QueryUtils::fetchRecords($sql, $sqlBindArray);
return $records;
}
public function getAll($pid)
{
$processingResult = new ProcessingResult();
$sqlBindArray = array();
$sql = $this->getSelectStatement(self::_transactionPredicate);
array_push($sqlBindArray, $pid);
$records = QueryUtils::fetchRecords($sql, $sqlBindArray);
if (count($records) > 0) {
$processingResult->addData($records);
}
return $processingResult;
}
public function insert($pid, $data)
{
sqlBeginTrans();
$transactionId = $this->insertTransaction($pid, $data);
if ($transactionId == false) {
return false;
}
$lbtDataId = $this->insertTransactionForm($transactionId, $data);
if ($lbtDataId == false) {
return false;
}
sqlCommitTrans();
return ["id" => $transactionId, "form_id" => $lbtDataId];
}
public function insertTransaction($pid, $data)
{
$user = $_SESSION['authUser'];
$sql =
"
INSERT INTO transactions SET
date=NOW(),
title=?,
pid=?,
groupname=?,
authorized=1,
user=?
";
$results = sqlInsert(
$sql,
array(
$data["type"],
$pid,
$data['groupname'],
$user
)
);
if (!$results) {
return false;
}
return $results;
}
public function insertTransactionForm($transactionId, $data)
{
$referById = $this->getUserIdByNpi($data["referByNpi"]);
$referToId = $this->getUserIdByNpi($data["referToNpi"]);
$sql =
"
INSERT INTO lbt_data (form_id, field_id, field_value) VALUES
(?, 'body', ?),
(?, 'refer_date', ?),
(?, 'refer_diag', ?),
(?, 'refer_from', ?),
(?, 'refer_to', ?),
(?, 'refer_risk_level', ?),
(?, 'refer_vitals', ?),
(?, 'refer_authorization', ?),
(?, 'refer_visits', ?),
(?, 'refer_validFrom', ?),
(?, 'refer_validThrough', ?)
";
$params = array
(
$transactionId, $data["body"],
$transactionId, $data["referralDate"],
$transactionId, $data["referDiagnosis"],
$transactionId, $referById,
$transactionId, $referToId,
$transactionId, strtolower($data["riskLevel"]),
$transactionId, $data["includeVitals"],
$transactionId, $data["authorization"],
$transactionId, $data["visits"],
$transactionId, $data["validFrom"],
$transactionId, $data["validThrough"],
);
$results = sqlInsert(
$sql,
$params
);
if (!$results) {
return false;
}
return $results;
}
public function update($tid, $data)
{
$referById = $this->getUserIdByNpi($data["referByNpi"]);
$referToId = $this->getUserIdByNpi($data["referToNpi"]);
$body = $data["body"];
$referralDate = $data["referralDate"];
$referralDiagnosis = $data["referDiagnosis"];
$riskLevel = strtolower($data["riskLevel"]);
$includeVitals = $data["includeVitals"];
$authorization = $data["authorization"];
$visits = $data["visits"];
$validFrom = $data["validFrom"];
$validThrough = $data["validThrough"];
sqlBeginTrans();
$this->updateTransactionForm($tid, 'refer_from', $referById);
$this->updateTransactionForm($tid, 'refer_to', $referToId);
$this->updateTransactionForm($tid, 'body', $body);
$this->updateTransactionForm($tid, 'refer_date', $referralDate);
$this->updateTransactionForm($tid, 'refer_diag', $referralDiagnosis);
$this->updateTransactionForm($tid, 'refer_risk_level', $riskLevel);
$this->updateTransactionForm($tid, 'refer_vitals', $includeVitals);
$this->updateTransactionForm($tid, 'refer_authorization', $authorization);
$this->updateTransactionForm($tid, 'refer_visits', $visits);
$this->updateTransactionForm($tid, 'refer_validFrom', $validFrom);
$this->updateTransactionForm($tid, 'refer_validThrough', $validThrough);
sqlCommitTrans();
return $this->getOneFromDb($tid);
}
public function updateTransactionForm($formId, $fieldId, $value)
{
if (empty($value) == false) {
$sql = "Update lbt_data SET field_value = ? Where field_id = ? and form_id = ?";
$params = array($value, $fieldId, $formId);
$res = sqlStatement($sql, $params);
}
}
public function validate($transaction)
{
$transactionType = $transaction["type"];
if (empty($transactionType)) {
$this->throwException('type is not valid', 'type');
}
$validator = new Validator();
switch ($transactionType) {
case "LBTref":
$validator->required('referralDate')->datetime('Y-m-d');
$validator->required('body')->lengthBetween(2, 150);
$validator->required('groupname')->string();
$validator->required('referByNpi')->string();
break;
}
return $validator->validate($transaction);
}
public function getUserIdByNpi($npi)
{
try {
return QueryUtils::fetchSingleValue('Select id FROM users WHERE npi = ? ', 'id', [$npi]);
} catch (exception $ex) {
return $ex;
}
}
}