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/formatting.inc.php");
38 require_once "$srcdir/options.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 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
51 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
52 $form_facility = $_POST['form_facility'];
53 $form_provider = $_POST['form_provider'];
55 if ($_POST['form_csvexport']) {
56 header("Pragma: public");
58 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
59 header("Content-Type: application/force-download");
60 header("Content-Disposition: attachment; filename=svc_financial_report_".attr($form_from_date)."--".attr($form_to_date).".csv");
61 header("Content-Description: File Transfer");
68 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
69 <link rel
="stylesheet" href
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
71 <?php
html_header_show();?
>
73 <style type
="text/css">
74 /* specifically include & exclude from printing */
80 #report_parameters_daterange {
89 /* specifically exclude some from the screen */
91 #report_parameters_daterange {
98 <script type
="text/javascript" src
="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script
>
99 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script
>
100 <script type
="text/javascript" src
="../../library/js/common.js?v=<?php echo $v_js_includes; ?>"></script
>
101 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
102 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script
>
104 <title
><?php
echo xlt('Financial Summary by Service Code') ?
></title
>
106 <script language
="JavaScript">
108 $
(document
).ready(function() {
109 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
110 var win
= top
.printLogSetup ? top
: opener
.top
;
111 win
.printLogSetup(document
.getElementById('printbutton'));
113 $
('.datepicker').datetimepicker({
114 <?php
$datetimepicker_timepicker = false; ?
>
115 <?php
$datetimepicker_formatInput = false; ?
>
116 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
117 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
125 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0' class="body_top">
126 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Financial Summary by Service Code'); ?
></span
>
127 <form method
='post' action
='svc_code_financial_report.php' id
='theform'>
128 <div id
="report_parameters">
129 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
130 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
134 <div style
='float:left'>
138 <?php
echo xlt('Facility'); ?
>:
141 <?php
dropdown_facility($form_facility, 'form_facility', true); ?
>
143 <td
><?php
echo xlt('Provider'); ?
>:</td
>
145 // Build a drop-down list of providers.
147 $query = "SELECT id, lname, fname FROM users WHERE ".
148 "authorized = 1 ORDER BY lname, fname"; //(CHEMED) facility filter
149 $ures = sqlStatement($query);
150 echo " <select name='form_provider'>\n";
151 echo " <option value=''>-- " . xlt('All') . " --\n";
152 while ($urow = sqlFetchArray($ures)) {
153 $provid = $urow['id'];
154 echo " <option value='" . attr($provid) ."'";
155 if ($provid == $_POST['form_provider']) echo " selected";
156 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
163 <?php
echo xlt('From'); ?
>: 
; 
; 
; 
;
164 <input type
='text' class='datepicker' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date) ?>'
168 <?php
echo xlt('To'); ?
>:
171 <input type
='text' class='datepicker' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
175 <input type
='checkbox' name
='form_details'<?php
if ($_POST['form_details']) echo ' checked'; ?
>>
176 <?php
echo xlt('Important Codes'); ?
>
182 <td align
='left' valign
='middle' height
="100%">
183 <table style
='border-left:1px solid; width:100%; height:100%' >
186 <div style
='margin-left:15px'>
187 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value",""); $("#theform").submit();'>
189 <?php
echo xlt('Submit'); ?
>
193 <?php
if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) { ?
>
195 <a href
='#' class='css_button' id
='printbutton'>
197 <?php
echo xlt('Print'); ?
>
200 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value",""); $("#form_csvexport").attr("value","true"); $("#theform").submit();'>
202 <?php
echo xlt('CSV Export'); ?
>
214 </div
> <!-- end of parameters
-->
220 if ($_POST['form_refresh'] ||
$_POST['form_csvexport']) {
222 $from_date = $form_from_date;
223 $to_date = $form_to_date;
224 $sqlBindArray = array();
225 $query = "select b.code,sum(b.units) as units,sum(b.fee) as billed,sum(ar_act.paid) as PaidAmount, " .
226 "sum(ar_act.adjust) as AdjustAmount,(sum(b.fee)-(sum(ar_act.paid)+sum(ar_act.adjust))) as Balance, " .
227 "c.financial_reporting " .
228 "FROM form_encounter as fe " .
229 "JOIN billing as b on b.pid=fe.pid and b.encounter=fe.encounter " .
230 "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 " .
231 "ON ar_act.pid=b.pid and ar_act.encounter=b.encounter and ar_act.code=b.code " .
232 "LEFT OUTER JOIN codes AS c ON c.code = b.code " .
233 "INNER JOIN code_types AS ct ON ct.ct_key = b.code_type AND ct.ct_fee = '1' " .
234 "WHERE b.code_type != 'COPAY' AND b.activity = 1 /* AND b.fee != 0 */ AND " .
235 "fe.date >= ? AND fe.date <= ?";
236 array_push($sqlBindArray,"$from_date 00:00:00","$to_date 23:59:59");
237 // If a facility was specified.
238 if ($form_facility) {
239 $query .= " AND fe.facility_id = ?";
240 array_push($sqlBindArray,$form_facility);
242 // If a provider was specified.
243 if ($form_provider) {
244 $query .= " AND b.provider_id = ?";
245 array_push($sqlBindArray,$form_provider);
247 // If selected important codes
248 if($_POST['form_details']) {
249 $query .= " AND c.financial_reporting = '1'";
251 $query .= " GROUP BY b.code ORDER BY b.code, fe.date, fe.id ";
252 $res = sqlStatement($query,$sqlBindArray);
253 $grand_total_units = 0;
254 $grand_total_amt_billed = 0;
255 $grand_total_amt_paid = 0;
256 $grand_total_amt_adjustment = 0;
257 $grand_total_amt_balance = 0;
259 while ($erow = sqlFetchArray($res)) {
261 $row['pid'] = $erow['pid'];
262 $row['provider_id'] = $erow['provider_id'];
263 $row['Procedure codes'] = $erow['code'];
264 $row['Units'] = $erow['units'];
265 $row['Amt Billed'] = $erow['billed'];
266 $row['Paid Amt'] = $erow['PaidAmount'];
267 $row['Adjustment Amt'] = $erow['AdjustAmount'];
268 $row['Balance Amt'] = $erow['Balance'];
269 $row['financial_reporting'] = $erow['financial_reporting'];
270 $rows[$erow['pid'] . '|' . $erow['code'] . '|' . $erow['units']] = $row;
272 if ($_POST['form_csvexport']) {
275 echo '"Procedure codes",';
277 echo '"Amt Billed",';
279 echo '"Adjustment Amt",';
280 echo '"Balance Amt",' . "\n";
283 ?
> <div id
="report_results">
284 <table id
='mymaintable'>
287 <?php
echo xlt('Procedure Codes'); ?
>
290 <?php
echo xlt('Units'); ?
>
293 <?php
echo xlt('Amt Billed'); ?
>
296 <?php
echo xlt('Paid Amt'); ?
>
299 <?php
echo xlt('Adjustment Amt'); ?
>
302 <?php
echo xlt('Balance Amt'); ?
>
309 foreach ($rows as $key => $row) {
313 if($row['financial_reporting']){ $bgcolor = "#FFFFDD"; }else { $bgcolor = "#FFDDDD"; }
314 $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>";
316 $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";
318 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
319 $grand_total_units +
= $row['Units'];
320 $grand_total_amt_billed +
= $row['Amt Billed'];
321 $grand_total_amt_paid +
= $row['Paid Amt'];
322 $grand_total_amt_adjustment +
= $row['Adjustment Amt'];
323 $grand_total_amt_balance +
= $row['Balance Amt'];
325 if ($_POST['form_csvexport']) { echo $csv; }
329 if (!$_POST['form_csvexport']) {
330 echo "<tr bgcolor='#ffffff'>\n";
331 echo " <td class='detail'>" . xlt("Grand Total") . "</td>\n";
332 echo " <td class='detail'>" . text($grand_total_units) . "</td>\n";
333 echo " <td class='detail'>" .
334 text(oeFormatMoney($grand_total_amt_billed)) . "</td>\n";
335 echo " <td class='detail'>" .
336 text(oeFormatMoney($grand_total_amt_paid)) . "</td>\n";
337 echo " <td class='detail'>" .
338 text(oeFormatMoney($grand_total_amt_adjustment)) . "</td>\n";
339 echo " <td class='detail'>" .
340 text(oeFormatMoney($grand_total_amt_balance)) . "</td>\n";
348 if (! $_POST['form_csvexport']) {
349 if ( $_POST['form_refresh'] && count($print) != 1)
351 echo "<span style='font-size:10pt;'>";
352 echo xlt('No matches found. Try search again.');
354 echo '<script>document.getElementById("report_results").style.display="none";</script>';
355 echo '<script>document.getElementById("controls").style.display="none";</script>';
358 if (!$_POST['form_refresh'] && !$_POST['form_csvexport']) { ?
>
360 <?php
echo xlt('Please input search criteria above, and click Submit to view results.' ); ?
>
368 } // End not csv export