2 require_once (dirname(__FILE__
) . "/../sql.inc");
3 require_once (dirname(__FILE__
) . "/../lists.inc");
4 require_once (dirname(__FILE__
) . "/../formdata.inc.php");
5 require_once("ORDataObject.class.php");
6 require_once("Patient.class.php");
7 require_once("Person.class.php");
8 require_once("Provider.class.php");
9 require_once("Pharmacy.class.php");
10 require_once("NumberToText.class.php");
11 //below is required for the set_medication() function
12 require_once (dirname(__FILE__
) . "/../formdata.inc.php");
14 // Below list of terms are deprecated, but we keep this list
15 // to keep track of the official openemr drugs terms and
16 // corresponding ID's for reference. Official is referring
17 // to the default settings after installing OpenEMR.
19 // define('UNIT_BLANK',0);
20 // define('UNIT_MG',1);
21 // define('UNIT_MG_1CC',2);
22 // define('UNIT_MG_2CC',3);
23 // define('UNIT_MG_3CC',4);
24 // define('UNIT_MG_4CC',5);
25 // define('UNIT_MG_5CC',6);
26 // define('UNIT_MCG',7);
27 // define('UNIT_GRAMS',8);
29 // define('INTERVAL_BLANK',0);
30 // define('INTERVAL_BID',1);
31 // define('INTERVAL_TID',2);
32 // define('INTERVAL_QID',3);
33 // define('INTERVAL_Q_3H',4);
34 // define('INTERVAL_Q_4H',5);
35 // define('INTERVAL_Q_5H',6);
36 // define('INTERVAL_Q_6H',7);
37 // define('INTERVAL_Q_8H',8);
38 // define('INTERVAL_QD',9);
39 // define('INTERVAL_AC',10); // added May 2008
40 // define('INTERVAL_PC',11); // added May 2008
41 // define('INTERVAL_AM',12); // added May 2008
42 // define('INTERVAL_PM',13); // added May 2008
43 // define('INTERVAL_ANTE',14); // added May 2008
44 // define('INTERVAL_H',15); // added May 2008
45 // define('INTERVAL_HS',16); // added May 2008
46 // define('INTERVAL_PRN',17); // added May 2008
47 // define('INTERVAL_STAT',18); // added May 2008
49 // define('FORM_BLANK',0);
50 // define('FORM_SUSPENSION',1);
51 // define('FORM_TABLET',2);
52 // define('FORM_CAPSULE',3);
53 // define('FORM_SOLUTION',4);
54 // define('FORM_TSP',5);
55 // define('FORM_ML',6);
56 // define('FORM_UNITS',7);
57 // define('FORM_INHILATIONS',8);
58 // define('FORM_GTTS_DROPS',9);
59 // define('FORM_CR',10);
60 // define('FORM_OINT',11);
62 // define('ROUTE_BLANK',0);
63 // define("ROUTE_PER_ORIS", 1);
64 // define("ROUTE_PER_RECTUM", 2);
65 // define("ROUTE_TO_SKIN", 3);
66 // define("ROUTE_TO_AFFECTED_AREA", 4);
67 // define("ROUTE_SUBLINGUAL", 5);
68 // define("ROUTE_OS", 6);
69 // define("ROUTE_OD", 7);
70 // define("ROUTE_OU", 8);
71 // define("ROUTE_SQ", 9);
72 // define("ROUTE_IM", 10);
73 // define("ROUTE_IV", 11);
74 // define("ROUTE_PER_NOSTRIL", 12);
75 // define("ROUTE_B_EAR", 13);
76 // define("ROUTE_L_EAR", 14);
77 // define("ROUTE_R_EAR", 15);
79 // define('SUBSTITUTE_YES',1);
80 // define('SUBSTITUTE_NO',2);
83 // Added 7-2009 by BM to incorporate the units, forms, interval, route lists from list_options
84 // This mechanism may only be temporary; will likely migrate changes more downstream to allow
85 // users the options of using the addlist widgets and validation frunctions from options.inc.php
86 // in the forms and output.
87 function load_drug_attributes($id) {
88 $res = sqlStatement("SELECT * FROM list_options WHERE list_id = '$id' AND activity = 1 ORDER BY seq");
89 while ($row = sqlFetchArray($res)) {
90 if ($row['title'] == '') {
91 $arr[$row['option_id']] = ' ';
94 $arr[$row['option_id']] = xl_list_label($row['title']);
104 class Prescription
extends ORDataObject
{
119 var $substitute_array;
120 var $medication_array;
139 var $rxnorm_drugcode;
156 * Constructor sets all Prescription attributes to their default value
159 function __construct($id= "", $_prefix = "") {
161 // Modified 7-2009 by BM to load the arrays from the lists in lists_options.
162 // Plan for this to only be temporary, hopefully have the lists used directly
163 // from forms in future to allow use of widgets etc.
164 $this->route_array
= load_drug_attributes('drug_route');
165 $this->form_array
= load_drug_attributes('drug_form');
166 $this->interval_array
= load_drug_attributes('drug_interval');
167 $this->unit_array
= load_drug_attributes('drug_units');
169 $this->substitute_array
= array("",xl("substitution allowed"),
170 xl ("do not substitute"));
172 $this->medication_array
= array(0 => xl('No'), 1 => xl('Yes'));
174 if (is_numeric($id)) { $this->id
= $id; }
177 //$this->unit = UNIT_MG;
178 //$this->route = ROUTE_PER_ORIS;
179 //$this->quantity = 1;
182 //$this->form = FORM_TABLET;
183 $this->substitute
= false;
184 $this->_prefix
= $_prefix;
185 $this->_table
= "prescriptions";
186 $this->pharmacy
= new Pharmacy();
187 $this->pharmacist
= new Person();
188 $this->provider
= new Provider($_SESSION['authId']);
189 $this->patient
= new Patient();
190 $this->start_date
= date("Y-m-d");
191 $this->date_added
= date("Y-m-d");
192 $this->date_modified
= date("Y-m-d");
193 $this->per_refill
= 0;
199 for($i=0;$i<21;$i++
) {
200 $this->refills_array
[$i] = sprintf("%02d",$i);
203 if ($id != "") { $this->populate(); }
207 $this->date_modified
= date("Y-m-d");
208 if ($this->id
== "") { $this->date_added
= date("Y-m-d"); }
209 if (parent
::persist()) { }
212 function populate() {
216 function toString($html = false) {
218 ."ID: " . $this->id
. "\n"
219 ."Patient:" . $this->patient
. "\n"
220 ."Patient ID:" . $this->patient
->id
. "\n"
221 ."Pharmacist: " . $this->pharmacist
. "\n"
222 ."Pharmacist ID: " . $this->pharmacist
->id
. "\n"
223 ."Date Added: " . $this->date_added
. "\n"
224 ."Date Modified: " . $this->date_modified
. "\n"
225 ."Pharmacy: " . $this->pharmacy
. "\n"
226 ."Pharmacy ID:" . $this->pharmacy
->id
. "\n"
227 ."Start Date: " . $this->start_date
. "\n"
228 ."Filled Date: " . $this->filled_date
. "\n"
229 ."Provider: " . $this->provider
. "\n"
230 ."Provider ID: " . $this->provider
->id
. "\n"
231 ."Note: " . $this->note
. "\n"
232 ."Drug: " . $this->drug
. "\n"
233 ."Code: " . $this->rxnorm_drugcode
. "\n"
234 ."Form: " . $this->form_array
[$this->form
]. "\n"
235 ."Dosage: " . $this->dosage
. "\n"
236 ."Qty: " . $this->quantity
. "\n"
237 ."Size: " . $this->size
. "\n"
238 ."Unit: " . $this->unit_array
[$this->unit
] . "\n"
239 ."Route: " . $this->route_array
[$this->route
] . "\n"
240 ."Interval: " .$this->interval_array
[$this->interval
]. "\n"
241 ."Substitute: " . $this->substitute_array
[$this->substitute
]. "\n"
242 ."Refills: " . $this->refills
. "\n"
243 ."Per Refill: " . $this->per_refill
. "\n"
244 ."Drug ID: " . $this->drug_id
. "\n"
245 ."Active: " . $this->active
;
247 if ($html) { return nl2br($string); }
248 else { return $string; }
250 function get_encounter(){
251 return $_SESSION['encounter'];
254 function get_unit_display( $display_form="" ) {
255 return( $this->unit_array
[$this->unit
] );
258 function get_unit() {
261 function set_unit($unit) {
262 if (is_numeric($unit)) { $this->unit
= $unit; }
265 function set_id($id) {
266 if (!empty($id) && is_numeric($id)) { $this->id
= $id; }
272 function get_dosage_display( $display_form="" ) {
273 if( empty($this->form
) && empty($this->interval
) ) {
274 return( $this->dosage
);
277 return ($this->dosage
. " " . xl('in') . " " . $this->form_array
[$this->form
] . " " . $this->interval_array
[$this->interval
]);
281 function set_dosage($dosage) {
282 $this->dosage
= $dosage;
284 function get_dosage() {
285 return $this->dosage
;
288 function set_form($form) {
289 if (is_numeric($form)) { $this->form
= $form; }
291 function get_form() {
295 function set_refills($refills) {
296 if (is_numeric($refills)) { $this->refills
= $refills; }
298 function get_refills() {
299 return $this->refills
;
302 function set_size($size) {
303 $this->size
= preg_replace("/[^0-9\/\.\-]/", "",$size);
305 function get_size() {
309 function set_quantity($qty) {
310 $this->quantity
= $qty;
312 function get_quantity() {
313 return $this->quantity
;
316 function set_route($route) {
317 if (is_numeric($route)) { $this->route
= $route; }
319 function get_route() {
323 function set_interval($interval) {
324 if (is_numeric($interval)) { $this->interval
= $interval; }
326 function get_interval() {
327 return $this->interval
;
330 function set_substitute($sub) {
331 if (is_numeric($sub)) { $this->substitute
= $sub; }
333 function get_substitute() {
334 return $this->substitute
;
336 function set_erx_source($erx_source) {
337 $this->erx_source
= $erx_source;
339 function set_medication($med) {
342 $this->medication
= $med;
344 // Avoid making a mess if we are not using the "medication" issue type.
345 if (isset($ISSUE_TYPES) && !$ISSUE_TYPES['medication']) return;
347 //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
348 // they have already been run through populate() hence stripped of escapes, so now need to be escaped for database (add_escape_custom() function).
350 //check if this drug is on the medication list
351 $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');
353 if ($med && !isset($dataRow['id'])){
354 $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');
356 if (!isset($dataRow['id'])){
357 //add the record to the medication list
358 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
) . "')");
361 $dataRow = sqlQuery('update lists set activity = 1'
362 . " ,user = '" . $
$_SESSION['authUser']
363 . "', groupname = '" . $
$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
366 elseif (!$med && isset($dataRow['id'])) {
367 //remove the drug from the medication list if it exists
368 $dataRow = sqlQuery('update lists set activity = 0'
369 . " ,user = '" . $
$_SESSION['authUser']
370 . "', groupname = '" . $
$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
374 function get_medication() {
375 return $this->medication
;
378 function set_per_refill($pr) {
379 if (is_numeric($pr)) { $this->per_refill
= $pr; }
381 function get_per_refill() {
382 return $this->per_refill
;
385 function set_patient_id($id) {
386 if (is_numeric($id)) { $this->patient
= new Patient($id); }
388 function get_patient_id() {
389 return $this->patient
->id
;
392 function set_provider_id($id) {
393 if (is_numeric($id)) { $this->provider
= new Provider($id); }
395 function get_provider_id() {
396 return $this->provider
->id
;
399 function set_provider($pobj) {
400 if (get_class($pobj) == "provider") { $this->provider
= $pobj; }
403 function set_pharmacy_id($id) {
404 if (is_numeric($id)) { $this->pharmacy
= new Pharmacy($id); }
406 function get_pharmacy_id() {
407 return $this->pharmacy
->id
;
410 function set_pharmacist_id($id) {
411 if (is_numeric($id)) { $this->pharmacist
= new Person($id); }
413 function get_pharmacist() {
414 return $this->pharmacist
->id
;
417 function get_start_date_y() {
418 $ymd = explode("-",$this->start_date
);
421 function set_start_date_y($year) {
422 if (is_numeric($year)) {
423 $ymd = explode("-",$this->start_date
);
425 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
428 function get_start_date_m() {
429 $ymd = explode("-",$this->start_date
);
432 function set_start_date_m($month) {
433 if (is_numeric($month)) {
434 $ymd = explode("-",$this->start_date
);
436 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
439 function get_start_date_d() {
440 $ymd = explode("-",$this->start_date
);
443 function set_start_date_d($day) {
444 if (is_numeric($day)) {
445 $ymd = explode("-",$this->start_date
);
447 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
450 function get_start_date() {
451 return $this->start_date
;
453 function set_start_date($date) {
454 return $this->start_date
= $date;
457 // TajEmo work by CB 2012/05/30 01:56:32 PM added encounter for auto ticking of checkboxes
458 function set_encounter($enc) {
459 return $this->encounter
= $enc;
462 function get_date_added() {
463 return $this->date_added
;
465 function set_date_added($date) {
466 return $this->date_added
= $date;
469 function get_date_modified() {
470 return $this->date_modified
;
472 function set_date_modified($date) {
473 return $this->date_modified
= $date;
476 function get_filled_date() {
477 return $this->filled_date
;
479 function set_filled_date($date) {
480 return $this->filled_date
= $date;
483 function set_note($note) {
486 function get_note() {
490 function set_drug($drug) {
493 function get_drug() {
497 function set_rxnorm_drugcode($rxnorm_drugcode) {
498 $this->rxnorm_drugcode
= $rxnorm_drugcode;
500 function get_rxnorm_drugcode() {
501 return $this->rxnorm_drugcode
;
504 function get_filled_by_id() {
505 return $this->pharmacist
->id
;
507 function set_filled_by_id($id) {
508 if (is_numeric($id)) { return $this->pharmacist
->id
= $id; }
511 function set_drug_id($drug_id) {
512 $this->drug_id
= $drug_id;
514 function get_drug_id() {
515 return $this->drug_id
;
518 function set_active($active) {
519 $this->active
= $active;
521 function get_active() {
522 return $this->active
;
525 function get_prescription_display() {
526 $pconfig = $GLOBALS['oer_config']['prescriptions'];
528 switch ($pconfig['format']) {
530 return $this->get_prescription_florida_display();
536 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider
->id
) . "'";
538 $results = $db->Execute($sql);
539 if (!$results->EOF
) {
540 $string = $results->fields
['name'] . "\n"
541 . $results->fields
['street'] . "\n"
542 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
543 . $results->fields
['phone'] . "\n\n";
547 ."Prescription For:" . "\t" .$this->patient
->get_name_display() . "\n"
548 ."DOB:"."\t".$this->patient
->get_dob()."\n"
549 ."Start Date: " . "\t\t" . $this->start_date
. "\n"
550 ."Provider: " . "\t\t" . $this->provider
->get_name_display(). "\n"
551 ."Provider DEA No.: " . "\t\t" . $this->provider
->federal_drug_id
. "\n"
552 ."Drug: " . "\t\t\t" . $this->drug
. "\n"
553 ."Dosage: " . "\t\t" . $this->dosage
. " in ". $this->form_array
[$this->form
]. " form " . $this->interval_array
[$this->interval
]. "\n"
554 ."Qty: " . "\t\t\t" . $this->quantity
. "\n"
555 ."Medication Unit: " . "\t" . $this->size
. " ". $this->unit_array
[$this->unit
] . "\n"
556 ."Substitute: " . "\t\t" . $this->substitute_array
[$this->substitute
]. "\n";
557 if ($this->refills
> 0) {
558 $string .= "Refills: " . "\t\t" . $this->refills
. ", of quantity: " . $this->per_refill
."\n";
560 $string .= "\n"."Notes: \n" . $this->note
. "\n";
564 function get_prescription_florida_display() {
567 $ntt = new NumberToText($this->quantity
);
568 $ntt2 = new NumberToText($this->per_refill
);
569 $ntt3 = new NumberToText($this->refills
);
573 $gnd = $this->provider
->get_name_display();
575 while(strlen($gnd)<31) { $gnd .= " "; }
577 $string .= $gnd . $this->provider
->federal_drug_id
. "\n";
579 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider
->id
) . "'";
580 $results = $db->Execute($sql);
582 if (!$results->EOF
) {
583 $rfn = $results->fields
['name'];
585 while(strlen($rfn)<31) { $rfn .= " "; }
587 $string .= $rfn . $this->provider
->get_provider_number_default() . "\n"
588 . $results->fields
['street'] . "\n"
589 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
590 . $results->fields
['phone'] . "\n";
594 $string .= strtoupper($this->patient
->lname
) . ", " . ucfirst($this->patient
->fname
) . " " . $this->patient
->mname
. "\n";
595 $string .= "DOB " . $this->patient
->date_of_birth
. "\n";
597 $string .= date("F j, Y", strtotime($this->start_date
)) . "\n";
599 $string .= strtoupper($this->drug
) . " " . $this->size
. " ". $this->unit_array
[$this->unit
] . "\n";
600 if (strlen($this->note
) > 0) {
601 $string .= "Notes: \n" . $this->note
. "\n";
603 if (!empty($this->dosage
)) {
604 $string .= $this->dosage
;
605 if (!empty($this->form
)){
606 $string .= " " . $this->form_array
[$this->form
];
608 if (!empty($this->interval
)) {
609 $string .= " " . $this->interval_array
[$this->interval
];
611 if (!empty($this->route
)) {
612 $string .= " " . $this->route_array
[$this->route
] . "\n";
615 if (!empty($this->quantity
)) {
616 $string .= "Disp: " . $this->quantity
. " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
619 $string .= "Refills: " . $this->refills
. " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill
. " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
620 $string .= $this->substitute_array
[$this->substitute
]. "\n";
626 static function prescriptions_factory($patient_id,
627 $order_by = "active DESC, date_modified DESC, date_added DESC")
629 $prescriptions = array();
630 require_once (dirname(__FILE__
) . "/../translation.inc.php");
631 $p = new Prescription();
632 $sql = "SELECT id FROM " . $p->_table
. " WHERE patient_id = " .
633 add_escape_custom($patient_id) .
634 " ORDER BY " . add_escape_custom($order_by);
635 $results = sqlQ($sql);
636 while ($row = sqlFetchArray($results) ) {
637 $prescriptions[] = new Prescription($row['id']);
639 return $prescriptions;
642 function get_dispensation_count() {
643 if (empty($this->id
)) return 0;
644 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
645 "WHERE prescription_id = '" . $this->id
. "' AND quantity > 0");
646 return $refills_row['count'];
649 }// end of Prescription