Bug fix for lists module to add codes to fee sheet - contributed by aaversa
[openemr.git] / controllers / C_Prescription.class.php
blob9a704140369c0b456ae53ab9605f80cf81fc9abf
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
8 require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
9 require_once($GLOBALS['fileroot'] . "/library/classes/Provider.class.php");
10 require_once($GLOBALS['fileroot'] . "/library/classes/RXList.class.php");
11 require_once($GLOBALS['fileroot'] . "/library/registry.inc");
12 require_once($GLOBALS['fileroot'] . "/library/amc.php");
14 class C_Prescription extends Controller {
16 var $template_mod;
17 var $pconfig;
18 var $providerid = 0;
19 var $is_faxing = false;
20 var $is_print_to_fax = false;
22 function C_Prescription($template_mod = "general") {
23 parent::Controller();
25 $this->template_mod = $template_mod;
26 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
27 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
28 $this->assign("STYLE", $GLOBALS['style']);
29 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
30 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
31 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
32 $this->assign("CSS_HEADER", $GLOBALS['css_header'] );
33 $this->assign("WEB_ROOT", $GLOBALS['webroot'] );
35 if ($GLOBALS['inhouse_pharmacy']) {
36 // Make an array of drug IDs and selectors for the template.
37 $drug_array_values = array(0);
38 $drug_array_output = array("-- or select from inventory --");
39 $drug_attributes = '';
41 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
43 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
44 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
45 "t.period, t.quantity, t.refills " .
46 "FROM drug_templates AS t, drugs AS d WHERE " .
47 "d.drug_id = t.drug_id ORDER BY t.selector");
49 while ($row = sqlFetchArray($res)) {
50 $tmp_output = $row['selector'];
51 if ($row['ndc_number']) {
52 $tmp_output .= ' [' . $row['ndc_number'] . ']';
54 $drug_array_values[] = $row['drug_id'];
55 $drug_array_output[] = $tmp_output;
56 if ($drug_attributes) $drug_attributes .= ',';
57 $drug_attributes .= "['" .
58 $row['name'] . "'," . // 0
59 $row['form'] . ",'" . // 1
60 $row['dosage'] . "'," . // 2
61 $row['size'] . "," . // 3
62 $row['unit'] . "," . // 4
63 $row['route'] . "," . // 5
64 $row['period'] . "," . // 6
65 $row['substitute'] . "," . // 7
66 $row['quantity'] . "," . // 8
67 $row['refills'] . "," . // 9
68 $row['quantity'] . "]"; // 10 quantity per_refill
70 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
71 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
72 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
76 function default_action() {
77 $this->assign("prescription",$this->prescriptions[0]);
78 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
81 function edit_action($id = "",$patient_id="",$p_obj = null) {
83 if ($p_obj != null && get_class($p_obj) == "prescription") {
84 $this->prescriptions[0] = $p_obj;
86 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
87 $this->prescriptions[0] = new Prescription($id);
90 if (!empty($patient_id)) {
91 $this->prescriptions[0]->set_patient_id($patient_id);
94 // If quantity to dispense is not already set from a POST, set its
95 // default value.
96 if (! $this->get_template_vars('DISP_QUANTITY')) {
97 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
100 $this->default_action();
103 function list_action($id,$sort = "") {
104 if (empty($id)) {
105 $this->function_argument_error();
106 exit;
108 if (!empty($sort)) {
109 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
111 else {
112 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
115 // flag to indicate the CAMOS form is regsitered and active
116 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
118 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
121 function block_action($id,$sort = "") {
122 if (empty($id)) {
123 $this->function_argument_error();
124 exit;
126 if (!empty($sort)) {
127 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
129 else {
130 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
132 //print_r(Prescription::prescriptions_factory($id));
133 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
136 function fragment_action($id,$sort = "") {
137 if (empty($id)) {
138 $this->function_argument_error();
139 exit;
141 if (!empty($sort)) {
142 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
144 else {
145 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
147 //print_r(Prescription::prescriptions_factory($id));
148 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
151 function lookup_action() {
152 $this->do_lookup();
153 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
156 function edit_action_process() {
157 if ($_POST['process'] != "true")
158 return;
159 //print_r($_POST);
161 // Stupid Smarty code treats empty values as not specified values.
162 // Since active is a checkbox, represent the unchecked state as -1.
163 if (empty($_POST['active'])) $_POST['active'] = '-1';
165 $this->prescriptions[0] = new Prescription($_POST['id']);
166 parent::populate_object($this->prescriptions[0]);
167 //echo $this->prescriptions[0]->toString(true);
168 $this->prescriptions[0]->persist();
169 $_POST['process'] = "";
171 // If the "Prescribe and Dispense" button was clicked, then
172 // redisplay as in edit_action() but also replicate the fee and
173 // include a piece of javascript to call dispense().
175 if ($_POST['disp_button']) {
176 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
177 $this->assign("DISP_FEE", $_POST['disp_fee']);
178 $this->assign("ENDING_JAVASCRIPT", "dispense();");
179 $this->_state = false;
180 return $this->edit_action($this->prescriptions[0]->id);
183 // Set the AMC reporting flag (to record percentage of prescriptions that
184 // are set as e-prescriptions)
185 if (!(empty($_POST['escribe_flag']))) {
186 // add the e-prescribe flag
187 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
189 else {
190 // remove the e-prescribe flag
191 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
194 if ($this->prescriptions[0]->get_active() > 0) {
195 return $this->send_action($this->prescriptions[0]->id);
197 $this->list_action($this->prescriptions[0]->get_patient_id());
198 exit;
201 function send_action($id) {
202 $_POST['process'] = "true";
203 if(empty($id)) {
204 $this->function_argument_error();
207 $rx = new Prescription($id);
208 // Populate pharmacy info if the patient has a default pharmacy.
209 // Probably the Prescription object should handle this instead, but
210 // doing it there will require more careful research and testing.
211 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
212 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
213 if ($prow['pharmacy_id']) {
214 $rx->pharmacy->set_id($prow['pharmacy_id']);
215 $rx->pharmacy->populate();
217 $this->assign("prescription", $rx);
219 $this->_state = false;
220 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
221 $this->template_mod . "_send.html");
224 function multiprintfax_header(& $pdf, $p) {
225 return $this->multiprint_header( $pdf, $p );
228 function multiprint_header(& $pdf, $p) {
229 $this->providerid = $p->provider->id;
230 //print header
231 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'],'','50','','center','');
232 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
233 $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 ='" .
234 mysql_real_escape_string($p->provider->id) . "'");
235 $pdf->ezText($res['addr'],12);
236 $my_y = $pdf->y;
237 $pdf->ezNewPage();
238 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
239 // A client had a bad experience with a patient misusing a DEA number, so
240 // now the doctors write those in on printed prescriptions and only when
241 // necessary. If you need to change this back, then please make it a
242 // configurable option. Faxed prescriptions were not changed. -- Rod
243 // Now it is configureable. Change value in
244 // Administration->Globals->Rx
245 if ($GLOBALS['rx_enable_DEA']) {
246 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
247 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
249 else {
250 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
254 if ($GLOBALS['rx_enable_NPI']) {
255 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
256 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
258 else {
259 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
262 if ($GLOBALS['rx_enable_SLN']) {
263 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
264 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
266 else {
267 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
270 $pdf->ezColumnsStop();
271 if ($my_y < $pdf->y){
272 $pdf->ezSetY($my_y);
274 $pdf->ezText('',10);
275 $pdf->setLineStyle(1);
276 $pdf->ezColumnsStart(array('num'=>2));
277 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
278 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
279 $pdf->ezText($p->patient->get_name_display(),10);
280 $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));
281 $pdf->ezText($res['addr']);
282 $my_y = $pdf->y;
283 $pdf->ezNewPage();
284 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
285 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
286 $pdf->ezText($p->patient->date_of_birth,10);
287 $pdf->ezText('');
288 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
289 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
290 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
291 $pdf->ezColumnsStop();
292 if ($my_y < $pdf->y){
293 $pdf->ezSetY($my_y);
295 $pdf->ezText('');
296 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
297 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
298 $pdf->ezText('',10);
301 function multiprintcss_header($p) {
302 echo("<div class='paddingdiv'>\n");
303 $this->providerid = $p->provider->id;
304 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
305 echo ("<tr>\n");
306 echo ("<td></td>\n");
307 echo ("<td>\n");
308 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
309 echo ("</td>\n");
310 echo ("</tr>\n");
311 echo ("<tr>\n");
312 echo ("<td>\n");
313 $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) . "'");
314 $patterns = array ('/\n/','/Tel:/','/Fax:/');
315 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
316 $res = preg_replace($patterns, $replace, $res);
317 echo ('<span class="large">' . $res['addr'] . '</span>');
318 echo ("</td>\n");
319 echo ("<td>\n");
320 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
322 if ($GLOBALS['rx_enable_DEA']) {
323 if ($GLOBALS['rx_show_DEA']) {
324 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
326 else {
327 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
330 if ($GLOBALS['rx_enable_NPI']) {
331 if ($GLOBALS['rx_show_NPI']) {
332 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
334 else {
335 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
338 if ($GLOBALS['rx_enable_SLN']) {
339 if ($GLOBALS['rx_show_SLN']) {
340 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
342 else {
343 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
346 echo ("</td>\n");
347 echo ("</tr>\n");
348 echo ("<tr>\n");
349 echo ("<td rowspan='2' class='bordered'>\n");
350 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
351 echo ($p->patient->get_name_display() . '<br>');
352 $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));
353 $patterns = array ('/\n/');
354 $replace = array ('<br>');
355 $res = preg_replace($patterns, $replace, $res);
356 echo ($res['addr']);
357 echo ("</td>\n");
358 echo ("<td class='bordered'>\n");
359 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
360 echo ($p->patient->date_of_birth );
361 echo ("</td>\n");
362 echo ("</tr>\n");
363 echo ("<tr>\n");
364 echo ("<td class='bordered'>\n");
365 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
366 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
367 echo ("</td>\n");
368 echo ("</tr>\n");
369 echo ("<tr>\n");
370 echo ("<td colspan='2' class='bordered'>\n");
371 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
372 echo ("</td>\n");
373 echo ("</tr>\n");
374 echo ("</table>\n");
377 function multiprintcss_preheader() {
378 // this sets styling and other header information of the multiprint css sheet
379 echo ("<html>\n");
380 echo ("<head>\n");
381 echo ("<style>\n");
382 echo ("div {\n");
383 echo (" padding: 0;\n");
384 echo (" margin: 0;\n");
385 echo ("}\n");
386 echo ("body {\n");
387 echo (" font-family: sans-serif;\n");
388 echo (" font-weight: normal;\n");
389 echo (" font-size: 10pt;\n");
390 echo (" background: white;\n");
391 echo (" color: black;\n");
392 echo ("}\n");
393 echo ("span.large {\n");
394 echo (" font-size: 12pt;\n");
395 echo ("}\n");
396 echo ("span.small {\n");
397 echo (" font-size: 6pt;\n");
398 echo ("}\n");
399 echo ("td {\n");
400 echo (" vertical-align: top;\n");
401 echo (" width: 50%;\n");
402 echo (" font-size: 10pt;\n");
403 echo (" padding-bottom: 8pt;\n");
404 echo ("}\n");
405 echo ("td.bordered {\n");
406 echo (" border-top:1pt solid black;\n");
407 echo ("}\n");
408 echo ("div.paddingdiv {\n");
409 echo (" width: 524pt;\n");
410 echo (" height: 668pt;\n");
411 echo ("}\n");
412 echo ("div.scriptdiv {\n");
413 echo (" padding-top: 12pt;\n");
414 echo (" padding-bottom: 22pt;\n");
415 echo (" padding-left: 35pt;\n");
416 echo (" border-bottom:1pt solid black;\n");
417 echo ("}\n");
418 echo ("div.signdiv {\n");
419 echo (" margin-top: 40pt;\n");
420 echo (" font-size: 12pt;\n");
421 echo ("}\n");
422 echo ("</style>\n");
424 echo ("<title>" . xl('Prescription') . "</title>\n");
425 echo ("</head>\n");
426 echo ("<body>\n");
429 function multiprintfax_footer( & $pdf ) {
430 return $this->multiprint_footer( $pdf );
433 function multiprint_footer(& $pdf) {
434 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
435 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
436 if (file_exists($sigfile)) {
437 $pdf->ezText( xl('Signature') . ": ",12);
438 // $pdf->ezImage($sigfile, "", "", "none", "left");
439 $pdf->ezImage($sigfile, "", "", "none", "center");
440 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
441 if ( $this->is_print_to_fax ) {
442 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
445 $addenumFile = $this->pconfig['addendum_file'];
446 if ( file_exists( $addenumFile ) ) {
447 $pdf->ezText('');
448 $f = fopen($addenumFile, "r");
449 while ( $line = fgets($f, 1000) ) {
450 $pdf->ezText(rtrim($line));
454 return;
457 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
460 function multiprintcss_footer() {
461 echo ("<div class='signdiv'>\n");
462 echo (xl('Signature') . ":________________________________<br>");
463 echo (xl('Date') . ": " . date('Y-m-d'));
464 echo ("</div>\n");
465 echo ("</div>\n");
468 function multiprintcss_postfooter() {
469 echo("<script language='JavaScript'>\n");
470 echo("window.print();\n");
471 echo("</script>\n");
472 echo("</body>\n");
473 echo("</html>\n");
476 function get_prescription_body_text($p) {
477 $body = '<b>' . xl('Rx') . ': ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
478 if ($p->get_form()) $body .= ' [' . $p->form_array[$p->get_form()] . "]";
479 $body .= "</b> <i>" .
480 $p->substitute_array[$p->get_substitute()] . "</i>\n" .
481 '<b>' . xl('Disp #') . ':</b> <u>' . $p->get_quantity() . "</u>\n" .
482 '<b>' . xl('Sig') . ':</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] . ' ' .
483 $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
484 if ($p->get_refills() > 0) {
485 $body .= "\n<b>" . xl('Refills') . ":</b> <u>" . $p->get_refills();
486 if ($p->get_per_refill()) {
487 $body .= " " . xl('of quantity') . " " . $p->get_per_refill();
489 $body .= "</u>\n";
491 else {
492 $body .= "\n<b>" . xl('Refills') . ":</b> <u>0 (" . xl('Zero') . ")</u>\n";
494 $note = $p->get_note();
495 if ($note != '') {
496 $body .= "\n$note\n";
498 return $body;
501 function multiprintfax_body(& $pdf, $p){
502 return $this->multiprint_body( $pdf, $p );
505 function multiprint_body(& $pdf, $p){
506 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
507 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
508 $d = $this->get_prescription_body_text($p);
509 if ( $pdf->ezText($d,10,array(),1) ) {
510 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
511 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
512 $this->multiprint_footer($pdf);
513 $pdf->ezNewPage();
514 $this->multiprint_header($pdf, $p);
515 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
516 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
518 $my_y = $pdf->y;
519 $pdf->ezText($d,10);
520 if($this->pconfig['shading']) {
521 $pdf->setColor(.9,.9,.9);
522 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
523 $pdf->setColor(0,0,0);
525 $pdf->ezSetY($my_y);
526 $pdf->ezText($d,10);
527 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
528 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
529 $pdf->ezText('');
530 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
531 $pdf->ezText('');
534 function multiprintcss_body($p){
535 $d = $this->get_prescription_body_text($p);
536 $patterns = array ('/\n/','/ /');
537 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
538 $d = preg_replace($patterns, $replace, $d);
539 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
542 function multiprintfax_action($id = "") {
543 $this->is_print_to_fax=true;
544 return $this->multiprint_action( $id );
547 function multiprint_action($id = "") {
548 $_POST['process'] = "true";
549 if(empty($id)) {
550 $this->function_argument_error();
552 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
553 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
554 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
555 ,$GLOBALS['rx_bottom_margin']
556 ,$GLOBALS['rx_left_margin']
557 ,$GLOBALS['rx_right_margin']
559 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
561 // $print_header = true;
562 $on_this_page = 0;
564 //print prescriptions body
565 $this->_state = false; // Added by Rod - see Controller.class.php
566 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
567 foreach ($ids as $id) {
568 $p = new Prescription($id);
569 // if ($print_header == true) {
570 if ($on_this_page == 0) {
571 $this->multiprint_header($pdf, $p);
573 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
574 $this->multiprint_footer($pdf);
575 $pdf->ezNewPage();
576 $this->multiprint_header($pdf, $p);
577 // $print_header = false;
578 $on_this_page = 1;
580 $this->multiprint_body($pdf, $p);
583 $this->multiprint_footer($pdf);
585 $pdf->ezStream();
586 return;
589 function multiprintcss_action($id = "") {
590 $_POST['process'] = "true";
591 if(empty($id)) {
592 $this->function_argument_error();
595 $this->multiprintcss_preheader();
597 $this->_state = false; // Added by Rod - see Controller.class.php
598 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
600 $on_this_page = 0;
601 foreach ($ids as $id) {
602 $p = new Prescription($id);
603 if ($on_this_page == 0) {
604 $this->multiprintcss_header($p);
606 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
607 $this->multiprintcss_footer();
608 $this->multiprintcss_header($p);
609 $on_this_page = 1;
611 $this->multiprintcss_body($p);
613 $this->multiprintcss_footer();
614 $this->multiprintcss_postfooter();
615 return;
618 function send_action_process($id) {
619 $dummy = ""; // Added by Rod to avoid run-time warnings
620 if ($_POST['process'] != "true")
621 return;
622 if(empty($id)) {
623 $this->function_argument_error();
625 $p = new Prescription($id);
626 switch ($_POST['submit']) {
628 case (xl("Print")." (".xl("PDF").")"):
629 // The following statement added by Rod.
630 // Looking at Controller.class.php, it appears that _state is set to false
631 // to indicate that no further HTML is to be generated.
632 $this->_state = false; // Added by Rod - see Controller.class.php
633 return $this->_print_prescription($p, $dummy);
634 break;
635 case (xl("Print")." (".xl("HTML").")"):
636 $this->_state = false;
637 return $this->_print_prescription_css($p, $dummy);
638 break;
639 case xl("Print To Fax"):
640 $this->_state = false;
641 $this->is_print_to_fax = true;
642 return $this->_print_prescription($p, $dummy);
643 break;
644 case xl("Email"):
645 return $this->_email_prescription($p,$_POST['email_to']);
646 break;
647 case xl("Fax"):
648 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
649 //$this->assign("process_result","No fax server is currently setup.");
650 return $this->_fax_prescription($p,$_POST['fax_to']);
651 break;
652 case xl("Auto Send"):
653 $pharmacy_id = $_POST['pharmacy_id'];
654 //echo "auto sending to : " . $_POST['pharmacy_id'];
655 $phar = new Pharmacy($_POST['pharmacy_id']);
656 //print_r($phar);
657 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
658 return $this->_print_prescription($p, $dummy);
660 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
661 $email = $phar->get_email();
662 if (!empty($email)) {
663 return $this->_email_prescription($p,$phar->get_email());
665 //else print it
667 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
668 $faxNum= $phar->get_fax();
669 if(!empty($faxNum)) {
670 Return $this->_fax_prescription ($p,$faxNum);
672 // return $this->assign("process_result","No fax server is currently setup.");
673 // else default is printing,
675 else {
676 //the pharmacy has no default or default is print
677 return $this->_print_prescription($p, $dummy);
679 break;
682 return;
686 function _print_prescription($p, & $toFile) {
687 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
688 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
689 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
690 ,$GLOBALS['rx_bottom_margin']
691 ,$GLOBALS['rx_left_margin']
692 ,$GLOBALS['rx_right_margin']
695 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
697 // Signature images are to be used only when faxing.
698 if(!empty($toFile)) $this->is_faxing = true;
700 $this->multiprint_header($pdf, $p);
701 $this->multiprint_body($pdf, $p);
702 $this->multiprint_footer($pdf);
704 if(!empty($toFile)) {
705 $toFile = $pdf->ezOutput();
707 else {
708 $pdf->ezStream();
709 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
711 return;
714 function _print_prescription_css($p, & $toFile) {
716 $this->multiprintcss_preheader();
717 $this->multiprintcss_header($p);
718 $this->multiprintcss_body($p);
719 $this->multiprintcss_footer();
720 $this->multiprintcss_postfooter();
724 function _print_prescription_old($p, & $toFile) {
725 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
726 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
727 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
728 ,$GLOBALS['rx_bottom_margin']
729 ,$GLOBALS['rx_left_margin']
730 ,$GLOBALS['rx_right_margin']
732 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
733 if(!empty($this->pconfig['logo'])) {
734 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
736 $pdf->ezText($p->get_prescription_display(),10);
737 if($this->pconfig['use_signature']) {
738 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
740 else{
741 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
743 if(!empty($toFile))
745 $toFile = $pdf->ezOutput();
747 else
749 $pdf->ezStream();
750 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
752 return;
755 function _email_prescription($p,$email) {
756 if (empty($email)) {
757 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
758 return;
760 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
761 $mail = new PHPMailer();
762 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
763 //this is a temporary config item until the rest of the per practice billing settings make their way in
764 $mail->From = $GLOBALS['practice_return_email_path'];
765 $mail->FromName = $p->provider->get_name_display();
766 $mail->isMail();
767 $mail->Host = "localhost";
768 $mail->Mailer = "mail";
769 $text_body = $p->get_prescription_display();
770 $mail->Body = $text_body;
771 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
772 $mail->AddAddress($email);
773 if($mail->Send()) {
774 $this->assign("process_result","Email was successfully sent to: " . $email);
775 return;
777 else {
778 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
779 return;
783 function do_lookup() {
784 if ($_POST['process'] != "true") {
785 // don't do a lookup
786 $this->assign("drug", $_GET['drug']);
787 return;
790 // process the lookup
791 $this->assign("drug", $_POST['drug']);
792 $list = array();
793 if (!empty($_POST['drug'])) {
794 $list = @RxList::get_list($_POST['drug']);
797 if (is_array($list)) {
798 $list = array_flip($list);
799 $this->assign("drug_options",$list);
800 $this->assign("drug_values",array_keys($list));
802 else {
803 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
805 //print_r($_POST);
806 //$this->assign("PROCESS","");
808 $_POST['process'] = "";
811 function _fax_prescription($p,$faxNum)
813 $err = "Sent fax";
814 //strip - ,(, ), and ws
815 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
816 //validate the number
818 if(!empty($faxNum) && is_numeric($faxNum))
820 //get the sendfax command and execute it
821 $cmd = $this->pconfig['sendfax'];
822 // prepend any prefix to the fax number
823 $pref=$this->pconfig['prefix'];
824 $faxNum=$pref.$faxNum;
825 if(empty($cmd))
827 $err .= " Send fax not set in includes/config.php";
828 break;
830 else
832 //generate file to fax
833 $faxFile = "Failed";
834 $this->_print_prescription($p, $faxFile);
835 if(empty($faxFile))
837 $err .= " _print_prescription returned empty file";
838 break;
840 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
841 $p->get_patient_id() . "_fax_.pdf";
842 //print "filename is $fileName";
843 touch($fileName); // php bug
844 $handle = fopen($fileName,"w");
845 if(!$handle)
847 $err .= " Failed to open file $fileName to write fax to";
848 break;
850 if(fwrite($handle, $faxFile) === false)
852 $err .= " Failed to write data to $fileName";
853 break;
855 fclose($handle);
856 $args = " -n -d $faxNum $fileName";
857 //print "command is $cmd $args<br>";
858 exec($cmd . $args);
862 else
864 $err = "bad fax number passed to function";
866 if($err)
868 $this->assign("process_result",$err);