fix: ccda zip import and php warnings and deprecations (#7416)
[openemr.git] / interface / eRxStore.php
blob840042bb0a4eb796d0ea852eb74a682b253c71e2
1 <?php
3 /**
4 * interface/eRxStore.php Functions for interacting with NewCrop database.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Sam Likins <sam.likins@wsi-services.com>
9 * @author Ken Chapple <ken@mi-squared.com>
10 * @copyright Copyright (c) 2013-2015 Sam Likins <sam.likins@wsi-services.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 require_once(__DIR__ . "/../library/api.inc.php");
16 class eRxStore
18 /**
19 * Strip away any non numerical characters
20 * @param string $value Value to sanitize
21 * @return string Value sanitized of all non numerical characters
23 public static function sanitizeNumber($value)
25 $sanitized = '';
26 if ($value !== null) {
27 $sanitized = preg_replace('/[^-0-9.]/', '', $value);
30 return $sanitized;
33 /**
34 * Return the primary business entity
35 * @return array Primary business entity
37 public function getFacilityPrimary()
39 $return = sqlQuery('SELECT `name`, `federal_ein`, `street`, `city`, `state`, `postal_code`, `country_code`, `phone`, `fax`
40 FROM `facility`
41 WHERE `primary_business_entity` = \'1\';');
43 return $return;
46 /**
47 * Return the Federal EIN established with the primary business entity
48 * @return string Federal EIN for the primary business entity
50 public function selectFederalEin()
52 $return = $this->getFacilityPrimary();
54 return $return['federal_ein'];
57 /**
58 * Return user information using user Id
59 * @param integer $id Id of user to return
60 * @return array Specified user information: index [id, username, lname, fname, mname, title, license, federaldrugid, upin, state_license_number, npi, newcrop_user_role]
62 public function getUserById($id)
64 return sqlQuery(
65 'SELECT id, username, lname, fname, mname, title, federaldrugid, upin, state_license_number, npi, newcrop_user_role
66 FROM users
67 WHERE id = ?;',
68 array($id)
72 /**
73 * Return user facility business entity
74 * @param integer $id Id of user to return
75 * @return array User facility business entity
77 public function getUserFacility($id)
79 return sqlQuery(
80 'SELECT facility.id, facility.name, facility.street, facility.city, facility.state, facility.postal_code, facility.country_code, facility.phone, facility.fax
81 FROM users
82 LEFT JOIN facility ON facility.id = users.facility_id
83 WHERE users.id = ?;',
84 array($id)
88 /**
89 * Return patient information using patient Id
90 * @param integer $patientId Id of patient
91 * @return array Specified patient information: index [pid, fname, mname, lname, street, city, state, postal_code, country_code, phone_home, date_of_birth, sex]
93 public function getPatientByPatientId($patientId)
95 return sqlQuery(
96 'SELECT pid, fname, mname, lname, street, city, state, postal_code, country_code, phone_home, DATE_FORMAT(DOB,\'%Y%m%d\') AS date_of_birth, sex
97 FROM patient_data
98 WHERE pid = ?;',
99 array($patientId)
103 public function getPatientVitalsByPatientId($patientId)
105 $result = sqlQuery(
106 "SELECT FORM_VITALS.date, FORM_VITALS.id
107 FROM form_vitals AS FORM_VITALS LEFT JOIN forms AS FORMS ON FORM_VITALS.id = FORMS.form_id
108 WHERE FORM_VITALS.pid=? AND FORMS.deleted != '1'
109 ORDER BY FORM_VITALS.date DESC",
110 array($patientId)
113 $data = formFetch("form_vitals", $result['id']);
115 $weight = number_format($data['weight'] * 0.45359237, 2);
116 $height = number_format(round($data['height'] * 2.54, 1), 2);
118 return [
119 'height' => $height,
120 'height_units' => 'cm',
121 'weight' => $weight,
122 'weight_units' => 'kg'
126 public function getPatientHealthplansByPatientId($patientId)
128 return sqlStatement(
129 'SELECT `ins`.`name`
130 FROM (
131 SELECT
132 `id`.`type`,
133 `ic`.`name`
134 FROM `insurance_data` AS `id`
135 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
136 WHERE `id`.`pid` = ?
137 AND `id`.`subscriber_relationship` = \'self\'
138 AND `id`.`provider` > 0
139 ORDER BY `id`.`date` DESC
140 ) AS `ins`
141 GROUP BY `ins`.`type`;',
142 array($patientId)
146 public function getPatientAllergiesByPatientId($patientId)
148 return sqlStatement(
149 'SELECT id, lists.title as title1, list_options.title as title2, comments
150 FROM lists
151 LEFT JOIN list_options ON lists.outcome = list_options.option_id
152 AND list_options.list_id = \'outcome\'
153 WHERE `type` = \'allergy\'
154 AND pid = ?
155 AND erx_source = \'0\'
156 AND erx_uploaded = \'0\'
157 AND (
158 enddate is NULL
159 OR enddate = \'0000-00-00\'
160 );',
161 array($patientId)
165 public function getPatientDiagnosisByPatientId($patientId)
167 return sqlStatement(
168 'SELECT diagnosis, begdate, enddate, title, date
169 FROM lists
170 WHERE `type` = \'medical_problem\'
171 AND pid = ?
173 array($patientId)
178 * Return TTL timestamp for provided patient Id and process
179 * @param string $process SOAP process to check
180 * @param integer $patientId Patient Id to check
181 * @return string|boolean TTL timestamp of last SOAP call for provided patient Id and process
183 public function getLastSOAP($process, $patientId)
185 $return = sqlQuery(
186 'SELECT updated
187 FROM erx_ttl_touch
188 WHERE patient_id = ?
189 AND process = ?;',
190 array(
191 $patientId,
192 $process
195 if ($return === false) {
196 return false;
199 return $return['updated'];
203 * Set TTL timestamp for provided patient Id and process
204 * @param string $process SOAP process to update
205 * @param integer $patientId Patient Id to update
207 public function setLastSOAP($process, $patientId)
209 sqlQuery(
210 'REPLACE INTO erx_ttl_touch
211 SET patient_id = ?,
212 process = ?,
213 updated = NOW();',
214 array(
215 $patientId,
216 $process
222 * Update external sourced prescripts active status for provided patient Id
223 * @param integer $patientId Patient Id to update
224 * @param integer $active Active status to set for provided patient
226 public function updatePrescriptionsActiveByPatientId($patientId, $active = 0)
228 sqlQuery(
229 'UPDATE prescriptions
230 SET active = ?
231 WHERE patient_id = ?
232 AND erx_source=\'1\'',
233 array(
234 ($active == 1 ? 1 : 0),
235 $patientId
240 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId)
242 sqlQuery(
243 'UPDATE prescriptions
244 SET erx_uploaded = ?,
245 active = ?
246 WHERE patient_id = ?
247 AND id = ?;',
248 array(
249 $upload,
250 $active,
251 $patientId,
252 $prescriptionId
258 * Return prescription specified
259 * @param integer $prescriptionId Id of the prescription to return
260 * @return array Prescription information specified
262 public function getPrescriptionById($prescriptionId)
264 return sqlQuery(
265 'SELECT p.note, p.dosage, p.substitute, p.per_refill, p.form, p.route, p.size, p.interval, p.drug, p.quantity,
266 p.id AS prescid, l1.title AS title1, l2.title AS title2, l3.title AS title3, l4.title AS title4,
267 DATE_FORMAT(date_added,\'%Y%m%d\') AS date_added, CONCAT_WS(fname, \' \', mname, \' \', lname) AS docname
268 FROM prescriptions AS p
269 LEFT JOIN users AS u ON p.provider_id = u.id
270 LEFT JOIN list_options AS l1 ON l1.list_id = \'drug_form\'
271 AND l1.option_id = p.form
272 LEFT JOIN list_options AS l2 ON l2.list_id = \'drug_route\'
273 AND l2.option_id = p.route
274 LEFT JOIN list_options AS l3 ON l3.list_id = \'drug_interval\'
275 AND l3.option_id = p.interval
276 LEFT JOIN list_options AS l4 ON l4.list_id = \'drug_units\'
277 AND l4.option_id = p.unit
278 WHERE p.drug <> \'\'
279 AND p.id = ?;',
280 array($prescriptionId)
285 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit)
287 return sqlStatement(
288 'SELECT id, begdate, title
289 FROM lists
290 WHERE type = \'medication\'
291 AND pid = ?
292 AND title <> \'\'
293 AND erx_uploaded = \'0\'
294 AND (? = 0
295 OR (enddate IS NULL
296 OR enddate = \'0000-00-00\'
299 ORDER BY enddate
300 LIMIT 0, ?;',
301 array(
302 $patientId,
303 $uploadActive,
304 $limit
309 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit)
311 return sqlStatement(
312 'SELECT id
313 FROM prescriptions
314 WHERE patient_id = ?
315 AND erx_source = \'0\'
316 AND erx_uploaded = \'0\'
317 AND (? = 0
318 OR active = 1
319 ) LIMIT 0, ?;',
320 array(
321 $patientId,
322 $uploadActive,
323 $limit,
329 * Return option Id for title text of specified list
330 * @param string $listId Id of list to reference
331 * @param string $title Title text to find
332 * @return string Option Id of selected list item
334 public function selectOptionIdByTitle($listId, $title)
336 $return = sqlQuery(
337 'SELECT option_id
338 FROM list_options
339 WHERE list_id = ? AND activity = 1
340 AND title = ?;',
341 array(
342 $listId,
343 $title
347 if (is_array($return)) {
348 $return = $return['option_id'];
351 return $return;
355 * Return highest option Id for provided list Id
356 * @param string $listId Id of list to reference
357 * @return integer Highest option Id for provided list Id
359 public function selectOptionIdsByListId($listId)
361 $return = sqlQuery(
362 'SELECT option_id
363 FROM list_options
364 WHERE list_id = ? AND activity = 1
365 ORDER BY ABS(option_id) DESC
366 LIMIT 1;',
367 array($listId)
370 if (is_array($return)) {
371 $return = $return['option_id'];
374 return $return;
378 * Return user Id by user name
379 * @param string $name Name of user to reference
380 * @return integer Id of provided user name
382 public function selectUserIdByUserName($name)
384 $return = sqlQuery(
385 'SELECT id
386 FROM users
387 WHERE username = ?;',
388 array($name)
391 return $return['id'];
395 * Insert new option to specified list
396 * @param string $listId Id of list to add option to
397 * @param string $optionId Option Id to add to referenced list
398 * @param string $title Title of option to add to new option
400 public function insertListOptions($listId, $optionId, $title)
402 sqlQuery(
403 'INSERT INTO list_options
404 (list_id, option_id, title, seq)
405 VALUES
406 (?, ?, ?, ?);',
407 array(
408 $listId,
409 $optionId,
410 $title,
411 $optionId
417 * Return Id of prescription selected by GUID and patient Id
418 * @param string $prescriptionGuid GUID of prescription
419 * @param integer $patientId Id of patient
420 * @return resource Prescription Id of specified GUID for selected patient, this resource comes from a call to mysql_query()
422 public function selectPrescriptionIdByGuidPatientId($prescriptionGuid, $patientId)
424 return sqlStatement(
425 'SELECT id
426 FROM prescriptions
427 WHERE prescriptionguid = ?
428 AND prescriptionguid IS NOT NULL
429 AND patient_id = ?;',
430 array(
431 $prescriptionGuid,
432 $patientId
438 * Insert new prescription as external sourced
439 * @param array $prescriptionData Information for creating prescription: [PrescriptionDate, DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
440 * @param integer $encounter Id of encounter for prescription
441 * @param integer $providerId Id of provider for prescription
442 * @param string $authUserId Id of user creating prescription
443 * @param integer $formOptionId Option Id for prescription form
444 * @param integer $routeOptionId Option Id for prescription route
445 * @param integer $unitsOptionId Option Id for prescription units
446 * @param integer $intervalOptionId Option Id for prescription interval
447 * @return integer Id of newly created prescription
449 public function insertPrescriptions($prescriptionData, $encounter, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
451 return sqlInsert(
452 'INSERT INTO `prescriptions`
454 `datetime`,
455 `erx_source`,
456 `encounter`,
457 `date_added`,
458 `user`,
459 `provider_id`,
460 `form`,
461 `unit`,
462 `route`,
463 `interval`,
464 `drug`,
465 `drug_id`,
466 `drug_info_erx`,
467 `dosage`,
468 `size`,
469 `refills`,
470 `note`,
471 `site`,
472 `rxnorm_drugcode`,
473 `prescriptionguid`,
474 `patient_id`
476 VALUES
478 NOW(), \'1\', ?, ?, ?,
479 ?, ?, ?, ?, ?, ?, ?, ?,
480 ?, ?, ?, ?, ?, ?, ?, ?
481 );',
482 array(
483 $encounter,
484 substr($prescriptionData['PrescriptionDate'], 0, 10),
485 $authUserId,
486 $providerId,
487 $formOptionId,
488 $unitsOptionId,
489 $routeOptionId,
490 $intervalOptionId,
491 $prescriptionData['DrugName'],
492 $prescriptionData['DrugID'],
493 $prescriptionData['DrugInfo'],
494 $prescriptionData['DosageNumberDescription'],
495 self::sanitizeNumber($prescriptionData['Strength']),
496 $prescriptionData['Refills'],
497 $prescriptionData['PrescriptionNotes'],
498 $prescriptionData['SiteID'],
499 $prescriptionData['rxcui'],
500 $prescriptionData['PrescriptionGuid'],
501 $prescriptionData['ExternalPatientID']
507 * Update prescription information as external sourced
508 * @param array $prescriptionData Information for creating prescription: [DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
509 * @param integer $providerId Id of provider for prescription
510 * @param string $authUserId Id of user creating prescription
511 * @param integer $formOptionId Option Id for prescription form
512 * @param integer $routeOptionId Option Id for prescription route
513 * @param integer $unitsOptionId Option Id for prescription units
514 * @param integer $intervalOptionId Option Id for prescription interval
516 public function updatePrescriptions($prescriptionData, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
518 sqlQuery(
519 'UPDATE prescriptions SET
520 `datetime` = NOW(),
521 `erx_source` = \'1\',
522 `active` = \'1\',
523 `user` = ?,
524 `provider_id` = ?,
525 `form` = ?,
526 `unit` = ?,
527 `route` = ?,
528 `interval` = ?,
529 `drug` = ?,
530 `drug_id` = ?,
531 `drug_info_erx` = ?,
532 `dosage` = ?,
533 `size` = ?,
534 `refills` = ?,
535 `note` = ?,
536 `site` = ?,
537 `rxnorm_drugcode` = ?
538 WHERE prescriptionguid = ?
539 AND patient_id = ?;',
540 array(
541 $authUserId,
542 $providerId,
543 $formOptionId,
544 $unitsOptionId,
545 $routeOptionId,
546 $intervalOptionId,
547 $prescriptionData['DrugName'],
548 $prescriptionData['DrugID'],
549 $prescriptionData['DrugInfo'],
550 $prescriptionData['DosageNumberDescription'],
551 self::sanitizeNumber($prescriptionData['Strength']),
552 $prescriptionData['Refills'],
553 $prescriptionData['PrescriptionNotes'],
554 $prescriptionData['SiteID'],
555 $prescriptionData['rxcui'],
556 $prescriptionData['PrescriptionGuid'],
557 $prescriptionData['ExternalPatientID']
563 * Return eRx source of specified active allergy for selected patient
564 * @param integer $patientId Id of patient to select
565 * @param string $name Name of active allergy to return
566 * @return integer eRx source flag of specified allergy for selected patient: [0 = OpenEMR, 1 = External]
568 public function selectAllergyErxSourceByPatientIdName($patientId, $name)
570 $return = sqlQuery(
571 'SELECT erx_source
572 FROM lists
573 WHERE pid = ?
574 AND type = \'allergy\'
575 AND title = ?
576 AND (
577 enddate IS NULL
578 OR enddate = \'0000-00-00\'
579 );',
580 array(
581 $patientId,
582 $name
586 if (is_array($return)) {
587 $return = $return['erx_source'];
590 return $return;
594 * Insert new allergy as external sourced
595 * @param string $name Allergy name to insert
596 * @param integer $allergyId External allergy Id
597 * @param integer $patientId Patient Id
598 * @param integer $authUserId User Id
599 * @param integer $outcome Allergy option Id
601 public function insertAllergy($name, $allergyId, $patientId, $authUserId, $outcome)
603 sqlQuery(
604 'INSERT INTO lists
606 date, type, erx_source, begdate,
607 title, external_allergyid, pid, user, outcome
609 VALUES
611 NOW(), \'allergy\', \'1\', NOW(),
612 ?, ?, ?, ?, ?
613 );',
614 array(
615 $name,
616 $allergyId,
617 $patientId,
618 $authUserId,
619 $outcome
623 setListTouch($patientId, 'allergy');
627 * Update allergy outcome and external Id as external sourced using patient Id and allergy name
628 * @param integer $outcome Allergy outcome Id to set
629 * @param integer $externalId External allergy Id to set
630 * @param integer $patientId Patient Id to select
631 * @param string $name Allergy name to select
633 public function updateAllergyOutcomeExternalIdByPatientIdName($outcome, $externalId, $patientId, $name)
635 sqlQuery(
636 'UPDATE lists
637 SET outcome = ?,
638 erx_source = \'1\',
639 external_allergyid = ?
640 WHERE pid = ?
641 AND title = ?;',
642 array(
643 $outcome,
644 $externalId,
645 $patientId,
646 $name
652 * Update external sourced allergy outcome using patient Id, external Id, and allergy name
653 * @param integer $outcome Allergy outcome Id to set
654 * @param integer $patientId Patient Id to select
655 * @param integer $externalId External allergy Id to select
656 * @param string $name Allergy name to select
658 public function updateAllergyOutcomeByPatientIdExternalIdName($outcome, $patientId, $externalId, $name)
660 sqlQuery(
661 'UPDATE lists
662 SET outcome = ?
663 WHERE pid = ?
664 AND erx_source = \'1\'
665 AND external_allergyid = ?
666 AND title = ?;',
667 array(
668 $outcome,
669 $patientId,
670 $externalId,
671 $name
676 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId)
678 sqlQuery(
679 'UPDATE lists
680 SET erx_uploaded = ?
681 WHERE type = \'allergy\'
682 AND pid = ?
683 AND id = ?;',
684 array(
685 $uploaded,
686 $patientId,
687 $allergyId
693 * Return all external sourced active allergies for patient using patient Id
694 * @param integer $patientId Patient Id to select
695 * @return resource Patients active allergies, this resource comes from a call to mysql_query()
697 public function selectActiveAllergiesByPatientId($patientId)
699 return sqlStatement(
700 'SELECT id, title
701 FROM lists
702 WHERE pid = ?
703 AND type = \'allergy\'
704 AND erx_source = \'1\'
705 AND (
706 enddate IS NULL
707 OR enddate = \'0000-00-00\'
708 );',
709 array($patientId)
714 * Update allergy end date for specified patient Id and list Id
715 * @param integer $patientId Id of patient to lookup
716 * @param integer $listId Id of allergy to update
718 public function updateAllergyEndDateByPatientIdListId($patientId, $listId)
720 sqlQuery(
721 'UPDATE lists
722 SET enddate = now()
723 WHERE pid = ?
724 AND id = ?
725 AND type = \'allergy\';',
726 array(
727 $patientId,
728 $listId
734 * Update eRx uploaded status using list Id
735 * @param integer $listId Id of list item
736 * @param integer $erx [optional - defaults to 0] Upload status to set: [0 = Pending NewCrop upload, 1 = Uploaded TO NewCrop]
738 public function updateErxUploadedByListId($listId, $erx = 0)
740 sqlQuery(
741 'UPDATE lists
742 SET erx_uploaded = ?
743 WHERE id = ?;',
744 array(
745 $erx,
746 $listId
752 * Return patient import status using patient Id
753 * @param integer $patientId Id of patient
754 * @return integer Import status for specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
756 public function getPatientImportStatusByPatientId($patientId)
758 $return = sqlquery(
759 'SELECT soap_import_status
760 FROM patient_data
761 WHERE pid = ?;',
762 array($patientId)
764 return $return['soap_import_status'];
768 * Update patient import status using patient Id
769 * @param integer $patientId Id of patient to update
770 * @param integer $status Import status to update specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
772 public function updatePatientImportStatusByPatientId($patientId, $status)
774 sqlQuery(
775 'UPDATE patient_data
776 SET soap_import_status = ?
777 WHERE pid = ?;',
778 array(
779 $status,
780 $patientId