Improved Code Sniffing (#928)
[openemr.git] / interface / eRxStore.php
blobf9d8bb3260782882b4c73973d5fead5f8244c86c
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)
33 return preg_replace('/[^-0-9.]/', '', $value);
36 /**
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`
43 FROM `facility`
44 WHERE `primary_business_entity` = \'1\';'
47 return $return;
50 /**
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'];
61 /**
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
69 FROM users
70 WHERE id = ?;',
71 array($id)
75 /**
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
83 FROM users
84 LEFT JOIN facility ON facility.id = users.facility_id
85 WHERE users.id = ?;',
86 array($id)
90 /**
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
98 FROM patient_data
99 WHERE pid = ?;',
100 array($patientId)
104 public function getPatientHealthplansByPatientId($patientId)
106 return sqlStatement('SELECT `ins`.`name`
107 FROM (
108 SELECT
109 `id`.`type`,
110 `ic`.`name`
111 FROM `insurance_data` AS `id`
112 LEFT JOIN `insurance_companies` AS `ic` ON `ic`.`id` = `id`.`provider`
113 WHERE `id`.`pid` = ?
114 AND `id`.`subscriber_relationship` = \'self\'
115 AND `id`.`provider` > 0
116 ORDER BY `id`.`date` DESC
117 ) AS `ins`
118 GROUP BY `ins`.`type`;',
119 array($patientId)
123 public function getPatientAllergiesByPatientId($patientId)
125 return sqlStatement('SELECT id, lists.title as title1, list_options.title as title2, comments
126 FROM lists
127 LEFT JOIN list_options ON lists.outcome = list_options.option_id
128 AND list_options.list_id = \'outcome\'
129 WHERE `type` = \'allergy\'
130 AND pid = ?
131 AND erx_source = \'0\'
132 AND erx_uploaded = \'0\'
133 AND (
134 enddate is NULL
135 OR enddate = \'\'
136 OR enddate = \'0000-00-00\'
137 );',
138 array($patientId)
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
152 FROM erx_ttl_touch
153 WHERE patient_id = ?
154 AND process = ?;',
155 array(
156 $patientId,
157 $process
160 if($return === false)
161 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
174 SET patient_id = ?,
175 process = ?,
176 updated = NOW();',
177 array(
178 $patientId,
179 $process
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
192 SET active = ?
193 WHERE patient_id = ?
194 AND erx_source=\'1\'',
195 array(
196 ($active == 1 ? 1 : 0),
197 $patientId
202 public function updatePrescriptionsUploadActiveByPatientIdPrescriptionId($upload, $active, $patientId, $prescriptionId)
204 sqlQuery('UPDATE prescriptions
205 SET erx_uploaded = ?,
206 active = ?
207 WHERE patient_id = ?
208 AND id = ?;',
209 array(
210 $upload,
211 $active,
212 $patientId,
213 $prescriptionId
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
238 WHERE p.drug <> \'\'
239 AND p.id = ?;',
240 array($prescriptionId)
245 public function selectMedicationsNotUploadedByPatientId($patientId, $uploadActive, $limit)
247 return sqlStatement('SELECT id, begdate, title
248 FROM lists
249 WHERE type = \'medication\'
250 AND pid = ?
251 AND title <> \'\'
252 AND erx_uploaded = \'0\'
253 AND (? = 0
254 OR (enddate IS NULL
255 OR enddate = \'\'
256 OR enddate = \'0000-00-00\'
259 ORDER BY enddate
260 LIMIT 0, ?;',
261 array(
262 $patientId,
263 $uploadActive,
264 $limit
270 public function selectPrescriptionIdsNotUploadedByPatientId($patientId, $uploadActive, $limit)
272 return sqlStatement('SELECT id
273 FROM prescriptions
274 WHERE patient_id = ?
275 AND erx_source = \'0\'
276 AND erx_uploaded = \'0\'
277 AND (? = 0
278 OR active = 1
279 ) LIMIT 0, ?;',
280 array(
281 $patientId,
282 $uploadActive,
283 $limit,
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
297 FROM list_options
298 WHERE list_id = ? AND activity = 1
299 AND title = ?;',
300 array(
301 $listId,
302 $title
306 if(is_array($return))
307 $return = $return['option_id'];
309 return $return;
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
320 FROM list_options
321 WHERE list_id = ? AND activity = 1
322 ORDER BY ABS(option_id) DESC
323 LIMIT 1;',
324 array($listId)
327 if(is_array($return))
328 $return = $return['option_id'];
330 return $return;
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
341 FROM users
342 WHERE username = ?;',
343 array($name)
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)
359 VALUES
360 (?, ?, ?, ?);',
361 array(
362 $listId,
363 $optionId,
364 $title,
365 $optionId
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
379 FROM prescriptions
380 WHERE prescriptionguid = ?
381 AND prescriptionguid IS NOT NULL
382 AND patient_id = ?;',
383 array(
384 $prescriptionGuid,
385 $patientId
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`
406 `datetime`,
407 `erx_source`,
408 `encounter`,
409 `date_added`,
410 `user`,
411 `provider_id`,
412 `form`,
413 `unit`,
414 `route`,
415 `interval`,
416 `drug`,
417 `drug_id`,
418 `drug_info_erx`,
419 `dosage`,
420 `size`,
421 `refills`,
422 `note`,
423 `site`,
424 `rxnorm_drugcode`,
425 `prescriptionguid`,
426 `patient_id`
428 VALUES
430 NOW(), \'1\', ?, ?, ?,
431 ?, ?, ?, ?, ?, ?, ?, ?,
432 ?, ?, ?, ?, ?, ?, ?, ?
433 );',
434 array(
435 $encounter,
436 substr($prescriptionData['PrescriptionDate'], 0, 10),
437 $authUserId,
438 $providerId,
439 $formOptionId,
440 $unitsOptionId,
441 $routeOptionId,
442 $intervalOptionId,
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
471 `datetime` = NOW(),
472 `erx_source` = \'1\',
473 `active` = \'1\',
474 `user` = ?,
475 `provider_id` = ?,
476 `form` = ?,
477 `unit` = ?,
478 `route` = ?,
479 `interval` = ?,
480 `drug` = ?,
481 `drug_id` = ?,
482 `drug_info_erx` = ?,
483 `dosage` = ?,
484 `size` = ?,
485 `refills` = ?,
486 `note` = ?,
487 `site` = ?,
488 `rxnorm_drugcode` = ?
489 WHERE prescriptionguid = ?
490 AND patient_id = ?;',
491 array(
492 $authUserId,
493 $providerId,
494 $formOptionId,
495 $unitsOptionId,
496 $routeOptionId,
497 $intervalOptionId,
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
522 FROM lists
523 WHERE pid = ?
524 AND type = \'allergy\'
525 AND title = ?
526 AND (
527 enddate IS NULL
528 OR enddate = \'\'
529 OR enddate = \'0000-00-00\'
530 );',
531 array(
532 $patientId,
533 $name
537 if(is_array($return))
538 $return = $return['erx_source'];
540 return $return;
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
558 VALUES
560 NOW(), \'allergy\', \'1\', NOW(),
561 ?, ?, ?, ?, ?
562 );',
563 array(
564 $name,
565 $allergyId,
566 $patientId,
567 $authUserId,
568 $outcome
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
585 SET outcome = ?,
586 erx_source = \'1\',
587 external_allergyid = ?
588 WHERE pid = ?
589 AND title = ?;',
590 array(
591 $outcome,
592 $externalId,
593 $patientId,
594 $name
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
609 SET outcome = ?
610 WHERE pid = ?
611 AND erx_source = \'1\'
612 AND external_allergyid = ?
613 AND title = ?;',
614 array(
615 $outcome,
616 $patientId,
617 $externalId,
618 $name
623 public function updateAllergyUploadedByPatientIdAllergyId($uploaded, $patientId, $allergyId)
625 sqlQuery('UPDATE lists
626 SET erx_uploaded = ?
627 WHERE type = \'allergy\'
628 AND pid = ?
629 AND id = ?;',
630 array(
631 $uploaded,
632 $patientId,
633 $allergyId
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
646 FROM lists
647 WHERE pid = ?
648 AND type = \'allergy\'
649 AND erx_source = \'1\'
650 AND (
651 enddate IS NULL
652 OR enddate = \'\'
653 OR enddate = \'0000-00-00\'
654 );',
655 array($patientId)
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
667 SET enddate = now()
668 WHERE pid = ?
669 AND id = ?
670 AND type = \'allergy\';',
671 array(
672 $patientId,
673 $listId
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
686 SET erx_uploaded = ?
687 WHERE id = ?;',
688 array(
689 $erx,
690 $listId
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
703 FROM patient_data
704 WHERE pid = ?;',
705 array($patientId)
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 = ?
719 WHERE pid = ?;',
720 array(
721 $status,
722 $patientId