updated user manual wiki page link for future 4.1.2
[openemr.git] / controllers / C_Prescription.class.php
blob2f86703781244b51884f7170037670cb0e87f192
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 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
195 // if ($this->prescriptions[0]->get_active() > 0) {
196 // return $this->send_action($this->prescriptions[0]->id);
197 // }
198 $this->list_action($this->prescriptions[0]->get_patient_id());
199 exit;
202 function send_action($id) {
203 $_POST['process'] = "true";
204 if(empty($id)) {
205 $this->function_argument_error();
208 $rx = new Prescription($id);
209 // Populate pharmacy info if the patient has a default pharmacy.
210 // Probably the Prescription object should handle this instead, but
211 // doing it there will require more careful research and testing.
212 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
213 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
214 if ($prow['pharmacy_id']) {
215 $rx->pharmacy->set_id($prow['pharmacy_id']);
216 $rx->pharmacy->populate();
218 $this->assign("prescription", $rx);
220 $this->_state = false;
221 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
222 $this->template_mod . "_send.html");
225 function multiprintfax_header(& $pdf, $p) {
226 return $this->multiprint_header( $pdf, $p );
229 function multiprint_header(& $pdf, $p) {
230 $this->providerid = $p->provider->id;
231 //print header
232 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'],'','50','','center','');
233 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
234 $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 ='" .
235 mysql_real_escape_string($p->provider->id) . "'");
236 $pdf->ezText($res['addr'],12);
237 $my_y = $pdf->y;
238 $pdf->ezNewPage();
239 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
240 // A client had a bad experience with a patient misusing a DEA number, so
241 // now the doctors write those in on printed prescriptions and only when
242 // necessary. If you need to change this back, then please make it a
243 // configurable option. Faxed prescriptions were not changed. -- Rod
244 // Now it is configureable. Change value in
245 // Administration->Globals->Rx
246 if ($GLOBALS['rx_enable_DEA']) {
247 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
248 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
250 else {
251 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
255 if ($GLOBALS['rx_enable_NPI']) {
256 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
257 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
259 else {
260 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
263 if ($GLOBALS['rx_enable_SLN']) {
264 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
265 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
267 else {
268 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
271 $pdf->ezColumnsStop();
272 if ($my_y < $pdf->y){
273 $pdf->ezSetY($my_y);
275 $pdf->ezText('',10);
276 $pdf->setLineStyle(1);
277 $pdf->ezColumnsStart(array('num'=>2));
278 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
279 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
280 $pdf->ezText($p->patient->get_name_display(),10);
281 $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));
282 $pdf->ezText($res['addr']);
283 $my_y = $pdf->y;
284 $pdf->ezNewPage();
285 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
286 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
287 $pdf->ezText($p->patient->date_of_birth,10);
288 $pdf->ezText('');
289 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
290 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
291 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
292 $pdf->ezColumnsStop();
293 if ($my_y < $pdf->y){
294 $pdf->ezSetY($my_y);
296 $pdf->ezText('');
297 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
298 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
299 $pdf->ezText('',10);
302 function multiprintcss_header($p) {
303 echo("<div class='paddingdiv'>\n");
304 $this->providerid = $p->provider->id;
305 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
306 echo ("<tr>\n");
307 echo ("<td></td>\n");
308 echo ("<td>\n");
309 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
310 echo ("</td>\n");
311 echo ("</tr>\n");
312 echo ("<tr>\n");
313 echo ("<td>\n");
314 $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) . "'");
315 $patterns = array ('/\n/','/Tel:/','/Fax:/');
316 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
317 $res = preg_replace($patterns, $replace, $res);
318 echo ('<span class="large">' . $res['addr'] . '</span>');
319 echo ("</td>\n");
320 echo ("<td>\n");
321 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
323 if ($GLOBALS['rx_enable_DEA']) {
324 if ($GLOBALS['rx_show_DEA']) {
325 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
327 else {
328 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
331 if ($GLOBALS['rx_enable_NPI']) {
332 if ($GLOBALS['rx_show_NPI']) {
333 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
335 else {
336 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
339 if ($GLOBALS['rx_enable_SLN']) {
340 if ($GLOBALS['rx_show_SLN']) {
341 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
343 else {
344 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
347 echo ("</td>\n");
348 echo ("</tr>\n");
349 echo ("<tr>\n");
350 echo ("<td rowspan='2' class='bordered'>\n");
351 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
352 echo ($p->patient->get_name_display() . '<br>');
353 $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));
354 $patterns = array ('/\n/');
355 $replace = array ('<br>');
356 $res = preg_replace($patterns, $replace, $res);
357 echo ($res['addr']);
358 echo ("</td>\n");
359 echo ("<td class='bordered'>\n");
360 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
361 echo ($p->patient->date_of_birth );
362 echo ("</td>\n");
363 echo ("</tr>\n");
364 echo ("<tr>\n");
365 echo ("<td class='bordered'>\n");
366 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
367 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
368 echo ("</td>\n");
369 echo ("</tr>\n");
370 echo ("<tr>\n");
371 echo ("<td colspan='2' class='bordered'>\n");
372 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
373 echo ("</td>\n");
374 echo ("</tr>\n");
375 echo ("</table>\n");
378 function multiprintcss_preheader() {
379 // this sets styling and other header information of the multiprint css sheet
380 echo ("<html>\n");
381 echo ("<head>\n");
382 echo ("<style>\n");
383 echo ("div {\n");
384 echo (" padding: 0;\n");
385 echo (" margin: 0;\n");
386 echo ("}\n");
387 echo ("body {\n");
388 echo (" font-family: sans-serif;\n");
389 echo (" font-weight: normal;\n");
390 echo (" font-size: 10pt;\n");
391 echo (" background: white;\n");
392 echo (" color: black;\n");
393 echo ("}\n");
394 echo ("span.large {\n");
395 echo (" font-size: 12pt;\n");
396 echo ("}\n");
397 echo ("span.small {\n");
398 echo (" font-size: 6pt;\n");
399 echo ("}\n");
400 echo ("td {\n");
401 echo (" vertical-align: top;\n");
402 echo (" width: 50%;\n");
403 echo (" font-size: 10pt;\n");
404 echo (" padding-bottom: 8pt;\n");
405 echo ("}\n");
406 echo ("td.bordered {\n");
407 echo (" border-top:1pt solid black;\n");
408 echo ("}\n");
409 echo ("div.paddingdiv {\n");
410 echo (" width: 524pt;\n");
411 echo (" height: 668pt;\n");
412 echo ("}\n");
413 echo ("div.scriptdiv {\n");
414 echo (" padding-top: 12pt;\n");
415 echo (" padding-bottom: 22pt;\n");
416 echo (" padding-left: 35pt;\n");
417 echo (" border-bottom:1pt solid black;\n");
418 echo ("}\n");
419 echo ("div.signdiv {\n");
420 echo (" margin-top: 40pt;\n");
421 echo (" font-size: 12pt;\n");
422 echo ("}\n");
423 echo ("</style>\n");
425 echo ("<title>" . xl('Prescription') . "</title>\n");
426 echo ("</head>\n");
427 echo ("<body>\n");
430 function multiprintfax_footer( & $pdf ) {
431 return $this->multiprint_footer( $pdf );
434 function multiprint_footer(& $pdf) {
435 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
436 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
437 if (file_exists($sigfile)) {
438 $pdf->ezText( xl('Signature') . ": ",12);
439 // $pdf->ezImage($sigfile, "", "", "none", "left");
440 $pdf->ezImage($sigfile, "", "", "none", "center");
441 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
442 if ( $this->is_print_to_fax ) {
443 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
446 $addenumFile = $this->pconfig['addendum_file'];
447 if ( file_exists( $addenumFile ) ) {
448 $pdf->ezText('');
449 $f = fopen($addenumFile, "r");
450 while ( $line = fgets($f, 1000) ) {
451 $pdf->ezText(rtrim($line));
455 return;
458 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
461 function multiprintcss_footer() {
462 echo ("<div class='signdiv'>\n");
463 echo (xl('Signature') . ":________________________________<br>");
464 echo (xl('Date') . ": " . date('Y-m-d'));
465 echo ("</div>\n");
466 echo ("</div>\n");
469 function multiprintcss_postfooter() {
470 echo("<script language='JavaScript'>\n");
471 echo("window.print();\n");
472 echo("</script>\n");
473 echo("</body>\n");
474 echo("</html>\n");
477 function get_prescription_body_text($p) {
478 $body = '<b>' . xl('Rx') . ': ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
479 if ($p->get_form()) $body .= ' [' . $p->form_array[$p->get_form()] . "]";
480 $body .= "</b> <i>" .
481 $p->substitute_array[$p->get_substitute()] . "</i>\n" .
482 '<b>' . xl('Disp #') . ':</b> <u>' . $p->get_quantity() . "</u>\n" .
483 '<b>' . xl('Sig') . ':</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] . ' ' .
484 $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
485 if ($p->get_refills() > 0) {
486 $body .= "\n<b>" . xl('Refills') . ":</b> <u>" . $p->get_refills();
487 if ($p->get_per_refill()) {
488 $body .= " " . xl('of quantity') . " " . $p->get_per_refill();
490 $body .= "</u>\n";
492 else {
493 $body .= "\n<b>" . xl('Refills') . ":</b> <u>0 (" . xl('Zero') . ")</u>\n";
495 $note = $p->get_note();
496 if ($note != '') {
497 $body .= "\n$note\n";
499 return $body;
502 function multiprintfax_body(& $pdf, $p){
503 return $this->multiprint_body( $pdf, $p );
506 function multiprint_body(& $pdf, $p){
507 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
508 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
509 $d = $this->get_prescription_body_text($p);
510 if ( $pdf->ezText($d,10,array(),1) ) {
511 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
512 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
513 $this->multiprint_footer($pdf);
514 $pdf->ezNewPage();
515 $this->multiprint_header($pdf, $p);
516 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
517 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
519 $my_y = $pdf->y;
520 $pdf->ezText($d,10);
521 if($this->pconfig['shading']) {
522 $pdf->setColor(.9,.9,.9);
523 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
524 $pdf->setColor(0,0,0);
526 $pdf->ezSetY($my_y);
527 $pdf->ezText($d,10);
528 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
529 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
530 $pdf->ezText('');
531 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
532 $pdf->ezText('');
535 function multiprintcss_body($p){
536 $d = $this->get_prescription_body_text($p);
537 $patterns = array ('/\n/','/ /');
538 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
539 $d = preg_replace($patterns, $replace, $d);
540 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
543 function multiprintfax_action($id = "") {
544 $this->is_print_to_fax=true;
545 return $this->multiprint_action( $id );
548 function multiprint_action($id = "") {
549 $_POST['process'] = "true";
550 if(empty($id)) {
551 $this->function_argument_error();
553 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
554 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
555 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
556 ,$GLOBALS['rx_bottom_margin']
557 ,$GLOBALS['rx_left_margin']
558 ,$GLOBALS['rx_right_margin']
560 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
562 // $print_header = true;
563 $on_this_page = 0;
565 //print prescriptions body
566 $this->_state = false; // Added by Rod - see Controller.class.php
567 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
568 foreach ($ids as $id) {
569 $p = new Prescription($id);
570 // if ($print_header == true) {
571 if ($on_this_page == 0) {
572 $this->multiprint_header($pdf, $p);
574 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
575 $this->multiprint_footer($pdf);
576 $pdf->ezNewPage();
577 $this->multiprint_header($pdf, $p);
578 // $print_header = false;
579 $on_this_page = 1;
581 $this->multiprint_body($pdf, $p);
584 $this->multiprint_footer($pdf);
586 $pdf->ezStream();
587 return;
590 function multiprintcss_action($id = "") {
591 $_POST['process'] = "true";
592 if(empty($id)) {
593 $this->function_argument_error();
596 $this->multiprintcss_preheader();
598 $this->_state = false; // Added by Rod - see Controller.class.php
599 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
601 $on_this_page = 0;
602 foreach ($ids as $id) {
603 $p = new Prescription($id);
604 if ($on_this_page == 0) {
605 $this->multiprintcss_header($p);
607 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
608 $this->multiprintcss_footer();
609 $this->multiprintcss_header($p);
610 $on_this_page = 1;
612 $this->multiprintcss_body($p);
614 $this->multiprintcss_footer();
615 $this->multiprintcss_postfooter();
616 return;
619 function send_action_process($id) {
620 $dummy = ""; // Added by Rod to avoid run-time warnings
621 if ($_POST['process'] != "true")
622 return;
623 if(empty($id)) {
624 $this->function_argument_error();
626 $p = new Prescription($id);
627 switch ($_POST['submit']) {
629 case (xl("Print")." (".xl("PDF").")"):
630 // The following statement added by Rod.
631 // Looking at Controller.class.php, it appears that _state is set to false
632 // to indicate that no further HTML is to be generated.
633 $this->_state = false; // Added by Rod - see Controller.class.php
634 return $this->_print_prescription($p, $dummy);
635 break;
636 case (xl("Print")." (".xl("HTML").")"):
637 $this->_state = false;
638 return $this->_print_prescription_css($p, $dummy);
639 break;
640 case xl("Print To Fax"):
641 $this->_state = false;
642 $this->is_print_to_fax = true;
643 return $this->_print_prescription($p, $dummy);
644 break;
645 case xl("Email"):
646 return $this->_email_prescription($p,$_POST['email_to']);
647 break;
648 case xl("Fax"):
649 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
650 //$this->assign("process_result","No fax server is currently setup.");
651 return $this->_fax_prescription($p,$_POST['fax_to']);
652 break;
653 case xl("Auto Send"):
654 $pharmacy_id = $_POST['pharmacy_id'];
655 //echo "auto sending to : " . $_POST['pharmacy_id'];
656 $phar = new Pharmacy($_POST['pharmacy_id']);
657 //print_r($phar);
658 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
659 return $this->_print_prescription($p, $dummy);
661 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
662 $email = $phar->get_email();
663 if (!empty($email)) {
664 return $this->_email_prescription($p,$phar->get_email());
666 //else print it
668 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
669 $faxNum= $phar->get_fax();
670 if(!empty($faxNum)) {
671 Return $this->_fax_prescription ($p,$faxNum);
673 // return $this->assign("process_result","No fax server is currently setup.");
674 // else default is printing,
676 else {
677 //the pharmacy has no default or default is print
678 return $this->_print_prescription($p, $dummy);
680 break;
683 return;
687 function _print_prescription($p, & $toFile) {
688 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
689 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
690 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
691 ,$GLOBALS['rx_bottom_margin']
692 ,$GLOBALS['rx_left_margin']
693 ,$GLOBALS['rx_right_margin']
696 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
698 // Signature images are to be used only when faxing.
699 if(!empty($toFile)) $this->is_faxing = true;
701 $this->multiprint_header($pdf, $p);
702 $this->multiprint_body($pdf, $p);
703 $this->multiprint_footer($pdf);
705 if(!empty($toFile)) {
706 $toFile = $pdf->ezOutput();
708 else {
709 $pdf->ezStream();
710 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
712 return;
715 function _print_prescription_css($p, & $toFile) {
717 $this->multiprintcss_preheader();
718 $this->multiprintcss_header($p);
719 $this->multiprintcss_body($p);
720 $this->multiprintcss_footer();
721 $this->multiprintcss_postfooter();
725 function _print_prescription_old($p, & $toFile) {
726 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
727 $pdf =& new Cezpdf($GLOBALS['rx_paper_size']);
728 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
729 ,$GLOBALS['rx_bottom_margin']
730 ,$GLOBALS['rx_left_margin']
731 ,$GLOBALS['rx_right_margin']
733 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
734 if(!empty($this->pconfig['logo'])) {
735 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
737 $pdf->ezText($p->get_prescription_display(),10);
738 if($this->pconfig['use_signature']) {
739 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
741 else{
742 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
744 if(!empty($toFile))
746 $toFile = $pdf->ezOutput();
748 else
750 $pdf->ezStream();
751 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
753 return;
756 function _email_prescription($p,$email) {
757 if (empty($email)) {
758 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
759 return;
761 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
762 $mail = new PHPMailer();
763 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
764 //this is a temporary config item until the rest of the per practice billing settings make their way in
765 $mail->From = $GLOBALS['practice_return_email_path'];
766 $mail->FromName = $p->provider->get_name_display();
767 $mail->isMail();
768 $mail->Host = "localhost";
769 $mail->Mailer = "mail";
770 $text_body = $p->get_prescription_display();
771 $mail->Body = $text_body;
772 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
773 $mail->AddAddress($email);
774 if($mail->Send()) {
775 $this->assign("process_result","Email was successfully sent to: " . $email);
776 return;
778 else {
779 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
780 return;
784 function do_lookup() {
785 if ($_POST['process'] != "true") {
786 // don't do a lookup
787 $this->assign("drug", $_GET['drug']);
788 return;
791 // process the lookup
792 $this->assign("drug", $_POST['drug']);
793 $list = array();
794 if (!empty($_POST['drug'])) {
795 $list = @RxList::get_list($_POST['drug']);
798 if (is_array($list)) {
799 $list = array_flip($list);
800 $this->assign("drug_options",$list);
801 $this->assign("drug_values",array_keys($list));
803 else {
804 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
806 //print_r($_POST);
807 //$this->assign("PROCESS","");
809 $_POST['process'] = "";
812 function _fax_prescription($p,$faxNum)
814 $err = "Sent fax";
815 //strip - ,(, ), and ws
816 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
817 //validate the number
819 if(!empty($faxNum) && is_numeric($faxNum))
821 //get the sendfax command and execute it
822 $cmd = $this->pconfig['sendfax'];
823 // prepend any prefix to the fax number
824 $pref=$this->pconfig['prefix'];
825 $faxNum=$pref.$faxNum;
826 if(empty($cmd))
828 $err .= " Send fax not set in includes/config.php";
829 break;
831 else
833 //generate file to fax
834 $faxFile = "Failed";
835 $this->_print_prescription($p, $faxFile);
836 if(empty($faxFile))
838 $err .= " _print_prescription returned empty file";
839 break;
841 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
842 $p->get_patient_id() . "_fax_.pdf";
843 //print "filename is $fileName";
844 touch($fileName); // php bug
845 $handle = fopen($fileName,"w");
846 if(!$handle)
848 $err .= " Failed to open file $fileName to write fax to";
849 break;
851 if(fwrite($handle, $faxFile) === false)
853 $err .= " Failed to write data to $fileName";
854 break;
856 fclose($handle);
857 $args = " -n -d $faxNum $fileName";
858 //print "command is $cmd $args<br>";
859 exec($cmd . $args);
863 else
865 $err = "bad fax number passed to function";
867 if($err)
869 $this->assign("process_result",$err);