internationalization
[openemr.git] / controllers / C_Prescription.class.php
blob1ae4bea85d70798bb9c3680702b3f1239ad821ac
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'];
28 if ($GLOBALS['inhouse_pharmacy']) {
29 // Make an array of drug IDs and selectors for the template.
30 $drug_array_values = array(0);
31 $drug_array_output = array("-- or select from inventory --");
32 $drug_attributes = '';
34 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
36 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
37 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
38 "t.period, t.quantity, t.refills " .
39 "FROM drug_templates AS t, drugs AS d WHERE " .
40 "d.drug_id = t.drug_id ORDER BY t.selector");
42 while ($row = sqlFetchArray($res)) {
43 $tmp_output = $row['selector'];
44 if ($row['ndc_number']) {
45 $tmp_output .= ' [' . $row['ndc_number'] . ']';
47 $drug_array_values[] = $row['drug_id'];
48 $drug_array_output[] = $tmp_output;
49 if ($drug_attributes) $drug_attributes .= ',';
50 $drug_attributes .= "['" .
51 $row['name'] . "'," . // 0
52 $row['form'] . ",'" . // 1
53 $row['dosage'] . "'," . // 2
54 $row['size'] . "," . // 3
55 $row['unit'] . "," . // 4
56 $row['route'] . "," . // 5
57 $row['period'] . "," . // 6
58 $row['substitute'] . "," . // 7
59 $row['quantity'] . "," . // 8
60 $row['refills'] . "," . // 9
61 $row['quantity'] . "]"; // 10 quantity per_refill
63 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
64 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
65 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
69 function default_action() {
70 $this->assign("prescription",$this->prescriptions[0]);
71 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
74 function edit_action($id = "",$patient_id="",$p_obj = null) {
76 if ($p_obj != null && get_class($p_obj) == "prescription") {
77 $this->prescriptions[0] = $p_obj;
79 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
80 $this->prescriptions[0] = new Prescription($id);
83 if (!empty($patient_id)) {
84 $this->prescriptions[0]->set_patient_id($patient_id);
87 // If quantity to dispense is not already set from a POST, set its
88 // default value.
89 if (! $this->get_template_vars('DISP_QUANTITY')) {
90 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
93 $this->default_action();
96 function list_action($id,$sort = "") {
97 if (empty($id)) {
98 $this->function_argument_error();
99 exit;
101 if (!empty($sort)) {
102 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
104 else {
105 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
108 // flag to indicate the CAMOS form is regsitered and active
109 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
111 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
114 function block_action($id,$sort = "") {
115 if (empty($id)) {
116 $this->function_argument_error();
117 exit;
119 if (!empty($sort)) {
120 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
122 else {
123 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
125 //print_r(Prescription::prescriptions_factory($id));
126 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
129 function lookup_action() {
130 $this->do_lookup();
131 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
134 function edit_action_process() {
135 if ($_POST['process'] != "true")
136 return;
137 //print_r($_POST);
139 // Stupid Smarty code treats empty values as not specified values.
140 // Since active is a checkbox, represent the unchecked state as -1.
141 if (empty($_POST['active'])) $_POST['active'] = '-1';
143 $this->prescriptions[0] = new Prescription($_POST['id']);
144 parent::populate_object($this->prescriptions[0]);
145 //echo $this->prescriptions[0]->toString(true);
146 $this->prescriptions[0]->persist();
147 $_POST['process'] = "";
149 // If the "Prescribe and Dispense" button was clicked, then
150 // redisplay as in edit_action() but also replicate the fee and
151 // include a piece of javascript to call dispense().
153 if ($_POST['disp_button']) {
154 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
155 $this->assign("DISP_FEE", $_POST['disp_fee']);
156 $this->assign("ENDING_JAVASCRIPT", "dispense();");
157 $this->_state = false;
158 return $this->edit_action($this->prescriptions[0]->id);
161 if ($this->prescriptions[0]->get_active() > 0) {
162 return $this->send_action($this->prescriptions[0]->id);
164 $this->list_action($this->prescriptions[0]->get_patient_id());
165 exit;
168 function send_action($id) {
169 $_POST['process'] = "true";
170 if(empty($id)) {
171 $this->function_argument_error();
174 $rx = new Prescription($id);
175 // Populate pharmacy info if the patient has a default pharmacy.
176 // Probably the Prescription object should handle this instead, but
177 // doing it there will require more careful research and testing.
178 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
179 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
180 if ($prow['pharmacy_id']) {
181 $rx->pharmacy->set_id($prow['pharmacy_id']);
182 $rx->pharmacy->populate();
184 $this->assign("prescription", $rx);
186 $this->_state = false;
187 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
188 $this->template_mod . "_send.html");
191 function multiprintfax_header(& $pdf, $p) {
192 return $this->multiprint_header( $pdf, $p );
195 function multiprint_header(& $pdf, $p) {
196 $this->providerid = $p->provider->id;
197 //print header
198 $pdf->ezImage($GLOBALS['fileroot'] . '/interface/pic/Rx.png','','50','','center','');
199 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
200 $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 ='" .
201 mysql_real_escape_string($p->provider->id) . "'");
202 $pdf->ezText($res['addr'],12);
203 $my_y = $pdf->y;
204 $pdf->ezNewPage();
205 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
206 // A client had a bad experience with a patient misusing a DEA number, so
207 // now the doctors write those in on printed prescriptions and only when
208 // necessary. If you need to change this back, then please make it a
209 // configurable option. Faxed prescriptions were not changed. -- Rod
210 // Now it is configureable. Change value in
211 // <openemr root>/includes/config.php - Tekknogenius
212 if ($this->is_faxing || $GLOBALS['oer_config']['prescriptions']['show_DEA'])
213 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
214 else
215 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
216 $pdf->ezColumnsStop();
217 if ($my_y < $pdf->y){
218 $pdf->ezSetY($my_y);
220 $pdf->ezText('',10);
221 $pdf->setLineStyle(1);
222 $pdf->ezColumnsStart(array('num'=>2));
223 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
224 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
225 $pdf->ezText($p->patient->get_name_display(),10);
226 $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));
227 $pdf->ezText($res['addr']);
228 $my_y = $pdf->y;
229 $pdf->ezNewPage();
230 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
231 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
232 $pdf->ezText($p->patient->date_of_birth,10);
233 $pdf->ezText('');
234 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
235 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
236 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
237 $pdf->ezColumnsStop();
238 if ($my_y < $pdf->y){
239 $pdf->ezSetY($my_y);
241 $pdf->ezText('');
242 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
243 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
244 $pdf->ezText('',10);
247 function multiprintcss_header($p) {
248 echo("<div class='paddingdiv'>\n");
249 $this->providerid = $p->provider->id;
250 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
251 echo ("<tr>\n");
252 echo ("<td></td>\n");
253 echo ("<td>\n");
254 echo ("<img WIDTH='68pt' src='./interface/pic/Rx.png' />");
255 echo ("</td>\n");
256 echo ("</tr>\n");
257 echo ("<tr>\n");
258 echo ("<td>\n");
259 $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) . "'");
260 $patterns = array ('/\n/','/Tel:/','/Fax:/');
261 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
262 $res = preg_replace($patterns, $replace, $res);
263 echo ('<span class="large">' . $res['addr'] . '</span>');
264 echo ("</td>\n");
265 echo ("<td>\n");
266 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
267 if ($GLOBALS['oer_config']['prescriptions']['show_DEA']) echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span>');
268 else echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________');
269 echo ("</td>\n");
270 echo ("</tr>\n");
271 echo ("<tr>\n");
272 echo ("<td rowspan='2' class='bordered'>\n");
273 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
274 echo ($p->patient->get_name_display() . '<br>');
275 $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));
276 $patterns = array ('/\n/');
277 $replace = array ('<br>');
278 $res = preg_replace($patterns, $replace, $res);
279 echo ($res['addr']);
280 echo ("</td>\n");
281 echo ("<td class='bordered'>\n");
282 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
283 echo ($p->patient->date_of_birth );
284 echo ("</td>\n");
285 echo ("</tr>\n");
286 echo ("<tr>\n");
287 echo ("<td class='bordered'>\n");
288 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
289 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
290 echo ("</td>\n");
291 echo ("</tr>\n");
292 echo ("<tr>\n");
293 echo ("<td colspan='2' class='bordered'>\n");
294 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
295 echo ("</td>\n");
296 echo ("</tr>\n");
297 echo ("</table>\n");
300 function multiprintcss_preheader() {
301 // this sets styling and other header information of the multiprint css sheet
302 echo ("<html>\n");
303 echo ("<head>\n");
304 echo ("<style>\n");
305 echo ("div {\n");
306 echo (" padding: 0;\n");
307 echo (" margin: 0;\n");
308 echo ("}\n");
309 echo ("body {\n");
310 echo (" font-family: sans-serif;\n");
311 echo (" font-weight: normal;\n");
312 echo (" font-size: 10pt;\n");
313 echo (" background: white;\n");
314 echo (" color: black;\n");
315 echo ("}\n");
316 echo ("span.large {\n");
317 echo (" font-size: 12pt;\n");
318 echo ("}\n");
319 echo ("span.small {\n");
320 echo (" font-size: 6pt;\n");
321 echo ("}\n");
322 echo ("td {\n");
323 echo (" vertical-align: top;\n");
324 echo (" width: 50%;\n");
325 echo (" font-size: 10pt;\n");
326 echo (" padding-bottom: 8pt;\n");
327 echo ("}\n");
328 echo ("td.bordered {\n");
329 echo (" border-top:1pt solid black;\n");
330 echo ("}\n");
331 echo ("div.paddingdiv {\n");
332 echo (" width: 524pt;\n");
333 echo (" height: 668pt;\n");
334 echo (" page-break-after: always;\n");
335 echo ("}\n");
336 echo ("div.scriptdiv {\n");
337 echo (" padding-top: 12pt;\n");
338 echo (" padding-bottom: 22pt;\n");
339 echo (" padding-left: 35pt;\n");
340 echo (" border-bottom:1pt solid black;\n");
341 echo ("}\n");
342 echo ("div.signdiv {\n");
343 echo (" margin-top: 40pt;\n");
344 echo (" font-size: 12pt;\n");
345 echo ("}\n");
346 echo ("</style>\n");
348 echo ("<title>" . xl('Prescription') . "</title>\n");
349 echo ("</head>\n");
350 echo ("<body>\n");
353 function multiprintfax_footer( & $pdf ) {
354 return $this->multiprint_footer( $pdf );
357 function multiprint_footer(& $pdf) {
358 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
359 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
360 if (file_exists($sigfile)) {
361 $pdf->ezText( xl('Signature') . ": ",12);
362 // $pdf->ezImage($sigfile, "", "", "none", "left");
363 $pdf->ezImage($sigfile, "", "", "none", "center");
364 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
365 if ( $this->is_print_to_fax ) {
366 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facimile.'));
369 $addenumFile = $this->pconfig['addendum_file'];
370 if ( file_exists( $addenumFile ) ) {
371 $pdf->ezText('');
372 $f = fopen($addenumFile, "r");
373 while ( $line = fgets($f, 1000) ) {
374 $pdf->ezText(rtrim($line));
378 return;
381 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
384 function multiprintcss_footer() {
385 echo ("<div class='signdiv'>\n");
386 echo (xl('Signature') . ":________________________________<br>");
387 echo (xl('Date') . ": " . date('Y-m-d'));
388 echo ("</div>\n");
389 echo ("</div>\n");
392 function multiprintcss_postfooter() {
393 echo("<script language='JavaScript'>\n");
394 echo("window.print();\n");
395 echo("</script>\n");
396 echo("</body>\n");
397 echo("</html>\n");
400 function get_prescription_body_text($p) {
401 $body = '<b>' . xl('Rx') . ': ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
402 if ($p->get_form()) $body .= ' [' . $p->form_array[$p->get_form()] . "]";
403 $body .= "</b> <i>" .
404 $p->substitute_array[$p->get_substitute()] . "</i>\n" .
405 '<b>' . xl('Disp #') . ':</b> <u>' . $p->get_quantity() . "</u>\n" .
406 '<b>' . xl('Sig') . ':</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] . ' ' .
407 $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
408 if ($p->get_refills() > 0) {
409 $body .= "\n<b>" . xl('Refills') . ":</b> <u>" . $p->get_refills();
410 if ($p->get_per_refill()) {
411 $body .= " " . xl('of quantity') . " " . $p->get_per_refill();
413 $body .= "</u>\n";
415 else {
416 $body .= "\n<b>" . xl('Refills') . ":</b> <u>0 (" . xl('Zero') . ")</u>\n";
418 $note = $p->get_note();
419 if ($note != '') {
420 $body .= "\n$note\n";
422 return $body;
425 function multiprintfax_body(& $pdf, $p){
426 return $this->multiprint_body( $pdf, $p );
429 function multiprint_body(& $pdf, $p){
430 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
431 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
432 $d = $this->get_prescription_body_text($p);
433 if ( $pdf->ezText($d,10,array(),1) ) {
434 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
435 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
436 $this->multiprint_footer($pdf);
437 $pdf->ezNewPage();
438 $this->multiprint_header($pdf, $p);
439 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
440 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
442 $my_y = $pdf->y;
443 $pdf->ezText($d,10);
444 if($this->pconfig['shading']) {
445 $pdf->setColor(.9,.9,.9);
446 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
447 $pdf->setColor(0,0,0);
449 $pdf->ezSetY($my_y);
450 $pdf->ezText($d,10);
451 $pdf->ez['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
452 $pdf->ez['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
453 $pdf->ezText('');
454 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
455 $pdf->ezText('');
458 function multiprintcss_body($p){
459 $d = $this->get_prescription_body_text($p);
460 $patterns = array ('/\n/','/ /');
461 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
462 $d = preg_replace($patterns, $replace, $d);
463 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
466 function multiprintfax_action($id = "") {
467 $this->is_print_to_fax=true;
468 return $this->multiprint_action( $id );
471 function multiprint_action($id = "") {
472 $_POST['process'] = "true";
473 if(empty($id)) {
474 $this->function_argument_error();
476 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
477 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
478 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
479 ,$GLOBALS['oer_config']['prescriptions']['bottom']
480 ,$GLOBALS['oer_config']['prescriptions']['left']
481 ,$GLOBALS['oer_config']['prescriptions']['right']
483 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
485 // $print_header = true;
486 $on_this_page = 0;
488 //print prescriptions body
489 $this->_state = false; // Added by Rod - see Controller.class.php
490 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
491 foreach ($ids as $id) {
492 $p = new Prescription($id);
493 // if ($print_header == true) {
494 if ($on_this_page == 0) {
495 $this->multiprint_header($pdf, $p);
497 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
498 $this->multiprint_footer($pdf);
499 $pdf->ezNewPage();
500 $this->multiprint_header($pdf, $p);
501 // $print_header = false;
502 $on_this_page = 1;
504 $this->multiprint_body($pdf, $p);
507 $this->multiprint_footer($pdf);
509 $pdf->ezStream();
510 return;
513 function multiprintcss_action($id = "") {
514 $_POST['process'] = "true";
515 if(empty($id)) {
516 $this->function_argument_error();
519 $this->multiprintcss_preheader();
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);
524 $on_this_page = 0;
525 foreach ($ids as $id) {
526 $p = new Prescription($id);
527 if ($on_this_page == 0) {
528 $this->multiprintcss_header($p);
530 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
531 $this->multiprintcss_footer();
532 $this->multiprintcss_header($p);
533 $on_this_page = 1;
535 $this->multiprintcss_body($p);
537 $this->multiprintcss_footer();
538 $this->multiprintcss_postfooter();
539 return;
542 function send_action_process($id) {
543 $dummy = ""; // Added by Rod to avoid run-time warnings
544 if ($_POST['process'] != "true")
545 return;
546 if(empty($id)) {
547 $this->function_argument_error();
549 $p = new Prescription($id);
550 switch ($_POST['submit']) {
552 case (xl("Print")." (".xl("PDF").")"):
553 // The following statement added by Rod.
554 // Looking at Controller.class.php, it appears that _state is set to false
555 // to indicate that no further HTML is to be generated.
556 $this->_state = false; // Added by Rod - see Controller.class.php
557 return $this->_print_prescription($p, $dummy);
558 break;
559 case (xl("Print")." (".xl("HTML").")"):
560 $this->_state = false;
561 return $this->_print_prescription_css($p, $dummy);
562 break;
563 case xl("Print To Fax"):
564 $this->_state = false;
565 $this->is_print_to_fax = true;
566 return $this->_print_prescription($p, $dummy);
567 break;
568 case xl("Email"):
569 return $this->_email_prescription($p,$_POST['email_to']);
570 break;
571 case xl("Fax"):
572 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
573 //$this->assign("process_result","No fax server is currently setup.");
574 return $this->_fax_prescription($p,$_POST['fax_to']);
575 break;
576 case xl("Auto Send"):
577 $pharmacy_id = $_POST['pharmacy_id'];
578 //echo "auto sending to : " . $_POST['pharmacy_id'];
579 $phar = new Pharmacy($_POST['pharmacy_id']);
580 //print_r($phar);
581 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
582 return $this->_print_prescription($p, $dummy);
584 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
585 $email = $phar->get_email();
586 if (!empty($email)) {
587 return $this->_email_prescription($p,$phar->get_email());
589 //else print it
591 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
592 $faxNum= $phar->get_fax();
593 if(!empty($faxNum)) {
594 Return $this->_fax_prescription ($p,$faxNum);
596 // return $this->assign("process_result","No fax server is currently setup.");
597 // else default is printing,
599 else {
600 //the pharmacy has no default or default is print
601 return $this->_print_prescription($p, $dummy);
603 break;
606 return;
610 function _print_prescription($p, & $toFile) {
611 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
612 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
613 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
614 ,$GLOBALS['oer_config']['prescriptions']['bottom']
615 ,$GLOBALS['oer_config']['prescriptions']['left']
616 ,$GLOBALS['oer_config']['prescriptions']['right']
619 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
621 // Signature images are to be used only when faxing.
622 if(!empty($toFile)) $this->is_faxing = true;
624 $this->multiprint_header($pdf, $p);
625 $this->multiprint_body($pdf, $p);
626 $this->multiprint_footer($pdf);
628 if(!empty($toFile)) {
629 $toFile = $pdf->ezOutput();
631 else {
632 $pdf->ezStream();
633 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
635 return;
638 function _print_prescription_css($p, & $toFile) {
640 $this->multiprintcss_preheader();
641 $this->multiprintcss_header($p);
642 $this->multiprintcss_body($p);
643 $this->multiprintcss_footer();
644 $this->multiprintcss_postfooter();
648 function _print_prescription_old($p, & $toFile) {
649 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
650 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
651 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
652 ,$GLOBALS['oer_config']['prescriptions']['bottom']
653 ,$GLOBALS['oer_config']['prescriptions']['left']
654 ,$GLOBALS['oer_config']['prescriptions']['right']
656 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
657 if(!empty($this->pconfig['logo'])) {
658 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
660 $pdf->ezText($p->get_prescription_display(),10);
661 if($this->pconfig['use_signature']) {
662 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
664 else{
665 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
667 if(!empty($toFile))
669 $toFile = $pdf->ezOutput();
671 else
673 $pdf->ezStream();
674 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
676 return;
679 function _email_prescription($p,$email) {
680 if (empty($email)) {
681 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
682 return;
684 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
685 $mail = new PHPMailer();
686 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
687 //this is a temporary config item until the rest of the per practice billing settings make their way in
688 $mail->From = $GLOBALS['practice_return_email_path'];
689 $mail->FromName = $p->provider->get_name_display();
690 $mail->isMail();
691 $mail->Host = "localhost";
692 $mail->Mailer = "mail";
693 $text_body = $p->get_prescription_display();
694 $mail->Body = $text_body;
695 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
696 $mail->AddAddress($email);
697 if($mail->Send()) {
698 $this->assign("process_result","Email was successfully sent to: " . $email);
699 return;
701 else {
702 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
703 return;
707 function do_lookup() {
708 if ($_POST['process'] != "true") {
709 // don't do a lookup
710 $this->assign("drug", $_GET['drug']);
711 return;
714 // process the lookup
715 $this->assign("drug", $_POST['drug']);
716 $list = array();
717 if (!empty($_POST['drug'])) {
718 $list = @RxList::get_list($_POST['drug']);
721 if (is_array($list)) {
722 $list = array_flip($list);
723 $this->assign("drug_options",$list);
724 $this->assign("drug_values",array_keys($list));
726 else {
727 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
729 //print_r($_POST);
730 //$this->assign("PROCESS","");
732 $_POST['process'] = "";
735 function _fax_prescription($p,$faxNum)
737 $err = "Sent fax";
738 //strip - ,(, ), and ws
739 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
740 //validate the number
742 if(!empty($faxNum) && is_numeric($faxNum))
744 //get the sendfax command and execute it
745 $cmd = $this->pconfig['sendfax'];
746 // prepend any prefix to the fax number
747 $pref=$this->pconfig['prefix'];
748 $faxNum=$pref.$faxNum;
749 if(empty($cmd))
751 $err .= " Send fax not set in includes/config.php";
752 break;
754 else
756 //generate file to fax
757 $faxFile = "Failed";
758 $this->_print_prescription($p, $faxFile);
759 if(empty($faxFile))
761 $err .= " _print_prescription returned empty file";
762 break;
764 $fileName = dirname(__FILE__)."/../documents/".$p->get_id()
765 .$p->get_patient_id()."_fax_.pdf";
766 //print "filename is $fileName";
767 touch($fileName); // php bug
768 $handle = fopen($fileName,"w");
769 if(!$handle)
771 $err .= " Failed to open file $fileName to write fax to";
772 break;
774 if(fwrite($handle, $faxFile) === false)
776 $err .= " Failed to write data to $fileName";
777 break;
779 fclose($handle);
780 $args = " -n -d $faxNum $fileName";
781 //print "command is $cmd $args<br>";
782 exec($cmd . $args);
786 else
788 $err = "bad fax number passed to function";
790 if($err)
792 $this->assign("process_result",$err);