Followup commit to complete the renaming and replacement of pos_checkout.php.
[openemr.git] / controllers / C_Prescription.class.php
blobe91e44dc5d1cc76b718b44b8824391cc51e8a284
1 <?php
3 /**
4 * C_Prescription class
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Roberto Vasquez <robertogagliotta@gmail.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @author Sherwin Gaddis <sherwingaddis@gmail.com>
11 * @copyright Copyright (c) 2015 Roberto Vasquez <robertogagliotta@gmail.com>
12 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
13 * @copyright Copyright (c) 2018 Sherwin Gaddis <sherwingaddis@gmail.com>
14 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
17 require_once($GLOBALS['fileroot'] . "/library/registry.inc");
18 require_once($GLOBALS['fileroot'] . "/library/amc.php");
20 use OpenEMR\Common\Csrf\CsrfUtils;
21 use OpenEMR\Common\Http\oeHttp;
22 use PHPMailer\PHPMailer\PHPMailer;
24 class C_Prescription extends Controller
27 var $template_mod;
28 var $pconfig;
29 var $providerid = 0;
30 var $is_faxing = false;
31 var $is_print_to_fax = false;
33 function __construct($template_mod = "general")
35 parent::__construct();
37 $this->template_mod = $template_mod;
38 $this->assign("FORM_ACTION", $GLOBALS['webroot'] . "/controller.php?" . attr($_SERVER['QUERY_STRING']));
39 $this->assign("TOP_ACTION", $GLOBALS['webroot'] . "/controller.php?" . "prescription" . "&");
40 $this->assign("STYLE", $GLOBALS['style']);
41 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
42 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
43 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
44 $this->RxList = new RxList();
46 // Assign the CSRF_TOKEN_FORM
47 $this->assign("CSRF_TOKEN_FORM", CsrfUtils::collectCsrfToken());
49 if ($GLOBALS['inhouse_pharmacy']) {
50 // Make an array of drug IDs and selectors for the template.
51 $drug_array_values = array(0);
52 $drug_array_output = array("-- " . xl('or select from inventory') . " --");
53 $drug_attributes = '';
55 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
57 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
58 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
59 "t.period, t.quantity, t.refills, d.drug_code " .
60 "FROM drug_templates AS t, drugs AS d WHERE " .
61 "d.drug_id = t.drug_id ORDER BY t.selector");
63 while ($row = sqlFetchArray($res)) {
64 $tmp_output = $row['selector'];
65 if ($row['ndc_number']) {
66 $tmp_output .= ' [' . $row['ndc_number'] . ']';
69 $drug_array_values[] = $row['drug_id'];
70 $drug_array_output[] = $tmp_output;
71 if ($drug_attributes) {
72 $drug_attributes .= ',';
75 $drug_attributes .= "[" .
76 js_escape($row['name']) . "," . // 0
77 js_escape($row['form']) . "," . // 1
78 js_escape($row['dosage']) . "," . // 2
79 js_escape($row['size']) . "," . // 3
80 js_escape($row['unit']) . "," . // 4
81 js_escape($row['route']) . "," . // 5
82 js_escape($row['period']) . "," . // 6
83 js_escape($row['substitute']) . "," . // 7
84 js_escape($row['quantity']) . "," . // 8
85 js_escape($row['refills']) . "," . // 9
86 js_escape($row['quantity']) . "," . // 10 quantity per_refill
87 js_escape($row['drug_code']) . "]"; // 11 rxnorm drug code
90 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
91 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
92 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
96 function default_action()
98 $this->assign("prescription", $this->prescriptions[0]);
99 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
102 function edit_action($id = "", $patient_id = "", $p_obj = null)
105 if ($p_obj != null && get_class($p_obj) == "prescription") {
106 $this->prescriptions[0] = $p_obj;
107 } elseif (empty($this->prescriptions[0]) || !is_object($this->prescriptions[0]) || (get_class($this->prescriptions[0]) != "prescription")) {
108 $this->prescriptions[0] = new Prescription($id);
111 if (!empty($patient_id)) {
112 $this->prescriptions[0]->set_patient_id($patient_id);
115 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
117 // If quantity to dispense is not already set from a POST, set its
118 // default value.
119 if (! $this->get_template_vars('DISP_QUANTITY')) {
120 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
123 $this->default_action();
126 function list_action($id, $sort = "")
128 if (empty($id)) {
129 $this->function_argument_error();
130 exit;
133 if (!empty($sort)) {
134 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
135 } else {
136 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
139 // Collect interactions if the global is turned on
140 if ($GLOBALS['rx_show_drug_drug']) {
141 $interaction = "";
142 // Ensure RxNorm installed
143 $rxn = sqlQuery("SELECT table_name FROM information_schema.tables WHERE table_name = 'RXNCONSO' OR table_name = 'rxconso'");
144 if ($rxn == false) {
145 $interaction = xlt("Could not find RxNorm Table! Please install.");
146 } elseif ($rxn == true) {
147 // Grab medication list from prescriptions list and load into array
148 $pid = $GLOBALS['pid'];
149 $medList = sqlStatement("SELECT drug FROM prescriptions WHERE active = 1 AND patient_id = ?", array($pid));
150 $nameList = array();
151 while ($name = sqlFetchArray($medList)) {
152 $drug = explode(" ", $name['drug']);
153 $rXn = sqlQuery("SELECT `rxcui` FROM `" . mitigateSqlTableUpperCase('RXNCONSO') . "` WHERE `str` LIKE ?", array("%" . $drug[0] . "%"));
154 $nameList[] = $rXn['rxcui'];
156 if (count($nameList) < 2) {
157 $interaction = xlt("Need more than one drug.");
158 } else {
159 // If there are drugs to compare, collect the data
160 // (array_filter removes empty items)
161 $rxcui_list = implode("+", array_filter($nameList));
162 // Unable to urlencode the $rxcui, since this breaks the + items on call to rxnav.nlm.nih.gov; so need to include it in the path
163 $response = oeHttp::get('https://rxnav.nlm.nih.gov/REST/interaction/list.json?rxcuis=' . $rxcui_list);
164 $data = $response->body();
165 $json = json_decode($data, true);
166 if (!empty($json['fullInteractionTypeGroup'][0]['fullInteractionType'])) {
167 foreach ($json['fullInteractionTypeGroup'][0]['fullInteractionType'] as $item) {
168 $interaction .= '<div class="alert alert-danger">';
169 $interaction .= xlt('Comment') . ":" . text($item['comment']) . "<br />";
170 $interaction .= xlt('Drug1 Name{{Drug1 Interaction}}') . ":" . text($item['minConcept'][0]['name']) . "<br />";
171 $interaction .= xlt('Drug2 Name{{Drug2 Interaction}}') . ":" . text($item['minConcept'][1]['name']) . "<br />";
172 $interaction .= xlt('Severity') . ":" . text($item['interactionPair'][0]['severity']) . "<br />";
173 $interaction .= xlt('Description') . ":" . text($item['interactionPair'][0]['description']);
174 $interaction .= '</div>';
176 } else {
177 $interaction = xlt('No interactions found');
181 $this->assign("INTERACTION", $interaction);
184 // flag to indicate the CAMOS form is regsitered and active
185 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
187 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
190 function block_action($id, $sort = "")
192 if (empty($id)) {
193 $this->function_argument_error();
194 exit;
197 if (!empty($sort)) {
198 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
199 } else {
200 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
203 //print_r(Prescription::prescriptions_factory($id));
204 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
207 function fragment_action($id, $sort = "")
209 if (empty($id)) {
210 $this->function_argument_error();
211 exit;
214 if (!empty($sort)) {
215 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
216 } else {
217 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
220 //print_r(Prescription::prescriptions_factory($id));
221 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
224 function lookup_action()
226 $this->do_lookup();
227 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
230 function edit_action_process()
232 if ($_POST['process'] != "true") {
233 return;
236 //print_r($_POST);
238 // Stupid Smarty code treats empty values as not specified values.
239 // Since active is a checkbox, represent the unchecked state as -1.
240 if (empty($_POST['active'])) {
241 $_POST['active'] = '-1';
243 if (!empty($_POST['start_date'])) {
244 $_POST['start_date'] = DateToYYYYMMDD($_POST['start_date']);
247 $this->prescriptions[0] = new Prescription($_POST['id']);
248 parent::populate_object($this->prescriptions[0]);
249 //echo $this->prescriptions[0]->toString(true);
250 $this->prescriptions[0]->persist();
251 $_POST['process'] = "";
253 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
255 // If the "Prescribe and Dispense" button was clicked, then
256 // redisplay as in edit_action() but also replicate the fee and
257 // include a piece of javascript to call dispense().
259 if (!empty($_POST['disp_button'])) {
260 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
261 $this->assign("DISP_FEE", $_POST['disp_fee']);
262 $this->assign("ENDING_JAVASCRIPT", "dispense();");
263 $this->_state = false;
264 return $this->edit_action($this->prescriptions[0]->id);
267 // Set the AMC reporting flag (to record percentage of prescriptions that
268 // are set as e-prescriptions)
269 if (!(empty($_POST['escribe_flag']))) {
270 // add the e-prescribe flag
271 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
272 } else {
273 // remove the e-prescribe flag
274 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
277 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
278 if (!(empty($_POST['checked_formulary_flag']))) {
279 // add the e-prescribe flag
280 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
281 } else {
282 // remove the e-prescribe flag
283 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
286 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
287 if (!(empty($_POST['controlled_substance_flag']))) {
288 // add the e-prescribe flag
289 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
290 } else {
291 // remove the e-prescribe flag
292 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
295 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
296 // if ($this->prescriptions[0]->get_active() > 0) {
297 // return $this->send_action($this->prescriptions[0]->id);
298 // }
299 $this->list_action($this->prescriptions[0]->get_patient_id());
300 exit;
303 function send_action($id)
305 $_POST['process'] = "true";
306 if (empty($id)) {
307 $this->function_argument_error();
310 $rx = new Prescription($id);
311 // Populate pharmacy info if the patient has a default pharmacy.
312 // Probably the Prescription object should handle this instead, but
313 // doing it there will require more careful research and testing.
314 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
315 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
316 if ($prow['pharmacy_id']) {
317 $rx->pharmacy->set_id($prow['pharmacy_id']);
318 $rx->pharmacy->populate();
321 $this->assign("prescription", $rx);
323 $this->_state = false;
324 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
325 $this->template_mod . "_send.html");
328 function multiprintfax_header(&$pdf, $p)
330 return $this->multiprint_header($pdf, $p);
333 function multiprint_header(&$pdf, $p)
335 $this->providerid = $p->provider->id;
336 //print header
337 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'], '', '50', '', 'center', '');
338 $pdf->ezColumnsStart(array('num' => 2, 'gap' => 10));
339 $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 ='" .
340 add_escape_custom($p->provider->id) . "'");
341 $pdf->ezText($res['addr'], 12);
342 $my_y = $pdf->y;
343 $pdf->ezNewPage();
344 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>', 12);
345 // A client had a bad experience with a patient misusing a DEA number, so
346 // now the doctors write those in on printed prescriptions and only when
347 // necessary. If you need to change this back, then please make it a
348 // configurable option. Faxed prescriptions were not changed. -- Rod
349 // Now it is configureable. Change value in
350 // Administration->Globals->Rx
351 if ($GLOBALS['rx_enable_DEA']) {
352 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
353 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
354 } else {
355 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
359 if ($GLOBALS['rx_enable_NPI']) {
360 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
361 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
362 } else {
363 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
367 if ($GLOBALS['rx_enable_SLN']) {
368 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
369 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
370 } else {
371 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
375 $pdf->ezColumnsStop();
376 if ($my_y < $pdf->y) {
377 $pdf->ezSetY($my_y);
380 $pdf->ezText('', 10);
381 $pdf->setLineStyle(1);
382 $pdf->ezColumnsStart(array('num' => 2));
383 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
384 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>', 6);
385 $pdf->ezText($p->patient->get_name_display(), 10);
386 $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));
387 $pdf->ezText($res['addr']);
388 $my_y = $pdf->y;
389 $pdf->ezNewPage();
390 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
391 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>', 6);
392 $pdf->ezText($p->patient->date_of_birth, 10);
393 $pdf->ezText('');
394 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
395 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>', 6);
396 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT), 10);
397 $pdf->ezColumnsStop();
398 if ($my_y < $pdf->y) {
399 $pdf->ezSetY($my_y);
402 $pdf->ezText('');
403 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
404 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>', 6);
405 $pdf->ezText('', 10);
408 function multiprintcss_header($p)
410 echo("<div class='paddingdiv'>\n");
411 $this->providerid = $p->provider->id;
412 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
413 echo ("<tr>\n");
414 echo ("<td></td>\n");
415 echo ("<td>\n");
416 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
417 echo ("</td>\n");
418 echo ("</tr>\n");
419 echo ("<tr>\n");
420 echo ("<td>\n");
421 $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) . "'");
422 if (!empty($res)) {
423 $patterns = array ('/\n/','/Tel:/','/Fax:/');
424 $replace = array ('<br />', xl('Tel') . ':', xl('Fax') . ':');
425 $res = preg_replace($patterns, $replace, $res);
428 echo ('<span class="large">' . $res['addr'] . '</span>');
429 echo ("</td>\n");
430 echo ("<td>\n");
431 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>' . '<br />');
433 if ($GLOBALS['rx_enable_DEA']) {
434 if ($GLOBALS['rx_show_DEA']) {
435 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br />');
436 } else {
437 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br />' );
441 if ($GLOBALS['rx_enable_NPI']) {
442 if ($GLOBALS['rx_show_NPI']) {
443 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br />');
444 } else {
445 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br />');
449 if ($GLOBALS['rx_enable_SLN']) {
450 if ($GLOBALS['rx_show_SLN']) {
451 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br />');
452 } else {
453 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br />');
457 echo ("</td>\n");
458 echo ("</tr>\n");
459 echo ("<tr>\n");
460 echo ("<td rowspan='2' class='bordered'>\n");
461 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>' . '<br />');
462 echo ($p->patient->get_name_display() . '<br />');
463 $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));
464 if (!empty($res)) {
465 $patterns = array ('/\n/');
466 $replace = array ('<br />');
467 $res = preg_replace($patterns, $replace, $res);
470 echo ($res['addr']);
471 echo ("</td>\n");
472 echo ("<td class='bordered'>\n");
473 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br />');
474 echo ($p->patient->date_of_birth );
475 echo ("</td>\n");
476 echo ("</tr>\n");
477 echo ("<tr>\n");
478 echo ("<td class='bordered'>\n");
479 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br />');
480 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
481 echo ("</td>\n");
482 echo ("</tr>\n");
483 echo ("<tr>\n");
484 echo ("<td colspan='2' class='bordered'>\n");
485 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
486 echo ("</td>\n");
487 echo ("</tr>\n");
488 echo ("</table>\n");
491 function multiprintcss_preheader()
493 // this sets styling and other header information of the multiprint css sheet
494 echo ("<html>\n");
495 echo ("<head>\n");
496 echo ("<style>\n");
497 echo ("div {\n");
498 echo (" padding: 0;\n");
499 echo (" margin: 0;\n");
500 echo ("}\n");
501 echo ("body {\n");
502 echo (" font-family: sans-serif;\n");
503 echo (" font-weight: normal;\n");
504 echo (" font-size: 10pt;\n");
505 echo (" background: white;\n");
506 echo (" color: black;\n");
507 echo ("}\n");
508 echo ("span.large {\n");
509 echo (" font-size: 12pt;\n");
510 echo ("}\n");
511 echo ("span.small {\n");
512 echo (" font-size: 6pt;\n");
513 echo ("}\n");
514 echo ("td {\n");
515 echo (" vertical-align: top;\n");
516 echo (" width: 50%;\n");
517 echo (" font-size: 10pt;\n");
518 echo (" padding-bottom: 8pt;\n");
519 echo ("}\n");
520 echo ("td.bordered {\n");
521 echo (" border-top:1pt solid black;\n");
522 echo ("}\n");
523 echo ("div.paddingdiv {\n");
524 echo (" width: 524pt;\n");
525 echo (" height: 668pt;\n");
526 echo ("}\n");
527 echo ("div.scriptdiv {\n");
528 echo (" padding-top: 12pt;\n");
529 echo (" padding-bottom: 22pt;\n");
530 echo (" padding-left: 35pt;\n");
531 echo (" border-bottom:1pt solid black;\n");
532 echo ("}\n");
533 echo ("div.signdiv {\n");
534 echo (" margin-top: 40pt;\n");
535 echo (" font-size: 12pt;\n");
536 echo ("}\n");
537 echo ("</style>\n");
539 echo ("<title>" . xl('Prescription') . "</title>\n");
540 echo ("</head>\n");
541 echo ("<body>\n");
544 function multiprintfax_footer(&$pdf)
546 return $this->multiprint_footer($pdf);
549 function multiprint_footer(&$pdf)
551 if ($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax )) {
552 $sigfile = str_replace('{userid}', $_SESSION["authUser"], $this->pconfig['signature']);
553 if (file_exists($sigfile)) {
554 $pdf->ezText(xl('Signature') . ": ", 12);
555 // $pdf->ezImage($sigfile, "", "", "none", "left");
556 $pdf->ezImage($sigfile, "", "", "none", "center");
557 $pdf->ezText(xl('Date') . ": " . date('Y-m-d'), 12);
558 if ($this->is_print_to_fax) {
559 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
562 $addenumFile = $this->pconfig['addendum_file'];
563 if (file_exists($addenumFile)) {
564 $pdf->ezText('');
565 $f = fopen($addenumFile, "r");
566 while ($line = fgets($f, 1000)) {
567 $pdf->ezText(rtrim($line));
571 return;
575 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'), 12);
578 function multiprintcss_footer()
580 echo ("<div class='signdiv'>\n");
581 echo (xl('Signature') . ":________________________________<br />");
582 echo (xl('Date') . ": " . date('Y-m-d'));
583 echo ("</div>\n");
584 echo ("</div>\n");
587 function multiprintcss_postfooter()
589 echo("<script>\n");
590 echo("opener.top.printLogPrint(window);\n");
591 echo("</script>\n");
592 echo("</body>\n");
593 echo("</html>\n");
596 function get_prescription_body_text($p)
598 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
599 if ($p->get_form()) {
600 $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
603 $body .= "</b> <i>" .
604 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
605 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
606 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
607 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
608 if ($p->get_refills() > 0) {
609 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
610 if ($p->get_per_refill()) {
611 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
614 $body .= "</u>\n";
615 } else {
616 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
619 $note = $p->get_note();
620 if ($note != '') {
621 $body .= "\n" . text($note) . "\n";
624 return $body;
627 function multiprintfax_body(&$pdf, $p)
629 return $this->multiprint_body($pdf, $p);
632 function multiprint_body(&$pdf, $p)
634 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
635 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
636 $d = $this->get_prescription_body_text($p);
637 if ($pdf->ezText($d, 10, array(), 1)) {
638 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
639 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
640 $this->multiprint_footer($pdf);
641 $pdf->ezNewPage();
642 $this->multiprint_header($pdf, $p);
643 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
644 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
647 $my_y = $pdf->y;
648 $pdf->ezText($d, 10);
649 if ($this->pconfig['shading']) {
650 $pdf->setColor(.9, .9, .9);
651 $pdf->filledRectangle($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'] - $pdf->ez['leftMargin'], $my_y - $pdf->y);
652 $pdf->setColor(0, 0, 0);
655 $pdf->ezSetY($my_y);
656 $pdf->ezText($d, 10);
657 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
658 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
659 $pdf->ezText('');
660 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
661 $pdf->ezText('');
664 function multiprintcss_body($p)
666 $d = $this->get_prescription_body_text($p);
667 $patterns = array ('/\n/','/ /');
668 $replace = array ('<br />','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
669 $d = preg_replace($patterns, $replace, $d);
670 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
673 function multiprintfax_action($id = "")
675 $this->is_print_to_fax = true;
676 return $this->multiprint_action($id);
679 function multiprint_action($id = "")
681 $_POST['process'] = "true";
682 if (empty($id)) {
683 $this->function_argument_error();
686 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
687 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
688 $pdf->selectFont('Helvetica');
690 // $print_header = true;
691 $on_this_page = 0;
693 //print prescriptions body
694 $this->_state = false; // Added by Rod - see Controller.class.php
695 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
696 foreach ($ids as $id) {
697 $p = new Prescription($id);
698 // if ($print_header == true) {
699 if ($on_this_page == 0) {
700 $this->multiprint_header($pdf, $p);
703 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
704 $this->multiprint_footer($pdf);
705 $pdf->ezNewPage();
706 $this->multiprint_header($pdf, $p);
707 // $print_header = false;
708 $on_this_page = 1;
711 $this->multiprint_body($pdf, $p);
714 $this->multiprint_footer($pdf);
716 $pFirstName = $p->patient->fname; //modified by epsdky for prescription title change to include patient name and ID
717 $pFName = convert_safe_file_dir_name($pFirstName);
718 $modedFileName = "Rx_{$pFName}_{$p->patient->id}.pdf";
720 $pdf->ezStream(array('Content-Disposition' => $modedFileName));
721 return;
724 function multiprintcss_action($id = "")
726 $_POST['process'] = "true";
727 if (empty($id)) {
728 $this->function_argument_error();
731 $this->multiprintcss_preheader();
733 $this->_state = false; // Added by Rod - see Controller.class.php
734 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
736 $on_this_page = 0;
737 foreach ($ids as $id) {
738 $p = new Prescription($id);
739 if ($on_this_page == 0) {
740 $this->multiprintcss_header($p);
743 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
744 $this->multiprintcss_footer();
745 $this->multiprintcss_header($p);
746 $on_this_page = 1;
749 $this->multiprintcss_body($p);
752 $this->multiprintcss_footer();
753 $this->multiprintcss_postfooter();
754 return;
757 function send_action_process($id)
759 $dummy = ""; // Added by Rod to avoid run-time warnings
760 if ($_POST['process'] != "true") {
761 return;
764 if (empty($id)) {
765 $this->function_argument_error();
768 $p = new Prescription($id);
769 switch ($_POST['submit']) {
770 case (xl("Print") . " (" . xl("PDF") . ")"):
771 // The following statement added by Rod.
772 // Looking at Controller.class.php, it appears that _state is set to false
773 // to indicate that no further HTML is to be generated.
774 $this->_state = false; // Added by Rod - see Controller.class.php
775 return $this->_print_prescription($p, $dummy);
776 break;
777 case (xl("Print") . " (" . xl("HTML") . ")"):
778 $this->_state = false;
779 return $this->_print_prescription_css($p, $dummy);
780 break;
781 case xl("Print To Fax"):
782 $this->_state = false;
783 $this->is_print_to_fax = true;
784 return $this->_print_prescription($p, $dummy);
785 break;
786 case xl("Email"):
787 return $this->_email_prescription($p, $_POST['email_to']);
788 break;
789 case xl("Fax"):
790 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
791 //$this->assign("process_result","No fax server is currently setup.");
792 return $this->_fax_prescription($p, $_POST['fax_to']);
793 break;
794 case xl("Auto Send"):
795 $pharmacy_id = $_POST['pharmacy_id'];
796 //echo "auto sending to : " . $_POST['pharmacy_id'];
797 $phar = new Pharmacy($_POST['pharmacy_id']);
798 //print_r($phar);
799 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
800 return $this->_print_prescription($p, $dummy);
801 } elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
802 $email = $phar->get_email();
803 if (!empty($email)) {
804 return $this->_email_prescription($p, $phar->get_email());
807 //else print it
808 } elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
809 $faxNum = $phar->get_fax();
810 if (!empty($faxNum)) {
811 return $this->_fax_prescription($p, $faxNum);
814 // return $this->assign("process_result","No fax server is currently setup.");
815 // else default is printing,
816 } else {
817 //the pharmacy has no default or default is print
818 return $this->_print_prescription($p, $dummy);
820 break;
823 return;
826 function _print_prescription($p, &$toFile)
828 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
829 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
831 $pdf->selectFont('Helvetica');
833 // Signature images are to be used only when faxing.
834 if (!empty($toFile)) {
835 $this->is_faxing = true;
838 $this->multiprint_header($pdf, $p);
839 $this->multiprint_body($pdf, $p);
840 $this->multiprint_footer($pdf);
842 if (!empty($toFile)) {
843 $toFile = $pdf->ezOutput();
844 } else {
845 $pdf->ezStream();
846 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
849 return;
852 function _print_prescription_css($p, &$toFile)
855 $this->multiprintcss_preheader();
856 $this->multiprintcss_header($p);
857 $this->multiprintcss_body($p);
858 $this->multiprintcss_footer();
859 $this->multiprintcss_postfooter();
862 function _print_prescription_old($p, &$toFile)
864 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
865 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
866 $pdf->selectFont('Helvetica');
867 if (!empty($this->pconfig['logo'])) {
868 $pdf->ezImage($this->pconfig['logo'], "", "", "none", "left");
871 $pdf->ezText($p->get_prescription_display(), 10);
872 if ($this->pconfig['use_signature']) {
873 $pdf->ezImage($this->pconfig['signature'], "", "", "none", "left");
874 } else {
875 $pdf->ezText("\n\n\n\nSignature:________________________________", 10);
878 if (!empty($toFile)) {
879 $toFile = $pdf->ezOutput();
880 } else {
881 $pdf->ezStream();
882 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
885 return;
888 function _email_prescription($p, $email)
890 if (empty($email)) {
891 $this->assign("process_result", "Email could not be sent, the address supplied: '$email' was empty or invalid.");
892 return;
895 $mail = new PHPMailer();
896 //this is a temporary config item until the rest of the per practice billing settings make their way in
897 $mail->From = $GLOBALS['practice_return_email_path'];
898 $mail->FromName = $p->provider->get_name_display();
899 $mail->isMail();
900 $mail->Host = "localhost";
901 $mail->Mailer = "mail";
902 $text_body = $p->get_prescription_display();
903 $mail->Body = $text_body;
904 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
905 $mail->AddAddress($email);
906 if ($mail->Send()) {
907 $this->assign("process_result", "Email was successfully sent to: " . $email);
908 return;
909 } else {
910 $this->assign("process_result", "There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
911 return;
915 function do_lookup()
917 if ($_POST['process'] != "true") {
918 // don't do a lookup
919 $this->assign("drug", $_GET['drug']);
920 return;
923 // process the lookup
924 $this->assign("drug", $_POST['drug']);
925 $list = array();
926 if (!empty($_POST['drug'])) {
927 $list = $this->RxList->get_list($_POST['drug']);
930 if (is_array($list)) {
931 $list = array_flip($list);
932 $this->assign("drug_options", $list);
933 $this->assign("drug_values", array_keys($list));
934 } else {
935 $this->assign("NO_RESULTS", xl("No results found for") . ": " . $_POST['drug']);
938 //print_r($_POST);
939 //$this->assign("PROCESS","");
941 $_POST['process'] = "";
944 function _fax_prescription($p, $faxNum)
946 $err = "Sent fax";
947 //strip - ,(, ), and ws
948 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/", "", $faxNum);
949 //validate the number
951 if (!empty($faxNum) && is_numeric($faxNum)) {
952 //get the sendfax command and execute it
953 $cmd = $this->pconfig['sendfax'];
954 // prepend any prefix to the fax number
955 $pref = $this->pconfig['prefix'];
956 $faxNum = $pref . $faxNum;
957 if (empty($cmd)) {
958 $err .= " Send fax not set in includes/config.php";
959 } else {
960 //generate file to fax
961 $faxFile = "Failed";
962 $this->_print_prescription($p, $faxFile);
963 if (empty($faxFile)) {
964 $err .= " _print_prescription returned empty file";
967 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
968 $p->get_patient_id() . "_fax_.pdf";
969 //print "filename is $fileName";
970 touch($fileName); // php bug
971 $handle = fopen($fileName, "w");
972 if (!$handle) {
973 $err .= " Failed to open file $fileName to write fax to";
976 if (fwrite($handle, $faxFile) === false) {
977 $err .= " Failed to write data to $fileName";
980 fclose($handle);
981 $args = " -n -d $faxNum $fileName";
982 //print "command is $cmd $args<br />";
983 exec($cmd . $args);
985 } else {
986 $err = "bad fax number passed to function";
989 if ($err) {
990 $this->assign("process_result", $err);