Support for optional logging of print actions.
[openemr.git] / interface / reports / svc_code_financial_report.php
blob768d32450031ff35e41419266d60d65ace1c64f4
1 <?php
2 /**
3 * This is a report of Financial Summary by Service Code.
5 * This is a summary of service code charge/pay/adjust and balance,
6 * with the ability to pick "important" codes to either highlight or
7 * limit to list to. Important codes can be configured in
8 * Administration->Service section by assigning code with
9 * 'Service Reporting'.
11 * Copyright (C) 2006-2015 Rod Roark <rod@sunsetsystems.com>
13 * LICENSE: This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
24 * @package OpenEMR
25 * @author Visolve
26 * @link http://www.open-emr.org
29 $sanitize_all_escapes=true;
30 $fake_register_globals=false;
32 require_once("../globals.php");
33 require_once("$srcdir/patient.inc");
34 require_once("$srcdir/sql-ledger.inc");
35 require_once("$srcdir/acl.inc");
36 require_once("$srcdir/formatting.inc.php");
37 require_once "$srcdir/options.inc.php";
38 require_once "$srcdir/formdata.inc.php";
39 require_once "$srcdir/appointments.inc.php";
41 $grand_total_units = 0;
42 $grand_total_amt_billed = 0;
43 $grand_total_amt_paid = 0;
44 $grand_total_amt_adjustment = 0;
45 $grand_total_amt_balance = 0;
48 if (! acl_check('acct', 'rep')) die(xlt("Unauthorized access."));
50 $INTEGRATED_AR = $GLOBALS['oer_config']['ws_accounting']['enabled'] === 2;
52 if (!$INTEGRATED_AR) SLConnect();
54 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
55 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
56 $form_facility = $_POST['form_facility'];
57 $form_provider = $_POST['form_provider'];
59 if ($_POST['form_csvexport']) {
60 header("Pragma: public");
61 header("Expires: 0");
62 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
63 header("Content-Type: application/force-download");
64 header("Content-Disposition: attachment; filename=svc_financial_report_".attr($form_from_date)."--".attr($form_to_date).".csv");
65 header("Content-Description: File Transfer");
66 // CSV headers:
67 } // end export
68 else {
70 <html>
71 <head>
72 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
73 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
74 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.1.3.2.js"></script>
75 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js"></script>
76 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-ui.js"></script>
77 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
78 <?php html_header_show();?>
79 <style type="text/css">
80 /* specifically include & exclude from printing */
81 @media print {
82 #report_parameters {
83 visibility: hidden;
84 display: none;
86 #report_parameters_daterange {
87 visibility: visible;
88 display: inline;
90 #report_results {
91 margin-top: 30px;
95 /* specifically exclude some from the screen */
96 @media screen {
97 #report_parameters_daterange {
98 visibility: hidden;
99 display: none;
102 </style>
104 <title><?php echo xlt('Financial Summary by Service Code') ?></title>
106 <script language="JavaScript">
108 $(document).ready(function() {
109 var win = top.printLogSetup ? top : opener.top;
110 win.printLogSetup(document.getElementById('printbutton'));
113 </script>
115 </head>
116 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class="body_top">
117 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Financial Summary by Service Code'); ?></span>
118 <form method='post' action='svc_code_financial_report.php' id='theform'>
119 <div id="report_parameters">
120 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
121 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
122 <table>
123 <tr>
124 <td width='70%'>
125 <div style='float:left'>
126 <table class='text'>
127 <tr>
128 <td class='label'>
129 <?php echo xlt('Facility'); ?>:
130 </td>
131 <td>
132 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
133 </td>
134 <td><?php echo xlt('Provider'); ?>:</td>
135 <td><?php
136 // Build a drop-down list of providers.
138 $query = "SELECT id, lname, fname FROM users WHERE ".
139 "authorized = 1 ORDER BY lname, fname"; //(CHEMED) facility filter
140 $ures = sqlStatement($query);
141 echo " <select name='form_provider'>\n";
142 echo " <option value=''>-- " . xlt('All') . " --\n";
143 while ($urow = sqlFetchArray($ures)) {
144 $provid = $urow['id'];
145 echo " <option value='" . attr($provid) ."'";
146 if ($provid == $_POST['form_provider']) echo " selected";
147 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
149 echo " </select>\n";
151 </td>
152 </tr><tr>
153 <td colspan="2">
154 <?php echo xlt('From'); ?>:&nbsp;&nbsp;&nbsp;&nbsp;
155 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
156 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
157 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
158 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
159 title='<?php echo xla("Click here to choose a date"); ?>'>
160 </td>
161 <td class='label'>
162 <?php echo xlt('To'); ?>:
163 </td>
164 <td>
165 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
166 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
167 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
168 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
169 title='<?php echo xla("Click here to choose a date"); ?>'>
170 </td>
171 <td>
172 <input type='checkbox' name='form_details'<?php if ($_POST['form_details']) echo ' checked'; ?>>
173 <?php echo xlt('Important Codes'); ?>
174 </td>
175 </tr>
176 </table>
177 </div>
178 </td>
179 <td align='left' valign='middle' height="100%">
180 <table style='border-left:1px solid; width:100%; height:100%' >
181 <tr>
182 <td>
183 <div style='margin-left:15px'>
184 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
185 <span>
186 <?php echo xlt('Submit'); ?>
187 </span>
188 </a>
190 <?php if ($_POST['form_refresh'] || $_POST['form_csvexport']) { ?>
191 <div id="controls">
192 <a href='#' class='css_button' id='printbutton'>
193 <span>
194 <?php echo xlt('Print'); ?>
195 </span>
196 </a>
197 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
198 <span>
199 <?php echo xlt('CSV Export'); ?>
200 </span>
201 </a>
202 </div>
203 <?php } ?>
204 </div>
205 </td>
206 </tr>
207 </table>
208 </td>
209 </tr>
210 </table>
211 </div> <!-- end of parameters -->
213 <?php
215 // end not export
217 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
218 $rows = array();
219 $from_date = $form_from_date;
220 $to_date = $form_to_date;
221 $sqlBindArray = array();
222 if ($INTEGRATED_AR) {
223 $query = "select b.code,sum(b.units) as units,sum(b.fee) as billed,sum(ar_act.paid) as PaidAmount, " .
224 "sum(ar_act.adjust) as AdjustAmount,(sum(b.fee)-(sum(ar_act.paid)+sum(ar_act.adjust))) as Balance, " .
225 "c.financial_reporting " .
226 "FROM form_encounter as fe " .
227 "JOIN billing as b on b.pid=fe.pid and b.encounter=fe.encounter " .
228 "JOIN (select pid,encounter,code,sum(pay_amount) as paid,sum(adj_amount) as adjust from ar_activity group by pid,encounter,code) as ar_act " .
229 "ON ar_act.pid=b.pid and ar_act.encounter=b.encounter and ar_act.code=b.code " .
230 "LEFT OUTER JOIN codes AS c ON c.code = b.code " .
231 "INNER JOIN code_types AS ct ON ct.ct_key = b.code_type AND ct.ct_fee = '1' " .
232 "WHERE b.code_type != 'COPAY' AND b.activity = 1 /* AND b.fee != 0 */ AND " .
233 "fe.date >= ? AND fe.date <= ?";
234 array_push($sqlBindArray,"$from_date 00:00:00","$to_date 23:59:59");
235 // If a facility was specified.
236 if ($form_facility) {
237 $query .= " AND fe.facility_id = ?";
238 array_push($sqlBindArray,$form_facility);
240 // If a provider was specified.
241 if ($form_provider) {
242 $query .= " AND b.provider_id = ?";
243 array_push($sqlBindArray,$form_provider);
245 // If selected important codes
246 if($_POST['form_details']) {
247 $query .= " AND c.financial_reporting = '1'";
249 $query .= " GROUP BY b.code ORDER BY b.code, fe.date, fe.id ";
250 $res = sqlStatement($query,$sqlBindArray);
251 $grand_total_units = 0;
252 $grand_total_amt_billed = 0;
253 $grand_total_amt_paid = 0;
254 $grand_total_amt_adjustment = 0;
255 $grand_total_amt_balance = 0;
257 while ($erow = sqlFetchArray($res)) {
258 $row = array();
259 $row['pid'] = $erow['pid'];
260 $row['provider_id'] = $erow['provider_id'];
261 $row['Procedure codes'] = $erow['code'];
262 $row['Units'] = $erow['units'];
263 $row['Amt Billed'] = $erow['billed'];
264 $row['Paid Amt'] = $erow['PaidAmount'];
265 $row['Adjustment Amt'] = $erow['AdjustAmount'];
266 $row['Balance Amt'] = $erow['Balance'];
267 $row['financial_reporting'] = $erow['financial_reporting'];
268 $rows[$erow['pid'] . '|' . $erow['code'] . '|' . $erow['units']] = $row;
270 if ($_POST['form_csvexport']) {
271 // CSV headers:
272 if (true) {
273 echo '"Procedure codes",';
274 echo '"Units",';
275 echo '"Amt Billed",';
276 echo '"Paid Amt",';
277 echo '"Adjustment Amt",';
278 echo '"Balance Amt",' . "\n";
280 } else {
281 ?> <div id="report_results">
282 <table >
283 <thead>
284 <th>
285 <?php echo xlt('Procedure Codes'); ?>
286 </th>
287 <th >
288 <?php echo xlt('Units'); ?>
289 </th>
290 <th>
291 <?php echo xlt('Amt Billed'); ?>
292 </th>
293 <th>
294 <?php echo xlt('Paid Amt'); ?>
295 </th>
296 <th >
297 <?php echo xlt('Adjustment Amt'); ?>
298 </th>
299 <th >
300 <?php echo xlt('Balance Amt'); ?>
301 </th>
302 </thead>
303 <?php
305 $orow = -1;
307 foreach ($rows as $key => $row) {
308 $print = '';
309 $csv = '';
311 if($row['financial_reporting']){ $bgcolor = "#FFFFDD"; }else { $bgcolor = "#FFDDDD"; }
312 $print = "<tr bgcolor='$bgcolor'><td class='detail'>".text($row['Procedure codes'])."</td><td class='detail'>".text($row['Units'])."</td><td class='detail'>".text(oeFormatMoney($row['Amt Billed']))."</td><td class='detail'>".text(oeFormatMoney($row['Paid Amt']))."</td><td class='detail'>".text(oeFormatMoney($row['Adjustment Amt']))."</td><td class='detail'>".text(oeFormatMoney($row['Balance Amt']))."</td>";
314 $csv = '"' . text($row['Procedure codes']) . '","' . text($row['Units']) . '","' . text(oeFormatMoney($row['Amt Billed'])) . '","' . text(oeFormatMoney($row['Paid Amt'])) . '","' . text(oeFormatMoney($row['Adjustment Amt'])) . '","' . text(oeFormatMoney($row['Balance Amt'])) . '"' . "\n";
316 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
317 $grand_total_units += $row['Units'];
318 $grand_total_amt_billed += $row['Amt Billed'];
319 $grand_total_amt_paid += $row['Paid Amt'];
320 $grand_total_amt_adjustment += $row['Adjustment Amt'];
321 $grand_total_amt_balance += $row['Balance Amt'];
323 if ($_POST['form_csvexport']) { echo $csv; }
324 else { echo $print;
327 if (!$_POST['form_csvexport']) {
328 echo "<tr bgcolor='#ffffff'>\n";
329 echo " <td class='detail'>" . xlt("Grand Total") . "</td>\n";
330 echo " <td class='detail'>" . text($grand_total_units) . "</td>\n";
331 echo " <td class='detail'>" .
332 text(oeFormatMoney($grand_total_amt_billed)) . "</td>\n";
333 echo " <td class='detail'>" .
334 text(oeFormatMoney($grand_total_amt_paid)) . "</td>\n";
335 echo " <td class='detail'>" .
336 text(oeFormatMoney($grand_total_amt_adjustment)) . "</td>\n";
337 echo " <td class='detail'>" .
338 text(oeFormatMoney($grand_total_amt_balance)) . "</td>\n";
339 echo " </tr>\n";
341 </table> </div>
342 <?php
347 if (! $_POST['form_csvexport']) {
348 if ( $_POST['form_refresh'] && count($print) != 1)
350 echo "<span style='font-size:10pt;'>";
351 echo xlt('No matches found. Try search again.');
352 echo "</span>";
353 echo '<script>document.getElementById("report_results").style.display="none";</script>';
354 echo '<script>document.getElementById("controls").style.display="none";</script>';
357 if (!$_POST['form_refresh'] && !$_POST['form_csvexport']) { ?>
358 <div class='text'>
359 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
360 </div>
361 <?php } ?>
362 </form>
363 </body>
365 <!-- stuff for the popup calendar -->
367 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
368 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
369 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
370 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
371 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
372 <script language="Javascript">
373 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
374 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
375 top.restoreSession();
376 </script>
377 </html>
378 <?php
379 } // End not csv export