mispelling fix by Jason Brooks - artifact 2951436
[openemr.git] / controllers / C_Prescription.class.php
blobc52559448ca8597d5a8914846cc31403fd91ad03
1 <?php
3 require_once($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
4 require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
5 require_once($GLOBALS['fileroot'] . "/library/classes/Provider.class.php");
6 require_once($GLOBALS['fileroot'] . "/library/classes/RXList.class.php");
7 require_once($GLOBALS['fileroot'] . "/library/registry.inc");
9 class C_Prescription extends Controller {
11 var $template_mod;
12 var $pconfig;
13 var $providerid = 0;
14 var $is_faxing = false;
15 var $is_print_to_fax = false;
17 function C_Prescription($template_mod = "general") {
18 parent::Controller();
20 $this->template_mod = $template_mod;
21 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
22 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
23 $this->assign("STYLE", $GLOBALS['style']);
24 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
25 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
26 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
27 $this->assign("CSS_HEADER", $GLOBALS['css_header'] );
29 if ($GLOBALS['inhouse_pharmacy']) {
30 // Make an array of drug IDs and selectors for the template.
31 $drug_array_values = array(0);
32 $drug_array_output = array("-- or select from inventory --");
33 $drug_attributes = '';
35 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
37 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
38 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
39 "t.period, t.quantity, t.refills " .
40 "FROM drug_templates AS t, drugs AS d WHERE " .
41 "d.drug_id = t.drug_id ORDER BY t.selector");
43 while ($row = sqlFetchArray($res)) {
44 $tmp_output = $row['selector'];
45 if ($row['ndc_number']) {
46 $tmp_output .= ' [' . $row['ndc_number'] . ']';
48 $drug_array_values[] = $row['drug_id'];
49 $drug_array_output[] = $tmp_output;
50 if ($drug_attributes) $drug_attributes .= ',';
51 $drug_attributes .= "['" .
52 $row['name'] . "'," . // 0
53 $row['form'] . ",'" . // 1
54 $row['dosage'] . "'," . // 2
55 $row['size'] . "," . // 3
56 $row['unit'] . "," . // 4
57 $row['route'] . "," . // 5
58 $row['period'] . "," . // 6
59 $row['substitute'] . "," . // 7
60 $row['quantity'] . "," . // 8
61 $row['refills'] . "," . // 9
62 $row['quantity'] . "]"; // 10 quantity per_refill
64 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
65 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
66 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
70 function default_action() {
71 $this->assign("prescription",$this->prescriptions[0]);
72 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
75 function edit_action($id = "",$patient_id="",$p_obj = null) {
77 if ($p_obj != null && get_class($p_obj) == "prescription") {
78 $this->prescriptions[0] = $p_obj;
80 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
81 $this->prescriptions[0] = new Prescription($id);
84 if (!empty($patient_id)) {
85 $this->prescriptions[0]->set_patient_id($patient_id);
88 // If quantity to dispense is not already set from a POST, set its
89 // default value.
90 if (! $this->get_template_vars('DISP_QUANTITY')) {
91 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
94 $this->default_action();
97 function list_action($id,$sort = "") {
98 if (empty($id)) {
99 $this->function_argument_error();
100 exit;
102 if (!empty($sort)) {
103 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
105 else {
106 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
109 // flag to indicate the CAMOS form is regsitered and active
110 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
112 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
115 function block_action($id,$sort = "") {
116 if (empty($id)) {
117 $this->function_argument_error();
118 exit;
120 if (!empty($sort)) {
121 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
123 else {
124 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
126 //print_r(Prescription::prescriptions_factory($id));
127 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
130 function fragment_action($id,$sort = "") {
131 if (empty($id)) {
132 $this->function_argument_error();
133 exit;
135 if (!empty($sort)) {
136 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
138 else {
139 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
141 //print_r(Prescription::prescriptions_factory($id));
142 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
145 function lookup_action() {
146 $this->do_lookup();
147 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
150 function edit_action_process() {
151 if ($_POST['process'] != "true")
152 return;
153 //print_r($_POST);
155 // Stupid Smarty code treats empty values as not specified values.
156 // Since active is a checkbox, represent the unchecked state as -1.
157 if (empty($_POST['active'])) $_POST['active'] = '-1';
159 $this->prescriptions[0] = new Prescription($_POST['id']);
160 parent::populate_object($this->prescriptions[0]);
161 //echo $this->prescriptions[0]->toString(true);
162 $this->prescriptions[0]->persist();
163 $_POST['process'] = "";
165 // If the "Prescribe and Dispense" button was clicked, then
166 // redisplay as in edit_action() but also replicate the fee and
167 // include a piece of javascript to call dispense().
169 if ($_POST['disp_button']) {
170 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
171 $this->assign("DISP_FEE", $_POST['disp_fee']);
172 $this->assign("ENDING_JAVASCRIPT", "dispense();");
173 $this->_state = false;
174 return $this->edit_action($this->prescriptions[0]->id);
177 if ($this->prescriptions[0]->get_active() > 0) {
178 return $this->send_action($this->prescriptions[0]->id);
180 $this->list_action($this->prescriptions[0]->get_patient_id());
181 exit;
184 function send_action($id) {
185 $_POST['process'] = "true";
186 if(empty($id)) {
187 $this->function_argument_error();
190 $rx = new Prescription($id);
191 // Populate pharmacy info if the patient has a default pharmacy.
192 // Probably the Prescription object should handle this instead, but
193 // doing it there will require more careful research and testing.
194 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
195 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
196 if ($prow['pharmacy_id']) {
197 $rx->pharmacy->set_id($prow['pharmacy_id']);
198 $rx->pharmacy->populate();
200 $this->assign("prescription", $rx);
202 $this->_state = false;
203 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
204 $this->template_mod . "_send.html");
207 function multiprintfax_header(& $pdf, $p) {
208 return $this->multiprint_header( $pdf, $p );
211 function multiprint_header(& $pdf, $p) {
212 $this->providerid = $p->provider->id;
213 //print header
214 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'],'','50','','center','');
215 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
216 $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 ='" .
217 mysql_real_escape_string($p->provider->id) . "'");
218 $pdf->ezText($res['addr'],12);
219 $my_y = $pdf->y;
220 $pdf->ezNewPage();
221 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
222 // A client had a bad experience with a patient misusing a DEA number, so
223 // now the doctors write those in on printed prescriptions and only when
224 // necessary. If you need to change this back, then please make it a
225 // configurable option. Faxed prescriptions were not changed. -- Rod
226 // Now it is configureable. Change value in
227 // <openemr root>/includes/config.php - Tekknogenius
228 if ($this->is_faxing || $GLOBALS['oer_config']['prescriptions']['show_DEA'])
229 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
230 else
231 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
232 $pdf->ezColumnsStop();
233 if ($my_y < $pdf->y){
234 $pdf->ezSetY($my_y);
236 $pdf->ezText('',10);
237 $pdf->setLineStyle(1);
238 $pdf->ezColumnsStart(array('num'=>2));
239 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
240 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
241 $pdf->ezText($p->patient->get_name_display(),10);
242 $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));
243 $pdf->ezText($res['addr']);
244 $my_y = $pdf->y;
245 $pdf->ezNewPage();
246 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
247 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
248 $pdf->ezText($p->patient->date_of_birth,10);
249 $pdf->ezText('');
250 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
251 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
252 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
253 $pdf->ezColumnsStop();
254 if ($my_y < $pdf->y){
255 $pdf->ezSetY($my_y);
257 $pdf->ezText('');
258 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
259 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
260 $pdf->ezText('',10);
263 function multiprintcss_header($p) {
264 echo("<div class='paddingdiv'>\n");
265 $this->providerid = $p->provider->id;
266 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
267 echo ("<tr>\n");
268 echo ("<td></td>\n");
269 echo ("<td>\n");
270 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
271 echo ("</td>\n");
272 echo ("</tr>\n");
273 echo ("<tr>\n");
274 echo ("<td>\n");
275 $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) . "'");
276 $patterns = array ('/\n/','/Tel:/','/Fax:/');
277 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
278 $res = preg_replace($patterns, $replace, $res);
279 echo ('<span class="large">' . $res['addr'] . '</span>');
280 echo ("</td>\n");
281 echo ("<td>\n");
282 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
283 if ($GLOBALS['oer_config']['prescriptions']['show_DEA']) echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span>');
284 else echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________');
285 echo ("</td>\n");
286 echo ("</tr>\n");
287 echo ("<tr>\n");
288 echo ("<td rowspan='2' class='bordered'>\n");
289 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
290 echo ($p->patient->get_name_display() . '<br>');
291 $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));
292 $patterns = array ('/\n/');
293 $replace = array ('<br>');
294 $res = preg_replace($patterns, $replace, $res);
295 echo ($res['addr']);
296 echo ("</td>\n");
297 echo ("<td class='bordered'>\n");
298 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
299 echo ($p->patient->date_of_birth );
300 echo ("</td>\n");
301 echo ("</tr>\n");
302 echo ("<tr>\n");
303 echo ("<td class='bordered'>\n");
304 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
305 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
306 echo ("</td>\n");
307 echo ("</tr>\n");
308 echo ("<tr>\n");
309 echo ("<td colspan='2' class='bordered'>\n");
310 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
311 echo ("</td>\n");
312 echo ("</tr>\n");
313 echo ("</table>\n");
316 function multiprintcss_preheader() {
317 // this sets styling and other header information of the multiprint css sheet
318 echo ("<html>\n");
319 echo ("<head>\n");
320 echo ("<style>\n");
321 echo ("div {\n");
322 echo (" padding: 0;\n");
323 echo (" margin: 0;\n");
324 echo ("}\n");
325 echo ("body {\n");
326 echo (" font-family: sans-serif;\n");
327 echo (" font-weight: normal;\n");
328 echo (" font-size: 10pt;\n");
329 echo (" background: white;\n");
330 echo (" color: black;\n");
331 echo ("}\n");
332 echo ("span.large {\n");
333 echo (" font-size: 12pt;\n");
334 echo ("}\n");
335 echo ("span.small {\n");
336 echo (" font-size: 6pt;\n");
337 echo ("}\n");
338 echo ("td {\n");
339 echo (" vertical-align: top;\n");
340 echo (" width: 50%;\n");
341 echo (" font-size: 10pt;\n");
342 echo (" padding-bottom: 8pt;\n");
343 echo ("}\n");
344 echo ("td.bordered {\n");
345 echo (" border-top:1pt solid black;\n");
346 echo ("}\n");
347 echo ("div.paddingdiv {\n");
348 echo (" width: 524pt;\n");
349 echo (" height: 668pt;\n");
350 echo (" page-break-after: always;\n");
351 echo ("}\n");
352 echo ("div.scriptdiv {\n");
353 echo (" padding-top: 12pt;\n");
354 echo (" padding-bottom: 22pt;\n");
355 echo (" padding-left: 35pt;\n");
356 echo (" border-bottom:1pt solid black;\n");
357 echo ("}\n");
358 echo ("div.signdiv {\n");
359 echo (" margin-top: 40pt;\n");
360 echo (" font-size: 12pt;\n");
361 echo ("}\n");
362 echo ("</style>\n");
364 echo ("<title>" . xl('Prescription') . "</title>\n");
365 echo ("</head>\n");
366 echo ("<body>\n");
369 function multiprintfax_footer( & $pdf ) {
370 return $this->multiprint_footer( $pdf );
373 function multiprint_footer(& $pdf) {
374 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
375 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
376 if (file_exists($sigfile)) {
377 $pdf->ezText( xl('Signature') . ": ",12);
378 // $pdf->ezImage($sigfile, "", "", "none", "left");
379 $pdf->ezImage($sigfile, "", "", "none", "center");
380 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
381 if ( $this->is_print_to_fax ) {
382 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
385 $addenumFile = $this->pconfig['addendum_file'];
386 if ( file_exists( $addenumFile ) ) {
387 $pdf->ezText('');
388 $f = fopen($addenumFile, "r");
389 while ( $line = fgets($f, 1000) ) {
390 $pdf->ezText(rtrim($line));
394 return;
397 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
400 function multiprintcss_footer() {
401 echo ("<div class='signdiv'>\n");
402 echo (xl('Signature') . ":________________________________<br>");
403 echo (xl('Date') . ": " . date('Y-m-d'));
404 echo ("</div>\n");
405 echo ("</div>\n");
408 function multiprintcss_postfooter() {
409 echo("<script language='JavaScript'>\n");
410 echo("window.print();\n");
411 echo("</script>\n");
412 echo("</body>\n");
413 echo("</html>\n");
416 function get_prescription_body_text($p) {
417 $body = '<b>' . xl('Rx') . ': ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
418 if ($p->get_form()) $body .= ' [' . $p->form_array[$p->get_form()] . "]";
419 $body .= "</b> <i>" .
420 $p->substitute_array[$p->get_substitute()] . "</i>\n" .
421 '<b>' . xl('Disp #') . ':</b> <u>' . $p->get_quantity() . "</u>\n" .
422 '<b>' . xl('Sig') . ':</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] . ' ' .
423 $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
424 if ($p->get_refills() > 0) {
425 $body .= "\n<b>" . xl('Refills') . ":</b> <u>" . $p->get_refills();
426 if ($p->get_per_refill()) {
427 $body .= " " . xl('of quantity') . " " . $p->get_per_refill();
429 $body .= "</u>\n";
431 else {
432 $body .= "\n<b>" . xl('Refills') . ":</b> <u>0 (" . xl('Zero') . ")</u>\n";
434 $note = $p->get_note();
435 if ($note != '') {
436 $body .= "\n$note\n";
438 return $body;
441 function multiprintfax_body(& $pdf, $p){
442 return $this->multiprint_body( $pdf, $p );
445 function multiprint_body(& $pdf, $p){
446 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
447 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
448 $d = $this->get_prescription_body_text($p);
449 if ( $pdf->ezText($d,10,array(),1) ) {
450 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
451 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
452 $this->multiprint_footer($pdf);
453 $pdf->ezNewPage();
454 $this->multiprint_header($pdf, $p);
455 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
456 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
458 $my_y = $pdf->y;
459 $pdf->ezText($d,10);
460 if($this->pconfig['shading']) {
461 $pdf->setColor(.9,.9,.9);
462 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
463 $pdf->setColor(0,0,0);
465 $pdf->ezSetY($my_y);
466 $pdf->ezText($d,10);
467 $pdf->ez['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
468 $pdf->ez['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
469 $pdf->ezText('');
470 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
471 $pdf->ezText('');
474 function multiprintcss_body($p){
475 $d = $this->get_prescription_body_text($p);
476 $patterns = array ('/\n/','/ /');
477 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
478 $d = preg_replace($patterns, $replace, $d);
479 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
482 function multiprintfax_action($id = "") {
483 $this->is_print_to_fax=true;
484 return $this->multiprint_action( $id );
487 function multiprint_action($id = "") {
488 $_POST['process'] = "true";
489 if(empty($id)) {
490 $this->function_argument_error();
492 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
493 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
494 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
495 ,$GLOBALS['oer_config']['prescriptions']['bottom']
496 ,$GLOBALS['oer_config']['prescriptions']['left']
497 ,$GLOBALS['oer_config']['prescriptions']['right']
499 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
501 // $print_header = true;
502 $on_this_page = 0;
504 //print prescriptions body
505 $this->_state = false; // Added by Rod - see Controller.class.php
506 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
507 foreach ($ids as $id) {
508 $p = new Prescription($id);
509 // if ($print_header == true) {
510 if ($on_this_page == 0) {
511 $this->multiprint_header($pdf, $p);
513 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
514 $this->multiprint_footer($pdf);
515 $pdf->ezNewPage();
516 $this->multiprint_header($pdf, $p);
517 // $print_header = false;
518 $on_this_page = 1;
520 $this->multiprint_body($pdf, $p);
523 $this->multiprint_footer($pdf);
525 $pdf->ezStream();
526 return;
529 function multiprintcss_action($id = "") {
530 $_POST['process'] = "true";
531 if(empty($id)) {
532 $this->function_argument_error();
535 $this->multiprintcss_preheader();
537 $this->_state = false; // Added by Rod - see Controller.class.php
538 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
540 $on_this_page = 0;
541 foreach ($ids as $id) {
542 $p = new Prescription($id);
543 if ($on_this_page == 0) {
544 $this->multiprintcss_header($p);
546 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
547 $this->multiprintcss_footer();
548 $this->multiprintcss_header($p);
549 $on_this_page = 1;
551 $this->multiprintcss_body($p);
553 $this->multiprintcss_footer();
554 $this->multiprintcss_postfooter();
555 return;
558 function send_action_process($id) {
559 $dummy = ""; // Added by Rod to avoid run-time warnings
560 if ($_POST['process'] != "true")
561 return;
562 if(empty($id)) {
563 $this->function_argument_error();
565 $p = new Prescription($id);
566 switch ($_POST['submit']) {
568 case (xl("Print")." (".xl("PDF").")"):
569 // The following statement added by Rod.
570 // Looking at Controller.class.php, it appears that _state is set to false
571 // to indicate that no further HTML is to be generated.
572 $this->_state = false; // Added by Rod - see Controller.class.php
573 return $this->_print_prescription($p, $dummy);
574 break;
575 case (xl("Print")." (".xl("HTML").")"):
576 $this->_state = false;
577 return $this->_print_prescription_css($p, $dummy);
578 break;
579 case xl("Print To Fax"):
580 $this->_state = false;
581 $this->is_print_to_fax = true;
582 return $this->_print_prescription($p, $dummy);
583 break;
584 case xl("Email"):
585 return $this->_email_prescription($p,$_POST['email_to']);
586 break;
587 case xl("Fax"):
588 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
589 //$this->assign("process_result","No fax server is currently setup.");
590 return $this->_fax_prescription($p,$_POST['fax_to']);
591 break;
592 case xl("Auto Send"):
593 $pharmacy_id = $_POST['pharmacy_id'];
594 //echo "auto sending to : " . $_POST['pharmacy_id'];
595 $phar = new Pharmacy($_POST['pharmacy_id']);
596 //print_r($phar);
597 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
598 return $this->_print_prescription($p, $dummy);
600 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
601 $email = $phar->get_email();
602 if (!empty($email)) {
603 return $this->_email_prescription($p,$phar->get_email());
605 //else print it
607 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
608 $faxNum= $phar->get_fax();
609 if(!empty($faxNum)) {
610 Return $this->_fax_prescription ($p,$faxNum);
612 // return $this->assign("process_result","No fax server is currently setup.");
613 // else default is printing,
615 else {
616 //the pharmacy has no default or default is print
617 return $this->_print_prescription($p, $dummy);
619 break;
622 return;
626 function _print_prescription($p, & $toFile) {
627 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
628 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
629 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
630 ,$GLOBALS['oer_config']['prescriptions']['bottom']
631 ,$GLOBALS['oer_config']['prescriptions']['left']
632 ,$GLOBALS['oer_config']['prescriptions']['right']
635 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
637 // Signature images are to be used only when faxing.
638 if(!empty($toFile)) $this->is_faxing = true;
640 $this->multiprint_header($pdf, $p);
641 $this->multiprint_body($pdf, $p);
642 $this->multiprint_footer($pdf);
644 if(!empty($toFile)) {
645 $toFile = $pdf->ezOutput();
647 else {
648 $pdf->ezStream();
649 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
651 return;
654 function _print_prescription_css($p, & $toFile) {
656 $this->multiprintcss_preheader();
657 $this->multiprintcss_header($p);
658 $this->multiprintcss_body($p);
659 $this->multiprintcss_footer();
660 $this->multiprintcss_postfooter();
664 function _print_prescription_old($p, & $toFile) {
665 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
666 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
667 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
668 ,$GLOBALS['oer_config']['prescriptions']['bottom']
669 ,$GLOBALS['oer_config']['prescriptions']['left']
670 ,$GLOBALS['oer_config']['prescriptions']['right']
672 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
673 if(!empty($this->pconfig['logo'])) {
674 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
676 $pdf->ezText($p->get_prescription_display(),10);
677 if($this->pconfig['use_signature']) {
678 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
680 else{
681 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
683 if(!empty($toFile))
685 $toFile = $pdf->ezOutput();
687 else
689 $pdf->ezStream();
690 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
692 return;
695 function _email_prescription($p,$email) {
696 if (empty($email)) {
697 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
698 return;
700 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
701 $mail = new PHPMailer();
702 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
703 //this is a temporary config item until the rest of the per practice billing settings make their way in
704 $mail->From = $GLOBALS['practice_return_email_path'];
705 $mail->FromName = $p->provider->get_name_display();
706 $mail->isMail();
707 $mail->Host = "localhost";
708 $mail->Mailer = "mail";
709 $text_body = $p->get_prescription_display();
710 $mail->Body = $text_body;
711 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
712 $mail->AddAddress($email);
713 if($mail->Send()) {
714 $this->assign("process_result","Email was successfully sent to: " . $email);
715 return;
717 else {
718 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
719 return;
723 function do_lookup() {
724 if ($_POST['process'] != "true") {
725 // don't do a lookup
726 $this->assign("drug", $_GET['drug']);
727 return;
730 // process the lookup
731 $this->assign("drug", $_POST['drug']);
732 $list = array();
733 if (!empty($_POST['drug'])) {
734 $list = @RxList::get_list($_POST['drug']);
737 if (is_array($list)) {
738 $list = array_flip($list);
739 $this->assign("drug_options",$list);
740 $this->assign("drug_values",array_keys($list));
742 else {
743 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
745 //print_r($_POST);
746 //$this->assign("PROCESS","");
748 $_POST['process'] = "";
751 function _fax_prescription($p,$faxNum)
753 $err = "Sent fax";
754 //strip - ,(, ), and ws
755 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
756 //validate the number
758 if(!empty($faxNum) && is_numeric($faxNum))
760 //get the sendfax command and execute it
761 $cmd = $this->pconfig['sendfax'];
762 // prepend any prefix to the fax number
763 $pref=$this->pconfig['prefix'];
764 $faxNum=$pref.$faxNum;
765 if(empty($cmd))
767 $err .= " Send fax not set in includes/config.php";
768 break;
770 else
772 //generate file to fax
773 $faxFile = "Failed";
774 $this->_print_prescription($p, $faxFile);
775 if(empty($faxFile))
777 $err .= " _print_prescription returned empty file";
778 break;
780 $fileName = dirname(__FILE__)."/../documents/".$p->get_id()
781 .$p->get_patient_id()."_fax_.pdf";
782 //print "filename is $fileName";
783 touch($fileName); // php bug
784 $handle = fopen($fileName,"w");
785 if(!$handle)
787 $err .= " Failed to open file $fileName to write fax to";
788 break;
790 if(fwrite($handle, $faxFile) === false)
792 $err .= " Failed to write data to $fileName";
793 break;
795 fclose($handle);
796 $args = " -n -d $faxNum $fileName";
797 //print "command is $cmd $args<br>";
798 exec($cmd . $args);
802 else
804 $err = "bad fax number passed to function";
806 if($err)
808 $this->assign("process_result",$err);