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.
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) {
79 $res = sqlStatement("SELECT * FROM list_options WHERE list_id = '$id' AND activity = 1 ORDER BY seq");
80 while ($row = sqlFetchArray($res)) {
81 if ($row['title'] == '') {
82 $arr[$row['option_id']] = ' ';
85 $arr[$row['option_id']] = xl_list_label($row['title']);
95 class Prescription
extends ORDataObject
{
110 var $substitute_array;
111 var $medication_array;
130 var $rxnorm_drugcode;
147 * Constructor sets all Prescription attributes to their default value
150 function __construct($id= "", $_prefix = "") {
152 // Modified 7-2009 by BM to load the arrays from the lists in lists_options.
153 // Plan for this to only be temporary, hopefully have the lists used directly
154 // from forms in future to allow use of widgets etc.
155 $this->route_array
= load_drug_attributes('drug_route');
156 $this->form_array
= load_drug_attributes('drug_form');
157 $this->interval_array
= load_drug_attributes('drug_interval');
158 $this->unit_array
= load_drug_attributes('drug_units');
160 $this->substitute_array
= array("",xl("substitution allowed"),
161 xl ("do not substitute"));
163 $this->medication_array
= array(0 => xl('No'), 1 => xl('Yes'));
165 if (is_numeric($id)) { $this->id
= $id; }
168 //$this->unit = UNIT_MG;
169 //$this->route = ROUTE_PER_ORIS;
170 //$this->quantity = 1;
173 //$this->form = FORM_TABLET;
174 $this->substitute
= false;
175 $this->_prefix
= $_prefix;
176 $this->_table
= "prescriptions";
177 $this->pharmacy
= new Pharmacy();
178 $this->pharmacist
= new Person();
179 $this->provider
= new Provider($_SESSION['authId']);
180 $this->patient
= new Patient();
181 $this->start_date
= date("Y-m-d");
182 $this->date_added
= date("Y-m-d");
183 $this->date_modified
= date("Y-m-d");
184 $this->per_refill
= 0;
190 for($i=0;$i<21;$i++
) {
191 $this->refills_array
[$i] = sprintf("%02d",$i);
194 if ($id != "") { $this->populate(); }
198 $this->date_modified
= date("Y-m-d");
199 if ($this->id
== "") { $this->date_added
= date("Y-m-d"); }
200 if (parent
::persist()) { }
203 function populate() {
207 function toString($html = false) {
209 ."ID: " . $this->id
. "\n"
210 ."Patient:" . $this->patient
. "\n"
211 ."Patient ID:" . $this->patient
->id
. "\n"
212 ."Pharmacist: " . $this->pharmacist
. "\n"
213 ."Pharmacist ID: " . $this->pharmacist
->id
. "\n"
214 ."Date Added: " . $this->date_added
. "\n"
215 ."Date Modified: " . $this->date_modified
. "\n"
216 ."Pharmacy: " . $this->pharmacy
. "\n"
217 ."Pharmacy ID:" . $this->pharmacy
->id
. "\n"
218 ."Start Date: " . $this->start_date
. "\n"
219 ."Filled Date: " . $this->filled_date
. "\n"
220 ."Provider: " . $this->provider
. "\n"
221 ."Provider ID: " . $this->provider
->id
. "\n"
222 ."Note: " . $this->note
. "\n"
223 ."Drug: " . $this->drug
. "\n"
224 ."Code: " . $this->rxnorm_drugcode
. "\n"
225 ."Form: " . $this->form_array
[$this->form
]. "\n"
226 ."Dosage: " . $this->dosage
. "\n"
227 ."Qty: " . $this->quantity
. "\n"
228 ."Size: " . $this->size
. "\n"
229 ."Unit: " . $this->unit_array
[$this->unit
] . "\n"
230 ."Route: " . $this->route_array
[$this->route
] . "\n"
231 ."Interval: " .$this->interval_array
[$this->interval
]. "\n"
232 ."Substitute: " . $this->substitute_array
[$this->substitute
]. "\n"
233 ."Refills: " . $this->refills
. "\n"
234 ."Per Refill: " . $this->per_refill
. "\n"
235 ."Drug ID: " . $this->drug_id
. "\n"
236 ."Active: " . $this->active
;
238 if ($html) { return nl2br($string); }
239 else { return $string; }
241 function get_encounter(){
242 return $_SESSION['encounter'];
245 function get_unit_display( $display_form="" ) {
246 return( $this->unit_array
[$this->unit
] );
249 function get_unit() {
252 function set_unit($unit) {
253 if (is_numeric($unit)) { $this->unit
= $unit; }
256 function set_id($id) {
257 if (!empty($id) && is_numeric($id)) { $this->id
= $id; }
263 function get_dosage_display( $display_form="" ) {
264 if( empty($this->form
) && empty($this->interval
) ) {
265 return( $this->dosage
);
268 return ($this->dosage
. " " . xl('in') . " " . $this->form_array
[$this->form
] . " " . $this->interval_array
[$this->interval
]);
272 function set_dosage($dosage) {
273 $this->dosage
= $dosage;
275 function get_dosage() {
276 return $this->dosage
;
279 function set_form($form) {
280 if (is_numeric($form)) { $this->form
= $form; }
282 function get_form() {
286 function set_refills($refills) {
287 if (is_numeric($refills)) { $this->refills
= $refills; }
289 function get_refills() {
290 return $this->refills
;
293 function set_size($size) {
294 $this->size
= preg_replace("/[^0-9\/\.\-]/", "",$size);
296 function get_size() {
300 function set_quantity($qty) {
301 $this->quantity
= $qty;
303 function get_quantity() {
304 return $this->quantity
;
307 function set_route($route) {
308 if (is_numeric($route)) { $this->route
= $route; }
310 function get_route() {
314 function set_interval($interval) {
315 if (is_numeric($interval)) { $this->interval
= $interval; }
317 function get_interval() {
318 return $this->interval
;
321 function set_substitute($sub) {
322 if (is_numeric($sub)) { $this->substitute
= $sub; }
324 function get_substitute() {
325 return $this->substitute
;
327 function set_erx_source($erx_source) {
328 $this->erx_source
= $erx_source;
330 function set_medication($med) {
333 $this->medication
= $med;
335 // Avoid making a mess if we are not using the "medication" issue type.
336 if (isset($ISSUE_TYPES) && !$ISSUE_TYPES['medication']) return;
338 //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
339 // they have already been run through populate() hence stripped of escapes, so now need to be escaped for database (add_escape_custom() function).
341 //check if this drug is on the medication list
342 $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');
344 if ($med && !isset($dataRow['id'])){
345 $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');
347 if (!isset($dataRow['id'])){
348 //add the record to the medication list
349 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
) . "')");
352 $dataRow = sqlQuery('update lists set activity = 1'
353 . " ,user = '" . $
$_SESSION['authUser']
354 . "', groupname = '" . $
$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
357 elseif (!$med && isset($dataRow['id'])) {
358 //remove the drug from the medication list if it exists
359 $dataRow = sqlQuery('update lists set activity = 0'
360 . " ,user = '" . $
$_SESSION['authUser']
361 . "', groupname = '" . $
$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
365 function get_medication() {
366 return $this->medication
;
369 function set_per_refill($pr) {
370 if (is_numeric($pr)) { $this->per_refill
= $pr; }
372 function get_per_refill() {
373 return $this->per_refill
;
376 function set_patient_id($id) {
377 if (is_numeric($id)) { $this->patient
= new Patient($id); }
379 function get_patient_id() {
380 return $this->patient
->id
;
383 function set_provider_id($id) {
384 if (is_numeric($id)) { $this->provider
= new Provider($id); }
386 function get_provider_id() {
387 return $this->provider
->id
;
390 function set_provider($pobj) {
391 if (get_class($pobj) == "provider") { $this->provider
= $pobj; }
394 function set_pharmacy_id($id) {
395 if (is_numeric($id)) { $this->pharmacy
= new Pharmacy($id); }
397 function get_pharmacy_id() {
398 return $this->pharmacy
->id
;
401 function set_pharmacist_id($id) {
402 if (is_numeric($id)) { $this->pharmacist
= new Person($id); }
404 function get_pharmacist() {
405 return $this->pharmacist
->id
;
408 function get_start_date_y() {
409 $ymd = explode("-",$this->start_date
);
412 function set_start_date_y($year) {
413 if (is_numeric($year)) {
414 $ymd = explode("-",$this->start_date
);
416 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
419 function get_start_date_m() {
420 $ymd = explode("-",$this->start_date
);
423 function set_start_date_m($month) {
424 if (is_numeric($month)) {
425 $ymd = explode("-",$this->start_date
);
427 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
430 function get_start_date_d() {
431 $ymd = explode("-",$this->start_date
);
434 function set_start_date_d($day) {
435 if (is_numeric($day)) {
436 $ymd = explode("-",$this->start_date
);
438 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
441 function get_start_date() {
442 return $this->start_date
;
444 function set_start_date($date) {
445 return $this->start_date
= $date;
448 // TajEmo work by CB 2012/05/30 01:56:32 PM added encounter for auto ticking of checkboxes
449 function set_encounter($enc) {
450 return $this->encounter
= $enc;
453 function get_date_added() {
454 return $this->date_added
;
456 function set_date_added($date) {
457 return $this->date_added
= $date;
460 function get_date_modified() {
461 return $this->date_modified
;
463 function set_date_modified($date) {
464 return $this->date_modified
= $date;
467 function get_filled_date() {
468 return $this->filled_date
;
470 function set_filled_date($date) {
471 return $this->filled_date
= $date;
474 function set_note($note) {
477 function get_note() {
481 function set_drug($drug) {
484 function get_drug() {
488 function set_rxnorm_drugcode($rxnorm_drugcode) {
489 $this->rxnorm_drugcode
= $rxnorm_drugcode;
491 function get_rxnorm_drugcode() {
492 return $this->rxnorm_drugcode
;
495 function get_filled_by_id() {
496 return $this->pharmacist
->id
;
498 function set_filled_by_id($id) {
499 if (is_numeric($id)) { return $this->pharmacist
->id
= $id; }
502 function set_drug_id($drug_id) {
503 $this->drug_id
= $drug_id;
505 function get_drug_id() {
506 return $this->drug_id
;
509 function set_active($active) {
510 $this->active
= $active;
512 function get_active() {
513 return $this->active
;
516 function get_prescription_display() {
517 $pconfig = $GLOBALS['oer_config']['prescriptions'];
519 switch ($pconfig['format']) {
521 return $this->get_prescription_florida_display();
527 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider
->id
) . "'";
529 $results = $db->Execute($sql);
530 if (!$results->EOF
) {
531 $string = $results->fields
['name'] . "\n"
532 . $results->fields
['street'] . "\n"
533 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
534 . $results->fields
['phone'] . "\n\n";
538 ."Prescription For:" . "\t" .$this->patient
->get_name_display() . "\n"
539 ."DOB:"."\t".$this->patient
->get_dob()."\n"
540 ."Start Date: " . "\t\t" . $this->start_date
. "\n"
541 ."Provider: " . "\t\t" . $this->provider
->get_name_display(). "\n"
542 ."Provider DEA No.: " . "\t\t" . $this->provider
->federal_drug_id
. "\n"
543 ."Drug: " . "\t\t\t" . $this->drug
. "\n"
544 ."Dosage: " . "\t\t" . $this->dosage
. " in ". $this->form_array
[$this->form
]. " form " . $this->interval_array
[$this->interval
]. "\n"
545 ."Qty: " . "\t\t\t" . $this->quantity
. "\n"
546 ."Medication Unit: " . "\t" . $this->size
. " ". $this->unit_array
[$this->unit
] . "\n"
547 ."Substitute: " . "\t\t" . $this->substitute_array
[$this->substitute
]. "\n";
548 if ($this->refills
> 0) {
549 $string .= "Refills: " . "\t\t" . $this->refills
. ", of quantity: " . $this->per_refill
."\n";
551 $string .= "\n"."Notes: \n" . $this->note
. "\n";
555 function get_prescription_florida_display() {
558 $ntt = new NumberToText($this->quantity
);
559 $ntt2 = new NumberToText($this->per_refill
);
560 $ntt3 = new NumberToText($this->refills
);
564 $gnd = $this->provider
->get_name_display();
566 while(strlen($gnd)<31) { $gnd .= " "; }
568 $string .= $gnd . $this->provider
->federal_drug_id
. "\n";
570 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider
->id
) . "'";
571 $results = $db->Execute($sql);
573 if (!$results->EOF
) {
574 $rfn = $results->fields
['name'];
576 while(strlen($rfn)<31) { $rfn .= " "; }
578 $string .= $rfn . $this->provider
->get_provider_number_default() . "\n"
579 . $results->fields
['street'] . "\n"
580 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
581 . $results->fields
['phone'] . "\n";
585 $string .= strtoupper($this->patient
->lname
) . ", " . ucfirst($this->patient
->fname
) . " " . $this->patient
->mname
. "\n";
586 $string .= "DOB " . $this->patient
->date_of_birth
. "\n";
588 $string .= date("F j, Y", strtotime($this->start_date
)) . "\n";
590 $string .= strtoupper($this->drug
) . " " . $this->size
. " ". $this->unit_array
[$this->unit
] . "\n";
591 if (strlen($this->note
) > 0) {
592 $string .= "Notes: \n" . $this->note
. "\n";
594 if (!empty($this->dosage
)) {
595 $string .= $this->dosage
;
596 if (!empty($this->form
)){
597 $string .= " " . $this->form_array
[$this->form
];
599 if (!empty($this->interval
)) {
600 $string .= " " . $this->interval_array
[$this->interval
];
602 if (!empty($this->route
)) {
603 $string .= " " . $this->route_array
[$this->route
] . "\n";
606 if (!empty($this->quantity
)) {
607 $string .= "Disp: " . $this->quantity
. " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
610 $string .= "Refills: " . $this->refills
. " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill
. " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
611 $string .= $this->substitute_array
[$this->substitute
]. "\n";
617 static function prescriptions_factory($patient_id,
618 $order_by = "active DESC, date_modified DESC, date_added DESC")
620 $prescriptions = array();
621 $p = new Prescription();
622 $sql = "SELECT id FROM " . $p->_table
. " WHERE patient_id = " .
623 add_escape_custom($patient_id) .
624 " ORDER BY " . add_escape_custom($order_by);
625 $results = sqlQ($sql);
626 while ($row = sqlFetchArray($results) ) {
627 $prescriptions[] = new Prescription($row['id']);
629 return $prescriptions;
632 function get_dispensation_count() {
633 if (empty($this->id
)) return 0;
634 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
635 "WHERE prescription_id = '" . $this->id
. "' AND quantity > 0");
636 return $refills_row['count'];
639 }// end of Prescription