MU2 for items e2 and f1.
[openemr.git] / interface / eRxStore.php
blobb6d50b50726d6244dc87bbd6496286b941a7e1eb
1 <?php
3 /**
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>.
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 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`
41 FROM `facility`
42 WHERE `primary_business_entity` = \'1\';'
45 return $return;
48 /**
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'];
58 /**
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
65 FROM users
66 WHERE id = ?;',
67 array($id)
71 /**
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
78 FROM users
79 LEFT JOIN facility ON facility.id = users.facility_id
80 WHERE users.id = ?;',
81 array($id)
85 /**
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
92 FROM patient_data
93 WHERE pid = ?;',
94 array($patientId)
98 public function getPatientHealthplansByPatientId($patientId) {
99 return sqlStatement('SELECT `ins`.`name`
100 FROM (
101 SELECT
102 `id`.`type`,
103 `ic`.`name`
104 FROM `insurance_data` AS `id`
105 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
106 WHERE `id`.`pid` = ?
107 AND `id`.`subscriber_relationship` = \'self\'
108 AND `id`.`provider` > 0
109 ORDER BY `id`.`date` DESC
110 ) AS `ins`
111 GROUP BY `ins`.`type`;',
112 array($patientId)
116 public function getPatientAllergiesByPatientId($patientId) {
117 return sqlStatement('SELECT id, lists.title as title1, list_options.title as title2, comments
118 FROM lists
119 LEFT JOIN list_options ON lists.outcome = list_options.option_id
120 AND list_options.list_id = \'outcome\'
121 WHERE `type` = \'allergy\'
122 AND pid = ?
123 AND erx_source = \'0\'
124 AND erx_uploaded = \'0\'
125 AND (
126 enddate is NULL
127 OR enddate = \'\'
128 OR enddate = \'0000-00-00\'
129 );',
130 array($patientId)
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
143 FROM erx_ttl_touch
144 WHERE patient_id = ?
145 AND process = ?;',
146 array(
147 $patientId,
148 $process
151 if($return === false)
152 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
164 SET patient_id = ?,
165 process = ?,
166 updated = NOW();',
167 array(
168 $patientId,
169 $process
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
181 SET active = ?
182 WHERE patient_id = ?
183 AND erx_source=\'1\'',
184 array(
185 ($active == 1 ? 1 : 0),
186 $patientId
191 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId) {
192 sqlQuery('UPDATE prescriptions
193 SET erx_uploaded = ?,
194 active = ?
195 WHERE patient_id = ?
196 AND id = ?;',
197 array(
198 $upload,
199 $active,
200 $patientId,
201 $prescriptionId
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
225 WHERE p.drug <> \'\'
226 AND p.id = ?;',
227 array($prescriptionId)
232 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit) {
233 return sqlStatement('SELECT id, begdate, title
234 FROM lists
235 WHERE type = \'medication\'
236 AND pid = ?
237 AND title <> \'\'
238 AND erx_uploaded = \'0\'
239 AND (? = 0
240 OR (enddate IS NULL
241 OR enddate = \'\'
242 OR enddate = \'0000-00-00\'
245 ORDER BY enddate
246 LIMIT 0, ?;',
247 array(
248 $patientId,
249 $uploadActive,
250 $limit
256 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit) {
257 return sqlStatement('SELECT id
258 FROM prescriptions
259 WHERE patient_id = ?
260 AND erx_source = \'0\'
261 AND erx_uploaded = \'0\'
262 AND (? = 0
263 OR active = 1
264 ) LIMIT 0, ?;',
265 array(
266 $patientId,
267 $uploadActive,
268 $limit,
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
281 FROM list_options
282 WHERE list_id = ?
283 AND title = ?;',
284 array(
285 $listId,
286 $title
290 if(is_array($return))
291 $return = $return['option_id'];
293 return $return;
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
303 FROM list_options
304 WHERE list_id = ?
305 ORDER BY ABS(option_id) DESC
306 LIMIT 1;',
307 array($listId)
310 if(is_array($return))
311 $return = $return['option_id'];
313 return $return;
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
323 FROM users
324 WHERE username = ?;',
325 array($name)
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)
340 VALUES
341 (?, ?, ?, ?);',
342 array(
343 $listId,
344 $optionId,
345 $title,
346 $optionId
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
359 FROM prescriptions
360 WHERE prescriptionguid = ?
361 AND prescriptionguid IS NOT NULL
362 AND patient_id = ?;',
363 array(
364 $prescriptionGuid,
365 $patientId
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`
385 `datetime`,
386 `erx_source`,
387 `encounter`,
388 `date_added`,
389 `user`,
390 `provider_id`,
391 `form`,
392 `unit`,
393 `route`,
394 `interval`,
395 `drug`,
396 `drug_id`,
397 `drug_info_erx`,
398 `dosage`,
399 `size`,
400 `refills`,
401 `note`,
402 `site`,
403 `rxnorm_drugcode`,
404 `prescriptionguid`,
405 `patient_id`
407 VALUES
409 NOW(), \'1\', ?, ?, ?,
410 ?, ?, ?, ?, ?, ?, ?, ?,
411 ?, ?, ?, ?, ?, ?, ?, ?
412 );',
413 array(
414 $encounter,
415 substr($prescriptionData['PrescriptionDate'], 0, 10),
416 $authUserId,
417 $providerId,
418 $formOptionId,
419 $unitsOptionId,
420 $routeOptionId,
421 $intervalOptionId,
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
449 `datetime` = NOW(),
450 `erx_source` = \'1\',
451 `active` = \'1\',
452 `user` = ?,
453 `provider_id` = ?,
454 `form` = ?,
455 `unit` = ?,
456 `route` = ?,
457 `interval` = ?,
458 `drug` = ?,
459 `drug_id` = ?,
460 `drug_info_erx` = ?,
461 `dosage` = ?,
462 `size` = ?,
463 `refills` = ?,
464 `note` = ?,
465 `site` = ?,
466 `rxnorm_drugcode` = ?
467 WHERE prescriptionguid = ?
468 AND patient_id = ?;',
469 array(
470 $authUserId,
471 $providerId,
472 $formOptionId,
473 $unitsOptionId,
474 $routeOptionId,
475 $intervalOptionId,
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
499 FROM lists
500 WHERE pid = ?
501 AND type = \'allergy\'
502 AND title = ?
503 AND (
504 enddate IS NULL
505 OR enddate = \'\'
506 OR enddate = \'0000-00-00\'
507 );',
508 array(
509 $patientId,
510 $name
514 if(is_array($return))
515 $return = $return['erx_source'];
517 return $return;
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
534 VALUES
536 NOW(), \'allergy\', \'1\', NOW(),
537 ?, ?, ?, ?, ?
538 );',
539 array(
540 $name,
541 $allergyId,
542 $patientId,
543 $authUserId,
544 $outcome
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
560 SET outcome = ?,
561 erx_source = \'1\',
562 external_allergyid = ?
563 WHERE pid = ?
564 AND title = ?;',
565 array(
566 $outcome,
567 $externalId,
568 $patientId,
569 $name
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
583 SET outcome = ?
584 WHERE pid = ?
585 AND erx_source = \'1\'
586 AND external_allergyid = ?
587 AND title = ?;',
588 array(
589 $outcome,
590 $patientId,
591 $externalId,
592 $name
597 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId) {
598 sqlQuery('UPDATE lists
599 SET erx_uploaded = ?
600 WHERE type = \'allergy\'
601 AND pid = ?
602 AND id = ?;',
603 array(
604 $uploaded,
605 $patientId,
606 $allergyId
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
618 FROM lists
619 WHERE pid = ?
620 AND type = \'allergy\'
621 AND erx_source = \'1\'
622 AND (
623 enddate IS NULL
624 OR enddate = \'\'
625 OR enddate = \'0000-00-00\'
626 );',
627 array($patientId)
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
638 SET enddate = now()
639 WHERE pid = ?
640 AND id = ?
641 AND type = \'allergy\';',
642 array(
643 $patientId,
644 $listId
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
656 SET erx_uploaded = ?
657 WHERE id = ?;',
658 array(
659 $erx,
660 $listId
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
672 FROM patient_data
673 WHERE pid = ?;',
674 array($patientId)
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 = ?
687 WHERE pid = ?;',
688 array(
689 $status,
690 $patientId