4 * interface/eRxStore.php Functions for interacting with NewCrop database.
6 * Copyright (C) 2013 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 Federal EIN established with the primary business entity
37 * @return string Federal EIN for the primary business entity
39 public function selectFederalEin() {
40 $return = sqlQuery('SELECT federal_ein
42 WHERE primary_business_entity = \'1\';'
45 return $return['federal_ein'];
49 * Return user ID and NPI by user Id
50 * @param integer $id Id of user to return
51 * @return array Specified user information: index [id, npi]
53 public function getUserById($id) {
54 return sqlQuery('SELECT id, npi
62 * Return TTL timestamp for provided patient Id and process
63 * @param string $process SOAP process to check
64 * @param integer $patientId Patient Id to check
65 * @return string|boolean TTL timestamp of last SOAP call for provided patient Id and process
67 public function getLastSOAP($process, $patientId) {
68 $return = sqlQuery('SELECT updated
80 return $return['updated'];
84 * Set TTL timestamp for provided patient Id and process
85 * @param string $process SOAP process to update
86 * @param integer $patientId Patient Id to update
88 public function setLastSOAP($process, $patientId) {
89 sqlQuery('REPLACE INTO erx_ttl_touch
101 * Update external sourced prescripts active status for provided patient Id
102 * @param integer $patientId Patient Id to update
103 * @param integer $active Active status to set for provided patient
105 public function updatePrescriptionsActiveByPatientId($patientId, $active = 0) {
106 sqlQuery('UPDATE prescriptions
109 AND erx_source=\'1\'',
111 ($active == 1 ?
1 : 0),
118 * Return option Id for title text of specified list
119 * @param string $listId Id of list to reference
120 * @param string $title Title text to find
121 * @return string Option Id of selected list item
123 public function selectOptionIdByTitle($listId, $title) {
124 $return = sqlQuery('SELECT option_id
134 if(is_array($return))
135 $return = $return['option_id'];
141 * Return highest option Id for provided list Id
142 * @param string $listId Id of list to reference
143 * @return integer Highest option Id for provided list Id
145 public function selectOptionIdsByListId($listId) {
146 $return = sqlQuery('SELECT option_id
149 ORDER BY ABS(option_id) DESC
154 if(is_array($return))
155 $return = $return['option_id'];
161 * Return user Id by user name
162 * @param string $name Name of user to reference
163 * @return integer Id of provided user name
165 public function selectUserIdByUserName($name) {
166 $return = sqlQuery('SELECT id
168 WHERE username = ?;',
172 return $return['id'];
176 * Insert new option to specified list
177 * @param string $listId Id of list to add option to
178 * @param string $optionId Option Id to add to referenced list
179 * @param string $title Title of option to add to new option
181 public function insertListOptions($listId, $optionId, $title) {
182 sqlQuery('INSERT INTO list_options
183 (list_id, option_id, title, seq)
196 * Return Id of prescription selected by GUID and patient Id
197 * @param string $prescriptionGuid GUID of prescription
198 * @param integer $patientId Id of patient
199 * @return resource Prescription Id of specified GUID for selected patient, this resource comes from a call to mysql_query()
201 public function selectPrescriptionIdByGuidPatientId($prescriptionGuid, $patientId) {
202 return sqlStatement('SELECT id
204 WHERE prescriptionguid = ?
205 AND prescriptionguid IS NOT NULL
206 AND patient_id = ?;',
215 * Insert new prescription as external sourced
216 * @param array $prescriptionData Information for creating prescription: [PrescriptionDate, DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
217 * @param integer $encounter Id of encounter for prescription
218 * @param integer $providerId Id of provider for prescription
219 * @param string $authUserId Id of user creating prescription
220 * @param integer $formOptionId Option Id for prescription form
221 * @param integer $routeOptionId Option Id for prescription route
222 * @param integer $unitsOptionId Option Id for prescription units
223 * @param integer $intervalOptionId Option Id for prescription interval
224 * @return integer Id of newly created prescription
226 public function insertPrescriptions($prescriptionData, $encounter, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId) {
227 return sqlInsert('INSERT INTO prescriptions
253 NOW(), \'1\', ?, ?, ?,
254 ?, ?, ?, ?, ?, ?, ?, ?,
255 ?, ?, ?, ?, ?, ?, ?, ?
259 substr($prescriptionData['PrescriptionDate'], 0, 10),
266 $prescriptionData['DrugName'],
267 $prescriptionData['DrugID'],
268 $prescriptionData['DrugInfo'],
269 $prescriptionData['DosageNumberDescription'],
270 self
::sanitizeNumber($prescriptionData['Strength']),
271 $prescriptionData['Refills'],
272 $prescriptionData['PrescriptionNotes'],
273 $prescriptionData['SiteID'],
274 $prescriptionData['rxcui'],
275 $prescriptionData['PrescriptionGuid'],
276 $prescriptionData['ExternalPatientID']
282 * Update prescription information as external sourced
283 * @param array $prescriptionData Information for creating prescription: [DrugName, DrugID, DrugInfo, DosageNumberDescription, Strength, Refills, PrescriptionNotes, SiteID, rxcui, PrescriptionGuid, ExternalPatientID]
284 * @param integer $providerId Id of provider for prescription
285 * @param string $authUserId Id of user creating prescription
286 * @param integer $formOptionId Option Id for prescription form
287 * @param integer $routeOptionId Option Id for prescription route
288 * @param integer $unitsOptionId Option Id for prescription units
289 * @param integer $intervalOptionId Option Id for prescription interval
291 public function updatePrescriptions($prescriptionData, $providerId, $authUserId, $formOptionId, $routeOptionId, $unitsOptionId, $intervalOptionId) {
292 sqlQuery('UPDATE prescriptions SET
311 WHERE prescriptionguid = ?
312 AND patient_id = ?;',
320 $prescriptionData['DrugName'],
321 $prescriptionData['DrugID'],
322 $prescriptionData['DrugInfo'],
323 $prescriptionData['DosageNumberDescription'],
324 self
::sanitizeNumber($prescriptionData['Strength']),
325 $prescriptionData['Refills'],
326 $prescriptionData['PrescriptionNotes'],
327 $prescriptionData['SiteID'],
328 $prescriptionData['rxcui'],
329 $prescriptionData['PrescriptionGuid'],
330 $prescriptionData['ExternalPatientID']
336 * Return eRx source of specified active allergy for selected patient
337 * @param integer $patientId Id of patient to select
338 * @param string $name Name of active allergy to return
339 * @return integer eRx source flag of specified allergy for selected patient: [0 = OpenEMR, 1 = External]
341 public function selectAllergyErxSourceByPatientIdName($patientId, $name) {
342 $return = sqlQuery('SELECT erx_source
345 AND type = \'allergy\'
350 OR enddate = \'0000-00-00\'
358 if(is_array($return))
359 $return = $return['erx_source'];
365 * Insert new allergy as external sourced
366 * @param string $name Allergy name to insert
367 * @param integer $allergyId External allergy Id
368 * @param integer $patientId Patient Id
369 * @param integer $authUserId User Id
370 * @param integer $outcome Allergy option Id
372 public function insertAllergy($name, $allergyId, $patientId, $authUserId, $outcome) {
373 sqlQuery('INSERT INTO lists
375 date, type, erx_source, begdate,
376 title, external_allergyid, pid, user, outcome
380 NOW(), \'allergy\', \'1\', NOW(),
392 setListTouch($patientId, 'allergy');
396 * Update allergy outcome and external Id as external sourced using patient Id and allergy name
397 * @param integer $outcome Allergy outcome Id to set
398 * @param integer $externalId External allergy Id to set
399 * @param integer $patientId Patient Id to select
400 * @param string $name Allergy name to select
402 public function updateAllergyOutcomeExternalIdByPatientIdName($outcome, $externalId, $patientId, $name) {
403 sqlQuery('UPDATE lists
406 external_allergyid = ?
419 * Update external sourced allergy outcome using patient Id, external Id, and allergy name
420 * @param integer $outcome Allergy outcome Id to set
421 * @param integer $patientId Patient Id to select
422 * @param integer $externalId External allergy Id to select
423 * @param string $name Allergy name to select
425 public function updateAllergyOutcomeByPatientIdExternalIdName($outcome, $patientId, $externalId, $name) {
426 sqlQuery('UPDATE lists
429 AND erx_source = \'1\'
430 AND external_allergyid = ?
442 * Return all external sourced active allergies for patient using patient Id
443 * @param integer $patientId Patient Id to select
444 * @return resource Patients active allergies, this resource comes from a call to mysql_query()
446 public function selectActiveAllergiesByPatientId($patientId) {
447 return sqlStatement('SELECT id, title
450 AND type = \'allergy\'
451 AND erx_source = \'1\'
455 OR enddate = \'0000-00-00\'
462 * Update allergy end date for specified patient Id and list Id
463 * @param integer $patientId Id of patient to lookup
464 * @param integer $listId Id of allergy to update
466 public function updateAllergyEndDateByPatientIdListId($patientId, $listId) {
467 sqlQuery('UPDATE lists
471 AND type = \'allergy\';',
480 * Update eRx uploaded status using list Id
481 * @param integer $listId Id of list item
482 * @param integer $erx [optional - defaults to 0] Upload status to set: [0 = Pending NewCrop upload, 1 = Uploaded TO NewCrop]
484 public function updateErxUploadedByListId($listId, $erx = 0) {
485 sqlQuery('UPDATE lists
496 * Return patient import status using patient Id
497 * @param integer $patientId Id of patient
498 * @return integer Import status for specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
500 public function getPatientImportStatusByPatientId($patientId) {
501 $return = sqlquery('SELECT soap_import_status
506 return $return['soap_import_status'];
510 * Update patient import status using patient Id
511 * @param integer $patientId Id of patient to update
512 * @param integer $status Import status to update specified patient: [1 = Prescription Press, 2 = Prescription Import, 3 = Allergy Press, 4 = Allergy Import]
514 public function updatePatientImportStatusByPatientId($patientId, $status) {
515 sqlQuery('UPDATE patient_data
516 SET soap_import_status = ?