quick minor path updates (#1968)
[openemr.git] / controllers / C_Prescription.class.php
blob31a48446794ed79e428174e58569589e066d1b34
1 <?php
2 /**
3 * C_Prescription class
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Roberto Vasquez <robertogagliotta@gmail.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2015 Roberto Vasquez <robertogagliotta@gmail.com>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
16 require_once($GLOBALS['fileroot'] . "/library/registry.inc");
17 require_once($GLOBALS['fileroot'] . "/library/amc.php");
19 use PHPMailer\PHPMailer\PHPMailer;
21 class C_Prescription extends Controller
24 var $template_mod;
25 var $pconfig;
26 var $providerid = 0;
27 var $is_faxing = false;
28 var $is_print_to_fax = false;
30 function __construct($template_mod = "general")
32 parent::__construct();
34 $this->template_mod = $template_mod;
35 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . attr($_SERVER['QUERY_STRING']));
36 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
37 $this->assign("STYLE", $GLOBALS['style']);
38 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
39 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
40 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
41 $this->RxList = new RxList();
43 if ($GLOBALS['inhouse_pharmacy']) {
44 // Make an array of drug IDs and selectors for the template.
45 $drug_array_values = array(0);
46 $drug_array_output = array("-- " . xl('or select from inventory') ." --");
47 $drug_attributes = '';
49 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
51 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
52 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
53 "t.period, t.quantity, t.refills, d.drug_code " .
54 "FROM drug_templates AS t, drugs AS d WHERE " .
55 "d.drug_id = t.drug_id ORDER BY t.selector");
57 while ($row = sqlFetchArray($res)) {
58 $tmp_output = $row['selector'];
59 if ($row['ndc_number']) {
60 $tmp_output .= ' [' . $row['ndc_number'] . ']';
63 $drug_array_values[] = $row['drug_id'];
64 $drug_array_output[] = $tmp_output;
65 if ($drug_attributes) {
66 $drug_attributes .= ',';
69 $drug_attributes .= "['" .
70 attr($row['name']) . "'," . // 0
71 attr($row['form']) . ",'" . // 1
72 attr($row['dosage']) . "','" . // 2
73 attr($row['size']) . "'," . // 3
74 attr($row['unit']) . "," . // 4
75 attr($row['route']) . "," . // 5
76 attr($row['period']) . "," . // 6
77 attr($row['substitute']) . "," . // 7
78 attr($row['quantity']) . "," . // 8
79 attr($row['refills']) . "," . // 9
80 attr($row['quantity']) . "," . // 10 quantity per_refill
81 attr($row['drug_code']) . "]"; // 11 rxnorm drug code
84 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
85 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
86 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
90 function default_action()
92 $this->assign("prescription", $this->prescriptions[0]);
93 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
96 function edit_action($id = "", $patient_id = "", $p_obj = null)
99 if ($p_obj != null && get_class($p_obj) == "prescription") {
100 $this->prescriptions[0] = $p_obj;
101 } elseif (!is_object($this->prescriptions[0]) || get_class($this->prescriptions[0]) != "prescription") {
102 $this->prescriptions[0] = new Prescription($id);
105 if (!empty($patient_id)) {
106 $this->prescriptions[0]->set_patient_id($patient_id);
109 // If quantity to dispense is not already set from a POST, set its
110 // default value.
111 if (! $this->get_template_vars('DISP_QUANTITY')) {
112 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
115 $this->default_action();
118 function list_action($id, $sort = "")
120 if (empty($id)) {
121 $this->function_argument_error();
122 exit;
125 if (!empty($sort)) {
126 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
127 } else {
128 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
131 // flag to indicate the CAMOS form is regsitered and active
132 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
134 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
137 function block_action($id, $sort = "")
139 if (empty($id)) {
140 $this->function_argument_error();
141 exit;
144 if (!empty($sort)) {
145 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
146 } else {
147 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
150 //print_r(Prescription::prescriptions_factory($id));
151 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
154 function fragment_action($id, $sort = "")
156 if (empty($id)) {
157 $this->function_argument_error();
158 exit;
161 if (!empty($sort)) {
162 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
163 } else {
164 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
167 //print_r(Prescription::prescriptions_factory($id));
168 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
171 function lookup_action()
173 $this->do_lookup();
174 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
177 function edit_action_process()
179 if ($_POST['process'] != "true") {
180 return;
183 //print_r($_POST);
185 // Stupid Smarty code treats empty values as not specified values.
186 // Since active is a checkbox, represent the unchecked state as -1.
187 if (empty($_POST['active'])) {
188 $_POST['active'] = '-1';
191 $this->prescriptions[0] = new Prescription($_POST['id']);
192 parent::populate_object($this->prescriptions[0]);
193 //echo $this->prescriptions[0]->toString(true);
194 $this->prescriptions[0]->persist();
195 $_POST['process'] = "";
197 // If the "Prescribe and Dispense" button was clicked, then
198 // redisplay as in edit_action() but also replicate the fee and
199 // include a piece of javascript to call dispense().
201 if ($_POST['disp_button']) {
202 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
203 $this->assign("DISP_FEE", $_POST['disp_fee']);
204 $this->assign("ENDING_JAVASCRIPT", "dispense();");
205 $this->_state = false;
206 return $this->edit_action($this->prescriptions[0]->id);
209 // Set the AMC reporting flag (to record percentage of prescriptions that
210 // are set as e-prescriptions)
211 if (!(empty($_POST['escribe_flag']))) {
212 // add the e-prescribe flag
213 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
214 } else {
215 // remove the e-prescribe flag
216 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
219 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
220 if (!(empty($_POST['checked_formulary_flag']))) {
221 // add the e-prescribe flag
222 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
223 } else {
224 // remove the e-prescribe flag
225 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
228 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
229 if (!(empty($_POST['controlled_substance_flag']))) {
230 // add the e-prescribe flag
231 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
232 } else {
233 // remove the e-prescribe flag
234 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
237 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
238 // if ($this->prescriptions[0]->get_active() > 0) {
239 // return $this->send_action($this->prescriptions[0]->id);
240 // }
241 $this->list_action($this->prescriptions[0]->get_patient_id());
242 exit;
245 function send_action($id)
247 $_POST['process'] = "true";
248 if (empty($id)) {
249 $this->function_argument_error();
252 $rx = new Prescription($id);
253 // Populate pharmacy info if the patient has a default pharmacy.
254 // Probably the Prescription object should handle this instead, but
255 // doing it there will require more careful research and testing.
256 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
257 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
258 if ($prow['pharmacy_id']) {
259 $rx->pharmacy->set_id($prow['pharmacy_id']);
260 $rx->pharmacy->populate();
263 $this->assign("prescription", $rx);
265 $this->_state = false;
266 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
267 $this->template_mod . "_send.html");
270 function multiprintfax_header(& $pdf, $p)
272 return $this->multiprint_header($pdf, $p);
275 function multiprint_header(& $pdf, $p)
277 $this->providerid = $p->provider->id;
278 //print header
279 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'], '', '50', '', 'center', '');
280 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
281 $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 ='" .
282 add_escape_custom($p->provider->id) . "'");
283 $pdf->ezText($res['addr'], 12);
284 $my_y = $pdf->y;
285 $pdf->ezNewPage();
286 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>', 12);
287 // A client had a bad experience with a patient misusing a DEA number, so
288 // now the doctors write those in on printed prescriptions and only when
289 // necessary. If you need to change this back, then please make it a
290 // configurable option. Faxed prescriptions were not changed. -- Rod
291 // Now it is configureable. Change value in
292 // Administration->Globals->Rx
293 if ($GLOBALS['rx_enable_DEA']) {
294 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
295 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
296 } else {
297 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
301 if ($GLOBALS['rx_enable_NPI']) {
302 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
303 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
304 } else {
305 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
309 if ($GLOBALS['rx_enable_SLN']) {
310 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
311 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
312 } else {
313 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
317 $pdf->ezColumnsStop();
318 if ($my_y < $pdf->y) {
319 $pdf->ezSetY($my_y);
322 $pdf->ezText('', 10);
323 $pdf->setLineStyle(1);
324 $pdf->ezColumnsStart(array('num'=>2));
325 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
326 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>', 6);
327 $pdf->ezText($p->patient->get_name_display(), 10);
328 $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 =". add_escape_custom($p->patient->id));
329 $pdf->ezText($res['addr']);
330 $my_y = $pdf->y;
331 $pdf->ezNewPage();
332 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
333 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>', 6);
334 $pdf->ezText($p->patient->date_of_birth, 10);
335 $pdf->ezText('');
336 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
337 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>', 6);
338 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT), 10);
339 $pdf->ezColumnsStop();
340 if ($my_y < $pdf->y) {
341 $pdf->ezSetY($my_y);
344 $pdf->ezText('');
345 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
346 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>', 6);
347 $pdf->ezText('', 10);
350 function multiprintcss_header($p)
352 echo("<div class='paddingdiv'>\n");
353 $this->providerid = $p->provider->id;
354 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
355 echo ("<tr>\n");
356 echo ("<td></td>\n");
357 echo ("<td>\n");
358 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
359 echo ("</td>\n");
360 echo ("</tr>\n");
361 echo ("<tr>\n");
362 echo ("<td>\n");
363 $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 ='" . add_escape_custom($p->provider->id) . "'");
364 if (!empty($res)) {
365 $patterns = array ('/\n/','/Tel:/','/Fax:/');
366 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
367 $res = preg_replace($patterns, $replace, $res);
370 echo ('<span class="large">' . $res['addr'] . '</span>');
371 echo ("</td>\n");
372 echo ("<td>\n");
373 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
375 if ($GLOBALS['rx_enable_DEA']) {
376 if ($GLOBALS['rx_show_DEA']) {
377 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
378 } else {
379 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
383 if ($GLOBALS['rx_enable_NPI']) {
384 if ($GLOBALS['rx_show_NPI']) {
385 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
386 } else {
387 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
391 if ($GLOBALS['rx_enable_SLN']) {
392 if ($GLOBALS['rx_show_SLN']) {
393 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
394 } else {
395 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
399 echo ("</td>\n");
400 echo ("</tr>\n");
401 echo ("<tr>\n");
402 echo ("<td rowspan='2' class='bordered'>\n");
403 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
404 echo ($p->patient->get_name_display() . '<br>');
405 $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 =". add_escape_custom($p->patient->id));
406 if (!empty($res)) {
407 $patterns = array ('/\n/');
408 $replace = array ('<br>');
409 $res = preg_replace($patterns, $replace, $res);
412 echo ($res['addr']);
413 echo ("</td>\n");
414 echo ("<td class='bordered'>\n");
415 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
416 echo ($p->patient->date_of_birth );
417 echo ("</td>\n");
418 echo ("</tr>\n");
419 echo ("<tr>\n");
420 echo ("<td class='bordered'>\n");
421 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
422 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
423 echo ("</td>\n");
424 echo ("</tr>\n");
425 echo ("<tr>\n");
426 echo ("<td colspan='2' class='bordered'>\n");
427 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
428 echo ("</td>\n");
429 echo ("</tr>\n");
430 echo ("</table>\n");
433 function multiprintcss_preheader()
435 // this sets styling and other header information of the multiprint css sheet
436 echo ("<html>\n");
437 echo ("<head>\n");
438 echo ("<style>\n");
439 echo ("div {\n");
440 echo (" padding: 0;\n");
441 echo (" margin: 0;\n");
442 echo ("}\n");
443 echo ("body {\n");
444 echo (" font-family: sans-serif;\n");
445 echo (" font-weight: normal;\n");
446 echo (" font-size: 10pt;\n");
447 echo (" background: white;\n");
448 echo (" color: black;\n");
449 echo ("}\n");
450 echo ("span.large {\n");
451 echo (" font-size: 12pt;\n");
452 echo ("}\n");
453 echo ("span.small {\n");
454 echo (" font-size: 6pt;\n");
455 echo ("}\n");
456 echo ("td {\n");
457 echo (" vertical-align: top;\n");
458 echo (" width: 50%;\n");
459 echo (" font-size: 10pt;\n");
460 echo (" padding-bottom: 8pt;\n");
461 echo ("}\n");
462 echo ("td.bordered {\n");
463 echo (" border-top:1pt solid black;\n");
464 echo ("}\n");
465 echo ("div.paddingdiv {\n");
466 echo (" width: 524pt;\n");
467 echo (" height: 668pt;\n");
468 echo ("}\n");
469 echo ("div.scriptdiv {\n");
470 echo (" padding-top: 12pt;\n");
471 echo (" padding-bottom: 22pt;\n");
472 echo (" padding-left: 35pt;\n");
473 echo (" border-bottom:1pt solid black;\n");
474 echo ("}\n");
475 echo ("div.signdiv {\n");
476 echo (" margin-top: 40pt;\n");
477 echo (" font-size: 12pt;\n");
478 echo ("}\n");
479 echo ("</style>\n");
481 echo ("<title>" . xl('Prescription') . "</title>\n");
482 echo ("</head>\n");
483 echo ("<body>\n");
486 function multiprintfax_footer(& $pdf)
488 return $this->multiprint_footer($pdf);
491 function multiprint_footer(& $pdf)
493 if ($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax )) {
494 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
495 if (file_exists($sigfile)) {
496 $pdf->ezText(xl('Signature') . ": ", 12);
497 // $pdf->ezImage($sigfile, "", "", "none", "left");
498 $pdf->ezImage($sigfile, "", "", "none", "center");
499 $pdf->ezText(xl('Date') . ": " . date('Y-m-d'), 12);
500 if ($this->is_print_to_fax) {
501 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
504 $addenumFile = $this->pconfig['addendum_file'];
505 if (file_exists($addenumFile)) {
506 $pdf->ezText('');
507 $f = fopen($addenumFile, "r");
508 while ($line = fgets($f, 1000)) {
509 $pdf->ezText(rtrim($line));
513 return;
517 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'), 12);
520 function multiprintcss_footer()
522 echo ("<div class='signdiv'>\n");
523 echo (xl('Signature') . ":________________________________<br>");
524 echo (xl('Date') . ": " . date('Y-m-d'));
525 echo ("</div>\n");
526 echo ("</div>\n");
529 function multiprintcss_postfooter()
531 echo("<script language='JavaScript'>\n");
532 echo("opener.top.printLogPrint(window);\n");
533 echo("</script>\n");
534 echo("</body>\n");
535 echo("</html>\n");
538 function get_prescription_body_text($p)
540 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
541 if ($p->get_form()) {
542 $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
545 $body .= "</b> <i>" .
546 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
547 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
548 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
549 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
550 if ($p->get_refills() > 0) {
551 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
552 if ($p->get_per_refill()) {
553 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
556 $body .= "</u>\n";
557 } else {
558 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
561 $note = $p->get_note();
562 if ($note != '') {
563 $body .= "\n" . text($note) . "\n";
566 return $body;
569 function multiprintfax_body(& $pdf, $p)
571 return $this->multiprint_body($pdf, $p);
574 function multiprint_body(& $pdf, $p)
576 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
577 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
578 $d = $this->get_prescription_body_text($p);
579 if ($pdf->ezText($d, 10, array(), 1)) {
580 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
581 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
582 $this->multiprint_footer($pdf);
583 $pdf->ezNewPage();
584 $this->multiprint_header($pdf, $p);
585 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
586 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
589 $my_y = $pdf->y;
590 $pdf->ezText($d, 10);
591 if ($this->pconfig['shading']) {
592 $pdf->setColor(.9, .9, .9);
593 $pdf->filledRectangle($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'], $my_y - $pdf->y);
594 $pdf->setColor(0, 0, 0);
597 $pdf->ezSetY($my_y);
598 $pdf->ezText($d, 10);
599 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
600 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
601 $pdf->ezText('');
602 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
603 $pdf->ezText('');
606 function multiprintcss_body($p)
608 $d = $this->get_prescription_body_text($p);
609 $patterns = array ('/\n/','/ /');
610 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
611 $d = preg_replace($patterns, $replace, $d);
612 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
615 function multiprintfax_action($id = "")
617 $this->is_print_to_fax=true;
618 return $this->multiprint_action($id);
621 function multiprint_action($id = "")
623 $_POST['process'] = "true";
624 if (empty($id)) {
625 $this->function_argument_error();
628 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
629 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
630 $pdf->selectFont('Helvetica');
632 // $print_header = true;
633 $on_this_page = 0;
635 //print prescriptions body
636 $this->_state = false; // Added by Rod - see Controller.class.php
637 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
638 foreach ($ids as $id) {
639 $p = new Prescription($id);
640 // if ($print_header == true) {
641 if ($on_this_page == 0) {
642 $this->multiprint_header($pdf, $p);
645 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
646 $this->multiprint_footer($pdf);
647 $pdf->ezNewPage();
648 $this->multiprint_header($pdf, $p);
649 // $print_header = false;
650 $on_this_page = 1;
653 $this->multiprint_body($pdf, $p);
656 $this->multiprint_footer($pdf);
658 $pFirstName = $p->patient->fname; //modified by epsdky for prescription title change to include patient name and ID
659 $pFName = convert_safe_file_dir_name($pFirstName);
660 $modedFileName = "Rx_{$pFName}_{$p->patient->id}.pdf";
662 $pdf->ezStream(array('Content-Disposition' => $modedFileName));
663 return;
666 function multiprintcss_action($id = "")
668 $_POST['process'] = "true";
669 if (empty($id)) {
670 $this->function_argument_error();
673 $this->multiprintcss_preheader();
675 $this->_state = false; // Added by Rod - see Controller.class.php
676 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
678 $on_this_page = 0;
679 foreach ($ids as $id) {
680 $p = new Prescription($id);
681 if ($on_this_page == 0) {
682 $this->multiprintcss_header($p);
685 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
686 $this->multiprintcss_footer();
687 $this->multiprintcss_header($p);
688 $on_this_page = 1;
691 $this->multiprintcss_body($p);
694 $this->multiprintcss_footer();
695 $this->multiprintcss_postfooter();
696 return;
699 function send_action_process($id)
701 $dummy = ""; // Added by Rod to avoid run-time warnings
702 if ($_POST['process'] != "true") {
703 return;
706 if (empty($id)) {
707 $this->function_argument_error();
710 $p = new Prescription($id);
711 switch ($_POST['submit']) {
712 case (xl("Print")." (".xl("PDF").")"):
713 // The following statement added by Rod.
714 // Looking at Controller.class.php, it appears that _state is set to false
715 // to indicate that no further HTML is to be generated.
716 $this->_state = false; // Added by Rod - see Controller.class.php
717 return $this->_print_prescription($p, $dummy);
718 break;
719 case (xl("Print")." (".xl("HTML").")"):
720 $this->_state = false;
721 return $this->_print_prescription_css($p, $dummy);
722 break;
723 case xl("Print To Fax"):
724 $this->_state = false;
725 $this->is_print_to_fax = true;
726 return $this->_print_prescription($p, $dummy);
727 break;
728 case xl("Email"):
729 return $this->_email_prescription($p, $_POST['email_to']);
730 break;
731 case xl("Fax"):
732 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
733 //$this->assign("process_result","No fax server is currently setup.");
734 return $this->_fax_prescription($p, $_POST['fax_to']);
735 break;
736 case xl("Auto Send"):
737 $pharmacy_id = $_POST['pharmacy_id'];
738 //echo "auto sending to : " . $_POST['pharmacy_id'];
739 $phar = new Pharmacy($_POST['pharmacy_id']);
740 //print_r($phar);
741 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
742 return $this->_print_prescription($p, $dummy);
743 } elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
744 $email = $phar->get_email();
745 if (!empty($email)) {
746 return $this->_email_prescription($p, $phar->get_email());
749 //else print it
750 } elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
751 $faxNum= $phar->get_fax();
752 if (!empty($faxNum)) {
753 return $this->_fax_prescription($p, $faxNum);
756 // return $this->assign("process_result","No fax server is currently setup.");
757 // else default is printing,
758 } else {
759 //the pharmacy has no default or default is print
760 return $this->_print_prescription($p, $dummy);
762 break;
765 return;
768 function _print_prescription($p, & $toFile)
770 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
771 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
773 $pdf->selectFont('Helvetica');
775 // Signature images are to be used only when faxing.
776 if (!empty($toFile)) {
777 $this->is_faxing = true;
780 $this->multiprint_header($pdf, $p);
781 $this->multiprint_body($pdf, $p);
782 $this->multiprint_footer($pdf);
784 if (!empty($toFile)) {
785 $toFile = $pdf->ezOutput();
786 } else {
787 $pdf->ezStream();
788 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
791 return;
794 function _print_prescription_css($p, & $toFile)
797 $this->multiprintcss_preheader();
798 $this->multiprintcss_header($p);
799 $this->multiprintcss_body($p);
800 $this->multiprintcss_footer();
801 $this->multiprintcss_postfooter();
804 function _print_prescription_old($p, & $toFile)
806 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
807 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
808 $pdf->selectFont('Helvetica');
809 if (!empty($this->pconfig['logo'])) {
810 $pdf->ezImage($this->pconfig['logo'], "", "", "none", "left");
813 $pdf->ezText($p->get_prescription_display(), 10);
814 if ($this->pconfig['use_signature']) {
815 $pdf->ezImage($this->pconfig['signature'], "", "", "none", "left");
816 } else {
817 $pdf->ezText("\n\n\n\nSignature:________________________________", 10);
820 if (!empty($toFile)) {
821 $toFile = $pdf->ezOutput();
822 } else {
823 $pdf->ezStream();
824 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
827 return;
830 function _email_prescription($p, $email)
832 if (empty($email)) {
833 $this->assign("process_result", "Email could not be sent, the address supplied: '$email' was empty or invalid.");
834 return;
837 $mail = new PHPMailer();
838 //this is a temporary config item until the rest of the per practice billing settings make their way in
839 $mail->From = $GLOBALS['practice_return_email_path'];
840 $mail->FromName = $p->provider->get_name_display();
841 $mail->isMail();
842 $mail->Host = "localhost";
843 $mail->Mailer = "mail";
844 $text_body = $p->get_prescription_display();
845 $mail->Body = $text_body;
846 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
847 $mail->AddAddress($email);
848 if ($mail->Send()) {
849 $this->assign("process_result", "Email was successfully sent to: " . $email);
850 return;
851 } else {
852 $this->assign("process_result", "There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
853 return;
857 function do_lookup()
859 if ($_POST['process'] != "true") {
860 // don't do a lookup
861 $this->assign("drug", $_GET['drug']);
862 return;
865 // process the lookup
866 $this->assign("drug", $_POST['drug']);
867 $list = array();
868 if (!empty($_POST['drug'])) {
869 $list = $this->RxList->get_list($_POST['drug']);
872 if (is_array($list)) {
873 $list = array_flip($list);
874 $this->assign("drug_options", $list);
875 $this->assign("drug_values", array_keys($list));
876 } else {
877 $this->assign("NO_RESULTS", "No results found for: " .$_POST['drug'] . "<br />");
880 //print_r($_POST);
881 //$this->assign("PROCESS","");
883 $_POST['process'] = "";
886 function _fax_prescription($p, $faxNum)
888 $err = "Sent fax";
889 //strip - ,(, ), and ws
890 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/", "", $faxNum);
891 //validate the number
893 if (!empty($faxNum) && is_numeric($faxNum)) {
894 //get the sendfax command and execute it
895 $cmd = $this->pconfig['sendfax'];
896 // prepend any prefix to the fax number
897 $pref=$this->pconfig['prefix'];
898 $faxNum=$pref.$faxNum;
899 if (empty($cmd)) {
900 $err .= " Send fax not set in includes/config.php";
901 } else {
902 //generate file to fax
903 $faxFile = "Failed";
904 $this->_print_prescription($p, $faxFile);
905 if (empty($faxFile)) {
906 $err .= " _print_prescription returned empty file";
909 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
910 $p->get_patient_id() . "_fax_.pdf";
911 //print "filename is $fileName";
912 touch($fileName); // php bug
913 $handle = fopen($fileName, "w");
914 if (!$handle) {
915 $err .= " Failed to open file $fileName to write fax to";
918 if (fwrite($handle, $faxFile) === false) {
919 $err .= " Failed to write data to $fileName";
922 fclose($handle);
923 $args = " -n -d $faxNum $fileName";
924 //print "command is $cmd $args<br>";
925 exec($cmd . $args);
927 } else {
928 $err = "bad fax number passed to function";
931 if ($err) {
932 $this->assign("process_result", $err);