php syntax bug fix in superbill report
[openemr.git] / interface / patient_file / printed_fee_sheet.php
blobea44aa1cdf23e8ca2d94641ec0a17afff099032c
1 <?php
3 // Copyright (C) 2007-2016 Rod Roark <rod@sunsetsystems.com>
4 //
5 // 2012 - Refactored extensively to allow for creating multiple feesheets on demand
6 // uses a session array of PIDS by Medical Information Integration, LLC - mi-squared.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 require_once("../globals.php");
14 require_once("$srcdir/acl.inc");
15 require_once("$srcdir/patient.inc");
16 require_once("$srcdir/billing.inc");
17 require_once("$srcdir/classes/Address.class.php");
18 require_once("$srcdir/classes/InsuranceCompany.class.php");
19 require_once("$srcdir/formatting.inc.php");
21 function genColumn($ix) {
22 global $html;
23 global $SBCODES;
24 for ($imax = count($SBCODES); $ix < $imax; ++$ix) {
25 $a = explode('|', $SBCODES[$ix], 2);
26 $cmd = trim($a[0]);
27 if ($cmd == '*C') { // column break
28 return++$ix;
30 if ($cmd == '*B') { // Borderless and empty
31 $html .= " <tr><td colspan='5' class='fscode' style='border-width:0 1px 0 0;padding-top:1px;' nowrap>&nbsp;</td></tr>\n";
32 } else if ($cmd == '*G') {
33 $title = htmlspecialchars($a[1]);
34 if (!$title)
35 $title = '&nbsp;';
36 $html .= " <tr><td colspan='5' align='center' class='fsgroup' style='vertical-align:middle' nowrap>$title</td></tr>\n";
38 else if ($cmd == '*H') {
39 $title = htmlspecialchars($a[1]);
40 if (!$title)
41 $title = '&nbsp;';
42 $html .= " <tr><td colspan='5' class='fshead' style='vertical-align:middle' nowrap>$title</td></tr>\n";
44 else {
45 $title = htmlspecialchars($a[1]);
46 if (!$title)
47 $title = '&nbsp;';
48 $b = explode(':', $cmd);
49 $html .= " <tr>\n";
50 $html .= " <td class='fscode' style='vertical-align:middle;width:14pt' nowrap>&nbsp;</td>\n";
51 if (count($b) <= 1) {
52 $code = $b[0];
53 if (!$code)
54 $code = '&nbsp;';
55 $html .= " <td class='fscode' style='vertical-align:middle' nowrap>$code</td>\n";
56 $html .= " <td colspan='3' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
58 else {
59 $html .= " <td colspan='2' class='fscode' style='vertical-align:middle' nowrap>" . $b[0] . '/' . $b[1] . "</td>\n";
60 $html .= " <td colspan='2' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
62 $html .= " </tr>\n";
65 return $ix;
68 // MAIN Body
70 // Build output to handle multiple pids and and superbill for each patient.
71 // This value is initially a maximum, and will be recomputed to
72 // distribute lines evenly among the pages. (was 55)
73 $lines_per_page = 55;
75 $lines_in_stats = 8;
77 $header_height = 44; // height of page headers in points
78 // This tells us if patient/encounter data is to be filled in.
79 // 1 = single PID from popup, 2=array of PIDs for session
81 if (empty($_GET['fill'])) {
82 $form_fill = 0;
83 } else {
84 $form_fill = $_GET['fill'];
87 // Show based on session array or single pid?
88 $pid_list = array();
90 if(!empty($_SESSION['pidList']) and $form_fill == 2)
92 $pid_list = $_SESSION['pidList'];
94 else if ($form_fill == 1)
96 array_push($pid_list,$pid); //get from active PID
97 } else {
98 array_push($pid_list,''); // empty element for blank form
101 // This file is optional. You can create it to customize how the printed
102 // fee sheet looks, otherwise you'll get a mirror of your actual fee sheet.
104 if (file_exists("../../custom/fee_sheet_codes.php"))
105 include_once ("../../custom/fee_sheet_codes.php");
107 // TBD: Move these to globals.php, or make them user-specific.
108 $fontsize = 7;
109 $page_height = 700;
111 $padding = 0;
113 // The $SBCODES table is a simple indexed array whose values are
114 // strings of the form "code|text" where code may be either a billing
115 // code or one of the following:
117 // *H - A main heading, where "text" is its title (to be centered).
118 // *G - Specifies a new category, where "text" is its name.
119 // *B - A borderless blank row.
120 // *C - Ends the current column and starts a new one.
121 // If $SBCODES is not provided, then manufacture it from the Fee Sheet.
123 if (empty($SBCODES)) {
124 $SBCODES = array();
125 $last_category = '';
127 // Create entries based on the fee_sheet_options table.
128 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
129 "ORDER BY fs_category, fs_option");
130 while ($row = sqlFetchArray($res)) {
131 $fs_category = $row['fs_category'];
132 $fs_option = $row['fs_option'];
133 $fs_codes = $row['fs_codes'];
134 if ($fs_category !== $last_category) {
135 $last_category = $fs_category;
136 $SBCODES[] = '*G|' . substr($fs_category, 1);
138 $SBCODES[] = " |" . substr($fs_option, 1);
141 // Create entries based on categories defined within the codes.
142 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
143 "WHERE list_id = 'superbill' AND activity = 1 ORDER BY seq");
144 while ($prow = sqlFetchArray($pres)) {
145 $SBCODES[] = '*G|' . xl_list_label($prow['title']);
146 $res = sqlStatement("SELECT code_type, code, code_text FROM codes " .
147 "WHERE superbill = '" . $prow['option_id'] . "' AND active = 1 " .
148 "ORDER BY code_text");
149 while ($row = sqlFetchArray($res)) {
150 $SBCODES[] = $row['code'] . '|' . $row['code_text'];
154 // Create one more group, for Products.
155 if ($GLOBALS['sell_non_drug_products']) {
156 $SBCODES[] = '*G|' . xl('Products');
157 $tres = sqlStatement("SELECT " .
158 "dt.drug_id, dt.selector, d.name, d.ndc_number " .
159 "FROM drug_templates AS dt, drugs AS d WHERE " .
160 "d.drug_id = dt.drug_id AND d.active = 1 " .
161 "ORDER BY d.name, dt.selector, dt.drug_id");
162 while ($trow = sqlFetchArray($tres)) {
163 $tmp = $trow['selector'];
164 if ($trow['name'] !== $trow['selector'])
165 $tmp .= ' ' . $trow['name'];
166 $prodcode = empty($trow['ndc_number']) ? ('(' . $trow['drug_id'] . ')') :
167 $trow['ndc_number'];
168 $SBCODES[] = "$prodcode|$tmp";
172 // Extra stuff for the labs section.
173 $SBCODES[] = '*G|' . xl('Notes');
174 $percol = intval((count($SBCODES) + 2) / 3);
175 while (count($SBCODES) < $percol * 3)
176 $SBCODES[] = '*B|';
178 // Adjust lines per page to distribute lines evenly among the pages.
179 $pages = intval(($percol + $lines_in_stats + $lines_per_page - 1) / $lines_per_page);
180 $lines_per_page = intval(($percol + $lines_in_stats + $pages - 1) / $pages);
182 // Figure out page and column breaks.
183 $pages = 1;
184 $lines = $percol;
185 $page_start_index = 0;
186 while ($lines + $lines_in_stats > $lines_per_page) {
187 ++$pages;
188 $lines_this_page = $lines > $lines_per_page ? $lines_per_page : $lines;
189 $lines -= $lines_this_page;
190 array_splice($SBCODES, $lines_this_page * 3 + $page_start_index, 0, '*C|');
191 array_splice($SBCODES, $lines_this_page * 2 + $page_start_index, 0, '*C|');
192 array_splice($SBCODES, $lines_this_page * 1 + $page_start_index, 0, '*C|');
193 $page_start_index += $lines_this_page * 3 + 3;
195 array_splice($SBCODES, $lines * 2 + $page_start_index, 0, '*C|');
196 array_splice($SBCODES, $lines * 1 + $page_start_index, 0, '*C|');
199 $lheight = sprintf('%d', ($page_height - $header_height) / $lines_per_page);
201 // Common HTML Header information
203 $html = "<html>
204 <head>";
206 $html .= "
207 <style>
208 body {
209 font-family: sans-serif;
210 font-weight: normal;
212 .bordertbl {
213 width: 100%;
214 border-style: solid;
215 border-width: 0 0 1px 1px;
216 border-spacing: 0;
217 border-collapse: collapse;
218 border-color: #999999;
220 td.toprow {
221 height: 1px;
222 padding: 0;
223 border-style: solid;
224 border-width: 0 0 0 0;
225 border-color: #999999;
227 td.fsgroup {
228 height: ${lheight}pt;
229 font-family: sans-serif;
230 font-weight: bold;
231 font-size: $fontsize pt;
232 background-color: #cccccc;
233 padding: ${padding}pt 2pt 0pt 2pt;
234 border-style: solid;
235 border-width: 1px 1px 0 0;
236 border-color: #999999;
238 td.fshead {
239 height: ${lheight}pt;
240 font-family: sans-serif;
241 font-weight: bold;
242 font-size: ${fontsize}pt;
243 padding: ${padding}pt 2pt 0pt 2pt;
244 border-style: solid;
245 border-width: 1px 1px 0 0;
246 border-color: #999999;
248 td.fscode {
249 height: ${lheight}pt;
250 font-family: sans-serif;
251 font-weight: normal;
252 font-size: ${fontsize}pt;
253 padding: ${padding}pt 2pt 0pt 2pt;
254 border-style: solid;
255 border-width: 1px 1px 0 0;
256 border-color: #999999;
259 .ftitletable {
260 width: 100%;
261 height: ${header_height}pt;
262 margin: 0 0 8pt 0;
264 .ftitlecell1 {
265 vertical-align: top;
266 text-align: left;
267 font-size: 14pt;
268 font-weight: bold;
270 .ftitlecell2 {
271 vertical-align: top;
272 text-align: right;
273 font-size: 9pt;
275 div.pagebreak {
276 page-break-after: always;
277 height: ${page_height}pt;
279 </style>";
281 $html .= "<title>" . htmlspecialchars($frow['name']) . "</title>
282 <script type='text/javascript' src='" . $GLOBALS['assets_static_relative'] . "/jquery-min-1-2-2/index.js'></script>
283 <script type=\"text/javascript\" src=\"../../library/dialog.js\"></script>
284 <script language=\"JavaScript\">";
286 $html .= "
287 $(document).ready(function() {
288 var win = top.printLogSetup ? top : opener.top;
289 win.printLogSetup(document.getElementById('printbutton'));
292 // Process click on Print button.
293 function printlog_before_print() {
294 var divstyle = document.getElementById('hideonprint').style;
295 divstyle.display = 'none';
298 </script>
299 </head>
300 <body bgcolor='#ffffff'>
301 <form name='theform' method='post' action='printed_fee_sheet.php?fill=$form_fill'
302 onsubmit='return opener.top.restoreSession()'>
303 <center>";
305 // Set Pagebreak for multi forms
306 if ($form_fill == 2) {
307 $html .= "<div class=pagebreak>\n";
308 } else {
309 $html .= "<div>\n";
312 $today = date('Y-m-d');
314 $alertmsg = ''; // anything here pops up in an alert box
316 // Get details for the primary facility.
317 $frow = sqlQuery("SELECT * FROM facility WHERE primary_business_entity = 1");
319 // If primary is not set try to old method of guessing...for backward compatibility
320 if (empty($frow)) {
321 $frow = sqlQuery("SELECT * FROM facility " .
322 "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
325 // Still missing...
326 if (empty($frow)) {
327 $alertmsg = xl("No Primary Business Entity selected in facility list");
330 // Loop on array of PIDS
331 $saved_pages = $pages; //Save calculated page count of a single fee sheet
333 foreach ($pid_list as $pid) {
335 if ($form_fill) {
336 // Get the patient's name and chart number.
337 $patdata = getPatientData($pid);
340 // This tracks our position in the $SBCODES array.
341 $cindex = 0;
343 while (--$pages >= 0) {
345 $html .= genFacilityTitle(xl('Superbill/Fee Sheet'), -1);
347 $html .="
348 <table class='bordertbl' cellspacing='0' cellpadding='0' width='100%'>
349 <tr>
350 <td valign='top'>
351 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
352 <tr>
353 <td class='toprow' style='width:10%'></td>
354 <td class='toprow' style='width:10%'></td>
355 <td class='toprow' style='width:25%'></td>
356 <td class='toprow' style='width:55%'></td>
357 </tr>";
359 $cindex = genColumn($cindex); // Column 1
361 if ($pages == 0) { // if this is the last page
362 $html .= "<tr>
363 <td colspan='3' valign='top' class='fshead' style='height:" . $lheight * 2 . "pt'>";
364 $html .= xl('Patient', 'r');
365 $html .= ":<br />";
367 if ($form_fill) {
368 $html .= $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'] . "<br />\n";
369 $html .= $patdata['street'] . "<br />\n";
370 $html .= $patdata['city'] . ', ' . $patdata['state'] . ' ' . $patdata['postal_code'] . "\n";
373 $html .= "</td>
374 <td valign='top' class='fshead'>";
375 $html .= xl('DOB', 'r');
376 $html .= ":<br />";
378 if ($form_fill)
379 $html .= $patdata['DOB'];
381 $html .= xl('ID', 'r');
382 $html .= ":<br />";
384 if ($form_fill)
385 $html .= $patdata['pubpid'];
387 $html .= "</td>
388 </tr>
389 <tr>
390 <td colspan='3' valign='top' class='fshead' style='height:${lheight}pt'>";
391 $html .= xl('Doctor', 'r');
392 $html .= ":<br />";
394 $encdata = false;
395 if ($form_fill && $encounter) {
396 $query = "SELECT fe.reason, fe.date, u.fname, u.mname, u.lname, u.username " .
397 "FROM forms AS f " .
398 "JOIN form_encounter AS fe ON fe.id = f.form_id " .
399 "LEFT JOIN users AS u ON u.username = f.user " .
400 "WHERE f.pid = '$pid' AND f.encounter = '$encounter' AND f.formdir = 'newpatient' AND f.deleted = 0 " .
401 "ORDER BY f.id LIMIT 1";
402 $encdata = sqlQuery($query);
403 if (!empty($encdata['username'])) {
404 $html .= $encdata['fname'] . ' ' . $encdata['mname'] . ' ' . $encdata['lname'];
408 $html .= "</td>
409 <td valign='top' class='fshead'>";
410 $html .= xl('Reason', 'r');
411 $html .= ":<br />";
413 if (!empty($encdata)) {
414 $html .= $encdata['reason'];
417 $html .= "</td>
418 </tr>
419 <tr>
420 <td colspan='4' valign='top' class='fshead' style='height:${lheight}pt'>";
422 if (empty($GLOBALS['ippf_specific'])) {
423 $html .= xl('Insurance', 'r').":";
424 if ($form_fill) {
425 foreach (array('primary', 'secondary', 'tertiary') as $instype) {
426 $query = "SELECT * FROM insurance_data WHERE " .
427 "pid = '$pid' AND type = '$instype' " .
428 "ORDER BY date DESC LIMIT 1";
429 $row = sqlQuery($query);
430 if ($row['provider']) {
431 $icobj = new InsuranceCompany($row['provider']);
432 $adobj = $icobj->get_address();
433 $insco_name = trim($icobj->get_name());
434 if ($instype != 'primary')
435 $html .= ",";
436 if ($insco_name) {
437 $html .= "&nbsp;$insco_name";
438 } else {
439 $html .= "&nbsp;<font color='red'><b>Missing Name</b></font>";
444 } else {
445 // IPPF wants a visit date box with the current date in it.
446 $html .= xl('Visit date','r');
447 $html .= ":<br />\n";
448 if (!empty($encdata)) {
449 $html .= substr($encdata['date'], 0, 10);
450 } else {
451 $html .= oeFormatShortDate(date('Y-m-d')) . "\n";
455 $html .= "</td>
456 </tr>
457 <tr>
458 <td colspan='4' valign='top' class='fshead' style='height:${lheight}pt'>";
459 $html .= xl('Prior Visit', 'r');
460 $html .= ":<br />
461 </td>
462 </tr>
463 <tr>
464 <td colspan='4' valign='top' class='fshead' style='height:${lheight}pt'>";
465 $html .= xl('Today\'s Charges', 'r');
466 $html .= ":<br />
467 </td>
468 </tr>
469 <tr>
470 <td colspan='4' valign='top' class='fshead' style='height:${lheight}pt'>";
471 $html .= xl('Today\'s Balance', 'r');
472 $html .= ":<br />
473 </td>
474 </tr>
475 <tr>
476 <td colspan='4' valign='top' class='fshead' style='height:${lheight}pt'>";
477 $html .= xl('Notes', 'r');
478 $html .= ":<br />
479 </td>
480 </tr>";
481 } // end if last page
483 $html .= "</table>
484 </td>
485 <td valign='top'>
486 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
487 <tr>
488 <td class='toprow' style='width:10%'></td>
489 <td class='toprow' style='width:10%'></td>
490 <td class='toprow' style='width:25%'></td>
491 <td class='toprow' style='width:55%'></td>
492 </tr>";
494 $cindex = genColumn($cindex); // Column 2
496 if ($pages == 0) { // if this is the last page
497 $html .= "<tr>
498 <td colspan='4' valign='top' class='fshead' style='height:" . $lheight * 8 . "pt'>";
499 $html .= xl('Notes', 'r');
500 $html .= ":<br />
501 </td>
502 </tr>";
503 } // end if last page
505 $html .= "</table>
506 </td>
507 <td valign='top'>
508 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
509 <tr>
510 <td class='toprow' style='width:10%'></td>
511 <td class='toprow' style='width:10%'></td>
512 <td class='toprow' style='width:25%'></td>
513 <td class='toprow' style='width:55%'></td>
514 </tr>";
516 $cindex = genColumn($cindex); // Column 3
518 if ($pages == 0) { // if this is the last page
519 $html .= "<tr>
520 <td valign='top' colspan='4' class='fshead' style='height:" . $lheight * 6 . "pt;border-width:0 1px 0 0'>
521 &nbsp;
522 </td>
523 </tr>
524 <tr>
525 <td valign='top' colspan='4' class='fshead' style='height:" . $lheight * 2 . "pt'>";
526 $html .= xl('Signature', 'r');
527 $html .= ":<br />
528 </td>
529 </tr>";
530 } // end if last page
532 $html .= "</table>
533 </td>
534 </tr>
536 </table>";
538 $html .= "</div>"; //end of div.pageLetter
540 } // end while
541 $pages = $saved_pages; //RESET
544 // Common End Code
545 if ($form_fill != 2) { //use native browser 'print' for multipage
546 $html .= "<div id='hideonprint'>
548 <input type='button' value='";
550 $html .= xla('Print');
551 $html .="' id='printbutton' />
552 </div>";
555 $html .= "
556 </form>
557 </center>
558 </body>
559 </html>";
561 // Send final result to display
562 echo $html;