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
27 * Strip away any non numerical characters
28 * @param string $value Value to sanitize
29 * @return string Value sanitized of all non numerical characters
31 static public function sanitizeNumber($value) {
32 return preg_replace('/[^-0-9.]/', '', $value);
36 * Return the primary business entity
37 * @return array Primary business entity
39 public function getFacilityPrimary() {
40 $return = sqlQuery('SELECT `name`, `federal_ein`, `street`, `city`, `state`, `postal_code`, `country_code`, `phone`, `fax`
42 WHERE `primary_business_entity` = \'1\';'
49 * Return the Federal EIN established with the primary business entity
50 * @return string Federal EIN for the primary business entity
52 public function selectFederalEin() {
53 $return = $this->getFacilityPrimary();
55 return $return['federal_ein'];
59 * Return user information using user Id
60 * @param integer $id Id of user to return
61 * @return array Specified user information: index [id, username, lname, fname, mname, title, license, federaldrugid, upin, state_license_number, npi, newcrop_user_role]
63 public function getUserById($id) {
64 return sqlQuery('SELECT id, username, lname, fname, mname, title, federaldrugid, upin, state_license_number, npi, newcrop_user_role
72 * Return user facility business entity
73 * @param integer $id Id of user to return
74 * @return array User facility business entity
76 public function getUserFacility($id) {
77 return sqlQuery('SELECT facility.id, facility.name, facility.street, facility.city, facility.state, facility.postal_code, facility.country_code, facility.phone, facility.fax
79 LEFT JOIN facility ON facility.id = users.facility_id
86 * Return patient information using patient Id
87 * @param integer $patientId Id of patient
88 * @return array Specified patient information: index [pid, fname, mname, lname, street, city, state, postal_code, country_code, phone_home, date_of_birth, sex]
90 public function getPatientByPatientId($patientId) {
91 return sqlQuery('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
98 public function getPatientHealthplansByPatientId($patientId) {
99 return sqlStatement('SELECT `ins`.`name`
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) {
117 return sqlStatement('SELECT id, lists.title as title1, list_options.title as title2, comments
119 LEFT JOIN list_options ON lists.outcome = list_options.option_id
120 AND list_options.list_id = \'outcome\'
121 WHERE `type` = \'allergy\'
123 AND erx_source = \'0\'
124 AND erx_uploaded = \'0\'
128 OR enddate = \'0000-00-00\'
136 * Return TTL timestamp for provided patient Id and process
137 * @param string $process SOAP process to check
138 * @param integer $patientId Patient Id to check
139 * @return string|boolean TTL timestamp of last SOAP call for provided patient Id and process
141 public function getLastSOAP($process, $patientId) {
142 $return = sqlQuery('SELECT updated
151 if($return === false)
154 return $return['updated'];
158 * Set TTL timestamp for provided patient Id and process
159 * @param string $process SOAP process to update
160 * @param integer $patientId Patient Id to update
162 public function setLastSOAP($process, $patientId) {
163 sqlQuery('REPLACE INTO erx_ttl_touch
175 * Update external sourced prescripts active status for provided patient Id
176 * @param integer $patientId Patient Id to update
177 * @param integer $active Active status to set for provided patient
179 public function updatePrescriptionsActiveByPatientId($patientId, $active = 0) {
180 sqlQuery('UPDATE prescriptions
183 AND erx_source=\'1\'',
185 ($active == 1 ?
1 : 0),
191 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId) {
192 sqlQuery('UPDATE prescriptions
193 SET erx_uploaded = ?,
207 * Return prescription specified
208 * @param integer $prescriptionId Id of the prescription to return
209 * @return array Prescription information specified
211 public function getPrescriptionById($prescriptionId) {
212 return sqlQuery('SELECT p.note, p.dosage, p.substitute, p.per_refill, p.form, p.route, p.size, p.interval, p.drug, p.quantity,
213 p.id AS prescid, l1.title AS title1, l2.title AS title2, l3.title AS title3, l4.title AS title4,
214 DATE_FORMAT(date_added,\'%Y%m%d\') AS date_added, CONCAT_WS(fname, \' \', mname, \' \', lname) AS docname
215 FROM prescriptions AS p
216 LEFT JOIN users AS u ON p.provider_id = u.id
217 LEFT JOIN list_options AS l1 ON l1.list_id = \'drug_form\'
218 AND l1.option_id = p.form
219 LEFT JOIN list_options AS l2 ON l2.list_id = \'drug_route\'
220 AND l2.option_id = p.route
221 LEFT JOIN list_options AS l3 ON l3.list_id = \'drug_interval\'
222 AND l3.option_id = p.interval
223 LEFT JOIN list_options AS l4 ON l4.list_id = \'drug_units\'
224 AND l4.option_id = p.unit
227 array($prescriptionId)
232 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit) {
233 return sqlStatement('SELECT id, begdate, title
235 WHERE type = \'medication\'
238 AND erx_uploaded = \'0\'
242 OR enddate = \'0000-00-00\'
256 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit) {
257 return sqlStatement('SELECT id
260 AND erx_source = \'0\'
261 AND erx_uploaded = \'0\'
274 * Return option Id for title text of specified list
275 * @param string $listId Id of list to reference
276 * @param string $title Title text to find
277 * @return string Option Id of selected list item
279 public function selectOptionIdByTitle($listId, $title) {
280 $return = sqlQuery('SELECT option_id
290 if(is_array($return))
291 $return = $return['option_id'];
297 * Return highest option Id for provided list Id
298 * @param string $listId Id of list to reference
299 * @return integer Highest option Id for provided list Id
301 public function selectOptionIdsByListId($listId) {
302 $return = sqlQuery('SELECT option_id
305 ORDER BY ABS(option_id) DESC
310 if(is_array($return))
311 $return = $return['option_id'];
317 * Return user Id by user name
318 * @param string $name Name of user to reference
319 * @return integer Id of provided user name
321 public function selectUserIdByUserName($name) {
322 $return = sqlQuery('SELECT id
324 WHERE username = ?;',
328 return $return['id'];
332 * Insert new option to specified list
333 * @param string $listId Id of list to add option to
334 * @param string $optionId Option Id to add to referenced list
335 * @param string $title Title of option to add to new option
337 public function insertListOptions($listId, $optionId, $title) {
338 sqlQuery('INSERT INTO list_options
339 (list_id, option_id, title, seq)
352 * Return Id of prescription selected by GUID and patient Id
353 * @param string $prescriptionGuid GUID of prescription
354 * @param integer $patientId Id of patient
355 * @return resource Prescription Id of specified GUID for selected patient, this resource comes from a call to mysql_query()
357 public function selectPrescriptionIdByGuidPatientId($prescriptionGuid, $patientId) {
358 return sqlStatement('SELECT id
360 WHERE prescriptionguid = ?
361 AND prescriptionguid IS NOT NULL
362 AND patient_id = ?;',
371 * Insert new prescription as external sourced
372 * @param array $prescriptionData Information for creating prescription: [PrescriptionDate, DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
373 * @param integer $encounter Id of encounter for prescription
374 * @param integer $providerId Id of provider for prescription
375 * @param string $authUserId Id of user creating prescription
376 * @param integer $formOptionId Option Id for prescription form
377 * @param integer $routeOptionId Option Id for prescription route
378 * @param integer $unitsOptionId Option Id for prescription units
379 * @param integer $intervalOptionId Option Id for prescription interval
380 * @return integer Id of newly created prescription
382 public function insertPrescriptions($prescriptionData, $encounter, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId) {
383 return sqlInsert('INSERT INTO `prescriptions`
409 NOW(), \'1\', ?, ?, ?,
410 ?, ?, ?, ?, ?, ?, ?, ?,
411 ?, ?, ?, ?, ?, ?, ?, ?
415 substr($prescriptionData['PrescriptionDate'], 0, 10),
422 $prescriptionData['DrugName'],
423 $prescriptionData['DrugID'],
424 $prescriptionData['DrugInfo'],
425 $prescriptionData['DosageNumberDescription'],
426 self
::sanitizeNumber($prescriptionData['Strength']),
427 $prescriptionData['Refills'],
428 $prescriptionData['PrescriptionNotes'],
429 $prescriptionData['SiteID'],
430 $prescriptionData['rxcui'],
431 $prescriptionData['PrescriptionGuid'],
432 $prescriptionData['ExternalPatientID']
438 * Update prescription information as external sourced
439 * @param array $prescriptionData Information for creating prescription: [DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
440 * @param integer $providerId Id of provider for prescription
441 * @param string $authUserId Id of user creating prescription
442 * @param integer $formOptionId Option Id for prescription form
443 * @param integer $routeOptionId Option Id for prescription route
444 * @param integer $unitsOptionId Option Id for prescription units
445 * @param integer $intervalOptionId Option Id for prescription interval
447 public function updatePrescriptions($prescriptionData, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId) {
448 sqlQuery('UPDATE prescriptions SET
450 `erx_source` = \'1\',
466 `rxnorm_drugcode` = ?
467 WHERE prescriptionguid = ?
468 AND patient_id = ?;',
476 $prescriptionData['DrugName'],
477 $prescriptionData['DrugID'],
478 $prescriptionData['DrugInfo'],
479 $prescriptionData['DosageNumberDescription'],
480 self
::sanitizeNumber($prescriptionData['Strength']),
481 $prescriptionData['Refills'],
482 $prescriptionData['PrescriptionNotes'],
483 $prescriptionData['SiteID'],
484 $prescriptionData['rxcui'],
485 $prescriptionData['PrescriptionGuid'],
486 $prescriptionData['ExternalPatientID']
492 * Return eRx source of specified active allergy for selected patient
493 * @param integer $patientId Id of patient to select
494 * @param string $name Name of active allergy to return
495 * @return integer eRx source flag of specified allergy for selected patient: [0 = OpenEMR, 1 = External]
497 public function selectAllergyErxSourceByPatientIdName($patientId, $name) {
498 $return = sqlQuery('SELECT erx_source
501 AND type = \'allergy\'
506 OR enddate = \'0000-00-00\'
514 if(is_array($return))
515 $return = $return['erx_source'];
521 * Insert new allergy as external sourced
522 * @param string $name Allergy name to insert
523 * @param integer $allergyId External allergy Id
524 * @param integer $patientId Patient Id
525 * @param integer $authUserId User Id
526 * @param integer $outcome Allergy option Id
528 public function insertAllergy($name, $allergyId, $patientId, $authUserId, $outcome) {
529 sqlQuery('INSERT INTO lists
531 date, type, erx_source, begdate,
532 title, external_allergyid, pid, user, outcome
536 NOW(), \'allergy\', \'1\', NOW(),
548 setListTouch($patientId, 'allergy');
552 * Update allergy outcome and external Id as external sourced using patient Id and allergy name
553 * @param integer $outcome Allergy outcome Id to set
554 * @param integer $externalId External allergy Id to set
555 * @param integer $patientId Patient Id to select
556 * @param string $name Allergy name to select
558 public function updateAllergyOutcomeExternalIdByPatientIdName($outcome, $externalId, $patientId, $name) {
559 sqlQuery('UPDATE lists
562 external_allergyid = ?
575 * Update external sourced allergy outcome using patient Id, external Id, and allergy name
576 * @param integer $outcome Allergy outcome Id to set
577 * @param integer $patientId Patient Id to select
578 * @param integer $externalId External allergy Id to select
579 * @param string $name Allergy name to select
581 public function updateAllergyOutcomeByPatientIdExternalIdName($outcome, $patientId, $externalId, $name) {
582 sqlQuery('UPDATE lists
585 AND erx_source = \'1\'
586 AND external_allergyid = ?
597 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId) {
598 sqlQuery('UPDATE lists
600 WHERE type = \'allergy\'
612 * Return all external sourced active allergies for patient using patient Id
613 * @param integer $patientId Patient Id to select
614 * @return resource Patients active allergies, this resource comes from a call to mysql_query()
616 public function selectActiveAllergiesByPatientId($patientId) {
617 return sqlStatement('SELECT id, title
620 AND type = \'allergy\'
621 AND erx_source = \'1\'
625 OR enddate = \'0000-00-00\'
632 * Update allergy end date for specified patient Id and list Id
633 * @param integer $patientId Id of patient to lookup
634 * @param integer $listId Id of allergy to update
636 public function updateAllergyEndDateByPatientIdListId($patientId, $listId) {
637 sqlQuery('UPDATE lists
641 AND type = \'allergy\';',
650 * Update eRx uploaded status using list Id
651 * @param integer $listId Id of list item
652 * @param integer $erx [optional - defaults to 0] Upload status to set: [0 = Pending NewCrop upload, 1 = Uploaded TO NewCrop]
654 public function updateErxUploadedByListId($listId, $erx = 0) {
655 sqlQuery('UPDATE lists
666 * Return patient import status using patient Id
667 * @param integer $patientId Id of patient
668 * @return integer Import status for specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
670 public function getPatientImportStatusByPatientId($patientId) {
671 $return = sqlquery('SELECT soap_import_status
676 return $return['soap_import_status'];
680 * Update patient import status using patient Id
681 * @param integer $patientId Id of patient to update
682 * @param integer $status Import status to update specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
684 public function updatePatientImportStatusByPatientId($patientId, $status) {
685 sqlQuery('UPDATE patient_data
686 SET soap_import_status = ?