added support for multiple pages
[openemr.git] / interface / patient_file / printed_fee_sheet.php
blob12ab481a99739673f92894489ae49cdde8a13716
1 <?php
2 // Copyright (C) 2007-2008 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 $lines_per_page = 55;
16 $lines_in_stats = 16;
18 // This file is optional. You can create it to customize how the printed
19 // fee sheet looks, otherwise you'll get a mirror of your actual fee sheet.
21 if (file_exists("../../custom/fee_sheet_codes.php"))
22 include_once ("../../custom/fee_sheet_codes.php");
24 $fontsize = trim($_REQUEST['fontsize']) + 0;
25 if (!$fontsize) $fontsize = 7;
26 $page_height = trim($_REQUEST['page_height']) + 0;
27 if (!$page_height) $page_height = 700;
28 $padding = 0;
30 // The $SBCODES table is a simple indexed array whose values are
31 // strings of the form "code|text" where code may be either a billing
32 // code or one of the following:
34 // *H - A main heading, where "text" is its title (to be centered).
35 // *G - Specifies a new category, where "text" is its name.
36 // *B - A borderless blank row.
37 // *C - Ends the current column and starts a new one.
39 // If $SBCODES is not provided, then manufacture it from the Fee Sheet.
41 if (empty($SBCODES)) {
42 $SBCODES = array();
43 $last_category = '';
45 // Create entries based on the fee_sheet_options table.
46 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
47 "ORDER BY fs_category, fs_option");
48 while ($row = sqlFetchArray($res)) {
49 $fs_category = $row['fs_category'];
50 $fs_option = $row['fs_option'];
51 $fs_codes = $row['fs_codes'];
52 if($fs_category !== $last_category) {
53 $last_category = $fs_category;
54 $SBCODES[] = '*G|' . substr($fs_category, 1);
56 $SBCODES[] = " |" . substr($fs_option, 1);
59 // Create entries based on categories defined within the codes.
60 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
61 "WHERE list_id = 'superbill' ORDER BY seq");
62 while ($prow = sqlFetchArray($pres)) {
63 $SBCODES[] = '*G|' . $prow['title'];
64 $res = sqlStatement("SELECT code_type, code, code_text FROM codes " .
65 "WHERE superbill = '" . $prow['option_id'] . "' " .
66 "ORDER BY code_text");
67 while ($row = sqlFetchArray($res)) {
68 $SBCODES[] = $row['code'] . '|' . $row['code_text'];
72 // Create one more group, for Products.
73 if ($GLOBALS['sell_non_drug_products']) {
74 $SBCODES[] = '*G|' . xl('Products');
75 $tres = sqlStatement("SELECT dt.drug_id, dt.selector, d.name " .
76 "FROM drug_templates AS dt, drugs AS d WHERE " .
77 "d.drug_id = dt.drug_id " .
78 "ORDER BY d.name, dt.selector, dt.drug_id");
79 while ($trow = sqlFetchArray($tres)) {
80 $tmp = $trow['selector'];
81 if ($trow['name'] !== $trow['selector']) $tmp .= ' ' . $trow['name'];
82 $SBCODES[] = $trow['drug_id'] . '|' . $tmp;
86 // Extra stuff for the labs section.
87 $SBCODES[] = '*G|' . xl('Additional Labs');
88 $percol = intval((count($SBCODES) + 2) / 3);
89 while (count($SBCODES) < $percol * 3) $SBCODES[] = '*B|';
91 // Adjust lines per page to distribute lines evenly among the pages.
92 $pages = intval(($percol + $lines_in_stats + $lines_per_page - 1) / $lines_per_page);
93 $lines_per_page = intval($percol + $lines_in_stats + $pages - 1) / $pages;
95 // Figure out page and column breaks.
96 $pages = 1;
97 $lines = $percol;
98 $page_start_index = 0;
99 while ($lines + $lines_in_stats > $lines_per_page) {
100 ++$pages;
101 $lines_this_page = $lines > $lines_per_page ? $lines_per_page : $lines;
102 $lines -= $lines_this_page;
103 array_splice($SBCODES, $lines_this_page*3 + $page_start_index, 0, '*C|');
104 array_splice($SBCODES, $lines_this_page*2 + $page_start_index, 0, '*C|');
105 array_splice($SBCODES, $lines_this_page*1 + $page_start_index, 0, '*C|');
106 $page_start_index += $lines_this_page * 3 + 3;
108 array_splice($SBCODES, $lines*2 + $page_start_index, 0, '*C|');
109 array_splice($SBCODES, $lines*1 + $page_start_index, 0, '*C|');
112 <html>
113 <head>
114 <?php html_header_show(); ?>
116 <style>
117 body {
118 font-family: sans-serif;
119 font-weight: normal;
121 .bordertbl {
122 width: 100%;
123 border-style: solid;
124 border-width: 0 0 1px 1px;
125 border-spacing: 0;
126 border-collapse: collapse;
127 border-color: #999999;
129 td.toprow {
130 height: 1px;
131 padding: 0;
132 border-style: solid;
133 border-width: 0 0 0 0;
134 border-color: #999999;
136 td.fsgroup {
137 font-family: sans-serif;
138 font-weight: bold;
139 font-size: <?php echo $fontsize ?>pt;
140 background-color: #cccccc;
141 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
142 border-style: solid;
143 border-width: 1px 1px 0 0;
144 border-color: #999999;
146 td.fshead {
147 font-family: sans-serif;
148 font-weight: bold;
149 font-size: <?php echo $fontsize ?>pt;
150 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
151 border-style: solid;
152 border-width: 1px 1px 0 0;
153 border-color: #999999;
155 td.fscode {
156 font-family: sans-serif;
157 font-weight: normal;
158 font-size: <?php echo $fontsize ?>pt;
159 padding: <?php echo $padding ?>pt 2pt 0pt 2pt;
160 border-style: solid;
161 border-width: 1px 1px 0 0;
162 border-color: #999999;
164 </style>
166 <?php
168 // Get the co-pay amount that is effective on the given date.
169 // Or if no insurance on that date, return -1.
171 function getCopay($patient_id, $encdate) {
172 $tmp = sqlQuery("SELECT provider, copay FROM insurance_data " .
173 "WHERE pid = '$patient_id' AND type = 'primary' " .
174 "AND date <= '$encdate' ORDER BY date DESC LIMIT 1");
175 if ($tmp['provider']) return sprintf('%01.2f', 0 + $tmp['copay']);
176 return -1;
179 function genColumn($ix) {
180 global $SBCODES;
181 for ($imax = count($SBCODES); $ix < $imax; ++$ix) {
182 $a = explode('|', $SBCODES[$ix], 2);
183 $cmd = trim($a[0]);
184 if ($cmd == '*C') { // column break
185 return ++$ix;
187 if ($cmd == '*B') { // Borderless and empty
188 echo "<tr><td colspan='5' class='fscode' style='border-width:0 1px 0 0;padding-top:1px;' nowrap>&nbsp;</td></tr>\n";
190 else if ($cmd == '*G') {
191 // $title = htmlentities(strtoupper($a[1]));
192 $title = htmlentities($a[1]);
193 if (!$title) $title='&nbsp;';
194 echo "<tr><td colspan='5' align='center' class='fsgroup' style='vertical-align:middle' nowrap>$title</td></tr>\n";
196 else if ($cmd == '*H') {
197 // $title = htmlentities(strtoupper($a[1]));
198 $title = htmlentities($a[1]);
199 if (!$title) $title='&nbsp;';
200 echo "<tr><td colspan='5' class='fshead' style='vertical-align:middle' nowrap>$title</td></tr>\n";
202 else {
203 $title = htmlentities($a[1]);
204 if (!$title) $title='&nbsp;';
205 $b = explode(':', $cmd);
206 echo "<tr>";
207 echo "<td class='fscode' style='vertical-align:middle;width:14pt' nowrap>&nbsp;</td>\n";
208 if (count($b) <= 1) {
209 $code = $b[0];
210 if (!$code) $code='&nbsp;';
211 echo "<td class='fscode' style='vertical-align:middle' nowrap>$code</td>\n";
212 echo "<td colspan='3' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
214 else {
215 echo "<td colspan='2' class='fscode' style='vertical-align:middle' nowrap>" . $b[0] . '/' . $b[1] . "</td>\n";
216 echo "<td colspan='2' class='fscode' style='vertical-align:middle' nowrap>$title</td>\n";
218 echo "</tr>\n";
221 return $ix;
224 $today = date('Y-m-d');
226 $alertmsg = ''; // anything here pops up in an alert box
228 // Get details for what we guess is the primary facility.
229 $frow = sqlQuery("SELECT * FROM facility " .
230 "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
232 // Get the patient's name and chart number.
233 $patdata = getPatientData($pid);
235 // This tracks our position in the $SBCODES array.
236 $cindex = 0;
239 <title><?php echo htmlentities($frow['name']); ?></title>
240 <script type="text/javascript" src="../../library/dialog.js"></script>
241 <script language="JavaScript">
243 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
245 // Process click on Print button.
246 function printme() {
247 var divstyle = document.getElementById('hideonprint').style;
248 divstyle.display = 'none';
249 window.print();
252 </script>
253 </head>
254 <body bgcolor='#ffffff'>
255 <form name='theform' method='post' action='printed_fee_sheet.php'
256 onsubmit='return top.restoreSession()'>
257 <center>
259 <?php while (--$pages >= 0) { ?>
261 <table class='bordertbl' cellspacing='0' cellpadding='0' width='100%'>
262 <tr>
263 <td valign='top'>
264 <table border='0' cellspacing='0' cellpadding='0' width='100%' style='height:<?php echo $page_height ?>pt'>
265 <tr>
266 <td class='toprow' style='width:10%'></td>
267 <td class='toprow' style='width:10%'></td>
268 <td class='toprow' style='width:25%'></td>
269 <td class='toprow' style='width:55%'></td>
270 </tr>
271 <?php
272 $cindex = genColumn($cindex); // Column 1
275 <?php if ($pages == 0) { // if this is the last page ?>
277 <tr>
278 <td colspan='3' valign='top' class='fshead' style='height:50pt'>
279 Patient:<br>
280 <?php
281 echo $patdata['fname'] . ' ' . $patdata['mname'] . ' ' . $patdata['lname'] . "<br>\n";
282 echo $patdata['street'] . "<br>\n";
283 echo $patdata['city'] . ', ' . $patdata['state'] . ' ' . $patdata['postal_code'] . "\n";
285 </td>
286 <td valign='top' class='fshead'>
287 DOB:<br><?php echo $patdata['DOB']; ?><br>
288 ID:<br><?php echo $patdata['pubpid']; ?>
289 </td>
290 </tr>
291 <tr>
292 <td colspan='3' valign='top' class='fshead' style='height:25pt'>
293 Doctor:&nbsp;
294 <?php // Doctor name here ?>
295 </td>
296 <td valign='top' class='fshead'>
297 Reason:&nbsp;
298 <?php // Encounter reason here ?>
299 </td>
300 </tr>
301 <tr>
302 <td colspan='4' valign='top' class='fshead' style='height:25pt'>
303 <?php
304 if (empty($GLOBALS['ippf_specific'])) {
305 echo "Insurance:\n";
306 foreach (array('primary','secondary','tertiary') as $instype) {
307 $query = "SELECT * FROM insurance_data WHERE " .
308 "pid = '$pid' AND type = '$instype' " .
309 "ORDER BY date DESC LIMIT 1";
310 $row = sqlQuery($query);
311 if ($row['provider']) {
312 $icobj = new InsuranceCompany($row['provider']);
313 $adobj = $icobj->get_address();
314 $insco_name = trim($icobj->get_name());
315 if ($instype != 'primary') echo ",";
316 if ($insco_name) {
317 echo "&nbsp;$insco_name";
318 } else {
319 echo "&nbsp;<font color='red'><b>Missing Name</b></font>";
324 else {
325 // IPPF wants a visit date box with the current date in it.
326 echo "Visit date:<br>\n";
327 echo date('Y-m-d') . "\n";
330 </td>
331 </tr>
332 <tr>
333 <td colspan='4' valign='top' class='fshead' style='height:25pt'>
334 Prior balance:<br>
335 </td>
336 </tr>
337 <tr>
338 <td colspan='4' valign='top' class='fshead' style='height:25pt'>
339 Today's charges:<br>
340 </td>
341 </tr>
342 <tr>
343 <td colspan='4' valign='top' class='fshead' style='height:25pt'>
344 Today's payment:<br>
345 </td>
346 </tr>
347 <tr>
348 <td colspan='4' valign='top' class='fshead' style='height:25pt'>
349 Notes:<br>
350 </td>
351 </tr>
353 <?php } // end if last page ?>
355 </table>
356 </td>
357 <td valign='top'>
358 <table border='0' cellspacing='0' cellpadding='0' width='100%' style='height:<?php echo $page_height ?>pt'>
359 <tr>
360 <td class='toprow' style='width:10%'></td>
361 <td class='toprow' style='width:10%'></td>
362 <td class='toprow' style='width:25%'></td>
363 <td class='toprow' style='width:55%'></td>
364 </tr>
365 <?php
366 $cindex = genColumn($cindex); // Column 2
369 <?php if ($pages == 0) { // if this is the last page ?>
371 <tr>
372 <td colspan='4' valign='top' class='fshead' style='height:100pt'>
373 Additional procedures:<br>
374 </td>
375 </tr>
376 <tr>
377 <td colspan='4' valign='top' class='fshead' style='height:100pt'>
378 Diagnosis:<br>
379 </td>
380 </tr>
382 <?php } // end if last page ?>
384 </table>
385 </td>
386 <td valign='top'>
387 <table border='0' cellspacing='0' cellpadding='0' width='100%' style='height:<?php echo $page_height ?>pt'>
388 <tr>
389 <td class='toprow' style='width:10%'></td>
390 <td class='toprow' style='width:10%'></td>
391 <td class='toprow' style='width:25%'></td>
392 <td class='toprow' style='width:55%'></td>
393 </tr>
394 <?php
395 $cindex = genColumn($cindex); // Column 3
398 <?php if ($pages == 0) { // if this is the last page ?>
400 <tr>
401 <td valign='top' colspan='4' class='fshead' style='height:150pt;border-width:0 1px 0 0'>
402 &nbsp;
403 </td>
404 </tr>
405 <tr>
406 <td valign='top' colspan='4' class='fshead' style='height:50pt'>
407 M.D. Signature:<br>
408 </td>
409 </tr>
411 <?php } // end if last page ?>
413 </table>
414 </td>
415 </tr>
417 </table>
419 <?php } // end while ?>
421 <div id='hideonprint'>
423 <input type='button' value='<?php xl('Print','e'); ?>' onclick='printme()' />
424 &nbsp;&nbsp;Font Size in Points:
425 <input type='text' name='fontsize' size='2' maxlength='5' value='<?php echo $fontsize ?>' />
426 &nbsp;&nbsp;Page Height in Points:
427 <input type='text' name='page_height' size='3' maxlength='4' value='<?php echo $page_height ?>' />
428 &nbsp;&nbsp;
429 <input type='submit' name='form_submit' value='Refresh' />
430 </div>
432 </form>
433 </center>
434 </body>
435 </html>