Merge pull request #1998 from bradymiller/api_2
[openemr.git] / controllers / C_Prescription.class.php
blob983691d108f40b7acb77312f5cac9f63dd190a7d
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 // Assign the CSRF_TOKEN_FORM
137 $this->assign("CSRF_TOKEN_FORM", collectCsrfToken());
139 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
142 function block_action($id, $sort = "")
144 if (empty($id)) {
145 $this->function_argument_error();
146 exit;
149 if (!empty($sort)) {
150 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
151 } else {
152 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
155 //print_r(Prescription::prescriptions_factory($id));
156 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
159 function fragment_action($id, $sort = "")
161 if (empty($id)) {
162 $this->function_argument_error();
163 exit;
166 if (!empty($sort)) {
167 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
168 } else {
169 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
172 //print_r(Prescription::prescriptions_factory($id));
173 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
176 function lookup_action()
178 $this->do_lookup();
179 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
182 function edit_action_process()
184 if ($_POST['process'] != "true") {
185 return;
188 //print_r($_POST);
190 // Stupid Smarty code treats empty values as not specified values.
191 // Since active is a checkbox, represent the unchecked state as -1.
192 if (empty($_POST['active'])) {
193 $_POST['active'] = '-1';
195 if (!empty($_POST['start_date'])) {
196 $_POST['start_date'] = DateToYYYYMMDD($_POST['start_date']);
199 $this->prescriptions[0] = new Prescription($_POST['id']);
200 parent::populate_object($this->prescriptions[0]);
201 //echo $this->prescriptions[0]->toString(true);
202 $this->prescriptions[0]->persist();
203 $_POST['process'] = "";
205 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
207 // If the "Prescribe and Dispense" button was clicked, then
208 // redisplay as in edit_action() but also replicate the fee and
209 // include a piece of javascript to call dispense().
211 if ($_POST['disp_button']) {
212 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
213 $this->assign("DISP_FEE", $_POST['disp_fee']);
214 $this->assign("ENDING_JAVASCRIPT", "dispense();");
215 $this->_state = false;
216 return $this->edit_action($this->prescriptions[0]->id);
219 // Set the AMC reporting flag (to record percentage of prescriptions that
220 // are set as e-prescriptions)
221 if (!(empty($_POST['escribe_flag']))) {
222 // add the e-prescribe flag
223 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
224 } else {
225 // remove the e-prescribe flag
226 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
229 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
230 if (!(empty($_POST['checked_formulary_flag']))) {
231 // add the e-prescribe flag
232 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
233 } else {
234 // remove the e-prescribe flag
235 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
238 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
239 if (!(empty($_POST['controlled_substance_flag']))) {
240 // add the e-prescribe flag
241 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
242 } else {
243 // remove the e-prescribe flag
244 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
247 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
248 // if ($this->prescriptions[0]->get_active() > 0) {
249 // return $this->send_action($this->prescriptions[0]->id);
250 // }
251 $this->list_action($this->prescriptions[0]->get_patient_id());
252 exit;
255 function send_action($id)
257 $_POST['process'] = "true";
258 if (empty($id)) {
259 $this->function_argument_error();
262 $rx = new Prescription($id);
263 // Populate pharmacy info if the patient has a default pharmacy.
264 // Probably the Prescription object should handle this instead, but
265 // doing it there will require more careful research and testing.
266 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
267 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
268 if ($prow['pharmacy_id']) {
269 $rx->pharmacy->set_id($prow['pharmacy_id']);
270 $rx->pharmacy->populate();
273 $this->assign("prescription", $rx);
275 $this->_state = false;
276 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
277 $this->template_mod . "_send.html");
280 function multiprintfax_header(& $pdf, $p)
282 return $this->multiprint_header($pdf, $p);
285 function multiprint_header(& $pdf, $p)
287 $this->providerid = $p->provider->id;
288 //print header
289 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'], '', '50', '', 'center', '');
290 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
291 $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 ='" .
292 add_escape_custom($p->provider->id) . "'");
293 $pdf->ezText($res['addr'], 12);
294 $my_y = $pdf->y;
295 $pdf->ezNewPage();
296 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>', 12);
297 // A client had a bad experience with a patient misusing a DEA number, so
298 // now the doctors write those in on printed prescriptions and only when
299 // necessary. If you need to change this back, then please make it a
300 // configurable option. Faxed prescriptions were not changed. -- Rod
301 // Now it is configureable. Change value in
302 // Administration->Globals->Rx
303 if ($GLOBALS['rx_enable_DEA']) {
304 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
305 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
306 } else {
307 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
311 if ($GLOBALS['rx_enable_NPI']) {
312 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
313 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
314 } else {
315 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
319 if ($GLOBALS['rx_enable_SLN']) {
320 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
321 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
322 } else {
323 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
327 $pdf->ezColumnsStop();
328 if ($my_y < $pdf->y) {
329 $pdf->ezSetY($my_y);
332 $pdf->ezText('', 10);
333 $pdf->setLineStyle(1);
334 $pdf->ezColumnsStart(array('num'=>2));
335 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
336 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>', 6);
337 $pdf->ezText($p->patient->get_name_display(), 10);
338 $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));
339 $pdf->ezText($res['addr']);
340 $my_y = $pdf->y;
341 $pdf->ezNewPage();
342 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
343 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>', 6);
344 $pdf->ezText($p->patient->date_of_birth, 10);
345 $pdf->ezText('');
346 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
347 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>', 6);
348 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT), 10);
349 $pdf->ezColumnsStop();
350 if ($my_y < $pdf->y) {
351 $pdf->ezSetY($my_y);
354 $pdf->ezText('');
355 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
356 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>', 6);
357 $pdf->ezText('', 10);
360 function multiprintcss_header($p)
362 echo("<div class='paddingdiv'>\n");
363 $this->providerid = $p->provider->id;
364 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
365 echo ("<tr>\n");
366 echo ("<td></td>\n");
367 echo ("<td>\n");
368 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
369 echo ("</td>\n");
370 echo ("</tr>\n");
371 echo ("<tr>\n");
372 echo ("<td>\n");
373 $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) . "'");
374 if (!empty($res)) {
375 $patterns = array ('/\n/','/Tel:/','/Fax:/');
376 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
377 $res = preg_replace($patterns, $replace, $res);
380 echo ('<span class="large">' . $res['addr'] . '</span>');
381 echo ("</td>\n");
382 echo ("<td>\n");
383 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
385 if ($GLOBALS['rx_enable_DEA']) {
386 if ($GLOBALS['rx_show_DEA']) {
387 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
388 } else {
389 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
393 if ($GLOBALS['rx_enable_NPI']) {
394 if ($GLOBALS['rx_show_NPI']) {
395 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
396 } else {
397 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
401 if ($GLOBALS['rx_enable_SLN']) {
402 if ($GLOBALS['rx_show_SLN']) {
403 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
404 } else {
405 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
409 echo ("</td>\n");
410 echo ("</tr>\n");
411 echo ("<tr>\n");
412 echo ("<td rowspan='2' class='bordered'>\n");
413 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
414 echo ($p->patient->get_name_display() . '<br>');
415 $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));
416 if (!empty($res)) {
417 $patterns = array ('/\n/');
418 $replace = array ('<br>');
419 $res = preg_replace($patterns, $replace, $res);
422 echo ($res['addr']);
423 echo ("</td>\n");
424 echo ("<td class='bordered'>\n");
425 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
426 echo ($p->patient->date_of_birth );
427 echo ("</td>\n");
428 echo ("</tr>\n");
429 echo ("<tr>\n");
430 echo ("<td class='bordered'>\n");
431 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
432 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
433 echo ("</td>\n");
434 echo ("</tr>\n");
435 echo ("<tr>\n");
436 echo ("<td colspan='2' class='bordered'>\n");
437 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
438 echo ("</td>\n");
439 echo ("</tr>\n");
440 echo ("</table>\n");
443 function multiprintcss_preheader()
445 // this sets styling and other header information of the multiprint css sheet
446 echo ("<html>\n");
447 echo ("<head>\n");
448 echo ("<style>\n");
449 echo ("div {\n");
450 echo (" padding: 0;\n");
451 echo (" margin: 0;\n");
452 echo ("}\n");
453 echo ("body {\n");
454 echo (" font-family: sans-serif;\n");
455 echo (" font-weight: normal;\n");
456 echo (" font-size: 10pt;\n");
457 echo (" background: white;\n");
458 echo (" color: black;\n");
459 echo ("}\n");
460 echo ("span.large {\n");
461 echo (" font-size: 12pt;\n");
462 echo ("}\n");
463 echo ("span.small {\n");
464 echo (" font-size: 6pt;\n");
465 echo ("}\n");
466 echo ("td {\n");
467 echo (" vertical-align: top;\n");
468 echo (" width: 50%;\n");
469 echo (" font-size: 10pt;\n");
470 echo (" padding-bottom: 8pt;\n");
471 echo ("}\n");
472 echo ("td.bordered {\n");
473 echo (" border-top:1pt solid black;\n");
474 echo ("}\n");
475 echo ("div.paddingdiv {\n");
476 echo (" width: 524pt;\n");
477 echo (" height: 668pt;\n");
478 echo ("}\n");
479 echo ("div.scriptdiv {\n");
480 echo (" padding-top: 12pt;\n");
481 echo (" padding-bottom: 22pt;\n");
482 echo (" padding-left: 35pt;\n");
483 echo (" border-bottom:1pt solid black;\n");
484 echo ("}\n");
485 echo ("div.signdiv {\n");
486 echo (" margin-top: 40pt;\n");
487 echo (" font-size: 12pt;\n");
488 echo ("}\n");
489 echo ("</style>\n");
491 echo ("<title>" . xl('Prescription') . "</title>\n");
492 echo ("</head>\n");
493 echo ("<body>\n");
496 function multiprintfax_footer(& $pdf)
498 return $this->multiprint_footer($pdf);
501 function multiprint_footer(& $pdf)
503 if ($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax )) {
504 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
505 if (file_exists($sigfile)) {
506 $pdf->ezText(xl('Signature') . ": ", 12);
507 // $pdf->ezImage($sigfile, "", "", "none", "left");
508 $pdf->ezImage($sigfile, "", "", "none", "center");
509 $pdf->ezText(xl('Date') . ": " . date('Y-m-d'), 12);
510 if ($this->is_print_to_fax) {
511 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
514 $addenumFile = $this->pconfig['addendum_file'];
515 if (file_exists($addenumFile)) {
516 $pdf->ezText('');
517 $f = fopen($addenumFile, "r");
518 while ($line = fgets($f, 1000)) {
519 $pdf->ezText(rtrim($line));
523 return;
527 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'), 12);
530 function multiprintcss_footer()
532 echo ("<div class='signdiv'>\n");
533 echo (xl('Signature') . ":________________________________<br>");
534 echo (xl('Date') . ": " . date('Y-m-d'));
535 echo ("</div>\n");
536 echo ("</div>\n");
539 function multiprintcss_postfooter()
541 echo("<script language='JavaScript'>\n");
542 echo("opener.top.printLogPrint(window);\n");
543 echo("</script>\n");
544 echo("</body>\n");
545 echo("</html>\n");
548 function get_prescription_body_text($p)
550 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
551 if ($p->get_form()) {
552 $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
555 $body .= "</b> <i>" .
556 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
557 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
558 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
559 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
560 if ($p->get_refills() > 0) {
561 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
562 if ($p->get_per_refill()) {
563 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
566 $body .= "</u>\n";
567 } else {
568 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
571 $note = $p->get_note();
572 if ($note != '') {
573 $body .= "\n" . text($note) . "\n";
576 return $body;
579 function multiprintfax_body(& $pdf, $p)
581 return $this->multiprint_body($pdf, $p);
584 function multiprint_body(& $pdf, $p)
586 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
587 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
588 $d = $this->get_prescription_body_text($p);
589 if ($pdf->ezText($d, 10, array(), 1)) {
590 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
591 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
592 $this->multiprint_footer($pdf);
593 $pdf->ezNewPage();
594 $this->multiprint_header($pdf, $p);
595 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
596 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
599 $my_y = $pdf->y;
600 $pdf->ezText($d, 10);
601 if ($this->pconfig['shading']) {
602 $pdf->setColor(.9, .9, .9);
603 $pdf->filledRectangle($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'], $my_y - $pdf->y);
604 $pdf->setColor(0, 0, 0);
607 $pdf->ezSetY($my_y);
608 $pdf->ezText($d, 10);
609 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
610 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
611 $pdf->ezText('');
612 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
613 $pdf->ezText('');
616 function multiprintcss_body($p)
618 $d = $this->get_prescription_body_text($p);
619 $patterns = array ('/\n/','/ /');
620 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
621 $d = preg_replace($patterns, $replace, $d);
622 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
625 function multiprintfax_action($id = "")
627 $this->is_print_to_fax=true;
628 return $this->multiprint_action($id);
631 function multiprint_action($id = "")
633 $_POST['process'] = "true";
634 if (empty($id)) {
635 $this->function_argument_error();
638 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
639 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
640 $pdf->selectFont('Helvetica');
642 // $print_header = true;
643 $on_this_page = 0;
645 //print prescriptions body
646 $this->_state = false; // Added by Rod - see Controller.class.php
647 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
648 foreach ($ids as $id) {
649 $p = new Prescription($id);
650 // if ($print_header == true) {
651 if ($on_this_page == 0) {
652 $this->multiprint_header($pdf, $p);
655 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
656 $this->multiprint_footer($pdf);
657 $pdf->ezNewPage();
658 $this->multiprint_header($pdf, $p);
659 // $print_header = false;
660 $on_this_page = 1;
663 $this->multiprint_body($pdf, $p);
666 $this->multiprint_footer($pdf);
668 $pFirstName = $p->patient->fname; //modified by epsdky for prescription title change to include patient name and ID
669 $pFName = convert_safe_file_dir_name($pFirstName);
670 $modedFileName = "Rx_{$pFName}_{$p->patient->id}.pdf";
672 $pdf->ezStream(array('Content-Disposition' => $modedFileName));
673 return;
676 function multiprintcss_action($id = "")
678 $_POST['process'] = "true";
679 if (empty($id)) {
680 $this->function_argument_error();
683 $this->multiprintcss_preheader();
685 $this->_state = false; // Added by Rod - see Controller.class.php
686 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
688 $on_this_page = 0;
689 foreach ($ids as $id) {
690 $p = new Prescription($id);
691 if ($on_this_page == 0) {
692 $this->multiprintcss_header($p);
695 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
696 $this->multiprintcss_footer();
697 $this->multiprintcss_header($p);
698 $on_this_page = 1;
701 $this->multiprintcss_body($p);
704 $this->multiprintcss_footer();
705 $this->multiprintcss_postfooter();
706 return;
709 function send_action_process($id)
711 $dummy = ""; // Added by Rod to avoid run-time warnings
712 if ($_POST['process'] != "true") {
713 return;
716 if (empty($id)) {
717 $this->function_argument_error();
720 $p = new Prescription($id);
721 switch ($_POST['submit']) {
722 case (xl("Print")." (".xl("PDF").")"):
723 // The following statement added by Rod.
724 // Looking at Controller.class.php, it appears that _state is set to false
725 // to indicate that no further HTML is to be generated.
726 $this->_state = false; // Added by Rod - see Controller.class.php
727 return $this->_print_prescription($p, $dummy);
728 break;
729 case (xl("Print")." (".xl("HTML").")"):
730 $this->_state = false;
731 return $this->_print_prescription_css($p, $dummy);
732 break;
733 case xl("Print To Fax"):
734 $this->_state = false;
735 $this->is_print_to_fax = true;
736 return $this->_print_prescription($p, $dummy);
737 break;
738 case xl("Email"):
739 return $this->_email_prescription($p, $_POST['email_to']);
740 break;
741 case xl("Fax"):
742 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
743 //$this->assign("process_result","No fax server is currently setup.");
744 return $this->_fax_prescription($p, $_POST['fax_to']);
745 break;
746 case xl("Auto Send"):
747 $pharmacy_id = $_POST['pharmacy_id'];
748 //echo "auto sending to : " . $_POST['pharmacy_id'];
749 $phar = new Pharmacy($_POST['pharmacy_id']);
750 //print_r($phar);
751 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
752 return $this->_print_prescription($p, $dummy);
753 } elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
754 $email = $phar->get_email();
755 if (!empty($email)) {
756 return $this->_email_prescription($p, $phar->get_email());
759 //else print it
760 } elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
761 $faxNum= $phar->get_fax();
762 if (!empty($faxNum)) {
763 return $this->_fax_prescription($p, $faxNum);
766 // return $this->assign("process_result","No fax server is currently setup.");
767 // else default is printing,
768 } else {
769 //the pharmacy has no default or default is print
770 return $this->_print_prescription($p, $dummy);
772 break;
775 return;
778 function _print_prescription($p, & $toFile)
780 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
781 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
783 $pdf->selectFont('Helvetica');
785 // Signature images are to be used only when faxing.
786 if (!empty($toFile)) {
787 $this->is_faxing = true;
790 $this->multiprint_header($pdf, $p);
791 $this->multiprint_body($pdf, $p);
792 $this->multiprint_footer($pdf);
794 if (!empty($toFile)) {
795 $toFile = $pdf->ezOutput();
796 } else {
797 $pdf->ezStream();
798 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
801 return;
804 function _print_prescription_css($p, & $toFile)
807 $this->multiprintcss_preheader();
808 $this->multiprintcss_header($p);
809 $this->multiprintcss_body($p);
810 $this->multiprintcss_footer();
811 $this->multiprintcss_postfooter();
814 function _print_prescription_old($p, & $toFile)
816 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
817 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
818 $pdf->selectFont('Helvetica');
819 if (!empty($this->pconfig['logo'])) {
820 $pdf->ezImage($this->pconfig['logo'], "", "", "none", "left");
823 $pdf->ezText($p->get_prescription_display(), 10);
824 if ($this->pconfig['use_signature']) {
825 $pdf->ezImage($this->pconfig['signature'], "", "", "none", "left");
826 } else {
827 $pdf->ezText("\n\n\n\nSignature:________________________________", 10);
830 if (!empty($toFile)) {
831 $toFile = $pdf->ezOutput();
832 } else {
833 $pdf->ezStream();
834 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
837 return;
840 function _email_prescription($p, $email)
842 if (empty($email)) {
843 $this->assign("process_result", "Email could not be sent, the address supplied: '$email' was empty or invalid.");
844 return;
847 $mail = new PHPMailer();
848 //this is a temporary config item until the rest of the per practice billing settings make their way in
849 $mail->From = $GLOBALS['practice_return_email_path'];
850 $mail->FromName = $p->provider->get_name_display();
851 $mail->isMail();
852 $mail->Host = "localhost";
853 $mail->Mailer = "mail";
854 $text_body = $p->get_prescription_display();
855 $mail->Body = $text_body;
856 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
857 $mail->AddAddress($email);
858 if ($mail->Send()) {
859 $this->assign("process_result", "Email was successfully sent to: " . $email);
860 return;
861 } else {
862 $this->assign("process_result", "There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
863 return;
867 function do_lookup()
869 if ($_POST['process'] != "true") {
870 // don't do a lookup
871 $this->assign("drug", $_GET['drug']);
872 return;
875 // process the lookup
876 $this->assign("drug", $_POST['drug']);
877 $list = array();
878 if (!empty($_POST['drug'])) {
879 $list = $this->RxList->get_list($_POST['drug']);
882 if (is_array($list)) {
883 $list = array_flip($list);
884 $this->assign("drug_options", $list);
885 $this->assign("drug_values", array_keys($list));
886 } else {
887 $this->assign("NO_RESULTS", "No results found for: " .$_POST['drug'] . "<br />");
890 //print_r($_POST);
891 //$this->assign("PROCESS","");
893 $_POST['process'] = "";
896 function _fax_prescription($p, $faxNum)
898 $err = "Sent fax";
899 //strip - ,(, ), and ws
900 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/", "", $faxNum);
901 //validate the number
903 if (!empty($faxNum) && is_numeric($faxNum)) {
904 //get the sendfax command and execute it
905 $cmd = $this->pconfig['sendfax'];
906 // prepend any prefix to the fax number
907 $pref=$this->pconfig['prefix'];
908 $faxNum=$pref.$faxNum;
909 if (empty($cmd)) {
910 $err .= " Send fax not set in includes/config.php";
911 } else {
912 //generate file to fax
913 $faxFile = "Failed";
914 $this->_print_prescription($p, $faxFile);
915 if (empty($faxFile)) {
916 $err .= " _print_prescription returned empty file";
919 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
920 $p->get_patient_id() . "_fax_.pdf";
921 //print "filename is $fileName";
922 touch($fileName); // php bug
923 $handle = fopen($fileName, "w");
924 if (!$handle) {
925 $err .= " Failed to open file $fileName to write fax to";
928 if (fwrite($handle, $faxFile) === false) {
929 $err .= " Failed to write data to $fileName";
932 fclose($handle);
933 $args = " -n -d $faxNum $fileName";
934 //print "command is $cmd $args<br>";
935 exec($cmd . $args);
937 } else {
938 $err = "bad fax number passed to function";
941 if ($err) {
942 $this->assign("process_result", $err);