4 * interface/eRxStore.php Functions for interacting with NewCrop database.
6 * Copyright (C) 2013-2015 Sam Likins <sam.likins@wsi-services.com>
8 * LICENSE: This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
11 * later version. This program is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details. You should have received a copy of the GNU
15 * General Public License along with this program.
16 * If not, see <http://opensource.org/licenses/gpl-license.php>.
20 * @author Sam Likins <sam.likins@wsi-services.com>
21 * @link http://www.open-emr.org
28 * Strip away any non numerical characters
29 * @param string $value Value to sanitize
30 * @return string Value sanitized of all non numerical characters
32 public static function sanitizeNumber($value)
34 return preg_replace('/[^-0-9.]/', '', $value);
38 * Return the primary business entity
39 * @return array Primary business entity
41 public function getFacilityPrimary()
43 $return = sqlQuery('SELECT `name`, `federal_ein`, `street`, `city`, `state`, `postal_code`, `country_code`, `phone`, `fax`
45 WHERE `primary_business_entity` = \'1\';');
51 * Return the Federal EIN established with the primary business entity
52 * @return string Federal EIN for the primary business entity
54 public function selectFederalEin()
56 $return = $this->getFacilityPrimary();
58 return $return['federal_ein'];
62 * Return user information using user Id
63 * @param integer $id Id of user to return
64 * @return array Specified user information: index [id, username, lname, fname, mname, title, license, federaldrugid, upin, state_license_number, npi, newcrop_user_role]
66 public function getUserById($id)
69 'SELECT id, username, lname, fname, mname, title, federaldrugid, upin, state_license_number, npi, newcrop_user_role
77 * Return user facility business entity
78 * @param integer $id Id of user to return
79 * @return array User facility business entity
81 public function getUserFacility($id)
84 'SELECT facility.id, facility.name, facility.street, facility.city, facility.state, facility.postal_code, facility.country_code, facility.phone, facility.fax
86 LEFT JOIN facility ON facility.id = users.facility_id
93 * Return patient information using patient Id
94 * @param integer $patientId Id of patient
95 * @return array Specified patient information: index [pid, fname, mname, lname, street, city, state, postal_code, country_code, phone_home, date_of_birth, sex]
97 public function getPatientByPatientId($patientId)
100 '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
107 public function getPatientHealthplansByPatientId($patientId)
115 FROM `insurance_data` AS `id`
116 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
118 AND `id`.`subscriber_relationship` = \'self\'
119 AND `id`.`provider` > 0
120 ORDER BY `id`.`date` DESC
122 GROUP BY `ins`.`type`;',
127 public function getPatientAllergiesByPatientId($patientId)
130 'SELECT id, lists.title as title1, list_options.title as title2, comments
132 LEFT JOIN list_options ON lists.outcome = list_options.option_id
133 AND list_options.list_id = \'outcome\'
134 WHERE `type` = \'allergy\'
136 AND erx_source = \'0\'
137 AND erx_uploaded = \'0\'
141 OR enddate = \'0000-00-00\'
148 * Return TTL timestamp for provided patient Id and process
149 * @param string $process SOAP process to check
150 * @param integer $patientId Patient Id to check
151 * @return string|boolean TTL timestamp of last SOAP call for provided patient Id and process
153 public function getLastSOAP($process, $patientId)
165 if ($return === false) {
169 return $return['updated'];
173 * Set TTL timestamp for provided patient Id and process
174 * @param string $process SOAP process to update
175 * @param integer $patientId Patient Id to update
177 public function setLastSOAP($process, $patientId)
180 'REPLACE INTO erx_ttl_touch
192 * Update external sourced prescripts active status for provided patient Id
193 * @param integer $patientId Patient Id to update
194 * @param integer $active Active status to set for provided patient
196 public function updatePrescriptionsActiveByPatientId($patientId, $active = 0)
199 'UPDATE prescriptions
202 AND erx_source=\'1\'',
204 ($active == 1 ?
1 : 0),
210 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId)
213 'UPDATE prescriptions
214 SET erx_uploaded = ?,
228 * Return prescription specified
229 * @param integer $prescriptionId Id of the prescription to return
230 * @return array Prescription information specified
232 public function getPrescriptionById($prescriptionId)
235 'SELECT p.note, p.dosage, p.substitute, p.per_refill, p.form, p.route, p.size, p.interval, p.drug, p.quantity,
236 p.id AS prescid, l1.title AS title1, l2.title AS title2, l3.title AS title3, l4.title AS title4,
237 DATE_FORMAT(date_added,\'%Y%m%d\') AS date_added, CONCAT_WS(fname, \' \', mname, \' \', lname) AS docname
238 FROM prescriptions AS p
239 LEFT JOIN users AS u ON p.provider_id = u.id
240 LEFT JOIN list_options AS l1 ON l1.list_id = \'drug_form\'
241 AND l1.option_id = p.form
242 LEFT JOIN list_options AS l2 ON l2.list_id = \'drug_route\'
243 AND l2.option_id = p.route
244 LEFT JOIN list_options AS l3 ON l3.list_id = \'drug_interval\'
245 AND l3.option_id = p.interval
246 LEFT JOIN list_options AS l4 ON l4.list_id = \'drug_units\'
247 AND l4.option_id = p.unit
250 array($prescriptionId)
255 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit)
258 'SELECT id, begdate, title
260 WHERE type = \'medication\'
263 AND erx_uploaded = \'0\'
267 OR enddate = \'0000-00-00\'
280 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit)
286 AND erx_source = \'0\'
287 AND erx_uploaded = \'0\'
300 * Return option Id for title text of specified list
301 * @param string $listId Id of list to reference
302 * @param string $title Title text to find
303 * @return string Option Id of selected list item
305 public function selectOptionIdByTitle($listId, $title)
310 WHERE list_id = ? AND activity = 1
318 if (is_array($return)) {
319 $return = $return['option_id'];
326 * Return highest option Id for provided list Id
327 * @param string $listId Id of list to reference
328 * @return integer Highest option Id for provided list Id
330 public function selectOptionIdsByListId($listId)
335 WHERE list_id = ? AND activity = 1
336 ORDER BY ABS(option_id) DESC
341 if (is_array($return)) {
342 $return = $return['option_id'];
349 * Return user Id by user name
350 * @param string $name Name of user to reference
351 * @return integer Id of provided user name
353 public function selectUserIdByUserName($name)
358 WHERE username = ?;',
362 return $return['id'];
366 * Insert new option to specified list
367 * @param string $listId Id of list to add option to
368 * @param string $optionId Option Id to add to referenced list
369 * @param string $title Title of option to add to new option
371 public function insertListOptions($listId, $optionId, $title)
374 'INSERT INTO list_options
375 (list_id, option_id, title, seq)
388 * Return Id of prescription selected by GUID and patient Id
389 * @param string $prescriptionGuid GUID of prescription
390 * @param integer $patientId Id of patient
391 * @return resource Prescription Id of specified GUID for selected patient, this resource comes from a call to mysql_query()
393 public function selectPrescriptionIdByGuidPatientId($prescriptionGuid, $patientId)
398 WHERE prescriptionguid = ?
399 AND prescriptionguid IS NOT NULL
400 AND patient_id = ?;',
409 * Insert new prescription as external sourced
410 * @param array $prescriptionData Information for creating prescription: [PrescriptionDate, DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
411 * @param integer $encounter Id of encounter for prescription
412 * @param integer $providerId Id of provider for prescription
413 * @param string $authUserId Id of user creating prescription
414 * @param integer $formOptionId Option Id for prescription form
415 * @param integer $routeOptionId Option Id for prescription route
416 * @param integer $unitsOptionId Option Id for prescription units
417 * @param integer $intervalOptionId Option Id for prescription interval
418 * @return integer Id of newly created prescription
420 public function insertPrescriptions($prescriptionData, $encounter, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
423 'INSERT INTO `prescriptions`
449 NOW(), \'1\', ?, ?, ?,
450 ?, ?, ?, ?, ?, ?, ?, ?,
451 ?, ?, ?, ?, ?, ?, ?, ?
455 substr($prescriptionData['PrescriptionDate'], 0, 10),
462 $prescriptionData['DrugName'],
463 $prescriptionData['DrugID'],
464 $prescriptionData['DrugInfo'],
465 $prescriptionData['DosageNumberDescription'],
466 self
::sanitizeNumber($prescriptionData['Strength']),
467 $prescriptionData['Refills'],
468 $prescriptionData['PrescriptionNotes'],
469 $prescriptionData['SiteID'],
470 $prescriptionData['rxcui'],
471 $prescriptionData['PrescriptionGuid'],
472 $prescriptionData['ExternalPatientID']
478 * Update prescription information as external sourced
479 * @param array $prescriptionData Information for creating prescription: [DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
480 * @param integer $providerId Id of provider for prescription
481 * @param string $authUserId Id of user creating prescription
482 * @param integer $formOptionId Option Id for prescription form
483 * @param integer $routeOptionId Option Id for prescription route
484 * @param integer $unitsOptionId Option Id for prescription units
485 * @param integer $intervalOptionId Option Id for prescription interval
487 public function updatePrescriptions($prescriptionData, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
490 'UPDATE prescriptions SET
492 `erx_source` = \'1\',
508 `rxnorm_drugcode` = ?
509 WHERE prescriptionguid = ?
510 AND patient_id = ?;',
518 $prescriptionData['DrugName'],
519 $prescriptionData['DrugID'],
520 $prescriptionData['DrugInfo'],
521 $prescriptionData['DosageNumberDescription'],
522 self
::sanitizeNumber($prescriptionData['Strength']),
523 $prescriptionData['Refills'],
524 $prescriptionData['PrescriptionNotes'],
525 $prescriptionData['SiteID'],
526 $prescriptionData['rxcui'],
527 $prescriptionData['PrescriptionGuid'],
528 $prescriptionData['ExternalPatientID']
534 * Return eRx source of specified active allergy for selected patient
535 * @param integer $patientId Id of patient to select
536 * @param string $name Name of active allergy to return
537 * @return integer eRx source flag of specified allergy for selected patient: [0 = OpenEMR, 1 = External]
539 public function selectAllergyErxSourceByPatientIdName($patientId, $name)
545 AND type = \'allergy\'
550 OR enddate = \'0000-00-00\'
558 if (is_array($return)) {
559 $return = $return['erx_source'];
566 * Insert new allergy as external sourced
567 * @param string $name Allergy name to insert
568 * @param integer $allergyId External allergy Id
569 * @param integer $patientId Patient Id
570 * @param integer $authUserId User Id
571 * @param integer $outcome Allergy option Id
573 public function insertAllergy($name, $allergyId, $patientId, $authUserId, $outcome)
578 date, type, erx_source, begdate,
579 title, external_allergyid, pid, user, outcome
583 NOW(), \'allergy\', \'1\', NOW(),
595 setListTouch($patientId, 'allergy');
599 * Update allergy outcome and external Id as external sourced using patient Id and allergy name
600 * @param integer $outcome Allergy outcome Id to set
601 * @param integer $externalId External allergy Id to set
602 * @param integer $patientId Patient Id to select
603 * @param string $name Allergy name to select
605 public function updateAllergyOutcomeExternalIdByPatientIdName($outcome, $externalId, $patientId, $name)
611 external_allergyid = ?
624 * Update external sourced allergy outcome using patient Id, external Id, and allergy name
625 * @param integer $outcome Allergy outcome Id to set
626 * @param integer $patientId Patient Id to select
627 * @param integer $externalId External allergy Id to select
628 * @param string $name Allergy name to select
630 public function updateAllergyOutcomeByPatientIdExternalIdName($outcome, $patientId, $externalId, $name)
636 AND erx_source = \'1\'
637 AND external_allergyid = ?
648 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId)
653 WHERE type = \'allergy\'
665 * Return all external sourced active allergies for patient using patient Id
666 * @param integer $patientId Patient Id to select
667 * @return resource Patients active allergies, this resource comes from a call to mysql_query()
669 public function selectActiveAllergiesByPatientId($patientId)
675 AND type = \'allergy\'
676 AND erx_source = \'1\'
680 OR enddate = \'0000-00-00\'
687 * Update allergy end date for specified patient Id and list Id
688 * @param integer $patientId Id of patient to lookup
689 * @param integer $listId Id of allergy to update
691 public function updateAllergyEndDateByPatientIdListId($patientId, $listId)
698 AND type = \'allergy\';',
707 * Update eRx uploaded status using list Id
708 * @param integer $listId Id of list item
709 * @param integer $erx [optional - defaults to 0] Upload status to set: [0 = Pending NewCrop upload, 1 = Uploaded TO NewCrop]
711 public function updateErxUploadedByListId($listId, $erx = 0)
725 * Return patient import status using patient Id
726 * @param integer $patientId Id of patient
727 * @return integer Import status for specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
729 public function getPatientImportStatusByPatientId($patientId)
732 'SELECT soap_import_status
737 return $return['soap_import_status'];
741 * Update patient import status using patient Id
742 * @param integer $patientId Id of patient to update
743 * @param integer $status Import status to update specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
745 public function updatePatientImportStatusByPatientId($patientId, $status)
749 SET soap_import_status = ?