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