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
11 * Copyright (C) 2006-2016 Rod Roark <rod@sunsetsystems.com>
12 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
14 * LICENSE: This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
27 * @author Brady Miller <brady.g.miller@gmail.com>
28 * @link http://www.open-emr.org
31 $sanitize_all_escapes=true;
32 $fake_register_globals=false;
34 require_once("../globals.php");
35 require_once("$srcdir/patient.inc");
36 require_once("$srcdir/acl.inc");
37 require_once "$srcdir/options.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");
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");
67 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
68 <link rel
="stylesheet" href
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
70 <?php
html_header_show();?
>
72 <style type
="text/css">
73 /* specifically include & exclude from printing */
79 #report_parameters_daterange {
88 /* specifically exclude some from the screen */
90 #report_parameters_daterange {
97 <script type
="text/javascript" src
="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script
>
98 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script
>
99 <script type
="text/javascript" src
="../../library/js/common.js?v=<?php echo $v_js_includes; ?>"></script
>
100 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
101 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script
>
103 <title
><?php
echo xlt('Financial Summary by Service Code') ?
></title
>
105 <script language
="JavaScript">
107 $
(document
).ready(function() {
108 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
109 var win
= top
.printLogSetup ? top
: opener
.top
;
110 win
.printLogSetup(document
.getElementById('printbutton'));
112 $
('.datepicker').datetimepicker({
113 <?php
$datetimepicker_timepicker = false; ?
>
114 <?php
$datetimepicker_formatInput = false; ?
>
115 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
116 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
124 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0' class="body_top">
125 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Financial Summary by Service Code'); ?
></span
>
126 <form method
='post' action
='svc_code_financial_report.php' id
='theform'>
127 <div id
="report_parameters">
128 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
129 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
133 <div style
='float:left'>
137 <?php
echo xlt('Facility'); ?
>:
140 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
142 <td
><?php
echo xlt('Provider'); ?
>:</td
>
144 // Build a drop-down list of providers.
146 $query = "SELECT id, lname, fname FROM users WHERE ".
147 "authorized = 1 ORDER BY lname, fname"; //(CHEMED) facility filter
148 $ures = sqlStatement($query);
149 echo " <select name='form_provider'>\n";
150 echo " <option value=''>-- " . xlt('All') . " --\n";
151 while ($urow = sqlFetchArray($ures)) {
152 $provid = $urow['id'];
153 echo " <option value='" . attr($provid) ."'";
154 if ($provid == $_POST['form_provider']) echo " selected";
155 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
162 <?php
echo xlt('From'); ?
>: 
; 
; 
; 
;
163 <input type
='text' class='datepicker' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date) ?>'
167 <?php
echo xlt('To'); ?
>:
170 <input type
='text' class='datepicker' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
174 <input type
='checkbox' name
='form_details'<?php
if ($_POST['form_details']) echo ' checked'; ?
>>
175 <?php
echo xlt('Important Codes'); ?
>
181 <td align
='left' valign
='middle' height
="100%">
182 <table style
='border-left:1px solid; width:100%; height:100%' >
185 <div style
='margin-left:15px'>
186 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
188 <?php
echo xlt('Submit'); ?
>
192 <?php
if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) { ?
>
194 <a href
='#' class='css_button' id
='printbutton'>
196 <?php
echo xlt('Print'); ?
>
199 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
201 <?php
echo xlt('CSV Export'); ?
>
213 </div
> <!-- end of parameters
-->
219 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
221 $from_date = $form_from_date;
222 $to_date = $form_to_date;
223 $sqlBindArray = array();
224 $query = "select b.code,sum(b.units) as units,sum(b.fee) as billed,sum(ar_act.paid) as PaidAmount, " .
225 "sum(ar_act.adjust) as AdjustAmount,(sum(b.fee)-(sum(ar_act.paid)+sum(ar_act.adjust))) as Balance, " .
226 "c.financial_reporting " .
227 "FROM form_encounter as fe " .
228 "JOIN billing as b on b.pid=fe.pid and b.encounter=fe.encounter " .
229 "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 " .
230 "ON ar_act.pid=b.pid and ar_act.encounter=b.encounter and ar_act.code=b.code " .
231 "LEFT OUTER JOIN codes AS c ON c.code = b.code " .
232 "INNER JOIN code_types AS ct ON ct.ct_key = b.code_type AND ct.ct_fee = '1' " .
233 "WHERE b.code_type != 'COPAY' AND b.activity = 1 /* AND b.fee != 0 */ AND " .
234 "fe.date >= ? AND fe.date <= ?";
235 array_push($sqlBindArray,"$from_date 00:00:00","$to_date 23:59:59");
236 // If a facility was specified.
237 if ($form_facility) {
238 $query .= " AND fe.facility_id = ?";
239 array_push($sqlBindArray,$form_facility);
241 // If a provider was specified.
242 if ($form_provider) {
243 $query .= " AND b.provider_id = ?";
244 array_push($sqlBindArray,$form_provider);
246 // If selected important codes
247 if($_POST['form_details']) {
248 $query .= " AND c.financial_reporting = '1'";
250 $query .= " GROUP BY b.code ORDER BY b.code, fe.date, fe.id ";
251 $res = sqlStatement($query,$sqlBindArray);
252 $grand_total_units = 0;
253 $grand_total_amt_billed = 0;
254 $grand_total_amt_paid = 0;
255 $grand_total_amt_adjustment = 0;
256 $grand_total_amt_balance = 0;
258 while ($erow = sqlFetchArray($res)) {
260 $row['pid'] = $erow['pid'];
261 $row['provider_id'] = $erow['provider_id'];
262 $row['Procedure codes'] = $erow['code'];
263 $row['Units'] = $erow['units'];
264 $row['Amt Billed'] = $erow['billed'];
265 $row['Paid Amt'] = $erow['PaidAmount'];
266 $row['Adjustment Amt'] = $erow['AdjustAmount'];
267 $row['Balance Amt'] = $erow['Balance'];
268 $row['financial_reporting'] = $erow['financial_reporting'];
269 $rows[$erow['pid'] . '|' . $erow['code'] . '|' . $erow['units']] = $row;
271 if ($_POST['form_csvexport']) {
274 echo '"Procedure codes",';
276 echo '"Amt Billed",';
278 echo '"Adjustment Amt",';
279 echo '"Balance Amt",' . "\n";
282 ?
> <div id
="report_results">
283 <table id
='mymaintable'>
286 <?php
echo xlt('Procedure Codes'); ?
>
289 <?php
echo xlt('Units'); ?
>
292 <?php
echo xlt('Amt Billed'); ?
>
295 <?php
echo xlt('Paid Amt'); ?
>
298 <?php
echo xlt('Adjustment Amt'); ?
>
301 <?php
echo xlt('Balance Amt'); ?
>
308 foreach ($rows as $key => $row) {
312 if($row['financial_reporting']){ $bgcolor = "#FFFFDD"; }else { $bgcolor = "#FFDDDD"; }
313 $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>";
315 $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";
317 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
318 $grand_total_units +
= $row['Units'];
319 $grand_total_amt_billed +
= $row['Amt Billed'];
320 $grand_total_amt_paid +
= $row['Paid Amt'];
321 $grand_total_amt_adjustment +
= $row['Adjustment Amt'];
322 $grand_total_amt_balance +
= $row['Balance Amt'];
324 if ($_POST['form_csvexport']) { echo $csv; }
328 if (!$_POST['form_csvexport']) {
329 echo "<tr bgcolor='#ffffff'>\n";
330 echo " <td class='detail'>" . xlt("Grand Total") . "</td>\n";
331 echo " <td class='detail'>" . text($grand_total_units) . "</td>\n";
332 echo " <td class='detail'>" .
333 text(oeFormatMoney($grand_total_amt_billed)) . "</td>\n";
334 echo " <td class='detail'>" .
335 text(oeFormatMoney($grand_total_amt_paid)) . "</td>\n";
336 echo " <td class='detail'>" .
337 text(oeFormatMoney($grand_total_amt_adjustment)) . "</td>\n";
338 echo " <td class='detail'>" .
339 text(oeFormatMoney($grand_total_amt_balance)) . "</td>\n";
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.');
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']) { ?
>
359 <?php
echo xlt('Please input search criteria above, and click Submit to view results.' ); ?
>
367 } // End not csv export