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");
20 * Strip away any non numerical characters
21 * @param string $value Value to sanitize
22 * @return string Value sanitized of all non numerical characters
24 public static function sanitizeNumber($value)
26 return preg_replace('/[^-0-9.]/', '', $value);
30 * Return the primary business entity
31 * @return array Primary business entity
33 public function getFacilityPrimary()
35 $return = sqlQuery('SELECT `name`, `federal_ein`, `street`, `city`, `state`, `postal_code`, `country_code`, `phone`, `fax`
37 WHERE `primary_business_entity` = \'1\';');
43 * Return the Federal EIN established with the primary business entity
44 * @return string Federal EIN for the primary business entity
46 public function selectFederalEin()
48 $return = $this->getFacilityPrimary();
50 return $return['federal_ein'];
54 * Return user information using user Id
55 * @param integer $id Id of user to return
56 * @return array Specified user information: index [id, username, lname, fname, mname, title, license, federaldrugid, upin, state_license_number, npi, newcrop_user_role]
58 public function getUserById($id)
61 'SELECT id, username, lname, fname, mname, title, federaldrugid, upin, state_license_number, npi, newcrop_user_role
69 * Return user facility business entity
70 * @param integer $id Id of user to return
71 * @return array User facility business entity
73 public function getUserFacility($id)
76 'SELECT facility.id, facility.name, facility.street, facility.city, facility.state, facility.postal_code, facility.country_code, facility.phone, facility.fax
78 LEFT JOIN facility ON facility.id = users.facility_id
85 * Return patient information using patient Id
86 * @param integer $patientId Id of patient
87 * @return array Specified patient information: index [pid, fname, mname, lname, street, city, state, postal_code, country_code, phone_home, date_of_birth, sex]
89 public function getPatientByPatientId($patientId)
92 '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
99 public function getPatientVitalsByPatientId($patientId)
102 "SELECT FORM_VITALS.date, FORM_VITALS.id
103 FROM form_vitals AS FORM_VITALS LEFT JOIN forms AS FORMS ON FORM_VITALS.id = FORMS.form_id
104 WHERE FORM_VITALS.pid=? AND FORMS.deleted != '1'
105 ORDER BY FORM_VITALS.date DESC",
109 $data = formFetch("form_vitals", $result['id']);
111 $weight = number_format($data['weight'] * 0.45359237, 2);
112 $height = round(number_format($data['height'] * 2.54, 2), 1);
116 'height_units' => 'cm',
118 'weight_units' => 'kg'
122 public function getPatientHealthplansByPatientId($patientId)
130 FROM `insurance_data` AS `id`
131 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
133 AND `id`.`subscriber_relationship` = \'self\'
134 AND `id`.`provider` > 0
135 ORDER BY `id`.`date` DESC
137 GROUP BY `ins`.`type`;',
142 public function getPatientAllergiesByPatientId($patientId)
145 'SELECT id, lists.title as title1, list_options.title as title2, comments
147 LEFT JOIN list_options ON lists.outcome = list_options.option_id
148 AND list_options.list_id = \'outcome\'
149 WHERE `type` = \'allergy\'
151 AND erx_source = \'0\'
152 AND erx_uploaded = \'0\'
156 OR enddate = \'0000-00-00\'
163 * Return TTL timestamp for provided patient Id and process
164 * @param string $process SOAP process to check
165 * @param integer $patientId Patient Id to check
166 * @return string|boolean TTL timestamp of last SOAP call for provided patient Id and process
168 public function getLastSOAP($process, $patientId)
180 if ($return === false) {
184 return $return['updated'];
188 * Set TTL timestamp for provided patient Id and process
189 * @param string $process SOAP process to update
190 * @param integer $patientId Patient Id to update
192 public function setLastSOAP($process, $patientId)
195 'REPLACE INTO erx_ttl_touch
207 * Update external sourced prescripts active status for provided patient Id
208 * @param integer $patientId Patient Id to update
209 * @param integer $active Active status to set for provided patient
211 public function updatePrescriptionsActiveByPatientId($patientId, $active = 0)
214 'UPDATE prescriptions
217 AND erx_source=\'1\'',
219 ($active == 1 ?
1 : 0),
225 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId)
228 'UPDATE prescriptions
229 SET erx_uploaded = ?,
243 * Return prescription specified
244 * @param integer $prescriptionId Id of the prescription to return
245 * @return array Prescription information specified
247 public function getPrescriptionById($prescriptionId)
250 'SELECT p.note, p.dosage, p.substitute, p.per_refill, p.form, p.route, p.size, p.interval, p.drug, p.quantity,
251 p.id AS prescid, l1.title AS title1, l2.title AS title2, l3.title AS title3, l4.title AS title4,
252 DATE_FORMAT(date_added,\'%Y%m%d\') AS date_added, CONCAT_WS(fname, \' \', mname, \' \', lname) AS docname
253 FROM prescriptions AS p
254 LEFT JOIN users AS u ON p.provider_id = u.id
255 LEFT JOIN list_options AS l1 ON l1.list_id = \'drug_form\'
256 AND l1.option_id = p.form
257 LEFT JOIN list_options AS l2 ON l2.list_id = \'drug_route\'
258 AND l2.option_id = p.route
259 LEFT JOIN list_options AS l3 ON l3.list_id = \'drug_interval\'
260 AND l3.option_id = p.interval
261 LEFT JOIN list_options AS l4 ON l4.list_id = \'drug_units\'
262 AND l4.option_id = p.unit
265 array($prescriptionId)
270 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit)
273 'SELECT id, begdate, title
275 WHERE type = \'medication\'
278 AND erx_uploaded = \'0\'
282 OR enddate = \'0000-00-00\'
295 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit)
301 AND erx_source = \'0\'
302 AND erx_uploaded = \'0\'
315 * Return option Id for title text of specified list
316 * @param string $listId Id of list to reference
317 * @param string $title Title text to find
318 * @return string Option Id of selected list item
320 public function selectOptionIdByTitle($listId, $title)
325 WHERE list_id = ? AND activity = 1
333 if (is_array($return)) {
334 $return = $return['option_id'];
341 * Return highest option Id for provided list Id
342 * @param string $listId Id of list to reference
343 * @return integer Highest option Id for provided list Id
345 public function selectOptionIdsByListId($listId)
350 WHERE list_id = ? AND activity = 1
351 ORDER BY ABS(option_id) DESC
356 if (is_array($return)) {
357 $return = $return['option_id'];
364 * Return user Id by user name
365 * @param string $name Name of user to reference
366 * @return integer Id of provided user name
368 public function selectUserIdByUserName($name)
373 WHERE username = ?;',
377 return $return['id'];
381 * Insert new option to specified list
382 * @param string $listId Id of list to add option to
383 * @param string $optionId Option Id to add to referenced list
384 * @param string $title Title of option to add to new option
386 public function insertListOptions($listId, $optionId, $title)
389 'INSERT INTO list_options
390 (list_id, option_id, title, seq)
403 * Return Id of prescription selected by GUID and patient Id
404 * @param string $prescriptionGuid GUID of prescription
405 * @param integer $patientId Id of patient
406 * @return resource Prescription Id of specified GUID for selected patient, this resource comes from a call to mysql_query()
408 public function selectPrescriptionIdByGuidPatientId($prescriptionGuid, $patientId)
413 WHERE prescriptionguid = ?
414 AND prescriptionguid IS NOT NULL
415 AND patient_id = ?;',
424 * Insert new prescription as external sourced
425 * @param array $prescriptionData Information for creating prescription: [PrescriptionDate, DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
426 * @param integer $encounter Id of encounter for prescription
427 * @param integer $providerId Id of provider for prescription
428 * @param string $authUserId Id of user creating prescription
429 * @param integer $formOptionId Option Id for prescription form
430 * @param integer $routeOptionId Option Id for prescription route
431 * @param integer $unitsOptionId Option Id for prescription units
432 * @param integer $intervalOptionId Option Id for prescription interval
433 * @return integer Id of newly created prescription
435 public function insertPrescriptions($prescriptionData, $encounter, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
438 'INSERT INTO `prescriptions`
464 NOW(), \'1\', ?, ?, ?,
465 ?, ?, ?, ?, ?, ?, ?, ?,
466 ?, ?, ?, ?, ?, ?, ?, ?
470 substr($prescriptionData['PrescriptionDate'], 0, 10),
477 $prescriptionData['DrugName'],
478 $prescriptionData['DrugID'],
479 $prescriptionData['DrugInfo'],
480 $prescriptionData['DosageNumberDescription'],
481 self
::sanitizeNumber($prescriptionData['Strength']),
482 $prescriptionData['Refills'],
483 $prescriptionData['PrescriptionNotes'],
484 $prescriptionData['SiteID'],
485 $prescriptionData['rxcui'],
486 $prescriptionData['PrescriptionGuid'],
487 $prescriptionData['ExternalPatientID']
493 * Update prescription information as external sourced
494 * @param array $prescriptionData Information for creating prescription: [DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
495 * @param integer $providerId Id of provider for prescription
496 * @param string $authUserId Id of user creating prescription
497 * @param integer $formOptionId Option Id for prescription form
498 * @param integer $routeOptionId Option Id for prescription route
499 * @param integer $unitsOptionId Option Id for prescription units
500 * @param integer $intervalOptionId Option Id for prescription interval
502 public function updatePrescriptions($prescriptionData, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
505 'UPDATE prescriptions SET
507 `erx_source` = \'1\',
523 `rxnorm_drugcode` = ?
524 WHERE prescriptionguid = ?
525 AND patient_id = ?;',
533 $prescriptionData['DrugName'],
534 $prescriptionData['DrugID'],
535 $prescriptionData['DrugInfo'],
536 $prescriptionData['DosageNumberDescription'],
537 self
::sanitizeNumber($prescriptionData['Strength']),
538 $prescriptionData['Refills'],
539 $prescriptionData['PrescriptionNotes'],
540 $prescriptionData['SiteID'],
541 $prescriptionData['rxcui'],
542 $prescriptionData['PrescriptionGuid'],
543 $prescriptionData['ExternalPatientID']
549 * Return eRx source of specified active allergy for selected patient
550 * @param integer $patientId Id of patient to select
551 * @param string $name Name of active allergy to return
552 * @return integer eRx source flag of specified allergy for selected patient: [0 = OpenEMR, 1 = External]
554 public function selectAllergyErxSourceByPatientIdName($patientId, $name)
560 AND type = \'allergy\'
565 OR enddate = \'0000-00-00\'
573 if (is_array($return)) {
574 $return = $return['erx_source'];
581 * Insert new allergy as external sourced
582 * @param string $name Allergy name to insert
583 * @param integer $allergyId External allergy Id
584 * @param integer $patientId Patient Id
585 * @param integer $authUserId User Id
586 * @param integer $outcome Allergy option Id
588 public function insertAllergy($name, $allergyId, $patientId, $authUserId, $outcome)
593 date, type, erx_source, begdate,
594 title, external_allergyid, pid, user, outcome
598 NOW(), \'allergy\', \'1\', NOW(),
610 setListTouch($patientId, 'allergy');
614 * Update allergy outcome and external Id as external sourced using patient Id and allergy name
615 * @param integer $outcome Allergy outcome Id to set
616 * @param integer $externalId External allergy Id to set
617 * @param integer $patientId Patient Id to select
618 * @param string $name Allergy name to select
620 public function updateAllergyOutcomeExternalIdByPatientIdName($outcome, $externalId, $patientId, $name)
626 external_allergyid = ?
639 * Update external sourced allergy outcome using patient Id, external Id, and allergy name
640 * @param integer $outcome Allergy outcome Id to set
641 * @param integer $patientId Patient Id to select
642 * @param integer $externalId External allergy Id to select
643 * @param string $name Allergy name to select
645 public function updateAllergyOutcomeByPatientIdExternalIdName($outcome, $patientId, $externalId, $name)
651 AND erx_source = \'1\'
652 AND external_allergyid = ?
663 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId)
668 WHERE type = \'allergy\'
680 * Return all external sourced active allergies for patient using patient Id
681 * @param integer $patientId Patient Id to select
682 * @return resource Patients active allergies, this resource comes from a call to mysql_query()
684 public function selectActiveAllergiesByPatientId($patientId)
690 AND type = \'allergy\'
691 AND erx_source = \'1\'
695 OR enddate = \'0000-00-00\'
702 * Update allergy end date for specified patient Id and list Id
703 * @param integer $patientId Id of patient to lookup
704 * @param integer $listId Id of allergy to update
706 public function updateAllergyEndDateByPatientIdListId($patientId, $listId)
713 AND type = \'allergy\';',
722 * Update eRx uploaded status using list Id
723 * @param integer $listId Id of list item
724 * @param integer $erx [optional - defaults to 0] Upload status to set: [0 = Pending NewCrop upload, 1 = Uploaded TO NewCrop]
726 public function updateErxUploadedByListId($listId, $erx = 0)
740 * Return patient import status using patient Id
741 * @param integer $patientId Id of patient
742 * @return integer Import status for specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
744 public function getPatientImportStatusByPatientId($patientId)
747 'SELECT soap_import_status
752 return $return['soap_import_status'];
756 * Update patient import status using patient Id
757 * @param integer $patientId Id of patient to update
758 * @param integer $status Import status to update specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
760 public function updatePatientImportStatusByPatientId($patientId, $status)
764 SET soap_import_status = ?