4 * Prescription.class.php
7 * @link https://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 // Below list of terms are deprecated, but we keep this list
14 // to keep track of the official openemr drugs terms and
15 // corresponding ID's for reference. Official is referring
16 // to the default settings after installing OpenEMR.
18 // define('UNIT_BLANK',0);
19 // define('UNIT_MG',1);
20 // define('UNIT_MG_1CC',2);
21 // define('UNIT_MG_2CC',3);
22 // define('UNIT_MG_3CC',4);
23 // define('UNIT_MG_4CC',5);
24 // define('UNIT_MG_5CC',6);
25 // define('UNIT_MCG',7);
26 // define('UNIT_GRAMS',8);
28 // define('INTERVAL_BLANK',0);
29 // define('INTERVAL_BID',1);
30 // define('INTERVAL_TID',2);
31 // define('INTERVAL_QID',3);
32 // define('INTERVAL_Q_3H',4);
33 // define('INTERVAL_Q_4H',5);
34 // define('INTERVAL_Q_5H',6);
35 // define('INTERVAL_Q_6H',7);
36 // define('INTERVAL_Q_8H',8);
37 // define('INTERVAL_QD',9);
38 // define('INTERVAL_AC',10); // added May 2008
39 // define('INTERVAL_PC',11); // added May 2008
40 // define('INTERVAL_AM',12); // added May 2008
41 // define('INTERVAL_PM',13); // added May 2008
42 // define('INTERVAL_ANTE',14); // added May 2008
43 // define('INTERVAL_H',15); // added May 2008
44 // define('INTERVAL_HS',16); // added May 2008
45 // define('INTERVAL_PRN',17); // added May 2008
46 // define('INTERVAL_STAT',18); // added May 2008
48 // define('FORM_BLANK',0);
49 // define('FORM_SUSPENSION',1);
50 // define('FORM_TABLET',2);
51 // define('FORM_CAPSULE',3);
52 // define('FORM_SOLUTION',4);
53 // define('FORM_TSP',5);
54 // define('FORM_ML',6);
55 // define('FORM_UNITS',7);
56 // define('FORM_INHILATIONS',8);
57 // define('FORM_GTTS_DROPS',9);
58 // define('FORM_CR',10);
59 // define('FORM_OINT',11);
61 // define('ROUTE_BLANK',0);
62 // define("ROUTE_PER_ORIS", 1);
63 // define("ROUTE_PER_RECTUM", 2);
64 // define("ROUTE_TO_SKIN", 3);
65 // define("ROUTE_TO_AFFECTED_AREA", 4);
66 // define("ROUTE_SUBLINGUAL", 5);
67 // define("ROUTE_OS", 6);
68 // define("ROUTE_OD", 7);
69 // define("ROUTE_OU", 8);
70 // define("ROUTE_SQ", 9);
71 // define("ROUTE_IM", 10);
72 // define("ROUTE_IV", 11);
73 // define("ROUTE_PER_NOSTRIL", 12);
74 // define("ROUTE_B_EAR", 13);
75 // define("ROUTE_L_EAR", 14);
76 // define("ROUTE_R_EAR", 15);
78 // define('SUBSTITUTE_YES',1);
79 // define('SUBSTITUTE_NO',2);
83 require_once(dirname(__FILE__
) . "/../lists.inc.php");
91 use OpenEMR\Common\ORDataObject\ORDataObject
;
93 class Prescription
extends ORDataObject
108 var $substitute_array;
109 var $medication_array;
129 var $rxnorm_drugcode;
153 * Constructor sets all Prescription attributes to their default value
156 function __construct($id = "", $_prefix = "")
158 $this->route_array
= $this->load_drug_attributes('drug_route');
159 $this->form_array
= $this->load_drug_attributes('drug_form');
160 $this->interval_array
= $this->load_drug_attributes('drug_interval');
161 $this->unit_array
= $this->load_drug_attributes('drug_units');
163 $this->substitute_array
= array("",xl("substitution allowed"),
164 xl("do not substitute"));
166 $this->medication_array
= array(0 => xl('No'), 1 => xl('Yes'));
168 if (is_numeric($id)) {
174 //$this->unit = UNIT_MG;
175 //$this->route = ROUTE_PER_ORIS;
176 //$this->quantity = 1;
179 //$this->form = FORM_TABLET;
180 $this->substitute
= false;
181 $this->_prefix
= $_prefix;
182 $this->_table
= "prescriptions";
183 $this->pharmacy
= new Pharmacy();
184 $this->pharmacist
= new Person();
185 // default provider is the current user
186 $this->provider
= new Provider($_SESSION['authUserID']);
187 $this->patient
= new Patient();
188 $this->start_date
= date("Y-m-d");
189 $this->date_added
= date("Y-m-d H:i:s");
190 $this->date_modified
= date("Y-m-d H:i:s");
191 $this->created_by
= $_SESSION['authUserID'];
192 $this->updated_by
= $_SESSION['authUserID'];
193 $this->per_refill
= 0;
201 for ($i = 0; $i < 21; $i++
) {
202 $this->refills_array
[$i] = sprintf("%02d", $i);
212 $this->date_modified
= date("Y-m-d H:i:s");
213 if ($this->id
== "") {
214 $this->date_added
= date("Y-m-d H:i:s");
217 if (parent
::persist()) {
224 // for old historical data we are going to populate our created_by and updated_by
225 if (empty($this->created_by
)) {
226 $this->created_by
= $this->get_provider_id();
228 if (empty($this->updated_by
)) {
229 $this->updated_by
= $this->get_provider_id();
233 function toString($html = false)
236 . "ID: " . $this->id
. "\n"
237 . "Patient:" . $this->patient
. "\n"
238 . "Patient ID:" . $this->patient
->id
. "\n"
239 . "Pharmacist: " . $this->pharmacist
. "\n"
240 . "Pharmacist ID: " . $this->pharmacist
->id
. "\n"
241 . "Date Added: " . $this->date_added
. "\n"
242 . "Date Modified: " . $this->date_modified
. "\n"
243 . "Pharmacy: " . $this->pharmacy
. "\n"
244 . "Pharmacy ID:" . $this->pharmacy
->id
. "\n"
245 . "Start Date: " . $this->start_date
. "\n"
246 . "Filled Date: " . $this->filled_date
. "\n"
247 . "Provider: " . $this->provider
. "\n"
248 . "Provider ID: " . $this->provider
->id
. "\n"
249 . "Note: " . $this->note
. "\n"
250 . "Drug: " . $this->drug
. "\n"
251 . "Code: " . $this->rxnorm_drugcode
. "\n"
252 . "Form: " . $this->form_array
[$this->form
] . "\n"
253 . "Dosage: " . $this->dosage
. "\n"
254 . "Qty: " . $this->quantity
. "\n"
255 . "Size: " . $this->size
. "\n"
256 . "Unit: " . $this->unit_array
[$this->unit
] . "\n"
257 . "Route: " . $this->route_array
[$this->route
] . "\n"
258 . "Interval: " . $this->interval_array
[$this->interval
] . "\n"
259 . "Substitute: " . $this->substitute_array
[$this->substitute
] . "\n"
260 . "Refills: " . $this->refills
. "\n"
261 . "Per Refill: " . $this->per_refill
. "\n"
262 . "Drug ID: " . $this->drug_id
. "\n"
263 . "Active: " . $this->active
. "\n"
264 . "Transmitted: " . $this->ntx
;
267 return nl2br($string);
273 private function load_drug_attributes($id)
275 $res = sqlStatement("SELECT * FROM list_options WHERE list_id = ? AND activity = 1 ORDER BY seq", array($id));
276 while ($row = sqlFetchArray($res)) {
277 if ($row['title'] == '') {
278 $arr[$row['option_id']] = ' ';
280 $arr[$row['option_id']] = xl_list_label($row['title']);
287 function get_encounter()
289 return $_SESSION['encounter'];
292 function get_unit_display($display_form = "")
294 return( ($this->unit_array
[$this->unit
] ??
'') );
301 function set_unit($unit)
303 if (is_numeric($unit)) {
310 if (!empty($id) && is_numeric($id)) {
319 function get_dosage_display($display_form = "")
321 if (empty($this->form
) && empty($this->interval
)) {
322 return( $this->dosage
);
324 return ($this->dosage
. " " . xl('in') . " " . $this->form_array
[$this->form
] . " " . $this->interval_array
[$this->interval
]);
328 function set_dosage($dosage)
330 $this->dosage
= $dosage;
332 function get_dosage()
334 return $this->dosage
;
337 function set_form($form)
339 if (is_numeric($form)) {
348 function set_refills($refills)
350 if (is_numeric($refills)) {
351 $this->refills
= $refills;
354 function get_refills()
356 return $this->refills
;
359 function set_size($size)
361 $this->size
= preg_replace("/[^0-9\/\.\-]/", "", $size);
368 function set_quantity($qty)
370 $this->quantity
= $qty;
372 function get_quantity()
374 return $this->quantity
;
377 function set_route($route)
379 $this->route
= $route;
386 function set_interval($interval)
388 if (is_numeric($interval)) {
389 $this->interval
= $interval;
392 function get_interval()
394 return $this->interval
;
397 function set_substitute($sub)
399 if (is_numeric($sub)) {
400 $this->substitute
= $sub;
403 function get_substitute()
405 return $this->substitute
;
407 function set_erx_source($erx_source)
409 $this->erx_source
= $erx_source;
411 function gen_lists_medication($id)
413 $instructions = $this->size
. $this->unit_array
[$this->unit
] . "\t\t" . $this->get_dosage_display();
415 $medId = sqlQuery("select list_id from lists_medication where list_id = '" . add_escape_custom($id) . "' limit 1");
416 if (isset($medId["list_id"])) {
417 $medId = sqlQuery("update lists_medication set drug_dosage_instructions = '" . add_escape_custom($instructions) . "' where list_id = '" . add_escape_custom($id) . "'");
419 sqlStatement("insert into lists_medication(list_id, drug_dosage_instructions) values ('" . add_escape_custom($id) . "', '" . add_escape_custom($instructions) . "')");
423 function set_medication($med)
427 $this->medication
= $med;
429 // Avoid making a mess if we are not using the "medication" issue type.
430 if (isset($ISSUE_TYPES) && !$ISSUE_TYPES['medication']) {
434 //below statements are bypassing the persist() function and being used directly in database statements, hence need to use the functions in library/formdata.inc.php
435 // they have already been run through populate() hence stripped of escapes, so now need to be escaped for database (add_escape_custom() function).
437 //check if this drug is on the medication list
438 $dataRow = sqlQuery("select id from lists where type = 'medication' and activity = 1 and (enddate is null or cast(now() as date) < enddate) and upper(trim(title)) = upper(trim('" . add_escape_custom($this->drug
) . "')) and pid = '" . add_escape_custom($this->patient
->id
) . "' limit 1");
440 if ($med && !isset($dataRow['id'])) {
441 $dataRow = sqlQuery("select id from lists where type = 'medication' and activity = 0 and (enddate is null or cast(now() as date) < enddate) and upper(trim(title)) = upper(trim('" . add_escape_custom($this->drug
) . "')) and pid = '" . add_escape_custom($this->patient
->id
) . "' limit 1");
443 if (!isset($dataRow['id'])) {
444 //add the record to the medication list
445 sqlStatement("insert into lists(date,begdate,type,activity,pid,user,groupname,title) values (now(),cast(now() as date),'medication',1,'" . add_escape_custom($this->patient
->id
) . "','" . add_escape_custom($_SESSION['authUser']) . "','" . add_escape_custom($_SESSION['authProvider']) . "','" . add_escape_custom($this->drug
) . "')");
446 $medListId = sqlQuery("select id from lists where type = 'medication' and (enddate is null or cast(now() as date) < enddate) and upper(trim(title)) = upper(trim('" . add_escape_custom($this->drug
) . "')) and pid = '" . add_escape_custom($this->patient
->id
) . "' limit 1");
447 $this->gen_lists_medication($medListId["id"], $dosageInstructions);
449 $dataRow = sqlQuery('update lists set activity = 1'
450 . " ,user = '" . add_escape_custom($_SESSION['authUser'])
451 . "', groupname = '" . add_escape_custom($_SESSION['authProvider']) . "' where id = '" . add_escape_custom($dataRow['id']) . "'");
452 $this->gen_lists_medication($dataRow["id"]);
454 } elseif (!$med && isset($dataRow['id'])) {
455 //remove the drug from the medication list if it exists
456 $dataRow = sqlQuery('update lists set activity = 0'
457 . " ,user = '" . add_escape_custom($_SESSION['authUser'])
458 . "', groupname = '" . add_escape_custom($_SESSION['authProvider']) . "' where id = '" . add_escape_custom($dataRow['id']) . "'");
459 } elseif ($med && isset($dataRow['id'])) {
460 $this->gen_lists_medication($dataRow["id"]);
464 function get_medication()
466 return $this->medication
;
469 function set_per_refill($pr)
471 if (is_numeric($pr)) {
472 $this->per_refill
= $pr;
475 function get_per_refill()
477 return $this->per_refill
;
480 function set_patient_id($id)
482 if (is_numeric($id)) {
483 $this->patient
= new Patient($id);
486 function get_patient_id()
488 return $this->patient
->id
;
491 function set_provider_id($id)
493 if (is_numeric($id)) {
494 $this->provider
= new Provider($id);
497 function get_provider_id()
499 return $this->provider
->id
;
502 function set_created_by($id)
504 if (is_numeric($id)) {
505 $this->created_by
= $id;
508 function get_created_by()
510 return $this->created_by
;
513 function set_updated_by($id)
515 if (is_numeric($id)) {
516 $this->updated_by
= $id;
519 function get_updated_by()
521 return $this->updated_by
;
524 function set_provider($pobj)
526 if (get_class($pobj) == "provider") {
527 $this->provider
= $pobj;
531 function set_pharmacy_id($id)
533 if (is_numeric($id)) {
534 $this->pharmacy
= new Pharmacy($id);
537 function get_pharmacy_id()
539 return $this->pharmacy
->id
;
542 function set_pharmacist_id($id)
544 if (is_numeric($id)) {
545 $this->pharmacist
= new Person($id);
548 function get_pharmacist()
550 return $this->pharmacist
->id
;
553 function get_start_date_y()
555 $ymd = explode("-", $this->start_date
);
558 function set_start_date_y($year)
560 if (is_numeric($year)) {
561 $ymd = explode("-", $this->start_date
);
563 $this->start_date
= $ymd[0] . "-" . $ymd[1] . "-" . $ymd[2];
566 function get_start_date_m()
568 $ymd = explode("-", $this->start_date
);
571 function set_start_date_m($month)
573 if (is_numeric($month)) {
574 $ymd = explode("-", $this->start_date
);
576 $this->start_date
= $ymd[0] . "-" . $ymd[1] . "-" . $ymd[2];
579 function get_start_date_d()
581 $ymd = explode("-", $this->start_date
);
584 function set_start_date_d($day)
586 if (is_numeric($day)) {
587 $ymd = explode("-", $this->start_date
);
589 $this->start_date
= $ymd[0] . "-" . $ymd[1] . "-" . $ymd[2];
592 function get_start_date()
594 return $this->start_date
;
596 function set_start_date($date)
598 return $this->start_date
= $date;
601 // TajEmo work by CB 2012/05/30 01:56:32 PM added encounter for auto ticking of checkboxes
602 function set_encounter($enc)
604 return $this->encounter
= $enc;
607 function get_date_added()
609 return $this->date_added
;
611 function set_date_added($date)
613 return $this->date_added
= $date;
615 function set_txDate($txdate)
617 return $this->txDate
= $txdate;
619 function get_txDate()
621 return $this->txDate
;
624 function get_date_modified()
626 return $this->date_modified
;
628 function set_date_modified($date)
630 return $this->date_modified
= $date;
633 function get_filled_date()
635 return $this->filled_date
;
637 function set_filled_date($date)
639 return $this->filled_date
= $date;
642 function set_note($note)
651 function set_drug($drug)
653 // If the medication already exists in the list and the drug name is being changed, update the title there as well
654 if (!empty($this->drug
) && $this->medication
) {
655 $dataRow = sqlQuery("select id from lists where type = 'medication' and (enddate is null or cast(now() as date) < enddate) and upper(trim(title)) = upper(trim('" . add_escape_custom($this->drug
) . "')) and pid = '" . add_escape_custom($this->patient
->id
) . "' limit 1");
656 if (isset($dataRow['id'])) {
657 $updateRow = sqlQuery('update lists set activity = 1'
658 . " ,user = '" . add_escape_custom($_SESSION['authUser'])
659 . "', groupname = '" . add_escape_custom($_SESSION['authProvider'])
660 . "', title = '" . add_escape_custom($drug)
661 . "' where id = '" . add_escape_custom($dataRow['id']) . "'");
662 $this->gen_lists_medication($dataRow["id"]);
672 function set_ntx($ntx)
681 function set_rxnorm_drugcode($rxnorm_drugcode)
683 $this->rxnorm_drugcode
= $rxnorm_drugcode;
685 function get_rxnorm_drugcode()
687 return $this->rxnorm_drugcode
;
690 function get_filled_by_id()
692 return $this->pharmacist
->id
;
694 function set_filled_by_id($id)
696 if (is_numeric($id)) {
697 return $this->pharmacist
->id
= $id;
701 function set_drug_id($drug_id)
703 $this->drug_id
= $drug_id;
705 function get_drug_id()
707 return $this->drug_id
;
710 function set_active($active)
712 $this->active
= $active;
714 function get_active()
716 return $this->active
;
718 function get_prescription_display()
720 $pconfig = $GLOBALS['oer_config']['prescriptions'];
722 switch ($pconfig['format']) {
724 return $this->get_prescription_florida_display();
730 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider
->id
) . "'";
732 $results = $db->Execute($sql);
733 if (!$results->EOF
) {
734 $string = $results->fields
['name'] . "\n"
735 . $results->fields
['street'] . "\n"
736 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
737 . $results->fields
['phone'] . "\n\n";
741 . "Prescription For:" . "\t" . $this->patient
->get_name_display() . "\n"
742 . "DOB:" . "\t" . $this->patient
->get_dob() . "\n"
743 . "Start Date: " . "\t\t" . $this->start_date
. "\n"
744 . "Provider: " . "\t\t" . $this->provider
->get_name_display() . "\n"
745 . "Provider DEA No.: " . "\t\t" . $this->provider
->federal_drug_id
. "\n"
746 . "Drug: " . "\t\t\t" . $this->drug
. "\n"
747 . "Dosage: " . "\t\t" . $this->dosage
. " in " . $this->form_array
[$this->form
] . " form " . $this->interval_array
[$this->interval
] . "\n"
748 . "Qty: " . "\t\t\t" . $this->quantity
. "\n"
749 . "Medication Unit: " . "\t" . $this->size
. " " . $this->unit_array
[$this->unit
] . "\n"
750 . "Substitute: " . "\t\t" . $this->substitute_array
[$this->substitute
] . "\n";
751 if ($this->refills
> 0) {
752 $string .= "Refills: " . "\t\t" . $this->refills
. ", of quantity: " . $this->per_refill
. "\n";
755 $string .= "\n" . "Notes: \n" . $this->note
. "\n";
759 function get_prescription_florida_display()
763 $ntt = new NumberToText($this->quantity
);
764 $ntt2 = new NumberToText($this->per_refill
);
765 $ntt3 = new NumberToText($this->refills
);
769 $gnd = $this->provider
->get_name_display();
771 while (strlen($gnd) < 31) {
775 $string .= $gnd . $this->provider
->federal_drug_id
. "\n";
777 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider
->id
) . "'";
778 $results = $db->Execute($sql);
780 if (!$results->EOF
) {
781 $rfn = $results->fields
['name'];
783 while (strlen($rfn) < 31) {
787 $string .= $rfn . $this->provider
->get_provider_number_default() . "\n"
788 . $results->fields
['street'] . "\n"
789 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
790 . $results->fields
['phone'] . "\n";
794 $string .= strtoupper($this->patient
->lname
) . ", " . ucfirst($this->patient
->fname
) . " " . $this->patient
->mname
. "\n";
795 $string .= "DOB " . $this->patient
->date_of_birth
. "\n";
797 $string .= date("F j, Y", strtotime($this->start_date
)) . "\n";
799 $string .= strtoupper($this->drug
) . " " . $this->size
. " " . $this->unit_array
[$this->unit
] . "\n";
800 if (strlen($this->note
) > 0) {
801 $string .= "Notes: \n" . $this->note
. "\n";
804 if (!empty($this->dosage
)) {
805 $string .= $this->dosage
;
806 if (!empty($this->form
)) {
807 $string .= " " . $this->form_array
[$this->form
];
810 if (!empty($this->interval
)) {
811 $string .= " " . $this->interval_array
[$this->interval
];
814 if (!empty($this->route
)) {
815 $string .= " " . $this->route_array
[$this->route
] . "\n";
819 if (!empty($this->quantity
)) {
820 $string .= "Disp: " . $this->quantity
. " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
824 $string .= "Refills: " . $this->refills
. " (" . trim(strtoupper($ntt3->convert())) . "), Per Refill Disp: " . $this->per_refill
. " (" . trim(strtoupper($ntt2->convert())) . ")" . "\n";
825 $string .= $this->substitute_array
[$this->substitute
] . "\n";
831 static function prescriptions_factory(
833 $order_by = "active DESC, date_modified DESC, date_added DESC"
836 $prescriptions = array();
837 $p = new Prescription();
838 $sql = "SELECT id FROM " . escape_table_name($p->_table
) . " WHERE patient_id = ? " .
839 "ORDER BY " . add_escape_custom($order_by);
840 $results = sqlQ($sql, array($patient_id));
841 while ($row = sqlFetchArray($results)) {
842 $prescriptions[] = new Prescription($row['id']);
845 return $prescriptions;
848 function get_dispensation_count()
850 if (empty($this->id
)) {
854 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
855 "WHERE prescription_id = ? AND quantity > 0", [$this->id
]);
856 return $refills_row['count'];
858 }// end of Prescription