Support for optional logging of print actions.
[openemr.git] / controllers / C_Prescription.class.php
blob72f3b0f6cc2454f672465608a7b7ddedbf3eb239
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/Controller.class.php");
25 require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
26 require_once($GLOBALS['fileroot'] . "/library/classes/Provider.class.php");
27 require_once($GLOBALS['fileroot'] . "/library/classes/RXList.class.php");
28 require_once($GLOBALS['fileroot'] . "/library/registry.inc");
29 require_once($GLOBALS['fileroot'] . "/library/amc.php");
31 class C_Prescription extends Controller {
33 var $template_mod;
34 var $pconfig;
35 var $providerid = 0;
36 var $is_faxing = false;
37 var $is_print_to_fax = false;
39 function C_Prescription($template_mod = "general") {
40 parent::Controller();
42 $this->template_mod = $template_mod;
43 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
44 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
45 $this->assign("STYLE", $GLOBALS['style']);
46 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
47 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
48 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
49 $this->assign("CSS_HEADER", $GLOBALS['css_header'] );
50 $this->assign("WEB_ROOT", $GLOBALS['webroot'] );
52 if ($GLOBALS['inhouse_pharmacy']) {
53 // Make an array of drug IDs and selectors for the template.
54 $drug_array_values = array(0);
55 $drug_array_output = array("-- " . xl('or select from inventory') ." --");
56 $drug_attributes = '';
58 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
60 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
61 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
62 "t.period, t.quantity, t.refills " .
63 "FROM drug_templates AS t, drugs AS d WHERE " .
64 "d.drug_id = t.drug_id ORDER BY t.selector");
66 while ($row = sqlFetchArray($res)) {
67 $tmp_output = $row['selector'];
68 if ($row['ndc_number']) {
69 $tmp_output .= ' [' . $row['ndc_number'] . ']';
71 $drug_array_values[] = $row['drug_id'];
72 $drug_array_output[] = $tmp_output;
73 if ($drug_attributes) $drug_attributes .= ',';
74 $drug_attributes .= "['" .
75 $row['name'] . "'," . // 0
76 $row['form'] . ",'" . // 1
77 $row['dosage'] . "'," . // 2
78 $row['size'] . "," . // 3
79 $row['unit'] . "," . // 4
80 $row['route'] . "," . // 5
81 $row['period'] . "," . // 6
82 $row['substitute'] . "," . // 7
83 $row['quantity'] . "," . // 8
84 $row['refills'] . "," . // 9
85 $row['quantity'] . "]"; // 10 quantity per_refill
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() {
94 $this->assign("prescription",$this->prescriptions[0]);
95 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
98 function edit_action($id = "",$patient_id="",$p_obj = null) {
100 if ($p_obj != null && get_class($p_obj) == "prescription") {
101 $this->prescriptions[0] = $p_obj;
103 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
104 $this->prescriptions[0] = new Prescription($id);
107 if (!empty($patient_id)) {
108 $this->prescriptions[0]->set_patient_id($patient_id);
111 // If quantity to dispense is not already set from a POST, set its
112 // default value.
113 if (! $this->get_template_vars('DISP_QUANTITY')) {
114 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
117 $this->default_action();
120 function list_action($id,$sort = "") {
121 if (empty($id)) {
122 $this->function_argument_error();
123 exit;
125 if (!empty($sort)) {
126 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
128 else {
129 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
132 // flag to indicate the CAMOS form is regsitered and active
133 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
135 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
138 function block_action($id,$sort = "") {
139 if (empty($id)) {
140 $this->function_argument_error();
141 exit;
143 if (!empty($sort)) {
144 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
146 else {
147 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
149 //print_r(Prescription::prescriptions_factory($id));
150 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
153 function fragment_action($id,$sort = "") {
154 if (empty($id)) {
155 $this->function_argument_error();
156 exit;
158 if (!empty($sort)) {
159 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
161 else {
162 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
164 //print_r(Prescription::prescriptions_factory($id));
165 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
168 function lookup_action() {
169 $this->do_lookup();
170 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
173 function edit_action_process() {
174 if ($_POST['process'] != "true")
175 return;
176 //print_r($_POST);
178 // Stupid Smarty code treats empty values as not specified values.
179 // Since active is a checkbox, represent the unchecked state as -1.
180 if (empty($_POST['active'])) $_POST['active'] = '-1';
182 $this->prescriptions[0] = new Prescription($_POST['id']);
183 parent::populate_object($this->prescriptions[0]);
184 //echo $this->prescriptions[0]->toString(true);
185 $this->prescriptions[0]->persist();
186 $_POST['process'] = "";
188 // If the "Prescribe and Dispense" button was clicked, then
189 // redisplay as in edit_action() but also replicate the fee and
190 // include a piece of javascript to call dispense().
192 if ($_POST['disp_button']) {
193 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
194 $this->assign("DISP_FEE", $_POST['disp_fee']);
195 $this->assign("ENDING_JAVASCRIPT", "dispense();");
196 $this->_state = false;
197 return $this->edit_action($this->prescriptions[0]->id);
200 // Set the AMC reporting flag (to record percentage of prescriptions that
201 // are set as e-prescriptions)
202 if (!(empty($_POST['escribe_flag']))) {
203 // add the e-prescribe flag
204 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
206 else {
207 // remove the e-prescribe flag
208 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
211 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
212 // if ($this->prescriptions[0]->get_active() > 0) {
213 // return $this->send_action($this->prescriptions[0]->id);
214 // }
215 $this->list_action($this->prescriptions[0]->get_patient_id());
216 exit;
219 function send_action($id) {
220 $_POST['process'] = "true";
221 if(empty($id)) {
222 $this->function_argument_error();
225 $rx = new Prescription($id);
226 // Populate pharmacy info if the patient has a default pharmacy.
227 // Probably the Prescription object should handle this instead, but
228 // doing it there will require more careful research and testing.
229 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
230 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
231 if ($prow['pharmacy_id']) {
232 $rx->pharmacy->set_id($prow['pharmacy_id']);
233 $rx->pharmacy->populate();
235 $this->assign("prescription", $rx);
237 $this->_state = false;
238 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
239 $this->template_mod . "_send.html");
242 function multiprintfax_header(& $pdf, $p) {
243 return $this->multiprint_header( $pdf, $p );
246 function multiprint_header(& $pdf, $p) {
247 $this->providerid = $p->provider->id;
248 //print header
249 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'],'','50','','center','');
250 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
251 $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 ='" .
252 mysql_real_escape_string($p->provider->id) . "'");
253 $pdf->ezText($res['addr'],12);
254 $my_y = $pdf->y;
255 $pdf->ezNewPage();
256 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
257 // A client had a bad experience with a patient misusing a DEA number, so
258 // now the doctors write those in on printed prescriptions and only when
259 // necessary. If you need to change this back, then please make it a
260 // configurable option. Faxed prescriptions were not changed. -- Rod
261 // Now it is configureable. Change value in
262 // Administration->Globals->Rx
263 if ($GLOBALS['rx_enable_DEA']) {
264 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
265 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
267 else {
268 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
272 if ($GLOBALS['rx_enable_NPI']) {
273 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
274 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
276 else {
277 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
280 if ($GLOBALS['rx_enable_SLN']) {
281 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
282 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
284 else {
285 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
288 $pdf->ezColumnsStop();
289 if ($my_y < $pdf->y){
290 $pdf->ezSetY($my_y);
292 $pdf->ezText('',10);
293 $pdf->setLineStyle(1);
294 $pdf->ezColumnsStart(array('num'=>2));
295 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
296 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
297 $pdf->ezText($p->patient->get_name_display(),10);
298 $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 =". mysql_real_escape_string ($p->patient->id));
299 $pdf->ezText($res['addr']);
300 $my_y = $pdf->y;
301 $pdf->ezNewPage();
302 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
303 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
304 $pdf->ezText($p->patient->date_of_birth,10);
305 $pdf->ezText('');
306 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
307 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
308 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
309 $pdf->ezColumnsStop();
310 if ($my_y < $pdf->y){
311 $pdf->ezSetY($my_y);
313 $pdf->ezText('');
314 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
315 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
316 $pdf->ezText('',10);
319 function multiprintcss_header($p) {
320 echo("<div class='paddingdiv'>\n");
321 $this->providerid = $p->provider->id;
322 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
323 echo ("<tr>\n");
324 echo ("<td></td>\n");
325 echo ("<td>\n");
326 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
327 echo ("</td>\n");
328 echo ("</tr>\n");
329 echo ("<tr>\n");
330 echo ("<td>\n");
331 $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 ='" . mysql_real_escape_string($p->provider->id) . "'");
332 $patterns = array ('/\n/','/Tel:/','/Fax:/');
333 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
334 $res = preg_replace($patterns, $replace, $res);
335 echo ('<span class="large">' . $res['addr'] . '</span>');
336 echo ("</td>\n");
337 echo ("<td>\n");
338 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
340 if ($GLOBALS['rx_enable_DEA']) {
341 if ($GLOBALS['rx_show_DEA']) {
342 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
344 else {
345 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
348 if ($GLOBALS['rx_enable_NPI']) {
349 if ($GLOBALS['rx_show_NPI']) {
350 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
352 else {
353 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
356 if ($GLOBALS['rx_enable_SLN']) {
357 if ($GLOBALS['rx_show_SLN']) {
358 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
360 else {
361 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
364 echo ("</td>\n");
365 echo ("</tr>\n");
366 echo ("<tr>\n");
367 echo ("<td rowspan='2' class='bordered'>\n");
368 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
369 echo ($p->patient->get_name_display() . '<br>');
370 $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 =". mysql_real_escape_string ($p->patient->id));
371 $patterns = array ('/\n/');
372 $replace = array ('<br>');
373 $res = preg_replace($patterns, $replace, $res);
374 echo ($res['addr']);
375 echo ("</td>\n");
376 echo ("<td class='bordered'>\n");
377 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
378 echo ($p->patient->date_of_birth );
379 echo ("</td>\n");
380 echo ("</tr>\n");
381 echo ("<tr>\n");
382 echo ("<td class='bordered'>\n");
383 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
384 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
385 echo ("</td>\n");
386 echo ("</tr>\n");
387 echo ("<tr>\n");
388 echo ("<td colspan='2' class='bordered'>\n");
389 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
390 echo ("</td>\n");
391 echo ("</tr>\n");
392 echo ("</table>\n");
395 function multiprintcss_preheader() {
396 // this sets styling and other header information of the multiprint css sheet
397 echo ("<html>\n");
398 echo ("<head>\n");
399 echo ("<style>\n");
400 echo ("div {\n");
401 echo (" padding: 0;\n");
402 echo (" margin: 0;\n");
403 echo ("}\n");
404 echo ("body {\n");
405 echo (" font-family: sans-serif;\n");
406 echo (" font-weight: normal;\n");
407 echo (" font-size: 10pt;\n");
408 echo (" background: white;\n");
409 echo (" color: black;\n");
410 echo ("}\n");
411 echo ("span.large {\n");
412 echo (" font-size: 12pt;\n");
413 echo ("}\n");
414 echo ("span.small {\n");
415 echo (" font-size: 6pt;\n");
416 echo ("}\n");
417 echo ("td {\n");
418 echo (" vertical-align: top;\n");
419 echo (" width: 50%;\n");
420 echo (" font-size: 10pt;\n");
421 echo (" padding-bottom: 8pt;\n");
422 echo ("}\n");
423 echo ("td.bordered {\n");
424 echo (" border-top:1pt solid black;\n");
425 echo ("}\n");
426 echo ("div.paddingdiv {\n");
427 echo (" width: 524pt;\n");
428 echo (" height: 668pt;\n");
429 echo ("}\n");
430 echo ("div.scriptdiv {\n");
431 echo (" padding-top: 12pt;\n");
432 echo (" padding-bottom: 22pt;\n");
433 echo (" padding-left: 35pt;\n");
434 echo (" border-bottom:1pt solid black;\n");
435 echo ("}\n");
436 echo ("div.signdiv {\n");
437 echo (" margin-top: 40pt;\n");
438 echo (" font-size: 12pt;\n");
439 echo ("}\n");
440 echo ("</style>\n");
442 echo ("<title>" . xl('Prescription') . "</title>\n");
443 echo ("</head>\n");
444 echo ("<body>\n");
447 function multiprintfax_footer( & $pdf ) {
448 return $this->multiprint_footer( $pdf );
451 function multiprint_footer(& $pdf) {
452 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
453 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
454 if (file_exists($sigfile)) {
455 $pdf->ezText( xl('Signature') . ": ",12);
456 // $pdf->ezImage($sigfile, "", "", "none", "left");
457 $pdf->ezImage($sigfile, "", "", "none", "center");
458 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
459 if ( $this->is_print_to_fax ) {
460 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
463 $addenumFile = $this->pconfig['addendum_file'];
464 if ( file_exists( $addenumFile ) ) {
465 $pdf->ezText('');
466 $f = fopen($addenumFile, "r");
467 while ( $line = fgets($f, 1000) ) {
468 $pdf->ezText(rtrim($line));
472 return;
475 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
478 function multiprintcss_footer() {
479 echo ("<div class='signdiv'>\n");
480 echo (xl('Signature') . ":________________________________<br>");
481 echo (xl('Date') . ": " . date('Y-m-d'));
482 echo ("</div>\n");
483 echo ("</div>\n");
486 function multiprintcss_postfooter() {
487 echo("<script language='JavaScript'>\n");
488 echo("opener.top.printLogPrint(window);\n");
489 echo("</script>\n");
490 echo("</body>\n");
491 echo("</html>\n");
494 function get_prescription_body_text($p) {
495 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
496 if ($p->get_form()) $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
497 $body .= "</b> <i>" .
498 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
499 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
500 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
501 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
502 if ($p->get_refills() > 0) {
503 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
504 if ($p->get_per_refill()) {
505 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
507 $body .= "</u>\n";
509 else {
510 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
512 $note = $p->get_note();
513 if ($note != '') {
514 $body .= "\n" . text($note) . "\n";
516 return $body;
519 function multiprintfax_body(& $pdf, $p){
520 return $this->multiprint_body( $pdf, $p );
523 function multiprint_body(& $pdf, $p){
524 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
525 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
526 $d = $this->get_prescription_body_text($p);
527 if ( $pdf->ezText($d,10,array(),1) ) {
528 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
529 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
530 $this->multiprint_footer($pdf);
531 $pdf->ezNewPage();
532 $this->multiprint_header($pdf, $p);
533 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
534 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
536 $my_y = $pdf->y;
537 $pdf->ezText($d,10);
538 if($this->pconfig['shading']) {
539 $pdf->setColor(.9,.9,.9);
540 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
541 $pdf->setColor(0,0,0);
543 $pdf->ezSetY($my_y);
544 $pdf->ezText($d,10);
545 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
546 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
547 $pdf->ezText('');
548 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
549 $pdf->ezText('');
552 function multiprintcss_body($p){
553 $d = $this->get_prescription_body_text($p);
554 $patterns = array ('/\n/','/ /');
555 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
556 $d = preg_replace($patterns, $replace, $d);
557 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
560 function multiprintfax_action($id = "") {
561 $this->is_print_to_fax=true;
562 return $this->multiprint_action( $id );
565 function multiprint_action($id = "") {
566 $_POST['process'] = "true";
567 if(empty($id)) {
568 $this->function_argument_error();
570 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
571 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
572 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
573 ,$GLOBALS['rx_bottom_margin']
574 ,$GLOBALS['rx_left_margin']
575 ,$GLOBALS['rx_right_margin']
577 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
579 // $print_header = true;
580 $on_this_page = 0;
582 //print prescriptions body
583 $this->_state = false; // Added by Rod - see Controller.class.php
584 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
585 foreach ($ids as $id) {
586 $p = new Prescription($id);
587 // if ($print_header == true) {
588 if ($on_this_page == 0) {
589 $this->multiprint_header($pdf, $p);
591 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
592 $this->multiprint_footer($pdf);
593 $pdf->ezNewPage();
594 $this->multiprint_header($pdf, $p);
595 // $print_header = false;
596 $on_this_page = 1;
598 $this->multiprint_body($pdf, $p);
601 $this->multiprint_footer($pdf);
603 $pdf->ezStream();
604 return;
607 function multiprintcss_action($id = "") {
608 $_POST['process'] = "true";
609 if(empty($id)) {
610 $this->function_argument_error();
613 $this->multiprintcss_preheader();
615 $this->_state = false; // Added by Rod - see Controller.class.php
616 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
618 $on_this_page = 0;
619 foreach ($ids as $id) {
620 $p = new Prescription($id);
621 if ($on_this_page == 0) {
622 $this->multiprintcss_header($p);
624 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
625 $this->multiprintcss_footer();
626 $this->multiprintcss_header($p);
627 $on_this_page = 1;
629 $this->multiprintcss_body($p);
631 $this->multiprintcss_footer();
632 $this->multiprintcss_postfooter();
633 return;
636 function send_action_process($id) {
637 $dummy = ""; // Added by Rod to avoid run-time warnings
638 if ($_POST['process'] != "true")
639 return;
640 if(empty($id)) {
641 $this->function_argument_error();
643 $p = new Prescription($id);
644 switch ($_POST['submit']) {
646 case (xl("Print")." (".xl("PDF").")"):
647 // The following statement added by Rod.
648 // Looking at Controller.class.php, it appears that _state is set to false
649 // to indicate that no further HTML is to be generated.
650 $this->_state = false; // Added by Rod - see Controller.class.php
651 return $this->_print_prescription($p, $dummy);
652 break;
653 case (xl("Print")." (".xl("HTML").")"):
654 $this->_state = false;
655 return $this->_print_prescription_css($p, $dummy);
656 break;
657 case xl("Print To Fax"):
658 $this->_state = false;
659 $this->is_print_to_fax = true;
660 return $this->_print_prescription($p, $dummy);
661 break;
662 case xl("Email"):
663 return $this->_email_prescription($p,$_POST['email_to']);
664 break;
665 case xl("Fax"):
666 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
667 //$this->assign("process_result","No fax server is currently setup.");
668 return $this->_fax_prescription($p,$_POST['fax_to']);
669 break;
670 case xl("Auto Send"):
671 $pharmacy_id = $_POST['pharmacy_id'];
672 //echo "auto sending to : " . $_POST['pharmacy_id'];
673 $phar = new Pharmacy($_POST['pharmacy_id']);
674 //print_r($phar);
675 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
676 return $this->_print_prescription($p, $dummy);
678 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
679 $email = $phar->get_email();
680 if (!empty($email)) {
681 return $this->_email_prescription($p,$phar->get_email());
683 //else print it
685 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
686 $faxNum= $phar->get_fax();
687 if(!empty($faxNum)) {
688 Return $this->_fax_prescription ($p,$faxNum);
690 // return $this->assign("process_result","No fax server is currently setup.");
691 // else default is printing,
693 else {
694 //the pharmacy has no default or default is print
695 return $this->_print_prescription($p, $dummy);
697 break;
700 return;
704 function _print_prescription($p, & $toFile) {
705 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
706 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
707 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
708 ,$GLOBALS['rx_bottom_margin']
709 ,$GLOBALS['rx_left_margin']
710 ,$GLOBALS['rx_right_margin']
713 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
715 // Signature images are to be used only when faxing.
716 if(!empty($toFile)) $this->is_faxing = true;
718 $this->multiprint_header($pdf, $p);
719 $this->multiprint_body($pdf, $p);
720 $this->multiprint_footer($pdf);
722 if(!empty($toFile)) {
723 $toFile = $pdf->ezOutput();
725 else {
726 $pdf->ezStream();
727 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
729 return;
732 function _print_prescription_css($p, & $toFile) {
734 $this->multiprintcss_preheader();
735 $this->multiprintcss_header($p);
736 $this->multiprintcss_body($p);
737 $this->multiprintcss_footer();
738 $this->multiprintcss_postfooter();
742 function _print_prescription_old($p, & $toFile) {
743 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
744 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
745 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
746 ,$GLOBALS['rx_bottom_margin']
747 ,$GLOBALS['rx_left_margin']
748 ,$GLOBALS['rx_right_margin']
750 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
751 if(!empty($this->pconfig['logo'])) {
752 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
754 $pdf->ezText($p->get_prescription_display(),10);
755 if($this->pconfig['use_signature']) {
756 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
758 else{
759 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
761 if(!empty($toFile))
763 $toFile = $pdf->ezOutput();
765 else
767 $pdf->ezStream();
768 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
770 return;
773 function _email_prescription($p,$email) {
774 if (empty($email)) {
775 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
776 return;
778 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
779 $mail = new PHPMailer();
780 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
781 //this is a temporary config item until the rest of the per practice billing settings make their way in
782 $mail->From = $GLOBALS['practice_return_email_path'];
783 $mail->FromName = $p->provider->get_name_display();
784 $mail->isMail();
785 $mail->Host = "localhost";
786 $mail->Mailer = "mail";
787 $text_body = $p->get_prescription_display();
788 $mail->Body = $text_body;
789 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
790 $mail->AddAddress($email);
791 if($mail->Send()) {
792 $this->assign("process_result","Email was successfully sent to: " . $email);
793 return;
795 else {
796 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
797 return;
801 function do_lookup() {
802 if ($_POST['process'] != "true") {
803 // don't do a lookup
804 $this->assign("drug", $_GET['drug']);
805 return;
808 // process the lookup
809 $this->assign("drug", $_POST['drug']);
810 $list = array();
811 if (!empty($_POST['drug'])) {
812 $list = @RxList::get_list($_POST['drug']);
815 if (is_array($list)) {
816 $list = array_flip($list);
817 $this->assign("drug_options",$list);
818 $this->assign("drug_values",array_keys($list));
820 else {
821 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
823 //print_r($_POST);
824 //$this->assign("PROCESS","");
826 $_POST['process'] = "";
829 function _fax_prescription($p,$faxNum)
831 $err = "Sent fax";
832 //strip - ,(, ), and ws
833 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
834 //validate the number
836 if(!empty($faxNum) && is_numeric($faxNum))
838 //get the sendfax command and execute it
839 $cmd = $this->pconfig['sendfax'];
840 // prepend any prefix to the fax number
841 $pref=$this->pconfig['prefix'];
842 $faxNum=$pref.$faxNum;
843 if(empty($cmd))
845 $err .= " Send fax not set in includes/config.php";
846 break;
848 else
850 //generate file to fax
851 $faxFile = "Failed";
852 $this->_print_prescription($p, $faxFile);
853 if(empty($faxFile))
855 $err .= " _print_prescription returned empty file";
856 break;
858 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
859 $p->get_patient_id() . "_fax_.pdf";
860 //print "filename is $fileName";
861 touch($fileName); // php bug
862 $handle = fopen($fileName,"w");
863 if(!$handle)
865 $err .= " Failed to open file $fileName to write fax to";
866 break;
868 if(fwrite($handle, $faxFile) === false)
870 $err .= " Failed to write data to $fileName";
871 break;
873 fclose($handle);
874 $args = " -n -d $faxNum $fileName";
875 //print "command is $cmd $args<br>";
876 exec($cmd . $args);
880 else
882 $err = "bad fax number passed to function";
884 if($err)
886 $this->assign("process_result",$err);