Emailing module: bug fixes to MyMailer class, so emailing should work. Also incorpora...
[openemr.git] / controllers / C_Prescription.class.php
blobb930bcc9d23a4fa2f6195c18fe01b7c0eccb60f9
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 (" page-break-after: always;\n");
368 echo ("}\n");
369 echo ("div.scriptdiv {\n");
370 echo (" padding-top: 12pt;\n");
371 echo (" padding-bottom: 22pt;\n");
372 echo (" padding-left: 35pt;\n");
373 echo (" border-bottom:1pt solid black;\n");
374 echo ("}\n");
375 echo ("div.signdiv {\n");
376 echo (" margin-top: 40pt;\n");
377 echo (" font-size: 12pt;\n");
378 echo ("}\n");
379 echo ("</style>\n");
381 echo ("<title>" . xl('Prescription') . "</title>\n");
382 echo ("</head>\n");
383 echo ("<body>\n");
386 function multiprintfax_footer( & $pdf ) {
387 return $this->multiprint_footer( $pdf );
390 function multiprint_footer(& $pdf) {
391 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
392 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
393 if (file_exists($sigfile)) {
394 $pdf->ezText( xl('Signature') . ": ",12);
395 // $pdf->ezImage($sigfile, "", "", "none", "left");
396 $pdf->ezImage($sigfile, "", "", "none", "center");
397 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
398 if ( $this->is_print_to_fax ) {
399 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
402 $addenumFile = $this->pconfig['addendum_file'];
403 if ( file_exists( $addenumFile ) ) {
404 $pdf->ezText('');
405 $f = fopen($addenumFile, "r");
406 while ( $line = fgets($f, 1000) ) {
407 $pdf->ezText(rtrim($line));
411 return;
414 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
417 function multiprintcss_footer() {
418 echo ("<div class='signdiv'>\n");
419 echo (xl('Signature') . ":________________________________<br>");
420 echo (xl('Date') . ": " . date('Y-m-d'));
421 echo ("</div>\n");
422 echo ("</div>\n");
425 function multiprintcss_postfooter() {
426 echo("<script language='JavaScript'>\n");
427 echo("window.print();\n");
428 echo("</script>\n");
429 echo("</body>\n");
430 echo("</html>\n");
433 function get_prescription_body_text($p) {
434 $body = '<b>' . xl('Rx') . ': ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
435 if ($p->get_form()) $body .= ' [' . $p->form_array[$p->get_form()] . "]";
436 $body .= "</b> <i>" .
437 $p->substitute_array[$p->get_substitute()] . "</i>\n" .
438 '<b>' . xl('Disp #') . ':</b> <u>' . $p->get_quantity() . "</u>\n" .
439 '<b>' . xl('Sig') . ':</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] . ' ' .
440 $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
441 if ($p->get_refills() > 0) {
442 $body .= "\n<b>" . xl('Refills') . ":</b> <u>" . $p->get_refills();
443 if ($p->get_per_refill()) {
444 $body .= " " . xl('of quantity') . " " . $p->get_per_refill();
446 $body .= "</u>\n";
448 else {
449 $body .= "\n<b>" . xl('Refills') . ":</b> <u>0 (" . xl('Zero') . ")</u>\n";
451 $note = $p->get_note();
452 if ($note != '') {
453 $body .= "\n$note\n";
455 return $body;
458 function multiprintfax_body(& $pdf, $p){
459 return $this->multiprint_body( $pdf, $p );
462 function multiprint_body(& $pdf, $p){
463 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
464 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
465 $d = $this->get_prescription_body_text($p);
466 if ( $pdf->ezText($d,10,array(),1) ) {
467 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
468 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
469 $this->multiprint_footer($pdf);
470 $pdf->ezNewPage();
471 $this->multiprint_header($pdf, $p);
472 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
473 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
475 $my_y = $pdf->y;
476 $pdf->ezText($d,10);
477 if($this->pconfig['shading']) {
478 $pdf->setColor(.9,.9,.9);
479 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
480 $pdf->setColor(0,0,0);
482 $pdf->ezSetY($my_y);
483 $pdf->ezText($d,10);
484 $pdf->ez['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
485 $pdf->ez['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
486 $pdf->ezText('');
487 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
488 $pdf->ezText('');
491 function multiprintcss_body($p){
492 $d = $this->get_prescription_body_text($p);
493 $patterns = array ('/\n/','/ /');
494 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
495 $d = preg_replace($patterns, $replace, $d);
496 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
499 function multiprintfax_action($id = "") {
500 $this->is_print_to_fax=true;
501 return $this->multiprint_action( $id );
504 function multiprint_action($id = "") {
505 $_POST['process'] = "true";
506 if(empty($id)) {
507 $this->function_argument_error();
509 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
510 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
511 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
512 ,$GLOBALS['oer_config']['prescriptions']['bottom']
513 ,$GLOBALS['oer_config']['prescriptions']['left']
514 ,$GLOBALS['oer_config']['prescriptions']['right']
516 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
518 // $print_header = true;
519 $on_this_page = 0;
521 //print prescriptions body
522 $this->_state = false; // Added by Rod - see Controller.class.php
523 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
524 foreach ($ids as $id) {
525 $p = new Prescription($id);
526 // if ($print_header == true) {
527 if ($on_this_page == 0) {
528 $this->multiprint_header($pdf, $p);
530 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
531 $this->multiprint_footer($pdf);
532 $pdf->ezNewPage();
533 $this->multiprint_header($pdf, $p);
534 // $print_header = false;
535 $on_this_page = 1;
537 $this->multiprint_body($pdf, $p);
540 $this->multiprint_footer($pdf);
542 $pdf->ezStream();
543 return;
546 function multiprintcss_action($id = "") {
547 $_POST['process'] = "true";
548 if(empty($id)) {
549 $this->function_argument_error();
552 $this->multiprintcss_preheader();
554 $this->_state = false; // Added by Rod - see Controller.class.php
555 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
557 $on_this_page = 0;
558 foreach ($ids as $id) {
559 $p = new Prescription($id);
560 if ($on_this_page == 0) {
561 $this->multiprintcss_header($p);
563 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
564 $this->multiprintcss_footer();
565 $this->multiprintcss_header($p);
566 $on_this_page = 1;
568 $this->multiprintcss_body($p);
570 $this->multiprintcss_footer();
571 $this->multiprintcss_postfooter();
572 return;
575 function send_action_process($id) {
576 $dummy = ""; // Added by Rod to avoid run-time warnings
577 if ($_POST['process'] != "true")
578 return;
579 if(empty($id)) {
580 $this->function_argument_error();
582 $p = new Prescription($id);
583 switch ($_POST['submit']) {
585 case (xl("Print")." (".xl("PDF").")"):
586 // The following statement added by Rod.
587 // Looking at Controller.class.php, it appears that _state is set to false
588 // to indicate that no further HTML is to be generated.
589 $this->_state = false; // Added by Rod - see Controller.class.php
590 return $this->_print_prescription($p, $dummy);
591 break;
592 case (xl("Print")." (".xl("HTML").")"):
593 $this->_state = false;
594 return $this->_print_prescription_css($p, $dummy);
595 break;
596 case xl("Print To Fax"):
597 $this->_state = false;
598 $this->is_print_to_fax = true;
599 return $this->_print_prescription($p, $dummy);
600 break;
601 case xl("Email"):
602 return $this->_email_prescription($p,$_POST['email_to']);
603 break;
604 case xl("Fax"):
605 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
606 //$this->assign("process_result","No fax server is currently setup.");
607 return $this->_fax_prescription($p,$_POST['fax_to']);
608 break;
609 case xl("Auto Send"):
610 $pharmacy_id = $_POST['pharmacy_id'];
611 //echo "auto sending to : " . $_POST['pharmacy_id'];
612 $phar = new Pharmacy($_POST['pharmacy_id']);
613 //print_r($phar);
614 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
615 return $this->_print_prescription($p, $dummy);
617 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
618 $email = $phar->get_email();
619 if (!empty($email)) {
620 return $this->_email_prescription($p,$phar->get_email());
622 //else print it
624 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
625 $faxNum= $phar->get_fax();
626 if(!empty($faxNum)) {
627 Return $this->_fax_prescription ($p,$faxNum);
629 // return $this->assign("process_result","No fax server is currently setup.");
630 // else default is printing,
632 else {
633 //the pharmacy has no default or default is print
634 return $this->_print_prescription($p, $dummy);
636 break;
639 return;
643 function _print_prescription($p, & $toFile) {
644 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
645 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
646 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
647 ,$GLOBALS['oer_config']['prescriptions']['bottom']
648 ,$GLOBALS['oer_config']['prescriptions']['left']
649 ,$GLOBALS['oer_config']['prescriptions']['right']
652 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
654 // Signature images are to be used only when faxing.
655 if(!empty($toFile)) $this->is_faxing = true;
657 $this->multiprint_header($pdf, $p);
658 $this->multiprint_body($pdf, $p);
659 $this->multiprint_footer($pdf);
661 if(!empty($toFile)) {
662 $toFile = $pdf->ezOutput();
664 else {
665 $pdf->ezStream();
666 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
668 return;
671 function _print_prescription_css($p, & $toFile) {
673 $this->multiprintcss_preheader();
674 $this->multiprintcss_header($p);
675 $this->multiprintcss_body($p);
676 $this->multiprintcss_footer();
677 $this->multiprintcss_postfooter();
681 function _print_prescription_old($p, & $toFile) {
682 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
683 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
684 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
685 ,$GLOBALS['oer_config']['prescriptions']['bottom']
686 ,$GLOBALS['oer_config']['prescriptions']['left']
687 ,$GLOBALS['oer_config']['prescriptions']['right']
689 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
690 if(!empty($this->pconfig['logo'])) {
691 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
693 $pdf->ezText($p->get_prescription_display(),10);
694 if($this->pconfig['use_signature']) {
695 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
697 else{
698 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
700 if(!empty($toFile))
702 $toFile = $pdf->ezOutput();
704 else
706 $pdf->ezStream();
707 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
709 return;
712 function _email_prescription($p,$email) {
713 if (empty($email)) {
714 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
715 return;
717 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
718 $mail = new PHPMailer();
719 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
720 //this is a temporary config item until the rest of the per practice billing settings make their way in
721 $mail->From = $GLOBALS['practice_return_email_path'];
722 $mail->FromName = $p->provider->get_name_display();
723 $mail->isMail();
724 $mail->Host = "localhost";
725 $mail->Mailer = "mail";
726 $text_body = $p->get_prescription_display();
727 $mail->Body = $text_body;
728 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
729 $mail->AddAddress($email);
730 if($mail->Send()) {
731 $this->assign("process_result","Email was successfully sent to: " . $email);
732 return;
734 else {
735 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
736 return;
740 function do_lookup() {
741 if ($_POST['process'] != "true") {
742 // don't do a lookup
743 $this->assign("drug", $_GET['drug']);
744 return;
747 // process the lookup
748 $this->assign("drug", $_POST['drug']);
749 $list = array();
750 if (!empty($_POST['drug'])) {
751 $list = @RxList::get_list($_POST['drug']);
754 if (is_array($list)) {
755 $list = array_flip($list);
756 $this->assign("drug_options",$list);
757 $this->assign("drug_values",array_keys($list));
759 else {
760 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
762 //print_r($_POST);
763 //$this->assign("PROCESS","");
765 $_POST['process'] = "";
768 function _fax_prescription($p,$faxNum)
770 $err = "Sent fax";
771 //strip - ,(, ), and ws
772 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
773 //validate the number
775 if(!empty($faxNum) && is_numeric($faxNum))
777 //get the sendfax command and execute it
778 $cmd = $this->pconfig['sendfax'];
779 // prepend any prefix to the fax number
780 $pref=$this->pconfig['prefix'];
781 $faxNum=$pref.$faxNum;
782 if(empty($cmd))
784 $err .= " Send fax not set in includes/config.php";
785 break;
787 else
789 //generate file to fax
790 $faxFile = "Failed";
791 $this->_print_prescription($p, $faxFile);
792 if(empty($faxFile))
794 $err .= " _print_prescription returned empty file";
795 break;
797 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
798 $p->get_patient_id() . "_fax_.pdf";
799 //print "filename is $fileName";
800 touch($fileName); // php bug
801 $handle = fopen($fileName,"w");
802 if(!$handle)
804 $err .= " Failed to open file $fileName to write fax to";
805 break;
807 if(fwrite($handle, $faxFile) === false)
809 $err .= " Failed to write data to $fileName";
810 break;
812 fclose($handle);
813 $args = " -n -d $faxNum $fileName";
814 //print "command is $cmd $args<br>";
815 exec($cmd . $args);
819 else
821 $err = "bad fax number passed to function";
823 if($err)
825 $this->assign("process_result",$err);