removed dangling </table>
[openemr.git] / library / classes / Prescription.class.php
blob0beb52c18aeaf4971c0daa46919b8252cabd75c2
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(" ", xl("b.i.d."), xl("t.i.d."),
132 xl("q.i.d."), xl("q.3h"), xl("q.4h"), xl("q.5h"), xl("q.6h"),
133 xl("q.8h"), xl("q.d."));
135 $this->substitute_array = array("",xl("substitution allowed"),
136 xl ("do not substitute"));
138 $this->medication_array = array(0 => xl('No'), 1 => xl('Yes'));
140 $this->unit_array = array(" ",xl("mg"), xl("mg/1cc"), "", "", "",
141 xl("mg/5cc"), xl ("mcg"));
143 if (is_numeric($id)) {
144 $this->id = $id;
146 else {
147 $id = "";
149 //$this->unit = UNIT_MG;
150 //$this->route = ROUTE_PER_ORIS;
151 //$this->quantity = 1;
152 //$this->size = 1;
153 $this->refills = 0;
154 //$this->form = FORM_TABLET;
155 $this->substitute = false;
156 $this->_prefix = $_prefix;
157 $this->_table = "prescriptions";
158 $this->pharmacy = new Pharmacy();
159 $this->pharmacist = new Person();
160 $this->provider = new Provider($_SESSION['authId']);
161 $this->patient = new Patient();
162 $this->start_date = date("Y-m-d");
163 $this->date_added = date("Y-m-d");
164 $this->date_modified = date("Y-m-d");
165 $this->per_refill = 0;
166 $this->note = "";
168 $this->drug_id = 0;
169 $this->active = 1;
171 for($i=0;$i<21;$i++) {
172 $this->refills_array[$i] = sprintf("%02d",$i);
174 if ($id != "") {
175 $this->populate();
179 function persist() {
181 $this->date_modified = date("Y-m-d");
182 if ($this->id == "") {
183 $this->date_added = date("Y-m-d");
185 if (parent::persist()) {
190 function populate() {
191 parent::populate();
194 function toString($html = false) {
195 $string .= "\n"
196 ."ID: " . $this->id . "\n"
197 ."Patient:" . $this->patient . "\n"
198 ."Patient ID:" . $this->patient->id . "\n"
199 ."Pharmacist: " . $this->pharmacist . "\n"
200 ."Pharmacist ID: " . $this->pharmacist->id . "\n"
201 ."Date Added: " . $this->date_added. "\n"
202 ."Date Modified: " . $this->date_modified. "\n"
203 ."Pharmacy: " . $this->pharmacy. "\n"
204 ."Pharmacy ID:" . $this->pharmacy->id . "\n"
205 ."Start Date: " . $this->start_date. "\n"
206 ."Filled Date: " . $this->filled_date. "\n"
207 ."Provider: " . $this->provider. "\n"
208 ."Provider ID: " . $this->provider->id. "\n"
209 ."Note: " . $this->note. "\n"
210 ."Drug: " . $this->drug. "\n"
211 ."Form: " . $this->form_array[$this->form]. "\n"
212 ."Dosage: " . $this->dosage. "\n"
213 ."Qty: " . $this->quantity. "\n"
214 ."Size: " . $this->size. "\n"
215 ."Unit: " . $this->unit_array[$this->unit] . "\n"
216 ."Route: " . $this->route_array[$this->route] . "\n"
217 ."Interval: " .$this->interval_array[$this->interval]. "\n"
218 ."Substitute: " . $this->substitute_array[$this->substitute]. "\n"
219 ."Refills: " . $this->refills. "\n"
220 ."Per Refill: " . $this->per_refill . "\n"
221 ."Drug ID: " . $this->drug_id . "\n"
222 ."Active: " . $this->active;
224 if ($html) {
225 return nl2br($string);
227 else {
228 return $string;
232 function get_unit_display() {
233 return $this->unit_array[$this->unit];
235 function get_unit() {
236 return $this->unit;
238 function set_unit($unit) {
239 if (is_numeric($unit)) {
240 $this->unit = $unit;
243 function set_id($id) {
244 if (!empty($id) && is_numeric($id)) {
245 $this->id = $id;
248 function get_id() {
249 return $this->id;
251 function get_dosage_display() {
252 if (empty($this->form) && empty($this->interval)) {
253 return ($this->dosage);
255 else {
256 return ($this->dosage . " in " . $this->form_array[$this->form] . " " . $this->interval_array[$this->interval]);
259 function set_dosage($dosage) {
260 $this->dosage = $dosage;
262 function get_dosage() {
263 return $this->dosage;
265 function set_form($form) {
266 if (is_numeric($form)) {
267 $this->form = $form;
270 function get_form() {
271 return $this->form;
274 function set_refills($refills) {
275 if (is_numeric($refills)) {
276 $this->refills = $refills;
279 function get_refills() {
280 return $this->refills;
283 function set_size($size) {
284 if (is_numeric($size)) {
285 $this->size = $size;
288 function get_size() {
289 return $this->size;
291 function set_quantity($qty) {
292 // if (is_numeric($qty)) {
293 $this->quantity = $qty;
294 // }
296 function get_quantity() {
297 return $this->quantity;
300 function set_route($route) {
301 if (is_numeric($route)) {
302 $this->route = $route;
305 function get_route() {
306 return $this->route;
309 function set_interval($interval) {
310 if (is_numeric($interval)) {
311 $this->interval = $interval;
314 function get_interval() {
315 return $this->interval;
317 function set_substitute($sub) {
318 if (is_numeric($sub)) {
319 $this->substitute = $sub;
322 function get_substitute() {
323 return $this->substitute;
325 function set_medication($med) {
326 $this->medication = $med;
328 // Avoid making a mess if we are not using the "medication" issue type.
329 if (!$ISSUE_TYPES['medication']) return;
331 //check if this drug is on the medication list
332 $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');
334 if ($med && !isset($dataRow['id'])){
335 $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');
336 if (!isset($dataRow['id'])){
337 //add the record to the medication list
338 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 . "')");
340 else {
341 $dataRow = sqlQuery('update lists set activity = 1'
342 . " ,user = '" . $$_SESSION['authUser']
343 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
346 elseif (!$med && isset($dataRow['id'])) {
347 //remove the drug from the medication list if it exists
348 $dataRow = sqlQuery('update lists set activity = 0'
349 . " ,user = '" . $$_SESSION['authUser']
350 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
353 function get_medication() {
354 return $this->medication;
357 function set_per_refill($pr) {
358 if (is_numeric($pr)) {
359 $this->per_refill = $pr;
362 function get_per_refill() {
363 return $this->per_refill;
365 function set_patient_id($id) {
366 if (is_numeric($id)) {
367 $this->patient = new Patient($id);
370 function get_patient_id() {
371 return $this->patient->id;
373 function set_provider_id($id) {
374 if (is_numeric($id)) {
375 $this->provider = new Provider($id);
378 function get_provider_id() {
379 return $this->provider->id;
381 function set_provider($pobj) {
382 if (get_class($pobj) == "provider") {
383 $this->provider = $pobj;
387 function set_pharmacy_id($id) {
388 if (is_numeric($id)) {
389 $this->pharmacy = new Pharmacy($id);
392 function get_pharmacy_id() {
393 return $this->pharmacy->id;
395 function set_pharmacist_id($id) {
396 if (is_numeric($id)) {
397 $this->pharmacist = new Person($id);
400 function get_pharmacist() {
401 return $this->pharmacist->id;
403 function get_start_date_y() {
404 $ymd = split("-",$this->start_date);
405 return $ymd[0];
407 function set_start_date_y($year) {
408 if (is_numeric($year)) {
409 $ymd = split("-",$this->start_date);
410 $ymd[0] = $year;
411 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
414 function get_start_date_m() {
415 $ymd = split("-",$this->start_date);
416 return $ymd[1];
418 function set_start_date_m($month) {
419 if (is_numeric($month)) {
420 $ymd = split("-",$this->start_date);
421 $ymd[1] = $month;
422 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
425 function get_start_date_d() {
426 $ymd = split("-",$this->start_date);
427 return $ymd[2];
429 function set_start_date_d($day) {
430 if (is_numeric($day)) {
431 $ymd = split("-",$this->start_date);
432 $ymd[2] = $day;
433 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
436 function get_start_date() {
437 return $this->start_date;
439 function set_start_date($date) {
440 return $this->start_date = $date;
442 function get_date_added() {
443 return $this->date_added;
445 function set_date_added($date) {
446 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;
454 function get_filled_date() {
455 return $this->filled_date;
457 function set_filled_date($date) {
458 return $this->filled_date = $date;
460 function set_note($note) {
461 $this->note = $note;
463 function get_note() {
464 return $this->note;
466 function set_drug($drug) {
467 $this->drug = $drug;
469 function get_drug() {
470 return $this->drug;
472 function get_filled_by_id() {
473 return $this->pharmacist->id;
475 function set_filled_by_id($id) {
476 if (is_numeric($id)) {
477 return $this->pharmacist->id = $id;
480 function set_drug_id($drug_id) {
481 $this->drug_id = $drug_id;
483 function get_drug_id() {
484 return $this->drug_id;
486 function set_active($active) {
487 $this->active = $active;
489 function get_active() {
490 return $this->active;
493 function get_prescription_display() {
495 $pconfig = $GLOBALS['oer_config']['prescriptions'];
497 switch ($pconfig['format']) {
498 case "FL":
499 return $this->get_prescription_florida_display();
500 break;
501 default:
502 break;
504 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
505 $db = get_db();
506 $results = $db->Execute($sql);
507 if (!$results->EOF) {
509 $string = $results->fields['name'] . "\n"
510 . $results->fields['street'] . "\n"
511 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
512 . $results->fields['phone'] . "\n\n";
515 $string .= ""
516 ."Prescription For:" . "\t" .$this->patient->get_name_display() . "\n"
517 ."DOB:"."\t".$this->patient->get_dob()."\n"
518 ."Start Date: " . "\t\t" . $this->start_date. "\n"
519 ."Provider: " . "\t\t" . $this->provider->get_name_display(). "\n"
520 ."Provider DEA No.: " . "\t\t" . $this->provider->federal_drug_id. "\n"
521 ."Drug: " . "\t\t\t" . $this->drug. "\n"
522 ."Dosage: " . "\t\t" . $this->dosage . " in ". $this->form_array[$this->form]. " form " . $this->interval_array[$this->interval]. "\n"
523 ."Qty: " . "\t\t\t" . $this->quantity. "\n"
524 ."Medication Unit: " . "\t" . $this->size . " ". $this->unit_array[$this->unit] . "\n"
525 ."Substitute: " . "\t\t" . $this->substitute_array[$this->substitute]. "\n";
526 if ($this->refills > 0) {
527 $string .= "Refills: " . "\t\t" . $this->refills. ", of quantity: " . $this->per_refill ."\n";
529 $string .= "\n"."Notes: \n" . $this->note . "\n";
530 return $string;
533 function get_prescription_florida_display() {
535 $db = get_db();
536 $ntt = new NumberToText($this->quantity);
537 $ntt2 = new NumberToText($this->per_refill);
538 $ntt3 = new NumberToText($this->refills);
540 $string = "";
542 $gnd = $this->provider->get_name_display();
544 while(strlen($gnd)<31) {
545 $gnd .= " ";
548 $string .= $gnd . $this->provider->federal_drug_id . "\n";
550 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
551 $results = $db->Execute($sql);
553 if (!$results->EOF) {
554 $rfn = $results->fields['name'];
556 while(strlen($rfn)<31) {
557 $rfn .= " ";
560 $string .= $rfn . $this->provider->get_provider_number_default() . "\n"
561 . $results->fields['street'] . "\n"
562 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
563 . $results->fields['phone'] . "\n";
566 $string .= "\n";
567 $string .= strtoupper($this->patient->lname) . ", " . ucfirst($this->patient->fname) . " " . $this->patient->mname . "\n";
568 $string .= "DOB " . $this->patient->date_of_birth . "\n";
569 $string .= "\n";
570 $string .= date("F j, Y", strtotime($this->start_date)) . "\n";
571 $string .= "\n";
572 $string .= strtoupper($this->drug) . " " . $this->size . " ". $this->unit_array[$this->unit] . "\n";
573 if (strlen($this->note) > 0) {
574 $string .= "Notes: \n" . $this->note . "\n";
576 if (!empty($this->dosage)) {
577 $string .= $this->dosage;
578 if (!empty($this->form)){
579 $string .= " " . $this->form_array[$this->form];
581 if (!empty($this->interval)) {
582 $string .= " " . $this->interval_array[$this->interval];
584 if (!empty($this->route)) {
585 $string .= " " . $this->route_array[$this->route] . "\n";
588 if (!empty($this->quantity)) {
589 $string .= "Disp: " . $this->quantity . " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
591 $string .= "\n";
592 $string .= "Refills: " . $this->refills . " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill . " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
593 $string .= $this->substitute_array[$this->substitute]. "\n";
594 $string .= "\n";
596 return $string;
599 function prescriptions_factory($patient_id,
600 $order_by = "active DESC, date_modified DESC, date_added DESC")
602 $prescriptions = array();
603 require_once (dirname(__FILE__) . "/../translation.inc.php");
604 $p = new Prescription();
605 $sql = "SELECT id FROM " . $p->_table . " WHERE patient_id = " .
606 mysql_real_escape_string($patient_id) .
607 " ORDER BY " . mysql_real_escape_string($order_by);
608 $results = sqlQ($sql);
609 while ($row = mysql_fetch_array($results) ) {
610 $prescriptions[] = new Prescription($row['id']);
612 return $prescriptions;
615 function get_dispensation_count() {
616 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
617 "WHERE prescription_id = '" . $this->id . "' AND quantity > 0");
618 return $refills_row['count'];
621 } // end of Prescription