3 * interface/eRxStore.php Functions for interacting with NewCrop database.
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
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);
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`
34 WHERE `primary_business_entity` = \'1\';');
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'];
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)
58 'SELECT id, username, lname, fname, mname, title, federaldrugid, upin, state_license_number, npi, newcrop_user_role
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)
73 'SELECT facility.id, facility.name, facility.street, facility.city, facility.state, facility.postal_code, facility.country_code, facility.phone, facility.fax
75 LEFT JOIN facility ON facility.id = users.facility_id
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)
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
96 public function getPatientHealthplansByPatientId($patientId)
104 FROM `insurance_data` AS `id`
105 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
107 AND `id`.`subscriber_relationship` = \'self\'
108 AND `id`.`provider` > 0
109 ORDER BY `id`.`date` DESC
111 GROUP BY `ins`.`type`;',
116 public function getPatientAllergiesByPatientId($patientId)
119 'SELECT id, lists.title as title1, list_options.title as title2, comments
121 LEFT JOIN list_options ON lists.outcome = list_options.option_id
122 AND list_options.list_id = \'outcome\'
123 WHERE `type` = \'allergy\'
125 AND erx_source = \'0\'
126 AND erx_uploaded = \'0\'
130 OR enddate = \'0000-00-00\'
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)
154 if ($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)
169 'REPLACE INTO erx_ttl_touch
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)
188 'UPDATE prescriptions
191 AND erx_source=\'1\'',
193 ($active == 1 ?
1 : 0),
199 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId)
202 'UPDATE prescriptions
203 SET erx_uploaded = ?,
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)
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
239 array($prescriptionId)
244 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit)
247 'SELECT id, begdate, title
249 WHERE type = \'medication\'
252 AND erx_uploaded = \'0\'
256 OR enddate = \'0000-00-00\'
269 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit)
275 AND erx_source = \'0\'
276 AND erx_uploaded = \'0\'
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)
299 WHERE list_id = ? AND activity = 1
307 if (is_array($return)) {
308 $return = $return['option_id'];
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)
324 WHERE list_id = ? AND activity = 1
325 ORDER BY ABS(option_id) DESC
330 if (is_array($return)) {
331 $return = $return['option_id'];
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)
347 WHERE username = ?;',
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)
363 'INSERT INTO list_options
364 (list_id, option_id, title, seq)
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)
387 WHERE prescriptionguid = ?
388 AND prescriptionguid IS NOT NULL
389 AND patient_id = ?;',
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)
412 'INSERT INTO `prescriptions`
438 NOW(), \'1\', ?, ?, ?,
439 ?, ?, ?, ?, ?, ?, ?, ?,
440 ?, ?, ?, ?, ?, ?, ?, ?
444 substr($prescriptionData['PrescriptionDate'], 0, 10),
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)
479 'UPDATE prescriptions SET
481 `erx_source` = \'1\',
497 `rxnorm_drugcode` = ?
498 WHERE prescriptionguid = ?
499 AND patient_id = ?;',
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)
534 AND type = \'allergy\'
539 OR enddate = \'0000-00-00\'
547 if (is_array($return)) {
548 $return = $return['erx_source'];
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)
567 date, type, erx_source, begdate,
568 title, external_allergyid, pid, user, outcome
572 NOW(), \'allergy\', \'1\', NOW(),
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)
600 external_allergyid = ?
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)
625 AND erx_source = \'1\'
626 AND external_allergyid = ?
637 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId)
642 WHERE type = \'allergy\'
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)
664 AND type = \'allergy\'
665 AND erx_source = \'1\'
669 OR enddate = \'0000-00-00\'
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)
687 AND type = \'allergy\';',
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)
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)
721 'SELECT soap_import_status
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)
738 SET soap_import_status = ?