Fully responsive globals.php with vertical menu (#2460)
[openemr.git] / interface / eRxStore.php
blobe6f7d7340cac24bd9032c3f4d423fad25152b9a1
1 <?php
2 /**
3 * interface/eRxStore.php Functions for interacting with NewCrop database.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Sam Likins <sam.likins@wsi-services.com>
8 * @copyright Copyright (c) 2013-2015 Sam Likins <sam.likins@wsi-services.com>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 class eRxStore
16 /**
17 * Strip away any non numerical characters
18 * @param string $value Value to sanitize
19 * @return string Value sanitized of all non numerical characters
21 public static function sanitizeNumber($value)
23 return preg_replace('/[^-0-9.]/', '', $value);
26 /**
27 * Return the primary business entity
28 * @return array Primary business entity
30 public function getFacilityPrimary()
32 $return = sqlQuery('SELECT `name`, `federal_ein`, `street`, `city`, `state`, `postal_code`, `country_code`, `phone`, `fax`
33 FROM `facility`
34 WHERE `primary_business_entity` = \'1\';');
36 return $return;
39 /**
40 * Return the Federal EIN established with the primary business entity
41 * @return string Federal EIN for the primary business entity
43 public function selectFederalEin()
45 $return = $this->getFacilityPrimary();
47 return $return['federal_ein'];
50 /**
51 * Return user information using user Id
52 * @param integer $id Id of user to return
53 * @return array Specified user information: index [id, username, lname, fname, mname, title, license, federaldrugid, upin, state_license_number, npi, newcrop_user_role]
55 public function getUserById($id)
57 return sqlQuery(
58 'SELECT id, username, lname, fname, mname, title, federaldrugid, upin, state_license_number, npi, newcrop_user_role
59 FROM users
60 WHERE id = ?;',
61 array($id)
65 /**
66 * Return user facility business entity
67 * @param integer $id Id of user to return
68 * @return array User facility business entity
70 public function getUserFacility($id)
72 return sqlQuery(
73 'SELECT facility.id, facility.name, facility.street, facility.city, facility.state, facility.postal_code, facility.country_code, facility.phone, facility.fax
74 FROM users
75 LEFT JOIN facility ON facility.id = users.facility_id
76 WHERE users.id = ?;',
77 array($id)
81 /**
82 * Return patient information using patient Id
83 * @param integer $patientId Id of patient
84 * @return array Specified patient information: index [pid, fname, mname, lname, street, city, state, postal_code, country_code, phone_home, date_of_birth, sex]
86 public function getPatientByPatientId($patientId)
88 return sqlQuery(
89 '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
90 FROM patient_data
91 WHERE pid = ?;',
92 array($patientId)
96 public function getPatientHealthplansByPatientId($patientId)
98 return sqlStatement(
99 'SELECT `ins`.`name`
100 FROM (
101 SELECT
102 `id`.`type`,
103 `ic`.`name`
104 FROM `insurance_data` AS `id`
105 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
106 WHERE `id`.`pid` = ?
107 AND `id`.`subscriber_relationship` = \'self\'
108 AND `id`.`provider` > 0
109 ORDER BY `id`.`date` DESC
110 ) AS `ins`
111 GROUP BY `ins`.`type`;',
112 array($patientId)
116 public function getPatientAllergiesByPatientId($patientId)
118 return sqlStatement(
119 'SELECT id, lists.title as title1, list_options.title as title2, comments
120 FROM lists
121 LEFT JOIN list_options ON lists.outcome = list_options.option_id
122 AND list_options.list_id = \'outcome\'
123 WHERE `type` = \'allergy\'
124 AND pid = ?
125 AND erx_source = \'0\'
126 AND erx_uploaded = \'0\'
127 AND (
128 enddate is NULL
129 OR enddate = \'\'
130 OR enddate = \'0000-00-00\'
131 );',
132 array($patientId)
137 * Return TTL timestamp for provided patient Id and process
138 * @param string $process SOAP process to check
139 * @param integer $patientId Patient Id to check
140 * @return string|boolean TTL timestamp of last SOAP call for provided patient Id and process
142 public function getLastSOAP($process, $patientId)
144 $return = sqlQuery(
145 'SELECT updated
146 FROM erx_ttl_touch
147 WHERE patient_id = ?
148 AND process = ?;',
149 array(
150 $patientId,
151 $process
154 if ($return === false) {
155 return false;
158 return $return['updated'];
162 * Set TTL timestamp for provided patient Id and process
163 * @param string $process SOAP process to update
164 * @param integer $patientId Patient Id to update
166 public function setLastSOAP($process, $patientId)
168 sqlQuery(
169 'REPLACE INTO erx_ttl_touch
170 SET patient_id = ?,
171 process = ?,
172 updated = NOW();',
173 array(
174 $patientId,
175 $process
181 * Update external sourced prescripts active status for provided patient Id
182 * @param integer $patientId Patient Id to update
183 * @param integer $active Active status to set for provided patient
185 public function updatePrescriptionsActiveByPatientId($patientId, $active = 0)
187 sqlQuery(
188 'UPDATE prescriptions
189 SET active = ?
190 WHERE patient_id = ?
191 AND erx_source=\'1\'',
192 array(
193 ($active == 1 ? 1 : 0),
194 $patientId
199 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId)
201 sqlQuery(
202 'UPDATE prescriptions
203 SET erx_uploaded = ?,
204 active = ?
205 WHERE patient_id = ?
206 AND id = ?;',
207 array(
208 $upload,
209 $active,
210 $patientId,
211 $prescriptionId
217 * Return prescription specified
218 * @param integer $prescriptionId Id of the prescription to return
219 * @return array Prescription information specified
221 public function getPrescriptionById($prescriptionId)
223 return sqlQuery(
224 'SELECT p.note, p.dosage, p.substitute, p.per_refill, p.form, p.route, p.size, p.interval, p.drug, p.quantity,
225 p.id AS prescid, l1.title AS title1, l2.title AS title2, l3.title AS title3, l4.title AS title4,
226 DATE_FORMAT(date_added,\'%Y%m%d\') AS date_added, CONCAT_WS(fname, \' \', mname, \' \', lname) AS docname
227 FROM prescriptions AS p
228 LEFT JOIN users AS u ON p.provider_id = u.id
229 LEFT JOIN list_options AS l1 ON l1.list_id = \'drug_form\'
230 AND l1.option_id = p.form
231 LEFT JOIN list_options AS l2 ON l2.list_id = \'drug_route\'
232 AND l2.option_id = p.route
233 LEFT JOIN list_options AS l3 ON l3.list_id = \'drug_interval\'
234 AND l3.option_id = p.interval
235 LEFT JOIN list_options AS l4 ON l4.list_id = \'drug_units\'
236 AND l4.option_id = p.unit
237 WHERE p.drug <> \'\'
238 AND p.id = ?;',
239 array($prescriptionId)
244 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit)
246 return sqlStatement(
247 'SELECT id, begdate, title
248 FROM lists
249 WHERE type = \'medication\'
250 AND pid = ?
251 AND title <> \'\'
252 AND erx_uploaded = \'0\'
253 AND (? = 0
254 OR (enddate IS NULL
255 OR enddate = \'\'
256 OR enddate = \'0000-00-00\'
259 ORDER BY enddate
260 LIMIT 0, ?;',
261 array(
262 $patientId,
263 $uploadActive,
264 $limit
269 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit)
271 return sqlStatement(
272 'SELECT id
273 FROM prescriptions
274 WHERE patient_id = ?
275 AND erx_source = \'0\'
276 AND erx_uploaded = \'0\'
277 AND (? = 0
278 OR active = 1
279 ) LIMIT 0, ?;',
280 array(
281 $patientId,
282 $uploadActive,
283 $limit,
289 * Return option Id for title text of specified list
290 * @param string $listId Id of list to reference
291 * @param string $title Title text to find
292 * @return string Option Id of selected list item
294 public function selectOptionIdByTitle($listId, $title)
296 $return = sqlQuery(
297 'SELECT option_id
298 FROM list_options
299 WHERE list_id = ? AND activity = 1
300 AND title = ?;',
301 array(
302 $listId,
303 $title
307 if (is_array($return)) {
308 $return = $return['option_id'];
311 return $return;
315 * Return highest option Id for provided list Id
316 * @param string $listId Id of list to reference
317 * @return integer Highest option Id for provided list Id
319 public function selectOptionIdsByListId($listId)
321 $return = sqlQuery(
322 'SELECT option_id
323 FROM list_options
324 WHERE list_id = ? AND activity = 1
325 ORDER BY ABS(option_id) DESC
326 LIMIT 1;',
327 array($listId)
330 if (is_array($return)) {
331 $return = $return['option_id'];
334 return $return;
338 * Return user Id by user name
339 * @param string $name Name of user to reference
340 * @return integer Id of provided user name
342 public function selectUserIdByUserName($name)
344 $return = sqlQuery(
345 'SELECT id
346 FROM users
347 WHERE username = ?;',
348 array($name)
351 return $return['id'];
355 * Insert new option to specified list
356 * @param string $listId Id of list to add option to
357 * @param string $optionId Option Id to add to referenced list
358 * @param string $title Title of option to add to new option
360 public function insertListOptions($listId, $optionId, $title)
362 sqlQuery(
363 'INSERT INTO list_options
364 (list_id, option_id, title, seq)
365 VALUES
366 (?, ?, ?, ?);',
367 array(
368 $listId,
369 $optionId,
370 $title,
371 $optionId
377 * Return Id of prescription selected by GUID and patient Id
378 * @param string $prescriptionGuid GUID of prescription
379 * @param integer $patientId Id of patient
380 * @return resource Prescription Id of specified GUID for selected patient, this resource comes from a call to mysql_query()
382 public function selectPrescriptionIdByGuidPatientId($prescriptionGuid, $patientId)
384 return sqlStatement(
385 'SELECT id
386 FROM prescriptions
387 WHERE prescriptionguid = ?
388 AND prescriptionguid IS NOT NULL
389 AND patient_id = ?;',
390 array(
391 $prescriptionGuid,
392 $patientId
398 * Insert new prescription as external sourced
399 * @param array $prescriptionData Information for creating prescription: [PrescriptionDate, DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
400 * @param integer $encounter Id of encounter for prescription
401 * @param integer $providerId Id of provider for prescription
402 * @param string $authUserId Id of user creating prescription
403 * @param integer $formOptionId Option Id for prescription form
404 * @param integer $routeOptionId Option Id for prescription route
405 * @param integer $unitsOptionId Option Id for prescription units
406 * @param integer $intervalOptionId Option Id for prescription interval
407 * @return integer Id of newly created prescription
409 public function insertPrescriptions($prescriptionData, $encounter, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
411 return sqlInsert(
412 'INSERT INTO `prescriptions`
414 `datetime`,
415 `erx_source`,
416 `encounter`,
417 `date_added`,
418 `user`,
419 `provider_id`,
420 `form`,
421 `unit`,
422 `route`,
423 `interval`,
424 `drug`,
425 `drug_id`,
426 `drug_info_erx`,
427 `dosage`,
428 `size`,
429 `refills`,
430 `note`,
431 `site`,
432 `rxnorm_drugcode`,
433 `prescriptionguid`,
434 `patient_id`
436 VALUES
438 NOW(), \'1\', ?, ?, ?,
439 ?, ?, ?, ?, ?, ?, ?, ?,
440 ?, ?, ?, ?, ?, ?, ?, ?
441 );',
442 array(
443 $encounter,
444 substr($prescriptionData['PrescriptionDate'], 0, 10),
445 $authUserId,
446 $providerId,
447 $formOptionId,
448 $unitsOptionId,
449 $routeOptionId,
450 $intervalOptionId,
451 $prescriptionData['DrugName'],
452 $prescriptionData['DrugID'],
453 $prescriptionData['DrugInfo'],
454 $prescriptionData['DosageNumberDescription'],
455 self::sanitizeNumber($prescriptionData['Strength']),
456 $prescriptionData['Refills'],
457 $prescriptionData['PrescriptionNotes'],
458 $prescriptionData['SiteID'],
459 $prescriptionData['rxcui'],
460 $prescriptionData['PrescriptionGuid'],
461 $prescriptionData['ExternalPatientID']
467 * Update prescription information as external sourced
468 * @param array $prescriptionData Information for creating prescription: [DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
469 * @param integer $providerId Id of provider for prescription
470 * @param string $authUserId Id of user creating prescription
471 * @param integer $formOptionId Option Id for prescription form
472 * @param integer $routeOptionId Option Id for prescription route
473 * @param integer $unitsOptionId Option Id for prescription units
474 * @param integer $intervalOptionId Option Id for prescription interval
476 public function updatePrescriptions($prescriptionData, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
478 sqlQuery(
479 'UPDATE prescriptions SET
480 `datetime` = NOW(),
481 `erx_source` = \'1\',
482 `active` = \'1\',
483 `user` = ?,
484 `provider_id` = ?,
485 `form` = ?,
486 `unit` = ?,
487 `route` = ?,
488 `interval` = ?,
489 `drug` = ?,
490 `drug_id` = ?,
491 `drug_info_erx` = ?,
492 `dosage` = ?,
493 `size` = ?,
494 `refills` = ?,
495 `note` = ?,
496 `site` = ?,
497 `rxnorm_drugcode` = ?
498 WHERE prescriptionguid = ?
499 AND patient_id = ?;',
500 array(
501 $authUserId,
502 $providerId,
503 $formOptionId,
504 $unitsOptionId,
505 $routeOptionId,
506 $intervalOptionId,
507 $prescriptionData['DrugName'],
508 $prescriptionData['DrugID'],
509 $prescriptionData['DrugInfo'],
510 $prescriptionData['DosageNumberDescription'],
511 self::sanitizeNumber($prescriptionData['Strength']),
512 $prescriptionData['Refills'],
513 $prescriptionData['PrescriptionNotes'],
514 $prescriptionData['SiteID'],
515 $prescriptionData['rxcui'],
516 $prescriptionData['PrescriptionGuid'],
517 $prescriptionData['ExternalPatientID']
523 * Return eRx source of specified active allergy for selected patient
524 * @param integer $patientId Id of patient to select
525 * @param string $name Name of active allergy to return
526 * @return integer eRx source flag of specified allergy for selected patient: [0 = OpenEMR, 1 = External]
528 public function selectAllergyErxSourceByPatientIdName($patientId, $name)
530 $return = sqlQuery(
531 'SELECT erx_source
532 FROM lists
533 WHERE pid = ?
534 AND type = \'allergy\'
535 AND title = ?
536 AND (
537 enddate IS NULL
538 OR enddate = \'\'
539 OR enddate = \'0000-00-00\'
540 );',
541 array(
542 $patientId,
543 $name
547 if (is_array($return)) {
548 $return = $return['erx_source'];
551 return $return;
555 * Insert new allergy as external sourced
556 * @param string $name Allergy name to insert
557 * @param integer $allergyId External allergy Id
558 * @param integer $patientId Patient Id
559 * @param integer $authUserId User Id
560 * @param integer $outcome Allergy option Id
562 public function insertAllergy($name, $allergyId, $patientId, $authUserId, $outcome)
564 sqlQuery(
565 'INSERT INTO lists
567 date, type, erx_source, begdate,
568 title, external_allergyid, pid, user, outcome
570 VALUES
572 NOW(), \'allergy\', \'1\', NOW(),
573 ?, ?, ?, ?, ?
574 );',
575 array(
576 $name,
577 $allergyId,
578 $patientId,
579 $authUserId,
580 $outcome
584 setListTouch($patientId, 'allergy');
588 * Update allergy outcome and external Id as external sourced using patient Id and allergy name
589 * @param integer $outcome Allergy outcome Id to set
590 * @param integer $externalId External allergy Id to set
591 * @param integer $patientId Patient Id to select
592 * @param string $name Allergy name to select
594 public function updateAllergyOutcomeExternalIdByPatientIdName($outcome, $externalId, $patientId, $name)
596 sqlQuery(
597 'UPDATE lists
598 SET outcome = ?,
599 erx_source = \'1\',
600 external_allergyid = ?
601 WHERE pid = ?
602 AND title = ?;',
603 array(
604 $outcome,
605 $externalId,
606 $patientId,
607 $name
613 * Update external sourced allergy outcome using patient Id, external Id, and allergy name
614 * @param integer $outcome Allergy outcome Id to set
615 * @param integer $patientId Patient Id to select
616 * @param integer $externalId External allergy Id to select
617 * @param string $name Allergy name to select
619 public function updateAllergyOutcomeByPatientIdExternalIdName($outcome, $patientId, $externalId, $name)
621 sqlQuery(
622 'UPDATE lists
623 SET outcome = ?
624 WHERE pid = ?
625 AND erx_source = \'1\'
626 AND external_allergyid = ?
627 AND title = ?;',
628 array(
629 $outcome,
630 $patientId,
631 $externalId,
632 $name
637 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId)
639 sqlQuery(
640 'UPDATE lists
641 SET erx_uploaded = ?
642 WHERE type = \'allergy\'
643 AND pid = ?
644 AND id = ?;',
645 array(
646 $uploaded,
647 $patientId,
648 $allergyId
654 * Return all external sourced active allergies for patient using patient Id
655 * @param integer $patientId Patient Id to select
656 * @return resource Patients active allergies, this resource comes from a call to mysql_query()
658 public function selectActiveAllergiesByPatientId($patientId)
660 return sqlStatement(
661 'SELECT id, title
662 FROM lists
663 WHERE pid = ?
664 AND type = \'allergy\'
665 AND erx_source = \'1\'
666 AND (
667 enddate IS NULL
668 OR enddate = \'\'
669 OR enddate = \'0000-00-00\'
670 );',
671 array($patientId)
676 * Update allergy end date for specified patient Id and list Id
677 * @param integer $patientId Id of patient to lookup
678 * @param integer $listId Id of allergy to update
680 public function updateAllergyEndDateByPatientIdListId($patientId, $listId)
682 sqlQuery(
683 'UPDATE lists
684 SET enddate = now()
685 WHERE pid = ?
686 AND id = ?
687 AND type = \'allergy\';',
688 array(
689 $patientId,
690 $listId
696 * Update eRx uploaded status using list Id
697 * @param integer $listId Id of list item
698 * @param integer $erx [optional - defaults to 0] Upload status to set: [0 = Pending NewCrop upload, 1 = Uploaded TO NewCrop]
700 public function updateErxUploadedByListId($listId, $erx = 0)
702 sqlQuery(
703 'UPDATE lists
704 SET erx_uploaded = ?
705 WHERE id = ?;',
706 array(
707 $erx,
708 $listId
714 * Return patient import status using patient Id
715 * @param integer $patientId Id of patient
716 * @return integer Import status for specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
718 public function getPatientImportStatusByPatientId($patientId)
720 $return = sqlquery(
721 'SELECT soap_import_status
722 FROM patient_data
723 WHERE pid = ?;',
724 array($patientId)
726 return $return['soap_import_status'];
730 * Update patient import status using patient Id
731 * @param integer $patientId Id of patient to update
732 * @param integer $status Import status to update specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
734 public function updatePatientImportStatusByPatientId($patientId, $status)
736 sqlQuery(
737 'UPDATE patient_data
738 SET soap_import_status = ?
739 WHERE pid = ?;',
740 array(
741 $status,
742 $patientId