permit non-numeric prescription quantity
[openemr.git] / library / classes / Prescription.class.php
blob12f592817f743d43a1e18f66ba39d72c0e2a7b38
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_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);
60 /**
61 * class Prescription
64 class Prescription extends ORDataObject {
66 /**
68 * @access public
72 /**
74 * static
76 var $form_array = array(" ",FORM_TABLET => "tablet", FORM_CAPSULE => "capsule", FORM_TSP => "tsp", FORM_ML => "ml", FORM_UNITS => "units",
77 FORM_INHILATIONS => "inhilations", FORM_GTTS_DROPS => "gtts(drops)"
78 ,FORM_CR => "cream", FORM_OINT => "ointment");
79 var $unit_array = array(" ","mg","mg/1cc","","","","mg/5cc","mcg");
80 var $route_array = array(" ","per oris","per rectum","apply to skin","apply to affected area","sublingual", "OS", "OD", "OU", "SQ", "IM", "IV", "per nostril","both ears","left ear","right ear");
81 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.");
82 var $substitute_array = array("","substitution allowed","substitution not allowed");
83 var $medication_array = array(0 => 'No', 1 => 'Yes');
84 var $refills_array;
86 /**
88 * @access private
91 var $id;
92 var $patient;
93 var $pharmacist;
94 var $date_added;
95 var $date_modified;
96 var $pharmacy;
97 var $start_date;
98 var $filled_date;
99 var $provider;
100 var $note;
101 var $drug;
102 var $form;
103 var $dosage;
104 var $quantity;
105 var $size;
106 var $unit;
107 var $route;
108 var $interval;
109 var $substitute;
110 var $refills;
111 var $per_refill;
112 var $medication;
114 var $drug_id;
117 * Constructor sets all Prescription attributes to their default value
120 function Prescription($id= "", $_prefix = "") {
121 if (is_numeric($id)) {
122 $this->id = $id;
124 else {
125 $id = "";
127 //$this->unit = UNIT_MG;
128 //$this->route = ROUTE_PER_ORIS;
129 //$this->quantity = 1;
130 //$this->size = 1;
131 $this->refills = 0;
132 //$this->form = FORM_TABLET;
133 $this->substitute = false;
134 $this->_prefix = $_prefix;
135 $this->_table = "prescriptions";
136 $this->pharmacy = new Pharmacy();
137 $this->pharmacist = new Person();
138 $this->provider = new Provider($_SESSION['authId']);
139 $this->patient = new Patient();
140 $this->start_date = date("Y-m-d");
141 $this->date_added = date("Y-m-d");
142 $this->date_modified = date("Y-m-d");
143 $this->per_refill = 0;
144 $this->note = "";
146 $this->drug_id = 0;
148 for($i=0;$i<21;$i++) {
149 $this->refills_array[$i] = sprintf("%02d",$i);
151 if ($id != "") {
152 $this->populate();
156 function persist() {
158 $this->date_modified = date("Y-m-d");
159 if ($this->id == "") {
160 $this->date_added = date("Y-m-d");
162 if (parent::persist()) {
167 function populate() {
168 parent::populate();
171 function toString($html = false) {
172 $string .= "\n"
173 ."ID: " . $this->id . "\n"
174 ."Patient:" . $this->patient . "\n"
175 ."Patient ID:" . $this->patient->id . "\n"
176 ."Pharmacist: " . $this->pharmacist . "\n"
177 ."Pharmacist ID: " . $this->pharmacist->id . "\n"
178 ."Date Added: " . $this->date_added. "\n"
179 ."Date Modified: " . $this->date_modified. "\n"
180 ."Pharmacy: " . $this->pharmacy. "\n"
181 ."Pharmacy ID:" . $this->pharmacy->id . "\n"
182 ."Start Date: " . $this->start_date. "\n"
183 ."Filled Date: " . $this->filled_date. "\n"
184 ."Provider: " . $this->provider. "\n"
185 ."Provider ID: " . $this->provider->id. "\n"
186 ."Note: " . $this->note. "\n"
187 ."Drug: " . $this->drug. "\n"
188 ."Form: " . $this->form_array[$this->form]. "\n"
189 ."Dosage: " . $this->dosage. "\n"
190 ."Qty: " . $this->quantity. "\n"
191 ."Size: " . $this->size. "\n"
192 ."Unit: " . $this->unit_array[$this->unit] . "\n"
193 ."Route: " . $this->route_array[$this->route] . "\n"
194 ."Interval: " .$this->interval_array[$this->interval]. "\n"
195 ."Substitute: " . $this->substitute_array[$this->substitute]. "\n"
196 ."Refills: " . $this->refills. "\n"
197 ."Per Refill: " . $this->per_refill . "\n"
198 ."Drug ID: " . $this->drug_id;
200 if ($html) {
201 return nl2br($string);
203 else {
204 return $string;
208 function get_unit_display() {
209 return $this->unit_array[$this->unit];
211 function get_unit() {
212 return $this->unit;
214 function set_unit($unit) {
215 if (is_numeric($unit)) {
216 $this->unit = $unit;
219 function set_id($id) {
220 if (!empty($id) && is_numeric($id)) {
221 $this->id = $id;
224 function get_id() {
225 return $this->id;
227 function get_dosage_display() {
228 if (empty($this->form) && empty($this->interval)) {
229 return ($this->dosage);
231 else {
232 return ($this->dosage . " in " . $this->form_array[$this->form] . " " . $this->interval_array[$this->interval]);
235 function set_dosage($dosage) {
236 $this->dosage = $dosage;
238 function get_dosage() {
239 return $this->dosage;
241 function set_form($form) {
242 if (is_numeric($form)) {
243 $this->form = $form;
246 function get_form() {
247 return $this->form;
250 function set_refills($refills) {
251 if (is_numeric($refills)) {
252 $this->refills = $refills;
255 function get_refills() {
256 return $this->refills;
259 function set_size($size) {
260 if (is_numeric($size)) {
261 $this->size = $size;
264 function get_size() {
265 return $this->size;
267 function set_quantity($qty) {
268 // if (is_numeric($qty)) {
269 $this->quantity = $qty;
270 // }
272 function get_quantity() {
273 return $this->quantity;
276 function set_route($route) {
277 if (is_numeric($route)) {
278 $this->route = $route;
281 function get_route() {
282 return $this->route;
285 function set_interval($interval) {
286 if (is_numeric($interval)) {
287 $this->interval = $interval;
290 function get_interval() {
291 return $this->interval;
293 function set_substitute($sub) {
294 if (is_numeric($sub)) {
295 $this->substitute = $sub;
298 function get_substitute() {
299 return $this->substitute;
301 function set_medication($med) {
302 $this->medication = $med;
303 //check if this drug is on the medication list
304 $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');
306 if ($med && !isset($dataRow['id'])){
307 $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');
308 if (!isset($dataRow['id'])){
309 //add the record to the medication list
310 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 . "')");
312 else {
313 $dataRow = sqlQuery('update lists set activity = 1'
314 . " ,user = '" . $$_SESSION['authUser']
315 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
318 elseif (!$med && isset($dataRow['id'])) {
319 //remove the drug from the medication list if it exists
320 $dataRow = sqlQuery('update lists set activity = 0'
321 . " ,user = '" . $$_SESSION['authUser']
322 . "', groupname = '" . $$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
325 function get_medication() {
326 return $this->medication;
329 function set_per_refill($pr) {
330 if (is_numeric($pr)) {
331 $this->per_refill = $pr;
334 function get_per_refill() {
335 return $this->per_refill;
337 function set_patient_id($id) {
338 if (is_numeric($id)) {
339 $this->patient = new Patient($id);
342 function get_patient_id() {
343 return $this->patient->id;
345 function set_provider_id($id) {
346 if (is_numeric($id)) {
347 $this->provider = new Provider($id);
350 function get_provider_id() {
351 return $this->provider->id;
353 function set_provider($pobj) {
354 if (get_class($pobj) == "provider") {
355 $this->provider = $pobj;
359 function set_pharmacy_id($id) {
360 if (is_numeric($id)) {
361 $this->pharmacy = new Pharmacy($id);
364 function get_pharmacy_id() {
365 return $this->pharmacy->id;
367 function set_pharmacist_id($id) {
368 if (is_numeric($id)) {
369 $this->pharmacist = new Person($id);
372 function get_pharmacist() {
373 return $this->pharmacist->id;
375 function get_start_date_y() {
376 $ymd = split("-",$this->start_date);
377 return $ymd[0];
379 function set_start_date_y($year) {
380 if (is_numeric($year)) {
381 $ymd = split("-",$this->start_date);
382 $ymd[0] = $year;
383 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
386 function get_start_date_m() {
387 $ymd = split("-",$this->start_date);
388 return $ymd[1];
390 function set_start_date_m($month) {
391 if (is_numeric($month)) {
392 $ymd = split("-",$this->start_date);
393 $ymd[1] = $month;
394 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
397 function get_start_date_d() {
398 $ymd = split("-",$this->start_date);
399 return $ymd[2];
401 function set_start_date_d($day) {
402 if (is_numeric($day)) {
403 $ymd = split("-",$this->start_date);
404 $ymd[2] = $day;
405 $this->start_date = $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
408 function get_start_date() {
409 return $this->start_date;
411 function set_start_date($date) {
412 return $this->start_date = $date;
414 function get_date_added() {
415 return $this->date_added;
417 function set_date_added($date) {
418 return $this->date_added = $date;
420 function get_date_modified() {
421 return $this->date_modified;
423 function set_date_modified($date) {
424 return $this->date_modified = $date;
426 function get_filled_date() {
427 return $this->filled_date;
429 function set_filled_date($date) {
430 return $this->filled_date = $date;
432 function set_note($note) {
433 $this->note = $note;
435 function get_note() {
436 return $this->note;
438 function set_drug($drug) {
439 $this->drug = $drug;
441 function get_drug() {
442 return $this->drug;
444 function get_filled_by_id() {
445 return $this->pharmacist->id;
447 function set_filled_by_id($id) {
448 if (is_numeric($id)) {
449 return $this->pharmacist->id = $id;
453 function set_drug_id($drug_id) {
454 $this->drug_id = $drug_id;
456 function get_drug_id() {
457 return $this->drug_id;
460 function get_prescription_display() {
462 $pconfig = $GLOBALS['oer_config']['prescriptions'];
464 switch ($pconfig['format']) {
465 case "FL":
466 return $this->get_prescription_florida_display();
467 break;
468 default:
469 break;
471 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
472 $db = get_db();
473 $results = $db->Execute($sql);
474 if (!$results->EOF) {
476 $string = $results->fields['name'] . "\n"
477 . $results->fields['street'] . "\n"
478 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
479 . $results->fields['phone'] . "\n\n";
482 $string .= ""
483 ."Prescription For:" . "\t" .$this->patient->get_name_display() . "\n"
484 ."DOB:"."\t".$this->patient->get_dob()."\n"
485 ."Start Date: " . "\t\t" . $this->start_date. "\n"
486 ."Provider: " . "\t\t" . $this->provider->get_name_display(). "\n"
487 ."Provider DEA No.: " . "\t\t" . $this->provider->federal_drug_id. "\n"
488 ."Drug: " . "\t\t\t" . $this->drug. "\n"
489 ."Dosage: " . "\t\t" . $this->dosage . " in ". $this->form_array[$this->form]. " form " . $this->interval_array[$this->interval]. "\n"
490 ."Qty: " . "\t\t\t" . $this->quantity. "\n"
491 ."Medication Unit: " . "\t" . $this->size . " ". $this->unit_array[$this->unit] . "\n"
492 ."Substitute: " . "\t\t" . $this->substitute_array[$this->substitute]. "\n";
493 if ($this->refills > 0) {
494 $string .= "Refills: " . "\t\t" . $this->refills. ", of quantity: " . $this->per_refill ."\n";
496 $string .= "\n"."Notes: \n" . $this->note . "\n";
497 return $string;
500 function get_prescription_florida_display() {
502 $db = get_db();
503 $ntt = new NumberToText($this->quantity);
504 $ntt2 = new NumberToText($this->per_refill);
505 $ntt3 = new NumberToText($this->refills);
507 $string = "";
509 $gnd = $this->provider->get_name_display();
511 while(strlen($gnd)<31) {
512 $gnd .= " ";
515 $string .= $gnd . $this->provider->federal_drug_id . "\n";
517 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider->id) . "'";
518 $results = $db->Execute($sql);
520 if (!$results->EOF) {
521 $rfn = $results->fields['name'];
523 while(strlen($rfn)<31) {
524 $rfn .= " ";
527 $string .= $rfn . $this->provider->get_provider_number_default() . "\n"
528 . $results->fields['street'] . "\n"
529 . $results->fields['city'] . ", " . $results->fields['state'] . " " . $results->fields['postal_code'] . "\n"
530 . $results->fields['phone'] . "\n";
533 $string .= "\n";
534 $string .= strtoupper($this->patient->lname) . ", " . ucfirst($this->patient->fname) . " " . $this->patient->mname . "\n";
535 $string .= "DOB " . $this->patient->date_of_birth . "\n";
536 $string .= "\n";
537 $string .= date("F j, Y", strtotime($this->start_date)) . "\n";
538 $string .= "\n";
539 $string .= strtoupper($this->drug) . " " . $this->size . " ". $this->unit_array[$this->unit] . "\n";
540 if (strlen($this->note) > 0) {
541 $string .= "Notes: \n" . $this->note . "\n";
543 if (!empty($this->dosage)) {
544 $string .= $this->dosage;
545 if (!empty($this->form)){
546 $string .= " " . $this->form_array[$this->form];
548 if (!empty($this->interval)) {
549 $string .= " " . $this->interval_array[$this->interval];
551 if (!empty($this->route)) {
552 $string .= " " . $this->route_array[$this->route] . "\n";
555 if (!empty($this->quantity)) {
556 $string .= "Disp: " . $this->quantity . " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
558 $string .= "\n";
559 $string .= "Refills: " . $this->refills . " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill . " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
560 $string .= $this->substitute_array[$this->substitute]. "\n";
561 $string .= "\n";
563 return $string;
566 function prescriptions_factory($patient_id,$order_by = "active DESC, date_modified DESC, date_added DESC") {
567 $prescriptions = array();
568 $p = new Prescription();
569 $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);
570 $results = sqlQ($sql);
571 //echo "sql: $sql";
572 while ($row = mysql_fetch_array($results) ) {
573 $prescriptions[] = new Prescription($row['id']);
575 return $prescriptions;
578 function get_dispensation_count() {
579 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
580 "WHERE prescription_id = '" . $this->id . "' AND quantity > 0");
581 return $refills_row['count'];
584 } // end of Prescription