Add New FMC code from Intesync
[openemr.git] / interface / patient_file / printed_fee_sheet.php
blob8c87384382ef89f0cd5e54932a5667b58092895f
1 <?php
2 // Copyright (C) 2007-2010 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once("../globals.php");
10 require_once("$srcdir/acl.inc");
11 require_once("$srcdir/patient.inc");
12 require_once("$srcdir/classes/Address.class.php");
13 require_once("$srcdir/classes/InsuranceCompany.class.php");
14 require_once("$srcdir/formatting.inc.php");
16 // This value is initially a maximum, and will be recomputed to
17 // distribute lines evenly among the pages.
18 $lines_per_page = 55;
20 $lines_in_stats = 8;
22 $header_height = 44; // height of page headers in points
24 // This tells us if patient/encounter data is to be filled in.
26 $form_fill = empty($_GET['fill']) ? 0 : 1;
28 // This file is optional. You can create it to customize how the printed
29 // fee sheet looks, otherwise you'll get a mirror of your actual fee sheet.
31 if (file_exists("../../custom/fee_sheet_codes.php"))
32 include_once ("../../custom/fee_sheet_codes.php");
34 // TBD: Move these to globals.php, or make them user-specific.
35 $fontsize = 7;
36 $page_height = 700;
38 $padding = 0;
40 // The $SBCODES table is a simple indexed array whose values are
41 // strings of the form "code|text" where code may be either a billing
42 // code or one of the following:
44 // *H - A main heading, where "text" is its title (to be centered).
45 // *G - Specifies a new category, where "text" is its name.
46 // *B - A borderless blank row.
47 // *C - Ends the current column and starts a new one.
49 // If $SBCODES is not provided, then manufacture it from the Fee Sheet.
51 if (empty($SBCODES)) {
52 $SBCODES = array();
53 $last_category = '';
55 // Create entries based on the fee_sheet_options table.
56 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
57 "ORDER BY fs_category, fs_option");
58 while ($row = sqlFetchArray($res)) {
59 $fs_category = $row['fs_category'];
60 $fs_option = $row['fs_option'];
61 $fs_codes = $row['fs_codes'];
62 if($fs_category !== $last_category) {
63 $last_category = $fs_category;
64 $SBCODES[] = '*G|' . substr($fs_category, 1);
66 $SBCODES[] = " |" . substr($fs_option, 1);
69 // Create entries based on categories defined within the codes.
70 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
71 "WHERE list_id = 'superbill' ORDER BY seq");
72 while ($prow = sqlFetchArray($pres)) {
73 $SBCODES[] = '*G|' . $prow['title'];
74 $res = sqlStatement("SELECT code_type, code, code_text FROM codes " .
75 "WHERE superbill = '" . $prow['option_id'] . "' AND active = 1 " .
76 "ORDER BY code_text");
77 while ($row = sqlFetchArray($res)) {
78 $SBCODES[] = $row['code'] . '|' . $row['code_text'];
82 // Create one more group, for Products.
83 if ($GLOBALS['sell_non_drug_products']) {
84 $SBCODES[] = '*G|' . xl('Products');
85 $tres = sqlStatement("SELECT dt.drug_id, dt.selector, d.name " .
86 "FROM drug_templates AS dt, drugs AS d WHERE " .
87 "d.drug_id = dt.drug_id AND d.active = 1 " .
88 "ORDER BY d.name, dt.selector, dt.drug_id");
89 while ($trow = sqlFetchArray($tres)) {
90 $tmp = $trow['selector'];
91 if ($trow['name'] !== $trow['selector']) $tmp .= ' ' . $trow['name'];
92 $SBCODES[] = $trow['drug_id'] . '|' . $tmp;
96 // Extra stuff for the labs section.
97 $SBCODES[] = '*G|' . xl('Notes');
98 $percol = intval((count($SBCODES) + 2) / 3);
99 while (count($SBCODES) < $percol * 3) $SBCODES[] = '*B|';
101 // Adjust lines per page to distribute lines evenly among the pages.
102 $pages = intval(($percol + $lines_in_stats + $lines_per_page - 1) / $lines_per_page);
103 $lines_per_page = intval(($percol + $lines_in_stats + $pages - 1) / $pages);
105 // Figure out page and column breaks.
106 $pages = 1;
107 $lines = $percol;
108 $page_start_index = 0;
109 while ($lines + $lines_in_stats > $lines_per_page) {
110 ++$pages;
111 $lines_this_page = $lines > $lines_per_page ? $lines_per_page : $lines;
112 $lines -= $lines_this_page;
113 array_splice($SBCODES, $lines_this_page*3 + $page_start_index, 0, '*C|');
114 array_splice($SBCODES, $lines_this_page*2 + $page_start_index, 0, '*C|');
115 array_splice($SBCODES, $lines_this_page*1 + $page_start_index, 0, '*C|');
116 $page_start_index += $lines_this_page * 3 + 3;
118 array_splice($SBCODES, $lines*2 + $page_start_index, 0, '*C|');
119 array_splice($SBCODES, $lines*1 + $page_start_index, 0, '*C|');
122 $lheight = sprintf('%d', ($page_height - $header_height) / $lines_per_page);
124 <html>
125 <head>
126 <?php html_header_show(); ?>
128 <style>
129 body {
130 font-family: sans-serif;
131 font-weight: normal;
133 .bordertbl {
134 width: 100%;
135 border-style: solid;
136 border-width: 0 0 1px 1px;
137 border-spacing: 0;
138 border-collapse: collapse;
139 border-color: #999999;
141 td.toprow {
142 height: 1px;
143 padding: 0;
144 border-style: solid;
145 border-width: 0 0 0 0;
146 border-color: #999999;
148 td.fsgroup {
149 height: <?php echo $lheight; ?>pt;
150 font-family: sans-serif;
151 font-weight: bold;
152 font-size: <?php echo $fontsize ?>pt;
153 background-color: #cccccc;
154 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
155 border-style: solid;
156 border-width: 1px 1px 0 0;
157 border-color: #999999;
159 td.fshead {
160 height: <?php echo $lheight; ?>pt;
161 font-family: sans-serif;
162 font-weight: bold;
163 font-size: <?php echo $fontsize ?>pt;
164 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
165 border-style: solid;
166 border-width: 1px 1px 0 0;
167 border-color: #999999;
169 td.fscode {
170 height: <?php echo $lheight; ?>pt;
171 font-family: sans-serif;
172 font-weight: normal;
173 font-size: <?php echo $fontsize ?>pt;
174 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
175 border-style: solid;
176 border-width: 1px 1px 0 0;
177 border-color: #999999;
180 .ftitletable {
181 width: 100%;
182 height: <?php echo $header_height; ?>pt;
183 margin: 0 0 8pt 0;
185 .ftitlecell1 {
186 vertical-align: top;
187 text-align: left;
188 font-size: 14pt;
189 font-weight: bold;
191 .ftitlecell2 {
192 vertical-align: top;
193 text-align: right;
194 font-size: 9pt;
196 </style>
198 <?php
200 // Get the co-pay amount that is effective on the given date.
201 // Or if no insurance on that date, return -1.
203 function getCopay($patient_id, $encdate) {
204 $tmp = sqlQuery("SELECT provider, copay FROM insurance_data " .
205 "WHERE pid = '$patient_id' AND type = 'primary' " .
206 "AND date <= '$encdate' ORDER BY date DESC LIMIT 1");
207 if ($tmp['provider']) return sprintf('%01.2f', 0 + $tmp['copay']);
208 return -1;
211 function genColumn($ix) {
212 global $SBCODES;
213 for ($imax = count($SBCODES); $ix < $imax; ++$ix) {
214 $a = explode('|', $SBCODES[$ix], 2);
215 $cmd = trim($a[0]);
216 if ($cmd == '*C') { // column break
217 return ++$ix;
219 if ($cmd == '*B') { // Borderless and empty
220 echo " <tr><td colspan='5' class='fscode' style='border-width:0 1px 0 0;padding-top:1px;' nowrap>&nbsp;</td></tr>\n";
222 else if ($cmd == '*G') {
223 $title = htmlspecialchars($a[1]);
224 if (!$title) $title='&nbsp;';
225 echo " <tr><td colspan='5' align='center' class='fsgroup' style='vertical-align:middle' nowrap>$title</td></tr>\n";
227 else if ($cmd == '*H') {
228 $title = htmlspecialchars($a[1]);
229 if (!$title) $title='&nbsp;';
230 echo " <tr><td colspan='5' class='fshead' style='vertical-align:middle' nowrap>$title</td></tr>\n";
232 else {
233 $title = htmlspecialchars($a[1]);
234 if (!$title) $title='&nbsp;';
235 $b = explode(':', $cmd);
236 echo " <tr>\n";
237 echo " <td class='fscode' style='vertical-align:middle;width:14pt' nowrap>&nbsp;</td>\n";
238 if (count($b) <= 1) {
239 $code = $b[0];
240 if (!$code) $code='&nbsp;';
241 echo " <td class='fscode' style='vertical-align:middle' nowrap>$code</td>\n";
242 echo " <td colspan='3' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
244 else {
245 echo " <td colspan='2' class='fscode' style='vertical-align:middle' nowrap>" . $b[0] . '/' . $b[1] . "</td>\n";
246 echo " <td colspan='2' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
248 echo " </tr>\n";
251 return $ix;
254 $today = date('Y-m-d');
256 $alertmsg = ''; // anything here pops up in an alert box
258 // Get details for what we guess is the primary facility.
259 $frow = sqlQuery("SELECT * FROM facility " .
260 "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
262 if ($form_fill) {
263 // Get the patient's name and chart number.
264 $patdata = getPatientData($pid);
267 // This tracks our position in the $SBCODES array.
268 $cindex = 0;
271 <title><?php echo htmlspecialchars($frow['name']); ?></title>
272 <script type="text/javascript" src="../../library/dialog.js"></script>
273 <script language="JavaScript">
275 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
277 // Process click on Print button.
278 function printme() {
279 var divstyle = document.getElementById('hideonprint').style;
280 divstyle.display = 'none';
281 window.print();
284 </script>
285 </head>
286 <body bgcolor='#ffffff'>
287 <form name='theform' method='post' action='printed_fee_sheet.php?fill=<?php echo $form_fill; ?>'
288 onsubmit='return top.restoreSession()'>
289 <center>
291 <?php while (--$pages >= 0) { ?>
293 <?php echo genFacilityTitle(xl('Fee Sheet'), -1); ?>
295 <table class='bordertbl' cellspacing='0' cellpadding='0' width='100%'>
296 <tr>
297 <td valign='top'>
298 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
299 <tr>
300 <td class='toprow' style='width:10%'></td>
301 <td class='toprow' style='width:10%'></td>
302 <td class='toprow' style='width:25%'></td>
303 <td class='toprow' style='width:55%'></td>
304 </tr>
305 <?php
306 $cindex = genColumn($cindex); // Column 1
309 <?php if ($pages == 0) { // if this is the last page ?>
311 <tr>
312 <td colspan='3' valign='top' class='fshead' style='height:<?php echo $lheight * 2; ?>pt'>
313 Patient:<br />
314 <?php
315 if ($form_fill) {
316 echo $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'] . "<br />\n";
317 echo $patdata['street'] . "<br />\n";
318 echo $patdata['city'] . ', ' . $patdata['state'] . ' ' . $patdata['postal_code'] . "\n";
321 </td>
322 <td valign='top' class='fshead'>
323 DOB:<br /><?php if ($form_fill) echo $patdata['DOB']; ?><br />
324 ID:<br /><?php if ($form_fill) echo $patdata['pubpid']; ?>
325 </td>
326 </tr>
327 <tr>
328 <td colspan='3' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
329 Doctor:<br />
330 <?php
331 $encdata = false;
332 if ($form_fill && $encounter) {
333 $query = "SELECT fe.reason, fe.date, u.fname, u.mname, u.lname, u.username " .
334 "FROM forms AS f " .
335 "JOIN form_encounter AS fe ON fe.id = f.form_id " .
336 "LEFT JOIN users AS u ON u.username = f.user " .
337 "WHERE f.pid = '$pid' AND f.encounter = '$encounter' AND f.formdir = 'newpatient' AND f.deleted = 0 " .
338 "ORDER BY f.id LIMIT 1";
339 $encdata = sqlQuery($query);
340 if (!empty($encdata['username'])) {
341 echo $encdata['fname'] . ' ' . $encdata['mname'] . ' ' . $encdata['lname'];
345 </td>
346 <td valign='top' class='fshead'>
347 Reason:<br />
348 <?php
349 if (!empty($encdata)) {
350 echo $encdata['reason'];
353 </td>
354 </tr>
355 <tr>
356 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
357 <?php
358 if (empty($GLOBALS['ippf_specific'])) {
359 echo "Insurance:\n";
360 if ($form_fill) {
361 foreach (array('primary','secondary','tertiary') as $instype) {
362 $query = "SELECT * FROM insurance_data WHERE " .
363 "pid = '$pid' AND type = '$instype' " .
364 "ORDER BY date DESC LIMIT 1";
365 $row = sqlQuery($query);
366 if ($row['provider']) {
367 $icobj = new InsuranceCompany($row['provider']);
368 $adobj = $icobj->get_address();
369 $insco_name = trim($icobj->get_name());
370 if ($instype != 'primary') echo ",";
371 if ($insco_name) {
372 echo "&nbsp;$insco_name";
373 } else {
374 echo "&nbsp;<font color='red'><b>Missing Name</b></font>";
380 else {
381 // IPPF wants a visit date box with the current date in it.
382 echo "Visit date:<br />\n";
383 if (!empty($encdata)) {
384 echo substr($encdata['date'], 0, 10);
386 else {
387 echo oeFormatShortDate(date('Y-m-d')) . "\n";
391 </td>
392 </tr>
393 <tr>
394 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
395 Prior balance:<br />
396 </td>
397 </tr>
398 <tr>
399 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
400 Today's charges:<br />
401 </td>
402 </tr>
403 <tr>
404 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
405 Today's payment:<br />
406 </td>
407 </tr>
408 <tr>
409 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
410 Notes:<br />
411 </td>
412 </tr>
414 <?php } // end if last page ?>
416 </table>
417 </td>
418 <td valign='top'>
419 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
420 <tr>
421 <td class='toprow' style='width:10%'></td>
422 <td class='toprow' style='width:10%'></td>
423 <td class='toprow' style='width:25%'></td>
424 <td class='toprow' style='width:55%'></td>
425 </tr>
426 <?php
427 $cindex = genColumn($cindex); // Column 2
430 <?php if ($pages == 0) { // if this is the last page ?>
432 <tr>
433 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight * 8; ?>pt'>
434 Notes:<br />
435 </td>
436 </tr>
438 <?php } // end if last page ?>
440 </table>
441 </td>
442 <td valign='top'>
443 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
444 <tr>
445 <td class='toprow' style='width:10%'></td>
446 <td class='toprow' style='width:10%'></td>
447 <td class='toprow' style='width:25%'></td>
448 <td class='toprow' style='width:55%'></td>
449 </tr>
450 <?php
451 $cindex = genColumn($cindex); // Column 3
454 <?php if ($pages == 0) { // if this is the last page ?>
456 <tr>
457 <td valign='top' colspan='4' class='fshead' style='height:<?php echo $lheight * 6; ?>pt;border-width:0 1px 0 0'>
458 &nbsp;
459 </td>
460 </tr>
461 <tr>
462 <td valign='top' colspan='4' class='fshead' style='height:<?php echo $lheight * 2; ?>pt'>
463 Signature:<br />
464 </td>
465 </tr>
467 <?php } // end if last page ?>
469 </table>
470 </td>
471 </tr>
473 </table>
475 <?php
476 if ($pages > 0) echo "<p style='page-break-after: always' />\n";
477 } // end while
479 <div id='hideonprint'>
481 <input type='button' value='<?php xl('Print','e'); ?>' onclick='printme()' />
483 </div>
484 </form>
485 </center>
486 </body>
487 </html>