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)
33 return preg_replace('/[^-0-9.]/', '', $value);
37 * Return the primary business entity
38 * @return array Primary business entity
40 public function getFacilityPrimary()
42 $return = sqlQuery('SELECT `name`, `federal_ein`, `street`, `city`, `state`, `postal_code`, `country_code`, `phone`, `fax`
44 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)
68 return sqlQuery('SELECT id, username, lname, fname, mname, title, federaldrugid, upin, state_license_number, npi, newcrop_user_role
76 * Return user facility business entity
77 * @param integer $id Id of user to return
78 * @return array User facility business entity
80 public function getUserFacility($id)
82 return sqlQuery('SELECT facility.id, facility.name, facility.street, facility.city, facility.state, facility.postal_code, facility.country_code, facility.phone, facility.fax
84 LEFT JOIN facility ON facility.id = users.facility_id
91 * Return patient information using patient Id
92 * @param integer $patientId Id of patient
93 * @return array Specified patient information: index [pid, fname, mname, lname, street, city, state, postal_code, country_code, phone_home, date_of_birth, sex]
95 public function getPatientByPatientId($patientId)
97 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
104 public function getPatientHealthplansByPatientId($patientId)
106 return sqlStatement('SELECT `ins`.`name`
111 FROM `insurance_data` AS `id`
112 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
114 AND `id`.`subscriber_relationship` = \'self\'
115 AND `id`.`provider` > 0
116 ORDER BY `id`.`date` DESC
118 GROUP BY `ins`.`type`;',
123 public function getPatientAllergiesByPatientId($patientId)
125 return sqlStatement('SELECT id, lists.title as title1, list_options.title as title2, comments
127 LEFT JOIN list_options ON lists.outcome = list_options.option_id
128 AND list_options.list_id = \'outcome\'
129 WHERE `type` = \'allergy\'
131 AND erx_source = \'0\'
132 AND erx_uploaded = \'0\'
136 OR enddate = \'0000-00-00\'
144 * Return TTL timestamp for provided patient Id and process
145 * @param string $process SOAP process to check
146 * @param integer $patientId Patient Id to check
147 * @return string|boolean TTL timestamp of last SOAP call for provided patient Id and process
149 public function getLastSOAP($process, $patientId)
151 $return = sqlQuery('SELECT updated
160 if($return === false)
163 return $return['updated'];
167 * Set TTL timestamp for provided patient Id and process
168 * @param string $process SOAP process to update
169 * @param integer $patientId Patient Id to update
171 public function setLastSOAP($process, $patientId)
173 sqlQuery('REPLACE INTO erx_ttl_touch
185 * Update external sourced prescripts active status for provided patient Id
186 * @param integer $patientId Patient Id to update
187 * @param integer $active Active status to set for provided patient
189 public function updatePrescriptionsActiveByPatientId($patientId, $active = 0)
191 sqlQuery('UPDATE prescriptions
194 AND erx_source=\'1\'',
196 ($active == 1 ?
1 : 0),
202 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId)
204 sqlQuery('UPDATE prescriptions
205 SET erx_uploaded = ?,
219 * Return prescription specified
220 * @param integer $prescriptionId Id of the prescription to return
221 * @return array Prescription information specified
223 public function getPrescriptionById($prescriptionId)
225 return sqlQuery('SELECT p.note, p.dosage, p.substitute, p.per_refill, p.form, p.route, p.size, p.interval, p.drug, p.quantity,
226 p.id AS prescid, l1.title AS title1, l2.title AS title2, l3.title AS title3, l4.title AS title4,
227 DATE_FORMAT(date_added,\'%Y%m%d\') AS date_added, CONCAT_WS(fname, \' \', mname, \' \', lname) AS docname
228 FROM prescriptions AS p
229 LEFT JOIN users AS u ON p.provider_id = u.id
230 LEFT JOIN list_options AS l1 ON l1.list_id = \'drug_form\'
231 AND l1.option_id = p.form
232 LEFT JOIN list_options AS l2 ON l2.list_id = \'drug_route\'
233 AND l2.option_id = p.route
234 LEFT JOIN list_options AS l3 ON l3.list_id = \'drug_interval\'
235 AND l3.option_id = p.interval
236 LEFT JOIN list_options AS l4 ON l4.list_id = \'drug_units\'
237 AND l4.option_id = p.unit
240 array($prescriptionId)
245 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit)
247 return sqlStatement('SELECT id, begdate, title
249 WHERE type = \'medication\'
252 AND erx_uploaded = \'0\'
256 OR enddate = \'0000-00-00\'
270 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit)
272 return sqlStatement('SELECT id
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)
296 $return = sqlQuery('SELECT option_id
298 WHERE list_id = ? AND activity = 1
306 if(is_array($return))
307 $return = $return['option_id'];
313 * Return highest option Id for provided list Id
314 * @param string $listId Id of list to reference
315 * @return integer Highest option Id for provided list Id
317 public function selectOptionIdsByListId($listId)
319 $return = sqlQuery('SELECT option_id
321 WHERE list_id = ? AND activity = 1
322 ORDER BY ABS(option_id) DESC
327 if(is_array($return))
328 $return = $return['option_id'];
334 * Return user Id by user name
335 * @param string $name Name of user to reference
336 * @return integer Id of provided user name
338 public function selectUserIdByUserName($name)
340 $return = sqlQuery('SELECT id
342 WHERE username = ?;',
346 return $return['id'];
350 * Insert new option to specified list
351 * @param string $listId Id of list to add option to
352 * @param string $optionId Option Id to add to referenced list
353 * @param string $title Title of option to add to new option
355 public function insertListOptions($listId, $optionId, $title)
357 sqlQuery('INSERT INTO list_options
358 (list_id, option_id, title, seq)
371 * Return Id of prescription selected by GUID and patient Id
372 * @param string $prescriptionGuid GUID of prescription
373 * @param integer $patientId Id of patient
374 * @return resource Prescription Id of specified GUID for selected patient, this resource comes from a call to mysql_query()
376 public function selectPrescriptionIdByGuidPatientId($prescriptionGuid, $patientId)
378 return sqlStatement('SELECT id
380 WHERE prescriptionguid = ?
381 AND prescriptionguid IS NOT NULL
382 AND patient_id = ?;',
391 * Insert new prescription as external sourced
392 * @param array $prescriptionData Information for creating prescription: [PrescriptionDate, DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
393 * @param integer $encounter Id of encounter for prescription
394 * @param integer $providerId Id of provider for prescription
395 * @param string $authUserId Id of user creating prescription
396 * @param integer $formOptionId Option Id for prescription form
397 * @param integer $routeOptionId Option Id for prescription route
398 * @param integer $unitsOptionId Option Id for prescription units
399 * @param integer $intervalOptionId Option Id for prescription interval
400 * @return integer Id of newly created prescription
402 public function insertPrescriptions($prescriptionData, $encounter, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
404 return sqlInsert('INSERT INTO `prescriptions`
430 NOW(), \'1\', ?, ?, ?,
431 ?, ?, ?, ?, ?, ?, ?, ?,
432 ?, ?, ?, ?, ?, ?, ?, ?
436 substr($prescriptionData['PrescriptionDate'], 0, 10),
443 $prescriptionData['DrugName'],
444 $prescriptionData['DrugID'],
445 $prescriptionData['DrugInfo'],
446 $prescriptionData['DosageNumberDescription'],
447 self
::sanitizeNumber($prescriptionData['Strength']),
448 $prescriptionData['Refills'],
449 $prescriptionData['PrescriptionNotes'],
450 $prescriptionData['SiteID'],
451 $prescriptionData['rxcui'],
452 $prescriptionData['PrescriptionGuid'],
453 $prescriptionData['ExternalPatientID']
459 * Update prescription information as external sourced
460 * @param array $prescriptionData Information for creating prescription: [DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
461 * @param integer $providerId Id of provider for prescription
462 * @param string $authUserId Id of user creating prescription
463 * @param integer $formOptionId Option Id for prescription form
464 * @param integer $routeOptionId Option Id for prescription route
465 * @param integer $unitsOptionId Option Id for prescription units
466 * @param integer $intervalOptionId Option Id for prescription interval
468 public function updatePrescriptions($prescriptionData, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId)
470 sqlQuery('UPDATE prescriptions SET
472 `erx_source` = \'1\',
488 `rxnorm_drugcode` = ?
489 WHERE prescriptionguid = ?
490 AND patient_id = ?;',
498 $prescriptionData['DrugName'],
499 $prescriptionData['DrugID'],
500 $prescriptionData['DrugInfo'],
501 $prescriptionData['DosageNumberDescription'],
502 self
::sanitizeNumber($prescriptionData['Strength']),
503 $prescriptionData['Refills'],
504 $prescriptionData['PrescriptionNotes'],
505 $prescriptionData['SiteID'],
506 $prescriptionData['rxcui'],
507 $prescriptionData['PrescriptionGuid'],
508 $prescriptionData['ExternalPatientID']
514 * Return eRx source of specified active allergy for selected patient
515 * @param integer $patientId Id of patient to select
516 * @param string $name Name of active allergy to return
517 * @return integer eRx source flag of specified allergy for selected patient: [0 = OpenEMR, 1 = External]
519 public function selectAllergyErxSourceByPatientIdName($patientId, $name)
521 $return = sqlQuery('SELECT erx_source
524 AND type = \'allergy\'
529 OR enddate = \'0000-00-00\'
537 if(is_array($return))
538 $return = $return['erx_source'];
544 * Insert new allergy as external sourced
545 * @param string $name Allergy name to insert
546 * @param integer $allergyId External allergy Id
547 * @param integer $patientId Patient Id
548 * @param integer $authUserId User Id
549 * @param integer $outcome Allergy option Id
551 public function insertAllergy($name, $allergyId, $patientId, $authUserId, $outcome)
553 sqlQuery('INSERT INTO lists
555 date, type, erx_source, begdate,
556 title, external_allergyid, pid, user, outcome
560 NOW(), \'allergy\', \'1\', NOW(),
572 setListTouch($patientId, 'allergy');
576 * Update allergy outcome and external Id as external sourced using patient Id and allergy name
577 * @param integer $outcome Allergy outcome Id to set
578 * @param integer $externalId External allergy Id to set
579 * @param integer $patientId Patient Id to select
580 * @param string $name Allergy name to select
582 public function updateAllergyOutcomeExternalIdByPatientIdName($outcome, $externalId, $patientId, $name)
584 sqlQuery('UPDATE lists
587 external_allergyid = ?
600 * Update external sourced allergy outcome using patient Id, external Id, and allergy name
601 * @param integer $outcome Allergy outcome Id to set
602 * @param integer $patientId Patient Id to select
603 * @param integer $externalId External allergy Id to select
604 * @param string $name Allergy name to select
606 public function updateAllergyOutcomeByPatientIdExternalIdName($outcome, $patientId, $externalId, $name)
608 sqlQuery('UPDATE lists
611 AND erx_source = \'1\'
612 AND external_allergyid = ?
623 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId)
625 sqlQuery('UPDATE lists
627 WHERE type = \'allergy\'
639 * Return all external sourced active allergies for patient using patient Id
640 * @param integer $patientId Patient Id to select
641 * @return resource Patients active allergies, this resource comes from a call to mysql_query()
643 public function selectActiveAllergiesByPatientId($patientId)
645 return sqlStatement('SELECT id, title
648 AND type = \'allergy\'
649 AND erx_source = \'1\'
653 OR enddate = \'0000-00-00\'
660 * Update allergy end date for specified patient Id and list Id
661 * @param integer $patientId Id of patient to lookup
662 * @param integer $listId Id of allergy to update
664 public function updateAllergyEndDateByPatientIdListId($patientId, $listId)
666 sqlQuery('UPDATE lists
670 AND type = \'allergy\';',
679 * Update eRx uploaded status using list Id
680 * @param integer $listId Id of list item
681 * @param integer $erx [optional - defaults to 0] Upload status to set: [0 = Pending NewCrop upload, 1 = Uploaded TO NewCrop]
683 public function updateErxUploadedByListId($listId, $erx = 0)
685 sqlQuery('UPDATE lists
696 * Return patient import status using patient Id
697 * @param integer $patientId Id of patient
698 * @return integer Import status for specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
700 public function getPatientImportStatusByPatientId($patientId)
702 $return = sqlquery('SELECT soap_import_status
707 return $return['soap_import_status'];
711 * Update patient import status using patient Id
712 * @param integer $patientId Id of patient to update
713 * @param integer $status Import status to update specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
715 public function updatePatientImportStatusByPatientId($patientId, $status)
717 sqlQuery('UPDATE patient_data
718 SET soap_import_status = ?