Fix of lists_touch sql table bug in mysql verion 5.7 and higher.
[openemr.git] / controllers / C_Prescription.class.php
bloba710b03fd68ab184c6bc19d05c600fc94beb32cf
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 " .
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
88 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
89 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
90 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
94 function default_action() {
95 $this->assign("prescription",$this->prescriptions[0]);
96 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
99 function edit_action($id = "",$patient_id="",$p_obj = null) {
101 if ($p_obj != null && get_class($p_obj) == "prescription") {
102 $this->prescriptions[0] = $p_obj;
104 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
105 $this->prescriptions[0] = new Prescription($id);
108 if (!empty($patient_id)) {
109 $this->prescriptions[0]->set_patient_id($patient_id);
112 // If quantity to dispense is not already set from a POST, set its
113 // default value.
114 if (! $this->get_template_vars('DISP_QUANTITY')) {
115 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
118 $this->default_action();
121 function list_action($id,$sort = "") {
122 if (empty($id)) {
123 $this->function_argument_error();
124 exit;
126 if (!empty($sort)) {
127 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
129 else {
130 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
133 // flag to indicate the CAMOS form is regsitered and active
134 $this->assign("CAMOS_FORM", isRegistered("CAMOS"));
136 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
139 function block_action($id,$sort = "") {
140 if (empty($id)) {
141 $this->function_argument_error();
142 exit;
144 if (!empty($sort)) {
145 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
147 else {
148 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
150 //print_r(Prescription::prescriptions_factory($id));
151 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
154 function fragment_action($id,$sort = "") {
155 if (empty($id)) {
156 $this->function_argument_error();
157 exit;
159 if (!empty($sort)) {
160 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
162 else {
163 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
165 //print_r(Prescription::prescriptions_factory($id));
166 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_fragment.html");
169 function lookup_action() {
170 $this->do_lookup();
171 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
174 function edit_action_process() {
175 if ($_POST['process'] != "true")
176 return;
177 //print_r($_POST);
179 // Stupid Smarty code treats empty values as not specified values.
180 // Since active is a checkbox, represent the unchecked state as -1.
181 if (empty($_POST['active'])) $_POST['active'] = '-1';
183 $this->prescriptions[0] = new Prescription($_POST['id']);
184 parent::populate_object($this->prescriptions[0]);
185 //echo $this->prescriptions[0]->toString(true);
186 $this->prescriptions[0]->persist();
187 $_POST['process'] = "";
189 // If the "Prescribe and Dispense" button was clicked, then
190 // redisplay as in edit_action() but also replicate the fee and
191 // include a piece of javascript to call dispense().
193 if ($_POST['disp_button']) {
194 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
195 $this->assign("DISP_FEE", $_POST['disp_fee']);
196 $this->assign("ENDING_JAVASCRIPT", "dispense();");
197 $this->_state = false;
198 return $this->edit_action($this->prescriptions[0]->id);
201 // Set the AMC reporting flag (to record percentage of prescriptions that
202 // are set as e-prescriptions)
203 if (!(empty($_POST['escribe_flag']))) {
204 // add the e-prescribe flag
205 processAmcCall('e_prescribe_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
207 else {
208 // remove the e-prescribe flag
209 processAmcCall('e_prescribe_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
212 // Set the AMC reporting flag (to record prescriptions that checked drug formulary)
213 if (!(empty($_POST['checked_formulary_flag']))) {
214 // add the e-prescribe flag
215 processAmcCall('e_prescribe_chk_formulary_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
217 else {
218 // remove the e-prescribe flag
219 processAmcCall('e_prescribe_chk_formulary_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
222 // Set the AMC reporting flag (to record prescriptions that are controlled substances)
223 if (!(empty($_POST['controlled_substance_flag']))) {
224 // add the e-prescribe flag
225 processAmcCall('e_prescribe_cont_subst_amc', true, 'add', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
227 else {
228 // remove the e-prescribe flag
229 processAmcCall('e_prescribe_cont_subst_amc', true, 'remove', $this->prescriptions[0]->get_patient_id(), 'prescriptions', $this->prescriptions[0]->id);
232 // TajEmo Work by CB 2012/05/29 02:58:29 PM to stop from going to send screen. Improves Work Flow
233 // if ($this->prescriptions[0]->get_active() > 0) {
234 // return $this->send_action($this->prescriptions[0]->id);
235 // }
236 $this->list_action($this->prescriptions[0]->get_patient_id());
237 exit;
240 function send_action($id) {
241 $_POST['process'] = "true";
242 if(empty($id)) {
243 $this->function_argument_error();
246 $rx = new Prescription($id);
247 // Populate pharmacy info if the patient has a default pharmacy.
248 // Probably the Prescription object should handle this instead, but
249 // doing it there will require more careful research and testing.
250 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
251 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
252 if ($prow['pharmacy_id']) {
253 $rx->pharmacy->set_id($prow['pharmacy_id']);
254 $rx->pharmacy->populate();
256 $this->assign("prescription", $rx);
258 $this->_state = false;
259 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
260 $this->template_mod . "_send.html");
263 function multiprintfax_header(& $pdf, $p) {
264 return $this->multiprint_header( $pdf, $p );
267 function multiprint_header(& $pdf, $p) {
268 $this->providerid = $p->provider->id;
269 //print header
270 $pdf->ezImage($GLOBALS['oer_config']['prescriptions']['logo'],'','50','','center','');
271 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
272 $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 ='" .
273 add_escape_custom($p->provider->id) . "'");
274 $pdf->ezText($res['addr'],12);
275 $my_y = $pdf->y;
276 $pdf->ezNewPage();
277 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
278 // A client had a bad experience with a patient misusing a DEA number, so
279 // now the doctors write those in on printed prescriptions and only when
280 // necessary. If you need to change this back, then please make it a
281 // configurable option. Faxed prescriptions were not changed. -- Rod
282 // Now it is configureable. Change value in
283 // Administration->Globals->Rx
284 if ($GLOBALS['rx_enable_DEA']) {
285 if ($this->is_faxing || $GLOBALS['rx_show_DEA']) {
286 $pdf->ezText('<b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id, 12);
288 else {
289 $pdf->ezText('<b>' . xl('DEA') . ':</b> ________________________', 12);
293 if ($GLOBALS['rx_enable_NPI']) {
294 if ($this->is_faxing || $GLOBALS['rx_show_NPI']) {
295 $pdf->ezText('<b>' . xl('NPI') . ':</b>' . $p->provider->npi, 12);
297 else {
298 $pdf->ezText('<b>' . xl('NPI') . ':</b> _________________________', 12);
301 if ($GLOBALS['rx_enable_SLN']) {
302 if ($this->is_faxing || $GLOBALS['rx_show_SLN']) {
303 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number, 12);
305 else {
306 $pdf->ezText('<b>' . xl('State Lic. #') . ':</b> ___________________', 12);
309 $pdf->ezColumnsStop();
310 if ($my_y < $pdf->y){
311 $pdf->ezSetY($my_y);
313 $pdf->ezText('',10);
314 $pdf->setLineStyle(1);
315 $pdf->ezColumnsStart(array('num'=>2));
316 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
317 $pdf->ezText('<b>' . xl('Patient Name & Address') . '</b>',6);
318 $pdf->ezText($p->patient->get_name_display(),10);
319 $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));
320 $pdf->ezText($res['addr']);
321 $my_y = $pdf->y;
322 $pdf->ezNewPage();
323 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
324 $pdf->ezText('<b>' . xl('Date of Birth') . '</b>',6);
325 $pdf->ezText($p->patient->date_of_birth,10);
326 $pdf->ezText('');
327 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
328 $pdf->ezText('<b>' . xl('Medical Record #') . '</b>',6);
329 $pdf->ezText(str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT),10);
330 $pdf->ezColumnsStop();
331 if ($my_y < $pdf->y){
332 $pdf->ezSetY($my_y);
334 $pdf->ezText('');
335 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
336 $pdf->ezText('<b>' . xl('Prescriptions') . '</b>',6);
337 $pdf->ezText('',10);
340 function multiprintcss_header($p) {
341 echo("<div class='paddingdiv'>\n");
342 $this->providerid = $p->provider->id;
343 echo ("<table cellspacing='0' cellpadding='0' width='100%'>\n");
344 echo ("<tr>\n");
345 echo ("<td></td>\n");
346 echo ("<td>\n");
347 echo ("<img WIDTH='68pt' src='./interface/pic/" . $GLOBALS['oer_config']['prescriptions']['logo_pic'] . "' />");
348 echo ("</td>\n");
349 echo ("</tr>\n");
350 echo ("<tr>\n");
351 echo ("<td>\n");
352 $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) . "'");
353 $patterns = array ('/\n/','/Tel:/','/Fax:/');
354 $replace = array ('<br>', xl('Tel').':', xl('Fax').':');
355 $res = preg_replace($patterns, $replace, $res);
356 echo ('<span class="large">' . $res['addr'] . '</span>');
357 echo ("</td>\n");
358 echo ("<td>\n");
359 echo ('<b><span class="large">' . $p->provider->get_name_display() . '</span></b>'. '<br>');
361 if ($GLOBALS['rx_enable_DEA']) {
362 if ($GLOBALS['rx_show_DEA']) {
363 echo ('<span class="large"><b>' . xl('DEA') . ':</b>' . $p->provider->federal_drug_id . '</span><br>');
365 else {
366 echo ('<b><span class="large">' . xl('DEA') . ':</span></b> ________________________<br>' );
369 if ($GLOBALS['rx_enable_NPI']) {
370 if ($GLOBALS['rx_show_NPI']) {
371 echo ('<span class="large"><b>' . xl('NPI') . ':</b>' . $p->provider->npi . '</span><br>');
373 else {
374 echo ('<b><span class="large">' . xl('NPI') . ':</span></b> ________________________<br>');
377 if ($GLOBALS['rx_enable_SLN']) {
378 if ($GLOBALS['rx_show_SLN']) {
379 echo ('<span class="large"><b>' . xl('State Lic. #') . ':</b>' . $p->provider->state_license_number . '</span><br>');
381 else {
382 echo ('<b><span class="large">' . xl('State Lic. #') . ':</span></b> ________________________<br>');
385 echo ("</td>\n");
386 echo ("</tr>\n");
387 echo ("<tr>\n");
388 echo ("<td rowspan='2' class='bordered'>\n");
389 echo ('<b><span class="small">' . xl('Patient Name & Address') . '</span></b>'. '<br>');
390 echo ($p->patient->get_name_display() . '<br>');
391 $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));
392 $patterns = array ('/\n/');
393 $replace = array ('<br>');
394 $res = preg_replace($patterns, $replace, $res);
395 echo ($res['addr']);
396 echo ("</td>\n");
397 echo ("<td class='bordered'>\n");
398 echo ('<b><span class="small">' . xl('Date of Birth') . '</span></b>' . '<br>');
399 echo ($p->patient->date_of_birth );
400 echo ("</td>\n");
401 echo ("</tr>\n");
402 echo ("<tr>\n");
403 echo ("<td class='bordered'>\n");
404 echo ('<b><span class="small">' . xl('Medical Record #') . '</span></b>' . '<br>');
405 echo (str_pad($p->patient->get_pubpid(), 10, "0", STR_PAD_LEFT));
406 echo ("</td>\n");
407 echo ("</tr>\n");
408 echo ("<tr>\n");
409 echo ("<td colspan='2' class='bordered'>\n");
410 echo ('<b><span class="small">' . xl('Prescriptions') . '</span></b>');
411 echo ("</td>\n");
412 echo ("</tr>\n");
413 echo ("</table>\n");
416 function multiprintcss_preheader() {
417 // this sets styling and other header information of the multiprint css sheet
418 echo ("<html>\n");
419 echo ("<head>\n");
420 echo ("<style>\n");
421 echo ("div {\n");
422 echo (" padding: 0;\n");
423 echo (" margin: 0;\n");
424 echo ("}\n");
425 echo ("body {\n");
426 echo (" font-family: sans-serif;\n");
427 echo (" font-weight: normal;\n");
428 echo (" font-size: 10pt;\n");
429 echo (" background: white;\n");
430 echo (" color: black;\n");
431 echo ("}\n");
432 echo ("span.large {\n");
433 echo (" font-size: 12pt;\n");
434 echo ("}\n");
435 echo ("span.small {\n");
436 echo (" font-size: 6pt;\n");
437 echo ("}\n");
438 echo ("td {\n");
439 echo (" vertical-align: top;\n");
440 echo (" width: 50%;\n");
441 echo (" font-size: 10pt;\n");
442 echo (" padding-bottom: 8pt;\n");
443 echo ("}\n");
444 echo ("td.bordered {\n");
445 echo (" border-top:1pt solid black;\n");
446 echo ("}\n");
447 echo ("div.paddingdiv {\n");
448 echo (" width: 524pt;\n");
449 echo (" height: 668pt;\n");
450 echo ("}\n");
451 echo ("div.scriptdiv {\n");
452 echo (" padding-top: 12pt;\n");
453 echo (" padding-bottom: 22pt;\n");
454 echo (" padding-left: 35pt;\n");
455 echo (" border-bottom:1pt solid black;\n");
456 echo ("}\n");
457 echo ("div.signdiv {\n");
458 echo (" margin-top: 40pt;\n");
459 echo (" font-size: 12pt;\n");
460 echo ("}\n");
461 echo ("</style>\n");
463 echo ("<title>" . xl('Prescription') . "</title>\n");
464 echo ("</head>\n");
465 echo ("<body>\n");
468 function multiprintfax_footer( & $pdf ) {
469 return $this->multiprint_footer( $pdf );
472 function multiprint_footer(& $pdf) {
473 if($this->pconfig['use_signature'] && ( $this->is_faxing || $this->is_print_to_fax ) ) {
474 $sigfile = str_replace('{userid}', $_SESSION{"authUser"}, $this->pconfig['signature']);
475 if (file_exists($sigfile)) {
476 $pdf->ezText( xl('Signature') . ": ",12);
477 // $pdf->ezImage($sigfile, "", "", "none", "left");
478 $pdf->ezImage($sigfile, "", "", "none", "center");
479 $pdf->ezText( xl('Date') . ": " . date('Y-m-d'), 12);
480 if ( $this->is_print_to_fax ) {
481 $pdf->ezText(xl('Please do not accept this prescription unless it was received via facsimile.'));
484 $addenumFile = $this->pconfig['addendum_file'];
485 if ( file_exists( $addenumFile ) ) {
486 $pdf->ezText('');
487 $f = fopen($addenumFile, "r");
488 while ( $line = fgets($f, 1000) ) {
489 $pdf->ezText(rtrim($line));
493 return;
496 $pdf->ezText("\n\n\n\n" . xl('Signature') . ":________________________________\n" . xl('Date') . ": " . date('Y-m-d'),12);
499 function multiprintcss_footer() {
500 echo ("<div class='signdiv'>\n");
501 echo (xl('Signature') . ":________________________________<br>");
502 echo (xl('Date') . ": " . date('Y-m-d'));
503 echo ("</div>\n");
504 echo ("</div>\n");
507 function multiprintcss_postfooter() {
508 echo("<script language='JavaScript'>\n");
509 echo("opener.top.printLogPrint(window);\n");
510 echo("</script>\n");
511 echo("</body>\n");
512 echo("</html>\n");
515 function get_prescription_body_text($p) {
516 $body = '<b>' . xlt('Rx') . ': ' . text($p->get_drug()) . ' ' . text($p->get_size()) . ' ' . text($p->get_unit_display());
517 if ($p->get_form()) $body .= ' [' . text($p->form_array[$p->get_form()]) . "]";
518 $body .= "</b> <i>" .
519 text($p->substitute_array[$p->get_substitute()]) . "</i>\n" .
520 '<b>' . xlt('Disp #') . ':</b> <u>' . text($p->get_quantity()) . "</u>\n" .
521 '<b>' . xlt('Sig') . ':</b> ' . text($p->get_dosage()) . ' ' . text($p->form_array[$p->get_form()]) . ' ' .
522 text($p->route_array[$p->get_route()]) . ' ' . text($p->interval_array[$p->get_interval()]) . "\n";
523 if ($p->get_refills() > 0) {
524 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>" . text($p->get_refills());
525 if ($p->get_per_refill()) {
526 $body .= " " . xlt('of quantity') . " " . text($p->get_per_refill());
528 $body .= "</u>\n";
530 else {
531 $body .= "\n<b>" . xlt('Refills') . ":</b> <u>0 (" . xlt('Zero') . ")</u>\n";
533 $note = $p->get_note();
534 if ($note != '') {
535 $body .= "\n" . text($note) . "\n";
537 return $body;
540 function multiprintfax_body(& $pdf, $p){
541 return $this->multiprint_body( $pdf, $p );
544 function multiprint_body(& $pdf, $p){
545 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
546 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
547 $d = $this->get_prescription_body_text($p);
548 if ( $pdf->ezText($d,10,array(),1) ) {
549 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
550 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
551 $this->multiprint_footer($pdf);
552 $pdf->ezNewPage();
553 $this->multiprint_header($pdf, $p);
554 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
555 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
557 $my_y = $pdf->y;
558 $pdf->ezText($d,10);
559 if($this->pconfig['shading']) {
560 $pdf->setColor(.9,.9,.9);
561 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
562 $pdf->setColor(0,0,0);
564 $pdf->ezSetY($my_y);
565 $pdf->ezText($d,10);
566 $pdf->ez['leftMargin'] = $GLOBALS['rx_left_margin'];
567 $pdf->ez['rightMargin'] = $GLOBALS['rx_right_margin'];
568 $pdf->ezText('');
569 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
570 $pdf->ezText('');
573 function multiprintcss_body($p){
574 $d = $this->get_prescription_body_text($p);
575 $patterns = array ('/\n/','/ /');
576 $replace = array ('<br>','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
577 $d = preg_replace($patterns, $replace, $d);
578 echo ("<div class='scriptdiv'>\n" . $d . "</div>\n");
581 function multiprintfax_action($id = "") {
582 $this->is_print_to_fax=true;
583 return $this->multiprint_action( $id );
586 function multiprint_action($id = "") {
587 $_POST['process'] = "true";
588 if(empty($id)) {
589 $this->function_argument_error();
591 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
592 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
593 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
594 ,$GLOBALS['rx_bottom_margin']
595 ,$GLOBALS['rx_left_margin']
596 ,$GLOBALS['rx_right_margin']
598 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
600 // $print_header = true;
601 $on_this_page = 0;
603 //print prescriptions body
604 $this->_state = false; // Added by Rod - see Controller.class.php
605 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
606 foreach ($ids as $id) {
607 $p = new Prescription($id);
608 // if ($print_header == true) {
609 if ($on_this_page == 0) {
610 $this->multiprint_header($pdf, $p);
612 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
613 $this->multiprint_footer($pdf);
614 $pdf->ezNewPage();
615 $this->multiprint_header($pdf, $p);
616 // $print_header = false;
617 $on_this_page = 1;
619 $this->multiprint_body($pdf, $p);
622 $this->multiprint_footer($pdf);
624 $pdf->ezStream();
625 return;
628 function multiprintcss_action($id = "") {
629 $_POST['process'] = "true";
630 if(empty($id)) {
631 $this->function_argument_error();
634 $this->multiprintcss_preheader();
636 $this->_state = false; // Added by Rod - see Controller.class.php
637 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
639 $on_this_page = 0;
640 foreach ($ids as $id) {
641 $p = new Prescription($id);
642 if ($on_this_page == 0) {
643 $this->multiprintcss_header($p);
645 if (++$on_this_page > 3 || $p->provider->id != $this->providerid) {
646 $this->multiprintcss_footer();
647 $this->multiprintcss_header($p);
648 $on_this_page = 1;
650 $this->multiprintcss_body($p);
652 $this->multiprintcss_footer();
653 $this->multiprintcss_postfooter();
654 return;
657 function send_action_process($id) {
658 $dummy = ""; // Added by Rod to avoid run-time warnings
659 if ($_POST['process'] != "true")
660 return;
661 if(empty($id)) {
662 $this->function_argument_error();
664 $p = new Prescription($id);
665 switch ($_POST['submit']) {
667 case (xl("Print")." (".xl("PDF").")"):
668 // The following statement added by Rod.
669 // Looking at Controller.class.php, it appears that _state is set to false
670 // to indicate that no further HTML is to be generated.
671 $this->_state = false; // Added by Rod - see Controller.class.php
672 return $this->_print_prescription($p, $dummy);
673 break;
674 case (xl("Print")." (".xl("HTML").")"):
675 $this->_state = false;
676 return $this->_print_prescription_css($p, $dummy);
677 break;
678 case xl("Print To Fax"):
679 $this->_state = false;
680 $this->is_print_to_fax = true;
681 return $this->_print_prescription($p, $dummy);
682 break;
683 case xl("Email"):
684 return $this->_email_prescription($p,$_POST['email_to']);
685 break;
686 case xl("Fax"):
687 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
688 //$this->assign("process_result","No fax server is currently setup.");
689 return $this->_fax_prescription($p,$_POST['fax_to']);
690 break;
691 case xl("Auto Send"):
692 $pharmacy_id = $_POST['pharmacy_id'];
693 //echo "auto sending to : " . $_POST['pharmacy_id'];
694 $phar = new Pharmacy($_POST['pharmacy_id']);
695 //print_r($phar);
696 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
697 return $this->_print_prescription($p, $dummy);
699 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
700 $email = $phar->get_email();
701 if (!empty($email)) {
702 return $this->_email_prescription($p,$phar->get_email());
704 //else print it
706 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
707 $faxNum= $phar->get_fax();
708 if(!empty($faxNum)) {
709 Return $this->_fax_prescription ($p,$faxNum);
711 // return $this->assign("process_result","No fax server is currently setup.");
712 // else default is printing,
714 else {
715 //the pharmacy has no default or default is print
716 return $this->_print_prescription($p, $dummy);
718 break;
721 return;
725 function _print_prescription($p, & $toFile) {
726 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
727 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
728 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
729 ,$GLOBALS['rx_bottom_margin']
730 ,$GLOBALS['rx_left_margin']
731 ,$GLOBALS['rx_right_margin']
734 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
736 // Signature images are to be used only when faxing.
737 if(!empty($toFile)) $this->is_faxing = true;
739 $this->multiprint_header($pdf, $p);
740 $this->multiprint_body($pdf, $p);
741 $this->multiprint_footer($pdf);
743 if(!empty($toFile)) {
744 $toFile = $pdf->ezOutput();
746 else {
747 $pdf->ezStream();
748 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
750 return;
753 function _print_prescription_css($p, & $toFile) {
755 $this->multiprintcss_preheader();
756 $this->multiprintcss_header($p);
757 $this->multiprintcss_body($p);
758 $this->multiprintcss_footer();
759 $this->multiprintcss_postfooter();
763 function _print_prescription_old($p, & $toFile) {
764 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
765 $pdf = new Cezpdf($GLOBALS['rx_paper_size']);
766 $pdf->ezSetMargins($GLOBALS['rx_top_margin']
767 ,$GLOBALS['rx_bottom_margin']
768 ,$GLOBALS['rx_left_margin']
769 ,$GLOBALS['rx_right_margin']
771 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
772 if(!empty($this->pconfig['logo'])) {
773 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
775 $pdf->ezText($p->get_prescription_display(),10);
776 if($this->pconfig['use_signature']) {
777 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
779 else{
780 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
782 if(!empty($toFile))
784 $toFile = $pdf->ezOutput();
786 else
788 $pdf->ezStream();
789 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
791 return;
794 function _email_prescription($p,$email) {
795 if (empty($email)) {
796 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
797 return;
799 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
800 $mail = new PHPMailer();
801 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
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";
867 break;
869 else
871 //generate file to fax
872 $faxFile = "Failed";
873 $this->_print_prescription($p, $faxFile);
874 if(empty($faxFile))
876 $err .= " _print_prescription returned empty file";
877 break;
879 $fileName = $GLOBALS['OE_SITE_DIR'] . "/documents/" . $p->get_id() .
880 $p->get_patient_id() . "_fax_.pdf";
881 //print "filename is $fileName";
882 touch($fileName); // php bug
883 $handle = fopen($fileName,"w");
884 if(!$handle)
886 $err .= " Failed to open file $fileName to write fax to";
887 break;
889 if(fwrite($handle, $faxFile) === false)
891 $err .= " Failed to write data to $fileName";
892 break;
894 fclose($handle);
895 $args = " -n -d $faxNum $fileName";
896 //print "command is $cmd $args<br>";
897 exec($cmd . $args);
901 else
903 $err = "bad fax number passed to function";
905 if($err)
907 $this->assign("process_result",$err);