Update setup.php (#2132)
[openemr.git] / services / EncounterService.php
blob06ff2eea2dde0c7b9c66e4342737804a8a31ac6f
1 <?php
2 /**
3 * EncounterService
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Matthew Vita <matthewvita48@gmail.com>
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2018 Matthew Vita <matthewvita48@gmail.com>
11 * @copyright Copyright (c) 2018 Jerry Padgett <sjpadgett@gmail.com>
12 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
17 namespace OpenEMR\Services;
19 use Particle\Validator\Validator;
21 class EncounterService
24 /**
25 * Default constructor.
27 public function __construct()
31 public function validateSoapNote($soapNote)
33 $validator = new Validator();
35 $validator->optional('subjective')->lengthBetween(2, 65535);
36 $validator->optional('objective')->lengthBetween(2, 65535);
37 $validator->optional('assessment')->lengthBetween(2, 65535);
38 $validator->optional('plan')->lengthBetween(2, 65535);
40 return $validator->validate($soapNote);
43 public function validateVital($vital)
45 $validator = new Validator();
47 $validator->optional('temp_method')->lengthBetween(1, 255);
48 $validator->optional('note')->lengthBetween(1, 255);
49 $validator->optional('BMI_status')->lengthBetween(1, 255);
50 $validator->optional('bps')->numeric();
51 $validator->optional('bpd')->numeric();
52 $validator->optional('weight')->numeric();
53 $validator->optional('height')->numeric();
54 $validator->optional('temperature')->numeric();
55 $validator->optional('pulse')->numeric();
56 $validator->optional('respiration')->numeric();
57 $validator->optional('BMI')->numeric();
58 $validator->optional('waist_circ')->numeric();
59 $validator->optional('head_circ')->numeric();
60 $validator->optional('oxygen_saturation')->numeric();
62 return $validator->validate($vital);
65 public function getEncountersForPatient($pid)
67 $sql = "SELECT fe.encounter as id,
68 fe.date,
69 fe.reason,
70 fe.facility,
71 fe.facility_id,
72 fe.pid,
73 fe.encounter as id,
74 fe.onset_date,
75 fe.sensitivity,
76 fe.billing_note,
77 fe.pc_catid,
78 fe.last_level_billed,
79 fe.last_level_closed,
80 fe.last_stmt_date,
81 fe.stmt_count,
82 fe.provider_id,
83 fe.supervisor_id,
84 fe.invoice_refno,
85 fe.referral_source,
86 fe.billing_facility,
87 fe.external_id,
88 fe.pos_code,
89 opc.pc_catname,
90 fa.name AS billing_facility_name
91 FROM form_encounter as fe
92 LEFT JOIN openemr_postcalendar_categories as opc
93 ON opc.pc_catid = fe.pc_catid
94 LEFT JOIN facility as fa ON fa.id = fe.billing_facility
95 WHERE pid=?
96 ORDER BY fe.id
97 DESC";
99 $statementResults = sqlStatement($sql, array($pid));
101 $results = array();
102 while ($row = sqlFetchArray($statementResults)) {
103 array_push($results, $row);
106 return $results;
109 public function getEncountersBySearch($search)
111 $sqlBindArray = array();
113 $sql = "SELECT fe.encounter as id,
114 fe.date,
115 fe.reason,
116 fe.facility,
117 fe.facility_id,
118 fe.pid,
119 fe.encounter,
120 fe.onset_date,
121 fe.sensitivity,
122 fe.billing_note,
123 fe.pc_catid,
124 fe.last_level_billed,
125 fe.last_level_closed,
126 fe.last_stmt_date,
127 fe.stmt_count,
128 fe.provider_id,
129 fe.supervisor_id,
130 fe.invoice_refno,
131 fe.referral_source,
132 fe.billing_facility,
133 fe.external_id,
134 fe.pos_code,
135 opc.pc_catname,
136 fa.name AS billing_facility_name
137 FROM form_encounter as fe
138 LEFT JOIN openemr_postcalendar_categories as opc
139 ON opc.pc_catid = fe.pc_catid
140 LEFT JOIN facility as fa ON fa.id = fe.billing_facility";
142 if ($search['pid'] || $search['provider_id']) {
143 $sql .= " WHERE ";
145 $whereClauses = array();
146 if ($search['pid']) {
147 array_push($whereClauses, "pid=?");
148 array_push($sqlBindArray, $search['pid']);
150 if ($search['provider_id']) {
151 array_push($whereClauses, "provider_id=?");
152 array_push($sqlBindArray, $search['provider_id']);
155 $sql .= implode(" AND ", $whereClauses);
156 } else {
157 return false;
159 $sql .= " ORDER BY fe.id DESC";
160 $statementResults = sqlStatement($sql, $sqlBindArray);
162 $results = array();
163 while ($row = sqlFetchArray($statementResults)) {
164 array_push($results, $row);
167 return $results;
170 public function getEncounter($eid)
172 $sql = "SELECT fe.encounter as id,
173 fe.date,
174 fe.reason,
175 fe.facility,
176 fe.facility_id,
177 fe.pid,
178 fe.onset_date,
179 fe.sensitivity,
180 fe.billing_note,
181 fe.pc_catid,
182 fe.last_level_billed,
183 fe.last_level_closed,
184 fe.last_stmt_date,
185 fe.stmt_count,
186 fe.provider_id,
187 fe.supervisor_id,
188 fe.invoice_refno,
189 fe.referral_source,
190 fe.billing_facility,
191 fe.external_id,
192 fe.pos_code,
193 opc.pc_catname,
194 fa.name AS billing_facility_name
195 FROM form_encounter as fe
196 LEFT JOIN openemr_postcalendar_categories as opc
197 ON opc.pc_catid = fe.pc_catid
198 LEFT JOIN facility as fa ON fa.id = fe.billing_facility
199 WHERE fe.encounter=?
200 ORDER BY fe.id
201 DESC";
203 return sqlQuery($sql, array($eid));
206 // @todo recm changing routes
207 // encounter id is system unique so pid is not needed
208 // resources should be independent where possible
209 public function getEncounterForPatient($pid, $eid)
211 $sql = "SELECT fe.encounter as id,
212 fe.date,
213 fe.reason,
214 fe.facility,
215 fe.facility_id,
216 fe.pid,
217 fe.onset_date,
218 fe.sensitivity,
219 fe.billing_note,
220 fe.pc_catid,
221 fe.last_level_billed,
222 fe.last_level_closed,
223 fe.last_stmt_date,
224 fe.stmt_count,
225 fe.provider_id,
226 fe.supervisor_id,
227 fe.invoice_refno,
228 fe.referral_source,
229 fe.billing_facility,
230 fe.external_id,
231 fe.pos_code,
232 opc.pc_catname,
233 fa.name AS billing_facility_name
234 FROM form_encounter as fe
235 LEFT JOIN openemr_postcalendar_categories as opc
236 ON opc.pc_catid = fe.pc_catid
237 LEFT JOIN facility as fa ON fa.id = fe.billing_facility
238 WHERE pid=? and fe.encounter=?
239 ORDER BY fe.id
240 DESC";
242 return sqlQuery($sql, array($pid, $eid));
245 public function insertSoapNote($pid, $eid, $data)
247 $soapSql = " INSERT INTO form_soap SET";
248 $soapSql .= " date=NOW(),";
249 $soapSql .= " activity=1,";
250 $soapSql .= " pid=?,";
251 $soapSql .= " subjective=?,";
252 $soapSql .= " objective=?,";
253 $soapSql .= " assessment=?,";
254 $soapSql .= " plan=?";
256 $soapResults = sqlInsert(
257 $soapSql,
258 array(
259 $pid,
260 $data["subjective"],
261 $data["objective"],
262 $data["assessment"],
263 $data["plan"]
267 if (!$soapResults) {
268 return false;
271 $formSql = "INSERT INTO forms SET";
272 $formSql .= " date=NOW(),";
273 $formSql .= " encounter=?,";
274 $formSql .= " form_name='SOAP',";
275 $formSql .= " authorized='1',";
276 $formSql .= " form_id=?,";
277 $formSql .= " pid=?,";
278 $formSql .= " formdir='soap'";
280 $formResults = sqlInsert(
281 $formSql,
282 array(
283 $eid,
284 $soapResults,
285 $pid
289 return array($soapResults, $formResults);
292 public function updateSoapNote($pid, $eid, $sid, $data)
294 $sql = " UPDATE form_soap SET";
295 $sql .= " date=NOW(),";
296 $sql .= " activity=1,";
297 $sql .= " pid=?,";
298 $sql .= " subjective=?,";
299 $sql .= " objective=?,";
300 $sql .= " assessment=?,";
301 $sql .= " plan=?";
302 $sql .= " where id=?";
304 return sqlStatement(
305 $sql,
306 array(
307 $pid,
308 $data["subjective"],
309 $data["objective"],
310 $data["assessment"],
311 $data["plan"],
312 $sid
317 public function updateVital($pid, $eid, $vid, $data)
319 $sql = " UPDATE form_vitals SET";
320 $sql .= " date=NOW(),";
321 $sql .= " activity=1,";
322 $sql .= " pid=?,";
323 $sql .= " bps=?,";
324 $sql .= " bpd=?,";
325 $sql .= " weight=?,";
326 $sql .= " height=?,";
327 $sql .= " temperature=?,";
328 $sql .= " temp_method=?,";
329 $sql .= " pulse=?,";
330 $sql .= " respiration=?,";
331 $sql .= " note=?,";
332 $sql .= " waist_circ=?,";
333 $sql .= " head_circ=?,";
334 $sql .= " oxygen_saturation=?";
335 $sql .= " where id=?";
337 return sqlStatement(
338 $sql,
339 array(
340 $pid,
341 $data["bps"],
342 $data["bpd"],
343 $data["weight"],
344 $data["height"],
345 $data["temperature"],
346 $data["temp_method"],
347 $data["pulse"],
348 $data["respiration"],
349 $data["note"],
350 $data["waist_circ"],
351 $data["head_circ"],
352 $data["oxygen_saturation"],
353 $vid
358 public function insertVital($pid, $eid, $data)
360 $vitalSql = " INSERT INTO form_vitals SET";
361 $vitalSql .= " date=NOW(),";
362 $vitalSql .= " activity=1,";
363 $vitalSql .= " pid=?,";
364 $vitalSql .= " bps=?,";
365 $vitalSql .= " bpd=?,";
366 $vitalSql .= " weight=?,";
367 $vitalSql .= " height=?,";
368 $vitalSql .= " temperature=?,";
369 $vitalSql .= " temp_method=?,";
370 $vitalSql .= " pulse=?,";
371 $vitalSql .= " respiration=?,";
372 $vitalSql .= " note=?,";
373 $vitalSql .= " waist_circ=?,";
374 $vitalSql .= " head_circ=?,";
375 $vitalSql .= " oxygen_saturation=?";
377 $vitalResults = sqlInsert(
378 $vitalSql,
379 array(
380 $pid,
381 $data["bps"],
382 $data["bpd"],
383 $data["weight"],
384 $data["height"],
385 $data["temperature"],
386 $data["temp_method"],
387 $data["pulse"],
388 $data["respiration"],
389 $data["note"],
390 $data["waist_circ"],
391 $data["head_circ"],
392 $data["oxygen_saturation"]
396 if (!$vitalResults) {
397 return false;
400 $formSql = "INSERT INTO forms SET";
401 $formSql .= " date=NOW(),";
402 $formSql .= " encounter=?,";
403 $formSql .= " form_name='Vitals',";
404 $formSql .= " authorized='1',";
405 $formSql .= " form_id=?,";
406 $formSql .= " pid=?,";
407 $formSql .= " formdir='vitals'";
409 $formResults = sqlInsert(
410 $formSql,
411 array(
412 $eid,
413 $vitalResults,
414 $pid
418 return array($vitalResults, $formResults);
421 public function getVitals($pid, $eid)
423 $sql = " SELECT fs.*";
424 $sql .= " FROM forms fo";
425 $sql .= " JOIN form_vitals fs on fs.id = fo.form_id";
426 $sql .= " WHERE fo.encounter = ?";
427 $sql .= " AND fs.pid = ?";
429 $statementResults = sqlStatement($sql, array($eid, $pid));
431 $results = array();
432 while ($row = sqlFetchArray($statementResults)) {
433 array_push($results, $row);
436 return $results;
439 public function getVital($pid, $eid, $vid)
441 $sql = " SELECT fs.*";
442 $sql .= " FROM forms fo";
443 $sql .= " JOIN form_vitals fs on fs.id = fo.form_id";
444 $sql .= " WHERE fo.encounter = ?";
445 $sql .= " AND fs.id = ?";
446 $sql .= " AND fs.pid = ?";
448 return sqlQuery($sql, array($eid, $vid, $pid));
451 public function getSoapNotes($pid, $eid)
453 $sql = " SELECT fs.*";
454 $sql .= " FROM forms fo";
455 $sql .= " JOIN form_soap fs on fs.id = fo.form_id";
456 $sql .= " WHERE fo.encounter = ?";
457 $sql .= " AND fs.pid = ?";
459 $statementResults = sqlStatement($sql, array($eid, $pid));
461 $results = array();
462 while ($row = sqlFetchArray($statementResults)) {
463 array_push($results, $row);
466 return $results;
469 public function getSoapNote($pid, $eid, $sid)
471 $sql = " SELECT fs.*";
472 $sql .= " FROM forms fo";
473 $sql .= " JOIN form_soap fs on fs.id = fo.form_id";
474 $sql .= " WHERE fo.encounter = ?";
475 $sql .= " AND fs.id = ?";
476 $sql .= " AND fs.pid = ?";
478 return sqlQuery($sql, array($eid, $sid, $pid));