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");
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);
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);
37 define('FORM_UNITS',7);
38 define('FORM_INHILATIONS',8);
39 define('FORM_GTTS_DROPS',9);
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);
63 class Prescription
extends ORDataObject
{
78 var $substitute_array;
79 var $medication_array;
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(" ",
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; }
163 //$this->unit = UNIT_MG;
164 //$this->route = ROUTE_PER_ORIS;
165 //$this->quantity = 1;
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;
185 for($i=0;$i<21;$i++
) {
186 $this->refills_array
[$i] = sprintf("%02d",$i);
189 if ($id != "") { $this->populate(); }
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() {
202 function toString($html = false) {
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
] );
241 return( $this->unit_array
[$this->unit
] );
244 function get_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; }
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
);
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() {
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() {
298 function set_quantity($qty) {
299 // if (is_numeric($qty)) {
300 $this->quantity
= $qty;
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() {
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) {
331 $this->medication
= $med;
333 // Avoid making a mess if we are not using the "medication" issue type.
334 if (isset($ISSUE_TYPES) && !$ISSUE_TYPES['medication']) return;
336 //check if this drug is on the medication list
337 $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');
339 if ($med && !isset($dataRow['id'])){
340 $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');
342 if (!isset($dataRow['id'])){
343 //add the record to the medication list
344 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
. "')");
347 $dataRow = sqlQuery('update lists set activity = 1'
348 . " ,user = '" . $
$_SESSION['authUser']
349 . "', groupname = '" . $
$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
352 elseif (!$med && isset($dataRow['id'])) {
353 //remove the drug from the medication list if it exists
354 $dataRow = sqlQuery('update lists set activity = 0'
355 . " ,user = '" . $
$_SESSION['authUser']
356 . "', groupname = '" . $
$_SESSION['authProvider'] . "' where id = " . $dataRow['id']);
360 function get_medication() {
361 return $this->medication
;
364 function set_per_refill($pr) {
365 if (is_numeric($pr)) { $this->per_refill
= $pr; }
367 function get_per_refill() {
368 return $this->per_refill
;
371 function set_patient_id($id) {
372 if (is_numeric($id)) { $this->patient
= new Patient($id); }
374 function get_patient_id() {
375 return $this->patient
->id
;
378 function set_provider_id($id) {
379 if (is_numeric($id)) { $this->provider
= new Provider($id); }
381 function get_provider_id() {
382 return $this->provider
->id
;
385 function set_provider($pobj) {
386 if (get_class($pobj) == "provider") { $this->provider
= $pobj; }
389 function set_pharmacy_id($id) {
390 if (is_numeric($id)) { $this->pharmacy
= new Pharmacy($id); }
392 function get_pharmacy_id() {
393 return $this->pharmacy
->id
;
396 function set_pharmacist_id($id) {
397 if (is_numeric($id)) { $this->pharmacist
= new Person($id); }
399 function get_pharmacist() {
400 return $this->pharmacist
->id
;
403 function get_start_date_y() {
404 $ymd = split("-",$this->start_date
);
407 function set_start_date_y($year) {
408 if (is_numeric($year)) {
409 $ymd = split("-",$this->start_date
);
411 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
414 function get_start_date_m() {
415 $ymd = split("-",$this->start_date
);
418 function set_start_date_m($month) {
419 if (is_numeric($month)) {
420 $ymd = split("-",$this->start_date
);
422 $this->start_date
= $ymd[0] ."-" . $ymd[1] ."-" . $ymd[2];
425 function get_start_date_d() {
426 $ymd = split("-",$this->start_date
);
429 function set_start_date_d($day) {
430 if (is_numeric($day)) {
431 $ymd = split("-",$this->start_date
);
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;
443 function get_date_added() {
444 return $this->date_added
;
446 function set_date_added($date) {
447 return $this->date_added
= $date;
450 function get_date_modified() {
451 return $this->date_modified
;
453 function set_date_modified($date) {
454 return $this->date_modified
= $date;
457 function get_filled_date() {
458 return $this->filled_date
;
460 function set_filled_date($date) {
461 return $this->filled_date
= $date;
464 function set_note($note) {
467 function get_note() {
471 function set_drug($drug) {
474 function get_drug() {
478 function get_filled_by_id() {
479 return $this->pharmacist
->id
;
481 function set_filled_by_id($id) {
482 if (is_numeric($id)) { return $this->pharmacist
->id
= $id; }
485 function set_drug_id($drug_id) {
486 $this->drug_id
= $drug_id;
488 function get_drug_id() {
489 return $this->drug_id
;
492 function set_active($active) {
493 $this->active
= $active;
495 function get_active() {
496 return $this->active
;
499 function get_prescription_display() {
500 $pconfig = $GLOBALS['oer_config']['prescriptions'];
502 switch ($pconfig['format']) {
504 return $this->get_prescription_florida_display();
510 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider
->id
) . "'";
512 $results = $db->Execute($sql);
513 if (!$results->EOF
) {
514 $string = $results->fields
['name'] . "\n"
515 . $results->fields
['street'] . "\n"
516 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
517 . $results->fields
['phone'] . "\n\n";
521 ."Prescription For:" . "\t" .$this->patient
->get_name_display() . "\n"
522 ."DOB:"."\t".$this->patient
->get_dob()."\n"
523 ."Start Date: " . "\t\t" . $this->start_date
. "\n"
524 ."Provider: " . "\t\t" . $this->provider
->get_name_display(). "\n"
525 ."Provider DEA No.: " . "\t\t" . $this->provider
->federal_drug_id
. "\n"
526 ."Drug: " . "\t\t\t" . $this->drug
. "\n"
527 ."Dosage: " . "\t\t" . $this->dosage
. " in ". $this->form_array
[$this->form
]. " form " . $this->interval_array
[$this->interval
]. "\n"
528 ."Qty: " . "\t\t\t" . $this->quantity
. "\n"
529 ."Medication Unit: " . "\t" . $this->size
. " ". $this->unit_array
[$this->unit
] . "\n"
530 ."Substitute: " . "\t\t" . $this->substitute_array
[$this->substitute
]. "\n";
531 if ($this->refills
> 0) {
532 $string .= "Refills: " . "\t\t" . $this->refills
. ", of quantity: " . $this->per_refill
."\n";
534 $string .= "\n"."Notes: \n" . $this->note
. "\n";
538 function get_prescription_florida_display() {
541 $ntt = new NumberToText($this->quantity
);
542 $ntt2 = new NumberToText($this->per_refill
);
543 $ntt3 = new NumberToText($this->refills
);
547 $gnd = $this->provider
->get_name_display();
549 while(strlen($gnd)<31) { $gnd .= " "; }
551 $string .= $gnd . $this->provider
->federal_drug_id
. "\n";
553 $sql = "SELECT * FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" . mysql_real_escape_string($this->provider
->id
) . "'";
554 $results = $db->Execute($sql);
556 if (!$results->EOF
) {
557 $rfn = $results->fields
['name'];
559 while(strlen($rfn)<31) { $rfn .= " "; }
561 $string .= $rfn . $this->provider
->get_provider_number_default() . "\n"
562 . $results->fields
['street'] . "\n"
563 . $results->fields
['city'] . ", " . $results->fields
['state'] . " " . $results->fields
['postal_code'] . "\n"
564 . $results->fields
['phone'] . "\n";
568 $string .= strtoupper($this->patient
->lname
) . ", " . ucfirst($this->patient
->fname
) . " " . $this->patient
->mname
. "\n";
569 $string .= "DOB " . $this->patient
->date_of_birth
. "\n";
571 $string .= date("F j, Y", strtotime($this->start_date
)) . "\n";
573 $string .= strtoupper($this->drug
) . " " . $this->size
. " ". $this->unit_array
[$this->unit
] . "\n";
574 if (strlen($this->note
) > 0) {
575 $string .= "Notes: \n" . $this->note
. "\n";
577 if (!empty($this->dosage
)) {
578 $string .= $this->dosage
;
579 if (!empty($this->form
)){
580 $string .= " " . $this->form_array
[$this->form
];
582 if (!empty($this->interval
)) {
583 $string .= " " . $this->interval_array
[$this->interval
];
585 if (!empty($this->route
)) {
586 $string .= " " . $this->route_array
[$this->route
] . "\n";
589 if (!empty($this->quantity
)) {
590 $string .= "Disp: " . $this->quantity
. " (" . trim(strtoupper($ntt->convert())) . ")" . "\n";
593 $string .= "Refills: " . $this->refills
. " (" . trim(strtoupper($ntt3->convert())) ."), Per Refill Disp: " . $this->per_refill
. " (" . trim(strtoupper($ntt2->convert())) . ")" ."\n";
594 $string .= $this->substitute_array
[$this->substitute
]. "\n";
600 function prescriptions_factory($patient_id,
601 $order_by = "active DESC, date_modified DESC, date_added DESC")
603 $prescriptions = array();
604 require_once (dirname(__FILE__
) . "/../translation.inc.php");
605 $p = new Prescription();
606 $sql = "SELECT id FROM " . $p->_table
. " WHERE patient_id = " .
607 mysql_real_escape_string($patient_id) .
608 " ORDER BY " . mysql_real_escape_string($order_by);
609 $results = sqlQ($sql);
610 while ($row = mysql_fetch_array($results) ) {
611 $prescriptions[] = new Prescription($row['id']);
613 return $prescriptions;
616 function get_dispensation_count() {
617 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
618 "WHERE prescription_id = '" . $this->id
. "' AND quantity > 0");
619 return $refills_row['count'];
622 }// end of Prescription