facility form fix (#419)
[openemr.git] / controllers / C_Prescription.class.php
blob69f7f5cf442d6135011b1d22b78659aaf6dfc4b3
1 <?php
2 /**
3 * This script print Prescriptions.
5 * Copyright (C) 2015 Roberto Vasquez <robertogagliotta@gmail.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Roberto Vasquez <robertogagliotta@gmail.com>
20 * @link http://www.open-emr.org
24 require_once($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
25 require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
26 require_once($GLOBALS['fileroot'] . "/library/classes/Provider.class.php");
27 require_once($GLOBALS['fileroot'] . "/library/classes/RXList.class.php");
28 require_once($GLOBALS['fileroot'] . "/library/registry.inc");
29 require_once($GLOBALS['fileroot'] . "/library/amc.php");
31 class C_Prescription extends Controller {
33 var $template_mod;
34 var $pconfig;
35 var $providerid = 0;
36 var $is_faxing = false;
37 var $is_print_to_fax = false;
39 function __construct($template_mod = "general") {
40 parent::__construct();
42 $this->template_mod = $template_mod;
43 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
44 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
45 $this->assign("STYLE", $GLOBALS['style']);
46 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
47 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
48 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
49 $this->assign("CSS_HEADER", $GLOBALS['css_header'] );
50 $this->assign("WEB_ROOT", $GLOBALS['webroot'] );
51 $this->RxList = new RxList();
53 if ($GLOBALS['inhouse_pharmacy']) {
54 // Make an array of drug IDs and selectors for the template.
55 $drug_array_values = array(0);
56 $drug_array_output = array("-- " . xl('or select from inventory') ." --");
57 $drug_attributes = '';
59 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
61 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
62 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
63 "t.period, t.quantity, t.refills, d.drug_code " .
64 "FROM drug_templates AS t, drugs AS d WHERE " .
65 "d.drug_id = t.drug_id ORDER BY t.selector");
67 while ($row = sqlFetchArray($res)) {
68 $tmp_output = $row['selector'];
69 if ($row['ndc_number']) {
70 $tmp_output .= ' [' . $row['ndc_number'] . ']';
72 $drug_array_values[] = $row['drug_id'];
73 $drug_array_output[] = $tmp_output;
74 if ($drug_attributes) $drug_attributes .= ',';
75 $drug_attributes .= "['" .
76 $row['name'] . "'," . // 0
77 $row['form'] . ",'" . // 1
78 $row['dosage'] . "','" . // 2
79 $row['size'] . "'," . // 3
80 $row['unit'] . "," . // 4
81 $row['route'] . "," . // 5
82 $row['period'] . "," . // 6
83 $row['substitute'] . "," . // 7
84 $row['quantity'] . "," . // 8
85 $row['refills'] . "," . // 9
86 $row['quantity'] . "," . // 10 quantity per_refill
87 $row['drug_code'] . "]"; // 11 rxnorm drug code
89 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
90 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
91 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
95 function default_action() {
96 $this->assign("prescription",$this->prescriptions[0]);
97 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
100 function edit_action($id = "",$patient_id="",$p_obj = null) {
102 if ($p_obj != null && get_class($p_obj) == "prescription") {
103 $this->prescriptions[0] = $p_obj;
105 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
106 $this->prescriptions[0] = new Prescription($id);
109 if (!empty($patient_id)) {
110 $this->prescriptions[0]->set_patient_id($patient_id);
113 // If quantity to dispense is not already set from a POST, set its
114 // default value.
115 if (! $this->get_template_vars('DISP_QUANTITY')) {
116 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
119 $this->default_action();
122 function list_action($id,$sort = "") {
123 if (empty($id)) {
124 $this->function_argument_error();
125 exit;
127 if (!empty($sort)) {
128 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
130 else {
131 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
134 // flag to indicate the CAMOS form is regsitered and active
135 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
137 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
140 function block_action($id,$sort = "") {
141 if (empty($id)) {
142 $this->function_argument_error();
143 exit;
145 if (!empty($sort)) {
146 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
148 else {
149 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
151 //print_r(Prescription::prescriptions_factory($id));
152 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
155 function fragment_action($id,$sort = "") {
156 if (empty($id)) {
157 $this->function_argument_error();
158 exit;
160 if (!empty($sort)) {
161 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
163 else {
164 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
166 //print_r(Prescription::prescriptions_factory($id));
167 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
170 function lookup_action() {
171 $this->do_lookup();
172 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
175 function edit_action_process() {
176 if ($_POST['process'] != "true")
177 return;
178 //print_r($_POST);
180 // Stupid Smarty code treats empty values as not specified values.
181 // Since active is a checkbox, represent the unchecked state as -1.
182 if (empty($_POST['active'])) $_POST['active'] = '-1';
184 $this->prescriptions[0] = new Prescription($_POST['id']);
185 parent::populate_object($this->prescriptions[0]);
186 //echo $this->prescriptions[0]->toString(true);
187 $this->prescriptions[0]->persist();
188 $_POST['process'] = "";
190 // If the "Prescribe and Dispense" button was clicked, then
191 // redisplay as in edit_action() but also replicate the fee and
192 // include a piece of javascript to call dispense().
194 if ($_POST['disp_button']) {
195 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
196 $this->assign("DISP_FEE", $_POST['disp_fee']);
197 $this->assign("ENDING_JAVASCRIPT", "dispense();");
198 $this->_state = false;
199 return $this->edit_action($this->prescriptions[0]->id);
202 // Set the AMC reporting flag (to record percentage of prescriptions that
203 // are set as e-prescriptions)
204 if (!(empty($_POST['escribe_flag']))) {
205 // add the e-prescribe flag
206 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
208 else {
209 // remove the e-prescribe flag
210 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
213 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
214 if (!(empty($_POST['checked_formulary_flag']))) {
215 // add the e-prescribe flag
216 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
218 else {
219 // remove the e-prescribe flag
220 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
223 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
224 if (!(empty($_POST['controlled_substance_flag']))) {
225 // add the e-prescribe flag
226 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
228 else {
229 // remove the e-prescribe flag
230 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
233 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
234 // if ($this->prescriptions[0]->get_active() > 0) {
235 // return $this->send_action($this->prescriptions[0]->id);
236 // }
237 $this->list_action($this->prescriptions[0]->get_patient_id());
238 exit;
241 function send_action($id) {
242 $_POST['process'] = "true";
243 if(empty($id)) {
244 $this->function_argument_error();
247 $rx = new Prescription($id);
248 // Populate pharmacy info if the patient has a default pharmacy.
249 // Probably the Prescription object should handle this instead, but
250 // doing it there will require more careful research and testing.
251 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
252 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
253 if ($prow['pharmacy_id']) {
254 $rx->pharmacy->set_id($prow['pharmacy_id']);
255 $rx->pharmacy->populate();
257 $this->assign("prescription", $rx);
259 $this->_state = false;
260 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
261 $this->template_mod . "_send.html");
264 function multiprintfax_header(& $pdf, $p) {
265 return $this->multiprint_header( $pdf, $p );
268 function multiprint_header(& $pdf, $p) {
269 $this->providerid = $p->provider->id;
270 //print header
271 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'],'','50','','center','');
272 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
273 $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 ='" .
274 add_escape_custom($p->provider->id) . "'");
275 $pdf->ezText($res['addr'],12);
276 $my_y = $pdf->y;
277 $pdf->ezNewPage();
278 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
279 // A client had a bad experience with a patient misusing a DEA number, so
280 // now the doctors write those in on printed prescriptions and only when
281 // necessary. If you need to change this back, then please make it a
282 // configurable option. Faxed prescriptions were not changed. -- Rod
283 // Now it is configureable. Change value in
284 // Administration->Globals->Rx
285 if ($GLOBALS['rx_enable_DEA']) {
286 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
287 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
289 else {
290 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
294 if ($GLOBALS['rx_enable_NPI']) {
295 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
296 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
298 else {
299 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
302 if ($GLOBALS['rx_enable_SLN']) {
303 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
304 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
306 else {
307 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
310 $pdf->ezColumnsStop();
311 if ($my_y < $pdf->y){
312 $pdf->ezSetY($my_y);
314 $pdf->ezText('',10);
315 $pdf->setLineStyle(1);
316 $pdf->ezColumnsStart(array('num'=>2));
317 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
318 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
319 $pdf->ezText($p->patient->get_name_display(),10);
320 $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 =". add_escape_custom($p->patient->id));
321 $pdf->ezText($res['addr']);
322 $my_y = $pdf->y;
323 $pdf->ezNewPage();
324 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
325 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
326 $pdf->ezText($p->patient->date_of_birth,10);
327 $pdf->ezText('');
328 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
329 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
330 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
331 $pdf->ezColumnsStop();
332 if ($my_y < $pdf->y){
333 $pdf->ezSetY($my_y);
335 $pdf->ezText('');
336 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
337 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
338 $pdf->ezText('',10);
341 function multiprintcss_header($p) {
342 echo("<div class='paddingdiv'>\n");
343 $this->providerid = $p->provider->id;
344 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
345 echo ("<tr>\n");
346 echo ("<td></td>\n");
347 echo ("<td>\n");
348 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
349 echo ("</td>\n");
350 echo ("</tr>\n");
351 echo ("<tr>\n");
352 echo ("<td>\n");
353 $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 ='" . add_escape_custom($p->provider->id) . "'");
354 if (!empty($res)) {
355 $patterns = array ('/\n/','/Tel:/','/Fax:/');
356 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
357 $res = preg_replace($patterns, $replace, $res);
359 echo ('<span class="large">' . $res['addr'] . '</span>');
360 echo ("</td>\n");
361 echo ("<td>\n");
362 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
364 if ($GLOBALS['rx_enable_DEA']) {
365 if ($GLOBALS['rx_show_DEA']) {
366 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
368 else {
369 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
372 if ($GLOBALS['rx_enable_NPI']) {
373 if ($GLOBALS['rx_show_NPI']) {
374 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
376 else {
377 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
380 if ($GLOBALS['rx_enable_SLN']) {
381 if ($GLOBALS['rx_show_SLN']) {
382 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
384 else {
385 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
388 echo ("</td>\n");
389 echo ("</tr>\n");
390 echo ("<tr>\n");
391 echo ("<td rowspan='2' class='bordered'>\n");
392 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
393 echo ($p->patient->get_name_display() . '<br>');
394 $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 =". add_escape_custom($p->patient->id));
395 if (!empty($res)) {
396 $patterns = array ('/\n/');
397 $replace = array ('<br>');
398 $res = preg_replace($patterns, $replace, $res);
400 echo ($res['addr']);
401 echo ("</td>\n");
402 echo ("<td class='bordered'>\n");
403 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
404 echo ($p->patient->date_of_birth );
405 echo ("</td>\n");
406 echo ("</tr>\n");
407 echo ("<tr>\n");
408 echo ("<td class='bordered'>\n");
409 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
410 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
411 echo ("</td>\n");
412 echo ("</tr>\n");
413 echo ("<tr>\n");
414 echo ("<td colspan='2' class='bordered'>\n");
415 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
416 echo ("</td>\n");
417 echo ("</tr>\n");
418 echo ("</table>\n");
421 function multiprintcss_preheader() {
422 // this sets styling and other header information of the multiprint css sheet
423 echo ("<html>\n");
424 echo ("<head>\n");
425 echo ("<style>\n");
426 echo ("div {\n");
427 echo (" padding: 0;\n");
428 echo (" margin: 0;\n");
429 echo ("}\n");
430 echo ("body {\n");
431 echo (" font-family: sans-serif;\n");
432 echo (" font-weight: normal;\n");
433 echo (" font-size: 10pt;\n");
434 echo (" background: white;\n");
435 echo (" color: black;\n");
436 echo ("}\n");
437 echo ("span.large {\n");
438 echo (" font-size: 12pt;\n");
439 echo ("}\n");
440 echo ("span.small {\n");
441 echo (" font-size: 6pt;\n");
442 echo ("}\n");
443 echo ("td {\n");
444 echo (" vertical-align: top;\n");
445 echo (" width: 50%;\n");
446 echo (" font-size: 10pt;\n");
447 echo (" padding-bottom: 8pt;\n");
448 echo ("}\n");
449 echo ("td.bordered {\n");
450 echo (" border-top:1pt solid black;\n");
451 echo ("}\n");
452 echo ("div.paddingdiv {\n");
453 echo (" width: 524pt;\n");
454 echo (" height: 668pt;\n");
455 echo ("}\n");
456 echo ("div.scriptdiv {\n");
457 echo (" padding-top: 12pt;\n");
458 echo (" padding-bottom: 22pt;\n");
459 echo (" padding-left: 35pt;\n");
460 echo (" border-bottom:1pt solid black;\n");
461 echo ("}\n");
462 echo ("div.signdiv {\n");
463 echo (" margin-top: 40pt;\n");
464 echo (" font-size: 12pt;\n");
465 echo ("}\n");
466 echo ("</style>\n");
468 echo ("<title>" . xl('Prescription') . "</title>\n");
469 echo ("</head>\n");
470 echo ("<body>\n");
473 function multiprintfax_footer( & $pdf ) {
474 return $this->multiprint_footer( $pdf );
477 function multiprint_footer(& $pdf) {
478 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
479 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
480 if (file_exists($sigfile)) {
481 $pdf->ezText( xl('Signature') . ": ",12);
482 // $pdf->ezImage($sigfile, "", "", "none", "left");
483 $pdf->ezImage($sigfile, "", "", "none", "center");
484 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
485 if ( $this->is_print_to_fax ) {
486 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
489 $addenumFile = $this->pconfig['addendum_file'];
490 if ( file_exists( $addenumFile ) ) {
491 $pdf->ezText('');
492 $f = fopen($addenumFile, "r");
493 while ( $line = fgets($f, 1000) ) {
494 $pdf->ezText(rtrim($line));
498 return;
501 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
504 function multiprintcss_footer() {
505 echo ("<div class='signdiv'>\n");
506 echo (xl('Signature') . ":________________________________<br>");
507 echo (xl('Date') . ": " . date('Y-m-d'));
508 echo ("</div>\n");
509 echo ("</div>\n");
512 function multiprintcss_postfooter() {
513 echo("<script language='JavaScript'>\n");
514 echo("opener.top.printLogPrint(window);\n");
515 echo("</script>\n");
516 echo("</body>\n");
517 echo("</html>\n");
520 function get_prescription_body_text($p) {
521 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
522 if ($p->get_form()) $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
523 $body .= "</b> <i>" .
524 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
525 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
526 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
527 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
528 if ($p->get_refills() > 0) {
529 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
530 if ($p->get_per_refill()) {
531 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
533 $body .= "</u>\n";
535 else {
536 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
538 $note = $p->get_note();
539 if ($note != '') {
540 $body .= "\n" . text($note) . "\n";
542 return $body;
545 function multiprintfax_body(& $pdf, $p){
546 return $this->multiprint_body( $pdf, $p );
549 function multiprint_body(& $pdf, $p){
550 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
551 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
552 $d = $this->get_prescription_body_text($p);
553 if ( $pdf->ezText($d,10,array(),1) ) {
554 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
555 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
556 $this->multiprint_footer($pdf);
557 $pdf->ezNewPage();
558 $this->multiprint_header($pdf, $p);
559 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
560 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
562 $my_y = $pdf->y;
563 $pdf->ezText($d,10);
564 if($this->pconfig['shading']) {
565 $pdf->setColor(.9,.9,.9);
566 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
567 $pdf->setColor(0,0,0);
569 $pdf->ezSetY($my_y);
570 $pdf->ezText($d,10);
571 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
572 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
573 $pdf->ezText('');
574 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
575 $pdf->ezText('');
578 function multiprintcss_body($p){
579 $d = $this->get_prescription_body_text($p);
580 $patterns = array ('/\n/','/ /');
581 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
582 $d = preg_replace($patterns, $replace, $d);
583 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
586 function multiprintfax_action($id = "") {
587 $this->is_print_to_fax=true;
588 return $this->multiprint_action( $id );
591 function multiprint_action($id = "") {
592 $_POST['process'] = "true";
593 if(empty($id)) {
594 $this->function_argument_error();
596 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
597 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
598 ,$GLOBALS['rx_bottom_margin']
599 ,$GLOBALS['rx_left_margin']
600 ,$GLOBALS['rx_right_margin']
602 $pdf->selectFont('Helvetica');
604 // $print_header = true;
605 $on_this_page = 0;
607 //print prescriptions body
608 $this->_state = false; // Added by Rod - see Controller.class.php
609 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
610 foreach ($ids as $id) {
611 $p = new Prescription($id);
612 // if ($print_header == true) {
613 if ($on_this_page == 0) {
614 $this->multiprint_header($pdf, $p);
616 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
617 $this->multiprint_footer($pdf);
618 $pdf->ezNewPage();
619 $this->multiprint_header($pdf, $p);
620 // $print_header = false;
621 $on_this_page = 1;
623 $this->multiprint_body($pdf, $p);
626 $this->multiprint_footer($pdf);
628 $pdf->ezStream();
629 return;
632 function multiprintcss_action($id = "") {
633 $_POST['process'] = "true";
634 if(empty($id)) {
635 $this->function_argument_error();
638 $this->multiprintcss_preheader();
640 $this->_state = false; // Added by Rod - see Controller.class.php
641 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
643 $on_this_page = 0;
644 foreach ($ids as $id) {
645 $p = new Prescription($id);
646 if ($on_this_page == 0) {
647 $this->multiprintcss_header($p);
649 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
650 $this->multiprintcss_footer();
651 $this->multiprintcss_header($p);
652 $on_this_page = 1;
654 $this->multiprintcss_body($p);
656 $this->multiprintcss_footer();
657 $this->multiprintcss_postfooter();
658 return;
661 function send_action_process($id) {
662 $dummy = ""; // Added by Rod to avoid run-time warnings
663 if ($_POST['process'] != "true")
664 return;
665 if(empty($id)) {
666 $this->function_argument_error();
668 $p = new Prescription($id);
669 switch ($_POST['submit']) {
671 case (xl("Print")." (".xl("PDF").")"):
672 // The following statement added by Rod.
673 // Looking at Controller.class.php, it appears that _state is set to false
674 // to indicate that no further HTML is to be generated.
675 $this->_state = false; // Added by Rod - see Controller.class.php
676 return $this->_print_prescription($p, $dummy);
677 break;
678 case (xl("Print")." (".xl("HTML").")"):
679 $this->_state = false;
680 return $this->_print_prescription_css($p, $dummy);
681 break;
682 case xl("Print To Fax"):
683 $this->_state = false;
684 $this->is_print_to_fax = true;
685 return $this->_print_prescription($p, $dummy);
686 break;
687 case xl("Email"):
688 return $this->_email_prescription($p,$_POST['email_to']);
689 break;
690 case xl("Fax"):
691 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
692 //$this->assign("process_result","No fax server is currently setup.");
693 return $this->_fax_prescription($p,$_POST['fax_to']);
694 break;
695 case xl("Auto Send"):
696 $pharmacy_id = $_POST['pharmacy_id'];
697 //echo "auto sending to : " . $_POST['pharmacy_id'];
698 $phar = new Pharmacy($_POST['pharmacy_id']);
699 //print_r($phar);
700 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
701 return $this->_print_prescription($p, $dummy);
703 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
704 $email = $phar->get_email();
705 if (!empty($email)) {
706 return $this->_email_prescription($p,$phar->get_email());
708 //else print it
710 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
711 $faxNum= $phar->get_fax();
712 if(!empty($faxNum)) {
713 Return $this->_fax_prescription ($p,$faxNum);
715 // return $this->assign("process_result","No fax server is currently setup.");
716 // else default is printing,
718 else {
719 //the pharmacy has no default or default is print
720 return $this->_print_prescription($p, $dummy);
722 break;
725 return;
729 function _print_prescription($p, & $toFile) {
730 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
731 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
732 ,$GLOBALS['rx_bottom_margin']
733 ,$GLOBALS['rx_left_margin']
734 ,$GLOBALS['rx_right_margin']
737 $pdf->selectFont('Helvetica');
739 // Signature images are to be used only when faxing.
740 if(!empty($toFile)) $this->is_faxing = true;
742 $this->multiprint_header($pdf, $p);
743 $this->multiprint_body($pdf, $p);
744 $this->multiprint_footer($pdf);
746 if(!empty($toFile)) {
747 $toFile = $pdf->ezOutput();
749 else {
750 $pdf->ezStream();
751 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
753 return;
756 function _print_prescription_css($p, & $toFile) {
758 $this->multiprintcss_preheader();
759 $this->multiprintcss_header($p);
760 $this->multiprintcss_body($p);
761 $this->multiprintcss_footer();
762 $this->multiprintcss_postfooter();
766 function _print_prescription_old($p, & $toFile) {
767 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
768 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
769 ,$GLOBALS['rx_bottom_margin']
770 ,$GLOBALS['rx_left_margin']
771 ,$GLOBALS['rx_right_margin']
773 $pdf->selectFont('Helvetica');
774 if(!empty($this->pconfig['logo'])) {
775 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
777 $pdf->ezText($p->get_prescription_display(),10);
778 if($this->pconfig['use_signature']) {
779 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
781 else{
782 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
784 if(!empty($toFile))
786 $toFile = $pdf->ezOutput();
788 else
790 $pdf->ezStream();
791 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
793 return;
796 function _email_prescription($p,$email) {
797 if (empty($email)) {
798 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
799 return;
801 $mail = new PHPMailer();
802 //this is a temporary config item until the rest of the per practice billing settings make their way in
803 $mail->From = $GLOBALS['practice_return_email_path'];
804 $mail->FromName = $p->provider->get_name_display();
805 $mail->isMail();
806 $mail->Host = "localhost";
807 $mail->Mailer = "mail";
808 $text_body = $p->get_prescription_display();
809 $mail->Body = $text_body;
810 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
811 $mail->AddAddress($email);
812 if($mail->Send()) {
813 $this->assign("process_result","Email was successfully sent to: " . $email);
814 return;
816 else {
817 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
818 return;
822 function do_lookup() {
823 if ($_POST['process'] != "true") {
824 // don't do a lookup
825 $this->assign("drug", $_GET['drug']);
826 return;
829 // process the lookup
830 $this->assign("drug", $_POST['drug']);
831 $list = array();
832 if (!empty($_POST['drug'])) {
833 $list = $this->RxList->get_list($_POST['drug']);
836 if (is_array($list)) {
837 $list = array_flip($list);
838 $this->assign("drug_options",$list);
839 $this->assign("drug_values",array_keys($list));
841 else {
842 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
844 //print_r($_POST);
845 //$this->assign("PROCESS","");
847 $_POST['process'] = "";
850 function _fax_prescription($p,$faxNum)
852 $err = "Sent fax";
853 //strip - ,(, ), and ws
854 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
855 //validate the number
857 if(!empty($faxNum) && is_numeric($faxNum))
859 //get the sendfax command and execute it
860 $cmd = $this->pconfig['sendfax'];
861 // prepend any prefix to the fax number
862 $pref=$this->pconfig['prefix'];
863 $faxNum=$pref.$faxNum;
864 if(empty($cmd))
866 $err .= " Send fax not set in includes/config.php";
868 else
870 //generate file to fax
871 $faxFile = "Failed";
872 $this->_print_prescription($p, $faxFile);
873 if(empty($faxFile))
875 $err .= " _print_prescription returned empty file";
877 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
878 $p->get_patient_id() . "_fax_.pdf";
879 //print "filename is $fileName";
880 touch($fileName); // php bug
881 $handle = fopen($fileName,"w");
882 if(!$handle)
884 $err .= " Failed to open file $fileName to write fax to";
886 if(fwrite($handle, $faxFile) === false)
888 $err .= " Failed to write data to $fileName";
890 fclose($handle);
891 $args = " -n -d $faxNum $fileName";
892 //print "command is $cmd $args<br>";
893 exec($cmd . $args);
897 else
899 $err = "bad fax number passed to function";
901 if($err)
903 $this->assign("process_result",$err);