dump db version
[openemr.git] / controllers / C_Prescription.class.php
blob98694cbca66aba8dd3eaa5d44836a030400cfa92
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 * @author Sherwin Gaddis <sherwingaddis@gmail.com>
10 * @copyright Copyright (c) 2015 Roberto Vasquez <robertogagliotta@gmail.com>
11 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
12 * @copyright Copyright (c) 2018 Sherwin Gaddis <sherwingaddis@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
17 require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
18 require_once($GLOBALS['fileroot'] . "/library/registry.inc");
19 require_once($GLOBALS['fileroot'] . "/library/amc.php");
21 use PHPMailer\PHPMailer\PHPMailer;
23 class C_Prescription extends Controller
26 var $template_mod;
27 var $pconfig;
28 var $providerid = 0;
29 var $is_faxing = false;
30 var $is_print_to_fax = false;
32 function __construct($template_mod = "general")
34 parent::__construct();
36 $this->template_mod = $template_mod;
37 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . attr($_SERVER['QUERY_STRING']));
38 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
39 $this->assign("STYLE", $GLOBALS['style']);
40 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
41 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
42 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
43 $this->RxList = new RxList();
45 // Assign the CSRF_TOKEN_FORM
46 $this->assign("CSRF_TOKEN_FORM", collectCsrfToken());
48 if ($GLOBALS['inhouse_pharmacy']) {
49 // Make an array of drug IDs and selectors for the template.
50 $drug_array_values = array(0);
51 $drug_array_output = array("-- " . xl('or select from inventory') ." --");
52 $drug_attributes = '';
54 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
56 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
57 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
58 "t.period, t.quantity, t.refills, d.drug_code " .
59 "FROM drug_templates AS t, drugs AS d WHERE " .
60 "d.drug_id = t.drug_id ORDER BY t.selector");
62 while ($row = sqlFetchArray($res)) {
63 $tmp_output = $row['selector'];
64 if ($row['ndc_number']) {
65 $tmp_output .= ' [' . $row['ndc_number'] . ']';
68 $drug_array_values[] = $row['drug_id'];
69 $drug_array_output[] = $tmp_output;
70 if ($drug_attributes) {
71 $drug_attributes .= ',';
74 $drug_attributes .= "[" .
75 js_escape($row['name']) . "," . // 0
76 js_escape($row['form']) . "," . // 1
77 js_escape($row['dosage']) . "," . // 2
78 js_escape($row['size']) . "," . // 3
79 js_escape($row['unit']) . "," . // 4
80 js_escape($row['route']) . "," . // 5
81 js_escape($row['period']) . "," . // 6
82 js_escape($row['substitute']) . "," . // 7
83 js_escape($row['quantity']) . "," . // 8
84 js_escape($row['refills']) . "," . // 9
85 js_escape($row['quantity']) . "," . // 10 quantity per_refill
86 js_escape($row['drug_code']) . "]"; // 11 rxnorm drug code
89 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
90 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
91 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
95 function default_action()
97 $this->assign("prescription", $this->prescriptions[0]);
98 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
101 function edit_action($id = "", $patient_id = "", $p_obj = null)
104 if ($p_obj != null && get_class($p_obj) == "prescription") {
105 $this->prescriptions[0] = $p_obj;
106 } elseif (!is_object($this->prescriptions[0]) || get_class($this->prescriptions[0]) != "prescription") {
107 $this->prescriptions[0] = new Prescription($id);
110 if (!empty($patient_id)) {
111 $this->prescriptions[0]->set_patient_id($patient_id);
114 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
116 // If quantity to dispense is not already set from a POST, set its
117 // default value.
118 if (! $this->get_template_vars('DISP_QUANTITY')) {
119 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
122 $this->default_action();
125 function list_action($id, $sort = "")
127 if (empty($id)) {
128 $this->function_argument_error();
129 exit;
132 if (!empty($sort)) {
133 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
134 } else {
135 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
138 // Collect interactions if the global is turned on
139 if ($GLOBALS['rx_show_drug_drug']) {
140 $interaction = "";
141 // Ensure RxNorm installed
142 $rxn = sqlQuery("SELECT table_name FROM information_schema.tables WHERE table_name = 'RXNCONSO' OR table_name = 'rxconso'");
143 if ($rxn == false) {
144 $interaction = xlt("Could not find RxNorm Table! Please install.");
145 } elseif ($rxn == true) {
146 // Grab medication list from prescriptions list and load into array
147 $pid = $GLOBALS['pid'];
148 $medList = sqlStatement("SELECT drug FROM prescriptions WHERE active = 1 AND patient_id = ?", array($pid));
149 $nameList = array();
150 while ($name = sqlFetchArray($medList)) {
151 $drug = explode(" ", $name['drug']);
152 $rXn = sqlQuery("SELECT `rxcui` FROM `" . mitigateSqlTableUpperCase('RXNCONSO') . "` WHERE `str` LIKE ?", array("%" . $drug[0] . "%"));
153 $nameList[] = $rXn['rxcui'];
155 if (count($nameList) < 2) {
156 $interaction = xlt("Need more than one drug.");
157 } else {
158 // If there are drugs to compare, collect the data
159 // (array_filter removes empty items)
160 $rxcui_list = implode("+", array_filter($nameList));
161 // Do not urlencode the $rxcui_list, since this breaks the + items
162 $data = file_get_contents("https://rxnav.nlm.nih.gov/REST/interaction/list.json?rxcuis=" . $rxcui_list);
163 $json = json_decode($data, true);
164 if (!empty($json['fullInteractionTypeGroup'][0]['fullInteractionType'])) {
165 foreach ($json['fullInteractionTypeGroup'][0]['fullInteractionType'] as $item) {
166 $interaction .= '<div class="alert alert-danger">';
167 $interaction .= xlt('Comment') . ":" . text($item['comment']) . "</br>";
168 $interaction .= xlt('Drug1 Name{{Drug1 Interaction}}') . ":" . text($item['minConcept'][0]['name']) . "</br>";
169 $interaction .= xlt('Drug2 Name{{Drug2 Interaction}}') . ":" . text($item['minConcept'][1]['name']) . "</br>";
170 $interaction .= xlt('Severity') . ":" . text($item['interactionPair'][0]['severity']) . "</br>";
171 $interaction .= xlt('Description') . ":" . text($item['interactionPair'][0]['description']);
172 $interaction .= '</div>';
174 } else {
175 $interaction = xlt('No interactions found');
179 $this->assign("INTERACTION", $interaction);
182 // flag to indicate the CAMOS form is regsitered and active
183 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
185 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
188 function block_action($id, $sort = "")
190 if (empty($id)) {
191 $this->function_argument_error();
192 exit;
195 if (!empty($sort)) {
196 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
197 } else {
198 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
201 //print_r(Prescription::prescriptions_factory($id));
202 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
205 function fragment_action($id, $sort = "")
207 if (empty($id)) {
208 $this->function_argument_error();
209 exit;
212 if (!empty($sort)) {
213 $this->assign("prescriptions", Prescription::prescriptions_factory($id, $sort));
214 } else {
215 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
218 //print_r(Prescription::prescriptions_factory($id));
219 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
222 function lookup_action()
224 $this->do_lookup();
225 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
228 function edit_action_process()
230 if ($_POST['process'] != "true") {
231 return;
234 //print_r($_POST);
236 // Stupid Smarty code treats empty values as not specified values.
237 // Since active is a checkbox, represent the unchecked state as -1.
238 if (empty($_POST['active'])) {
239 $_POST['active'] = '-1';
241 if (!empty($_POST['start_date'])) {
242 $_POST['start_date'] = DateToYYYYMMDD($_POST['start_date']);
245 $this->prescriptions[0] = new Prescription($_POST['id']);
246 parent::populate_object($this->prescriptions[0]);
247 //echo $this->prescriptions[0]->toString(true);
248 $this->prescriptions[0]->persist();
249 $_POST['process'] = "";
251 $this->assign("GBL_CURRENCY_SYMBOL", $GLOBALS['gbl_currency_symbol']);
253 // If the "Prescribe and Dispense" button was clicked, then
254 // redisplay as in edit_action() but also replicate the fee and
255 // include a piece of javascript to call dispense().
257 if ($_POST['disp_button']) {
258 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
259 $this->assign("DISP_FEE", $_POST['disp_fee']);
260 $this->assign("ENDING_JAVASCRIPT", "dispense();");
261 $this->_state = false;
262 return $this->edit_action($this->prescriptions[0]->id);
265 // Set the AMC reporting flag (to record percentage of prescriptions that
266 // are set as e-prescriptions)
267 if (!(empty($_POST['escribe_flag']))) {
268 // add the e-prescribe flag
269 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
270 } else {
271 // remove the e-prescribe flag
272 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
275 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
276 if (!(empty($_POST['checked_formulary_flag']))) {
277 // add the e-prescribe flag
278 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
279 } else {
280 // remove the e-prescribe flag
281 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
284 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
285 if (!(empty($_POST['controlled_substance_flag']))) {
286 // add the e-prescribe flag
287 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
288 } else {
289 // remove the e-prescribe flag
290 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
293 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
294 // if ($this->prescriptions[0]->get_active() > 0) {
295 // return $this->send_action($this->prescriptions[0]->id);
296 // }
297 $this->list_action($this->prescriptions[0]->get_patient_id());
298 exit;
301 function send_action($id)
303 $_POST['process'] = "true";
304 if (empty($id)) {
305 $this->function_argument_error();
308 $rx = new Prescription($id);
309 // Populate pharmacy info if the patient has a default pharmacy.
310 // Probably the Prescription object should handle this instead, but
311 // doing it there will require more careful research and testing.
312 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
313 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
314 if ($prow['pharmacy_id']) {
315 $rx->pharmacy->set_id($prow['pharmacy_id']);
316 $rx->pharmacy->populate();
319 $this->assign("prescription", $rx);
321 $this->_state = false;
322 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
323 $this->template_mod . "_send.html");
326 function multiprintfax_header(& $pdf, $p)
328 return $this->multiprint_header($pdf, $p);
331 function multiprint_header(& $pdf, $p)
333 $this->providerid = $p->provider->id;
334 //print header
335 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'], '', '50', '', 'center', '');
336 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
337 $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 ='" .
338 add_escape_custom($p->provider->id) . "'");
339 $pdf->ezText($res['addr'], 12);
340 $my_y = $pdf->y;
341 $pdf->ezNewPage();
342 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>', 12);
343 // A client had a bad experience with a patient misusing a DEA number, so
344 // now the doctors write those in on printed prescriptions and only when
345 // necessary. If you need to change this back, then please make it a
346 // configurable option. Faxed prescriptions were not changed. -- Rod
347 // Now it is configureable. Change value in
348 // Administration->Globals->Rx
349 if ($GLOBALS['rx_enable_DEA']) {
350 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
351 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
352 } else {
353 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
357 if ($GLOBALS['rx_enable_NPI']) {
358 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
359 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
360 } else {
361 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
365 if ($GLOBALS['rx_enable_SLN']) {
366 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
367 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
368 } else {
369 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
373 $pdf->ezColumnsStop();
374 if ($my_y < $pdf->y) {
375 $pdf->ezSetY($my_y);
378 $pdf->ezText('', 10);
379 $pdf->setLineStyle(1);
380 $pdf->ezColumnsStart(array('num'=>2));
381 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
382 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>', 6);
383 $pdf->ezText($p->patient->get_name_display(), 10);
384 $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));
385 $pdf->ezText($res['addr']);
386 $my_y = $pdf->y;
387 $pdf->ezNewPage();
388 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
389 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>', 6);
390 $pdf->ezText($p->patient->date_of_birth, 10);
391 $pdf->ezText('');
392 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
393 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>', 6);
394 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT), 10);
395 $pdf->ezColumnsStop();
396 if ($my_y < $pdf->y) {
397 $pdf->ezSetY($my_y);
400 $pdf->ezText('');
401 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
402 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>', 6);
403 $pdf->ezText('', 10);
406 function multiprintcss_header($p)
408 echo("<div class='paddingdiv'>\n");
409 $this->providerid = $p->provider->id;
410 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
411 echo ("<tr>\n");
412 echo ("<td></td>\n");
413 echo ("<td>\n");
414 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
415 echo ("</td>\n");
416 echo ("</tr>\n");
417 echo ("<tr>\n");
418 echo ("<td>\n");
419 $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) . "'");
420 if (!empty($res)) {
421 $patterns = array ('/\n/','/Tel:/','/Fax:/');
422 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
423 $res = preg_replace($patterns, $replace, $res);
426 echo ('<span class="large">' . $res['addr'] . '</span>');
427 echo ("</td>\n");
428 echo ("<td>\n");
429 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
431 if ($GLOBALS['rx_enable_DEA']) {
432 if ($GLOBALS['rx_show_DEA']) {
433 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
434 } else {
435 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
439 if ($GLOBALS['rx_enable_NPI']) {
440 if ($GLOBALS['rx_show_NPI']) {
441 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
442 } else {
443 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
447 if ($GLOBALS['rx_enable_SLN']) {
448 if ($GLOBALS['rx_show_SLN']) {
449 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
450 } else {
451 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
455 echo ("</td>\n");
456 echo ("</tr>\n");
457 echo ("<tr>\n");
458 echo ("<td rowspan='2' class='bordered'>\n");
459 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
460 echo ($p->patient->get_name_display() . '<br>');
461 $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));
462 if (!empty($res)) {
463 $patterns = array ('/\n/');
464 $replace = array ('<br>');
465 $res = preg_replace($patterns, $replace, $res);
468 echo ($res['addr']);
469 echo ("</td>\n");
470 echo ("<td class='bordered'>\n");
471 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
472 echo ($p->patient->date_of_birth );
473 echo ("</td>\n");
474 echo ("</tr>\n");
475 echo ("<tr>\n");
476 echo ("<td class='bordered'>\n");
477 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
478 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
479 echo ("</td>\n");
480 echo ("</tr>\n");
481 echo ("<tr>\n");
482 echo ("<td colspan='2' class='bordered'>\n");
483 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
484 echo ("</td>\n");
485 echo ("</tr>\n");
486 echo ("</table>\n");
489 function multiprintcss_preheader()
491 // this sets styling and other header information of the multiprint css sheet
492 echo ("<html>\n");
493 echo ("<head>\n");
494 echo ("<style>\n");
495 echo ("div {\n");
496 echo (" padding: 0;\n");
497 echo (" margin: 0;\n");
498 echo ("}\n");
499 echo ("body {\n");
500 echo (" font-family: sans-serif;\n");
501 echo (" font-weight: normal;\n");
502 echo (" font-size: 10pt;\n");
503 echo (" background: white;\n");
504 echo (" color: black;\n");
505 echo ("}\n");
506 echo ("span.large {\n");
507 echo (" font-size: 12pt;\n");
508 echo ("}\n");
509 echo ("span.small {\n");
510 echo (" font-size: 6pt;\n");
511 echo ("}\n");
512 echo ("td {\n");
513 echo (" vertical-align: top;\n");
514 echo (" width: 50%;\n");
515 echo (" font-size: 10pt;\n");
516 echo (" padding-bottom: 8pt;\n");
517 echo ("}\n");
518 echo ("td.bordered {\n");
519 echo (" border-top:1pt solid black;\n");
520 echo ("}\n");
521 echo ("div.paddingdiv {\n");
522 echo (" width: 524pt;\n");
523 echo (" height: 668pt;\n");
524 echo ("}\n");
525 echo ("div.scriptdiv {\n");
526 echo (" padding-top: 12pt;\n");
527 echo (" padding-bottom: 22pt;\n");
528 echo (" padding-left: 35pt;\n");
529 echo (" border-bottom:1pt solid black;\n");
530 echo ("}\n");
531 echo ("div.signdiv {\n");
532 echo (" margin-top: 40pt;\n");
533 echo (" font-size: 12pt;\n");
534 echo ("}\n");
535 echo ("</style>\n");
537 echo ("<title>" . xl('Prescription') . "</title>\n");
538 echo ("</head>\n");
539 echo ("<body>\n");
542 function multiprintfax_footer(& $pdf)
544 return $this->multiprint_footer($pdf);
547 function multiprint_footer(& $pdf)
549 if ($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax )) {
550 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
551 if (file_exists($sigfile)) {
552 $pdf->ezText(xl('Signature') . ": ", 12);
553 // $pdf->ezImage($sigfile, "", "", "none", "left");
554 $pdf->ezImage($sigfile, "", "", "none", "center");
555 $pdf->ezText(xl('Date') . ": " . date('Y-m-d'), 12);
556 if ($this->is_print_to_fax) {
557 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
560 $addenumFile = $this->pconfig['addendum_file'];
561 if (file_exists($addenumFile)) {
562 $pdf->ezText('');
563 $f = fopen($addenumFile, "r");
564 while ($line = fgets($f, 1000)) {
565 $pdf->ezText(rtrim($line));
569 return;
573 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'), 12);
576 function multiprintcss_footer()
578 echo ("<div class='signdiv'>\n");
579 echo (xl('Signature') . ":________________________________<br>");
580 echo (xl('Date') . ": " . date('Y-m-d'));
581 echo ("</div>\n");
582 echo ("</div>\n");
585 function multiprintcss_postfooter()
587 echo("<script language='JavaScript'>\n");
588 echo("opener.top.printLogPrint(window);\n");
589 echo("</script>\n");
590 echo("</body>\n");
591 echo("</html>\n");
594 function get_prescription_body_text($p)
596 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
597 if ($p->get_form()) {
598 $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
601 $body .= "</b> <i>" .
602 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
603 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
604 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
605 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
606 if ($p->get_refills() > 0) {
607 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
608 if ($p->get_per_refill()) {
609 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
612 $body .= "</u>\n";
613 } else {
614 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
617 $note = $p->get_note();
618 if ($note != '') {
619 $body .= "\n" . text($note) . "\n";
622 return $body;
625 function multiprintfax_body(& $pdf, $p)
627 return $this->multiprint_body($pdf, $p);
630 function multiprint_body(& $pdf, $p)
632 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
633 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
634 $d = $this->get_prescription_body_text($p);
635 if ($pdf->ezText($d, 10, array(), 1)) {
636 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
637 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
638 $this->multiprint_footer($pdf);
639 $pdf->ezNewPage();
640 $this->multiprint_header($pdf, $p);
641 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
642 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
645 $my_y = $pdf->y;
646 $pdf->ezText($d, 10);
647 if ($this->pconfig['shading']) {
648 $pdf->setColor(.9, .9, .9);
649 $pdf->filledRectangle($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'], $my_y - $pdf->y);
650 $pdf->setColor(0, 0, 0);
653 $pdf->ezSetY($my_y);
654 $pdf->ezText($d, 10);
655 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
656 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
657 $pdf->ezText('');
658 $pdf->line($pdf->ez['leftMargin'], $pdf->y, $pdf->ez['pageWidth']-$pdf->ez['rightMargin'], $pdf->y);
659 $pdf->ezText('');
662 function multiprintcss_body($p)
664 $d = $this->get_prescription_body_text($p);
665 $patterns = array ('/\n/','/ /');
666 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
667 $d = preg_replace($patterns, $replace, $d);
668 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
671 function multiprintfax_action($id = "")
673 $this->is_print_to_fax=true;
674 return $this->multiprint_action($id);
677 function multiprint_action($id = "")
679 $_POST['process'] = "true";
680 if (empty($id)) {
681 $this->function_argument_error();
684 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
685 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
686 $pdf->selectFont('Helvetica');
688 // $print_header = true;
689 $on_this_page = 0;
691 //print prescriptions body
692 $this->_state = false; // Added by Rod - see Controller.class.php
693 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
694 foreach ($ids as $id) {
695 $p = new Prescription($id);
696 // if ($print_header == true) {
697 if ($on_this_page == 0) {
698 $this->multiprint_header($pdf, $p);
701 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
702 $this->multiprint_footer($pdf);
703 $pdf->ezNewPage();
704 $this->multiprint_header($pdf, $p);
705 // $print_header = false;
706 $on_this_page = 1;
709 $this->multiprint_body($pdf, $p);
712 $this->multiprint_footer($pdf);
714 $pFirstName = $p->patient->fname; //modified by epsdky for prescription title change to include patient name and ID
715 $pFName = convert_safe_file_dir_name($pFirstName);
716 $modedFileName = "Rx_{$pFName}_{$p->patient->id}.pdf";
718 $pdf->ezStream(array('Content-Disposition' => $modedFileName));
719 return;
722 function multiprintcss_action($id = "")
724 $_POST['process'] = "true";
725 if (empty($id)) {
726 $this->function_argument_error();
729 $this->multiprintcss_preheader();
731 $this->_state = false; // Added by Rod - see Controller.class.php
732 $ids = preg_split('/::/', substr($id, 1, strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
734 $on_this_page = 0;
735 foreach ($ids as $id) {
736 $p = new Prescription($id);
737 if ($on_this_page == 0) {
738 $this->multiprintcss_header($p);
741 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
742 $this->multiprintcss_footer();
743 $this->multiprintcss_header($p);
744 $on_this_page = 1;
747 $this->multiprintcss_body($p);
750 $this->multiprintcss_footer();
751 $this->multiprintcss_postfooter();
752 return;
755 function send_action_process($id)
757 $dummy = ""; // Added by Rod to avoid run-time warnings
758 if ($_POST['process'] != "true") {
759 return;
762 if (empty($id)) {
763 $this->function_argument_error();
766 $p = new Prescription($id);
767 switch ($_POST['submit']) {
768 case (xl("Print")." (".xl("PDF").")"):
769 // The following statement added by Rod.
770 // Looking at Controller.class.php, it appears that _state is set to false
771 // to indicate that no further HTML is to be generated.
772 $this->_state = false; // Added by Rod - see Controller.class.php
773 return $this->_print_prescription($p, $dummy);
774 break;
775 case (xl("Print")." (".xl("HTML").")"):
776 $this->_state = false;
777 return $this->_print_prescription_css($p, $dummy);
778 break;
779 case xl("Print To Fax"):
780 $this->_state = false;
781 $this->is_print_to_fax = true;
782 return $this->_print_prescription($p, $dummy);
783 break;
784 case xl("Email"):
785 return $this->_email_prescription($p, $_POST['email_to']);
786 break;
787 case xl("Fax"):
788 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
789 //$this->assign("process_result","No fax server is currently setup.");
790 return $this->_fax_prescription($p, $_POST['fax_to']);
791 break;
792 case xl("Auto Send"):
793 $pharmacy_id = $_POST['pharmacy_id'];
794 //echo "auto sending to : " . $_POST['pharmacy_id'];
795 $phar = new Pharmacy($_POST['pharmacy_id']);
796 //print_r($phar);
797 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
798 return $this->_print_prescription($p, $dummy);
799 } elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
800 $email = $phar->get_email();
801 if (!empty($email)) {
802 return $this->_email_prescription($p, $phar->get_email());
805 //else print it
806 } elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
807 $faxNum= $phar->get_fax();
808 if (!empty($faxNum)) {
809 return $this->_fax_prescription($p, $faxNum);
812 // return $this->assign("process_result","No fax server is currently setup.");
813 // else default is printing,
814 } else {
815 //the pharmacy has no default or default is print
816 return $this->_print_prescription($p, $dummy);
818 break;
821 return;
824 function _print_prescription($p, & $toFile)
826 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
827 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
829 $pdf->selectFont('Helvetica');
831 // Signature images are to be used only when faxing.
832 if (!empty($toFile)) {
833 $this->is_faxing = true;
836 $this->multiprint_header($pdf, $p);
837 $this->multiprint_body($pdf, $p);
838 $this->multiprint_footer($pdf);
840 if (!empty($toFile)) {
841 $toFile = $pdf->ezOutput();
842 } else {
843 $pdf->ezStream();
844 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
847 return;
850 function _print_prescription_css($p, & $toFile)
853 $this->multiprintcss_preheader();
854 $this->multiprintcss_header($p);
855 $this->multiprintcss_body($p);
856 $this->multiprintcss_footer();
857 $this->multiprintcss_postfooter();
860 function _print_prescription_old($p, & $toFile)
862 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
863 $pdf->ezSetMargins($GLOBALS['rx_top_margin'], $GLOBALS['rx_bottom_margin'], $GLOBALS['rx_left_margin'], $GLOBALS['rx_right_margin']);
864 $pdf->selectFont('Helvetica');
865 if (!empty($this->pconfig['logo'])) {
866 $pdf->ezImage($this->pconfig['logo'], "", "", "none", "left");
869 $pdf->ezText($p->get_prescription_display(), 10);
870 if ($this->pconfig['use_signature']) {
871 $pdf->ezImage($this->pconfig['signature'], "", "", "none", "left");
872 } else {
873 $pdf->ezText("\n\n\n\nSignature:________________________________", 10);
876 if (!empty($toFile)) {
877 $toFile = $pdf->ezOutput();
878 } else {
879 $pdf->ezStream();
880 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
883 return;
886 function _email_prescription($p, $email)
888 if (empty($email)) {
889 $this->assign("process_result", "Email could not be sent, the address supplied: '$email' was empty or invalid.");
890 return;
893 $mail = new PHPMailer();
894 //this is a temporary config item until the rest of the per practice billing settings make their way in
895 $mail->From = $GLOBALS['practice_return_email_path'];
896 $mail->FromName = $p->provider->get_name_display();
897 $mail->isMail();
898 $mail->Host = "localhost";
899 $mail->Mailer = "mail";
900 $text_body = $p->get_prescription_display();
901 $mail->Body = $text_body;
902 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
903 $mail->AddAddress($email);
904 if ($mail->Send()) {
905 $this->assign("process_result", "Email was successfully sent to: " . $email);
906 return;
907 } else {
908 $this->assign("process_result", "There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
909 return;
913 function do_lookup()
915 if ($_POST['process'] != "true") {
916 // don't do a lookup
917 $this->assign("drug", $_GET['drug']);
918 return;
921 // process the lookup
922 $this->assign("drug", $_POST['drug']);
923 $list = array();
924 if (!empty($_POST['drug'])) {
925 $list = $this->RxList->get_list($_POST['drug']);
928 if (is_array($list)) {
929 $list = array_flip($list);
930 $this->assign("drug_options", $list);
931 $this->assign("drug_values", array_keys($list));
932 } else {
933 $this->assign("NO_RESULTS", "No results found for: " .$_POST['drug'] . "<br />");
936 //print_r($_POST);
937 //$this->assign("PROCESS","");
939 $_POST['process'] = "";
942 function _fax_prescription($p, $faxNum)
944 $err = "Sent fax";
945 //strip - ,(, ), and ws
946 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/", "", $faxNum);
947 //validate the number
949 if (!empty($faxNum) && is_numeric($faxNum)) {
950 //get the sendfax command and execute it
951 $cmd = $this->pconfig['sendfax'];
952 // prepend any prefix to the fax number
953 $pref=$this->pconfig['prefix'];
954 $faxNum=$pref.$faxNum;
955 if (empty($cmd)) {
956 $err .= " Send fax not set in includes/config.php";
957 } else {
958 //generate file to fax
959 $faxFile = "Failed";
960 $this->_print_prescription($p, $faxFile);
961 if (empty($faxFile)) {
962 $err .= " _print_prescription returned empty file";
965 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
966 $p->get_patient_id() . "_fax_.pdf";
967 //print "filename is $fileName";
968 touch($fileName); // php bug
969 $handle = fopen($fileName, "w");
970 if (!$handle) {
971 $err .= " Failed to open file $fileName to write fax to";
974 if (fwrite($handle, $faxFile) === false) {
975 $err .= " Failed to write data to $fileName";
978 fclose($handle);
979 $args = " -n -d $faxNum $fileName";
980 //print "command is $cmd $args<br>";
981 exec($cmd . $args);
983 } else {
984 $err = "bad fax number passed to function";
987 if ($err) {
988 $this->assign("process_result", $err);