4 * 2012 - Refactored extensively to allow for creating multiple feesheets on demand
5 * uses a session array of PIDS by Medical Information Integration, LLC - mi-squared.com
8 * @link http://www.open-emr.org
9 * @author Rod Roark <rod@sunsetsystems.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @author Ron Pulcer <rspulcer_2k@yahoo.com>
12 * @author Stephen Waite <stephen.waite@cmsvt.com>
13 * @copyright Copyright (c) 2007-2016 Rod Roark <rod@sunsetsystems.com>
14 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
15 * @copyright Copyright (c) 2019 Ron Pulcer <rspulcer_2k@yahoo.com>
16 * @copyright Copyright (c) 2019 Stephen Waite <stephen.waite@cmsvt.com>
17 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20 require_once("../globals.php");
21 require_once("$srcdir/appointments.inc.php");
22 require_once("$srcdir/patient.inc.php");
23 require_once("$srcdir/user.inc.php");
25 use OpenEMR\Core\Header
;
26 use OpenEMR\Services\FacilityService
;
28 $facilityService = new FacilityService();
30 function genColumn($ix)
34 for ($imax = count($SBCODES); $ix < $imax; ++
$ix) {
35 $a = explode('|', $SBCODES[$ix], 2);
37 if ($cmd == '*C') { // column break
41 if ($cmd == '*B') { // Borderless and empty
42 $html .= " <tr><td colspan='5' class='fscode' style='border-width:0 1px 0 0;padding-top:1px;' nowrap> </td></tr>\n";
43 } elseif ($cmd == '*G') {
49 $html .= " <tr><td colspan='5' align='center' class='fsgroup' style='vertical-align:middle' nowrap>$title</td></tr>\n";
50 } elseif ($cmd == '*H') {
56 $html .= " <tr><td colspan='5' class='fshead' style='vertical-align:middle' nowrap>$title</td></tr>\n";
63 $b = explode(':', $cmd);
65 $html .= " <td class='fscode' style='vertical-align:middle;width:14pt' nowrap> </td>\n";
72 $html .= " <td class='fscode' style='vertical-align:middle' nowrap>$code</td>\n";
73 $html .= " <td colspan='3' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
75 $html .= " <td colspan='2' class='fscode' style='vertical-align:middle' nowrap>" . text($b[0]) . '/' . text($b[1]) . "</td>\n";
76 $html .= " <td colspan='2' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
88 // Build output to handle multiple pids and and superbill for each patient.
89 // This value is initially a maximum, and will be recomputed to
90 // distribute lines evenly among the pages. (was 55)
95 $header_height = 44; // height of page headers in points
96 // This tells us if patient/encounter data is to be filled in.
97 // 1 = single PID from popup, 2=array of PIDs for session
99 if (empty($_GET['fill'])) {
102 $form_fill = $_GET['fill'];
105 // Show based on session array or single pid?
107 $apptdate_list = array();
110 if (!empty($_SESSION['pidList']) and $form_fill == 2) {
111 $pid_list = $_SESSION['pidList'];
112 // If PID list is in Session, then Appt. Date list is expected to be a parallel array
113 $apptdate_list = $_SESSION['apptdateList'];
114 } elseif ($form_fill == 1) {
115 array_push($pid_list, $pid); //get from active PID
117 array_push($pid_list, ''); // empty element for blank form
120 // This file is optional. You can create it to customize how the printed
121 // fee sheet looks, otherwise you'll get a mirror of your actual fee sheet.
123 if (file_exists("../../custom/fee_sheet_codes.php")) {
124 include_once("../../custom/fee_sheet_codes.php");
127 // TBD: Move these to globals.php, or make them user-specific.
133 // The $SBCODES table is a simple indexed array whose values are
134 // strings of the form "code|text" where code may be either a billing
135 // code or one of the following:
137 // *H - A main heading, where "text" is its title (to be centered).
138 // *G - Specifies a new category, where "text" is its name.
139 // *B - A borderless blank row.
140 // *C - Ends the current column and starts a new one.
141 // If $SBCODES is not provided, then manufacture it from the Fee Sheet.
143 if (empty($SBCODES)) {
147 // Create entries based on the fee_sheet_options table.
148 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
149 "ORDER BY fs_category, fs_option");
150 while ($row = sqlFetchArray($res)) {
151 $fs_category = $row['fs_category'];
152 $fs_option = $row['fs_option'];
153 $fs_codes = $row['fs_codes'];
154 if ($fs_category !== $last_category) {
155 $last_category = $fs_category;
156 $SBCODES[] = '*G|' . substr($fs_category, 1);
159 $SBCODES[] = " |" . substr($fs_option, 1);
162 // Create entries based on categories defined within the codes.
163 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
164 "WHERE list_id = 'superbill' AND activity = 1 ORDER BY seq");
165 while ($prow = sqlFetchArray($pres)) {
166 $SBCODES[] = '*G|' . xl_list_label($prow['title']);
167 $res = sqlStatement("SELECT code_type, code, code_text FROM codes " .
168 "WHERE superbill = ? AND active = 1 " .
169 "ORDER BY code_text", array($prow['option_id']));
170 while ($row = sqlFetchArray($res)) {
171 $SBCODES[] = $row['code'] . '|' . $row['code_text'];
175 // Create one more group, for Products.
176 if ($GLOBALS['sell_non_drug_products']) {
177 $SBCODES[] = '*G|' . xl('Products');
178 $tres = sqlStatement("SELECT " .
179 "dt.drug_id, dt.selector, d.name, d.ndc_number " .
180 "FROM drug_templates AS dt, drugs AS d WHERE " .
181 "d.drug_id = dt.drug_id AND d.active = 1 " .
182 "ORDER BY d.name, dt.selector, dt.drug_id");
183 while ($trow = sqlFetchArray($tres)) {
184 $tmp = $trow['selector'];
185 if ($trow['name'] !== $trow['selector']) {
186 $tmp .= ' ' . $trow['name'];
189 $prodcode = empty($trow['ndc_number']) ?
('(' . $trow['drug_id'] . ')') :
191 $SBCODES[] = "$prodcode|$tmp";
195 // Extra stuff for the labs section.
196 $SBCODES[] = '*G|' . xl('Notes');
197 $percol = intval((count($SBCODES) +
2) / 3);
198 while (count($SBCODES) < $percol * 3) {
202 // Adjust lines per page to distribute lines evenly among the pages.
203 $pages = intval(($percol +
$lines_in_stats +
$lines_per_page - 1) / $lines_per_page);
204 $lines_per_page = intval(($percol +
$lines_in_stats +
$pages - 1) / $pages);
206 // Figure out page and column breaks.
209 $page_start_index = 0;
210 while ($lines +
$lines_in_stats > $lines_per_page) {
212 $lines_this_page = $lines > $lines_per_page ?
$lines_per_page : $lines;
213 $lines -= $lines_this_page;
214 array_splice($SBCODES, $lines_this_page * 3 +
$page_start_index, 0, '*C|');
215 array_splice($SBCODES, $lines_this_page * 2 +
$page_start_index, 0, '*C|');
216 array_splice($SBCODES, $lines_this_page * 1 +
$page_start_index, 0, '*C|');
217 $page_start_index +
= $lines_this_page * 3 +
3;
220 array_splice($SBCODES, $lines * 2 +
$page_start_index, 0, '*C|');
221 array_splice($SBCODES, $lines * 1 +
$page_start_index, 0, '*C|');
224 $lheight = sprintf('%d', ($page_height - $header_height) / $lines_per_page);
226 // Common HTML Header information
234 font-family: sans-serif;
240 border-width: 0 0 1px 1px;
242 border-collapse: collapse;
243 border-color: #999999;
249 border-width: 0 0 0 0;
250 border-color: #999999;
253 height: " . attr($lheight) . "pt;
254 font-family: sans-serif;
256 font-size: " . attr($fontsize) . " pt;
257 background-color: #cccccc;
258 padding: " . attr($padding) . "pt 2pt 0pt 2pt;
260 border-width: 1px 1px 0 0;
261 border-color: #999999;
264 height: " . attr($lheight) . "pt;
265 font-family: sans-serif;
267 font-size: " . attr($fontsize) . "pt;
268 padding: " . attr($padding) . "pt 2pt 0pt 2pt;
270 border-width: 1px 1px 0 0;
271 border-color: #999999;
274 height: " . attr($lheight) . "pt;
275 font-family: sans-serif;
277 font-size: " . attr($fontsize) . "pt;
278 padding: " . attr($padding) . "pt 2pt 0pt 2pt;
280 border-width: 1px 1px 0 0;
281 border-color: #999999;
286 height: " . attr($header_height) . "pt;
311 page-break-after: always;
312 height: " . attr($page_height) . "pt;
316 $html .= "<title>" . text($frow['name'] ??
'') . "</title>" .
317 Header
::setupHeader(['opener', 'topdialog'], false) .
322 var win = top.printLogSetup ? top : opener.top;
323 win.printLogSetup(document.getElementById('printbutton'));
326 // Process click on Print button.
327 function printlog_before_print() {
328 var divstyle = document.getElementById('hideonprint').style;
329 divstyle.display = 'none';
334 <body bgcolor='#ffffff'>
335 <form name='theform' method='post' action='printed_fee_sheet.php?fill=" . attr_url($form_fill) . "'
336 onsubmit='return opener.top.restoreSession()'>
337 <div style='text-align: center;'>";
339 $today = date('Y-m-d');
341 $alertmsg = ''; // anything here pops up in an alert box
343 // Get details for the primary facility.
344 $frow = $facilityService->getPrimaryBusinessEntity();
346 // If primary is not set try to old method of guessing...for backward compatibility
348 $frow = $facilityService->getPrimaryBusinessEntity(array("useLegacyImplementation" => true));
353 $alertmsg = xl("No Primary Business Entity selected in facility list");
357 $ma_logo_path = "sites/" . $_SESSION['site_id'] . "/images/ma_logo.png";
358 if (is_file("$webserver_root/$ma_logo_path")) {
359 $logo = "$web_root/$ma_logo_path";
364 // Loop on array of PIDS
365 $saved_pages = $pages; //Save calculated page count of a single fee sheet
366 $loop_idx = 0; // counter for appt list
368 foreach ($pid_list as $pid) {
369 $apptdate = $apptdate_list[$loop_idx] ??
null; // parallel array to pid_list
370 $appointment = fetchAppointments($apptdate, $apptdate, $pid); // Only expecting one row for pid
371 // Set Pagebreak for multi forms
372 if ($form_fill == 2) {
373 $html .= "<div class=pagebreak>\n";
379 // Get the patient's name and chart number.
380 $patdata = getPatientData($pid);
381 // Get the referring providers info
382 $referDoc = getUserIDInfo($patdata['ref_providerID']);
385 // This tracks our position in the $SBCODES array.
388 while (--$pages >= 0) {
389 $html .= genFacilityTitle(xl('Superbill/Fee Sheet'), -1, $logo);
390 $html .= '<table style="width: 100%"><tr>' .
391 '<td>' . xlt('Patient') . ': <span style="font-weight: bold;">' . text($patdata['fname'] ??
'') . ' ' . text($patdata['mname'] ??
'') . ' ' . text($patdata['lname'] ??
'') . '</span></td>' .
392 '<td>' . xlt('DOB') . ': <span style="font-weight: bold;">' . text(oeFormatShortDate($patdata['DOB'] ??
'')) . '</span></td>' .
393 '<td>' . xlt('Date of Service') . ': <span style="font-weight: bold;">' . text(oeFormatShortDate($appointment[0]['pc_eventDate'] ??
'')) . ' ' . text(oeFormatTime($appointment[0]['pc_startTime'] ??
'')) . '</span></td>' .
394 '<td>' . xlt('Ref Prov') . ': <span style="font-weight: bold;">' . text($referDoc['fname'] ??
'') . ' ' . text($referDoc['lname'] ??
'') . '</span></td>' .
397 <table class='bordertbl' cellspacing='0' cellpadding='0' width='100%'>
400 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
402 <td class='toprow' style='width:10%'></td>
403 <td class='toprow' style='width:10%'></td>
404 <td class='toprow' style='width:25%'></td>
405 <td class='toprow' style='width:55%'></td>
408 $cindex = genColumn($cindex); // Column 1
410 if ($pages == 0) { // if this is the last page
412 <td colspan='3' valign='top' class='fshead' style='height:" . $lheight * 2 . "pt'>";
413 $html .= xlt('Patient') . ": ";
416 $html .= text($patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname']) . "<br />\n";
417 $html .= text($patdata['street']) . "<br />\n";
418 $html .= text($patdata['city'] . ', ' . $patdata['state'] . ' ' . $patdata['postal_code']) . "\n";
422 <td valign='top' class='fshead'>";
427 $html .= text($patdata['DOB']);
435 $html .= text($patdata['pubpid']);
441 <td colspan='3' valign='top' class='fshead' style='height:{$lheight}pt'>";
442 $html .= xlt('Provider');
446 if ($form_fill && $encounter) {
447 $query = "SELECT fe.reason, fe.date, u.fname, u.mname, u.lname, u.username " .
449 "JOIN form_encounter AS fe ON fe.id = f.form_id " .
450 "LEFT JOIN users AS u ON u.username = f.user " .
451 "WHERE f.pid = ? AND f.encounter = ? AND f.formdir = 'newpatient' AND f.deleted = 0 " .
452 "ORDER BY f.id LIMIT 1";
453 $encdata = sqlQuery($query, array($pid, $encounter));
454 if (!empty($encdata['username'])) {
455 $html .= $encdata['fname'] . ' ' . $encdata['mname'] . ' ' . $encdata['lname'];
460 <td valign='top' class='fshead'>";
461 $html .= xlt('Reason');
464 if (!empty($encdata)) {
465 $html .= text($encdata['reason']);
468 // Note: You would think that pc_comments would have the Appt. comments,
469 // but it is actually stored in pc_hometext in DB table (openemr_postcalendar_events).
470 $html .= $appointment['pc_hometext'] ??
'';
475 <td colspan='4' valign='top' class='fshead' style='height:{$lheight}pt'>";
477 if (empty($GLOBALS['ippf_specific'])) {
478 $html .= xlt('Insurance') . ":";
480 foreach (array('primary', 'secondary', 'tertiary') as $instype) {
481 $query = "SELECT * FROM insurance_data WHERE " .
482 "pid = ? AND type = ? " .
483 "ORDER BY date DESC LIMIT 1";
484 $row = sqlQuery($query, array($pid, $instype));
485 if (!empty($row['provider'])) {
486 $icobj = new InsuranceCompany($row['provider']);
487 $adobj = $icobj->get_address();
488 $insco_name = trim($icobj->get_name());
489 if ($instype != 'primary') {
494 $html .= " " . text($insco_name);
496 $html .= " <font color='red'><b>Missing Name</b></font>";
502 // IPPF wants a visit date box with the current date in it.
503 $html .= xlt('Visit date');
504 $html .= ":<br />\n";
505 if (!empty($encdata)) {
506 $html .= text(substr($encdata['date'], 0, 10));
508 $html .= text(oeFormatShortDate(date('Y-m-d'))) . "\n";
515 <td colspan='4' valign='top' class='fshead' style='height:{$lheight}pt'>";
516 $html .= xlt('Prior Visit');
521 <td colspan='4' valign='top' class='fshead' style='height:{$lheight}pt'>";
522 $html .= xlt('Today\'s Charges');
527 <td colspan='4' valign='top' class='fshead' style='height:{$lheight}pt'>";
528 $html .= xlt('Today\'s Balance');
533 <td colspan='4' valign='top' class='fshead' style='height:{$lheight}pt'>";
534 $html .= xlt('Notes');
538 } // end if last page
543 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
545 <td class='toprow' style='width:10%'></td>
546 <td class='toprow' style='width:10%'></td>
547 <td class='toprow' style='width:25%'></td>
548 <td class='toprow' style='width:55%'></td>
551 $cindex = genColumn($cindex); // Column 2
553 if ($pages == 0) { // if this is the last page
555 <td colspan='4' valign='top' class='fshead' style='height:" . $lheight * 8 . "pt'>";
556 $html .= xlt('Notes');
560 } // end if last page
565 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
567 <td class='toprow' style='width:10%'></td>
568 <td class='toprow' style='width:10%'></td>
569 <td class='toprow' style='width:25%'></td>
570 <td class='toprow' style='width:55%'></td>
573 $cindex = genColumn($cindex); // Column 3
575 if ($pages == 0) { // if this is the last page
577 <td valign='top' colspan='4' class='fshead' style='height:" . $lheight * 6 . "pt;border-width:0 1px 0 0'>
582 <td valign='top' colspan='4' class='fshead' style='height:" . $lheight * 2 . "pt'>";
583 $html .= xlt('Signature');
587 } // end if last page
595 $html .= "</div>"; // end of div.pageLetter
597 $pages = $saved_pages; // reset
598 $loop_idx++
; // appt list counter
602 if ($form_fill != 2) { //use native browser 'print' for multipage
603 $html .= "<div id='hideonprint'>
605 <input type='button' class='btn btn-secondary btn-print mt-3' value='";
607 $html .= xla('Print');
608 $html .= "' id='printbutton' />
618 // Send final result to display