composer packages update (#6006)
[openemr.git] / controllers / C_Prescription.class.php
bloba7d728c274de4614c68086cf4782d1d7f7509282
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.php");
18 require_once($GLOBALS['fileroot'] . "/library/amc.php");
20 use OpenEMR\Common\Csrf\CsrfUtils;
21 use OpenEMR\Common\Http\oeHttp;
22 use OpenEMR\Rx\RxList;
23 use PHPMailer\PHPMailer\PHPMailer;
25 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;
32 var $RxList;
33 var $prescriptions;
35 function __construct($template_mod = "general")
37 parent::__construct();
38 $this->template_mod = $template_mod;
39 $this->assign("FORM_ACTION", $GLOBALS['webroot'] . "/controller.php?" . attr($_SERVER['QUERY_STRING']));
40 $this->assign("TOP_ACTION", $GLOBALS['webroot'] . "/controller.php?" . "prescription" . "&");
41 $this->assign("STYLE", $GLOBALS['style']);
42 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
43 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
44 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
45 $this->RxList = new RxList();
46 // test if rxnorm available for lookups.
47 $rxn = sqlQuery("SELECT table_name FROM information_schema.tables WHERE table_name = 'RXNCONSO' OR table_name = 'rxconso'");
48 $rxcui = sqlQuery("SELECT ct_id FROM `code_types` WHERE `ct_key` = ? AND `ct_active` = 1", array('RXCUI'));
49 $this->assign("RXNORMS_AVAILABLE", !empty($rxn));
50 $this->assign("RXCUI_AVAILABLE", !empty($rxcui));
51 // Assign the CSRF_TOKEN_FORM
52 $this->assign("CSRF_TOKEN_FORM", CsrfUtils::collectCsrfToken());
54 if ($GLOBALS['inhouse_pharmacy']) {
55 // Make an array of drug IDs and selectors for the template.
56 $drug_array_values = array(0);
57 $drug_array_output = array("-- " . xl('or select from inventory') . " --");
58 $drug_attributes = '';
60 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
62 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
63 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
64 "t.period, t.quantity, t.refills, d.drug_code " .
65 "FROM drug_templates AS t, drugs AS d WHERE " .
66 "d.drug_id = t.drug_id ORDER BY t.selector");
68 while ($row = sqlFetchArray($res)) {
69 $tmp_output = $row['selector'];
70 if ($row['ndc_number']) {
71 $tmp_output .= ' [' . $row['ndc_number'] . ']';
74 $drug_array_values[] = $row['drug_id'];
75 $drug_array_output[] = $tmp_output;
76 if ($drug_attributes) {
77 $drug_attributes .= ',';
80 $drug_attributes .= "[" .
81 js_escape($row['name']) . "," . // 0
82 js_escape($row['form']) . "," . // 1
83 js_escape($row['dosage']) . "," . // 2
84 js_escape($row['size']) . "," . // 3
85 js_escape($row['unit']) . "," . // 4
86 js_escape($row['route']) . "," . // 5
87 js_escape($row['period']) . "," . // 6
88 js_escape($row['substitute']) . "," . // 7
89 js_escape($row['quantity']) . "," . // 8
90 js_escape($row['refills']) . "," . // 9
91 js_escape($row['quantity']) . "," . // 10 quantity per_refill
92 js_escape($row['drug_code']) . "]"; // 11 rxnorm drug code
95 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
96 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
97 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
101 function default_action()
103 $this->assign("prescription", $this->prescriptions[0]);
104 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
107 function edit_action($id = "", $patient_id = "", $p_obj = null)
110 if ($p_obj != null && get_class($p_obj) == "prescription") {
111 $this->prescriptions[0] = $p_obj;
112 } elseif (empty($this->prescriptions[0]) || !is_object($this->prescriptions[0]) || (get_class($this->prescriptions[0]) != "prescription")) {
113 $this->prescriptions[0] = new Prescription($id);
116 if (!empty($patient_id)) {
117 $this->prescriptions[0]->set_patient_id($patient_id);
120 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
122 // If quantity to dispense is not already set from a POST, set its
123 // default value.
124 if (! $this->getTemplateVars('DISP_QUANTITY')) {
125 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
128 $this->default_action();
131 function list_action($id, $sort = "")
133 if (empty($id)) {
134 $this->function_argument_error();
135 exit;
138 if (!empty($sort)) {
139 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
140 } else {
141 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
144 // Collect interactions if the global is turned on
145 if ($GLOBALS['rx_show_drug_drug']) {
146 $interaction = "";
147 // Ensure RxNorm installed
148 $rxn = sqlQuery("SELECT table_name FROM information_schema.tables WHERE table_name = 'RXNCONSO' OR table_name = 'rxconso'");
149 if ($rxn == false) {
150 $interaction = xlt("Could not find RxNorm Table! Please install.");
151 } elseif ($rxn == true) {
152 // Grab medication list from prescriptions list and load into array
153 $pid = $GLOBALS['pid'];
154 $medList = sqlStatement("SELECT drug FROM prescriptions WHERE active = 1 AND patient_id = ?", array($pid));
155 $nameList = array();
156 while ($name = sqlFetchArray($medList)) {
157 $drug = explode(" ", $name['drug']);
158 $rXn = sqlQuery("SELECT `rxcui` FROM `" . mitigateSqlTableUpperCase('RXNCONSO') . "` WHERE `str` LIKE ?", array("%" . $drug[0] . "%"));
159 $nameList[] = $rXn['rxcui'];
161 if (count($nameList) < 2) {
162 $interaction = xlt("Need more than one drug.");
163 } else {
164 // If there are drugs to compare, collect the data
165 // (array_filter removes empty items)
166 $rxcui_list = implode("+", array_filter($nameList));
167 // 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
168 $response = oeHttp::get('https://rxnav.nlm.nih.gov/REST/interaction/list.json?rxcuis=' . $rxcui_list);
169 $data = $response->body();
170 $json = json_decode($data, true);
171 if (!empty($json['fullInteractionTypeGroup'][0]['fullInteractionType'])) {
172 foreach ($json['fullInteractionTypeGroup'][0]['fullInteractionType'] as $item) {
173 $interaction .= '<div class="alert alert-danger">';
174 $interaction .= xlt('Comment') . ":" . text($item['comment']) . "<br />";
175 $interaction .= xlt('Drug1 Name{{Drug1 Interaction}}') . ":" . text($item['minConcept'][0]['name']) . "<br />";
176 $interaction .= xlt('Drug2 Name{{Drug2 Interaction}}') . ":" . text($item['minConcept'][1]['name']) . "<br />";
177 $interaction .= xlt('Severity') . ":" . text($item['interactionPair'][0]['severity']) . "<br />";
178 $interaction .= xlt('Description') . ":" . text($item['interactionPair'][0]['description']);
179 $interaction .= '</div>';
181 } else {
182 $interaction = xlt('No interactions found');
186 $this->assign("INTERACTION", $interaction);
189 // flag to indicate the CAMOS form is regsitered and active
190 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
192 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
195 function block_action($id, $sort = "")
197 if (empty($id)) {
198 $this->function_argument_error();
199 exit;
202 if (!empty($sort)) {
203 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
204 } else {
205 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
208 //print_r(Prescription::prescriptions_factory($id));
209 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
212 function fragment_action($id, $sort = "")
214 if (empty($id)) {
215 $this->function_argument_error();
216 exit;
219 if (!empty($sort)) {
220 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
221 } else {
222 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
225 //print_r(Prescription::prescriptions_factory($id));
226 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
229 function lookup_action()
231 $this->do_lookup();
232 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
235 function edit_action_process()
237 if ($_POST['process'] != "true") {
238 return;
241 //print_r($_POST);
243 // Stupid Smarty code treats empty values as not specified values.
244 // Since active is a checkbox, represent the unchecked state as -1.
245 if (empty($_POST['active'])) {
246 $_POST['active'] = '-1';
248 if (!empty($_POST['start_date'])) {
249 $_POST['start_date'] = DateToYYYYMMDD($_POST['start_date']);
252 $this->prescriptions[0] = new Prescription($_POST['id']);
253 parent::populate_object($this->prescriptions[0]);
254 //echo $this->prescriptions[0]->toString(true);
255 $this->prescriptions[0]->persist();
256 $_POST['process'] = "";
258 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
260 // If the "Prescribe and Dispense" button was clicked, then
261 // redisplay as in edit_action() but also replicate the fee and
262 // include a piece of javascript to call dispense().
264 if (!empty($_POST['disp_button'])) {
265 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
266 $this->assign("DISP_FEE", $_POST['disp_fee']);
267 $this->assign("ENDING_JAVASCRIPT", "dispense();");
268 $this->_state = false;
269 return $this->edit_action($this->prescriptions[0]->id);
272 // Set the AMC reporting flag (to record percentage of prescriptions that
273 // are set as e-prescriptions)
274 if (!(empty($_POST['escribe_flag']))) {
275 // add the e-prescribe flag
276 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
277 } else {
278 // remove the e-prescribe flag
279 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
282 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
283 if (!(empty($_POST['checked_formulary_flag']))) {
284 // add the e-prescribe flag
285 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
286 } else {
287 // remove the e-prescribe flag
288 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
291 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
292 if (!(empty($_POST['controlled_substance_flag']))) {
293 // add the e-prescribe flag
294 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
295 } else {
296 // remove the e-prescribe flag
297 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
300 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
301 // if ($this->prescriptions[0]->get_active() > 0) {
302 // return $this->send_action($this->prescriptions[0]->id);
303 // }
304 $this->list_action($this->prescriptions[0]->get_patient_id());
305 exit;
308 function send_action($id)
310 $_POST['process'] = "true";
311 if (empty($id)) {
312 $this->function_argument_error();
315 $rx = new Prescription($id);
316 // Populate pharmacy info if the patient has a default pharmacy.
317 // Probably the Prescription object should handle this instead, but
318 // doing it there will require more careful research and testing.
319 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
320 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
321 if ($prow['pharmacy_id']) {
322 $rx->pharmacy->set_id($prow['pharmacy_id']);
323 $rx->pharmacy->populate();
326 $this->assign("prescription", $rx);
328 $this->_state = false;
329 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
330 $this->template_mod . "_send.html");
333 function multiprintfax_header(&$pdf, $p)
335 return $this->multiprint_header($pdf, $p);
338 function multiprint_header(&$pdf, $p)
340 $this->providerid = $p->provider->id;
341 //print header
342 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'], null, '50', '', 'center', '');
343 $pdf->ezColumnsStart(array('num' => 2, 'gap' => 10));
344 $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 ='" .
345 add_escape_custom($p->provider->id) . "'");
346 $pdf->ezText($res['addr'] ?? '', 12);
347 $my_y = $pdf->y;
348 $pdf->ezNewPage();
349 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>', 12);
350 // A client had a bad experience with a patient misusing a DEA number, so
351 // now the doctors write those in on printed prescriptions and only when
352 // necessary. If you need to change this back, then please make it a
353 // configurable option. Faxed prescriptions were not changed. -- Rod
354 // Now it is configureable. Change value in
355 // Administration->Globals->Rx
356 if ($GLOBALS['rx_enable_DEA']) {
357 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
358 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
359 } else {
360 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
364 if ($GLOBALS['rx_enable_NPI']) {
365 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
366 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
367 } else {
368 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
372 if ($GLOBALS['rx_enable_SLN']) {
373 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
374 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
375 } else {
376 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
380 $pdf->ezColumnsStop();
381 if ($my_y < $pdf->y) {
382 $pdf->ezSetY($my_y);
385 $pdf->ezText('', 10);
386 $pdf->setLineStyle(1);
387 $pdf->ezColumnsStart(array('num' => 2));
388 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
389 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>', 6);
390 $pdf->ezText($p->patient->get_name_display(), 10);
391 $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));
392 $pdf->ezText($res['addr']);
393 $my_y = $pdf->y;
394 $pdf->ezNewPage();
395 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
396 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>', 6);
397 $pdf->ezText($p->patient->date_of_birth, 10);
398 $pdf->ezText('');
399 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
400 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>', 6);
401 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT), 10);
402 $pdf->ezColumnsStop();
403 if ($my_y < $pdf->y) {
404 $pdf->ezSetY($my_y);
407 $pdf->ezText('');
408 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
409 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>', 6);
410 $pdf->ezText('', 10);
413 function multiprintcss_header($p)
415 echo("<div class='paddingdiv'>\n");
416 $this->providerid = $p->provider->id;
417 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
418 echo ("<tr>\n");
419 echo ("<td></td>\n");
420 echo ("<td>\n");
421 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
422 echo ("</td>\n");
423 echo ("</tr>\n");
424 echo ("<tr>\n");
425 echo ("<td>\n");
426 $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) . "'");
427 if (!empty($res)) {
428 $patterns = array ('/\n/','/Tel:/','/Fax:/');
429 $replace = array ('<br />', xl('Tel') . ':', xl('Fax') . ':');
430 $res = preg_replace($patterns, $replace, $res);
433 echo ('<span class="large">' . ($res['addr'] ?? '') . '</span>');
434 echo ("</td>\n");
435 echo ("<td>\n");
436 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>' . '<br />');
438 if ($GLOBALS['rx_enable_DEA']) {
439 if ($GLOBALS['rx_show_DEA']) {
440 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br />');
441 } else {
442 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br />' );
446 if ($GLOBALS['rx_enable_NPI']) {
447 if ($GLOBALS['rx_show_NPI']) {
448 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br />');
449 } else {
450 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br />');
454 if ($GLOBALS['rx_enable_SLN']) {
455 if ($GLOBALS['rx_show_SLN']) {
456 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br />');
457 } else {
458 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br />');
462 echo ("</td>\n");
463 echo ("</tr>\n");
464 echo ("<tr>\n");
465 echo ("<td rowspan='2' class='bordered'>\n");
466 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>' . '<br />');
467 echo ($p->patient->get_name_display() . '<br />');
468 $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));
469 if (!empty($res)) {
470 $patterns = array ('/\n/');
471 $replace = array ('<br />');
472 $res = preg_replace($patterns, $replace, $res);
475 echo ($res['addr']);
476 echo ("</td>\n");
477 echo ("<td class='bordered'>\n");
478 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br />');
479 echo ($p->patient->date_of_birth );
480 echo ("</td>\n");
481 echo ("</tr>\n");
482 echo ("<tr>\n");
483 echo ("<td class='bordered'>\n");
484 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br />');
485 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
486 echo ("</td>\n");
487 echo ("</tr>\n");
488 echo ("<tr>\n");
489 echo ("<td colspan='2' class='bordered'>\n");
490 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
491 echo ("</td>\n");
492 echo ("</tr>\n");
493 echo ("</table>\n");
496 function multiprintcss_preheader()
498 // this sets styling and other header information of the multiprint css sheet
499 echo ("<html>\n");
500 echo ("<head>\n");
501 echo ("<style>\n");
502 echo ("div {\n");
503 echo (" padding: 0;\n");
504 echo (" margin: 0;\n");
505 echo ("}\n");
506 echo ("body {\n");
507 echo (" font-family: sans-serif;\n");
508 echo (" font-weight: normal;\n");
509 echo (" font-size: 10pt;\n");
510 echo (" background: white;\n");
511 echo (" color: black;\n");
512 echo ("}\n");
513 echo ("span.large {\n");
514 echo (" font-size: 12pt;\n");
515 echo ("}\n");
516 echo ("span.small {\n");
517 echo (" font-size: 6pt;\n");
518 echo ("}\n");
519 echo ("td {\n");
520 echo (" vertical-align: top;\n");
521 echo (" width: 50%;\n");
522 echo (" font-size: 10pt;\n");
523 echo (" padding-bottom: 8pt;\n");
524 echo ("}\n");
525 echo ("td.bordered {\n");
526 echo (" border-top:1pt solid black;\n");
527 echo ("}\n");
528 echo ("div.paddingdiv {\n");
529 echo (" width: 524pt;\n");
530 echo (" height: 668pt;\n");
531 echo ("}\n");
532 echo ("div.scriptdiv {\n");
533 echo (" padding-top: 12pt;\n");
534 echo (" padding-bottom: 22pt;\n");
535 echo (" padding-left: 35pt;\n");
536 echo (" border-bottom:1pt solid black;\n");
537 echo ("}\n");
538 echo ("div.signdiv {\n");
539 echo (" margin-top: 40pt;\n");
540 echo (" font-size: 12pt;\n");
541 echo ("}\n");
542 echo ("</style>\n");
544 echo ("<title>" . xl('Prescription') . "</title>\n");
545 echo ("</head>\n");
546 echo ("<body>\n");
549 function multiprintfax_footer(&$pdf)
551 return $this->multiprint_footer($pdf);
554 function multiprint_footer(&$pdf)
556 if ($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax )) {
557 $sigfile = str_replace('{userid}', $_SESSION["authUser"], $this->pconfig['signature']);
558 if (file_exists($sigfile)) {
559 $pdf->ezText(xl('Signature') . ": ", 12);
560 // $pdf->ezImage($sigfile, "", "", "none", "left");
561 $pdf->ezImage($sigfile, "", "", "none", "center");
562 $pdf->ezText(xl('Date') . ": " . date('Y-m-d'), 12);
563 if ($this->is_print_to_fax) {
564 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
567 $addenumFile = $this->pconfig['addendum_file'];
568 if (file_exists($addenumFile)) {
569 $pdf->ezText('');
570 $f = fopen($addenumFile, "r");
571 while ($line = fgets($f, 1000)) {
572 $pdf->ezText(rtrim($line));
576 return;
580 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'), 12);
583 function multiprintcss_footer()
585 echo ("<div class='signdiv'>\n");
586 echo (xl('Signature') . ":________________________________<br />");
587 echo (xl('Date') . ": " . date('Y-m-d'));
588 echo ("</div>\n");
589 echo ("</div>\n");
592 function multiprintcss_postfooter()
594 echo("<script>\n");
595 echo("opener.top.printLogPrint(window);\n");
596 echo("</script>\n");
597 echo("</body>\n");
598 echo("</html>\n");
601 function get_prescription_body_text($p)
603 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
604 if ($p->get_form()) {
605 $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
608 $body .= "</b> <i>" .
609 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
610 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
611 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage() ?? '') . ' ' . text($p->form_array[$p->get_form()] ?? '') . ' ' .
612 text($p->route_array[$p->get_route()] ?? '') . ' ' . text($p->interval_array[$p->get_interval()] ?? '') . "\n";
613 if ($p->get_refills() > 0) {
614 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
615 if ($p->get_per_refill()) {
616 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
619 $body .= "</u>\n";
620 } else {
621 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
624 $note = $p->get_note();
625 if ($note != '') {
626 $body .= "\n" . text($note) . "\n";
629 return $body;
632 function multiprintfax_body(&$pdf, $p)
634 return $this->multiprint_body($pdf, $p);
637 function multiprint_body(&$pdf, $p)
639 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
640 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
641 $d = $this->get_prescription_body_text($p);
642 if ($pdf->ezText($d, 10, array(), 1)) {
643 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
644 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
645 $this->multiprint_footer($pdf);
646 $pdf->ezNewPage();
647 $this->multiprint_header($pdf, $p);
648 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
649 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
652 $my_y = $pdf->y;
653 $pdf->ezText($d, 10);
654 if ($this->pconfig['shading']) {
655 $pdf->setColor(.9, .9, .9);
656 $pdf->filledRectangle($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'] - $pdf->ez['leftMargin'], $my_y - $pdf->y);
657 $pdf->setColor(0, 0, 0);
660 $pdf->ezSetY($my_y);
661 $pdf->ezText($d, 10);
662 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
663 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
664 $pdf->ezText('');
665 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth'] - $pdf->ez['rightMargin'], $pdf->y);
666 $pdf->ezText('');
669 function multiprintcss_body($p)
671 $d = $this->get_prescription_body_text($p);
672 $patterns = array ('/\n/','/ /');
673 $replace = array ('<br />','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
674 $d = preg_replace($patterns, $replace, $d);
675 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
678 function multiprintfax_action($id = "")
680 $this->is_print_to_fax = true;
681 return $this->multiprint_action($id);
684 function multiprint_action($id = "")
686 $_POST['process'] = "true";
687 if (empty($id)) {
688 $this->function_argument_error();
691 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
692 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
693 $pdf->selectFont('Helvetica');
695 // $print_header = true;
696 $on_this_page = 0;
698 //print prescriptions body
699 $this->_state = false; // Added by Rod - see Controller.class.php
700 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
701 foreach ($ids as $id) {
702 $p = new Prescription($id);
703 // if ($print_header == true) {
704 if ($on_this_page == 0) {
705 $this->multiprint_header($pdf, $p);
708 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
709 $this->multiprint_footer($pdf);
710 $pdf->ezNewPage();
711 $this->multiprint_header($pdf, $p);
712 // $print_header = false;
713 $on_this_page = 1;
716 $this->multiprint_body($pdf, $p);
719 $this->multiprint_footer($pdf);
721 $pFirstName = $p->patient->fname; //modified by epsdky for prescription filename change to include patient name and ID
722 $pFName = convert_safe_file_dir_name($pFirstName);
723 $modedFileName = "Rx_{$pFName}_{$p->patient->id}.pdf";
724 $pdf->ezStream(array('Content-Disposition' => $modedFileName));
725 return;
728 function multiprintcss_action($id = "")
730 $_POST['process'] = "true";
731 if (empty($id)) {
732 $this->function_argument_error();
735 $this->multiprintcss_preheader();
737 $this->_state = false; // Added by Rod - see Controller.class.php
738 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
740 $on_this_page = 0;
741 foreach ($ids as $id) {
742 $p = new Prescription($id);
743 if ($on_this_page == 0) {
744 $this->multiprintcss_header($p);
747 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
748 $this->multiprintcss_footer();
749 $this->multiprintcss_header($p);
750 $on_this_page = 1;
753 $this->multiprintcss_body($p);
756 $this->multiprintcss_footer();
757 $this->multiprintcss_postfooter();
758 return;
761 function send_action_process($id)
763 $dummy = ""; // Added by Rod to avoid run-time warnings
764 if ($_POST['process'] != "true") {
765 return;
768 if (empty($id)) {
769 $this->function_argument_error();
772 $p = new Prescription($id);
773 switch ($_POST['submit']) {
774 case (xl("Print") . " (" . xl("PDF") . ")"):
775 // The following statement added by Rod.
776 // Looking at Controller.class.php, it appears that _state is set to false
777 // to indicate that no further HTML is to be generated.
778 $this->_state = false; // Added by Rod - see Controller.class.php
779 return $this->print_prescription($p, $dummy);
780 break;
781 case (xl("Print") . " (" . xl("HTML") . ")"):
782 $this->_state = false;
783 return $this->print_prescription_css($p, $dummy);
784 break;
785 case xl("Print To Fax"):
786 $this->_state = false;
787 $this->is_print_to_fax = true;
788 return $this->print_prescription($p, $dummy);
789 break;
790 case xl("Email"):
791 return $this->email_prescription($p, $_POST['email_to']);
792 break;
793 case xl("Fax"):
794 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
795 //$this->assign("process_result","No fax server is currently setup.");
796 return $this->fax_prescription($p, $_POST['fax_to']);
797 break;
798 case xl("Auto Send"):
799 $pharmacy_id = $_POST['pharmacy_id'];
800 //echo "auto sending to : " . $_POST['pharmacy_id'];
801 $phar = new Pharmacy($_POST['pharmacy_id']);
802 //print_r($phar);
803 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
804 return $this->print_prescription($p, $dummy);
805 } elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
806 $email = $phar->get_email();
807 if (!empty($email)) {
808 return $this->email_prescription($p, $phar->get_email());
811 //else print it
812 } elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
813 $faxNum = $phar->get_fax();
814 if (!empty($faxNum)) {
815 return $this->fax_prescription($p, $faxNum);
818 // return $this->assign("process_result","No fax server is currently setup.");
819 // else default is printing,
820 } else {
821 //the pharmacy has no default or default is print
822 return $this->print_prescription($p, $dummy);
824 break;
827 return;
830 function print_prescription($p, &$toFile)
832 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
833 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
835 $pdf->selectFont('Helvetica');
837 // Signature images are to be used only when faxing.
838 if (!empty($toFile)) {
839 $this->is_faxing = true;
842 $this->multiprint_header($pdf, $p);
843 $this->multiprint_body($pdf, $p);
844 $this->multiprint_footer($pdf);
846 if (!empty($toFile)) {
847 $toFile = $pdf->ezOutput();
848 } else {
849 $pdf->ezStream();
850 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
853 return;
856 function print_prescription_css($p, &$toFile)
859 $this->multiprintcss_preheader();
860 $this->multiprintcss_header($p);
861 $this->multiprintcss_body($p);
862 $this->multiprintcss_footer();
863 $this->multiprintcss_postfooter();
866 function print_prescription_old($p, &$toFile)
868 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
869 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
870 $pdf->selectFont('Helvetica');
871 if (!empty($this->pconfig['logo'])) {
872 $pdf->ezImage($this->pconfig['logo'], "", "", "none", "left");
875 $pdf->ezText($p->get_prescription_display(), 10);
876 if ($this->pconfig['use_signature']) {
877 $pdf->ezImage($this->pconfig['signature'], "", "", "none", "left");
878 } else {
879 $pdf->ezText("\n\n\n\nSignature:________________________________", 10);
882 if (!empty($toFile)) {
883 $toFile = $pdf->ezOutput();
884 } else {
885 $pdf->ezStream();
886 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
889 return;
892 function email_prescription($p, $email)
894 if (empty($email)) {
895 $this->assign("process_result", "Email could not be sent, the address supplied: '$email' was empty or invalid.");
896 return;
899 $mail = new PHPMailer();
900 //this is a temporary config item until the rest of the per practice billing settings make their way in
901 $mail->From = $GLOBALS['practice_return_email_path'];
902 $mail->FromName = $p->provider->get_name_display();
903 $mail->isMail();
904 $mail->Host = "localhost";
905 $mail->Mailer = "mail";
906 $text_body = $p->get_prescription_display();
907 $mail->Body = $text_body;
908 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
909 $mail->AddAddress($email);
910 if ($mail->Send()) {
911 $this->assign("process_result", "Email was successfully sent to: " . $email);
912 return;
913 } else {
914 $this->assign("process_result", "There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
915 return;
919 function do_lookup()
921 if ($_POST['process'] != "true") {
922 // don't do a lookup
923 $this->assign("drug", $_GET['drug']);
924 return;
927 // process the lookup
928 $this->assign("drug", $_POST['drug']);
929 $list = array();
930 if (!empty($_POST['drug'])) {
931 $list = $this->RxList->getList($_POST['drug']);
934 if (is_array($list)) {
935 $list = array_flip($list);
936 $this->assign("drug_options", $list);
937 $this->assign("drug_values", array_keys($list));
938 } else {
939 $this->assign("NO_RESULTS", xl("No results found for") . ": " . $_POST['drug']);
942 //print_r($_POST);
943 //$this->assign("PROCESS","");
945 $_POST['process'] = "";
948 function fax_prescription($p, $faxNum)
950 $err = "Sent fax";
951 //strip - ,(, ), and ws
952 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/", "", $faxNum);
953 //validate the number
955 if (!empty($faxNum) && is_numeric($faxNum)) {
956 //get the sendfax command and execute it
957 $cmd = $this->pconfig['sendfax'];
958 // prepend any prefix to the fax number
959 $pref = $this->pconfig['prefix'];
960 $faxNum = $pref . $faxNum;
961 if (empty($cmd)) {
962 $err .= " Send fax not set in includes/config.php";
963 } else {
964 //generate file to fax
965 $faxFile = "Failed";
966 $this->print_prescription($p, $faxFile);
967 if (empty($faxFile)) {
968 $err .= " print_prescription returned empty file";
971 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
972 $p->get_patient_id() . "_fax_.pdf";
973 //print "filename is $fileName";
974 touch($fileName); // php bug
975 $handle = fopen($fileName, "w");
976 if (!$handle) {
977 $err .= " Failed to open file $fileName to write fax to";
980 if (fwrite($handle, $faxFile) === false) {
981 $err .= " Failed to write data to $fileName";
984 fclose($handle);
985 $args = " -n -d $faxNum $fileName";
986 //print "command is $cmd $args<br />";
987 exec($cmd . $args);
989 } else {
990 $err = "bad fax number passed to function";
993 if ($err) {
994 $this->assign("process_result", $err);