remove obsolete frame divider images
[openemr.git] / library / classes / Prescription.class.php
blob36e0927cdaf8e05847d776a6712f5b8af28ccdbd
1 <?php
3 require_once (dirname(__FILE__) ."/../sql.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_GRAMS',7);
18 define('UNIT_MCG',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);
41 define("ROUTE_PER_ORIS", 1);
42 define("ROUTE_PER_RECTUM", 2);
43 define("ROUTE_TO_SKIN", 3);
44 define("ROUTE_TO_AFFECTED_AREA", 4);
45 define("ROUTE_SUBLINGUAL", 5);
46 define("ROUTE_OS", 6);
47 define("ROUTE_OD", 7);
48 define("ROUTE_OU", 8);
49 define("ROUTE_SQ", 9);
50 define("ROUTE_IM", 10);
51 define("ROUTE_IV", 11);
52 define("ROUTE_PER_NOSTRIL", 12);
54 /**
55 * class Prescription
58 class Prescription extends ORDataObject {
60 /**
62 * @access public
66 /**
68 * static
70 var $form_array = array(" ",FORM_TABLET => "tablet", FORM_CAPSULE => "capsule", FORM_TSP => "tsp", FORM_ML => "ml", FORM_UNITS => "units",
71 FORM_INHILATIONS => "inhilations", FORM_GTTS_DROPS => "gtts(drops)");
72 var $unit_array = array(" ","mg","mg/1cc","","","","mg/5cc","mcg");
73 var $route_array = array(" ","Per Oris","Per Rectum","To Skin","To Affected Area","Sublingual", "OS", "OD", "OU", "SQ", "IM", "IV", "Per Nostril");
74 var $interval_array = array(" ","b.i.d.","t.i.d.","q.i.d.","q.3h","q.4h","q.5h","q.6h","q.8h","q.d.");
75 var $substitute_array = array("","substitution allowed","substitution not allowed");
76 var $refills_array;
78 /**
80 * @access private
83 var $id;
84 var $patient;
85 var $pharmacist;
86 var $date_added;
87 var $date_modified;
88 var $pharmacy;
89 var $start_date;
90 var $filled_date;
91 var $provider;
92 var $note;
93 var $drug;
94 var $form;
95 var $dosage;
96 var $quantity;
97 var $size;
98 var $unit;
99 var $route;
100 var $interval;
101 var $substitute;
102 var $refills;
103 var $per_refill;
107 * Constructor sets all Prescription attributes to their default value
110 function Prescription($id= "", $_prefix = "") {
111 if (is_numeric($id)) {
112 $this->id = $id;
114 else {
115 $id = "";
117 //$this->unit = UNIT_MG;
118 //$this->route = ROUTE_PER_ORIS;
119 //$this->quantity = 1;
120 //$this->size = 1;
121 $this->refills = 0;
122 //$this->form = FORM_TABLET;
123 $this->substitute = false;
124 $this->_prefix = $_prefix;
125 $this->_table = "prescriptions";
126 $this->pharmacy = new Pharmacy();
127 $this->pharmacist = new Person();
128 $this->provider = new Provider($_SESSION['authId']);
129 $this->patient = new Patient();
130 $this->start_date = date("Y-m-d");
131 $this->date_added = date("Y-m-d");
132 $this->date_modified = date("Y-m-d");
133 $this->per_refill = 0;
134 $this->note = "";
135 for($i=0;$i<21;$i++) {
136 $this->refills_array[$i] = sprintf("%02d",$i);
138 if ($id != "") {
139 $this->populate();
143 function persist() {
145 $this->date_modified = date("Y-m-d");
146 if ($this->id == "") {
147 $this->date_added = date("Y-m-d");
149 if (parent::persist()) {
154 function populate() {
155 parent::populate();
158 function toString($html = false) {
159 $string .= "\n"
160 ."ID: " . $this->id . "\n"
161 ."Patient:" . $this->patient . "\n"
162 ."Patient ID:" . $this->patient->id . "\n"
163 ."Pharmacist: " . $this->pharmacist . "\n"
164 ."Pharmacist ID: " . $this->pharmacist->id . "\n"
165 ."Date Added: " . $this->date_added. "\n"
166 ."Date Modified: " . $this->date_modified. "\n"
167 ."Pharmacy: " . $this->pharmacy. "\n"
168 ."Pharmacy ID:" . $this->pharmacy->id . "\n"
169 ."Start Date: " . $this->start_date. "\n"
170 ."Filled Date: " . $this->filled_date. "\n"
171 ."Provider: " . $this->provider. "\n"
172 ."Provider ID: " . $this->provider->id. "\n"
173 ."Note: " . $this->note. "\n"
174 ."Drug: " . $this->drug. "\n"
175 ."Form: " . $this->form_array[$this->form]. "\n"
176 ."Dosage: " . $this->dosage. "\n"
177 ."Qty: " . $this->quantity. "\n"
178 ."Size: " . $this->size. "\n"
179 ."Unit: " . $this->unit_array[$this->unit] . "\n"
180 ."Route: " . $this->route_array[$this->route] . "\n"
181 ."Interval: " .$this->interval_array[$this->interval]. "\n"
182 ."Substitute: " . $this->substitute_array[$this->substitute]. "\n"
183 ."Refills: " . $this->refills. "\n"
184 ."Per Refill: " . $this->per_refill;
186 if ($html) {
187 return nl2br($string);
189 else {
190 return $string;
194 function get_unit_display() {
195 return $this->unit_array[$this->unit];
197 function get_unit() {
198 return $this->unit;
200 function set_unit($unit) {
201 if (is_numeric($unit)) {
202 $this->unit = $unit;
205 function set_id($id) {
206 if (!empty($id) && is_numeric($id)) {
207 $this->id = $id;
210 function get_id() {
211 return $this->id;
213 function get_dosage_display() {
214 if (empty($this->form) && empty($this->interval)) {
215 return ($this->dosage);
217 else {
218 return ($this->dosage . " in " . $this->form_array[$this->form] . " " . $this->interval_array[$this->interval]);
221 function set_dosage($dosage) {
222 $this->dosage = $dosage;
224 function get_dosage() {
225 return $this->dosage;
227 function set_form($form) {
228 if (is_numeric($form)) {
229 $this->form = $form;
232 function get_form() {
233 return $this->form;
236 function set_refills($refills) {
237 if (is_numeric($refills)) {
238 $this->refills = $refills;
241 function get_refills() {
242 return $this->refills;
245 function set_size($size) {
246 if (is_numeric($size)) {
247 $this->size = $size;
250 function get_size() {
251 return $this->size;
253 function set_quantity($qty) {
254 if (is_numeric($qty)) {
255 $this->quantity = $qty;
258 function get_quantity() {
259 return $this->quantity;
262 function set_route($route) {
263 if (is_numeric($route)) {
264 $this->route = $route;
267 function get_route() {
268 return $this->route;
271 function set_interval($interval) {
272 if (is_numeric($interval)) {
273 $this->interval = $interval;
276 function get_interval() {
277 return $this->interval;
279 function set_substitute($sub) {
280 if (is_numeric($sub)) {
281 $this->substitute = $sub;
284 function get_substitute() {
285 return $this->substitute;
287 function set_per_refill($pr) {
288 if (is_numeric($pr)) {
289 $this->per_refill = $pr;
292 function get_per_refill() {
293 return $this->per_refill;
295 function set_patient_id($id) {
296 if (is_numeric($id)) {
297 $this->patient = new Patient($id);
300 function get_patient_id() {
301 return $this->patient->id;
303 function set_provider_id($id) {
304 if (is_numeric($id)) {
305 $this->provider = new Provider($id);
308 function get_provider_id() {
309 return $this->provider->id;
311 function set_provider($pobj) {
312 if (get_class($pobj) == "provider") {
313 $this->provider = $pobj;
317 function set_pharmacy_id($id) {
318 if (is_numeric($id)) {
319 $this->pharmacy = new Pharmacy($id);
322 function get_pharmacy_id() {
323 return $this->pharmacy->id;
325 function set_pharmacist_id($id) {
326 if (is_numeric($id)) {
327 $this->pharmacist = new Person($id);
330 function get_pharmacist() {
331 return $this->pharmacist->id;
333 function get_start_date_y() {
334 $ymd = split("-",$this->start_date);
335 return $ymd[0];
337 function set_start_date_y($year) {
338 if (is_numeric($year)) {
339 $ymd = split("-",$this->start_date);
340 $ymd[0] = $year;
341 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
344 function get_start_date_m() {
345 $ymd = split("-",$this->start_date);
346 return $ymd[1];
348 function set_start_date_m($month) {
349 if (is_numeric($month)) {
350 $ymd = split("-",$this->start_date);
351 $ymd[1] = $month;
352 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
355 function get_start_date_d() {
356 $ymd = split("-",$this->start_date);
357 return $ymd[2];
359 function set_start_date_d($day) {
360 if (is_numeric($day)) {
361 $ymd = split("-",$this->start_date);
362 $ymd[2] = $day;
363 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
366 function get_start_date() {
367 return $this->start_date;
369 function set_start_date($date) {
370 return $this->start_date = $date;
372 function get_date_added() {
373 return $this->date_added;
375 function set_date_added($date) {
376 return $this->date_added = $date;
378 function get_date_modified() {
379 return $this->date_modified;
381 function set_date_modified($date) {
382 return $this->date_modified = $date;
384 function get_filled_date() {
385 return $this->filled_date;
387 function set_filled_date($date) {
388 return $this->filled_date = $date;
390 function set_note($note) {
391 $this->note = $note;
393 function get_note() {
394 return $this->note;
396 function set_drug($drug) {
397 $this->drug = $drug;
399 function get_drug() {
400 return $this->drug;
402 function get_filled_by_id() {
403 return $this->pharmacist->id;
405 function set_filled_by_id($id) {
406 if (is_numeric($id)) {
407 return $this->pharmacist->id = $id;
411 function get_prescription_display() {
413 $pconfig = $GLOBALS['oer_config']['prescriptions'];
415 switch ($pconfig['format']) {
416 case "FL":
417 return $this->get_prescription_florida_display();
418 break;
419 default:
420 break;
422 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
423 $db = get_db();
424 $results = $db->Execute($sql);
425 if (!$results->EOF) {
427 $string = $results->fields['name'] . "\n"
428 . $results->fields['street'] . "\n"
429 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
430 . $results->fields['phone'] . "\n\n";
433 $string .= ""
434 ."Prescription For:" . "\t" .$this->patient->get_name_display() . "\n"
435 ."Start Date: " . "\t\t" . $this->start_date. "\n"
436 ."Provider: " . "\t\t" . $this->provider->get_name_display(). "\n"
437 ."Provider FDID: " . "\t\t" . $this->provider->federal_drug_id. "\n"
438 ."Drug: " . "\t\t\t" . $this->drug. "\n"
439 ."Dosage: " . "\t\t" . $this->dosage . " in ". $this->form_array[$this->form]. " form " . $this->interval_array[$this->interval]. "\n"
440 ."Qty: " . "\t\t\t" . $this->quantity. "\n"
441 ."Medication Unit: " . "\t" . $this->size . " ". $this->unit_array[$this->unit] . "\n"
442 ."Substitute: " . "\t\t" . $this->substitute_array[$this->substitute]. "\n";
443 if ($this->refills > 0) {
444 $string .= "Refills: " . "\t\t" . $this->refills. ", of quantity: " . $this->per_refill ."\n";
446 $string .= "\n"."Notes: \n" . $this->note . "\n";
447 return $string;
450 function get_prescription_florida_display() {
452 $db = get_db();
453 $ntt = new NumberToText($this->quantity);
454 $ntt2 = new NumberToText($this->per_refill);
455 $ntt3 = new NumberToText($this->refills);
457 $string = "";
459 $gnd = $this->provider->get_name_display();
461 while(strlen($gnd)<31) {
462 $gnd .= " ";
465 $string .= $gnd . $this->provider->federal_drug_id . "\n";
467 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
468 $results = $db->Execute($sql);
470 if (!$results->EOF) {
471 $rfn = $results->fields['name'];
473 while(strlen($rfn)<31) {
474 $rfn .= " ";
477 $string .= $rfn . $this->provider->get_provider_number_default() . "\n"
478 . $results->fields['street'] . "\n"
479 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
480 . $results->fields['phone'] . "\n";
483 $string .= "\n";
484 $string .= strtoupper($this->patient->lname) . ", " . ucfirst($this->patient->fname) . " " . $this->patient->mname . "\n";
485 $string .= "DOB " . $this->patient->date_of_birth . "\n";
486 $string .= "\n";
487 $string .= date("F j, Y", strtotime($this->start_date)) . "\n";
488 $string .= "\n";
489 $string .= strtoupper($this->drug) . " " . $this->size . " ". $this->unit_array[$this->unit] . "\n";
490 if (strlen($this->note) > 0) {
491 $string .= "Notes: \n" . $this->note . "\n";
493 if (!empty($this->dosage)) {
494 $string .= $this->dosage;
495 if (!empty($this->form)){
496 $string .= " " . $this->form_array[$this->form];
498 if (!empty($this->interval)) {
499 $string .= " " . $this->interval_array[$this->interval];
501 if (!empty($this->route)) {
502 $string .= " " . $this->route_array[$this->route] . "\n";
505 if (!empty($this->quantity)) {
506 $string .= "Disp: " . $this->quantity . " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
508 $string .= "\n";
509 $string .= "Refills: " . $this->refills . " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill . " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
510 $string .= $this->substitute_array[$this->substitute]. "\n";
511 $string .= "\n";
513 return $string;
516 function prescriptions_factory($patient_id,$order_by = "active DESC, date_modified DESC, date_added DESC") {
517 $prescriptions = array();
518 $p = new Prescription();
519 $sql = "SELECT id FROM " . $p->_table . " WHERE patient_id = " .mysql_real_escape_string($patient_id) . " and active = 1 ORDER BY " . mysql_real_escape_string($order_by);
520 $results = sqlQ($sql);
521 //echo "sql: $sql";
522 while ($row = mysql_fetch_array($results) ) {
523 $prescriptions[] = new Prescription($row['id']);
525 return $prescriptions;
528 } // end of Prescription
530 //class test
532 $rx = new Prescription(1);
533 echo "<br /><br />the object is now: " . $rx->toString(true) . "<br />";
534 $rx->form = FORM_TABLET;
535 echo "<br /><br />the object is now: " . $rx->toString(true) . "<br />";
536 $rx->persist();
537 echo "<br /><br />the object is now: " . $rx->toString(true) . "<br />";
539 $rx2 = new Prescription(1);
540 echo "<br /><br />the object is now: " . $rx->toString(true) . "<br />";