docker dev update
[openemr.git] / library / classes / Prescription.class.php
blob58535c234d93f6ade58e7c4d22214d38a76bfd5b
1 <?php
2 require_once(dirname(__FILE__) . "/../lists.inc");
3 //below is required for the set_medication() function
5 // Below list of terms are deprecated, but we keep this list
6 // to keep track of the official openemr drugs terms and
7 // corresponding ID's for reference. Official is referring
8 // to the default settings after installing OpenEMR.
9 //
10 // define('UNIT_BLANK',0);
11 // define('UNIT_MG',1);
12 // define('UNIT_MG_1CC',2);
13 // define('UNIT_MG_2CC',3);
14 // define('UNIT_MG_3CC',4);
15 // define('UNIT_MG_4CC',5);
16 // define('UNIT_MG_5CC',6);
17 // define('UNIT_MCG',7);
18 // define('UNIT_GRAMS',8);
20 // define('INTERVAL_BLANK',0);
21 // define('INTERVAL_BID',1);
22 // define('INTERVAL_TID',2);
23 // define('INTERVAL_QID',3);
24 // define('INTERVAL_Q_3H',4);
25 // define('INTERVAL_Q_4H',5);
26 // define('INTERVAL_Q_5H',6);
27 // define('INTERVAL_Q_6H',7);
28 // define('INTERVAL_Q_8H',8);
29 // define('INTERVAL_QD',9);
30 // define('INTERVAL_AC',10); // added May 2008
31 // define('INTERVAL_PC',11); // added May 2008
32 // define('INTERVAL_AM',12); // added May 2008
33 // define('INTERVAL_PM',13); // added May 2008
34 // define('INTERVAL_ANTE',14); // added May 2008
35 // define('INTERVAL_H',15); // added May 2008
36 // define('INTERVAL_HS',16); // added May 2008
37 // define('INTERVAL_PRN',17); // added May 2008
38 // define('INTERVAL_STAT',18); // added May 2008
40 // define('FORM_BLANK',0);
41 // define('FORM_SUSPENSION',1);
42 // define('FORM_TABLET',2);
43 // define('FORM_CAPSULE',3);
44 // define('FORM_SOLUTION',4);
45 // define('FORM_TSP',5);
46 // define('FORM_ML',6);
47 // define('FORM_UNITS',7);
48 // define('FORM_INHILATIONS',8);
49 // define('FORM_GTTS_DROPS',9);
50 // define('FORM_CR',10);
51 // define('FORM_OINT',11);
53 // define('ROUTE_BLANK',0);
54 // define("ROUTE_PER_ORIS", 1);
55 // define("ROUTE_PER_RECTUM", 2);
56 // define("ROUTE_TO_SKIN", 3);
57 // define("ROUTE_TO_AFFECTED_AREA", 4);
58 // define("ROUTE_SUBLINGUAL", 5);
59 // define("ROUTE_OS", 6);
60 // define("ROUTE_OD", 7);
61 // define("ROUTE_OU", 8);
62 // define("ROUTE_SQ", 9);
63 // define("ROUTE_IM", 10);
64 // define("ROUTE_IV", 11);
65 // define("ROUTE_PER_NOSTRIL", 12);
66 // define("ROUTE_B_EAR", 13);
67 // define("ROUTE_L_EAR", 14);
68 // define("ROUTE_R_EAR", 15);
70 // define('SUBSTITUTE_YES',1);
71 // define('SUBSTITUTE_NO',2);
74 // Added 7-2009 by BM to incorporate the units, forms, interval, route lists from list_options
75 // This mechanism may only be temporary; will likely migrate changes more downstream to allow
76 // users the options of using the addlist widgets and validation frunctions from options.inc.php
77 // in the forms and output.
78 function load_drug_attributes($id)
80 $res = sqlStatement("SELECT * FROM list_options WHERE list_id = ? AND activity = 1 ORDER BY seq", array($id));
81 while ($row = sqlFetchArray($res)) {
82 if ($row['title'] == '') {
83 $arr[$row['option_id']] = ' ';
84 } else {
85 $arr[$row['option_id']] = xl_list_label($row['title']);
89 return $arr;
92 /**
93 * class Prescription
96 class Prescription extends ORDataObject
99 /**
101 * @access public
106 * static
108 var $form_array;
109 var $unit_array;
110 var $route_array;
111 var $interval_array;
112 var $substitute_array;
113 var $medication_array;
114 var $refills_array;
118 * @access private
121 var $id;
122 var $patient;
123 var $pharmacist;
124 var $date_added;
125 var $date_modified;
126 var $pharmacy;
127 var $start_date;
128 var $filled_date;
129 var $provider;
130 var $note;
131 var $drug;
132 var $rxnorm_drugcode;
133 var $form;
134 var $dosage;
135 var $quantity;
136 var $size;
137 var $unit;
138 var $route;
139 var $interval;
140 var $substitute;
141 var $refills;
142 var $per_refill;
143 var $medication;
145 var $drug_id;
146 var $active;
147 var $ntx;
150 * Constructor sets all Prescription attributes to their default value
153 function __construct($id = "", $_prefix = "")
156 // Modified 7-2009 by BM to load the arrays from the lists in lists_options.
157 // Plan for this to only be temporary, hopefully have the lists used directly
158 // from forms in future to allow use of widgets etc.
159 $this->route_array = load_drug_attributes('drug_route');
160 $this->form_array = load_drug_attributes('drug_form');
161 $this->interval_array = load_drug_attributes('drug_interval');
162 $this->unit_array = load_drug_attributes('drug_units');
164 $this->substitute_array = array("",xl("substitution allowed"),
165 xl("do not substitute"));
167 $this->medication_array = array(0 => xl('No'), 1 => xl('Yes'));
169 if (is_numeric($id)) {
170 $this->id = $id;
171 } else {
172 $id = "";
175 //$this->unit = UNIT_MG;
176 //$this->route = ROUTE_PER_ORIS;
177 //$this->quantity = 1;
178 //$this->size = 1;
179 $this->refills = 0;
180 //$this->form = FORM_TABLET;
181 $this->substitute = false;
182 $this->_prefix = $_prefix;
183 $this->_table = "prescriptions";
184 $this->pharmacy = new Pharmacy();
185 $this->pharmacist = new Person();
186 $this->provider = new Provider($_SESSION['authId']);
187 $this->patient = new Patient();
188 $this->start_date = date("Y-m-d");
189 $this->date_added = date("Y-m-d");
190 $this->date_modified = date("Y-m-d");
191 $this->per_refill = 0;
192 $this->note = "";
194 $this->drug_id = 0;
195 $this->active = 1;
197 $this->ntx = 0;
199 for ($i=0; $i<21; $i++) {
200 $this->refills_array[$i] = sprintf("%02d", $i);
203 if ($id != "") {
204 $this->populate();
208 function persist()
210 $this->date_modified = date("Y-m-d");
211 if ($this->id == "") {
212 $this->date_added = date("Y-m-d");
215 if (parent::persist()) {
219 function populate()
221 parent::populate();
224 function toString($html = false)
226 $string .= "\n"
227 ."ID: " . $this->id . "\n"
228 ."Patient:" . $this->patient . "\n"
229 ."Patient ID:" . $this->patient->id . "\n"
230 ."Pharmacist: " . $this->pharmacist . "\n"
231 ."Pharmacist ID: " . $this->pharmacist->id . "\n"
232 ."Date Added: " . $this->date_added. "\n"
233 ."Date Modified: " . $this->date_modified. "\n"
234 ."Pharmacy: " . $this->pharmacy. "\n"
235 ."Pharmacy ID:" . $this->pharmacy->id . "\n"
236 ."Start Date: " . $this->start_date. "\n"
237 ."Filled Date: " . $this->filled_date. "\n"
238 ."Provider: " . $this->provider. "\n"
239 ."Provider ID: " . $this->provider->id. "\n"
240 ."Note: " . $this->note. "\n"
241 ."Drug: " . $this->drug. "\n"
242 ."Code: " . $this->rxnorm_drugcode. "\n"
243 ."Form: " . $this->form_array[$this->form]. "\n"
244 ."Dosage: " . $this->dosage. "\n"
245 ."Qty: " . $this->quantity. "\n"
246 ."Size: " . $this->size. "\n"
247 ."Unit: " . $this->unit_array[$this->unit] . "\n"
248 ."Route: " . $this->route_array[$this->route] . "\n"
249 ."Interval: " .$this->interval_array[$this->interval]. "\n"
250 ."Substitute: " . $this->substitute_array[$this->substitute]. "\n"
251 ."Refills: " . $this->refills. "\n"
252 ."Per Refill: " . $this->per_refill . "\n"
253 ."Drug ID: " . $this->drug_id . "\n"
254 ."Active: " . $this->active . "\n"
255 ."Transmitted: " . $this->ntx;
257 if ($html) {
258 return nl2br($string);
259 } else {
260 return $string;
263 function get_encounter()
265 return $_SESSION['encounter'];
268 function get_unit_display($display_form = "")
270 return( $this->unit_array[$this->unit] );
273 function get_unit()
275 return $this->unit;
277 function set_unit($unit)
279 if (is_numeric($unit)) {
280 $this->unit = $unit;
284 function set_id($id)
286 if (!empty($id) && is_numeric($id)) {
287 $this->id = $id;
290 function get_id()
292 return $this->id;
295 function get_dosage_display($display_form = "")
297 if (empty($this->form) && empty($this->interval)) {
298 return( $this->dosage );
299 } else {
300 return ($this->dosage . " " . xl('in') . " " . $this->form_array[$this->form] . " " . $this->interval_array[$this->interval]);
304 function set_dosage($dosage)
306 $this->dosage = $dosage;
308 function get_dosage()
310 return $this->dosage;
313 function set_form($form)
315 if (is_numeric($form)) {
316 $this->form = $form;
319 function get_form()
321 return $this->form;
324 function set_refills($refills)
326 if (is_numeric($refills)) {
327 $this->refills = $refills;
330 function get_refills()
332 return $this->refills;
335 function set_size($size)
337 $this->size = preg_replace("/[^0-9\/\.\-]/", "", $size);
339 function get_size()
341 return $this->size;
344 function set_quantity($qty)
346 $this->quantity = $qty;
348 function get_quantity()
350 return $this->quantity;
353 function set_route($route)
355 if (is_numeric($route)) {
356 $this->route = $route;
359 function get_route()
361 return $this->route;
364 function set_interval($interval)
366 if (is_numeric($interval)) {
367 $this->interval = $interval;
370 function get_interval()
372 return $this->interval;
375 function set_substitute($sub)
377 if (is_numeric($sub)) {
378 $this->substitute = $sub;
381 function get_substitute()
383 return $this->substitute;
385 function set_erx_source($erx_source)
387 $this->erx_source = $erx_source;
389 function set_medication($med)
391 global $ISSUE_TYPES;
393 $this->medication = $med;
395 // Avoid making a mess if we are not using the "medication" issue type.
396 if (isset($ISSUE_TYPES) && !$ISSUE_TYPES['medication']) {
397 return;
400 //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
401 // they have already been run through populate() hence stripped of escapes, so now need to be escaped for database (add_escape_custom() function).
403 //check if this drug is on the medication list
404 $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');
406 if ($med && !isset($dataRow['id'])) {
407 $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');
409 if (!isset($dataRow['id'])) {
410 //add the record to the medication list
411 sqlInsert("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) . ",'" . $$_SESSION['authUser']. "','" . $$_SESSION['authProvider'] . "','" . add_escape_custom($this->drug) . "')");
412 } else {
413 $dataRow = sqlQuery('update lists set activity = 1'
414 . " ,user = '" . $$_SESSION['authUser']
415 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
417 } elseif (!$med && isset($dataRow['id'])) {
418 //remove the drug from the medication list if it exists
419 $dataRow = sqlQuery('update lists set activity = 0'
420 . " ,user = '" . $$_SESSION['authUser']
421 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
425 function get_medication()
427 return $this->medication;
430 function set_per_refill($pr)
432 if (is_numeric($pr)) {
433 $this->per_refill = $pr;
436 function get_per_refill()
438 return $this->per_refill;
441 function set_patient_id($id)
443 if (is_numeric($id)) {
444 $this->patient = new Patient($id);
447 function get_patient_id()
449 return $this->patient->id;
452 function set_provider_id($id)
454 if (is_numeric($id)) {
455 $this->provider = new Provider($id);
458 function get_provider_id()
460 return $this->provider->id;
463 function set_provider($pobj)
465 if (get_class($pobj) == "provider") {
466 $this->provider = $pobj;
470 function set_pharmacy_id($id)
472 if (is_numeric($id)) {
473 $this->pharmacy = new Pharmacy($id);
476 function get_pharmacy_id()
478 return $this->pharmacy->id;
481 function set_pharmacist_id($id)
483 if (is_numeric($id)) {
484 $this->pharmacist = new Person($id);
487 function get_pharmacist()
489 return $this->pharmacist->id;
492 function get_start_date_y()
494 $ymd = explode("-", $this->start_date);
495 return $ymd[0];
497 function set_start_date_y($year)
499 if (is_numeric($year)) {
500 $ymd = explode("-", $this->start_date);
501 $ymd[0] = $year;
502 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
505 function get_start_date_m()
507 $ymd = explode("-", $this->start_date);
508 return $ymd[1];
510 function set_start_date_m($month)
512 if (is_numeric($month)) {
513 $ymd = explode("-", $this->start_date);
514 $ymd[1] = $month;
515 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
518 function get_start_date_d()
520 $ymd = explode("-", $this->start_date);
521 return $ymd[2];
523 function set_start_date_d($day)
525 if (is_numeric($day)) {
526 $ymd = explode("-", $this->start_date);
527 $ymd[2] = $day;
528 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
531 function get_start_date()
533 return $this->start_date;
535 function set_start_date($date)
537 return $this->start_date = $date;
540 // TajEmo work by CB 2012/05/30 01:56:32 PM added encounter for auto ticking of checkboxes
541 function set_encounter($enc)
543 return $this->encounter = $enc;
546 function get_date_added()
548 return $this->date_added;
550 function set_date_added($date)
552 return $this->date_added = $date;
554 function set_txDate($txdate)
556 return $this->txDate = $txdate;
558 function get_txDate()
560 return $this->txDate;
563 function get_date_modified()
565 return $this->date_modified;
567 function set_date_modified($date)
569 return $this->date_modified = $date;
572 function get_filled_date()
574 return $this->filled_date;
576 function set_filled_date($date)
578 return $this->filled_date = $date;
581 function set_note($note)
583 $this->note = $note;
585 function get_note()
587 return $this->note;
590 function set_drug($drug)
592 if ($GLOBALS['weno_rx_enable']) {
593 $drug = explode("-", $drug); //striping the price from the drug name.
594 $drug = trim($drug[0]);
596 $this->drug = $drug;
598 if ($GLOBALS['weno_rx_enable']) {
599 $sql = "SELECT NDC FROM erx_drug_paid WHERE drug_label_name LIKE ? ";
600 $val = array('%'.$drug.'%');
601 $ndc = sqlQuery($sql, $val);
602 $drug_id = $ndc['NDC'];
603 //Save this drug id
604 $this->drug_id = $drug_id;
607 function get_drug()
609 if ($GLOBALS['weno_rx_enable']) {
610 $drug = trim($this->drug);
611 $sql = "SELECT NDC FROM erx_drug_paid WHERE drug_label_name LIKE ? ";
612 $val = array('%'.$drug.'%');
613 $ndc = sqlQuery($sql, $val);
614 $drug_id = $ndc['NDC'];
615 //Save this drug id
616 $this->drug_id = $drug_id;
618 return $this->drug;
620 function set_ntx($ntx)
622 $this->ntx = $ntx;
624 function get_ntx()
626 return $this->ntx;
629 function set_rxnorm_drugcode($rxnorm_drugcode)
631 $this->rxnorm_drugcode = $rxnorm_drugcode;
633 function get_rxnorm_drugcode()
635 return $this->rxnorm_drugcode;
638 function get_filled_by_id()
640 return $this->pharmacist->id;
642 function set_filled_by_id($id)
644 if (is_numeric($id)) {
645 return $this->pharmacist->id = $id;
649 function set_drug_id($drug_id)
651 $this->drug_id = $drug_id;
653 function get_drug_id()
655 return $this->drug_id;
658 function set_active($active)
660 $this->active = $active;
662 function get_active()
664 return $this->active;
667 function get_prescription_display()
669 $pconfig = $GLOBALS['oer_config']['prescriptions'];
671 switch ($pconfig['format']) {
672 case "FL":
673 return $this->get_prescription_florida_display();
674 break;
675 default:
676 break;
679 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider->id) . "'";
680 $db = get_db();
681 $results = $db->Execute($sql);
682 if (!$results->EOF) {
683 $string = $results->fields['name'] . "\n"
684 . $results->fields['street'] . "\n"
685 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
686 . $results->fields['phone'] . "\n\n";
689 $string .= ""
690 ."Prescription For:" . "\t" .$this->patient->get_name_display() . "\n"
691 ."DOB:"."\t".$this->patient->get_dob()."\n"
692 ."Start Date: " . "\t\t" . $this->start_date. "\n"
693 ."Provider: " . "\t\t" . $this->provider->get_name_display(). "\n"
694 ."Provider DEA No.: " . "\t\t" . $this->provider->federal_drug_id. "\n"
695 ."Drug: " . "\t\t\t" . $this->drug. "\n"
696 ."Dosage: " . "\t\t" . $this->dosage . " in ". $this->form_array[$this->form]. " form " . $this->interval_array[$this->interval]. "\n"
697 ."Qty: " . "\t\t\t" . $this->quantity. "\n"
698 ."Medication Unit: " . "\t" . $this->size . " ". $this->unit_array[$this->unit] . "\n"
699 ."Substitute: " . "\t\t" . $this->substitute_array[$this->substitute]. "\n";
700 if ($this->refills > 0) {
701 $string .= "Refills: " . "\t\t" . $this->refills. ", of quantity: " . $this->per_refill ."\n";
704 $string .= "\n"."Notes: \n" . $this->note . "\n";
705 return $string;
708 function get_prescription_florida_display()
711 $db = get_db();
712 $ntt = new NumberToText($this->quantity);
713 $ntt2 = new NumberToText($this->per_refill);
714 $ntt3 = new NumberToText($this->refills);
716 $string = "";
718 $gnd = $this->provider->get_name_display();
720 while (strlen($gnd)<31) {
721 $gnd .= " ";
724 $string .= $gnd . $this->provider->federal_drug_id . "\n";
726 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider->id) . "'";
727 $results = $db->Execute($sql);
729 if (!$results->EOF) {
730 $rfn = $results->fields['name'];
732 while (strlen($rfn)<31) {
733 $rfn .= " ";
736 $string .= $rfn . $this->provider->get_provider_number_default() . "\n"
737 . $results->fields['street'] . "\n"
738 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
739 . $results->fields['phone'] . "\n";
742 $string .= "\n";
743 $string .= strtoupper($this->patient->lname) . ", " . ucfirst($this->patient->fname) . " " . $this->patient->mname . "\n";
744 $string .= "DOB " . $this->patient->date_of_birth . "\n";
745 $string .= "\n";
746 $string .= date("F j, Y", strtotime($this->start_date)) . "\n";
747 $string .= "\n";
748 $string .= strtoupper($this->drug) . " " . $this->size . " ". $this->unit_array[$this->unit] . "\n";
749 if (strlen($this->note) > 0) {
750 $string .= "Notes: \n" . $this->note . "\n";
753 if (!empty($this->dosage)) {
754 $string .= $this->dosage;
755 if (!empty($this->form)) {
756 $string .= " " . $this->form_array[$this->form];
759 if (!empty($this->interval)) {
760 $string .= " " . $this->interval_array[$this->interval];
763 if (!empty($this->route)) {
764 $string .= " " . $this->route_array[$this->route] . "\n";
768 if (!empty($this->quantity)) {
769 $string .= "Disp: " . $this->quantity . " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
772 $string .= "\n";
773 $string .= "Refills: " . $this->refills . " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill . " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
774 $string .= $this->substitute_array[$this->substitute]. "\n";
775 $string .= "\n";
777 return $string;
780 static function prescriptions_factory(
781 $patient_id,
782 $order_by = "active DESC, date_modified DESC, date_added DESC"
785 $prescriptions = array();
786 $p = new Prescription();
787 $sql = "SELECT id FROM " . $p->_table . " WHERE patient_id = ?" .
788 " ORDER BY " . add_escape_custom($order_by);
789 $results = sqlQ($sql, array($patient_id));
790 while ($row = sqlFetchArray($results)) {
791 $prescriptions[] = new Prescription($row['id']);
794 return $prescriptions;
797 function get_dispensation_count()
799 if (empty($this->id)) {
800 return 0;
803 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
804 "WHERE prescription_id = '" . $this->id . "' AND quantity > 0");
805 return $refills_row['count'];
807 }// end of Prescription