added support for optional custom pdf logic
[openemr.git] / interface / patient_file / printed_fee_sheet.php
bloba71f426bddfa9e7067aaf524e1ec7539b9f4afd3
1 <?php
2 // Copyright (C) 2007-2009 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");
15 // This value is initially a maximum, and will be recomputed to
16 // distribute lines evenly among the pages.
17 $lines_per_page = 55;
19 $lines_in_stats = 8;
21 $header_height = 44; // height of page headers in points
23 // This tells us if patient/encounter data is to be filled in.
25 $form_fill = empty($_GET['fill']) ? 0 : 1;
27 // This file is optional. You can create it to customize how the printed
28 // fee sheet looks, otherwise you'll get a mirror of your actual fee sheet.
30 if (file_exists("../../custom/fee_sheet_codes.php"))
31 include_once ("../../custom/fee_sheet_codes.php");
33 // TBD: Move these to globals.php, or make them user-specific.
34 $fontsize = 7;
35 $page_height = 700;
37 $padding = 0;
39 // The $SBCODES table is a simple indexed array whose values are
40 // strings of the form "code|text" where code may be either a billing
41 // code or one of the following:
43 // *H - A main heading, where "text" is its title (to be centered).
44 // *G - Specifies a new category, where "text" is its name.
45 // *B - A borderless blank row.
46 // *C - Ends the current column and starts a new one.
48 // If $SBCODES is not provided, then manufacture it from the Fee Sheet.
50 if (empty($SBCODES)) {
51 $SBCODES = array();
52 $last_category = '';
54 // Create entries based on the fee_sheet_options table.
55 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
56 "ORDER BY fs_category, fs_option");
57 while ($row = sqlFetchArray($res)) {
58 $fs_category = $row['fs_category'];
59 $fs_option = $row['fs_option'];
60 $fs_codes = $row['fs_codes'];
61 if($fs_category !== $last_category) {
62 $last_category = $fs_category;
63 $SBCODES[] = '*G|' . substr($fs_category, 1);
65 $SBCODES[] = " |" . substr($fs_option, 1);
68 // Create entries based on categories defined within the codes.
69 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
70 "WHERE list_id = 'superbill' ORDER BY seq");
71 while ($prow = sqlFetchArray($pres)) {
72 $SBCODES[] = '*G|' . $prow['title'];
73 $res = sqlStatement("SELECT code_type, code, code_text FROM codes " .
74 "WHERE superbill = '" . $prow['option_id'] . "' " .
75 "ORDER BY code_text");
76 while ($row = sqlFetchArray($res)) {
77 $SBCODES[] = $row['code'] . '|' . $row['code_text'];
81 // Create one more group, for Products.
82 if ($GLOBALS['sell_non_drug_products']) {
83 $SBCODES[] = '*G|' . xl('Products');
84 $tres = sqlStatement("SELECT dt.drug_id, dt.selector, d.name " .
85 "FROM drug_templates AS dt, drugs AS d WHERE " .
86 "d.drug_id = dt.drug_id " .
87 "ORDER BY d.name, dt.selector, dt.drug_id");
88 while ($trow = sqlFetchArray($tres)) {
89 $tmp = $trow['selector'];
90 if ($trow['name'] !== $trow['selector']) $tmp .= ' ' . $trow['name'];
91 $SBCODES[] = $trow['drug_id'] . '|' . $tmp;
95 // Extra stuff for the labs section.
96 $SBCODES[] = '*G|' . xl('Notes');
97 $percol = intval((count($SBCODES) + 2) / 3);
98 while (count($SBCODES) < $percol * 3) $SBCODES[] = '*B|';
100 // Adjust lines per page to distribute lines evenly among the pages.
101 $pages = intval(($percol + $lines_in_stats + $lines_per_page - 1) / $lines_per_page);
102 $lines_per_page = intval(($percol + $lines_in_stats + $pages - 1) / $pages);
104 // Figure out page and column breaks.
105 $pages = 1;
106 $lines = $percol;
107 $page_start_index = 0;
108 while ($lines + $lines_in_stats > $lines_per_page) {
109 ++$pages;
110 $lines_this_page = $lines > $lines_per_page ? $lines_per_page : $lines;
111 $lines -= $lines_this_page;
112 array_splice($SBCODES, $lines_this_page*3 + $page_start_index, 0, '*C|');
113 array_splice($SBCODES, $lines_this_page*2 + $page_start_index, 0, '*C|');
114 array_splice($SBCODES, $lines_this_page*1 + $page_start_index, 0, '*C|');
115 $page_start_index += $lines_this_page * 3 + 3;
117 array_splice($SBCODES, $lines*2 + $page_start_index, 0, '*C|');
118 array_splice($SBCODES, $lines*1 + $page_start_index, 0, '*C|');
121 $lheight = sprintf('%d', ($page_height - $header_height) / $lines_per_page);
123 <html>
124 <head>
125 <?php html_header_show(); ?>
127 <style>
128 body {
129 font-family: sans-serif;
130 font-weight: normal;
132 .bordertbl {
133 width: 100%;
134 border-style: solid;
135 border-width: 0 0 1px 1px;
136 border-spacing: 0;
137 border-collapse: collapse;
138 border-color: #999999;
140 td.toprow {
141 height: 1px;
142 padding: 0;
143 border-style: solid;
144 border-width: 0 0 0 0;
145 border-color: #999999;
147 td.fsgroup {
148 height: <?php echo $lheight; ?>pt;
149 font-family: sans-serif;
150 font-weight: bold;
151 font-size: <?php echo $fontsize ?>pt;
152 background-color: #cccccc;
153 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
154 border-style: solid;
155 border-width: 1px 1px 0 0;
156 border-color: #999999;
158 td.fshead {
159 height: <?php echo $lheight; ?>pt;
160 font-family: sans-serif;
161 font-weight: bold;
162 font-size: <?php echo $fontsize ?>pt;
163 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
164 border-style: solid;
165 border-width: 1px 1px 0 0;
166 border-color: #999999;
168 td.fscode {
169 height: <?php echo $lheight; ?>pt;
170 font-family: sans-serif;
171 font-weight: normal;
172 font-size: <?php echo $fontsize ?>pt;
173 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
174 border-style: solid;
175 border-width: 1px 1px 0 0;
176 border-color: #999999;
179 .ftitletable {
180 width: 100%;
181 height: <?php echo $header_height; ?>pt;
182 margin: 0 0 8pt 0;
184 .ftitlecell1 {
185 vertical-align: top;
186 text-align: left;
187 font-size: 14pt;
188 font-weight: bold;
190 .ftitlecell2 {
191 vertical-align: top;
192 text-align: right;
193 font-size: 9pt;
195 </style>
197 <?php
199 // Get the co-pay amount that is effective on the given date.
200 // Or if no insurance on that date, return -1.
202 function getCopay($patient_id, $encdate) {
203 $tmp = sqlQuery("SELECT provider, copay FROM insurance_data " .
204 "WHERE pid = '$patient_id' AND type = 'primary' " .
205 "AND date <= '$encdate' ORDER BY date DESC LIMIT 1");
206 if ($tmp['provider']) return sprintf('%01.2f', 0 + $tmp['copay']);
207 return -1;
210 function genColumn($ix) {
211 global $SBCODES;
212 for ($imax = count($SBCODES); $ix < $imax; ++$ix) {
213 $a = explode('|', $SBCODES[$ix], 2);
214 $cmd = trim($a[0]);
215 if ($cmd == '*C') { // column break
216 return ++$ix;
218 if ($cmd == '*B') { // Borderless and empty
219 echo " <tr><td colspan='5' class='fscode' style='border-width:0 1px 0 0;padding-top:1px;' nowrap>&nbsp;</td></tr>\n";
221 else if ($cmd == '*G') {
222 $title = htmlspecialchars($a[1]);
223 if (!$title) $title='&nbsp;';
224 echo " <tr><td colspan='5' align='center' class='fsgroup' style='vertical-align:middle' nowrap>$title</td></tr>\n";
226 else if ($cmd == '*H') {
227 $title = htmlspecialchars($a[1]);
228 if (!$title) $title='&nbsp;';
229 echo " <tr><td colspan='5' class='fshead' style='vertical-align:middle' nowrap>$title</td></tr>\n";
231 else {
232 $title = htmlspecialchars($a[1]);
233 if (!$title) $title='&nbsp;';
234 $b = explode(':', $cmd);
235 echo " <tr>\n";
236 echo " <td class='fscode' style='vertical-align:middle;width:14pt' nowrap>&nbsp;</td>\n";
237 if (count($b) <= 1) {
238 $code = $b[0];
239 if (!$code) $code='&nbsp;';
240 echo " <td class='fscode' style='vertical-align:middle' nowrap>$code</td>\n";
241 echo " <td colspan='3' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
243 else {
244 echo " <td colspan='2' class='fscode' style='vertical-align:middle' nowrap>" . $b[0] . '/' . $b[1] . "</td>\n";
245 echo " <td colspan='2' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
247 echo " </tr>\n";
250 return $ix;
253 $today = date('Y-m-d');
255 $alertmsg = ''; // anything here pops up in an alert box
257 // Get details for what we guess is the primary facility.
258 $frow = sqlQuery("SELECT * FROM facility " .
259 "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
261 if ($form_fill) {
262 // Get the patient's name and chart number.
263 $patdata = getPatientData($pid);
266 // This tracks our position in the $SBCODES array.
267 $cindex = 0;
270 <title><?php echo htmlspecialchars($frow['name']); ?></title>
271 <script type="text/javascript" src="../../library/dialog.js"></script>
272 <script language="JavaScript">
274 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
276 // Process click on Print button.
277 function printme() {
278 var divstyle = document.getElementById('hideonprint').style;
279 divstyle.display = 'none';
280 window.print();
283 </script>
284 </head>
285 <body bgcolor='#ffffff'>
286 <form name='theform' method='post' action='printed_fee_sheet.php?fill=<?php echo $form_fill; ?>'
287 onsubmit='return top.restoreSession()'>
288 <center>
290 <?php while (--$pages >= 0) { ?>
292 <?php echo genFacilityTitle(xl('Fee Sheet'), -1); ?>
294 <table class='bordertbl' cellspacing='0' cellpadding='0' width='100%'>
295 <tr>
296 <td valign='top'>
297 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
298 <tr>
299 <td class='toprow' style='width:10%'></td>
300 <td class='toprow' style='width:10%'></td>
301 <td class='toprow' style='width:25%'></td>
302 <td class='toprow' style='width:55%'></td>
303 </tr>
304 <?php
305 $cindex = genColumn($cindex); // Column 1
308 <?php if ($pages == 0) { // if this is the last page ?>
310 <tr>
311 <td colspan='3' valign='top' class='fshead' style='height:<?php echo $lheight * 2; ?>pt'>
312 Patient:<br />
313 <?php
314 if ($form_fill) {
315 echo $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'] . "<br />\n";
316 echo $patdata['street'] . "<br />\n";
317 echo $patdata['city'] . ', ' . $patdata['state'] . ' ' . $patdata['postal_code'] . "\n";
320 </td>
321 <td valign='top' class='fshead'>
322 DOB:<br /><?php if ($form_fill) echo $patdata['DOB']; ?><br />
323 ID:<br /><?php if ($form_fill) echo $patdata['pubpid']; ?>
324 </td>
325 </tr>
326 <tr>
327 <td colspan='3' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
328 Doctor:<br />
329 <?php
330 $encdata = false;
331 if ($form_fill && $encounter) {
332 $query = "SELECT fe.reason, fe.date, u.fname, u.mname, u.lname, u.username " .
333 "FROM forms AS f " .
334 "JOIN form_encounter AS fe ON fe.id = f.form_id " .
335 "LEFT JOIN users AS u ON u.username = f.user " .
336 "WHERE f.pid = '$pid' AND f.encounter = '$encounter' AND f.formdir = 'newpatient' AND f.deleted = 0 " .
337 "ORDER BY f.id LIMIT 1";
338 $encdata = sqlQuery($query);
339 if (!empty($encdata['username'])) {
340 echo $encdata['fname'] . ' ' . $encdata['mname'] . ' ' . $encdata['lname'];
344 </td>
345 <td valign='top' class='fshead'>
346 Reason:<br />
347 <?php
348 if (!empty($encdata)) {
349 echo $encdata['reason'];
352 </td>
353 </tr>
354 <tr>
355 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
356 <?php
357 if (empty($GLOBALS['ippf_specific'])) {
358 echo "Insurance:\n";
359 if ($form_fill) {
360 foreach (array('primary','secondary','tertiary') as $instype) {
361 $query = "SELECT * FROM insurance_data WHERE " .
362 "pid = '$pid' AND type = '$instype' " .
363 "ORDER BY date DESC LIMIT 1";
364 $row = sqlQuery($query);
365 if ($row['provider']) {
366 $icobj = new InsuranceCompany($row['provider']);
367 $adobj = $icobj->get_address();
368 $insco_name = trim($icobj->get_name());
369 if ($instype != 'primary') echo ",";
370 if ($insco_name) {
371 echo "&nbsp;$insco_name";
372 } else {
373 echo "&nbsp;<font color='red'><b>Missing Name</b></font>";
379 else {
380 // IPPF wants a visit date box with the current date in it.
381 echo "Visit date:<br />\n";
382 if (!empty($encdata)) {
383 echo substr($encdata['date'], 0, 10);
385 else {
386 echo date('Y-m-d') . "\n";
390 </td>
391 </tr>
392 <tr>
393 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
394 Prior balance:<br />
395 </td>
396 </tr>
397 <tr>
398 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
399 Today's charges:<br />
400 </td>
401 </tr>
402 <tr>
403 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
404 Today's payment:<br />
405 </td>
406 </tr>
407 <tr>
408 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight; ?>pt'>
409 Notes:<br />
410 </td>
411 </tr>
413 <?php } // end if last page ?>
415 </table>
416 </td>
417 <td valign='top'>
418 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
419 <tr>
420 <td class='toprow' style='width:10%'></td>
421 <td class='toprow' style='width:10%'></td>
422 <td class='toprow' style='width:25%'></td>
423 <td class='toprow' style='width:55%'></td>
424 </tr>
425 <?php
426 $cindex = genColumn($cindex); // Column 2
429 <?php if ($pages == 0) { // if this is the last page ?>
431 <tr>
432 <td colspan='4' valign='top' class='fshead' style='height:<?php echo $lheight * 8; ?>pt'>
433 Notes:<br />
434 </td>
435 </tr>
437 <?php } // end if last page ?>
439 </table>
440 </td>
441 <td valign='top'>
442 <table border='0' cellspacing='0' cellpadding='0' width='100%'>
443 <tr>
444 <td class='toprow' style='width:10%'></td>
445 <td class='toprow' style='width:10%'></td>
446 <td class='toprow' style='width:25%'></td>
447 <td class='toprow' style='width:55%'></td>
448 </tr>
449 <?php
450 $cindex = genColumn($cindex); // Column 3
453 <?php if ($pages == 0) { // if this is the last page ?>
455 <tr>
456 <td valign='top' colspan='4' class='fshead' style='height:<?php echo $lheight * 6; ?>pt;border-width:0 1px 0 0'>
457 &nbsp;
458 </td>
459 </tr>
460 <tr>
461 <td valign='top' colspan='4' class='fshead' style='height:<?php echo $lheight * 2; ?>pt'>
462 Signature:<br />
463 </td>
464 </tr>
466 <?php } // end if last page ?>
468 </table>
469 </td>
470 </tr>
472 </table>
474 <?php
475 if ($pages > 0) echo "<p style='page-break-after: always' />\n";
476 } // end while
478 <div id='hideonprint'>
480 <input type='button' value='<?php xl('Print','e'); ?>' onclick='printme()' />
482 </div>
483 </form>
484 </center>
485 </body>
486 </html>