incremented patch version 13
[openemr.git] / controllers / C_Prescription.class.php
blobabc64e70b3dd15cff19748a449f1186d7e5f0f2b
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 // <openemr root>/includes/config.php - Tekknogenius
245 if ($this->is_faxing || $GLOBALS['oer_config']['prescriptions']['show_DEA'])
246 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
247 else
248 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
249 $pdf->ezColumnsStop();
250 if ($my_y < $pdf->y){
251 $pdf->ezSetY($my_y);
253 $pdf->ezText('',10);
254 $pdf->setLineStyle(1);
255 $pdf->ezColumnsStart(array('num'=>2));
256 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
257 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
258 $pdf->ezText($p->patient->get_name_display(),10);
259 $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));
260 $pdf->ezText($res['addr']);
261 $my_y = $pdf->y;
262 $pdf->ezNewPage();
263 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
264 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
265 $pdf->ezText($p->patient->date_of_birth,10);
266 $pdf->ezText('');
267 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
268 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
269 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
270 $pdf->ezColumnsStop();
271 if ($my_y < $pdf->y){
272 $pdf->ezSetY($my_y);
274 $pdf->ezText('');
275 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
276 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
277 $pdf->ezText('',10);
280 function multiprintcss_header($p) {
281 echo("<div class='paddingdiv'>\n");
282 $this->providerid = $p->provider->id;
283 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
284 echo ("<tr>\n");
285 echo ("<td></td>\n");
286 echo ("<td>\n");
287 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
288 echo ("</td>\n");
289 echo ("</tr>\n");
290 echo ("<tr>\n");
291 echo ("<td>\n");
292 $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) . "'");
293 $patterns = array ('/\n/','/Tel:/','/Fax:/');
294 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
295 $res = preg_replace($patterns, $replace, $res);
296 echo ('<span class="large">' . $res['addr'] . '</span>');
297 echo ("</td>\n");
298 echo ("<td>\n");
299 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
300 if ($GLOBALS['oer_config']['prescriptions']['show_DEA']) echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span>');
301 else echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________');
302 echo ("</td>\n");
303 echo ("</tr>\n");
304 echo ("<tr>\n");
305 echo ("<td rowspan='2' class='bordered'>\n");
306 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
307 echo ($p->patient->get_name_display() . '<br>');
308 $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));
309 $patterns = array ('/\n/');
310 $replace = array ('<br>');
311 $res = preg_replace($patterns, $replace, $res);
312 echo ($res['addr']);
313 echo ("</td>\n");
314 echo ("<td class='bordered'>\n");
315 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
316 echo ($p->patient->date_of_birth );
317 echo ("</td>\n");
318 echo ("</tr>\n");
319 echo ("<tr>\n");
320 echo ("<td class='bordered'>\n");
321 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
322 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
323 echo ("</td>\n");
324 echo ("</tr>\n");
325 echo ("<tr>\n");
326 echo ("<td colspan='2' class='bordered'>\n");
327 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
328 echo ("</td>\n");
329 echo ("</tr>\n");
330 echo ("</table>\n");
333 function multiprintcss_preheader() {
334 // this sets styling and other header information of the multiprint css sheet
335 echo ("<html>\n");
336 echo ("<head>\n");
337 echo ("<style>\n");
338 echo ("div {\n");
339 echo (" padding: 0;\n");
340 echo (" margin: 0;\n");
341 echo ("}\n");
342 echo ("body {\n");
343 echo (" font-family: sans-serif;\n");
344 echo (" font-weight: normal;\n");
345 echo (" font-size: 10pt;\n");
346 echo (" background: white;\n");
347 echo (" color: black;\n");
348 echo ("}\n");
349 echo ("span.large {\n");
350 echo (" font-size: 12pt;\n");
351 echo ("}\n");
352 echo ("span.small {\n");
353 echo (" font-size: 6pt;\n");
354 echo ("}\n");
355 echo ("td {\n");
356 echo (" vertical-align: top;\n");
357 echo (" width: 50%;\n");
358 echo (" font-size: 10pt;\n");
359 echo (" padding-bottom: 8pt;\n");
360 echo ("}\n");
361 echo ("td.bordered {\n");
362 echo (" border-top:1pt solid black;\n");
363 echo ("}\n");
364 echo ("div.paddingdiv {\n");
365 echo (" width: 524pt;\n");
366 echo (" height: 668pt;\n");
367 echo ("}\n");
368 echo ("div.scriptdiv {\n");
369 echo (" padding-top: 12pt;\n");
370 echo (" padding-bottom: 22pt;\n");
371 echo (" padding-left: 35pt;\n");
372 echo (" border-bottom:1pt solid black;\n");
373 echo ("}\n");
374 echo ("div.signdiv {\n");
375 echo (" margin-top: 40pt;\n");
376 echo (" font-size: 12pt;\n");
377 echo ("}\n");
378 echo ("</style>\n");
380 echo ("<title>" . xl('Prescription') . "</title>\n");
381 echo ("</head>\n");
382 echo ("<body>\n");
385 function multiprintfax_footer( & $pdf ) {
386 return $this->multiprint_footer( $pdf );
389 function multiprint_footer(& $pdf) {
390 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
391 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
392 if (file_exists($sigfile)) {
393 $pdf->ezText( xl('Signature') . ": ",12);
394 // $pdf->ezImage($sigfile, "", "", "none", "left");
395 $pdf->ezImage($sigfile, "", "", "none", "center");
396 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
397 if ( $this->is_print_to_fax ) {
398 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
401 $addenumFile = $this->pconfig['addendum_file'];
402 if ( file_exists( $addenumFile ) ) {
403 $pdf->ezText('');
404 $f = fopen($addenumFile, "r");
405 while ( $line = fgets($f, 1000) ) {
406 $pdf->ezText(rtrim($line));
410 return;
413 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
416 function multiprintcss_footer() {
417 echo ("<div class='signdiv'>\n");
418 echo (xl('Signature') . ":________________________________<br>");
419 echo (xl('Date') . ": " . date('Y-m-d'));
420 echo ("</div>\n");
421 echo ("</div>\n");
424 function multiprintcss_postfooter() {
425 echo("<script language='JavaScript'>\n");
426 echo("window.print();\n");
427 echo("</script>\n");
428 echo("</body>\n");
429 echo("</html>\n");
432 function get_prescription_body_text($p) {
433 $body = '<b>' . xl('Rx') . ': ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
434 if ($p->get_form()) $body .= ' [' . $p->form_array[$p->get_form()] . "]";
435 $body .= "</b> <i>" .
436 $p->substitute_array[$p->get_substitute()] . "</i>\n" .
437 '<b>' . xl('Disp #') . ':</b> <u>' . $p->get_quantity() . "</u>\n" .
438 '<b>' . xl('Sig') . ':</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] . ' ' .
439 $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
440 if ($p->get_refills() > 0) {
441 $body .= "\n<b>" . xl('Refills') . ":</b> <u>" . $p->get_refills();
442 if ($p->get_per_refill()) {
443 $body .= " " . xl('of quantity') . " " . $p->get_per_refill();
445 $body .= "</u>\n";
447 else {
448 $body .= "\n<b>" . xl('Refills') . ":</b> <u>0 (" . xl('Zero') . ")</u>\n";
450 $note = $p->get_note();
451 if ($note != '') {
452 $body .= "\n$note\n";
454 return $body;
457 function multiprintfax_body(& $pdf, $p){
458 return $this->multiprint_body( $pdf, $p );
461 function multiprint_body(& $pdf, $p){
462 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
463 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
464 $d = $this->get_prescription_body_text($p);
465 if ( $pdf->ezText($d,10,array(),1) ) {
466 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
467 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
468 $this->multiprint_footer($pdf);
469 $pdf->ezNewPage();
470 $this->multiprint_header($pdf, $p);
471 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
472 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
474 $my_y = $pdf->y;
475 $pdf->ezText($d,10);
476 if($this->pconfig['shading']) {
477 $pdf->setColor(.9,.9,.9);
478 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
479 $pdf->setColor(0,0,0);
481 $pdf->ezSetY($my_y);
482 $pdf->ezText($d,10);
483 $pdf->ez['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
484 $pdf->ez['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
485 $pdf->ezText('');
486 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
487 $pdf->ezText('');
490 function multiprintcss_body($p){
491 $d = $this->get_prescription_body_text($p);
492 $patterns = array ('/\n/','/ /');
493 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
494 $d = preg_replace($patterns, $replace, $d);
495 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
498 function multiprintfax_action($id = "") {
499 $this->is_print_to_fax=true;
500 return $this->multiprint_action( $id );
503 function multiprint_action($id = "") {
504 $_POST['process'] = "true";
505 if(empty($id)) {
506 $this->function_argument_error();
508 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
509 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
510 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
511 ,$GLOBALS['oer_config']['prescriptions']['bottom']
512 ,$GLOBALS['oer_config']['prescriptions']['left']
513 ,$GLOBALS['oer_config']['prescriptions']['right']
515 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
517 // $print_header = true;
518 $on_this_page = 0;
520 //print prescriptions body
521 $this->_state = false; // Added by Rod - see Controller.class.php
522 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
523 foreach ($ids as $id) {
524 $p = new Prescription($id);
525 // if ($print_header == true) {
526 if ($on_this_page == 0) {
527 $this->multiprint_header($pdf, $p);
529 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
530 $this->multiprint_footer($pdf);
531 $pdf->ezNewPage();
532 $this->multiprint_header($pdf, $p);
533 // $print_header = false;
534 $on_this_page = 1;
536 $this->multiprint_body($pdf, $p);
539 $this->multiprint_footer($pdf);
541 $pdf->ezStream();
542 return;
545 function multiprintcss_action($id = "") {
546 $_POST['process'] = "true";
547 if(empty($id)) {
548 $this->function_argument_error();
551 $this->multiprintcss_preheader();
553 $this->_state = false; // Added by Rod - see Controller.class.php
554 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
556 $on_this_page = 0;
557 foreach ($ids as $id) {
558 $p = new Prescription($id);
559 if ($on_this_page == 0) {
560 $this->multiprintcss_header($p);
562 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
563 $this->multiprintcss_footer();
564 $this->multiprintcss_header($p);
565 $on_this_page = 1;
567 $this->multiprintcss_body($p);
569 $this->multiprintcss_footer();
570 $this->multiprintcss_postfooter();
571 return;
574 function send_action_process($id) {
575 $dummy = ""; // Added by Rod to avoid run-time warnings
576 if ($_POST['process'] != "true")
577 return;
578 if(empty($id)) {
579 $this->function_argument_error();
581 $p = new Prescription($id);
582 switch ($_POST['submit']) {
584 case (xl("Print")." (".xl("PDF").")"):
585 // The following statement added by Rod.
586 // Looking at Controller.class.php, it appears that _state is set to false
587 // to indicate that no further HTML is to be generated.
588 $this->_state = false; // Added by Rod - see Controller.class.php
589 return $this->_print_prescription($p, $dummy);
590 break;
591 case (xl("Print")." (".xl("HTML").")"):
592 $this->_state = false;
593 return $this->_print_prescription_css($p, $dummy);
594 break;
595 case xl("Print To Fax"):
596 $this->_state = false;
597 $this->is_print_to_fax = true;
598 return $this->_print_prescription($p, $dummy);
599 break;
600 case xl("Email"):
601 return $this->_email_prescription($p,$_POST['email_to']);
602 break;
603 case xl("Fax"):
604 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
605 //$this->assign("process_result","No fax server is currently setup.");
606 return $this->_fax_prescription($p,$_POST['fax_to']);
607 break;
608 case xl("Auto Send"):
609 $pharmacy_id = $_POST['pharmacy_id'];
610 //echo "auto sending to : " . $_POST['pharmacy_id'];
611 $phar = new Pharmacy($_POST['pharmacy_id']);
612 //print_r($phar);
613 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
614 return $this->_print_prescription($p, $dummy);
616 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
617 $email = $phar->get_email();
618 if (!empty($email)) {
619 return $this->_email_prescription($p,$phar->get_email());
621 //else print it
623 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
624 $faxNum= $phar->get_fax();
625 if(!empty($faxNum)) {
626 Return $this->_fax_prescription ($p,$faxNum);
628 // return $this->assign("process_result","No fax server is currently setup.");
629 // else default is printing,
631 else {
632 //the pharmacy has no default or default is print
633 return $this->_print_prescription($p, $dummy);
635 break;
638 return;
642 function _print_prescription($p, & $toFile) {
643 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
644 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
645 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
646 ,$GLOBALS['oer_config']['prescriptions']['bottom']
647 ,$GLOBALS['oer_config']['prescriptions']['left']
648 ,$GLOBALS['oer_config']['prescriptions']['right']
651 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
653 // Signature images are to be used only when faxing.
654 if(!empty($toFile)) $this->is_faxing = true;
656 $this->multiprint_header($pdf, $p);
657 $this->multiprint_body($pdf, $p);
658 $this->multiprint_footer($pdf);
660 if(!empty($toFile)) {
661 $toFile = $pdf->ezOutput();
663 else {
664 $pdf->ezStream();
665 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
667 return;
670 function _print_prescription_css($p, & $toFile) {
672 $this->multiprintcss_preheader();
673 $this->multiprintcss_header($p);
674 $this->multiprintcss_body($p);
675 $this->multiprintcss_footer();
676 $this->multiprintcss_postfooter();
680 function _print_prescription_old($p, & $toFile) {
681 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
682 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
683 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
684 ,$GLOBALS['oer_config']['prescriptions']['bottom']
685 ,$GLOBALS['oer_config']['prescriptions']['left']
686 ,$GLOBALS['oer_config']['prescriptions']['right']
688 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
689 if(!empty($this->pconfig['logo'])) {
690 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
692 $pdf->ezText($p->get_prescription_display(),10);
693 if($this->pconfig['use_signature']) {
694 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
696 else{
697 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
699 if(!empty($toFile))
701 $toFile = $pdf->ezOutput();
703 else
705 $pdf->ezStream();
706 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
708 return;
711 function _email_prescription($p,$email) {
712 if (empty($email)) {
713 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
714 return;
716 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
717 $mail = new PHPMailer();
718 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
719 //this is a temporary config item until the rest of the per practice billing settings make their way in
720 $mail->From = $GLOBALS['practice_return_email_path'];
721 $mail->FromName = $p->provider->get_name_display();
722 $mail->isMail();
723 $mail->Host = "localhost";
724 $mail->Mailer = "mail";
725 $text_body = $p->get_prescription_display();
726 $mail->Body = $text_body;
727 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
728 $mail->AddAddress($email);
729 if($mail->Send()) {
730 $this->assign("process_result","Email was successfully sent to: " . $email);
731 return;
733 else {
734 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
735 return;
739 function do_lookup() {
740 if ($_POST['process'] != "true") {
741 // don't do a lookup
742 $this->assign("drug", $_GET['drug']);
743 return;
746 // process the lookup
747 $this->assign("drug", $_POST['drug']);
748 $list = array();
749 if (!empty($_POST['drug'])) {
750 $list = @RxList::get_list($_POST['drug']);
753 if (is_array($list)) {
754 $list = array_flip($list);
755 $this->assign("drug_options",$list);
756 $this->assign("drug_values",array_keys($list));
758 else {
759 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
761 //print_r($_POST);
762 //$this->assign("PROCESS","");
764 $_POST['process'] = "";
767 function _fax_prescription($p,$faxNum)
769 $err = "Sent fax";
770 //strip - ,(, ), and ws
771 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
772 //validate the number
774 if(!empty($faxNum) && is_numeric($faxNum))
776 //get the sendfax command and execute it
777 $cmd = $this->pconfig['sendfax'];
778 // prepend any prefix to the fax number
779 $pref=$this->pconfig['prefix'];
780 $faxNum=$pref.$faxNum;
781 if(empty($cmd))
783 $err .= " Send fax not set in includes/config.php";
784 break;
786 else
788 //generate file to fax
789 $faxFile = "Failed";
790 $this->_print_prescription($p, $faxFile);
791 if(empty($faxFile))
793 $err .= " _print_prescription returned empty file";
794 break;
796 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
797 $p->get_patient_id() . "_fax_.pdf";
798 //print "filename is $fileName";
799 touch($fileName); // php bug
800 $handle = fopen($fileName,"w");
801 if(!$handle)
803 $err .= " Failed to open file $fileName to write fax to";
804 break;
806 if(fwrite($handle, $faxFile) === false)
808 $err .= " Failed to write data to $fileName";
809 break;
811 fclose($handle);
812 $args = " -n -d $faxNum $fileName";
813 //print "command is $cmd $args<br>";
814 exec($cmd . $args);
818 else
820 $err = "bad fax number passed to function";
822 if($err)
824 $this->assign("process_result",$err);