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");
8 class C_Prescription
extends Controller
{
13 var $is_faxing = false;
15 function C_Prescription($template_mod = "general") {
18 $this->template_mod
= $template_mod;
19 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
20 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
21 $this->assign("STYLE", $GLOBALS['style']);
22 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
23 $this->assign("SIMPLIFIED_PRESCRIPTIONS", $GLOBALS['simplified_prescriptions']);
24 $this->pconfig
= $GLOBALS['oer_config']['prescriptions'];
26 if ($GLOBALS['inhouse_pharmacy']) {
27 // Make an array of drug IDs and selectors for the template.
28 $drug_array_values = array(0);
29 $drug_array_output = array("-- or select from inventory --");
30 $drug_attributes = '';
32 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
34 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
35 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
36 "t.period, t.quantity, t.refills " .
37 "FROM drug_templates AS t, drugs AS d WHERE " .
38 "d.drug_id = t.drug_id ORDER BY t.selector");
40 while ($row = sqlFetchArray($res)) {
41 $tmp_output = $row['selector'];
42 if ($row['ndc_number']) {
43 $tmp_output .= ' [' . $row['ndc_number'] . ']';
45 $drug_array_values[] = $row['drug_id'];
46 $drug_array_output[] = $tmp_output;
47 if ($drug_attributes) $drug_attributes .= ',';
48 $drug_attributes .= "['" .
49 $row['name'] . "'," . // 0
50 $row['form'] . ",'" . // 1
51 $row['dosage'] . "'," . // 2
52 $row['size'] . "," . // 3
53 $row['unit'] . "," . // 4
54 $row['route'] . "," . // 5
55 $row['period'] . "," . // 6
56 $row['substitute'] . "," . // 7
57 $row['quantity'] . "," . // 8
58 $row['refills'] . "," . // 9
59 $row['quantity'] . "]"; // 10 quantity per_refill
61 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
62 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
63 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
67 function default_action() {
68 $this->assign("prescription",$this->prescriptions
[0]);
69 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_edit.html");
72 function edit_action($id = "",$patient_id="",$p_obj = null) {
74 if ($p_obj != null && get_class($p_obj) == "prescription") {
75 $this->prescriptions
[0] = $p_obj;
77 elseif (get_class($this->prescriptions
[0]) != "prescription" ) {
78 $this->prescriptions
[0] = new Prescription($id);
81 if (!empty($patient_id)) {
82 $this->prescriptions
[0]->set_patient_id($patient_id);
85 // If quantity to dispense is not already set from a POST, set its
87 if (! $this->get_template_vars('DISP_QUANTITY')) {
88 $this->assign('DISP_QUANTITY', $this->prescriptions
[0]->quantity
);
91 $this->default_action();
94 function list_action($id,$sort = "") {
96 $this->function_argument_error();
100 $this->assign("prescriptions", Prescription
::prescriptions_factory($id,$sort));
103 $this->assign("prescriptions", Prescription
::prescriptions_factory($id));
105 //print_r(Prescription::prescriptions_factory($id));
106 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_list.html");
109 function block_action($id,$sort = "") {
111 $this->function_argument_error();
115 $this->assign("prescriptions", Prescription
::prescriptions_factory($id,$sort));
118 $this->assign("prescriptions", Prescription
::prescriptions_factory($id));
120 //print_r(Prescription::prescriptions_factory($id));
121 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_block.html");
124 function lookup_action() {
126 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_lookup.html");
129 function edit_action_process() {
130 if ($_POST['process'] != "true")
134 // Stupid Smarty code treats empty values as not specified values.
135 // Since active is a checkbox, represent the unchecked state as -1.
136 if (empty($_POST['active'])) $_POST['active'] = '-1';
138 $this->prescriptions
[0] = new Prescription($_POST['id']);
139 parent
::populate_object($this->prescriptions
[0]);
140 //echo $this->prescriptions[0]->toString(true);
141 $this->prescriptions
[0]->persist();
142 $_POST['process'] = "";
144 // If the "Prescribe and Dispense" button was clicked, then
145 // redisplay as in edit_action() but also replicate the fee and
146 // include a piece of javascript to call dispense().
148 if ($_POST['disp_button']) {
149 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
150 $this->assign("DISP_FEE", $_POST['disp_fee']);
151 $this->assign("ENDING_JAVASCRIPT", "dispense();");
152 $this->_state
= false;
153 return $this->edit_action($this->prescriptions
[0]->id
);
156 if ($this->prescriptions
[0]->get_active() > 0) {
157 return $this->send_action($this->prescriptions
[0]->id
);
159 $this->list_action($this->prescriptions
[0]->get_patient_id());
163 function send_action($id) {
164 $_POST['process'] = "true";
166 $this->function_argument_error();
169 $rx = new Prescription($id);
170 // Populate pharmacy info if the patient has a default pharmacy.
171 // Probably the Prescription object should handle this instead, but
172 // doing it there will require more careful research and testing.
173 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
174 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
175 if ($prow['pharmacy_id']) {
176 $rx->pharmacy
->set_id($prow['pharmacy_id']);
177 $rx->pharmacy
->populate();
179 $this->assign("prescription", $rx);
181 $this->_state
= false;
182 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
183 $this->template_mod
. "_send.html");
186 function multiprint_header(& $pdf, $p) {
187 $this->providerid
= $p->provider
->id
;
189 $pdf->ezImage($GLOBALS['fileroot'] . '/interface/pic/Rx.png','','50','','center','');
190 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
191 $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 ='" .
192 mysql_real_escape_string($p->provider
->id
) . "'");
193 $pdf->ezText($res['addr'],12);
196 $pdf->ezText('<b>' . $p->provider
->get_name_display() . '</b>',12);
197 // A client had a bad experience with a patient misusing a DEA number, so
198 // now the doctors write those in on printed prescriptions and only when
199 // necessary. If you need to change this back, then please make it a
200 // configurable option. Faxed prescriptions were not changed. -- Rod
201 // Now it is configureable. Change value in
202 // <openemr root>/includes/config.php - Tekknogenius
203 if ($this->is_faxing ||
$GLOBALS['oer_config']['prescriptions']['show_DEA'])
204 $pdf->ezText('<b>DEA:</b>' . $p->provider
->federal_drug_id
, 12);
206 $pdf->ezText('<b>DEA:</b> ________________________', 12);
207 $pdf->ezColumnsStop();
208 if ($my_y < $pdf->y
){
212 $pdf->setLineStyle(1);
213 $pdf->ezColumnsStart(array('num'=>2));
214 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
215 $pdf->ezText('<b>Patient Name & Address</b>',6);
216 $pdf->ezText($p->patient
->get_name_display(),10);
217 $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
));
218 $pdf->ezText($res['addr']);
221 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
222 $pdf->ezText('<b>Date of Birth</b>',6);
223 $pdf->ezText($p->patient
->date_of_birth
,10);
225 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
226 $pdf->ezText('<b>Medical Record #</b>',6);
227 $pdf->ezText(str_pad($p->patient
->get_id(), 10, "0", STR_PAD_LEFT
),10);
228 $pdf->ezColumnsStop();
229 if ($my_y < $pdf->y
){
233 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
234 $pdf->ezText('<b>Prescriptions</b>',6);
238 function multiprint_footer(& $pdf) {
239 if($this->pconfig
['use_signature'] && $this->is_faxing
) {
240 $sigfile = str_replace('{userid}', $this->providerid
, $this->pconfig
['signature']);
241 if (file_exists($sigfile)) {
242 $pdf->ezText("Signature: ",12);
243 // $pdf->ezImage($sigfile, "", "", "none", "left");
244 $pdf->ezImage($sigfile, "", "", "none", "center");
245 $pdf->ezText("Date: " . date('Y-m-d'), 12);
249 $pdf->ezText("\n\n\n\nSignature:________________________________\nDate: " . date('Y-m-d'),12);
252 function get_prescription_body_text($p) {
253 $body = '<b>Rx: ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
254 if ($p->get_form()) $body .= ' [' . $p->form_array
[$p->get_form()] . "]";
255 $body .= "</b> <i>" .
256 $p->substitute_array
[$p->get_substitute()] . "</i>\n" .
257 '<b>Disp #:</b> <u>' . $p->get_quantity() . "</u>\n" .
258 '<b>Sig:</b> ' . $p->get_dosage() . ' ' . $p->form_array
[$p->get_form()] . ' ' .
259 $p->route_array
[$p->get_route()] . ' ' . $p->interval_array
[$p->get_interval()] . "\n";
260 if ($p->get_refills() > 0) {
261 $body .= "\n<b>Refills:</b> <u>" . $p->get_refills();
262 if ($p->get_per_refill()) {
263 $body .= " of quantity " . $p->get_per_refill();
268 $body .= "\n<b>Refills:</b> <u>0 (Zero)</u>\n";
270 $note = $p->get_note();
272 $body .= "\n$note\n";
277 function multiprint_body(& $pdf, $p){
278 $pdf->ez
['leftMargin'] +
= $pdf->ez
['leftMargin'];
279 $pdf->ez
['rightMargin'] +
= $pdf->ez
['rightMargin'];
280 $d = $this->get_prescription_body_text($p);
281 if ( $pdf->ezText($d,10,array(),1) ) {
282 $pdf->ez
['leftMargin'] -= $pdf->ez
['leftMargin'];
283 $pdf->ez
['rightMargin'] -= $pdf->ez
['rightMargin'];
284 $this->multiprint_footer($pdf);
286 $this->multiprint_header($pdf, $p);
287 $pdf->ez
['leftMargin'] +
= $pdf->ez
['leftMargin'];
288 $pdf->ez
['rightMargin'] +
= $pdf->ez
['rightMargin'];
292 if($this->pconfig
['shading']) {
293 $pdf->setColor(.9,.9,.9);
294 $pdf->filledRectangle($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin']-$pdf->ez
['leftMargin'],$my_y - $pdf->y
);
295 $pdf->setColor(0,0,0);
299 $pdf->ez
['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
300 $pdf->ez
['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
302 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
306 function multiprint_action($id = "") {
307 $_POST['process'] = "true";
309 $this->function_argument_error();
311 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
312 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
313 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
314 ,$GLOBALS['oer_config']['prescriptions']['bottom']
315 ,$GLOBALS['oer_config']['prescriptions']['left']
316 ,$GLOBALS['oer_config']['prescriptions']['right']
318 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
320 // $print_header = true;
323 //print prescriptions body
324 $this->_state
= false; // Added by Rod - see Controller.class.php
325 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY
);
326 foreach ($ids as $id) {
327 $p = new Prescription($id);
328 // if ($print_header == true) {
329 if ($on_this_page == 0) {
330 $this->multiprint_header($pdf, $p);
332 if (++
$on_this_page > 3 ||
$p->provider
->id
!= $this->providerid
) {
333 $this->multiprint_footer($pdf);
335 $this->multiprint_header($pdf, $p);
336 // $print_header = false;
339 $this->multiprint_body($pdf, $p);
342 $this->multiprint_footer($pdf);
348 function send_action_process($id) {
349 $dummy = ""; // Added by Rod to avoid run-time warnings
350 if ($_POST['process'] != "true")
353 $this->function_argument_error();
355 $p = new Prescription($id);
356 switch ($_POST['submit']) {
359 // The following statement added by Rod.
360 // Looking at Controller.class.php, it appears that _state is set to false
361 // to indicate that no further HTML is to be generated.
362 $this->_state
= false; // Added by Rod - see Controller.class.php
363 return $this->_print_prescription($p, $dummy);
366 return $this->_email_prescription($p,$_POST['email_to']);
369 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
370 //$this->assign("process_result","No fax server is currently setup.");
371 return $this->_fax_prescription($p,$_POST['fax_to']);
374 $pharmacy_id = $_POST['pharmacy_id'];
375 //echo "auto sending to : " . $_POST['pharmacy_id'];
376 $phar = new Pharmacy($_POST['pharmacy_id']);
378 if ($phar->get_transmit_method() == TRANSMIT_PRINT
) {
379 return $this->_print_prescription($p, $dummy);
381 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL
) {
382 $email = $phar->get_email();
383 if (!empty($email)) {
384 return $this->_email_prescription($p,$phar->get_email());
388 elseif ($phar->get_transmit_method() == TRANSMIT_FAX
) {
389 $faxNum= $phar->get_fax();
390 if(!empty($faxNum)) {
391 Return $this->_fax_prescription ($p,$faxNum);
393 // return $this->assign("process_result","No fax server is currently setup.");
394 // else default is printing,
397 //the pharmacy has no default or default is print
398 return $this->_print_prescription($p, $dummy);
407 function _print_prescription($p, & $toFile) {
408 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
409 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
410 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
411 ,$GLOBALS['oer_config']['prescriptions']['bottom']
412 ,$GLOBALS['oer_config']['prescriptions']['left']
413 ,$GLOBALS['oer_config']['prescriptions']['right']
416 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
418 // Signature images are to be used only when faxing.
419 if(!empty($toFile)) $this->is_faxing
= true;
421 $this->multiprint_header($pdf, $p);
422 $this->multiprint_body($pdf, $p);
423 $this->multiprint_footer($pdf);
425 if(!empty($toFile)) {
426 $toFile = $pdf->ezOutput();
430 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
435 function _print_prescription_old($p, & $toFile) {
436 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
437 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
438 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
439 ,$GLOBALS['oer_config']['prescriptions']['bottom']
440 ,$GLOBALS['oer_config']['prescriptions']['left']
441 ,$GLOBALS['oer_config']['prescriptions']['right']
443 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
444 if(!empty($this->pconfig
['logo'])) {
445 $pdf->ezImage($this->pconfig
['logo'],"","","none","left");
447 $pdf->ezText($p->get_prescription_display(),10);
448 if($this->pconfig
['use_signature']) {
449 $pdf->ezImage($this->pconfig
['signature'],"","","none","left");
452 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
456 $toFile = $pdf->ezOutput();
461 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
466 function _email_prescription($p,$email) {
468 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
471 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
472 $mail = new PHPMailer();
473 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
474 //this is a temporary config item until the rest of the per practice billing settings make their way in
475 $mail->From
= $GLOBALS['practice_return_email_path'];
476 $mail->FromName
= $p->provider
->get_name_display();
478 $mail->Host
= "localhost";
479 $mail->Mailer
= "mail";
480 $text_body = $p->get_prescription_display();
481 $mail->Body
= $text_body;
482 $mail->Subject
= "Prescription for: " . $p->patient
->get_name_display();
483 $mail->AddAddress($email);
485 $this->assign("process_result","Email was successfully sent to: " . $email);
489 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo
);
494 function do_lookup() {
495 if ($_POST['process'] != "true")
498 if (!empty($_POST['drug'])) {
499 $list = @RxList
::get_list($_POST['drug']);
501 if (is_array($list)) {
502 $list = array_flip($list);
503 $this->assign("drug_options",$list);
504 $this->assign("drug_values",array_keys($list));
507 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
510 //$this->assign("PROCESS","");
512 $_POST['process'] = "";
515 function _fax_prescription($p,$faxNum)
518 //strip - ,(, ), and ws
519 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
520 //validate the number
522 if(!empty($faxNum) && is_numeric($faxNum))
524 //get the sendfax command and execute it
525 $cmd = $this->pconfig
['sendfax'];
526 // prepend any prefix to the fax number
527 $pref=$this->pconfig
['prefix'];
528 $faxNum=$pref.$faxNum;
531 $err .= " Send fax not set in includes/config.php";
536 //generate file to fax
538 $this->_print_prescription($p, $faxFile);
541 $err .= " _print_prescription returned empty file";
544 $fileName = dirname(__FILE__
)."/../documents/".$p->get_id()
545 .$p->get_patient_id()."_fax_.pdf";
546 //print "filename is $fileName";
547 touch($fileName); // php bug
548 $handle = fopen($fileName,"w");
551 $err .= " Failed to open file $fileName to write fax to";
554 if(fwrite($handle, $faxFile) === false)
556 $err .= " Failed to write data to $fileName";
560 $args = " -n -d $faxNum $fileName";
561 //print "command is $cmd $args<br>";
568 $err = "bad fax number passed to function";
572 $this->assign("process_result",$err);