Zirmed claim generating script.
[openemr.git] / controllers / C_Prescription.class.php
blobdbd02c27f56dc51806d4b8a5c67e7cc57c8b2586
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);
82 // If quantity to dispense is not already set from a POST, set its
83 // default value.
84 if (! $this->get_template_vars('DISP_QUANTITY')) {
85 $this->assign('DISP_QUANTITY', $this->prescriptions[0]->quantity);
88 $this->default_action();
91 function list_action($id,$sort = "") {
92 if (empty($id)) {
93 $this->function_argument_error();
94 exit;
96 if (!empty($sort)) {
97 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
99 else {
100 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
102 //print_r(Prescription::prescriptions_factory($id));
103 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
106 function block_action($id,$sort = "") {
107 if (empty($id)) {
108 $this->function_argument_error();
109 exit;
111 if (!empty($sort)) {
112 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
114 else {
115 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
117 //print_r(Prescription::prescriptions_factory($id));
118 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
121 function lookup_action() {
122 $this->do_lookup();
123 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
126 function edit_action_process() {
127 if ($_POST['process'] != "true")
128 return;
129 //print_r($_POST);
131 $this->prescriptions[0] = new Prescription($_POST['id']);
132 parent::populate_object($this->prescriptions[0]);
133 //echo $this->prescriptions[0]->toString(true);
134 $this->prescriptions[0]->persist();
135 $_POST['process'] = "";
137 // If the "Prescribe and Dispense" button was clicked, then
138 // redisplay as in edit_action() but also replicate the fee and
139 // include a piece of javascript to call dispense().
141 if ($_POST['disp_button']) {
142 $this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
143 $this->assign("DISP_FEE", $_POST['disp_fee']);
144 $this->assign("ENDING_JAVASCRIPT", "dispense();");
145 $this->_state = false;
146 return $this->edit_action($this->prescriptions[0]->id);
149 return $this->send_action($this->prescriptions[0]->id);
152 function send_action($id) {
153 $_POST['process'] = "true";
154 if(empty($id)) {
155 $this->function_argument_error();
158 $rx = new Prescription($id);
159 // Populate pharmacy info if the patient has a default pharmacy.
160 // Probably the Prescription object should handle this instead, but
161 // doing it there will require more careful research and testing.
162 $prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
163 "patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
164 if ($prow['pharmacy_id']) {
165 $rx->pharmacy->set_id($prow['pharmacy_id']);
166 $rx->pharmacy->populate();
168 $this->assign("prescription", $rx);
170 $this->_state = false;
171 return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
172 $this->template_mod . "_send.html");
175 function multiprint_header(& $pdf, $p) {
176 //print header
177 $pdf->ezImage($GLOBALS['fileroot'] . '/interface/pic/Rx.png','','50','','center','');
178 $pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
179 $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 ='"
180 . mysql_real_escape_string($p->provider->id) . "'");
182 $pdf->ezText($res['addr'],12);
183 $my_y = $pdf->y;
184 $pdf->ezNewPage();
185 $pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
186 $pdf->ezText('<b>DEA:</b>' . $p->provider->federal_drug_id,12);
187 $pdf->ezColumnsStop();
188 if ($my_y < $pdf->y){
189 $pdf->ezSetY($my_y);
191 $pdf->ezText('',10);
192 $pdf->setLineStyle(1);
193 $pdf->ezColumnsStart(array('num'=>2));
194 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
195 $pdf->ezText('<b>Patient Name & Address</b>',6);
196 $pdf->ezText($p->patient->get_name_display(),10);
197 $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));
198 $pdf->ezText($res['addr']);
199 $my_y = $pdf->y;
200 $pdf->ezNewPage();
201 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
202 $pdf->ezText('<b>Date of Birth</b>',6);
203 $pdf->ezText($p->patient->date_of_birth,10);
204 $pdf->ezText('');
205 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
206 $pdf->ezText('<b>Medical Record #</b>',6);
207 $pdf->ezText(str_pad($p->patient->get_id(), 10, "0", STR_PAD_LEFT),10);
208 $pdf->ezColumnsStop();
209 if ($my_y < $pdf->y){
210 $pdf->ezSetY($my_y);
212 $pdf->ezText('');
213 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
215 $pdf->ezText('<b>Prescriptions</b>',6);
216 $pdf->ezText('',10);
219 function multiprint_footer(& $pdf){
220 if($this->pconfig['use_signature'] == true ) {
221 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
223 else{
224 $pdf->ezText("\n\n\n\nSignature:________________________________\nDate: " . date('Y-m-d'),12);
228 function get_prescription_body_text($p) {
229 $body = '<b>Rx: ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display()
230 . ' [' . $p->form_array[$p->get_form()] . "]</b> <i>"
231 . $p->substitute_array[$p->get_substitute()] . "</i>\n"
232 . '<b>Disp #:</b> <u>' . $p->get_quantity() . "</u>\n"
233 . '<b>Sig:</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] .' '
234 . $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
235 if ($p->get_refills() > 0) {
236 $body .= "\n<b>Refills:</b> <u>" . $p->get_refills() . " of quantity " . $p->get_per_refill() . "</u>\n";
238 else {
239 $body .= "\n<b>Refills:</b> <u>0 (Zero)</u>\n";
241 $note = $p->get_note();
242 if ($note != '') {
243 $body .= "\n$note\n";
245 return $body;
248 function multiprint_body(& $pdf, $p){
249 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
250 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
251 $d = $this->get_prescription_body_text($p);
253 if ( $pdf->ezText($d,10,array(),1) ) {
254 $pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
255 $pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
256 $this->multiprint_footer($pdf, $p);
257 $pdf->ezNewPage();
258 $this->multiprint_header($pdf, $p);
259 $pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
260 $pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
262 $my_y = $pdf->y;
263 $pdf->ezText($d,10);
264 $pdf->setColor(.9,.9,.9);
265 $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
266 $pdf->setColor(0,0,0);
267 $pdf->ezSetY($my_y);
268 $pdf->ezText($d,10);
269 $pdf->ez['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
270 $pdf->ez['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
271 $pdf->ezText('');
272 $pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
273 $pdf->ezText('');
277 function multiprint_action($id = "") {
278 $_POST['process'] = "true";
279 if(empty($id)) {
280 $this->function_argument_error();
282 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
283 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
284 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
285 ,$GLOBALS['oer_config']['prescriptions']['bottom']
286 ,$GLOBALS['oer_config']['prescriptions']['left']
287 ,$GLOBALS['oer_config']['prescriptions']['right']
289 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
292 $print_header = true;
294 //print prescriptions body
295 $this->_state = false; // Added by Rod - see Controller.class.php
296 $ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
297 foreach ($ids as $id) {
298 $p = new Prescription($id);
299 if ($print_header == true) {
300 $this->multiprint_header($pdf, $p);
301 $print_header = false;
303 $this->multiprint_body($pdf, $p);
306 $this->multiprint_footer($pdf);
308 $pdf->ezStream();
309 return;
312 function send_action_process($id) {
313 $dummy = ""; // Added by Rod to avoid run-time warnings
314 if ($_POST['process'] != "true")
315 return;
316 if(empty($id)) {
317 $this->function_argument_error();
319 $p = new Prescription($id);
320 switch ($_POST['submit']) {
322 case "Print":
323 // The following statement added by Rod.
324 // Looking at Controller.class.php, it appears that _state is set to false
325 // to indicate that no further HTML is to be generated.
326 $this->_state = false; // Added by Rod - see Controller.class.php
327 return $this->_print_prescription($p, $dummy);
328 break;
329 case "Email":
330 return $this->_email_prescription($p,$_POST['email_to']);
331 break;
332 case "Fax":
333 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
334 //$this->assign("process_result","No fax server is currently setup.");
335 return $this->_fax_prescription($p,$_POST['fax_to']);
336 break;
337 case "Auto Send":
338 $pharmacy_id = $_POST['pharmacy_id'];
339 //echo "auto sending to : " . $_POST['pharmacy_id'];
340 $phar = new Pharmacy($_POST['pharmacy_id']);
341 //print_r($phar);
342 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
343 return $this->_print_prescription($p, $dummy);
345 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
346 $email = $phar->get_email();
347 if (!empty($email)) {
348 return $this->_email_prescription($p,$phar->get_email());
350 //else print it
352 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
353 $faxNum= $phar->get_fax();
354 if(!empty($faxNum)) {
355 Return $this->_fax_prescription ($p,$faxNum);
357 // return $this->assign("process_result","No fax server is currently setup.");
358 // else default is printing,
360 else {
361 //the pharmacy has no default or default is print
362 return $this->_print_prescription($p, $dummy);
364 break;
367 return;
371 function _print_prescription($p, & $toFile) {
373 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
374 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
375 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
376 ,$GLOBALS['oer_config']['prescriptions']['bottom']
377 ,$GLOBALS['oer_config']['prescriptions']['left']
378 ,$GLOBALS['oer_config']['prescriptions']['right']
381 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
383 $this->multiprint_header($pdf, $p);
385 $this->multiprint_body($pdf, $p);
387 $this->multiprint_footer($pdf);
389 if(!empty($toFile))
391 $toFile = $pdf->ezOutput();
393 else
395 $pdf->ezStream();
396 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
398 return;
401 function _print_prescription_old($p, & $toFile) {
403 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
404 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
405 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
406 ,$GLOBALS['oer_config']['prescriptions']['bottom']
407 ,$GLOBALS['oer_config']['prescriptions']['left']
408 ,$GLOBALS['oer_config']['prescriptions']['right']
411 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
413 if(!empty($this->pconfig['logo'])) {
414 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
416 $pdf->ezText($p->get_prescription_display(),10);
417 if($this->pconfig['use_signature'] == true ) {
418 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
420 else{
421 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
424 if(!empty($toFile))
426 $toFile = $pdf->ezOutput();
428 else
430 $pdf->ezStream();
431 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
433 return;
436 function _email_prescription($p,$email) {
437 if (empty($email)) {
438 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
439 return;
441 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
442 $mail = new PHPMailer();
443 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
444 //this is a temporary config item until the rest of the per practice billing settings make their way in
445 $mail->From = $GLOBALS['practice_return_email_path'];
446 $mail->FromName = $p->provider->get_name_display();
447 $mail->isMail();
448 $mail->Host = "localhost";
449 $mail->Mailer = "mail";
450 $text_body = $p->get_prescription_display();
451 $mail->Body = $text_body;
452 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
453 $mail->AddAddress($email);
454 if($mail->Send()) {
455 $this->assign("process_result","Email was successfully sent to: " . $email);
456 return;
458 else {
459 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
460 return;
464 function do_lookup() {
465 if ($_POST['process'] != "true")
466 return;
467 $list = array();
468 if (!empty($_POST['drug'])) {
469 $list = @RxList::get_list($_POST['drug']);
471 if (is_array($list)) {
472 $list = array_flip($list);
473 $this->assign("drug_options",$list);
474 $this->assign("drug_values",array_keys($list));
476 else {
477 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
479 //print_r($_POST);
480 //$this->assign("PROCESS","");
482 $_POST['process'] = "";
485 function _fax_prescription($p,$faxNum)
487 $err = "Sent fax";
488 //strip - ,(, ), and ws
489 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
490 //validate the number
492 if(!empty($faxNum) && is_numeric($faxNum))
494 //get the sendfax command and execute it
495 $cmd = $this->pconfig['sendfax'];
496 // prepend any prefix to the fax number
497 $pref=$this->pconfig['prefix'];
498 $faxNum=$pref.$faxNum;
499 if(empty($cmd))
501 $err .= " Send fax not set in includes/config.php";
502 break;
504 else
506 //generate file to fax
507 $faxFile = "Failed";
508 $this->_print_prescription($p, $faxFile);
509 if(empty($faxFile))
511 $err .= " _print_prescription returned empty file";
512 break;
514 $fileName = dirname(__FILE__)."/../documents/".$p->get_id()
515 .$p->get_patient_id()."_fax_.pdf";
516 //print "filename is $fileName";
517 touch($fileName); // php bug
518 $handle = fopen($fileName,"w");
519 if(!$handle)
521 $err .= " Failed to open file $fileName to write fax to";
522 break;
524 if(fwrite($handle, $faxFile) === false)
526 $err .= " Failed to write data to $fileName";
527 break;
529 fclose($handle);
530 $args = " -n -d $faxNum $fileName";
531 //print "command is $cmd $args<br>";
532 exec($cmd . $args);
536 else
538 $err = "bad fax number passed to function";
540 if($err)
542 $this->assign("process_result",$err);