file permission changes for windows cvs snapshot
[openemr.git] / controllers / C_Prescription.class.php
blob0f71f3b14b3d648fcbf4c3d4b63f6c64df35a2e1
1 <?php
3 require_once($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
4 require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
5 require_once($GLOBALS['fileroot'] . "/library/classes/Provider.class.php");
6 require_once($GLOBALS['fileroot'] . "/library/classes/RXList.class.php");
8 class C_Prescription extends Controller {
10 var $template_mod;
11 var $pconfig;
12 var $providerid = 0;
13 var $is_faxing = false;
15 function C_Prescription($template_mod = "general") {
16 parent::Controller();
18 $this->template_mod = $template_mod;
19 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
20 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
21 $this->assign("STYLE", $GLOBALS['style']);
22 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
23 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
24 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
26 if ($GLOBALS['inhouse_pharmacy']) {
27 // Make an array of drug IDs and selectors for the template.
28 $drug_array_values = array(0);
29 $drug_array_output = array("-- or select from inventory --");
30 $drug_attributes = '';
32 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
34 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
35 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
36 "t.period, t.quantity, t.refills " .
37 "FROM drug_templates AS t, drugs AS d WHERE " .
38 "d.drug_id = t.drug_id ORDER BY t.selector");
40 while ($row = sqlFetchArray($res)) {
41 $tmp_output = $row['selector'];
42 if ($row['ndc_number']) {
43 $tmp_output .= ' [' . $row['ndc_number'] . ']';
45 $drug_array_values[] = $row['drug_id'];
46 $drug_array_output[] = $tmp_output;
47 if ($drug_attributes) $drug_attributes .= ',';
48 $drug_attributes .= "['" .
49 $row['name'] . "'," . // 0
50 $row['form'] . ",'" . // 1
51 $row['dosage'] . "'," . // 2
52 $row['size'] . "," . // 3
53 $row['unit'] . "," . // 4
54 $row['route'] . "," . // 5
55 $row['period'] . "," . // 6
56 $row['substitute'] . "," . // 7
57 $row['quantity'] . "," . // 8
58 $row['refills'] . "," . // 9
59 $row['quantity'] . "]"; // 10 quantity per_refill
61 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
62 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
63 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
67 function default_action() {
68 $this->assign("prescription",$this->prescriptions[0]);
69 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
72 function edit_action($id = "",$patient_id="",$p_obj = null) {
74 if ($p_obj != null && get_class($p_obj) == "prescription") {
75 $this->prescriptions[0] = $p_obj;
77 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
78 $this->prescriptions[0] = new Prescription($id);
81 if (!empty($patient_id)) {
82 $this->prescriptions[0]->set_patient_id($patient_id);
85 // If quantity to dispense is not already set from a POST, set its
86 // default value.
87 if (! $this->get_template_vars('DISP_QUANTITY')) {
88 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
91 $this->default_action();
94 function list_action($id,$sort = "") {
95 if (empty($id)) {
96 $this->function_argument_error();
97 exit;
99 if (!empty($sort)) {
100 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
102 else {
103 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
105 //print_r(Prescription::prescriptions_factory($id));
106 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
109 function block_action($id,$sort = "") {
110 if (empty($id)) {
111 $this->function_argument_error();
112 exit;
114 if (!empty($sort)) {
115 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
117 else {
118 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
120 //print_r(Prescription::prescriptions_factory($id));
121 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
124 function lookup_action() {
125 $this->do_lookup();
126 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
129 function edit_action_process() {
130 if ($_POST['process'] != "true")
131 return;
132 //print_r($_POST);
134 // Stupid Smarty code treats empty values as not specified values.
135 // Since active is a checkbox, represent the unchecked state as -1.
136 if (empty($_POST['active'])) $_POST['active'] = '-1';
138 $this->prescriptions[0] = new Prescription($_POST['id']);
139 parent::populate_object($this->prescriptions[0]);
140 //echo $this->prescriptions[0]->toString(true);
141 $this->prescriptions[0]->persist();
142 $_POST['process'] = "";
144 // If the "Prescribe and Dispense" button was clicked, then
145 // redisplay as in edit_action() but also replicate the fee and
146 // include a piece of javascript to call dispense().
148 if ($_POST['disp_button']) {
149 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
150 $this->assign("DISP_FEE", $_POST['disp_fee']);
151 $this->assign("ENDING_JAVASCRIPT", "dispense();");
152 $this->_state = false;
153 return $this->edit_action($this->prescriptions[0]->id);
156 if ($this->prescriptions[0]->get_active() > 0) {
157 return $this->send_action($this->prescriptions[0]->id);
159 $this->list_action($this->prescriptions[0]->get_patient_id());
160 exit;
163 function send_action($id) {
164 $_POST['process'] = "true";
165 if(empty($id)) {
166 $this->function_argument_error();
169 $rx = new Prescription($id);
170 // Populate pharmacy info if the patient has a default pharmacy.
171 // Probably the Prescription object should handle this instead, but
172 // doing it there will require more careful research and testing.
173 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
174 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
175 if ($prow['pharmacy_id']) {
176 $rx->pharmacy->set_id($prow['pharmacy_id']);
177 $rx->pharmacy->populate();
179 $this->assign("prescription", $rx);
181 $this->_state = false;
182 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
183 $this->template_mod . "_send.html");
186 function multiprint_header(& $pdf, $p) {
187 $this->providerid = $p->provider->id;
188 //print header
189 $pdf->ezImage($GLOBALS['fileroot'] . '/interface/pic/Rx.png','','50','','center','');
190 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
191 $res = sqlQuery("SELECT concat('<b>',f.name,'</b>\n',f.street,'\n',f.city,', ',f.state,' ',f.postal_code,'\nTel:',f.phone,if(f.fax != '',concat('\nFax: ',f.fax),'')) addr FROM users JOIN facility AS f ON f.name = users.facility where users.id ='" .
192 mysql_real_escape_string($p->provider->id) . "'");
193 $pdf->ezText($res['addr'],12);
194 $my_y = $pdf->y;
195 $pdf->ezNewPage();
196 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
197 // A client had a bad experience with a patient misusing a DEA number, so
198 // now the doctors write those in on printed prescriptions and only when
199 // necessary. If you need to change this back, then please make it a
200 // configurable option. Faxed prescriptions were not changed. -- Rod
201 // Now it is configureable. Change value in
202 // <openemr root>/includes/config.php - Tekknogenius
203 if ($this->is_faxing || $GLOBALS['oer_config']['prescriptions']['show_DEA'])
204 $pdf->ezText('<b>DEA:</b>' . $p->provider->federal_drug_id, 12);
205 else
206 $pdf->ezText('<b>DEA:</b> ________________________', 12);
207 $pdf->ezColumnsStop();
208 if ($my_y < $pdf->y){
209 $pdf->ezSetY($my_y);
211 $pdf->ezText('',10);
212 $pdf->setLineStyle(1);
213 $pdf->ezColumnsStart(array('num'=>2));
214 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
215 $pdf->ezText('<b>Patient Name & Address</b>',6);
216 $pdf->ezText($p->patient->get_name_display(),10);
217 $res = sqlQuery("SELECT concat(street,'\n',city,', ',state,' ',postal_code,'\n',if(phone_home!='',phone_home,if(phone_cell!='',phone_cell,if(phone_biz!='',phone_biz,'')))) addr from patient_data where pid =". mysql_real_escape_string ($p->patient->id));
218 $pdf->ezText($res['addr']);
219 $my_y = $pdf->y;
220 $pdf->ezNewPage();
221 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
222 $pdf->ezText('<b>Date of Birth</b>',6);
223 $pdf->ezText($p->patient->date_of_birth,10);
224 $pdf->ezText('');
225 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
226 $pdf->ezText('<b>Medical Record #</b>',6);
227 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
228 $pdf->ezColumnsStop();
229 if ($my_y < $pdf->y){
230 $pdf->ezSetY($my_y);
232 $pdf->ezText('');
233 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
234 $pdf->ezText('<b>Prescriptions</b>',6);
235 $pdf->ezText('',10);
238 function multiprint_footer(& $pdf) {
239 if($this->pconfig['use_signature'] && $this->is_faxing) {
240 $sigfile = str_replace('{userid}', $this->providerid, $this->pconfig['signature']);
241 if (file_exists($sigfile)) {
242 $pdf->ezText("Signature: ",12);
243 // $pdf->ezImage($sigfile, "", "", "none", "left");
244 $pdf->ezImage($sigfile, "", "", "none", "center");
245 $pdf->ezText("Date: " . date('Y-m-d'), 12);
246 return;
249 $pdf->ezText("\n\n\n\nSignature:________________________________\nDate: " . date('Y-m-d'),12);
252 function get_prescription_body_text($p) {
253 $body = '<b>Rx: ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
254 if ($p->get_form()) $body .= ' [' . $p->form_array[$p->get_form()] . "]";
255 $body .= "</b> <i>" .
256 $p->substitute_array[$p->get_substitute()] . "</i>\n" .
257 '<b>Disp #:</b> <u>' . $p->get_quantity() . "</u>\n" .
258 '<b>Sig:</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] . ' ' .
259 $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
260 if ($p->get_refills() > 0) {
261 $body .= "\n<b>Refills:</b> <u>" . $p->get_refills();
262 if ($p->get_per_refill()) {
263 $body .= " of quantity " . $p->get_per_refill();
265 $body .= "</u>\n";
267 else {
268 $body .= "\n<b>Refills:</b> <u>0 (Zero)</u>\n";
270 $note = $p->get_note();
271 if ($note != '') {
272 $body .= "\n$note\n";
274 return $body;
277 function multiprint_body(& $pdf, $p){
278 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
279 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
280 $d = $this->get_prescription_body_text($p);
281 if ( $pdf->ezText($d,10,array(),1) ) {
282 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
283 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
284 $this->multiprint_footer($pdf);
285 $pdf->ezNewPage();
286 $this->multiprint_header($pdf, $p);
287 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
288 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
290 $my_y = $pdf->y;
291 $pdf->ezText($d,10);
292 if($this->pconfig['shading']) {
293 $pdf->setColor(.9,.9,.9);
294 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
295 $pdf->setColor(0,0,0);
297 $pdf->ezSetY($my_y);
298 $pdf->ezText($d,10);
299 $pdf->ez['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
300 $pdf->ez['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
301 $pdf->ezText('');
302 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
303 $pdf->ezText('');
306 function multiprint_action($id = "") {
307 $_POST['process'] = "true";
308 if(empty($id)) {
309 $this->function_argument_error();
311 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
312 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
313 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
314 ,$GLOBALS['oer_config']['prescriptions']['bottom']
315 ,$GLOBALS['oer_config']['prescriptions']['left']
316 ,$GLOBALS['oer_config']['prescriptions']['right']
318 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
320 // $print_header = true;
321 $on_this_page = 0;
323 //print prescriptions body
324 $this->_state = false; // Added by Rod - see Controller.class.php
325 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
326 foreach ($ids as $id) {
327 $p = new Prescription($id);
328 // if ($print_header == true) {
329 if ($on_this_page == 0) {
330 $this->multiprint_header($pdf, $p);
332 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
333 $this->multiprint_footer($pdf);
334 $pdf->ezNewPage();
335 $this->multiprint_header($pdf, $p);
336 // $print_header = false;
337 $on_this_page = 1;
339 $this->multiprint_body($pdf, $p);
342 $this->multiprint_footer($pdf);
344 $pdf->ezStream();
345 return;
348 function send_action_process($id) {
349 $dummy = ""; // Added by Rod to avoid run-time warnings
350 if ($_POST['process'] != "true")
351 return;
352 if(empty($id)) {
353 $this->function_argument_error();
355 $p = new Prescription($id);
356 switch ($_POST['submit']) {
358 case "Print":
359 // The following statement added by Rod.
360 // Looking at Controller.class.php, it appears that _state is set to false
361 // to indicate that no further HTML is to be generated.
362 $this->_state = false; // Added by Rod - see Controller.class.php
363 return $this->_print_prescription($p, $dummy);
364 break;
365 case "Email":
366 return $this->_email_prescription($p,$_POST['email_to']);
367 break;
368 case "Fax":
369 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
370 //$this->assign("process_result","No fax server is currently setup.");
371 return $this->_fax_prescription($p,$_POST['fax_to']);
372 break;
373 case "Auto Send":
374 $pharmacy_id = $_POST['pharmacy_id'];
375 //echo "auto sending to : " . $_POST['pharmacy_id'];
376 $phar = new Pharmacy($_POST['pharmacy_id']);
377 //print_r($phar);
378 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
379 return $this->_print_prescription($p, $dummy);
381 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
382 $email = $phar->get_email();
383 if (!empty($email)) {
384 return $this->_email_prescription($p,$phar->get_email());
386 //else print it
388 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
389 $faxNum= $phar->get_fax();
390 if(!empty($faxNum)) {
391 Return $this->_fax_prescription ($p,$faxNum);
393 // return $this->assign("process_result","No fax server is currently setup.");
394 // else default is printing,
396 else {
397 //the pharmacy has no default or default is print
398 return $this->_print_prescription($p, $dummy);
400 break;
403 return;
407 function _print_prescription($p, & $toFile) {
408 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
409 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
410 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
411 ,$GLOBALS['oer_config']['prescriptions']['bottom']
412 ,$GLOBALS['oer_config']['prescriptions']['left']
413 ,$GLOBALS['oer_config']['prescriptions']['right']
416 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
418 // Signature images are to be used only when faxing.
419 if(!empty($toFile)) $this->is_faxing = true;
421 $this->multiprint_header($pdf, $p);
422 $this->multiprint_body($pdf, $p);
423 $this->multiprint_footer($pdf);
425 if(!empty($toFile)) {
426 $toFile = $pdf->ezOutput();
428 else {
429 $pdf->ezStream();
430 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
432 return;
435 function _print_prescription_old($p, & $toFile) {
436 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
437 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
438 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
439 ,$GLOBALS['oer_config']['prescriptions']['bottom']
440 ,$GLOBALS['oer_config']['prescriptions']['left']
441 ,$GLOBALS['oer_config']['prescriptions']['right']
443 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
444 if(!empty($this->pconfig['logo'])) {
445 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
447 $pdf->ezText($p->get_prescription_display(),10);
448 if($this->pconfig['use_signature']) {
449 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
451 else{
452 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
454 if(!empty($toFile))
456 $toFile = $pdf->ezOutput();
458 else
460 $pdf->ezStream();
461 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
463 return;
466 function _email_prescription($p,$email) {
467 if (empty($email)) {
468 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
469 return;
471 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
472 $mail = new PHPMailer();
473 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
474 //this is a temporary config item until the rest of the per practice billing settings make their way in
475 $mail->From = $GLOBALS['practice_return_email_path'];
476 $mail->FromName = $p->provider->get_name_display();
477 $mail->isMail();
478 $mail->Host = "localhost";
479 $mail->Mailer = "mail";
480 $text_body = $p->get_prescription_display();
481 $mail->Body = $text_body;
482 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
483 $mail->AddAddress($email);
484 if($mail->Send()) {
485 $this->assign("process_result","Email was successfully sent to: " . $email);
486 return;
488 else {
489 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
490 return;
494 function do_lookup() {
495 if ($_POST['process'] != "true") {
496 // don't do a lookup
497 $this->assign("drug", $_GET['drug']);
498 return;
501 // process the lookup
502 $this->assign("drug", $_POST['drug']);
503 $list = array();
504 if (!empty($_POST['drug'])) {
505 $list = @RxList::get_list($_POST['drug']);
508 if (is_array($list)) {
509 $list = array_flip($list);
510 $this->assign("drug_options",$list);
511 $this->assign("drug_values",array_keys($list));
513 else {
514 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
516 //print_r($_POST);
517 //$this->assign("PROCESS","");
519 $_POST['process'] = "";
522 function _fax_prescription($p,$faxNum)
524 $err = "Sent fax";
525 //strip - ,(, ), and ws
526 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
527 //validate the number
529 if(!empty($faxNum) && is_numeric($faxNum))
531 //get the sendfax command and execute it
532 $cmd = $this->pconfig['sendfax'];
533 // prepend any prefix to the fax number
534 $pref=$this->pconfig['prefix'];
535 $faxNum=$pref.$faxNum;
536 if(empty($cmd))
538 $err .= " Send fax not set in includes/config.php";
539 break;
541 else
543 //generate file to fax
544 $faxFile = "Failed";
545 $this->_print_prescription($p, $faxFile);
546 if(empty($faxFile))
548 $err .= " _print_prescription returned empty file";
549 break;
551 $fileName = dirname(__FILE__)."/../documents/".$p->get_id()
552 .$p->get_patient_id()."_fax_.pdf";
553 //print "filename is $fileName";
554 touch($fileName); // php bug
555 $handle = fopen($fileName,"w");
556 if(!$handle)
558 $err .= " Failed to open file $fileName to write fax to";
559 break;
561 if(fwrite($handle, $faxFile) === false)
563 $err .= " Failed to write data to $fileName";
564 break;
566 fclose($handle);
567 $args = " -n -d $faxNum $fileName";
568 //print "command is $cmd $args<br>";
569 exec($cmd . $args);
573 else
575 $err = "bad fax number passed to function";
577 if($err)
579 $this->assign("process_result",$err);