fix to use gbl currency symbol in rx when dispensing module on (#1977)
[openemr.git] / controllers / C_Prescription.class.php
blob9919050b52ec7b9a4e4a5894d691378cde5b0ce1
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 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
111 // If quantity to dispense is not already set from a POST, set its
112 // default value.
113 if (! $this->get_template_vars('DISP_QUANTITY')) {
114 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
117 $this->default_action();
120 function list_action($id, $sort = "")
122 if (empty($id)) {
123 $this->function_argument_error();
124 exit;
127 if (!empty($sort)) {
128 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
129 } else {
130 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
133 // flag to indicate the CAMOS form is regsitered and active
134 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
136 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
139 function block_action($id, $sort = "")
141 if (empty($id)) {
142 $this->function_argument_error();
143 exit;
146 if (!empty($sort)) {
147 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
148 } else {
149 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
152 //print_r(Prescription::prescriptions_factory($id));
153 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
156 function fragment_action($id, $sort = "")
158 if (empty($id)) {
159 $this->function_argument_error();
160 exit;
163 if (!empty($sort)) {
164 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
165 } else {
166 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
169 //print_r(Prescription::prescriptions_factory($id));
170 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
173 function lookup_action()
175 $this->do_lookup();
176 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
179 function edit_action_process()
181 if ($_POST['process'] != "true") {
182 return;
185 //print_r($_POST);
187 // Stupid Smarty code treats empty values as not specified values.
188 // Since active is a checkbox, represent the unchecked state as -1.
189 if (empty($_POST['active'])) {
190 $_POST['active'] = '-1';
192 if (!empty($_POST['start_date'])) {
193 $_POST['start_date'] = DateToYYYYMMDD($_POST['start_date']);
196 $this->prescriptions[0] = new Prescription($_POST['id']);
197 parent::populate_object($this->prescriptions[0]);
198 //echo $this->prescriptions[0]->toString(true);
199 $this->prescriptions[0]->persist();
200 $_POST['process'] = "";
202 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
204 // If the "Prescribe and Dispense" button was clicked, then
205 // redisplay as in edit_action() but also replicate the fee and
206 // include a piece of javascript to call dispense().
208 if ($_POST['disp_button']) {
209 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
210 $this->assign("DISP_FEE", $_POST['disp_fee']);
211 $this->assign("ENDING_JAVASCRIPT", "dispense();");
212 $this->_state = false;
213 return $this->edit_action($this->prescriptions[0]->id);
216 // Set the AMC reporting flag (to record percentage of prescriptions that
217 // are set as e-prescriptions)
218 if (!(empty($_POST['escribe_flag']))) {
219 // add the e-prescribe flag
220 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
221 } else {
222 // remove the e-prescribe flag
223 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
226 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
227 if (!(empty($_POST['checked_formulary_flag']))) {
228 // add the e-prescribe flag
229 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
230 } else {
231 // remove the e-prescribe flag
232 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
235 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
236 if (!(empty($_POST['controlled_substance_flag']))) {
237 // add the e-prescribe flag
238 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
239 } else {
240 // remove the e-prescribe flag
241 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
244 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
245 // if ($this->prescriptions[0]->get_active() > 0) {
246 // return $this->send_action($this->prescriptions[0]->id);
247 // }
248 $this->list_action($this->prescriptions[0]->get_patient_id());
249 exit;
252 function send_action($id)
254 $_POST['process'] = "true";
255 if (empty($id)) {
256 $this->function_argument_error();
259 $rx = new Prescription($id);
260 // Populate pharmacy info if the patient has a default pharmacy.
261 // Probably the Prescription object should handle this instead, but
262 // doing it there will require more careful research and testing.
263 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
264 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
265 if ($prow['pharmacy_id']) {
266 $rx->pharmacy->set_id($prow['pharmacy_id']);
267 $rx->pharmacy->populate();
270 $this->assign("prescription", $rx);
272 $this->_state = false;
273 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
274 $this->template_mod . "_send.html");
277 function multiprintfax_header(& $pdf, $p)
279 return $this->multiprint_header($pdf, $p);
282 function multiprint_header(& $pdf, $p)
284 $this->providerid = $p->provider->id;
285 //print header
286 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'], '', '50', '', 'center', '');
287 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
288 $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 ='" .
289 add_escape_custom($p->provider->id) . "'");
290 $pdf->ezText($res['addr'], 12);
291 $my_y = $pdf->y;
292 $pdf->ezNewPage();
293 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>', 12);
294 // A client had a bad experience with a patient misusing a DEA number, so
295 // now the doctors write those in on printed prescriptions and only when
296 // necessary. If you need to change this back, then please make it a
297 // configurable option. Faxed prescriptions were not changed. -- Rod
298 // Now it is configureable. Change value in
299 // Administration->Globals->Rx
300 if ($GLOBALS['rx_enable_DEA']) {
301 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
302 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
303 } else {
304 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
308 if ($GLOBALS['rx_enable_NPI']) {
309 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
310 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
311 } else {
312 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
316 if ($GLOBALS['rx_enable_SLN']) {
317 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
318 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
319 } else {
320 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
324 $pdf->ezColumnsStop();
325 if ($my_y < $pdf->y) {
326 $pdf->ezSetY($my_y);
329 $pdf->ezText('', 10);
330 $pdf->setLineStyle(1);
331 $pdf->ezColumnsStart(array('num'=>2));
332 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
333 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>', 6);
334 $pdf->ezText($p->patient->get_name_display(), 10);
335 $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));
336 $pdf->ezText($res['addr']);
337 $my_y = $pdf->y;
338 $pdf->ezNewPage();
339 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
340 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>', 6);
341 $pdf->ezText($p->patient->date_of_birth, 10);
342 $pdf->ezText('');
343 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
344 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>', 6);
345 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT), 10);
346 $pdf->ezColumnsStop();
347 if ($my_y < $pdf->y) {
348 $pdf->ezSetY($my_y);
351 $pdf->ezText('');
352 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
353 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>', 6);
354 $pdf->ezText('', 10);
357 function multiprintcss_header($p)
359 echo("<div class='paddingdiv'>\n");
360 $this->providerid = $p->provider->id;
361 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
362 echo ("<tr>\n");
363 echo ("<td></td>\n");
364 echo ("<td>\n");
365 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
366 echo ("</td>\n");
367 echo ("</tr>\n");
368 echo ("<tr>\n");
369 echo ("<td>\n");
370 $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) . "'");
371 if (!empty($res)) {
372 $patterns = array ('/\n/','/Tel:/','/Fax:/');
373 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
374 $res = preg_replace($patterns, $replace, $res);
377 echo ('<span class="large">' . $res['addr'] . '</span>');
378 echo ("</td>\n");
379 echo ("<td>\n");
380 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
382 if ($GLOBALS['rx_enable_DEA']) {
383 if ($GLOBALS['rx_show_DEA']) {
384 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
385 } else {
386 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
390 if ($GLOBALS['rx_enable_NPI']) {
391 if ($GLOBALS['rx_show_NPI']) {
392 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
393 } else {
394 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
398 if ($GLOBALS['rx_enable_SLN']) {
399 if ($GLOBALS['rx_show_SLN']) {
400 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
401 } else {
402 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
406 echo ("</td>\n");
407 echo ("</tr>\n");
408 echo ("<tr>\n");
409 echo ("<td rowspan='2' class='bordered'>\n");
410 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
411 echo ($p->patient->get_name_display() . '<br>');
412 $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));
413 if (!empty($res)) {
414 $patterns = array ('/\n/');
415 $replace = array ('<br>');
416 $res = preg_replace($patterns, $replace, $res);
419 echo ($res['addr']);
420 echo ("</td>\n");
421 echo ("<td class='bordered'>\n");
422 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
423 echo ($p->patient->date_of_birth );
424 echo ("</td>\n");
425 echo ("</tr>\n");
426 echo ("<tr>\n");
427 echo ("<td class='bordered'>\n");
428 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
429 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
430 echo ("</td>\n");
431 echo ("</tr>\n");
432 echo ("<tr>\n");
433 echo ("<td colspan='2' class='bordered'>\n");
434 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
435 echo ("</td>\n");
436 echo ("</tr>\n");
437 echo ("</table>\n");
440 function multiprintcss_preheader()
442 // this sets styling and other header information of the multiprint css sheet
443 echo ("<html>\n");
444 echo ("<head>\n");
445 echo ("<style>\n");
446 echo ("div {\n");
447 echo (" padding: 0;\n");
448 echo (" margin: 0;\n");
449 echo ("}\n");
450 echo ("body {\n");
451 echo (" font-family: sans-serif;\n");
452 echo (" font-weight: normal;\n");
453 echo (" font-size: 10pt;\n");
454 echo (" background: white;\n");
455 echo (" color: black;\n");
456 echo ("}\n");
457 echo ("span.large {\n");
458 echo (" font-size: 12pt;\n");
459 echo ("}\n");
460 echo ("span.small {\n");
461 echo (" font-size: 6pt;\n");
462 echo ("}\n");
463 echo ("td {\n");
464 echo (" vertical-align: top;\n");
465 echo (" width: 50%;\n");
466 echo (" font-size: 10pt;\n");
467 echo (" padding-bottom: 8pt;\n");
468 echo ("}\n");
469 echo ("td.bordered {\n");
470 echo (" border-top:1pt solid black;\n");
471 echo ("}\n");
472 echo ("div.paddingdiv {\n");
473 echo (" width: 524pt;\n");
474 echo (" height: 668pt;\n");
475 echo ("}\n");
476 echo ("div.scriptdiv {\n");
477 echo (" padding-top: 12pt;\n");
478 echo (" padding-bottom: 22pt;\n");
479 echo (" padding-left: 35pt;\n");
480 echo (" border-bottom:1pt solid black;\n");
481 echo ("}\n");
482 echo ("div.signdiv {\n");
483 echo (" margin-top: 40pt;\n");
484 echo (" font-size: 12pt;\n");
485 echo ("}\n");
486 echo ("</style>\n");
488 echo ("<title>" . xl('Prescription') . "</title>\n");
489 echo ("</head>\n");
490 echo ("<body>\n");
493 function multiprintfax_footer(& $pdf)
495 return $this->multiprint_footer($pdf);
498 function multiprint_footer(& $pdf)
500 if ($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax )) {
501 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
502 if (file_exists($sigfile)) {
503 $pdf->ezText(xl('Signature') . ": ", 12);
504 // $pdf->ezImage($sigfile, "", "", "none", "left");
505 $pdf->ezImage($sigfile, "", "", "none", "center");
506 $pdf->ezText(xl('Date') . ": " . date('Y-m-d'), 12);
507 if ($this->is_print_to_fax) {
508 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
511 $addenumFile = $this->pconfig['addendum_file'];
512 if (file_exists($addenumFile)) {
513 $pdf->ezText('');
514 $f = fopen($addenumFile, "r");
515 while ($line = fgets($f, 1000)) {
516 $pdf->ezText(rtrim($line));
520 return;
524 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'), 12);
527 function multiprintcss_footer()
529 echo ("<div class='signdiv'>\n");
530 echo (xl('Signature') . ":________________________________<br>");
531 echo (xl('Date') . ": " . date('Y-m-d'));
532 echo ("</div>\n");
533 echo ("</div>\n");
536 function multiprintcss_postfooter()
538 echo("<script language='JavaScript'>\n");
539 echo("opener.top.printLogPrint(window);\n");
540 echo("</script>\n");
541 echo("</body>\n");
542 echo("</html>\n");
545 function get_prescription_body_text($p)
547 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
548 if ($p->get_form()) {
549 $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
552 $body .= "</b> <i>" .
553 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
554 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
555 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
556 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
557 if ($p->get_refills() > 0) {
558 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
559 if ($p->get_per_refill()) {
560 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
563 $body .= "</u>\n";
564 } else {
565 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
568 $note = $p->get_note();
569 if ($note != '') {
570 $body .= "\n" . text($note) . "\n";
573 return $body;
576 function multiprintfax_body(& $pdf, $p)
578 return $this->multiprint_body($pdf, $p);
581 function multiprint_body(& $pdf, $p)
583 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
584 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
585 $d = $this->get_prescription_body_text($p);
586 if ($pdf->ezText($d, 10, array(), 1)) {
587 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
588 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
589 $this->multiprint_footer($pdf);
590 $pdf->ezNewPage();
591 $this->multiprint_header($pdf, $p);
592 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
593 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
596 $my_y = $pdf->y;
597 $pdf->ezText($d, 10);
598 if ($this->pconfig['shading']) {
599 $pdf->setColor(.9, .9, .9);
600 $pdf->filledRectangle($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'], $my_y - $pdf->y);
601 $pdf->setColor(0, 0, 0);
604 $pdf->ezSetY($my_y);
605 $pdf->ezText($d, 10);
606 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
607 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
608 $pdf->ezText('');
609 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
610 $pdf->ezText('');
613 function multiprintcss_body($p)
615 $d = $this->get_prescription_body_text($p);
616 $patterns = array ('/\n/','/ /');
617 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
618 $d = preg_replace($patterns, $replace, $d);
619 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
622 function multiprintfax_action($id = "")
624 $this->is_print_to_fax=true;
625 return $this->multiprint_action($id);
628 function multiprint_action($id = "")
630 $_POST['process'] = "true";
631 if (empty($id)) {
632 $this->function_argument_error();
635 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
636 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
637 $pdf->selectFont('Helvetica');
639 // $print_header = true;
640 $on_this_page = 0;
642 //print prescriptions body
643 $this->_state = false; // Added by Rod - see Controller.class.php
644 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
645 foreach ($ids as $id) {
646 $p = new Prescription($id);
647 // if ($print_header == true) {
648 if ($on_this_page == 0) {
649 $this->multiprint_header($pdf, $p);
652 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
653 $this->multiprint_footer($pdf);
654 $pdf->ezNewPage();
655 $this->multiprint_header($pdf, $p);
656 // $print_header = false;
657 $on_this_page = 1;
660 $this->multiprint_body($pdf, $p);
663 $this->multiprint_footer($pdf);
665 $pFirstName = $p->patient->fname; //modified by epsdky for prescription title change to include patient name and ID
666 $pFName = convert_safe_file_dir_name($pFirstName);
667 $modedFileName = "Rx_{$pFName}_{$p->patient->id}.pdf";
669 $pdf->ezStream(array('Content-Disposition' => $modedFileName));
670 return;
673 function multiprintcss_action($id = "")
675 $_POST['process'] = "true";
676 if (empty($id)) {
677 $this->function_argument_error();
680 $this->multiprintcss_preheader();
682 $this->_state = false; // Added by Rod - see Controller.class.php
683 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
685 $on_this_page = 0;
686 foreach ($ids as $id) {
687 $p = new Prescription($id);
688 if ($on_this_page == 0) {
689 $this->multiprintcss_header($p);
692 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
693 $this->multiprintcss_footer();
694 $this->multiprintcss_header($p);
695 $on_this_page = 1;
698 $this->multiprintcss_body($p);
701 $this->multiprintcss_footer();
702 $this->multiprintcss_postfooter();
703 return;
706 function send_action_process($id)
708 $dummy = ""; // Added by Rod to avoid run-time warnings
709 if ($_POST['process'] != "true") {
710 return;
713 if (empty($id)) {
714 $this->function_argument_error();
717 $p = new Prescription($id);
718 switch ($_POST['submit']) {
719 case (xl("Print")." (".xl("PDF").")"):
720 // The following statement added by Rod.
721 // Looking at Controller.class.php, it appears that _state is set to false
722 // to indicate that no further HTML is to be generated.
723 $this->_state = false; // Added by Rod - see Controller.class.php
724 return $this->_print_prescription($p, $dummy);
725 break;
726 case (xl("Print")." (".xl("HTML").")"):
727 $this->_state = false;
728 return $this->_print_prescription_css($p, $dummy);
729 break;
730 case xl("Print To Fax"):
731 $this->_state = false;
732 $this->is_print_to_fax = true;
733 return $this->_print_prescription($p, $dummy);
734 break;
735 case xl("Email"):
736 return $this->_email_prescription($p, $_POST['email_to']);
737 break;
738 case xl("Fax"):
739 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
740 //$this->assign("process_result","No fax server is currently setup.");
741 return $this->_fax_prescription($p, $_POST['fax_to']);
742 break;
743 case xl("Auto Send"):
744 $pharmacy_id = $_POST['pharmacy_id'];
745 //echo "auto sending to : " . $_POST['pharmacy_id'];
746 $phar = new Pharmacy($_POST['pharmacy_id']);
747 //print_r($phar);
748 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
749 return $this->_print_prescription($p, $dummy);
750 } elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
751 $email = $phar->get_email();
752 if (!empty($email)) {
753 return $this->_email_prescription($p, $phar->get_email());
756 //else print it
757 } elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
758 $faxNum= $phar->get_fax();
759 if (!empty($faxNum)) {
760 return $this->_fax_prescription($p, $faxNum);
763 // return $this->assign("process_result","No fax server is currently setup.");
764 // else default is printing,
765 } else {
766 //the pharmacy has no default or default is print
767 return $this->_print_prescription($p, $dummy);
769 break;
772 return;
775 function _print_prescription($p, & $toFile)
777 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
778 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
780 $pdf->selectFont('Helvetica');
782 // Signature images are to be used only when faxing.
783 if (!empty($toFile)) {
784 $this->is_faxing = true;
787 $this->multiprint_header($pdf, $p);
788 $this->multiprint_body($pdf, $p);
789 $this->multiprint_footer($pdf);
791 if (!empty($toFile)) {
792 $toFile = $pdf->ezOutput();
793 } else {
794 $pdf->ezStream();
795 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
798 return;
801 function _print_prescription_css($p, & $toFile)
804 $this->multiprintcss_preheader();
805 $this->multiprintcss_header($p);
806 $this->multiprintcss_body($p);
807 $this->multiprintcss_footer();
808 $this->multiprintcss_postfooter();
811 function _print_prescription_old($p, & $toFile)
813 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
814 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
815 $pdf->selectFont('Helvetica');
816 if (!empty($this->pconfig['logo'])) {
817 $pdf->ezImage($this->pconfig['logo'], "", "", "none", "left");
820 $pdf->ezText($p->get_prescription_display(), 10);
821 if ($this->pconfig['use_signature']) {
822 $pdf->ezImage($this->pconfig['signature'], "", "", "none", "left");
823 } else {
824 $pdf->ezText("\n\n\n\nSignature:________________________________", 10);
827 if (!empty($toFile)) {
828 $toFile = $pdf->ezOutput();
829 } else {
830 $pdf->ezStream();
831 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
834 return;
837 function _email_prescription($p, $email)
839 if (empty($email)) {
840 $this->assign("process_result", "Email could not be sent, the address supplied: '$email' was empty or invalid.");
841 return;
844 $mail = new PHPMailer();
845 //this is a temporary config item until the rest of the per practice billing settings make their way in
846 $mail->From = $GLOBALS['practice_return_email_path'];
847 $mail->FromName = $p->provider->get_name_display();
848 $mail->isMail();
849 $mail->Host = "localhost";
850 $mail->Mailer = "mail";
851 $text_body = $p->get_prescription_display();
852 $mail->Body = $text_body;
853 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
854 $mail->AddAddress($email);
855 if ($mail->Send()) {
856 $this->assign("process_result", "Email was successfully sent to: " . $email);
857 return;
858 } else {
859 $this->assign("process_result", "There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
860 return;
864 function do_lookup()
866 if ($_POST['process'] != "true") {
867 // don't do a lookup
868 $this->assign("drug", $_GET['drug']);
869 return;
872 // process the lookup
873 $this->assign("drug", $_POST['drug']);
874 $list = array();
875 if (!empty($_POST['drug'])) {
876 $list = $this->RxList->get_list($_POST['drug']);
879 if (is_array($list)) {
880 $list = array_flip($list);
881 $this->assign("drug_options", $list);
882 $this->assign("drug_values", array_keys($list));
883 } else {
884 $this->assign("NO_RESULTS", "No results found for: " .$_POST['drug'] . "<br />");
887 //print_r($_POST);
888 //$this->assign("PROCESS","");
890 $_POST['process'] = "";
893 function _fax_prescription($p, $faxNum)
895 $err = "Sent fax";
896 //strip - ,(, ), and ws
897 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/", "", $faxNum);
898 //validate the number
900 if (!empty($faxNum) && is_numeric($faxNum)) {
901 //get the sendfax command and execute it
902 $cmd = $this->pconfig['sendfax'];
903 // prepend any prefix to the fax number
904 $pref=$this->pconfig['prefix'];
905 $faxNum=$pref.$faxNum;
906 if (empty($cmd)) {
907 $err .= " Send fax not set in includes/config.php";
908 } else {
909 //generate file to fax
910 $faxFile = "Failed";
911 $this->_print_prescription($p, $faxFile);
912 if (empty($faxFile)) {
913 $err .= " _print_prescription returned empty file";
916 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
917 $p->get_patient_id() . "_fax_.pdf";
918 //print "filename is $fileName";
919 touch($fileName); // php bug
920 $handle = fopen($fileName, "w");
921 if (!$handle) {
922 $err .= " Failed to open file $fileName to write fax to";
925 if (fwrite($handle, $faxFile) === false) {
926 $err .= " Failed to write data to $fileName";
929 fclose($handle);
930 $args = " -n -d $faxNum $fileName";
931 //print "command is $cmd $args<br>";
932 exec($cmd . $args);
934 } else {
935 $err = "bad fax number passed to function";
938 if ($err) {
939 $this->assign("process_result", $err);