Removed sql-ledger, take 3.
[openemr.git] / interface / reports / svc_code_financial_report.php
blob4204442f599f972a5db074311e800f07604d94e7
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/acl.inc");
35 require_once("$srcdir/formatting.inc.php");
36 require_once "$srcdir/options.inc.php";
37 require_once "$srcdir/formdata.inc.php";
38 require_once "$srcdir/appointments.inc.php";
40 $grand_total_units = 0;
41 $grand_total_amt_billed = 0;
42 $grand_total_amt_paid = 0;
43 $grand_total_amt_adjustment = 0;
44 $grand_total_amt_balance = 0;
47 if (! acl_check('acct', 'rep')) die(xlt("Unauthorized access."));
49 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
50 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
51 $form_facility = $_POST['form_facility'];
52 $form_provider = $_POST['form_provider'];
54 if ($_POST['form_csvexport']) {
55 header("Pragma: public");
56 header("Expires: 0");
57 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
58 header("Content-Type: application/force-download");
59 header("Content-Disposition: attachment; filename=svc_financial_report_".attr($form_from_date)."--".attr($form_to_date).".csv");
60 header("Content-Description: File Transfer");
61 // CSV headers:
62 } // end export
63 else {
65 <html>
66 <head>
67 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
68 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
69 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.1.3.2.js"></script>
70 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js"></script>
71 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-ui.js"></script>
72 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
73 <?php html_header_show();?>
74 <style type="text/css">
75 /* specifically include & exclude from printing */
76 @media print {
77 #report_parameters {
78 visibility: hidden;
79 display: none;
81 #report_parameters_daterange {
82 visibility: visible;
83 display: inline;
85 #report_results {
86 margin-top: 30px;
90 /* specifically exclude some from the screen */
91 @media screen {
92 #report_parameters_daterange {
93 visibility: hidden;
94 display: none;
97 </style>
99 <title><?php echo xlt('Financial Summary by Service Code') ?></title>
101 <script language="JavaScript">
103 $(document).ready(function() {
104 var win = top.printLogSetup ? top : opener.top;
105 win.printLogSetup(document.getElementById('printbutton'));
108 </script>
110 </head>
111 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' class="body_top">
112 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Financial Summary by Service Code'); ?></span>
113 <form method='post' action='svc_code_financial_report.php' id='theform'>
114 <div id="report_parameters">
115 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
116 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
117 <table>
118 <tr>
119 <td width='70%'>
120 <div style='float:left'>
121 <table class='text'>
122 <tr>
123 <td class='label'>
124 <?php echo xlt('Facility'); ?>:
125 </td>
126 <td>
127 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
128 </td>
129 <td><?php echo xlt('Provider'); ?>:</td>
130 <td><?php
131 // Build a drop-down list of providers.
133 $query = "SELECT id, lname, fname FROM users WHERE ".
134 "authorized = 1 ORDER BY lname, fname"; //(CHEMED) facility filter
135 $ures = sqlStatement($query);
136 echo " <select name='form_provider'>\n";
137 echo " <option value=''>-- " . xlt('All') . " --\n";
138 while ($urow = sqlFetchArray($ures)) {
139 $provid = $urow['id'];
140 echo " <option value='" . attr($provid) ."'";
141 if ($provid == $_POST['form_provider']) echo " selected";
142 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
144 echo " </select>\n";
146 </td>
147 </tr><tr>
148 <td colspan="2">
149 <?php echo xlt('From'); ?>:&nbsp;&nbsp;&nbsp;&nbsp;
150 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>'
151 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
152 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
153 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
154 title='<?php echo xla("Click here to choose a date"); ?>'>
155 </td>
156 <td class='label'>
157 <?php echo xlt('To'); ?>:
158 </td>
159 <td>
160 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
161 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
162 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
163 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
164 title='<?php echo xla("Click here to choose a date"); ?>'>
165 </td>
166 <td>
167 <input type='checkbox' name='form_details'<?php if ($_POST['form_details']) echo ' checked'; ?>>
168 <?php echo xlt('Important Codes'); ?>
169 </td>
170 </tr>
171 </table>
172 </div>
173 </td>
174 <td align='left' valign='middle' height="100%">
175 <table style='border-left:1px solid; width:100%; height:100%' >
176 <tr>
177 <td>
178 <div style='margin-left:15px'>
179 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
180 <span>
181 <?php echo xlt('Submit'); ?>
182 </span>
183 </a>
185 <?php if ($_POST['form_refresh'] || $_POST['form_csvexport']) { ?>
186 <div id="controls">
187 <a href='#' class='css_button' id='printbutton'>
188 <span>
189 <?php echo xlt('Print'); ?>
190 </span>
191 </a>
192 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
193 <span>
194 <?php echo xlt('CSV Export'); ?>
195 </span>
196 </a>
197 </div>
198 <?php } ?>
199 </div>
200 </td>
201 </tr>
202 </table>
203 </td>
204 </tr>
205 </table>
206 </div> <!-- end of parameters -->
208 <?php
210 // end not export
212 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
213 $rows = array();
214 $from_date = $form_from_date;
215 $to_date = $form_to_date;
216 $sqlBindArray = array();
217 $query = "select b.code,sum(b.units) as units,sum(b.fee) as billed,sum(ar_act.paid) as PaidAmount, " .
218 "sum(ar_act.adjust) as AdjustAmount,(sum(b.fee)-(sum(ar_act.paid)+sum(ar_act.adjust))) as Balance, " .
219 "c.financial_reporting " .
220 "FROM form_encounter as fe " .
221 "JOIN billing as b on b.pid=fe.pid and b.encounter=fe.encounter " .
222 "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 " .
223 "ON ar_act.pid=b.pid and ar_act.encounter=b.encounter and ar_act.code=b.code " .
224 "LEFT OUTER JOIN codes AS c ON c.code = b.code " .
225 "INNER JOIN code_types AS ct ON ct.ct_key = b.code_type AND ct.ct_fee = '1' " .
226 "WHERE b.code_type != 'COPAY' AND b.activity = 1 /* AND b.fee != 0 */ AND " .
227 "fe.date >= ? AND fe.date <= ?";
228 array_push($sqlBindArray,"$from_date 00:00:00","$to_date 23:59:59");
229 // If a facility was specified.
230 if ($form_facility) {
231 $query .= " AND fe.facility_id = ?";
232 array_push($sqlBindArray,$form_facility);
234 // If a provider was specified.
235 if ($form_provider) {
236 $query .= " AND b.provider_id = ?";
237 array_push($sqlBindArray,$form_provider);
239 // If selected important codes
240 if($_POST['form_details']) {
241 $query .= " AND c.financial_reporting = '1'";
243 $query .= " GROUP BY b.code ORDER BY b.code, fe.date, fe.id ";
244 $res = sqlStatement($query,$sqlBindArray);
245 $grand_total_units = 0;
246 $grand_total_amt_billed = 0;
247 $grand_total_amt_paid = 0;
248 $grand_total_amt_adjustment = 0;
249 $grand_total_amt_balance = 0;
251 while ($erow = sqlFetchArray($res)) {
252 $row = array();
253 $row['pid'] = $erow['pid'];
254 $row['provider_id'] = $erow['provider_id'];
255 $row['Procedure codes'] = $erow['code'];
256 $row['Units'] = $erow['units'];
257 $row['Amt Billed'] = $erow['billed'];
258 $row['Paid Amt'] = $erow['PaidAmount'];
259 $row['Adjustment Amt'] = $erow['AdjustAmount'];
260 $row['Balance Amt'] = $erow['Balance'];
261 $row['financial_reporting'] = $erow['financial_reporting'];
262 $rows[$erow['pid'] . '|' . $erow['code'] . '|' . $erow['units']] = $row;
264 if ($_POST['form_csvexport']) {
265 // CSV headers:
266 if (true) {
267 echo '"Procedure codes",';
268 echo '"Units",';
269 echo '"Amt Billed",';
270 echo '"Paid Amt",';
271 echo '"Adjustment Amt",';
272 echo '"Balance Amt",' . "\n";
274 } else {
275 ?> <div id="report_results">
276 <table >
277 <thead>
278 <th>
279 <?php echo xlt('Procedure Codes'); ?>
280 </th>
281 <th >
282 <?php echo xlt('Units'); ?>
283 </th>
284 <th>
285 <?php echo xlt('Amt Billed'); ?>
286 </th>
287 <th>
288 <?php echo xlt('Paid Amt'); ?>
289 </th>
290 <th >
291 <?php echo xlt('Adjustment Amt'); ?>
292 </th>
293 <th >
294 <?php echo xlt('Balance Amt'); ?>
295 </th>
296 </thead>
297 <?php
299 $orow = -1;
301 foreach ($rows as $key => $row) {
302 $print = '';
303 $csv = '';
305 if($row['financial_reporting']){ $bgcolor = "#FFFFDD"; }else { $bgcolor = "#FFDDDD"; }
306 $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>";
308 $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";
310 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
311 $grand_total_units += $row['Units'];
312 $grand_total_amt_billed += $row['Amt Billed'];
313 $grand_total_amt_paid += $row['Paid Amt'];
314 $grand_total_amt_adjustment += $row['Adjustment Amt'];
315 $grand_total_amt_balance += $row['Balance Amt'];
317 if ($_POST['form_csvexport']) { echo $csv; }
318 else { echo $print;
321 if (!$_POST['form_csvexport']) {
322 echo "<tr bgcolor='#ffffff'>\n";
323 echo " <td class='detail'>" . xlt("Grand Total") . "</td>\n";
324 echo " <td class='detail'>" . text($grand_total_units) . "</td>\n";
325 echo " <td class='detail'>" .
326 text(oeFormatMoney($grand_total_amt_billed)) . "</td>\n";
327 echo " <td class='detail'>" .
328 text(oeFormatMoney($grand_total_amt_paid)) . "</td>\n";
329 echo " <td class='detail'>" .
330 text(oeFormatMoney($grand_total_amt_adjustment)) . "</td>\n";
331 echo " <td class='detail'>" .
332 text(oeFormatMoney($grand_total_amt_balance)) . "</td>\n";
333 echo " </tr>\n";
335 </table> </div>
336 <?php
340 if (! $_POST['form_csvexport']) {
341 if ( $_POST['form_refresh'] && count($print) != 1)
343 echo "<span style='font-size:10pt;'>";
344 echo xlt('No matches found. Try search again.');
345 echo "</span>";
346 echo '<script>document.getElementById("report_results").style.display="none";</script>';
347 echo '<script>document.getElementById("controls").style.display="none";</script>';
350 if (!$_POST['form_refresh'] && !$_POST['form_csvexport']) { ?>
351 <div class='text'>
352 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
353 </div>
354 <?php } ?>
355 </form>
356 </body>
358 <!-- stuff for the popup calendar -->
360 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
361 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
362 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
363 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
364 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
365 <script language="Javascript">
366 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
367 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
368 top.restoreSession();
369 </script>
370 </html>
371 <?php
372 } // End not csv export