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
12 // Below list of terms are deprecated, but we keep this list
13 // to keep track of the official openemr drugs terms and
14 // corresponding ID's for reference. Official is referring
15 // to the default settings after installing OpenEMR.
17 // define('UNIT_BLANK',0);
18 // define('UNIT_MG',1);
19 // define('UNIT_MG_1CC',2);
20 // define('UNIT_MG_2CC',3);
21 // define('UNIT_MG_3CC',4);
22 // define('UNIT_MG_4CC',5);
23 // define('UNIT_MG_5CC',6);
24 // define('UNIT_MCG',7);
25 // define('UNIT_GRAMS',8);
27 // define('INTERVAL_BLANK',0);
28 // define('INTERVAL_BID',1);
29 // define('INTERVAL_TID',2);
30 // define('INTERVAL_QID',3);
31 // define('INTERVAL_Q_3H',4);
32 // define('INTERVAL_Q_4H',5);
33 // define('INTERVAL_Q_5H',6);
34 // define('INTERVAL_Q_6H',7);
35 // define('INTERVAL_Q_8H',8);
36 // define('INTERVAL_QD',9);
37 // define('INTERVAL_AC',10); // added May 2008
38 // define('INTERVAL_PC',11); // added May 2008
39 // define('INTERVAL_AM',12); // added May 2008
40 // define('INTERVAL_PM',13); // added May 2008
41 // define('INTERVAL_ANTE',14); // added May 2008
42 // define('INTERVAL_H',15); // added May 2008
43 // define('INTERVAL_HS',16); // added May 2008
44 // define('INTERVAL_PRN',17); // added May 2008
45 // define('INTERVAL_STAT',18); // added May 2008
47 // define('FORM_BLANK',0);
48 // define('FORM_SUSPENSION',1);
49 // define('FORM_TABLET',2);
50 // define('FORM_CAPSULE',3);
51 // define('FORM_SOLUTION',4);
52 // define('FORM_TSP',5);
53 // define('FORM_ML',6);
54 // define('FORM_UNITS',7);
55 // define('FORM_INHILATIONS',8);
56 // define('FORM_GTTS_DROPS',9);
57 // define('FORM_CR',10);
58 // define('FORM_OINT',11);
60 // define('ROUTE_BLANK',0);
61 // define("ROUTE_PER_ORIS", 1);
62 // define("ROUTE_PER_RECTUM", 2);
63 // define("ROUTE_TO_SKIN", 3);
64 // define("ROUTE_TO_AFFECTED_AREA", 4);
65 // define("ROUTE_SUBLINGUAL", 5);
66 // define("ROUTE_OS", 6);
67 // define("ROUTE_OD", 7);
68 // define("ROUTE_OU", 8);
69 // define("ROUTE_SQ", 9);
70 // define("ROUTE_IM", 10);
71 // define("ROUTE_IV", 11);
72 // define("ROUTE_PER_NOSTRIL", 12);
73 // define("ROUTE_B_EAR", 13);
74 // define("ROUTE_L_EAR", 14);
75 // define("ROUTE_R_EAR", 15);
77 // define('SUBSTITUTE_YES',1);
78 // define('SUBSTITUTE_NO',2);
81 // Added 7-2009 by BM to incorporate the units, forms, interval, route lists from list_options
82 // This mechanism may only be temporary; will likely migrate changes more downstream to allow
83 // users the options of using the addlist widgets and validation frunctions from options.inc.php
84 // in the forms and output.
85 function load_drug_attributes($id) {
86 $res = sqlStatement("SELECT * FROM list_options WHERE list_id = '$id' AND activity = 1 ORDER BY seq");
87 while ($row = sqlFetchArray($res)) {
88 if ($row['title'] == '') {
89 $arr[$row['option_id']] = ' ';
92 $arr[$row['option_id']] = xl_list_label($row['title']);
102 class Prescription
extends ORDataObject
{
117 var $substitute_array;
118 var $medication_array;
137 var $rxnorm_drugcode;
154 * Constructor sets all Prescription attributes to their default value
157 function __construct($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; }
175 //$this->unit = UNIT_MG;
176 //$this->route = ROUTE_PER_ORIS;
177 //$this->quantity = 1;
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;
197 for($i=0;$i<21;$i++
) {
198 $this->refills_array
[$i] = sprintf("%02d",$i);
201 if ($id != "") { $this->populate(); }
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() {
214 function toString($html = false) {
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 ."Code: " . $this->rxnorm_drugcode
. "\n"
232 ."Form: " . $this->form_array
[$this->form
]. "\n"
233 ."Dosage: " . $this->dosage
. "\n"
234 ."Qty: " . $this->quantity
. "\n"
235 ."Size: " . $this->size
. "\n"
236 ."Unit: " . $this->unit_array
[$this->unit
] . "\n"
237 ."Route: " . $this->route_array
[$this->route
] . "\n"
238 ."Interval: " .$this->interval_array
[$this->interval
]. "\n"
239 ."Substitute: " . $this->substitute_array
[$this->substitute
]. "\n"
240 ."Refills: " . $this->refills
. "\n"
241 ."Per Refill: " . $this->per_refill
. "\n"
242 ."Drug ID: " . $this->drug_id
. "\n"
243 ."Active: " . $this->active
;
245 if ($html) { return nl2br($string); }
246 else { return $string; }
248 function get_encounter(){
249 return $_SESSION['encounter'];
252 function get_unit_display( $display_form="" ) {
253 return( $this->unit_array
[$this->unit
] );
256 function get_unit() {
259 function set_unit($unit) {
260 if (is_numeric($unit)) { $this->unit
= $unit; }
263 function set_id($id) {
264 if (!empty($id) && is_numeric($id)) { $this->id
= $id; }
270 function get_dosage_display( $display_form="" ) {
271 if( empty($this->form
) && empty($this->interval
) ) {
272 return( $this->dosage
);
275 return ($this->dosage
. " " . xl('in') . " " . $this->form_array
[$this->form
] . " " . $this->interval_array
[$this->interval
]);
279 function set_dosage($dosage) {
280 $this->dosage
= $dosage;
282 function get_dosage() {
283 return $this->dosage
;
286 function set_form($form) {
287 if (is_numeric($form)) { $this->form
= $form; }
289 function get_form() {
293 function set_refills($refills) {
294 if (is_numeric($refills)) { $this->refills
= $refills; }
296 function get_refills() {
297 return $this->refills
;
300 function set_size($size) {
301 $this->size
= preg_replace("/[^0-9\/\.\-]/", "",$size);
303 function get_size() {
307 function set_quantity($qty) {
308 $this->quantity
= $qty;
310 function get_quantity() {
311 return $this->quantity
;
314 function set_route($route) {
315 if (is_numeric($route)) { $this->route
= $route; }
317 function get_route() {
321 function set_interval($interval) {
322 if (is_numeric($interval)) { $this->interval
= $interval; }
324 function get_interval() {
325 return $this->interval
;
328 function set_substitute($sub) {
329 if (is_numeric($sub)) { $this->substitute
= $sub; }
331 function get_substitute() {
332 return $this->substitute
;
334 function set_erx_source($erx_source) {
335 $this->erx_source
= $erx_source;
337 function set_medication($med) {
340 $this->medication
= $med;
342 // Avoid making a mess if we are not using the "medication" issue type.
343 if (isset($ISSUE_TYPES) && !$ISSUE_TYPES['medication']) return;
345 //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
346 // they have already been run through populate() hence stripped of escapes, so now need to be escaped for database (add_escape_custom() function).
348 //check if this drug is on the medication list
349 $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');
351 if ($med && !isset($dataRow['id'])){
352 $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');
354 if (!isset($dataRow['id'])){
355 //add the record to the medication list
356 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 $dataRow = sqlQuery('update lists set activity = 1'
360 . " ,user = '" . $
$_SESSION['authUser']
361 . "', groupname = '" . $
$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
364 elseif (!$med && isset($dataRow['id'])) {
365 //remove the drug from the medication list if it exists
366 $dataRow = sqlQuery('update lists set activity = 0'
367 . " ,user = '" . $
$_SESSION['authUser']
368 . "', groupname = '" . $
$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
372 function get_medication() {
373 return $this->medication
;
376 function set_per_refill($pr) {
377 if (is_numeric($pr)) { $this->per_refill
= $pr; }
379 function get_per_refill() {
380 return $this->per_refill
;
383 function set_patient_id($id) {
384 if (is_numeric($id)) { $this->patient
= new Patient($id); }
386 function get_patient_id() {
387 return $this->patient
->id
;
390 function set_provider_id($id) {
391 if (is_numeric($id)) { $this->provider
= new Provider($id); }
393 function get_provider_id() {
394 return $this->provider
->id
;
397 function set_provider($pobj) {
398 if (get_class($pobj) == "provider") { $this->provider
= $pobj; }
401 function set_pharmacy_id($id) {
402 if (is_numeric($id)) { $this->pharmacy
= new Pharmacy($id); }
404 function get_pharmacy_id() {
405 return $this->pharmacy
->id
;
408 function set_pharmacist_id($id) {
409 if (is_numeric($id)) { $this->pharmacist
= new Person($id); }
411 function get_pharmacist() {
412 return $this->pharmacist
->id
;
415 function get_start_date_y() {
416 $ymd = explode("-",$this->start_date
);
419 function set_start_date_y($year) {
420 if (is_numeric($year)) {
421 $ymd = explode("-",$this->start_date
);
423 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
426 function get_start_date_m() {
427 $ymd = explode("-",$this->start_date
);
430 function set_start_date_m($month) {
431 if (is_numeric($month)) {
432 $ymd = explode("-",$this->start_date
);
434 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
437 function get_start_date_d() {
438 $ymd = explode("-",$this->start_date
);
441 function set_start_date_d($day) {
442 if (is_numeric($day)) {
443 $ymd = explode("-",$this->start_date
);
445 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
448 function get_start_date() {
449 return $this->start_date
;
451 function set_start_date($date) {
452 return $this->start_date
= $date;
455 // TajEmo work by CB 2012/05/30 01:56:32 PM added encounter for auto ticking of checkboxes
456 function set_encounter($enc) {
457 return $this->encounter
= $enc;
460 function get_date_added() {
461 return $this->date_added
;
463 function set_date_added($date) {
464 return $this->date_added
= $date;
467 function get_date_modified() {
468 return $this->date_modified
;
470 function set_date_modified($date) {
471 return $this->date_modified
= $date;
474 function get_filled_date() {
475 return $this->filled_date
;
477 function set_filled_date($date) {
478 return $this->filled_date
= $date;
481 function set_note($note) {
484 function get_note() {
488 function set_drug($drug) {
491 function get_drug() {
495 function set_rxnorm_drugcode($rxnorm_drugcode) {
496 $this->rxnorm_drugcode
= $rxnorm_drugcode;
498 function get_rxnorm_drugcode() {
499 return $this->rxnorm_drugcode
;
502 function get_filled_by_id() {
503 return $this->pharmacist
->id
;
505 function set_filled_by_id($id) {
506 if (is_numeric($id)) { return $this->pharmacist
->id
= $id; }
509 function set_drug_id($drug_id) {
510 $this->drug_id
= $drug_id;
512 function get_drug_id() {
513 return $this->drug_id
;
516 function set_active($active) {
517 $this->active
= $active;
519 function get_active() {
520 return $this->active
;
523 function get_prescription_display() {
524 $pconfig = $GLOBALS['oer_config']['prescriptions'];
526 switch ($pconfig['format']) {
528 return $this->get_prescription_florida_display();
534 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider
->id
) . "'";
536 $results = $db->Execute($sql);
537 if (!$results->EOF
) {
538 $string = $results->fields
['name'] . "\n"
539 . $results->fields
['street'] . "\n"
540 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
541 . $results->fields
['phone'] . "\n\n";
545 ."Prescription For:" . "\t" .$this->patient
->get_name_display() . "\n"
546 ."DOB:"."\t".$this->patient
->get_dob()."\n"
547 ."Start Date: " . "\t\t" . $this->start_date
. "\n"
548 ."Provider: " . "\t\t" . $this->provider
->get_name_display(). "\n"
549 ."Provider DEA No.: " . "\t\t" . $this->provider
->federal_drug_id
. "\n"
550 ."Drug: " . "\t\t\t" . $this->drug
. "\n"
551 ."Dosage: " . "\t\t" . $this->dosage
. " in ". $this->form_array
[$this->form
]. " form " . $this->interval_array
[$this->interval
]. "\n"
552 ."Qty: " . "\t\t\t" . $this->quantity
. "\n"
553 ."Medication Unit: " . "\t" . $this->size
. " ". $this->unit_array
[$this->unit
] . "\n"
554 ."Substitute: " . "\t\t" . $this->substitute_array
[$this->substitute
]. "\n";
555 if ($this->refills
> 0) {
556 $string .= "Refills: " . "\t\t" . $this->refills
. ", of quantity: " . $this->per_refill
."\n";
558 $string .= "\n"."Notes: \n" . $this->note
. "\n";
562 function get_prescription_florida_display() {
565 $ntt = new NumberToText($this->quantity
);
566 $ntt2 = new NumberToText($this->per_refill
);
567 $ntt3 = new NumberToText($this->refills
);
571 $gnd = $this->provider
->get_name_display();
573 while(strlen($gnd)<31) { $gnd .= " "; }
575 $string .= $gnd . $this->provider
->federal_drug_id
. "\n";
577 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . add_escape_custom($this->provider
->id
) . "'";
578 $results = $db->Execute($sql);
580 if (!$results->EOF
) {
581 $rfn = $results->fields
['name'];
583 while(strlen($rfn)<31) { $rfn .= " "; }
585 $string .= $rfn . $this->provider
->get_provider_number_default() . "\n"
586 . $results->fields
['street'] . "\n"
587 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
588 . $results->fields
['phone'] . "\n";
592 $string .= strtoupper($this->patient
->lname
) . ", " . ucfirst($this->patient
->fname
) . " " . $this->patient
->mname
. "\n";
593 $string .= "DOB " . $this->patient
->date_of_birth
. "\n";
595 $string .= date("F j, Y", strtotime($this->start_date
)) . "\n";
597 $string .= strtoupper($this->drug
) . " " . $this->size
. " ". $this->unit_array
[$this->unit
] . "\n";
598 if (strlen($this->note
) > 0) {
599 $string .= "Notes: \n" . $this->note
. "\n";
601 if (!empty($this->dosage
)) {
602 $string .= $this->dosage
;
603 if (!empty($this->form
)){
604 $string .= " " . $this->form_array
[$this->form
];
606 if (!empty($this->interval
)) {
607 $string .= " " . $this->interval_array
[$this->interval
];
609 if (!empty($this->route
)) {
610 $string .= " " . $this->route_array
[$this->route
] . "\n";
613 if (!empty($this->quantity
)) {
614 $string .= "Disp: " . $this->quantity
. " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
617 $string .= "Refills: " . $this->refills
. " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill
. " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
618 $string .= $this->substitute_array
[$this->substitute
]. "\n";
624 static function prescriptions_factory($patient_id,
625 $order_by = "active DESC, date_modified DESC, date_added DESC")
627 $prescriptions = array();
628 require_once (dirname(__FILE__
) . "/../translation.inc.php");
629 $p = new Prescription();
630 $sql = "SELECT id FROM " . $p->_table
. " WHERE patient_id = " .
631 add_escape_custom($patient_id) .
632 " ORDER BY " . add_escape_custom($order_by);
633 $results = sqlQ($sql);
634 while ($row = sqlFetchArray($results) ) {
635 $prescriptions[] = new Prescription($row['id']);
637 return $prescriptions;
640 function get_dispensation_count() {
641 if (empty($this->id
)) return 0;
642 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
643 "WHERE prescription_id = '" . $this->id
. "' AND quantity > 0");
644 return $refills_row['count'];
647 }// end of Prescription