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 $use_signature_images = 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 $pdf->ezText('<b>DEA:</b>' . $p->provider
->federal_drug_id
,12);
198 $pdf->ezColumnsStop();
199 if ($my_y < $pdf->y
){
203 $pdf->setLineStyle(1);
204 $pdf->ezColumnsStart(array('num'=>2));
205 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
206 $pdf->ezText('<b>Patient Name & Address</b>',6);
207 $pdf->ezText($p->patient
->get_name_display(),10);
208 $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
));
209 $pdf->ezText($res['addr']);
212 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
213 $pdf->ezText('<b>Date of Birth</b>',6);
214 $pdf->ezText($p->patient
->date_of_birth
,10);
216 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
217 $pdf->ezText('<b>Medical Record #</b>',6);
218 $pdf->ezText(str_pad($p->patient
->get_id(), 10, "0", STR_PAD_LEFT
),10);
219 $pdf->ezColumnsStop();
220 if ($my_y < $pdf->y
){
224 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
225 $pdf->ezText('<b>Prescriptions</b>',6);
229 function multiprint_footer(& $pdf) {
230 if($this->pconfig
['use_signature'] && $this->use_signature_images
) {
231 $sigfile = str_replace('{userid}', $this->providerid
, $this->pconfig
['signature']);
232 if (file_exists($sigfile)) {
233 $pdf->ezText("Signature: ",12);
234 // $pdf->ezImage($sigfile, "", "", "none", "left");
235 $pdf->ezImage($sigfile, "", "", "none", "center");
236 $pdf->ezText("Date: " . date('Y-m-d'), 12);
240 $pdf->ezText("\n\n\n\nSignature:________________________________\nDate: " . date('Y-m-d'),12);
243 function get_prescription_body_text($p) {
244 $body = '<b>Rx: ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display();
245 if ($p->get_form()) $body .= ' [' . $p->form_array
[$p->get_form()] . "]";
246 $body .= "</b> <i>" .
247 $p->substitute_array
[$p->get_substitute()] . "</i>\n" .
248 '<b>Disp #:</b> <u>' . $p->get_quantity() . "</u>\n" .
249 '<b>Sig:</b> ' . $p->get_dosage() . ' ' . $p->form_array
[$p->get_form()] . ' ' .
250 $p->route_array
[$p->get_route()] . ' ' . $p->interval_array
[$p->get_interval()] . "\n";
251 if ($p->get_refills() > 0) {
252 $body .= "\n<b>Refills:</b> <u>" . $p->get_refills();
253 if ($p->get_per_refill()) {
254 $body .= " of quantity " . $p->get_per_refill();
259 $body .= "\n<b>Refills:</b> <u>0 (Zero)</u>\n";
261 $note = $p->get_note();
263 $body .= "\n$note\n";
268 function multiprint_body(& $pdf, $p){
269 $pdf->ez
['leftMargin'] +
= $pdf->ez
['leftMargin'];
270 $pdf->ez
['rightMargin'] +
= $pdf->ez
['rightMargin'];
271 $d = $this->get_prescription_body_text($p);
272 if ( $pdf->ezText($d,10,array(),1) ) {
273 $pdf->ez
['leftMargin'] -= $pdf->ez
['leftMargin'];
274 $pdf->ez
['rightMargin'] -= $pdf->ez
['rightMargin'];
275 $this->multiprint_footer($pdf);
277 $this->multiprint_header($pdf, $p);
278 $pdf->ez
['leftMargin'] +
= $pdf->ez
['leftMargin'];
279 $pdf->ez
['rightMargin'] +
= $pdf->ez
['rightMargin'];
283 if($this->pconfig
['shading']) {
284 $pdf->setColor(.9,.9,.9);
285 $pdf->filledRectangle($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin']-$pdf->ez
['leftMargin'],$my_y - $pdf->y
);
286 $pdf->setColor(0,0,0);
290 $pdf->ez
['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
291 $pdf->ez
['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
293 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
297 function multiprint_action($id = "") {
298 $_POST['process'] = "true";
300 $this->function_argument_error();
302 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
303 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
304 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
305 ,$GLOBALS['oer_config']['prescriptions']['bottom']
306 ,$GLOBALS['oer_config']['prescriptions']['left']
307 ,$GLOBALS['oer_config']['prescriptions']['right']
309 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
311 // $print_header = true;
314 //print prescriptions body
315 $this->_state
= false; // Added by Rod - see Controller.class.php
316 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY
);
317 foreach ($ids as $id) {
318 $p = new Prescription($id);
319 // if ($print_header == true) {
320 if ($on_this_page == 0) {
321 $this->multiprint_header($pdf, $p);
323 if (++
$on_this_page > 3 ||
$p->provider
->id
!= $this->providerid
) {
324 $this->multiprint_footer($pdf);
326 $this->multiprint_header($pdf, $p);
327 // $print_header = false;
330 $this->multiprint_body($pdf, $p);
333 $this->multiprint_footer($pdf);
339 function send_action_process($id) {
340 $dummy = ""; // Added by Rod to avoid run-time warnings
341 if ($_POST['process'] != "true")
344 $this->function_argument_error();
346 $p = new Prescription($id);
347 switch ($_POST['submit']) {
350 // The following statement added by Rod.
351 // Looking at Controller.class.php, it appears that _state is set to false
352 // to indicate that no further HTML is to be generated.
353 $this->_state
= false; // Added by Rod - see Controller.class.php
354 return $this->_print_prescription($p, $dummy);
357 return $this->_email_prescription($p,$_POST['email_to']);
360 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
361 //$this->assign("process_result","No fax server is currently setup.");
362 return $this->_fax_prescription($p,$_POST['fax_to']);
365 $pharmacy_id = $_POST['pharmacy_id'];
366 //echo "auto sending to : " . $_POST['pharmacy_id'];
367 $phar = new Pharmacy($_POST['pharmacy_id']);
369 if ($phar->get_transmit_method() == TRANSMIT_PRINT
) {
370 return $this->_print_prescription($p, $dummy);
372 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL
) {
373 $email = $phar->get_email();
374 if (!empty($email)) {
375 return $this->_email_prescription($p,$phar->get_email());
379 elseif ($phar->get_transmit_method() == TRANSMIT_FAX
) {
380 $faxNum= $phar->get_fax();
381 if(!empty($faxNum)) {
382 Return $this->_fax_prescription ($p,$faxNum);
384 // return $this->assign("process_result","No fax server is currently setup.");
385 // else default is printing,
388 //the pharmacy has no default or default is print
389 return $this->_print_prescription($p, $dummy);
398 function _print_prescription($p, & $toFile) {
399 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
400 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
401 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
402 ,$GLOBALS['oer_config']['prescriptions']['bottom']
403 ,$GLOBALS['oer_config']['prescriptions']['left']
404 ,$GLOBALS['oer_config']['prescriptions']['right']
407 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
409 // Signature images are to be used only when faxing.
410 if(!empty($toFile)) $this->use_signature_images
= true;
412 $this->multiprint_header($pdf, $p);
413 $this->multiprint_body($pdf, $p);
414 $this->multiprint_footer($pdf);
416 if(!empty($toFile)) {
417 $toFile = $pdf->ezOutput();
421 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
426 function _print_prescription_old($p, & $toFile) {
427 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
428 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
429 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
430 ,$GLOBALS['oer_config']['prescriptions']['bottom']
431 ,$GLOBALS['oer_config']['prescriptions']['left']
432 ,$GLOBALS['oer_config']['prescriptions']['right']
434 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
435 if(!empty($this->pconfig
['logo'])) {
436 $pdf->ezImage($this->pconfig
['logo'],"","","none","left");
438 $pdf->ezText($p->get_prescription_display(),10);
439 if($this->pconfig
['use_signature']) {
440 $pdf->ezImage($this->pconfig
['signature'],"","","none","left");
443 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
447 $toFile = $pdf->ezOutput();
452 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
457 function _email_prescription($p,$email) {
459 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
462 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
463 $mail = new PHPMailer();
464 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
465 //this is a temporary config item until the rest of the per practice billing settings make their way in
466 $mail->From
= $GLOBALS['practice_return_email_path'];
467 $mail->FromName
= $p->provider
->get_name_display();
469 $mail->Host
= "localhost";
470 $mail->Mailer
= "mail";
471 $text_body = $p->get_prescription_display();
472 $mail->Body
= $text_body;
473 $mail->Subject
= "Prescription for: " . $p->patient
->get_name_display();
474 $mail->AddAddress($email);
476 $this->assign("process_result","Email was successfully sent to: " . $email);
480 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo
);
485 function do_lookup() {
486 if ($_POST['process'] != "true")
489 if (!empty($_POST['drug'])) {
490 $list = @RxList
::get_list($_POST['drug']);
492 if (is_array($list)) {
493 $list = array_flip($list);
494 $this->assign("drug_options",$list);
495 $this->assign("drug_values",array_keys($list));
498 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
501 //$this->assign("PROCESS","");
503 $_POST['process'] = "";
506 function _fax_prescription($p,$faxNum)
509 //strip - ,(, ), and ws
510 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
511 //validate the number
513 if(!empty($faxNum) && is_numeric($faxNum))
515 //get the sendfax command and execute it
516 $cmd = $this->pconfig
['sendfax'];
517 // prepend any prefix to the fax number
518 $pref=$this->pconfig
['prefix'];
519 $faxNum=$pref.$faxNum;
522 $err .= " Send fax not set in includes/config.php";
527 //generate file to fax
529 $this->_print_prescription($p, $faxFile);
532 $err .= " _print_prescription returned empty file";
535 $fileName = dirname(__FILE__
)."/../documents/".$p->get_id()
536 .$p->get_patient_id()."_fax_.pdf";
537 //print "filename is $fileName";
538 touch($fileName); // php bug
539 $handle = fopen($fileName,"w");
542 $err .= " Failed to open file $fileName to write fax to";
545 if(fwrite($handle, $faxFile) === false)
547 $err .= " Failed to write data to $fileName";
551 $args = " -n -d $faxNum $fileName";
552 //print "command is $cmd $args<br>";
559 $err = "bad fax number passed to function";
563 $this->assign("process_result",$err);