Enhance Photo and Patient ID card handling with a widget that supports thumnails...
[openemr.git] / library / classes / Prescription.class.php
blob220b50c1c941c2433944aaf318fb90e067dc861a
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; }
248 function get_unit_display( $display_form="" ) {
249 return( $this->unit_array[$this->unit] );
252 function get_unit() {
253 return $this->unit;
255 function set_unit($unit) {
256 if (is_numeric($unit)) { $this->unit = $unit; }
259 function set_id($id) {
260 if (!empty($id) && is_numeric($id)) { $this->id = $id; }
262 function get_id() {
263 return $this->id;
266 function get_dosage_display( $display_form="" ) {
267 if( empty($this->form) && empty($this->interval) ) {
268 return( $this->dosage );
270 else {
271 return ($this->dosage . " " . xl('in') . " " . $this->form_array[$this->form] . " " . $this->interval_array[$this->interval]);
275 function set_dosage($dosage) {
276 $this->dosage = $dosage;
278 function get_dosage() {
279 return $this->dosage;
282 function set_form($form) {
283 if (is_numeric($form)) { $this->form = $form; }
285 function get_form() {
286 return $this->form;
289 function set_refills($refills) {
290 if (is_numeric($refills)) { $this->refills = $refills; }
292 function get_refills() {
293 return $this->refills;
296 function set_size($size) {
297 if (is_numeric($size)) { $this->size = $size; }
299 function get_size() {
300 return $this->size;
303 function set_quantity($qty) {
304 // if (is_numeric($qty)) {
305 $this->quantity = $qty;
306 // }
308 function get_quantity() {
309 return $this->quantity;
312 function set_route($route) {
313 if (is_numeric($route)) { $this->route = $route; }
315 function get_route() {
316 return $this->route;
319 function set_interval($interval) {
320 if (is_numeric($interval)) { $this->interval = $interval; }
322 function get_interval() {
323 return $this->interval;
326 function set_substitute($sub) {
327 if (is_numeric($sub)) { $this->substitute = $sub; }
329 function get_substitute() {
330 return $this->substitute;
333 function set_medication($med) {
334 global $ISSUE_TYPES;
336 $this->medication = $med;
338 // Avoid making a mess if we are not using the "medication" issue type.
339 if (isset($ISSUE_TYPES) && !$ISSUE_TYPES['medication']) return;
341 //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
342 // they have already been run through populate() hence stripped of escapes, so now need to be escaped for database (add_escape_custom() function).
344 //check if this drug is on the medication list
345 $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');
347 if ($med && !isset($dataRow['id'])){
348 $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');
350 if (!isset($dataRow['id'])){
351 //add the record to the medication list
352 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) . "')");
354 else {
355 $dataRow = sqlQuery('update lists set activity = 1'
356 . " ,user = '" . $$_SESSION['authUser']
357 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
360 elseif (!$med && isset($dataRow['id'])) {
361 //remove the drug from the medication list if it exists
362 $dataRow = sqlQuery('update lists set activity = 0'
363 . " ,user = '" . $$_SESSION['authUser']
364 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
368 function get_medication() {
369 return $this->medication;
372 function set_per_refill($pr) {
373 if (is_numeric($pr)) { $this->per_refill = $pr; }
375 function get_per_refill() {
376 return $this->per_refill;
379 function set_patient_id($id) {
380 if (is_numeric($id)) { $this->patient = new Patient($id); }
382 function get_patient_id() {
383 return $this->patient->id;
386 function set_provider_id($id) {
387 if (is_numeric($id)) { $this->provider = new Provider($id); }
389 function get_provider_id() {
390 return $this->provider->id;
393 function set_provider($pobj) {
394 if (get_class($pobj) == "provider") { $this->provider = $pobj; }
397 function set_pharmacy_id($id) {
398 if (is_numeric($id)) { $this->pharmacy = new Pharmacy($id); }
400 function get_pharmacy_id() {
401 return $this->pharmacy->id;
404 function set_pharmacist_id($id) {
405 if (is_numeric($id)) { $this->pharmacist = new Person($id); }
407 function get_pharmacist() {
408 return $this->pharmacist->id;
411 function get_start_date_y() {
412 $ymd = split("-",$this->start_date);
413 return $ymd[0];
415 function set_start_date_y($year) {
416 if (is_numeric($year)) {
417 $ymd = split("-",$this->start_date);
418 $ymd[0] = $year;
419 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
422 function get_start_date_m() {
423 $ymd = split("-",$this->start_date);
424 return $ymd[1];
426 function set_start_date_m($month) {
427 if (is_numeric($month)) {
428 $ymd = split("-",$this->start_date);
429 $ymd[1] = $month;
430 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
433 function get_start_date_d() {
434 $ymd = split("-",$this->start_date);
435 return $ymd[2];
437 function set_start_date_d($day) {
438 if (is_numeric($day)) {
439 $ymd = split("-",$this->start_date);
440 $ymd[2] = $day;
441 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
444 function get_start_date() {
445 return $this->start_date;
447 function set_start_date($date) {
448 return $this->start_date = $date;
451 function get_date_added() {
452 return $this->date_added;
454 function set_date_added($date) {
455 return $this->date_added = $date;
458 function get_date_modified() {
459 return $this->date_modified;
461 function set_date_modified($date) {
462 return $this->date_modified = $date;
465 function get_filled_date() {
466 return $this->filled_date;
468 function set_filled_date($date) {
469 return $this->filled_date = $date;
472 function set_note($note) {
473 $this->note = $note;
475 function get_note() {
476 return $this->note;
479 function set_drug($drug) {
480 $this->drug = $drug;
482 function get_drug() {
483 return $this->drug;
486 function get_filled_by_id() {
487 return $this->pharmacist->id;
489 function set_filled_by_id($id) {
490 if (is_numeric($id)) { return $this->pharmacist->id = $id; }
493 function set_drug_id($drug_id) {
494 $this->drug_id = $drug_id;
496 function get_drug_id() {
497 return $this->drug_id;
500 function set_active($active) {
501 $this->active = $active;
503 function get_active() {
504 return $this->active;
507 function get_prescription_display() {
508 $pconfig = $GLOBALS['oer_config']['prescriptions'];
510 switch ($pconfig['format']) {
511 case "FL":
512 return $this->get_prescription_florida_display();
513 break;
514 default:
515 break;
518 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
519 $db = get_db();
520 $results = $db->Execute($sql);
521 if (!$results->EOF) {
522 $string = $results->fields['name'] . "\n"
523 . $results->fields['street'] . "\n"
524 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
525 . $results->fields['phone'] . "\n\n";
528 $string .= ""
529 ."Prescription For:" . "\t" .$this->patient->get_name_display() . "\n"
530 ."DOB:"."\t".$this->patient->get_dob()."\n"
531 ."Start Date: " . "\t\t" . $this->start_date. "\n"
532 ."Provider: " . "\t\t" . $this->provider->get_name_display(). "\n"
533 ."Provider DEA No.: " . "\t\t" . $this->provider->federal_drug_id. "\n"
534 ."Drug: " . "\t\t\t" . $this->drug. "\n"
535 ."Dosage: " . "\t\t" . $this->dosage . " in ". $this->form_array[$this->form]. " form " . $this->interval_array[$this->interval]. "\n"
536 ."Qty: " . "\t\t\t" . $this->quantity. "\n"
537 ."Medication Unit: " . "\t" . $this->size . " ". $this->unit_array[$this->unit] . "\n"
538 ."Substitute: " . "\t\t" . $this->substitute_array[$this->substitute]. "\n";
539 if ($this->refills > 0) {
540 $string .= "Refills: " . "\t\t" . $this->refills. ", of quantity: " . $this->per_refill ."\n";
542 $string .= "\n"."Notes: \n" . $this->note . "\n";
543 return $string;
546 function get_prescription_florida_display() {
548 $db = get_db();
549 $ntt = new NumberToText($this->quantity);
550 $ntt2 = new NumberToText($this->per_refill);
551 $ntt3 = new NumberToText($this->refills);
553 $string = "";
555 $gnd = $this->provider->get_name_display();
557 while(strlen($gnd)<31) { $gnd .= " "; }
559 $string .= $gnd . $this->provider->federal_drug_id . "\n";
561 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
562 $results = $db->Execute($sql);
564 if (!$results->EOF) {
565 $rfn = $results->fields['name'];
567 while(strlen($rfn)<31) { $rfn .= " "; }
569 $string .= $rfn . $this->provider->get_provider_number_default() . "\n"
570 . $results->fields['street'] . "\n"
571 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
572 . $results->fields['phone'] . "\n";
575 $string .= "\n";
576 $string .= strtoupper($this->patient->lname) . ", " . ucfirst($this->patient->fname) . " " . $this->patient->mname . "\n";
577 $string .= "DOB " . $this->patient->date_of_birth . "\n";
578 $string .= "\n";
579 $string .= date("F j, Y", strtotime($this->start_date)) . "\n";
580 $string .= "\n";
581 $string .= strtoupper($this->drug) . " " . $this->size . " ". $this->unit_array[$this->unit] . "\n";
582 if (strlen($this->note) > 0) {
583 $string .= "Notes: \n" . $this->note . "\n";
585 if (!empty($this->dosage)) {
586 $string .= $this->dosage;
587 if (!empty($this->form)){
588 $string .= " " . $this->form_array[$this->form];
590 if (!empty($this->interval)) {
591 $string .= " " . $this->interval_array[$this->interval];
593 if (!empty($this->route)) {
594 $string .= " " . $this->route_array[$this->route] . "\n";
597 if (!empty($this->quantity)) {
598 $string .= "Disp: " . $this->quantity . " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
600 $string .= "\n";
601 $string .= "Refills: " . $this->refills . " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill . " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
602 $string .= $this->substitute_array[$this->substitute]. "\n";
603 $string .= "\n";
605 return $string;
608 function prescriptions_factory($patient_id,
609 $order_by = "active DESC, date_modified DESC, date_added DESC")
611 $prescriptions = array();
612 require_once (dirname(__FILE__) . "/../translation.inc.php");
613 $p = new Prescription();
614 $sql = "SELECT id FROM " . $p->_table . " WHERE patient_id = " .
615 mysql_real_escape_string($patient_id) .
616 " ORDER BY " . mysql_real_escape_string($order_by);
617 $results = sqlQ($sql);
618 while ($row = mysql_fetch_array($results) ) {
619 $prescriptions[] = new Prescription($row['id']);
621 return $prescriptions;
624 function get_dispensation_count() {
625 if (empty($this->id)) return 0;
626 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
627 "WHERE prescription_id = '" . $this->id . "' AND quantity > 0");
628 return $refills_row['count'];
631 }// end of Prescription