Fix to using most recent development translation set on development demo.
[openemr.git] / interface / eRxStore.php
blobb252197b232de942c4389a85a2d70a95f955bba0
1 <?php
3 /**
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>.
18 * @package OpenEMR
19 * @subpackage NewCrop
20 * @author Sam Likins <sam.likins@wsi-services.com>
21 * @link http://www.open-emr.org
24 class eRxStore {
26 /**
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);
35 /**
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
41 FROM facility
42 WHERE primary_business_entity = \'1\';'
45 return $return['federal_ein'];
48 /**
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
55 FROM users
56 WHERE id = ?;',
57 array($id)
61 /**
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
69 FROM erx_ttl_touch
70 WHERE patient_id = ?
71 AND process = ?;',
72 array(
73 $patientId,
74 $process
77 if($return === false)
78 return false;
80 return $return['updated'];
83 /**
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
90 SET patient_id = ?,
91 process = ?,
92 updated = NOW();',
93 array(
94 $patientId,
95 $process
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
107 SET active = ?
108 WHERE patient_id = ?
109 AND erx_source=\'1\'',
110 array(
111 ($active == 1 ? 1 : 0),
112 $patientId
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
125 FROM list_options
126 WHERE list_id = ?
127 AND title = ?;',
128 array(
129 $listId,
130 $title
134 if(is_array($return))
135 $return = $return['option_id'];
137 return $return;
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
147 FROM list_options
148 WHERE list_id = ?
149 ORDER BY ABS(option_id) DESC
150 LIMIT 1;',
151 array($listId)
154 if(is_array($return))
155 $return = $return['option_id'];
157 return $return;
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
167 FROM users
168 WHERE username = ?;',
169 array($name)
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)
184 VALUES
185 (?, ?, ?, ?);',
186 array(
187 $listId,
188 $optionId,
189 $title,
190 $optionId
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
203 FROM prescriptions
204 WHERE prescriptionguid = ?
205 AND prescriptionguid IS NOT NULL
206 AND patient_id = ?;',
207 array(
208 $prescriptionGuid,
209 $patientId
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
229 `DATETIME`,
230 erx_source,
231 encounter,
232 date_added,
233 `USER`,
234 provider_id,
235 form,
236 unit,
237 route,
238 `INTERVAL`,
239 drug,
240 drug_id,
241 drug_info_erx,
242 dosage,
243 size,
244 refills,
245 note,
246 site,
247 rxnorm_drugcode,
248 prescriptionguid,
249 patient_id
251 VALUES
253 NOW(), \'1\', ?, ?, ?,
254 ?, ?, ?, ?, ?, ?, ?, ?,
255 ?, ?, ?, ?, ?, ?, ?, ?
256 );',
257 array(
258 $encounter,
259 substr($prescriptionData['PrescriptionDate'], 0, 10),
260 $authUserId,
261 $providerId,
262 $formOptionId,
263 $unitsOptionId,
264 $routeOptionId,
265 $intervalOptionId,
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
293 `DATETIME` = NOW(),
294 erx_source = \'1\',
295 active = \'1\',
296 `USER` = ?,
297 provider_id = ?,
298 form = ?,
299 unit = ?,
300 route = ?,
301 `INTERVAL` = ?,
302 drug = ?,
303 drug_id = ?,
304 drug_info_erx = ?,
305 dosage = ?,
306 size = ?,
307 refills = ?,
308 note = ?,
309 site = ?,
310 rxnorm_drugcode = ?
311 WHERE prescriptionguid = ?
312 AND patient_id = ?;',
313 array(
314 $authUserId,
315 $providerId,
316 $formOptionId,
317 $unitsOptionId,
318 $routeOptionId,
319 $intervalOptionI,
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
343 FROM lists
344 WHERE pid = ?
345 AND type = \'allergy\'
346 AND title = ?
347 AND (
348 enddate IS NULL
349 OR enddate = \'\'
350 OR enddate = \'0000-00-00\'
351 );',
352 array(
353 $patientId,
354 $name
358 if(is_array($return))
359 $return = $return['erx_source'];
361 return $return;
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
378 VALUES
380 NOW(), \'allergy\', \'1\', NOW(),
381 ?, ?, ?, ?, ?
382 );',
383 array(
384 $name,
385 $allergyId,
386 $patientId,
387 $authUserId,
388 $outcome
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
404 SET outcome = ?,
405 erx_source = \'1\',
406 external_allergyid = ?
407 WHERE pid = ?
408 AND title = ?;',
409 array(
410 $outcome,
411 $externalId,
412 $patientId,
413 $name
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
427 SET outcome = ?
428 WHERE pid = ?
429 AND erx_source = \'1\'
430 AND external_allergyid = ?
431 AND title = ?;',
432 array(
433 $outcome,
434 $patientId,
435 $externalId,
436 $name
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
448 FROM lists
449 WHERE pid = ?
450 AND type = \'allergy\'
451 AND erx_source = \'1\'
452 AND (
453 enddate IS NULL
454 OR enddate = \'\'
455 OR enddate = \'0000-00-00\'
456 );',
457 array($patientId)
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
468 SET enddate = now()
469 WHERE pid = ?
470 AND id = ?
471 AND type = \'allergy\';',
472 array(
473 $patientId,
474 $listId
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
486 SET erx_uploaded = ?
487 WHERE id = ?;',
488 array(
489 $erx,
490 $listId
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
502 FROM patient_data
503 WHERE pid = ?;',
504 array($patientId)
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 = ?
517 WHERE pid = ?;',
518 array(
519 $status,
520 $patientId