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 function C_Prescription($template_mod = "general") {
16 $this->template_mod
= $template_mod;
17 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
18 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
19 $this->assign("STYLE", $GLOBALS['style']);
20 $this->assign("WEIGHT_LOSS_CLINIC", $GLOBALS['weight_loss_clinic']);
21 $this->pconfig
= $GLOBALS['oer_config']['prescriptions'];
23 if ($GLOBALS['inhouse_pharmacy']) {
24 // Make an array of drug IDs and selectors for the template.
25 $drug_array_values = array(0);
26 $drug_array_output = array("-- or select from inventory --");
27 $drug_attributes = '';
29 // $res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
31 $res = sqlStatement("SELECT d.name, d.ndc_number, d.form, d.size, " .
32 "d.unit, d.route, d.substitute, t.drug_id, t.selector, t.dosage, " .
33 "t.period, t.quantity, t.refills " .
34 "FROM drug_templates AS t, drugs AS d WHERE " .
35 "d.drug_id = t.drug_id ORDER BY t.selector");
37 while ($row = sqlFetchArray($res)) {
38 $tmp_output = $row['selector'];
39 if ($row['ndc_number']) {
40 $tmp_output .= ' [' . $row['ndc_number'] . ']';
42 $drug_array_values[] = $row['drug_id'];
43 $drug_array_output[] = $tmp_output;
44 if ($drug_attributes) $drug_attributes .= ',';
45 $drug_attributes .= "['" .
46 $row['name'] . "'," . // 0
47 $row['form'] . ",'" . // 1
48 $row['dosage'] . "'," . // 2
49 $row['size'] . "," . // 3
50 $row['unit'] . "," . // 4
51 $row['route'] . "," . // 5
52 $row['period'] . "," . // 6
53 $row['substitute'] . "," . // 7
54 $row['quantity'] . "," . // 8
55 $row['refills'] . "," . // 9
56 $row['quantity'] . "]"; // 10 quantity per_refill
58 $this->assign("DRUG_ARRAY_VALUES", $drug_array_values);
59 $this->assign("DRUG_ARRAY_OUTPUT", $drug_array_output);
60 $this->assign("DRUG_ATTRIBUTES", $drug_attributes);
64 function default_action() {
65 $this->assign("prescription",$this->prescriptions
[0]);
66 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_edit.html");
69 function edit_action($id = "",$patient_id="",$p_obj = null) {
71 if ($p_obj != null && get_class($p_obj) == "prescription") {
72 $this->prescriptions
[0] = $p_obj;
74 elseif (get_class($this->prescriptions
[0]) != "prescription" ) {
75 $this->prescriptions
[0] = new Prescription($id);
78 if (!empty($patient_id)) {
79 $this->prescriptions
[0]->set_patient_id($patient_id);
81 $this->default_action();
84 function list_action($id,$sort = "") {
86 $this->function_argument_error();
90 $this->assign("prescriptions", Prescription
::prescriptions_factory($id,$sort));
93 $this->assign("prescriptions", Prescription
::prescriptions_factory($id));
95 //print_r(Prescription::prescriptions_factory($id));
96 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_list.html");
99 function block_action($id,$sort = "") {
101 $this->function_argument_error();
105 $this->assign("prescriptions", Prescription
::prescriptions_factory($id,$sort));
108 $this->assign("prescriptions", Prescription
::prescriptions_factory($id));
110 //print_r(Prescription::prescriptions_factory($id));
111 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_block.html");
114 function lookup_action() {
116 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_lookup.html");
119 function edit_action_process() {
120 if ($_POST['process'] != "true")
124 $this->prescriptions
[0] = new Prescription($_POST['id']);
125 parent
::populate_object($this->prescriptions
[0]);
126 //echo $this->prescriptions[0]->toString(true);
127 $this->prescriptions
[0]->persist();
128 $_POST['process'] = "";
130 // If the "Prescribe and Dispense" button was clicked, then
131 // redisplay as in edit_action() but also replicate the fee and
132 // include a piece of javascript to call dispense().
134 if ($_POST['disp_button']) {
135 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
136 $this->assign("DISP_FEE", $_POST['disp_fee']);
137 $this->assign("ENDING_JAVASCRIPT", "dispense();");
138 $this->_state
= false;
139 return $this->edit_action($this->prescriptions
[0]->id
);
142 return $this->send_action($this->prescriptions
[0]->id
);
145 function send_action($id) {
146 $_POST['process'] = "true";
148 $this->function_argument_error();
151 $rx = new Prescription($id);
152 // Populate pharmacy info if the patient has a default pharmacy.
153 // Probably the Prescription object should handle this instead, but
154 // doing it there will require more careful research and testing.
155 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
156 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
157 if ($prow['pharmacy_id']) {
158 $rx->pharmacy
->set_id($prow['pharmacy_id']);
159 $rx->pharmacy
->populate();
161 $this->assign("prescription", $rx);
163 $this->_state
= false;
164 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
165 $this->template_mod
. "_send.html");
168 function multiprint_header(& $pdf, $p) {
170 $pdf->ezImage($GLOBALS['fileroot'] . '/interface/pic/Rx.png','','50','','center','');
171 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
172 $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 ='"
173 . mysql_real_escape_string($p->provider
->id
) . "'");
175 $pdf->ezText($res['addr'],12);
178 $pdf->ezText('<b>' . $p->provider
->get_name_display() . '</b>',12);
179 $pdf->ezText('<b>DEA:</b>' . $p->provider
->federal_drug_id
,12);
180 $pdf->ezColumnsStop();
181 if ($my_y < $pdf->y
){
185 $pdf->setLineStyle(1);
186 $pdf->ezColumnsStart(array('num'=>2));
187 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
188 $pdf->ezText('<b>Patient Name & Address</b>',6);
189 $pdf->ezText($p->patient
->get_name_display(),10);
190 $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
));
191 $pdf->ezText($res['addr']);
194 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
195 $pdf->ezText('<b>Date of Birth</b>',6);
196 $pdf->ezText($p->patient
->date_of_birth
,10);
198 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
199 $pdf->ezText('<b>Medical Record #</b>',6);
200 $pdf->ezText(str_pad($p->patient
->get_id(), 10, "0", STR_PAD_LEFT
),10);
201 $pdf->ezColumnsStop();
202 if ($my_y < $pdf->y
){
206 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
208 $pdf->ezText('<b>Prescriptions</b>',6);
212 function multiprint_footer(& $pdf){
213 if($this->pconfig
['use_signature'] == true ) {
214 $pdf->ezImage($this->pconfig
['signature'],"","","none","left");
217 $pdf->ezText("\n\n\n\nSignature:________________________________\nDate: " . date('Y-m-d'),12);
221 function get_prescription_body_text($p) {
222 $body = '<b>Rx: ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display()
223 . ' [' . $p->form_array
[$p->get_form()] . "]</b> <i>"
224 . $p->substitute_array
[$p->get_substitute()] . "</i>\n"
225 . '<b>Disp #:</b> <u>' . $p->get_quantity() . "</u>\n"
226 . '<b>Sig:</b> ' . $p->get_dosage() . ' ' . $p->form_array
[$p->get_form()] .' '
227 . $p->route_array
[$p->get_route()] . ' ' . $p->interval_array
[$p->get_interval()] . "\n";
228 if ($p->get_refills() > 0) {
229 $body .= "\n<b>Refills:</b> <u>" . $p->get_refills() . " of quantity " . $p->get_per_refill() . "</u>\n";
232 $body .= "\n<b>Refills:</b> <u>0 (Zero)</u>\n";
234 $note = $p->get_note();
236 $body .= "\n$note\n";
241 function multiprint_body(& $pdf, $p){
242 $pdf->ez
['leftMargin'] +
= $pdf->ez
['leftMargin'];
243 $pdf->ez
['rightMargin'] +
= $pdf->ez
['rightMargin'];
244 $d = $this->get_prescription_body_text($p);
246 if ( $pdf->ezText($d,10,array(),1) ) {
247 $pdf->ez
['leftMargin'] -= $pdf->ez
['leftMargin'];
248 $pdf->ez
['rightMargin'] -= $pdf->ez
['rightMargin'];
249 $this->multiprint_footer($pdf, $p);
251 $this->multiprint_header($pdf, $p);
252 $pdf->ez
['leftMargin'] +
= $pdf->ez
['leftMargin'];
253 $pdf->ez
['rightMargin'] +
= $pdf->ez
['rightMargin'];
257 $pdf->setColor(.9,.9,.9);
258 $pdf->filledRectangle($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin']-$pdf->ez
['leftMargin'],$my_y - $pdf->y
);
259 $pdf->setColor(0,0,0);
262 $pdf->ez
['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
263 $pdf->ez
['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
265 $pdf->line($pdf->ez
['leftMargin'],$pdf->y
,$pdf->ez
['pageWidth']-$pdf->ez
['rightMargin'],$pdf->y
);
270 function multiprint_action($id = "") {
271 $_POST['process'] = "true";
273 $this->function_argument_error();
275 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
276 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
277 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
278 ,$GLOBALS['oer_config']['prescriptions']['bottom']
279 ,$GLOBALS['oer_config']['prescriptions']['left']
280 ,$GLOBALS['oer_config']['prescriptions']['right']
282 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
285 $print_header = true;
287 //print prescriptions body
288 $this->_state
= false; // Added by Rod - see Controller.class.php
289 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY
);
290 foreach ($ids as $id) {
291 $p = new Prescription($id);
292 if ($print_header == true) {
293 $this->multiprint_header($pdf, $p);
294 $print_header = false;
296 $this->multiprint_body($pdf, $p);
299 $this->multiprint_footer($pdf);
305 function send_action_process($id) {
306 $dummy = ""; // Added by Rod to avoid run-time warnings
307 if ($_POST['process'] != "true")
310 $this->function_argument_error();
312 $p = new Prescription($id);
313 switch ($_POST['submit']) {
316 // The following statement added by Rod.
317 // Looking at Controller.class.php, it appears that _state is set to false
318 // to indicate that no further HTML is to be generated.
319 $this->_state
= false; // Added by Rod - see Controller.class.php
320 return $this->_print_prescription($p, $dummy);
323 return $this->_email_prescription($p,$_POST['email_to']);
326 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
327 //$this->assign("process_result","No fax server is currently setup.");
328 return $this->_fax_prescription($p,$_POST['fax_to']);
331 $pharmacy_id = $_POST['pharmacy_id'];
332 //echo "auto sending to : " . $_POST['pharmacy_id'];
333 $phar = new Pharmacy($_POST['pharmacy_id']);
335 if ($phar->get_transmit_method() == TRANSMIT_PRINT
) {
336 return $this->_print_prescription($p, $dummy);
338 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL
) {
339 $email = $phar->get_email();
340 if (!empty($email)) {
341 return $this->_email_prescription($p,$phar->get_email());
345 elseif ($phar->get_transmit_method() == TRANSMIT_FAX
) {
346 $faxNum= $phar->get_fax();
347 if(!empty($faxNum)) {
348 Return $this->_fax_prescription ($p,$faxNum);
350 // return $this->assign("process_result","No fax server is currently setup.");
351 // else default is printing,
354 //the pharmacy has no default or default is print
355 return $this->_print_prescription($p, $dummy);
364 function _print_prescription($p, & $toFile) {
366 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
367 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
368 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
369 ,$GLOBALS['oer_config']['prescriptions']['bottom']
370 ,$GLOBALS['oer_config']['prescriptions']['left']
371 ,$GLOBALS['oer_config']['prescriptions']['right']
374 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
376 $this->multiprint_header($pdf, $p);
378 $this->multiprint_body($pdf, $p);
380 $this->multiprint_footer($pdf);
384 $toFile = $pdf->ezOutput();
389 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
394 function _print_prescription_old($p, & $toFile) {
396 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
397 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
398 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
399 ,$GLOBALS['oer_config']['prescriptions']['bottom']
400 ,$GLOBALS['oer_config']['prescriptions']['left']
401 ,$GLOBALS['oer_config']['prescriptions']['right']
404 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
406 if(!empty($this->pconfig
['logo'])) {
407 $pdf->ezImage($this->pconfig
['logo'],"","","none","left");
409 $pdf->ezText($p->get_prescription_display(),10);
410 if($this->pconfig
['use_signature'] == true ) {
411 $pdf->ezImage($this->pconfig
['signature'],"","","none","left");
414 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
419 $toFile = $pdf->ezOutput();
424 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
429 function _email_prescription($p,$email) {
431 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
434 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
435 $mail = new PHPMailer();
436 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
437 //this is a temporary config item until the rest of the per practice billing settings make their way in
438 $mail->From
= $GLOBALS['practice_return_email_path'];
439 $mail->FromName
= $p->provider
->get_name_display();
441 $mail->Host
= "localhost";
442 $mail->Mailer
= "mail";
443 $text_body = $p->get_prescription_display();
444 $mail->Body
= $text_body;
445 $mail->Subject
= "Prescription for: " . $p->patient
->get_name_display();
446 $mail->AddAddress($email);
448 $this->assign("process_result","Email was successfully sent to: " . $email);
452 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo
);
457 function do_lookup() {
458 if ($_POST['process'] != "true")
461 if (!empty($_POST['drug'])) {
462 $list = @RxList
::get_list($_POST['drug']);
464 if (is_array($list)) {
465 $list = array_flip($list);
466 $this->assign("drug_options",$list);
467 $this->assign("drug_values",array_keys($list));
470 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
473 //$this->assign("PROCESS","");
475 $_POST['process'] = "";
478 function _fax_prescription($p,$faxNum)
481 //strip - ,(, ), and ws
482 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
483 //validate the number
485 if(!empty($faxNum) && is_numeric($faxNum))
487 //get the sendfax command and execute it
488 $cmd = $this->pconfig
['sendfax'];
489 // prepend any prefix to the fax number
490 $pref=$this->pconfig
['prefix'];
491 $faxNum=$pref.$faxNum;
494 $err .= " Send fax not set in includes/config.php";
499 //generate file to fax
501 $this->_print_prescription($p, $faxFile);
504 $err .= " _print_prescription returned empty file";
507 $fileName = dirname(__FILE__
)."/../documents/".$p->get_id()
508 .$p->get_patient_id()."_fax_.pdf";
509 //print "filename is $fileName";
510 touch($fileName); // php bug
511 $handle = fopen($fileName,"w");
514 $err .= " Failed to open file $fileName to write fax to";
517 if(fwrite($handle, $faxFile) === false)
519 $err .= " Failed to write data to $fileName";
523 $args = " -n -d $faxNum $fileName";
524 //print "command is $cmd $args<br>";
531 $err = "bad fax number passed to function";
535 $this->assign("process_result",$err);