Added access controls for encounter categories
[openemr.git] / controllers / C_Prescription.class.php
blobb00ad3eb265017c618c1ba2cfcd3768acad12fac
1 <?php
2 /**
3 * This script print Prescriptions.
5 * Copyright (C) 2015 Roberto Vasquez <robertogagliotta@gmail.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Roberto Vasquez <robertogagliotta@gmail.com>
20 * @link http://www.open-emr.org
24 require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
25 require_once($GLOBALS['fileroot'] . "/library/registry.inc");
26 require_once($GLOBALS['fileroot'] . "/library/amc.php");
28 class C_Prescription extends Controller {
30 var $template_mod;
31 var $pconfig;
32 var $providerid = 0;
33 var $is_faxing = false;
34 var $is_print_to_fax = false;
36 function __construct($template_mod = "general")
38 parent::__construct();
40 $this->template_mod = $template_mod;
41 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
42 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
43 $this->assign("STYLE", $GLOBALS['style']);
44 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
45 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
46 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
47 $this->assign("CSS_HEADER", $GLOBALS['css_header'] );
48 $this->assign("WEB_ROOT", $GLOBALS['webroot'] );
49 $this->RxList = new RxList();
51 if ($GLOBALS['inhouse_pharmacy']) {
52 // Make an array of drug IDs and selectors for the template.
53 $drug_array_values = array(0);
54 $drug_array_output = array("-- " . xl('or select from inventory') ." --");
55 $drug_attributes = '';
57 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
59 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
60 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
61 "t.period, t.quantity, t.refills, d.drug_code " .
62 "FROM drug_templates AS t, drugs AS d WHERE " .
63 "d.drug_id = t.drug_id ORDER BY t.selector");
65 while ($row = sqlFetchArray($res)) {
66 $tmp_output = $row['selector'];
67 if ($row['ndc_number']) {
68 $tmp_output .= ' [' . $row['ndc_number'] . ']';
70 $drug_array_values[] = $row['drug_id'];
71 $drug_array_output[] = $tmp_output;
72 if ($drug_attributes) $drug_attributes .= ',';
73 $drug_attributes .= "['" .
74 $row['name'] . "'," . // 0
75 $row['form'] . ",'" . // 1
76 $row['dosage'] . "','" . // 2
77 $row['size'] . "'," . // 3
78 $row['unit'] . "," . // 4
79 $row['route'] . "," . // 5
80 $row['period'] . "," . // 6
81 $row['substitute'] . "," . // 7
82 $row['quantity'] . "," . // 8
83 $row['refills'] . "," . // 9
84 $row['quantity'] . "," . // 10 quantity per_refill
85 $row['drug_code'] . "]"; // 11 rxnorm drug code
87 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
88 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
89 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
93 function default_action()
95 $this->assign("prescription",$this->prescriptions[0]);
96 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
99 function edit_action($id = "",$patient_id="",$p_obj = null)
102 if ($p_obj != null && get_class($p_obj) == "prescription") {
103 $this->prescriptions[0] = $p_obj;
105 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
106 $this->prescriptions[0] = new Prescription($id);
109 if (!empty($patient_id)) {
110 $this->prescriptions[0]->set_patient_id($patient_id);
113 // If quantity to dispense is not already set from a POST, set its
114 // default value.
115 if (! $this->get_template_vars('DISP_QUANTITY')) {
116 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
119 $this->default_action();
122 function list_action($id,$sort = "")
124 if (empty($id)) {
125 $this->function_argument_error();
126 exit;
128 if (!empty($sort)) {
129 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
131 else {
132 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
135 // flag to indicate the CAMOS form is regsitered and active
136 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
138 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
141 function block_action($id,$sort = "")
143 if (empty($id)) {
144 $this->function_argument_error();
145 exit;
147 if (!empty($sort)) {
148 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
150 else {
151 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
153 //print_r(Prescription::prescriptions_factory($id));
154 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
157 function fragment_action($id,$sort = "")
159 if (empty($id)) {
160 $this->function_argument_error();
161 exit;
163 if (!empty($sort)) {
164 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
166 else {
167 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
169 //print_r(Prescription::prescriptions_factory($id));
170 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
173 function lookup_action()
175 $this->do_lookup();
176 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
179 function edit_action_process()
181 if ($_POST['process'] != "true")
182 return;
183 //print_r($_POST);
185 // Stupid Smarty code treats empty values as not specified values.
186 // Since active is a checkbox, represent the unchecked state as -1.
187 if (empty($_POST['active'])) $_POST['active'] = '-1';
189 $this->prescriptions[0] = new Prescription($_POST['id']);
190 parent::populate_object($this->prescriptions[0]);
191 //echo $this->prescriptions[0]->toString(true);
192 $this->prescriptions[0]->persist();
193 $_POST['process'] = "";
195 // If the "Prescribe and Dispense" button was clicked, then
196 // redisplay as in edit_action() but also replicate the fee and
197 // include a piece of javascript to call dispense().
199 if ($_POST['disp_button']) {
200 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
201 $this->assign("DISP_FEE", $_POST['disp_fee']);
202 $this->assign("ENDING_JAVASCRIPT", "dispense();");
203 $this->_state = false;
204 return $this->edit_action($this->prescriptions[0]->id);
207 // Set the AMC reporting flag (to record percentage of prescriptions that
208 // are set as e-prescriptions)
209 if (!(empty($_POST['escribe_flag']))) {
210 // add the e-prescribe flag
211 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
213 else {
214 // remove the e-prescribe flag
215 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
218 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
219 if (!(empty($_POST['checked_formulary_flag']))) {
220 // add the e-prescribe flag
221 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
223 else {
224 // remove the e-prescribe flag
225 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
228 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
229 if (!(empty($_POST['controlled_substance_flag']))) {
230 // add the e-prescribe flag
231 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
233 else {
234 // remove the e-prescribe flag
235 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
238 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
239 // if ($this->prescriptions[0]->get_active() > 0) {
240 // return $this->send_action($this->prescriptions[0]->id);
241 // }
242 $this->list_action($this->prescriptions[0]->get_patient_id());
243 exit;
246 function send_action($id)
248 $_POST['process'] = "true";
249 if(empty($id)) {
250 $this->function_argument_error();
253 $rx = new Prescription($id);
254 // Populate pharmacy info if the patient has a default pharmacy.
255 // Probably the Prescription object should handle this instead, but
256 // doing it there will require more careful research and testing.
257 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
258 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
259 if ($prow['pharmacy_id']) {
260 $rx->pharmacy->set_id($prow['pharmacy_id']);
261 $rx->pharmacy->populate();
263 $this->assign("prescription", $rx);
265 $this->_state = false;
266 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
267 $this->template_mod . "_send.html");
270 function multiprintfax_header(& $pdf, $p)
272 return $this->multiprint_header( $pdf, $p );
275 function multiprint_header(& $pdf, $p)
277 $this->providerid = $p->provider->id;
278 //print header
279 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'],'','50','','center','');
280 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
281 $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 ='" .
282 add_escape_custom($p->provider->id) . "'");
283 $pdf->ezText($res['addr'],12);
284 $my_y = $pdf->y;
285 $pdf->ezNewPage();
286 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
287 // A client had a bad experience with a patient misusing a DEA number, so
288 // now the doctors write those in on printed prescriptions and only when
289 // necessary. If you need to change this back, then please make it a
290 // configurable option. Faxed prescriptions were not changed. -- Rod
291 // Now it is configureable. Change value in
292 // Administration->Globals->Rx
293 if ($GLOBALS['rx_enable_DEA']) {
294 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
295 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
297 else {
298 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
302 if ($GLOBALS['rx_enable_NPI']) {
303 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
304 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
306 else {
307 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
310 if ($GLOBALS['rx_enable_SLN']) {
311 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
312 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
314 else {
315 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
318 $pdf->ezColumnsStop();
319 if ($my_y < $pdf->y){
320 $pdf->ezSetY($my_y);
322 $pdf->ezText('',10);
323 $pdf->setLineStyle(1);
324 $pdf->ezColumnsStart(array('num'=>2));
325 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
326 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
327 $pdf->ezText($p->patient->get_name_display(),10);
328 $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));
329 $pdf->ezText($res['addr']);
330 $my_y = $pdf->y;
331 $pdf->ezNewPage();
332 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
333 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
334 $pdf->ezText($p->patient->date_of_birth,10);
335 $pdf->ezText('');
336 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
337 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
338 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
339 $pdf->ezColumnsStop();
340 if ($my_y < $pdf->y){
341 $pdf->ezSetY($my_y);
343 $pdf->ezText('');
344 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
345 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
346 $pdf->ezText('',10);
349 function multiprintcss_header($p)
351 echo("<div class='paddingdiv'>\n");
352 $this->providerid = $p->provider->id;
353 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
354 echo ("<tr>\n");
355 echo ("<td></td>\n");
356 echo ("<td>\n");
357 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
358 echo ("</td>\n");
359 echo ("</tr>\n");
360 echo ("<tr>\n");
361 echo ("<td>\n");
362 $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) . "'");
363 if (!empty($res)) {
364 $patterns = array ('/\n/','/Tel:/','/Fax:/');
365 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
366 $res = preg_replace($patterns, $replace, $res);
368 echo ('<span class="large">' . $res['addr'] . '</span>');
369 echo ("</td>\n");
370 echo ("<td>\n");
371 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
373 if ($GLOBALS['rx_enable_DEA']) {
374 if ($GLOBALS['rx_show_DEA']) {
375 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
377 else {
378 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
381 if ($GLOBALS['rx_enable_NPI']) {
382 if ($GLOBALS['rx_show_NPI']) {
383 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
385 else {
386 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
389 if ($GLOBALS['rx_enable_SLN']) {
390 if ($GLOBALS['rx_show_SLN']) {
391 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
393 else {
394 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
397 echo ("</td>\n");
398 echo ("</tr>\n");
399 echo ("<tr>\n");
400 echo ("<td rowspan='2' class='bordered'>\n");
401 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
402 echo ($p->patient->get_name_display() . '<br>');
403 $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));
404 if (!empty($res)) {
405 $patterns = array ('/\n/');
406 $replace = array ('<br>');
407 $res = preg_replace($patterns, $replace, $res);
409 echo ($res['addr']);
410 echo ("</td>\n");
411 echo ("<td class='bordered'>\n");
412 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
413 echo ($p->patient->date_of_birth );
414 echo ("</td>\n");
415 echo ("</tr>\n");
416 echo ("<tr>\n");
417 echo ("<td class='bordered'>\n");
418 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
419 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
420 echo ("</td>\n");
421 echo ("</tr>\n");
422 echo ("<tr>\n");
423 echo ("<td colspan='2' class='bordered'>\n");
424 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
425 echo ("</td>\n");
426 echo ("</tr>\n");
427 echo ("</table>\n");
430 function multiprintcss_preheader()
432 // this sets styling and other header information of the multiprint css sheet
433 echo ("<html>\n");
434 echo ("<head>\n");
435 echo ("<style>\n");
436 echo ("div {\n");
437 echo (" padding: 0;\n");
438 echo (" margin: 0;\n");
439 echo ("}\n");
440 echo ("body {\n");
441 echo (" font-family: sans-serif;\n");
442 echo (" font-weight: normal;\n");
443 echo (" font-size: 10pt;\n");
444 echo (" background: white;\n");
445 echo (" color: black;\n");
446 echo ("}\n");
447 echo ("span.large {\n");
448 echo (" font-size: 12pt;\n");
449 echo ("}\n");
450 echo ("span.small {\n");
451 echo (" font-size: 6pt;\n");
452 echo ("}\n");
453 echo ("td {\n");
454 echo (" vertical-align: top;\n");
455 echo (" width: 50%;\n");
456 echo (" font-size: 10pt;\n");
457 echo (" padding-bottom: 8pt;\n");
458 echo ("}\n");
459 echo ("td.bordered {\n");
460 echo (" border-top:1pt solid black;\n");
461 echo ("}\n");
462 echo ("div.paddingdiv {\n");
463 echo (" width: 524pt;\n");
464 echo (" height: 668pt;\n");
465 echo ("}\n");
466 echo ("div.scriptdiv {\n");
467 echo (" padding-top: 12pt;\n");
468 echo (" padding-bottom: 22pt;\n");
469 echo (" padding-left: 35pt;\n");
470 echo (" border-bottom:1pt solid black;\n");
471 echo ("}\n");
472 echo ("div.signdiv {\n");
473 echo (" margin-top: 40pt;\n");
474 echo (" font-size: 12pt;\n");
475 echo ("}\n");
476 echo ("</style>\n");
478 echo ("<title>" . xl('Prescription') . "</title>\n");
479 echo ("</head>\n");
480 echo ("<body>\n");
483 function multiprintfax_footer( & $pdf )
485 return $this->multiprint_footer( $pdf );
488 function multiprint_footer(& $pdf)
490 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
491 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
492 if (file_exists($sigfile)) {
493 $pdf->ezText( xl('Signature') . ": ",12);
494 // $pdf->ezImage($sigfile, "", "", "none", "left");
495 $pdf->ezImage($sigfile, "", "", "none", "center");
496 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
497 if ( $this->is_print_to_fax ) {
498 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
501 $addenumFile = $this->pconfig['addendum_file'];
502 if ( file_exists( $addenumFile ) ) {
503 $pdf->ezText('');
504 $f = fopen($addenumFile, "r");
505 while ( $line = fgets($f, 1000) ) {
506 $pdf->ezText(rtrim($line));
510 return;
513 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
516 function multiprintcss_footer()
518 echo ("<div class='signdiv'>\n");
519 echo (xl('Signature') . ":________________________________<br>");
520 echo (xl('Date') . ": " . date('Y-m-d'));
521 echo ("</div>\n");
522 echo ("</div>\n");
525 function multiprintcss_postfooter()
527 echo("<script language='JavaScript'>\n");
528 echo("opener.top.printLogPrint(window);\n");
529 echo("</script>\n");
530 echo("</body>\n");
531 echo("</html>\n");
534 function get_prescription_body_text($p)
536 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
537 if ($p->get_form()) $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
538 $body .= "</b> <i>" .
539 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
540 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
541 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
542 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
543 if ($p->get_refills() > 0) {
544 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
545 if ($p->get_per_refill()) {
546 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
548 $body .= "</u>\n";
550 else {
551 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
553 $note = $p->get_note();
554 if ($note != '') {
555 $body .= "\n" . text($note) . "\n";
557 return $body;
560 function multiprintfax_body(& $pdf, $p)
562 return $this->multiprint_body( $pdf, $p );
565 function multiprint_body(& $pdf, $p)
567 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
568 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
569 $d = $this->get_prescription_body_text($p);
570 if ( $pdf->ezText($d,10,array(),1) ) {
571 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
572 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
573 $this->multiprint_footer($pdf);
574 $pdf->ezNewPage();
575 $this->multiprint_header($pdf, $p);
576 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
577 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
579 $my_y = $pdf->y;
580 $pdf->ezText($d,10);
581 if($this->pconfig['shading']) {
582 $pdf->setColor(.9,.9,.9);
583 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
584 $pdf->setColor(0,0,0);
586 $pdf->ezSetY($my_y);
587 $pdf->ezText($d,10);
588 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
589 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
590 $pdf->ezText('');
591 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
592 $pdf->ezText('');
595 function multiprintcss_body($p)
597 $d = $this->get_prescription_body_text($p);
598 $patterns = array ('/\n/','/ /');
599 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
600 $d = preg_replace($patterns, $replace, $d);
601 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
604 function multiprintfax_action($id = "")
606 $this->is_print_to_fax=true;
607 return $this->multiprint_action( $id );
610 function multiprint_action($id = "")
612 $_POST['process'] = "true";
613 if(empty($id)) {
614 $this->function_argument_error();
616 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
617 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
618 ,$GLOBALS['rx_bottom_margin']
619 ,$GLOBALS['rx_left_margin']
620 ,$GLOBALS['rx_right_margin']
622 $pdf->selectFont('Helvetica');
624 // $print_header = true;
625 $on_this_page = 0;
627 //print prescriptions body
628 $this->_state = false; // Added by Rod - see Controller.class.php
629 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
630 foreach ($ids as $id) {
631 $p = new Prescription($id);
632 // if ($print_header == true) {
633 if ($on_this_page == 0) {
634 $this->multiprint_header($pdf, $p);
636 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
637 $this->multiprint_footer($pdf);
638 $pdf->ezNewPage();
639 $this->multiprint_header($pdf, $p);
640 // $print_header = false;
641 $on_this_page = 1;
643 $this->multiprint_body($pdf, $p);
646 $this->multiprint_footer($pdf);
648 $pFirstName = $p->patient->fname; //modified by epsdky for prescription title change to include patient name and ID
649 $pFName = convert_safe_file_dir_name($pFirstName);
650 $modedFileName = "Rx_{$pFName}_{$p->patient->id}.pdf";
652 $pdf->ezStream(array('Content-Disposition' => $modedFileName));
653 return;
656 function multiprintcss_action($id = "")
658 $_POST['process'] = "true";
659 if(empty($id)) {
660 $this->function_argument_error();
663 $this->multiprintcss_preheader();
665 $this->_state = false; // Added by Rod - see Controller.class.php
666 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
668 $on_this_page = 0;
669 foreach ($ids as $id) {
670 $p = new Prescription($id);
671 if ($on_this_page == 0) {
672 $this->multiprintcss_header($p);
674 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
675 $this->multiprintcss_footer();
676 $this->multiprintcss_header($p);
677 $on_this_page = 1;
679 $this->multiprintcss_body($p);
681 $this->multiprintcss_footer();
682 $this->multiprintcss_postfooter();
683 return;
686 function send_action_process($id)
688 $dummy = ""; // Added by Rod to avoid run-time warnings
689 if ($_POST['process'] != "true")
690 return;
691 if(empty($id)) {
692 $this->function_argument_error();
694 $p = new Prescription($id);
695 switch ($_POST['submit']) {
697 case (xl("Print")." (".xl("PDF").")"):
698 // The following statement added by Rod.
699 // Looking at Controller.class.php, it appears that _state is set to false
700 // to indicate that no further HTML is to be generated.
701 $this->_state = false; // Added by Rod - see Controller.class.php
702 return $this->_print_prescription($p, $dummy);
703 break;
704 case (xl("Print")." (".xl("HTML").")"):
705 $this->_state = false;
706 return $this->_print_prescription_css($p, $dummy);
707 break;
708 case xl("Print To Fax"):
709 $this->_state = false;
710 $this->is_print_to_fax = true;
711 return $this->_print_prescription($p, $dummy);
712 break;
713 case xl("Email"):
714 return $this->_email_prescription($p,$_POST['email_to']);
715 break;
716 case xl("Fax"):
717 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
718 //$this->assign("process_result","No fax server is currently setup.");
719 return $this->_fax_prescription($p,$_POST['fax_to']);
720 break;
721 case xl("Auto Send"):
722 $pharmacy_id = $_POST['pharmacy_id'];
723 //echo "auto sending to : " . $_POST['pharmacy_id'];
724 $phar = new Pharmacy($_POST['pharmacy_id']);
725 //print_r($phar);
726 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
727 return $this->_print_prescription($p, $dummy);
729 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
730 $email = $phar->get_email();
731 if (!empty($email)) {
732 return $this->_email_prescription($p,$phar->get_email());
734 //else print it
736 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
737 $faxNum= $phar->get_fax();
738 if(!empty($faxNum)) {
739 return $this->_fax_prescription ($p,$faxNum);
741 // return $this->assign("process_result","No fax server is currently setup.");
742 // else default is printing,
744 else {
745 //the pharmacy has no default or default is print
746 return $this->_print_prescription($p, $dummy);
748 break;
751 return;
755 function _print_prescription($p, & $toFile)
757 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
758 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
759 ,$GLOBALS['rx_bottom_margin']
760 ,$GLOBALS['rx_left_margin']
761 ,$GLOBALS['rx_right_margin']
764 $pdf->selectFont('Helvetica');
766 // Signature images are to be used only when faxing.
767 if(!empty($toFile)) $this->is_faxing = true;
769 $this->multiprint_header($pdf, $p);
770 $this->multiprint_body($pdf, $p);
771 $this->multiprint_footer($pdf);
773 if(!empty($toFile)) {
774 $toFile = $pdf->ezOutput();
776 else {
777 $pdf->ezStream();
778 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
780 return;
783 function _print_prescription_css($p, & $toFile)
786 $this->multiprintcss_preheader();
787 $this->multiprintcss_header($p);
788 $this->multiprintcss_body($p);
789 $this->multiprintcss_footer();
790 $this->multiprintcss_postfooter();
794 function _print_prescription_old($p, & $toFile)
796 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
797 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
798 ,$GLOBALS['rx_bottom_margin']
799 ,$GLOBALS['rx_left_margin']
800 ,$GLOBALS['rx_right_margin']
802 $pdf->selectFont('Helvetica');
803 if(!empty($this->pconfig['logo'])) {
804 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
806 $pdf->ezText($p->get_prescription_display(),10);
807 if($this->pconfig['use_signature']) {
808 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
810 else{
811 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
813 if(!empty($toFile))
815 $toFile = $pdf->ezOutput();
817 else
819 $pdf->ezStream();
820 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
822 return;
825 function _email_prescription($p,$email)
827 if (empty($email)) {
828 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
829 return;
831 $mail = new PHPMailer();
832 //this is a temporary config item until the rest of the per practice billing settings make their way in
833 $mail->From = $GLOBALS['practice_return_email_path'];
834 $mail->FromName = $p->provider->get_name_display();
835 $mail->isMail();
836 $mail->Host = "localhost";
837 $mail->Mailer = "mail";
838 $text_body = $p->get_prescription_display();
839 $mail->Body = $text_body;
840 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
841 $mail->AddAddress($email);
842 if($mail->Send()) {
843 $this->assign("process_result","Email was successfully sent to: " . $email);
844 return;
846 else {
847 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
848 return;
852 function do_lookup()
854 if ($_POST['process'] != "true") {
855 // don't do a lookup
856 $this->assign("drug", $_GET['drug']);
857 return;
860 // process the lookup
861 $this->assign("drug", $_POST['drug']);
862 $list = array();
863 if (!empty($_POST['drug'])) {
864 $list = $this->RxList->get_list($_POST['drug']);
867 if (is_array($list)) {
868 $list = array_flip($list);
869 $this->assign("drug_options",$list);
870 $this->assign("drug_values",array_keys($list));
872 else {
873 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
875 //print_r($_POST);
876 //$this->assign("PROCESS","");
878 $_POST['process'] = "";
881 function _fax_prescription($p,$faxNum)
883 $err = "Sent fax";
884 //strip - ,(, ), and ws
885 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
886 //validate the number
888 if(!empty($faxNum) && is_numeric($faxNum))
890 //get the sendfax command and execute it
891 $cmd = $this->pconfig['sendfax'];
892 // prepend any prefix to the fax number
893 $pref=$this->pconfig['prefix'];
894 $faxNum=$pref.$faxNum;
895 if(empty($cmd))
897 $err .= " Send fax not set in includes/config.php";
899 else
901 //generate file to fax
902 $faxFile = "Failed";
903 $this->_print_prescription($p, $faxFile);
904 if(empty($faxFile))
906 $err .= " _print_prescription returned empty file";
908 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
909 $p->get_patient_id() . "_fax_.pdf";
910 //print "filename is $fileName";
911 touch($fileName); // php bug
912 $handle = fopen($fileName,"w");
913 if(!$handle)
915 $err .= " Failed to open file $fileName to write fax to";
917 if(fwrite($handle, $faxFile) === false)
919 $err .= " Failed to write data to $fileName";
921 fclose($handle);
922 $args = " -n -d $faxNum $fileName";
923 //print "command is $cmd $args<br>";
924 exec($cmd . $args);
928 else
930 $err = "bad fax number passed to function";
932 if($err)
934 $this->assign("process_result",$err);