fe0f0d72c565448702464771cc49507fec917b0f
[openemr.git] / library / classes / Prescription.class.php
blobfe0f0d72c565448702464771cc49507fec917b0f
1 <?php
2 require_once (dirname(__FILE__) . "/../sql.inc");
3 require_once (dirname(__FILE__) . "/../lists.inc");
4 require_once("ORDataObject.class.php");
5 require_once("Patient.class.php");
6 require_once("Person.class.php");
7 require_once("Provider.class.php");
8 require_once("Pharmacy.class.php");
9 require_once("NumberToText.class.php");
10 //below is required for the set_medication() function
11 require_once (dirname(__FILE__) . "/../formdata.inc.php");
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);
82 // Added 7-2009 by BM to incorporate the units, forms, interval, route lists from list_options
83 // This mechanism may only be temporary; will likely migrate changes more downstream to allow
84 // users the options of using the addlist widgets and validation frunctions from options.inc.php
85 // in the forms and output.
86 function load_drug_attributes($id) {
87 $res = sqlStatement("SELECT * FROM list_options WHERE list_id = '$id' ORDER BY seq");
88 while ($row = sqlFetchArray($res)) {
89 if ($row['title'] == '') {
90 $arr[$row['option_id']] = ' ';
92 else {
93 $arr[$row['option_id']] = xl_list_label($row['title']);
96 return $arr;
99 /**
100 * class Prescription
103 class Prescription extends ORDataObject {
107 * @access public
112 * static
114 var $form_array;
115 var $unit_array;
116 var $route_array;
117 var $interval_array;
118 var $substitute_array;
119 var $medication_array;
120 var $refills_array;
124 * @access private
127 var $id;
128 var $patient;
129 var $pharmacist;
130 var $date_added;
131 var $date_modified;
132 var $pharmacy;
133 var $start_date;
134 var $filled_date;
135 var $provider;
136 var $note;
137 var $drug;
138 var $form;
139 var $dosage;
140 var $quantity;
141 var $size;
142 var $unit;
143 var $route;
144 var $interval;
145 var $substitute;
146 var $refills;
147 var $per_refill;
148 var $medication;
150 var $drug_id;
151 var $active;
154 * Constructor sets all Prescription attributes to their default value
157 function Prescription($id= "", $_prefix = "") {
159 // Modified 7-2009 by BM to load the arrays from the lists in lists_options.
160 // Plan for this to only be temporary, hopefully have the lists used directly
161 // from forms in future to allow use of widgets etc.
162 $this->route_array = load_drug_attributes('drug_route');
163 $this->form_array = load_drug_attributes('drug_form');
164 $this->interval_array = load_drug_attributes('drug_interval');
165 $this->unit_array = load_drug_attributes('drug_units');
167 $this->substitute_array = array("",xl("substitution allowed"),
168 xl ("do not substitute"));
170 $this->medication_array = array(0 => xl('No'), 1 => xl('Yes'));
172 if (is_numeric($id)) { $this->id = $id; }
173 else { $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 for($i=0;$i<21;$i++) {
198 $this->refills_array[$i] = sprintf("%02d",$i);
201 if ($id != "") { $this->populate(); }
204 function persist() {
205 $this->date_modified = date("Y-m-d");
206 if ($this->id == "") { $this->date_added = date("Y-m-d"); }
207 if (parent::persist()) { }
210 function populate() {
211 parent::populate();
214 function toString($html = false) {
215 $string .= "\n"
216 ."ID: " . $this->id . "\n"
217 ."Patient:" . $this->patient . "\n"
218 ."Patient ID:" . $this->patient->id . "\n"
219 ."Pharmacist: " . $this->pharmacist . "\n"
220 ."Pharmacist ID: " . $this->pharmacist->id . "\n"
221 ."Date Added: " . $this->date_added. "\n"
222 ."Date Modified: " . $this->date_modified. "\n"
223 ."Pharmacy: " . $this->pharmacy. "\n"
224 ."Pharmacy ID:" . $this->pharmacy->id . "\n"
225 ."Start Date: " . $this->start_date. "\n"
226 ."Filled Date: " . $this->filled_date. "\n"
227 ."Provider: " . $this->provider. "\n"
228 ."Provider ID: " . $this->provider->id. "\n"
229 ."Note: " . $this->note. "\n"
230 ."Drug: " . $this->drug. "\n"
231 ."Form: " . $this->form_array[$this->form]. "\n"
232 ."Dosage: " . $this->dosage. "\n"
233 ."Qty: " . $this->quantity. "\n"
234 ."Size: " . $this->size. "\n"
235 ."Unit: " . $this->unit_array[$this->unit] . "\n"
236 ."Route: " . $this->route_array[$this->route] . "\n"
237 ."Interval: " .$this->interval_array[$this->interval]. "\n"
238 ."Substitute: " . $this->substitute_array[$this->substitute]. "\n"
239 ."Refills: " . $this->refills. "\n"
240 ."Per Refill: " . $this->per_refill . "\n"
241 ."Drug ID: " . $this->drug_id . "\n"
242 ."Active: " . $this->active;
244 if ($html) { return nl2br($string); }
245 else { return $string; }
247 function get_encounter(){
248 return $_SESSION['encounter'];
251 function get_unit_display( $display_form="" ) {
252 return( $this->unit_array[$this->unit] );
255 function get_unit() {
256 return $this->unit;
258 function set_unit($unit) {
259 if (is_numeric($unit)) { $this->unit = $unit; }
262 function set_id($id) {
263 if (!empty($id) && is_numeric($id)) { $this->id = $id; }
265 function get_id() {
266 return $this->id;
269 function get_dosage_display( $display_form="" ) {
270 if( empty($this->form) && empty($this->interval) ) {
271 return( $this->dosage );
273 else {
274 return ($this->dosage . " " . xl('in') . " " . $this->form_array[$this->form] . " " . $this->interval_array[$this->interval]);
278 function set_dosage($dosage) {
279 $this->dosage = $dosage;
281 function get_dosage() {
282 return $this->dosage;
285 function set_form($form) {
286 if (is_numeric($form)) { $this->form = $form; }
288 function get_form() {
289 return $this->form;
292 function set_refills($refills) {
293 if (is_numeric($refills)) { $this->refills = $refills; }
295 function get_refills() {
296 return $this->refills;
299 function set_size($size) {
300 if (is_numeric($size)) { $this->size = $size; }
302 function get_size() {
303 return $this->size;
306 function set_quantity($qty) {
307 // if (is_numeric($qty)) {
308 $this->quantity = $qty;
309 // }
311 function get_quantity() {
312 return $this->quantity;
315 function set_route($route) {
316 if (is_numeric($route)) { $this->route = $route; }
318 function get_route() {
319 return $this->route;
322 function set_interval($interval) {
323 if (is_numeric($interval)) { $this->interval = $interval; }
325 function get_interval() {
326 return $this->interval;
329 function set_substitute($sub) {
330 if (is_numeric($sub)) { $this->substitute = $sub; }
332 function get_substitute() {
333 return $this->substitute;
335 function set_erx_source($erx_source) {
336 $this->erx_source = $erx_source;
338 function set_medication($med) {
339 global $ISSUE_TYPES;
341 $this->medication = $med;
343 // Avoid making a mess if we are not using the "medication" issue type.
344 if (isset($ISSUE_TYPES) && !$ISSUE_TYPES['medication']) return;
346 //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
347 // they have already been run through populate() hence stripped of escapes, so now need to be escaped for database (add_escape_custom() function).
349 //check if this drug is on the medication list
350 $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');
352 if ($med && !isset($dataRow['id'])){
353 $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');
355 if (!isset($dataRow['id'])){
356 //add the record to the medication list
357 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) . "')");
359 else {
360 $dataRow = sqlQuery('update lists set activity = 1'
361 . " ,user = '" . $$_SESSION['authUser']
362 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
365 elseif (!$med && isset($dataRow['id'])) {
366 //remove the drug from the medication list if it exists
367 $dataRow = sqlQuery('update lists set activity = 0'
368 . " ,user = '" . $$_SESSION['authUser']
369 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
373 function get_medication() {
374 return $this->medication;
377 function set_per_refill($pr) {
378 if (is_numeric($pr)) { $this->per_refill = $pr; }
380 function get_per_refill() {
381 return $this->per_refill;
384 function set_patient_id($id) {
385 if (is_numeric($id)) { $this->patient = new Patient($id); }
387 function get_patient_id() {
388 return $this->patient->id;
391 function set_provider_id($id) {
392 if (is_numeric($id)) { $this->provider = new Provider($id); }
394 function get_provider_id() {
395 return $this->provider->id;
398 function set_provider($pobj) {
399 if (get_class($pobj) == "provider") { $this->provider = $pobj; }
402 function set_pharmacy_id($id) {
403 if (is_numeric($id)) { $this->pharmacy = new Pharmacy($id); }
405 function get_pharmacy_id() {
406 return $this->pharmacy->id;
409 function set_pharmacist_id($id) {
410 if (is_numeric($id)) { $this->pharmacist = new Person($id); }
412 function get_pharmacist() {
413 return $this->pharmacist->id;
416 function get_start_date_y() {
417 $ymd = split("-",$this->start_date);
418 return $ymd[0];
420 function set_start_date_y($year) {
421 if (is_numeric($year)) {
422 $ymd = split("-",$this->start_date);
423 $ymd[0] = $year;
424 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
427 function get_start_date_m() {
428 $ymd = split("-",$this->start_date);
429 return $ymd[1];
431 function set_start_date_m($month) {
432 if (is_numeric($month)) {
433 $ymd = split("-",$this->start_date);
434 $ymd[1] = $month;
435 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
438 function get_start_date_d() {
439 $ymd = split("-",$this->start_date);
440 return $ymd[2];
442 function set_start_date_d($day) {
443 if (is_numeric($day)) {
444 $ymd = split("-",$this->start_date);
445 $ymd[2] = $day;
446 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
449 function get_start_date() {
450 return $this->start_date;
452 function set_start_date($date) {
453 return $this->start_date = $date;
456 // TajEmo work by CB 2012/05/30 01:56:32 PM added encounter for auto ticking of checkboxes
457 function set_encounter($enc) {
458 return $this->encounter = $enc;
461 function get_date_added() {
462 return $this->date_added;
464 function set_date_added($date) {
465 return $this->date_added = $date;
468 function get_date_modified() {
469 return $this->date_modified;
471 function set_date_modified($date) {
472 return $this->date_modified = $date;
475 function get_filled_date() {
476 return $this->filled_date;
478 function set_filled_date($date) {
479 return $this->filled_date = $date;
482 function set_note($note) {
483 $this->note = $note;
485 function get_note() {
486 return $this->note;
489 function set_drug($drug) {
490 $this->drug = $drug;
492 function get_drug() {
493 return $this->drug;
496 function get_filled_by_id() {
497 return $this->pharmacist->id;
499 function set_filled_by_id($id) {
500 if (is_numeric($id)) { return $this->pharmacist->id = $id; }
503 function set_drug_id($drug_id) {
504 $this->drug_id = $drug_id;
506 function get_drug_id() {
507 return $this->drug_id;
510 function set_active($active) {
511 $this->active = $active;
513 function get_active() {
514 return $this->active;
517 function get_prescription_display() {
518 $pconfig = $GLOBALS['oer_config']['prescriptions'];
520 switch ($pconfig['format']) {
521 case "FL":
522 return $this->get_prescription_florida_display();
523 break;
524 default:
525 break;
528 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
529 $db = get_db();
530 $results = $db->Execute($sql);
531 if (!$results->EOF) {
532 $string = $results->fields['name'] . "\n"
533 . $results->fields['street'] . "\n"
534 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
535 . $results->fields['phone'] . "\n\n";
538 $string .= ""
539 ."Prescription For:" . "\t" .$this->patient->get_name_display() . "\n"
540 ."DOB:"."\t".$this->patient->get_dob()."\n"
541 ."Start Date: " . "\t\t" . $this->start_date. "\n"
542 ."Provider: " . "\t\t" . $this->provider->get_name_display(). "\n"
543 ."Provider DEA No.: " . "\t\t" . $this->provider->federal_drug_id. "\n"
544 ."Drug: " . "\t\t\t" . $this->drug. "\n"
545 ."Dosage: " . "\t\t" . $this->dosage . " in ". $this->form_array[$this->form]. " form " . $this->interval_array[$this->interval]. "\n"
546 ."Qty: " . "\t\t\t" . $this->quantity. "\n"
547 ."Medication Unit: " . "\t" . $this->size . " ". $this->unit_array[$this->unit] . "\n"
548 ."Substitute: " . "\t\t" . $this->substitute_array[$this->substitute]. "\n";
549 if ($this->refills > 0) {
550 $string .= "Refills: " . "\t\t" . $this->refills. ", of quantity: " . $this->per_refill ."\n";
552 $string .= "\n"."Notes: \n" . $this->note . "\n";
553 return $string;
556 function get_prescription_florida_display() {
558 $db = get_db();
559 $ntt = new NumberToText($this->quantity);
560 $ntt2 = new NumberToText($this->per_refill);
561 $ntt3 = new NumberToText($this->refills);
563 $string = "";
565 $gnd = $this->provider->get_name_display();
567 while(strlen($gnd)<31) { $gnd .= " "; }
569 $string .= $gnd . $this->provider->federal_drug_id . "\n";
571 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
572 $results = $db->Execute($sql);
574 if (!$results->EOF) {
575 $rfn = $results->fields['name'];
577 while(strlen($rfn)<31) { $rfn .= " "; }
579 $string .= $rfn . $this->provider->get_provider_number_default() . "\n"
580 . $results->fields['street'] . "\n"
581 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
582 . $results->fields['phone'] . "\n";
585 $string .= "\n";
586 $string .= strtoupper($this->patient->lname) . ", " . ucfirst($this->patient->fname) . " " . $this->patient->mname . "\n";
587 $string .= "DOB " . $this->patient->date_of_birth . "\n";
588 $string .= "\n";
589 $string .= date("F j, Y", strtotime($this->start_date)) . "\n";
590 $string .= "\n";
591 $string .= strtoupper($this->drug) . " " . $this->size . " ". $this->unit_array[$this->unit] . "\n";
592 if (strlen($this->note) > 0) {
593 $string .= "Notes: \n" . $this->note . "\n";
595 if (!empty($this->dosage)) {
596 $string .= $this->dosage;
597 if (!empty($this->form)){
598 $string .= " " . $this->form_array[$this->form];
600 if (!empty($this->interval)) {
601 $string .= " " . $this->interval_array[$this->interval];
603 if (!empty($this->route)) {
604 $string .= " " . $this->route_array[$this->route] . "\n";
607 if (!empty($this->quantity)) {
608 $string .= "Disp: " . $this->quantity . " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
610 $string .= "\n";
611 $string .= "Refills: " . $this->refills . " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill . " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
612 $string .= $this->substitute_array[$this->substitute]. "\n";
613 $string .= "\n";
615 return $string;
618 function prescriptions_factory($patient_id,
619 $order_by = "active DESC, date_modified DESC, date_added DESC")
621 $prescriptions = array();
622 require_once (dirname(__FILE__) . "/../translation.inc.php");
623 $p = new Prescription();
624 $sql = "SELECT id FROM " . $p->_table . " WHERE patient_id = " .
625 mysql_real_escape_string($patient_id) .
626 " ORDER BY " . mysql_real_escape_string($order_by);
627 $results = sqlQ($sql);
628 while ($row = mysql_fetch_array($results) ) {
629 $prescriptions[] = new Prescription($row['id']);
631 return $prescriptions;
634 function get_dispensation_count() {
635 if (empty($this->id)) return 0;
636 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
637 "WHERE prescription_id = '" . $this->id . "' AND quantity > 0");
638 return $refills_row['count'];
641 }// end of Prescription