4 * interface/eRxStore.php Functions for interacting with NewCrop database.
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");
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)
26 if ($value !== null) {
27 $sanitized = preg_replace('/[^-0-9.]/', '', $value);
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`
41 WHERE `primary_business_entity` = \'1\';');
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'];
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)
65 'SELECT id, username, lname, fname, mname, title, federaldrugid, upin, state_license_number, npi, newcrop_user_role
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)
80 'SELECT facility.id, facility.name, facility.street, facility.city, facility.state, facility.postal_code, facility.country_code, facility.phone, facility.fax
82 LEFT JOIN facility ON facility.id = users.facility_id
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)
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
103 public function getPatientVitalsByPatientId($patientId)
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",
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);
120 'height_units' => 'cm',
122 'weight_units' => 'kg'
126 public function getPatientHealthplansByPatientId($patientId)
134 FROM `insurance_data` AS `id`
135 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
137 AND `id`.`subscriber_relationship` = \'self\'
138 AND `id`.`provider` > 0
139 ORDER BY `id`.`date` DESC
141 GROUP BY `ins`.`type`;',
146 public function getPatientAllergiesByPatientId($patientId)
149 'SELECT id, lists.title as title1, list_options.title as title2, comments
151 LEFT JOIN list_options ON lists.outcome = list_options.option_id
152 AND list_options.list_id = \'outcome\'
153 WHERE `type` = \'allergy\'
155 AND erx_source = \'0\'
156 AND erx_uploaded = \'0\'
159 OR enddate = \'0000-00-00\'
165 public function getPatientDiagnosisByPatientId($patientId)
168 'SELECT diagnosis, begdate, enddate, title, date
170 WHERE `type` = \'medical_problem\'
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)
195 if ($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)
210 'REPLACE INTO erx_ttl_touch
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)
229 'UPDATE prescriptions
232 AND erx_source=\'1\'',
234 ($active == 1 ?
1 : 0),
240 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId)
243 'UPDATE prescriptions
244 SET erx_uploaded = ?,
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)
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
280 array($prescriptionId)
285 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit)
288 'SELECT id, begdate, title
290 WHERE type = \'medication\'
293 AND erx_uploaded = \'0\'
296 OR enddate = \'0000-00-00\'
309 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit)
315 AND erx_source = \'0\'
316 AND erx_uploaded = \'0\'
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)
339 WHERE list_id = ? AND activity = 1
347 if (is_array($return)) {
348 $return = $return['option_id'];
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)
364 WHERE list_id = ? AND activity = 1
365 ORDER BY ABS(option_id) DESC
370 if (is_array($return)) {
371 $return = $return['option_id'];
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)
387 WHERE username = ?;',
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)
403 'INSERT INTO list_options
404 (list_id, option_id, title, seq)
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)
427 WHERE prescriptionguid = ?
428 AND prescriptionguid IS NOT NULL
429 AND patient_id = ?;',
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)
452 'INSERT INTO `prescriptions`
478 NOW(), \'1\', ?, ?, ?,
479 ?, ?, ?, ?, ?, ?, ?, ?,
480 ?, ?, ?, ?, ?, ?, ?, ?
484 substr($prescriptionData['PrescriptionDate'], 0, 10),
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)
519 'UPDATE prescriptions SET
521 `erx_source` = \'1\',
537 `rxnorm_drugcode` = ?
538 WHERE prescriptionguid = ?
539 AND patient_id = ?;',
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)
574 AND type = \'allergy\'
578 OR enddate = \'0000-00-00\'
586 if (is_array($return)) {
587 $return = $return['erx_source'];
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)
606 date, type, erx_source, begdate,
607 title, external_allergyid, pid, user, outcome
611 NOW(), \'allergy\', \'1\', NOW(),
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)
639 external_allergyid = ?
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)
664 AND erx_source = \'1\'
665 AND external_allergyid = ?
676 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId)
681 WHERE type = \'allergy\'
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)
703 AND type = \'allergy\'
704 AND erx_source = \'1\'
707 OR enddate = \'0000-00-00\'
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)
725 AND type = \'allergy\';',
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)
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)
759 'SELECT soap_import_status
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)
776 SET soap_import_status = ?