added new values to interval_array and cleaned code
[openemr.git] / library / classes / Prescription.class.php
blob1ae4cb0d0885ded0206856c122e9aba7b32bfefc
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");
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);
19 define('INTERVAL_BID',1);
20 define('INTERVAL_TID',2);
21 define('INTERVAL_QID',3);
22 define('INTERVAL_Q_3H',4);
23 define('INTERVAL_Q_4H',5);
24 define('INTERVAL_Q_5H',6);
25 define('INTERVAL_Q_6H',7);
26 define('INTERVAL_Q_8H',8);
27 define('INTERVAL_QD',9);
28 define('SUBSTITUTE_YES',1);
29 define('SUBSTITUTE_NO',2);
31 define('FORM_SUSPENSION',1);
32 define('FORM_TABLET',2);
33 define('FORM_CAPSULE',3);
34 define('FORM_SOLUTION',4);
35 define('FORM_TSP',5);
36 define('FORM_ML',6);
37 define('FORM_UNITS',7);
38 define('FORM_INHILATIONS',8);
39 define('FORM_GTTS_DROPS',9);
40 define('FORM_CR',10);
41 define('FORM_OINT',11);
43 define("ROUTE_PER_ORIS", 1);
44 define("ROUTE_PER_RECTUM", 2);
45 define("ROUTE_TO_SKIN", 3);
46 define("ROUTE_TO_AFFECTED_AREA", 4);
47 define("ROUTE_SUBLINGUAL", 5);
48 define("ROUTE_OS", 6);
49 define("ROUTE_OD", 7);
50 define("ROUTE_OU", 8);
51 define("ROUTE_SQ", 9);
52 define("ROUTE_IM", 10);
53 define("ROUTE_IV", 11);
54 define("ROUTE_PER_NOSTRIL", 12);
55 define("ROUTE_B_EAR", 13);
56 define("ROUTE_L_EAR", 14);
57 define("ROUTE_R_EAR", 15);
59 /**
60 * class Prescription
63 class Prescription extends ORDataObject {
65 /**
67 * @access public
70 /**
72 * static
74 var $form_array;
75 var $unit_array;
76 var $route_array;
77 var $interval_array;
78 var $substitute_array;
79 var $medication_array;
80 var $refills_array;
82 /**
84 * @access private
87 var $id;
88 var $patient;
89 var $pharmacist;
90 var $date_added;
91 var $date_modified;
92 var $pharmacy;
93 var $start_date;
94 var $filled_date;
95 var $provider;
96 var $note;
97 var $drug;
98 var $form;
99 var $dosage;
100 var $quantity;
101 var $size;
102 var $unit;
103 var $route;
104 var $interval;
105 var $substitute;
106 var $refills;
107 var $per_refill;
108 var $medication;
110 var $drug_id;
111 var $active;
114 * Constructor sets all Prescription attributes to their default value
117 function Prescription($id= "", $_prefix = "") {
119 $this->route_array = array(" ", xl("per oris"), xl("per rectum"),
120 xl("apply to skin"), xl("apply to affected area"), xl("sublingual"),
121 xl("OS"), xl("OD"), xl("OU"), xl("SQ"), xl("IM"), xl("IV"),
122 xl("per nostril"),xl("both ears"), xl("left ear"), xl("right ear"));
124 $this->form_array = array(" ", FORM_TABLET => xl("tablet"),
125 FORM_CAPSULE => xl("capsule"), FORM_TSP => xl("tsp"),
126 FORM_ML => xl("ml"), FORM_UNITS => xl("units"),
127 FORM_INHILATIONS => xl("inhilations"),
128 FORM_GTTS_DROPS => xl("gtts(drops)"), FORM_CR => xl("cream"),
129 FORM_OINT => xl("ointment"));
131 $this->interval_array = array(" ",
132 xl("b.i.d."),
133 xl("t.i.d."),
134 xl("q.i.d."),
135 xl("q.3h"),
136 xl("q.4h"),
137 xl("q.5h"),
138 xl("q.6h"),
139 xl("q.8h"),
140 xl("q.d."),
141 xl("a.c."), // added May 2008
142 xl("p.c."), // added May 2008
143 xl("a.m."), // added May 2008
144 xl("p.m."), // added May 2008
145 xl("ante"), // added May 2008
146 xl("h"), // added May 2008
147 xl("h.s."), // added May 2008
148 xl("p.r.n."), // added May 2008
149 xl("stat") // added May 2008
152 $this->substitute_array = array("",xl("substitution allowed"),
153 xl ("do not substitute"));
155 $this->medication_array = array(0 => xl('No'), 1 => xl('Yes'));
157 $this->unit_array = array(" ",xl("mg"), xl("mg/1cc"), "", "", "",
158 xl("mg/5cc"), xl ("mcg"));
160 if (is_numeric($id)) { $this->id = $id; }
161 else { $id = "";}
163 //$this->unit = UNIT_MG;
164 //$this->route = ROUTE_PER_ORIS;
165 //$this->quantity = 1;
166 //$this->size = 1;
167 $this->refills = 0;
168 //$this->form = FORM_TABLET;
169 $this->substitute = false;
170 $this->_prefix = $_prefix;
171 $this->_table = "prescriptions";
172 $this->pharmacy = new Pharmacy();
173 $this->pharmacist = new Person();
174 $this->provider = new Provider($_SESSION['authId']);
175 $this->patient = new Patient();
176 $this->start_date = date("Y-m-d");
177 $this->date_added = date("Y-m-d");
178 $this->date_modified = date("Y-m-d");
179 $this->per_refill = 0;
180 $this->note = "";
182 $this->drug_id = 0;
183 $this->active = 1;
185 for($i=0;$i<21;$i++) {
186 $this->refills_array[$i] = sprintf("%02d",$i);
189 if ($id != "") { $this->populate(); }
192 function persist() {
193 $this->date_modified = date("Y-m-d");
194 if ($this->id == "") { $this->date_added = date("Y-m-d"); }
195 if (parent::persist()) { }
198 function populate() {
199 parent::populate();
202 function toString($html = false) {
203 $string .= "\n"
204 ."ID: " . $this->id . "\n"
205 ."Patient:" . $this->patient . "\n"
206 ."Patient ID:" . $this->patient->id . "\n"
207 ."Pharmacist: " . $this->pharmacist . "\n"
208 ."Pharmacist ID: " . $this->pharmacist->id . "\n"
209 ."Date Added: " . $this->date_added. "\n"
210 ."Date Modified: " . $this->date_modified. "\n"
211 ."Pharmacy: " . $this->pharmacy. "\n"
212 ."Pharmacy ID:" . $this->pharmacy->id . "\n"
213 ."Start Date: " . $this->start_date. "\n"
214 ."Filled Date: " . $this->filled_date. "\n"
215 ."Provider: " . $this->provider. "\n"
216 ."Provider ID: " . $this->provider->id. "\n"
217 ."Note: " . $this->note. "\n"
218 ."Drug: " . $this->drug. "\n"
219 ."Form: " . $this->form_array[$this->form]. "\n"
220 ."Dosage: " . $this->dosage. "\n"
221 ."Qty: " . $this->quantity. "\n"
222 ."Size: " . $this->size. "\n"
223 ."Unit: " . $this->unit_array[$this->unit] . "\n"
224 ."Route: " . $this->route_array[$this->route] . "\n"
225 ."Interval: " .$this->interval_array[$this->interval]. "\n"
226 ."Substitute: " . $this->substitute_array[$this->substitute]. "\n"
227 ."Refills: " . $this->refills. "\n"
228 ."Per Refill: " . $this->per_refill . "\n"
229 ."Drug ID: " . $this->drug_id . "\n"
230 ."Active: " . $this->active;
232 if ($html) { return nl2br($string); }
233 else { return $string; }
236 // larry :: added unit size too
237 function get_unit_display( $display_form="" ) {
238 if( $display_form == "dutch" )
239 return( $this->size . " " . $this->unit_array[$this->unit] );
240 else
241 return( $this->unit_array[$this->unit] );
244 function get_unit() {
245 return $this->unit;
247 function set_unit($unit) {
248 if (is_numeric($unit)) { $this->unit = $unit; }
251 function set_id($id) {
252 if (!empty($id) && is_numeric($id)) { $this->id = $id; }
254 function get_id() {
255 return $this->id;
258 function get_dosage_display( $display_form="" ) {
259 if( empty($this->form) && empty($this->interval) ) {
260 return( $this->dosage );
262 elseif( $display_form == "dutch" ) {
263 return( $this->form_array[$this->form] . " " . $this->interval_array[$this->interval] . " " . $this->dosage );
265 else {
266 return ($this->dosage . " in " . $this->form_array[$this->form] . " " . $this->interval_array[$this->interval]);
270 function set_dosage($dosage) {
271 $this->dosage = $dosage;
273 function get_dosage() {
274 return $this->dosage;
277 function set_form($form) {
278 if (is_numeric($form)) { $this->form = $form; }
280 function get_form() {
281 return $this->form;
284 function set_refills($refills) {
285 if (is_numeric($refills)) { $this->refills = $refills; }
287 function get_refills() {
288 return $this->refills;
291 function set_size($size) {
292 if (is_numeric($size)) { $this->size = $size; }
294 function get_size() {
295 return $this->size;
298 function set_quantity($qty) {
299 // if (is_numeric($qty)) {
300 $this->quantity = $qty;
301 // }
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() {
311 return $this->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;
328 function set_medication($med) {
329 $this->medication = $med;
331 // Avoid making a mess if we are not using the "medication" issue type.
332 if (!$ISSUE_TYPES['medication']) return;
334 //check if this drug is on the medication list
335 $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('" . $this->drug . "')) and pid = " . $this->patient->id . ' limit 1');
337 if ($med && !isset($dataRow['id'])){
338 $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('" . $this->drug . "')) and pid = " . $this->patient->id . ' limit 1');
340 if (!isset($dataRow['id'])){
341 //add the record to the medication list
342 sqlInsert("insert into lists(date,begdate,type,activity,pid,user,groupname,title) values (now(),cast(now() as date),'medication',1," . $this->patient->id . ",'" . $$_SESSION['authUser']. "','" . $$_SESSION['authProvider'] . "','" . $this->drug . "')");
344 else {
345 $dataRow = sqlQuery('update lists set activity = 1'
346 . " ,user = '" . $$_SESSION['authUser']
347 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
350 elseif (!$med && isset($dataRow['id'])) {
351 //remove the drug from the medication list if it exists
352 $dataRow = sqlQuery('update lists set activity = 0'
353 . " ,user = '" . $$_SESSION['authUser']
354 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
358 function get_medication() {
359 return $this->medication;
362 function set_per_refill($pr) {
363 if (is_numeric($pr)) { $this->per_refill = $pr; }
365 function get_per_refill() {
366 return $this->per_refill;
369 function set_patient_id($id) {
370 if (is_numeric($id)) { $this->patient = new Patient($id); }
372 function get_patient_id() {
373 return $this->patient->id;
376 function set_provider_id($id) {
377 if (is_numeric($id)) { $this->provider = new Provider($id); }
379 function get_provider_id() {
380 return $this->provider->id;
383 function set_provider($pobj) {
384 if (get_class($pobj) == "provider") { $this->provider = $pobj; }
387 function set_pharmacy_id($id) {
388 if (is_numeric($id)) { $this->pharmacy = new Pharmacy($id); }
390 function get_pharmacy_id() {
391 return $this->pharmacy->id;
394 function set_pharmacist_id($id) {
395 if (is_numeric($id)) { $this->pharmacist = new Person($id); }
397 function get_pharmacist() {
398 return $this->pharmacist->id;
401 function get_start_date_y() {
402 $ymd = split("-",$this->start_date);
403 return $ymd[0];
405 function set_start_date_y($year) {
406 if (is_numeric($year)) {
407 $ymd = split("-",$this->start_date);
408 $ymd[0] = $year;
409 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
412 function get_start_date_m() {
413 $ymd = split("-",$this->start_date);
414 return $ymd[1];
416 function set_start_date_m($month) {
417 if (is_numeric($month)) {
418 $ymd = split("-",$this->start_date);
419 $ymd[1] = $month;
420 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
423 function get_start_date_d() {
424 $ymd = split("-",$this->start_date);
425 return $ymd[2];
427 function set_start_date_d($day) {
428 if (is_numeric($day)) {
429 $ymd = split("-",$this->start_date);
430 $ymd[2] = $day;
431 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
434 function get_start_date() {
435 return $this->start_date;
437 function set_start_date($date) {
438 return $this->start_date = $date;
441 function get_date_added() {
442 return $this->date_added;
444 function set_date_added($date) {
445 return $this->date_added = $date;
448 function get_date_modified() {
449 return $this->date_modified;
451 function set_date_modified($date) {
452 return $this->date_modified = $date;
455 function get_filled_date() {
456 return $this->filled_date;
458 function set_filled_date($date) {
459 return $this->filled_date = $date;
462 function set_note($note) {
463 $this->note = $note;
465 function get_note() {
466 return $this->note;
469 function set_drug($drug) {
470 $this->drug = $drug;
472 function get_drug() {
473 return $this->drug;
476 function get_filled_by_id() {
477 return $this->pharmacist->id;
479 function set_filled_by_id($id) {
480 if (is_numeric($id)) { return $this->pharmacist->id = $id; }
483 function set_drug_id($drug_id) {
484 $this->drug_id = $drug_id;
486 function get_drug_id() {
487 return $this->drug_id;
490 function set_active($active) {
491 $this->active = $active;
493 function get_active() {
494 return $this->active;
497 function get_prescription_display() {
498 $pconfig = $GLOBALS['oer_config']['prescriptions'];
500 switch ($pconfig['format']) {
501 case "FL":
502 return $this->get_prescription_florida_display();
503 break;
504 default:
505 break;
508 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
509 $db = get_db();
510 $results = $db->Execute($sql);
511 if (!$results->EOF) {
512 $string = $results->fields['name'] . "\n"
513 . $results->fields['street'] . "\n"
514 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
515 . $results->fields['phone'] . "\n\n";
518 $string .= ""
519 ."Prescription For:" . "\t" .$this->patient->get_name_display() . "\n"
520 ."DOB:"."\t".$this->patient->get_dob()."\n"
521 ."Start Date: " . "\t\t" . $this->start_date. "\n"
522 ."Provider: " . "\t\t" . $this->provider->get_name_display(). "\n"
523 ."Provider DEA No.: " . "\t\t" . $this->provider->federal_drug_id. "\n"
524 ."Drug: " . "\t\t\t" . $this->drug. "\n"
525 ."Dosage: " . "\t\t" . $this->dosage . " in ". $this->form_array[$this->form]. " form " . $this->interval_array[$this->interval]. "\n"
526 ."Qty: " . "\t\t\t" . $this->quantity. "\n"
527 ."Medication Unit: " . "\t" . $this->size . " ". $this->unit_array[$this->unit] . "\n"
528 ."Substitute: " . "\t\t" . $this->substitute_array[$this->substitute]. "\n";
529 if ($this->refills > 0) {
530 $string .= "Refills: " . "\t\t" . $this->refills. ", of quantity: " . $this->per_refill ."\n";
532 $string .= "\n"."Notes: \n" . $this->note . "\n";
533 return $string;
536 function get_prescription_florida_display() {
538 $db = get_db();
539 $ntt = new NumberToText($this->quantity);
540 $ntt2 = new NumberToText($this->per_refill);
541 $ntt3 = new NumberToText($this->refills);
543 $string = "";
545 $gnd = $this->provider->get_name_display();
547 while(strlen($gnd)<31) { $gnd .= " "; }
549 $string .= $gnd . $this->provider->federal_drug_id . "\n";
551 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
552 $results = $db->Execute($sql);
554 if (!$results->EOF) {
555 $rfn = $results->fields['name'];
557 while(strlen($rfn)<31) { $rfn .= " "; }
559 $string .= $rfn . $this->provider->get_provider_number_default() . "\n"
560 . $results->fields['street'] . "\n"
561 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
562 . $results->fields['phone'] . "\n";
565 $string .= "\n";
566 $string .= strtoupper($this->patient->lname) . ", " . ucfirst($this->patient->fname) . " " . $this->patient->mname . "\n";
567 $string .= "DOB " . $this->patient->date_of_birth . "\n";
568 $string .= "\n";
569 $string .= date("F j, Y", strtotime($this->start_date)) . "\n";
570 $string .= "\n";
571 $string .= strtoupper($this->drug) . " " . $this->size . " ". $this->unit_array[$this->unit] . "\n";
572 if (strlen($this->note) > 0) {
573 $string .= "Notes: \n" . $this->note . "\n";
575 if (!empty($this->dosage)) {
576 $string .= $this->dosage;
577 if (!empty($this->form)){
578 $string .= " " . $this->form_array[$this->form];
580 if (!empty($this->interval)) {
581 $string .= " " . $this->interval_array[$this->interval];
583 if (!empty($this->route)) {
584 $string .= " " . $this->route_array[$this->route] . "\n";
587 if (!empty($this->quantity)) {
588 $string .= "Disp: " . $this->quantity . " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
590 $string .= "\n";
591 $string .= "Refills: " . $this->refills . " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill . " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
592 $string .= $this->substitute_array[$this->substitute]. "\n";
593 $string .= "\n";
595 return $string;
598 function prescriptions_factory($patient_id,
599 $order_by = "active DESC, date_modified DESC, date_added DESC")
601 $prescriptions = array();
602 require_once (dirname(__FILE__) . "/../translation.inc.php");
603 $p = new Prescription();
604 $sql = "SELECT id FROM " . $p->_table . " WHERE patient_id = " .
605 mysql_real_escape_string($patient_id) .
606 " ORDER BY " . mysql_real_escape_string($order_by);
607 $results = sqlQ($sql);
608 while ($row = mysql_fetch_array($results) ) {
609 $prescriptions[] = new Prescription($row['id']);
611 return $prescriptions;
614 function get_dispensation_count() {
615 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
616 "WHERE prescription_id = '" . $this->id . "' AND quantity > 0");
617 return $refills_row['count'];
620 }// end of Prescription