added drug templates
[openemr.git] / controllers / C_Prescription.class.php
blob9d83c946ce2b74d415457d6fac96457c2c863e76
1 <?php
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 {
10 var $template_mod;
11 var $pconfig;
13 function C_Prescription($template_mod = "general") {
14 parent::Controller();
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 = "") {
85 if (empty($id)) {
86 $this->function_argument_error();
87 exit;
89 if (!empty($sort)) {
90 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
92 else {
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 = "") {
100 if (empty($id)) {
101 $this->function_argument_error();
102 exit;
104 if (!empty($sort)) {
105 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
107 else {
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() {
115 $this->do_lookup();
116 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
119 function edit_action_process() {
120 if ($_POST['process'] != "true")
121 return;
122 //print_r($_POST);
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";
147 if(empty($id)) {
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) {
169 //print header
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);
176 $my_y = $pdf->y;
177 $pdf->ezNewPage();
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){
182 $pdf->ezSetY($my_y);
184 $pdf->ezText('',10);
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']);
192 $my_y = $pdf->y;
193 $pdf->ezNewPage();
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);
197 $pdf->ezText('');
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){
203 $pdf->ezSetY($my_y);
205 $pdf->ezText('');
206 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
208 $pdf->ezText('<b>Prescriptions</b>',6);
209 $pdf->ezText('',10);
212 function multiprint_footer(& $pdf){
213 if($this->pconfig['use_signature'] == true ) {
214 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
216 else{
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";
231 else {
232 $body .= "\n<b>Refills:</b> <u>0 (Zero)</u>\n";
234 $note = $p->get_note();
235 if ($note != '') {
236 $body .= "\n$note\n";
238 return $body;
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);
250 $pdf->ezNewPage();
251 $this->multiprint_header($pdf, $p);
252 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
253 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
255 $my_y = $pdf->y;
256 $pdf->ezText($d,10);
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);
260 $pdf->ezSetY($my_y);
261 $pdf->ezText($d,10);
262 $pdf->ez['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
263 $pdf->ez['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
264 $pdf->ezText('');
265 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
266 $pdf->ezText('');
270 function multiprint_action($id = "") {
271 $_POST['process'] = "true";
272 if(empty($id)) {
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);
301 $pdf->ezStream();
302 return;
305 function send_action_process($id) {
306 $dummy = ""; // Added by Rod to avoid run-time warnings
307 if ($_POST['process'] != "true")
308 return;
309 if(empty($id)) {
310 $this->function_argument_error();
312 $p = new Prescription($id);
313 switch ($_POST['submit']) {
315 case "Print":
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);
321 break;
322 case "Email":
323 return $this->_email_prescription($p,$_POST['email_to']);
324 break;
325 case "Fax":
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']);
329 break;
330 case "Auto Send":
331 $pharmacy_id = $_POST['pharmacy_id'];
332 //echo "auto sending to : " . $_POST['pharmacy_id'];
333 $phar = new Pharmacy($_POST['pharmacy_id']);
334 //print_r($phar);
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());
343 //else print it
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,
353 else {
354 //the pharmacy has no default or default is print
355 return $this->_print_prescription($p, $dummy);
357 break;
360 return;
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);
382 if(!empty($toFile))
384 $toFile = $pdf->ezOutput();
386 else
388 $pdf->ezStream();
389 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
391 return;
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");
413 else{
414 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
417 if(!empty($toFile))
419 $toFile = $pdf->ezOutput();
421 else
423 $pdf->ezStream();
424 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
426 return;
429 function _email_prescription($p,$email) {
430 if (empty($email)) {
431 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
432 return;
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();
440 $mail->isMail();
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);
447 if($mail->Send()) {
448 $this->assign("process_result","Email was successfully sent to: " . $email);
449 return;
451 else {
452 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
453 return;
457 function do_lookup() {
458 if ($_POST['process'] != "true")
459 return;
460 $list = array();
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));
469 else {
470 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
472 //print_r($_POST);
473 //$this->assign("PROCESS","");
475 $_POST['process'] = "";
478 function _fax_prescription($p,$faxNum)
480 $err = "Sent fax";
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;
492 if(empty($cmd))
494 $err .= " Send fax not set in includes/config.php";
495 break;
497 else
499 //generate file to fax
500 $faxFile = "Failed";
501 $this->_print_prescription($p, $faxFile);
502 if(empty($faxFile))
504 $err .= " _print_prescription returned empty file";
505 break;
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");
512 if(!$handle)
514 $err .= " Failed to open file $fileName to write fax to";
515 break;
517 if(fwrite($handle, $faxFile) === false)
519 $err .= " Failed to write data to $fileName";
520 break;
522 fclose($handle);
523 $args = " -n -d $faxNum $fileName";
524 //print "command is $cmd $args<br>";
525 exec($cmd . $args);
529 else
531 $err = "bad fax number passed to function";
533 if($err)
535 $this->assign("process_result",$err);