3 * Report - Cash receipts by Provider
5 * This module was written for one of my clients to report on cash
6 * receipts by practitioner. It is not as complete as it should be
7 * but I wanted to make the code available to the project because
8 * many other practices have this same need. - rod@sunsetsystems.com
10 * Copyright (C) 2016 Terry Hill <terry@lillysystems.com>
11 * Copyright (C) 2006-2016 Rod Roark <rod@sunsetsystems.com>
12 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
14 * 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.
20 * @author Rod Roark <rod@sunsetsystems.com>
21 * @author Terry Hill <terry@lillysystems.com>
22 * @author Brady Miller <brady.g.miller@gmail.com>
23 * @link http://open-emr.org
28 use OpenEMR\Core\Header
;
30 require_once('../globals.php');
31 require_once($GLOBALS['srcdir'].'/patient.inc');
32 require_once($GLOBALS['srcdir'].'/acl.inc');
33 require_once($GLOBALS['srcdir'].'/options.inc.php');
34 require_once($GLOBALS['fileroot'].'/custom/code_types.inc.php');
36 // This determines if a particular procedure code corresponds to receipts
37 // for the "Clinic" column as opposed to receipts for the practitioner. Each
38 // practice will have its own policies in this regard, so you'll probably
39 // have to customize this function. If you use the "fee sheet" encounter
40 // form then the code below may work for you.
42 require_once('../forms/fee_sheet/codes.php');
43 function is_clinic($code)
46 $i = strpos($code, ':');
48 $code = substr($code, 0, $i);
51 return ($bcodes['CPT4'][xl('Lab')][$code] ||
52 $bcodes['CPT4'][xl('Immunizations')][$code] ||
53 $bcodes['HCPCS'][xl('Therapeutic Injections')][$code]);
56 function bucks($amount)
59 echo attr(oeFormatMoney($amount));
63 if (! acl_check('acct', 'rep')) {
64 die(xlt("Unauthorized access."));
68 $form_use_edate = $_POST['form_use_edate'];
70 $form_proc_codefull = trim($_POST['form_proc_codefull']);
71 // Parse the code type and the code from <code_type>:<code>
72 $tmp_code_array = explode(':', $form_proc_codefull);
73 $form_proc_codetype = $tmp_code_array[0];
74 $form_proc_code = $tmp_code_array[1];
76 $form_dx_codefull = trim($_POST['form_dx_codefull']);
77 // Parse the code type and the code from <code_type>:<code>
78 $tmp_code_array = explode(':', $form_dx_codefull);
79 $form_dx_codetype = $tmp_code_array[0];
80 $form_dx_code = $tmp_code_array[1];
82 $form_procedures = empty($_POST['form_procedures']) ?
0 : 1;
83 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-01'));
84 $form_to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
85 $form_facility = $_POST['form_facility'];
90 <?php Header
::setupHeader(['datetime-picker', 'report-helper']); ?
>
92 <style type
="text/css">
93 /* specifically include & exclude from printing */
99 #report_parameters_daterange {
108 /* specifically exclude some from the screen */
110 #report_parameters_daterange {
117 <script language
="JavaScript">
119 $
(document
).ready(function() {
120 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
121 var win
= top
.printLogSetup ? top
: opener
.top
;
122 win
.printLogSetup(document
.getElementById('printbutton'));
124 $
('.datepicker').datetimepicker({
125 <?php
$datetimepicker_timepicker = false; ?
>
126 <?php
$datetimepicker_showseconds = false; ?
>
127 <?php
$datetimepicker_formatInput = false; ?
>
128 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
129 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
133 // This is for callback by the find-code popup.
134 // Erases the current entry
135 // The target element is set by the find-code popup
136 // (this allows use of this in multiple form elements on the same page)
137 function set_related_target(codetype
, code
, selector
, codedesc
, target_element
) {
138 var f
= document
.forms
[0];
139 var s
= f
[target_element
].value
;
141 s
= codetype +
':' + code
;
145 f
[target_element
].value
= s
;
148 // This invokes the find-code (procedure/service codes) popup.
149 function sel_procedure() {
150 dlgopen('../patient_file/encounter/find_code_popup.php?target_element=form_proc_codefull&codetype=<?php echo attr(collect_codetypes("procedure", "csv")) ?>', '_blank', 500, 400);
153 // This invokes the find-code (diagnosis codes) popup.
154 function sel_diagnosis() {
155 dlgopen('../patient_file/encounter/find_code_popup.php?target_element=form_dx_codefull&codetype=<?php echo attr(collect_codetypes("diagnosis", "csv")) ?>', '_blank', 500, 400);
160 <title
><?php
echo xlt('Cash Receipts by Provider')?
></title
>
163 <body
class="body_top">
165 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Cash Receipts by Provider'); ?
></span
>
167 <form method
='post' action
='sl_receipts_report.php' id
='theform'>
169 <div id
="report_parameters">
171 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
176 <div style
='float:left'>
180 <td
class='control-label'>
181 <?php
echo xlt('Facility'); ?
>:
184 <?php
dropdown_facility($form_facility, 'form_facility'); ?
>
186 <td
class='control-label'>
187 <?php
echo xlt('Provider'); ?
>:
191 if (acl_check('acct', 'rep_a')) {
192 // Build a drop-down list of providers.
194 $query = "select id, lname, fname from users where " .
195 "authorized = 1 order by lname, fname";
196 $res = sqlStatement($query);
197 echo " <select name='form_doctor' class='form-control'>\n";
198 echo " <option value=''>-- " . xlt('All Providers') . " --\n";
199 while ($row = sqlFetchArray($res)) {
200 $provid = $row['id'];
201 echo " <option value='". attr($provid) ."'";
202 if ($provid == $_POST['form_doctor']) {
206 echo ">" . text($row['lname']) . ", " . text($row['fname']) . "\n";
211 echo "<input type='hidden' name='form_doctor' value='" . attr($_SESSION['authUserID']) . "'>";
216 <select name
='form_use_edate' class='form-control'>
217 <option value
='0'><?php
echo xlt('Payment Date'); ?
></option
>
218 <option value
='1'<?php
if ($form_use_edate) {
220 } ?
>><?php
echo xlt('Invoice Date'); ?
></option
>
225 <td
class='control-label'>
226 <?php
echo xlt('From'); ?
>:
229 <input type
='text' class='datepicker form-control' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo attr($form_from_date); ?>'
230 title
='<?php echo xla('Date of appointments mm
/dd
/yyyy
'); ?>' >
232 <td
class='control-label'>
233 <?php
echo xlt('To'); ?
>:
236 <input type
='text' class='datepicker form-control' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date); ?>'
237 title
='<?php echo xla('Optional end date mm
/dd
/yyyy
'); ?>' >
242 <td
class='control-label'>
243 <?php
if (!$GLOBALS['simplified_demographics']) {
244 echo ' ' . xlt('Procedure/Service') . ':';
248 <input type
='text' class='form-control' name
='form_proc_codefull' size
='11' value
='<?php echo attr($form_proc_codefull); ?>' onclick
='sel_procedure()'
249 title
='<?php echo xla('Optional procedure
/service code
'); ?>'
250 <?php
if ($GLOBALS['simplified_demographics']) {
251 echo "style='display:none'";
255 <td
class='control-label'>
256 <?php
if (!$GLOBALS['simplified_demographics']) {
257 echo ' ' . xlt('Diagnosis') . ':';
261 <input type
='text' class='form-control' name
='form_dx_codefull' size
='11' value
='<?php echo attr($form_dx_codefull); ?>' onclick
='sel_diagnosis()'
262 title
='<?php echo xla('Enter a diagnosis code to exclude all invoices not containing it
'); ?>'
263 <?php
if ($GLOBALS['simplified_demographics']) {
264 echo "style='display:none'";
269 <div
class='checkbox'>
270 <label
><input type
='checkbox' name
='form_details' value
='1'<?php
if ($_POST['form_details']) {
272 } ?
>><?php
echo xlt('Details')?
></label
>
274 <div
class='checkbox'>
275 <label
><input type
='checkbox' name
='form_procedures' value
='1'<?php
if ($form_procedures) {
277 } ?
>><?php
echo xlt('Procedures')?
></label
>
288 <td align
='left' valign
='middle' height
="100%">
290 <table style
='border-left:1px solid; width:100%; height:100%' >
293 <div
class="text-center">
294 <div
class="btn-group" role
="group">
295 <a href
='#' class='btn btn-default btn-save' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
296 <?php
echo xlt('Submit'); ?
>
298 <?php
if ($_POST['form_refresh']) { ?
>
299 <a href
='#' class='btn btn-default btn-print' id
='printbutton'>
300 <?php
echo xlt('Print'); ?
>
314 if ($_POST['form_refresh']) {
316 <div id
="report_results">
317 <table border
='0' cellpadding
='1' cellspacing
='2' width
='98%' id
='mymaintable'>
320 <?php
echo xlt('Practitioner') ?
>
323 <?php
echo xlt('Date') ?
>
325 <?php
if ($form_procedures) { ?
>
327 <?php
if ($GLOBALS['cash_receipts_report_invoice'] == '0') {
334 <?php
if ($form_proc_codefull) { ?
>
336 <?php
echo xlt('InvAmt') ?
>
339 <?php
if ($form_proc_codefull) { ?
>
341 <?php
echo xlt('Insurance') ?
>
344 <?php
if ($form_procedures) { ?
>
346 <?php
echo xlt('Procedure') ?
>
349 <?php
echo xlt('Prof.') ?
>
352 <?php
echo xlt('Clinic') ?
>
356 <?php
echo xlt('Received') ?
>
361 if ($_POST['form_refresh']) {
362 $form_doctor = $_POST['form_doctor'];
365 $ids_to_skip = array();
368 // Get copays. These will be ignored if a CPT code was specified.
370 if (!$form_proc_code ||
!$form_proc_codetype) {
371 /*************************************************************
372 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.modifier, " .
373 "fe.date, fe.id AS trans_id, u.id AS docid " .
374 "FROM billing AS b " .
375 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
376 "JOIN forms AS f ON f.pid = b.pid AND f.encounter = b.encounter AND f.formdir = 'newpatient' " .
377 "LEFT OUTER JOIN users AS u ON u.username = f.user " .
378 "WHERE b.code_type = 'COPAY' AND b.activity = 1 AND " .
379 "fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59'";
380 // If a facility was specified.
381 if ($form_facility) {
382 $query .= " AND fe.facility_id = '$form_facility'";
384 // If a doctor was specified.
386 $query .= " AND u.id = '$form_doctor'";
388 *************************************************************/
389 $sqlBindArray = array();
390 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, b.code, b.modifier, " .
391 "fe.date, fe.id AS trans_id, fe.provider_id AS docid, fe.invoice_refno " .
392 "FROM billing AS b " .
393 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
394 "WHERE b.code_type = 'COPAY' AND b.activity = 1 AND " .
395 "fe.date >= ? AND fe.date <= ?";
396 array_push($sqlBindArray, $form_from_date . " 00:00:00", $form_to_date . " 23:59:59");
397 // If a facility was specified.
398 if ($form_facility) {
399 $query .= " AND fe.facility_id = ?";
400 array_push($sqlBindArray, $form_facility);
403 // If a doctor was specified.
405 $query .= " AND fe.provider_id = ?";
406 array_push($sqlBindArray, $form_doctor);
409 /************************************************************/
411 $res = sqlStatement($query, $sqlBindArray);
412 while ($row = sqlFetchArray($res)) {
413 $trans_id = $row['trans_id'];
414 $thedate = substr($row['date'], 0, 10);
415 $patient_id = $row['pid'];
416 $encounter_id = $row['encounter'];
418 if (!empty($ids_to_skip[$trans_id])) {
423 // If a diagnosis code was given then skip any invoices without
425 if ($form_dx_code && $form_dx_codetype) {
426 $tmp = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
427 "pid = ? AND encounter = ? AND " .
428 "code_type = ? AND code LIKE ? AND " .
429 "activity = 1", array($patient_id,$encounter_id,$form_dx_codetype,$form_dx_code));
430 if (empty($tmp['count'])) {
431 $ids_to_skip[$trans_id] = 1;
438 "%08u%s%08u%08u%06u",
445 $arows[$key] = array();
446 $arows[$key]['transdate'] = $thedate;
447 $arows[$key]['amount'] = $row['fee'];
448 $arows[$key]['docid'] = $row['docid'];
449 $arows[$key]['project_id'] = 0;
450 $arows[$key]['memo'] = '';
451 if ($GLOBALS['cash_receipts_report_invoice'] == '0') {
452 $arows[$key]['invnumber'] = "$patient_id.$encounter_id";
454 $arows[$key]['invnumber'] = "$patient_name";
457 $arows[$key]['irnumber'] = $row['invoice_refno'];
459 } // end copays (not $form_proc_code)
461 // Get ar_activity (having payments), form_encounter, forms, users, optional ar_session
462 /***************************************************************
463 $query = "SELECT a.pid, a.encounter, a.post_time, a.code, a.modifier, a.pay_amount, " .
464 "fe.date, fe.id AS trans_id, u.id AS docid, s.deposit_date, s.payer_id " .
465 "FROM ar_activity AS a " .
466 "JOIN form_encounter AS fe ON fe.pid = a.pid AND fe.encounter = a.encounter " .
467 "JOIN forms AS f ON f.pid = a.pid AND f.encounter = a.encounter AND f.formdir = 'newpatient' " .
468 "LEFT OUTER JOIN users AS u ON u.username = f.user " .
469 "LEFT OUTER JOIN ar_session AS s ON s.session_id = a.session_id " .
470 "WHERE a.pay_amount != 0 AND ( " .
471 "a.post_time >= '$form_from_date 00:00:00' AND a.post_time <= '$form_to_date 23:59:59' " .
472 "OR fe.date >= '$form_from_date 00:00:00' AND fe.date <= '$form_to_date 23:59:59' " .
473 "OR s.deposit_date >= '$form_from_date' AND s.deposit_date <= '$form_to_date' )";
474 // If a procedure code was specified.
475 if ($form_proc_code) $query .= " AND a.code = '$form_proc_code'";
476 // If a facility was specified.
477 if ($form_facility) $query .= " AND fe.facility_id = '$form_facility'";
478 // If a doctor was specified.
479 if ($form_doctor) $query .= " AND u.id = '$form_doctor'";
480 ***************************************************************/
481 $sqlBindArray = array();
482 $query = "SELECT a.pid, a.encounter, a.post_time, a.code, a.modifier, a.pay_amount, " .
483 "fe.date, fe.id AS trans_id, fe.provider_id AS docid, fe.invoice_refno, s.deposit_date, s.payer_id, " .
484 "b.provider_id, concat(p.lname, ' ', p.fname) as 'pat_fulname' " .
485 "FROM ar_activity AS a " .
486 "JOIN form_encounter AS fe ON fe.pid = a.pid AND fe.encounter = a.encounter " .
487 "LEFT OUTER JOIN ar_session AS s ON s.session_id = a.session_id " .
488 "LEFT OUTER JOIN patient_data AS p ON p.pid = a.pid " .
489 "LEFT OUTER JOIN billing AS b ON b.pid = a.pid AND b.encounter = a.encounter AND " .
490 "b.code = a.code AND b.modifier = a.modifier AND b.activity = 1 AND " .
491 "b.code_type != 'COPAY' AND b.code_type != 'TAX' " .
492 "WHERE a.pay_amount != 0 AND ( " .
493 "a.post_time >= ? AND a.post_time <= ? " .
494 "OR fe.date >= ? AND fe.date <= ? " .
495 "OR s.deposit_date >= ? AND s.deposit_date <= ? )";
496 array_push($sqlBindArray, $form_from_date . " 00:00:00", $form_to_date . " 23:59:59", $form_from_date . " 00:00:00", $form_to_date . " 23:59:59", $form_from_date, $form_to_date);
497 // If a procedure code was specified.
498 // Support code type if it is in the ar_activity table. Note it is not always included, so
499 // also support a blank code type in ar_activity table.
500 if ($form_proc_codetype && $form_proc_code) {
501 $query .= " AND (a.code_type = ? OR a.code_type = '') AND a.code = ?";
502 array_push($sqlBindArray, $form_proc_codetype, $form_proc_code);
505 // If a facility was specified.
506 if ($form_facility) {
507 $query .= " AND fe.facility_id = ?";
508 array_push($sqlBindArray, $form_facility);
511 // If a doctor was specified.
513 $query .= " AND ( b.provider_id = ? OR " .
514 "( ( b.provider_id IS NULL OR b.provider_id = 0 ) AND " .
515 "fe.provider_id = ? ) )";
516 array_push($sqlBindArray, $form_doctor, $form_doctor);
519 /**************************************************************/
521 $res = sqlStatement($query, $sqlBindArray);
522 while ($row = sqlFetchArray($res)) {
523 $trans_id = $row['trans_id'];
524 $patient_id = $row['pid'];
525 $encounter_id = $row['encounter'];
526 $patient_name = $row['pat_fulname'];
528 if (!empty($ids_to_skip[$trans_id])) {
533 if ($form_use_edate) {
534 $thedate = substr($row['date'], 0, 10);
536 if (!empty($row['deposit_date'])) {
537 $thedate = $row['deposit_date'];
539 $thedate = substr($row['post_time'], 0, 10);
543 if (strcmp($thedate, $form_from_date) < 0 ||
strcmp($thedate, $form_to_date) > 0) {
548 // If a diagnosis code was given then skip any invoices without
550 if ($form_dx_code && $form_dx_codetype) {
551 $tmp = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
552 "pid = ? AND encounter = ? AND " .
553 "code_type = ? AND code LIKE ? AND " .
554 "activity = 1", array($patient_id,$encounter_id,$form_dx_codetype,$form_dx_code));
555 if (empty($tmp['count'])) {
556 $ids_to_skip[$trans_id] = 1;
562 $docid = empty($row['encounter_id']) ?
$row['docid'] : $row['encounter_id'];
564 "%08u%s%08u%08u%06u",
571 $arows[$key] = array();
572 $arows[$key]['transdate'] = $thedate;
573 $arows[$key]['amount'] = 0 - $row['pay_amount'];
574 $arows[$key]['docid'] = $docid;
575 $arows[$key]['project_id'] = empty($row['payer_id']) ?
0 : $row['payer_id'];
576 $arows[$key]['memo'] = $row['code'];
577 if ($GLOBALS['cash_receipts_report_invoice'] == '0') {
578 $arows[$key]['invnumber'] = "$patient_id.$encounter_id";
580 $arows[$key]['invnumber'] = "$patient_name";
583 $arows[$key]['irnumber'] = $row['invoice_refno'];
589 foreach ($arows as $row) {
590 // Get insurance company name
592 if ($form_proc_codefull && $row['project_id']) {
593 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE " .
594 "id = ?", array($row['project_id']));
595 $insconame = $tmp['name'];
600 if ($form_procedures && is_clinic($row['memo'])) {
601 $amount2 -= $row['amount'];
603 $amount1 -= $row['amount'];
606 // if ($docid != $row['employee_id']) {
607 if ($docid != $row['docid']) {
612 <tr bgcolor
="#ddddff">
613 <td
class="detail" colspan
="<?php echo ($form_proc_codefull ? 4 : 2) + ($form_procedures ? 2 : 0); ?>">
614 <?php
echo xlt('Totals for ') . text($docname) ?
>
617 <?php
bucks($doctotal1) ?
>
619 <?php
if ($form_procedures) { ?
>
621 <?php
bucks($doctotal2) ?
>
631 $docid = $row['docid'];
632 $tmp = sqlQuery("SELECT lname, fname FROM users WHERE id = ?", array($docid));
633 $docname = empty($tmp) ?
xl('Unknown') : $tmp['fname'] . ' ' . $tmp['lname'];
635 $docnameleft = $docname;
638 if ($_POST['form_details']) {
643 <?php
echo text($docnameleft); $docnameleft = " " ?
>
646 <?php
echo oeFormatShortDate($row['transdate']) ?
>
648 <?php
if ($form_procedures) { ?
>
650 <?php
echo empty($row['irnumber']) ?
text($row['invnumber']) : text($row['irnumber']); ?
>
654 if ($form_proc_code && $form_proc_codetype) {
655 echo " <td class='detail' align='right'>";
656 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
657 $tmp = sqlQuery("SELECT SUM(fee) AS sum FROM billing WHERE " .
658 "pid = ? AND encounter = ? AND " .
659 "code_type = ? AND code = ? AND activity = 1", array($patient_id,$encounter_id,$form_proc_codetype,$form_proc_code));
664 <?php
if ($form_proc_codefull) { ?
>
666 <?php
echo text($insconame) ?
>
669 <?php
if ($form_procedures) { ?
>
671 <?php
echo text($row['memo']) ?
>
674 <td
class="detail" align
="right">
675 <?php
bucks($amount1) ?
>
677 <?php
if ($form_procedures) { ?
>
678 <td
class="detail" align
="right">
679 <?php
bucks($amount2) ?
>
685 $doctotal1 +
= $amount1;
686 $doctotal2 +
= $amount2;
687 $grandtotal1 +
= $amount1;
688 $grandtotal2 +
= $amount2;
692 <tr bgcolor
="#ddddff">
693 <td
class="detail" colspan
="<?php echo ($form_proc_codefull ? 4 : 2) + ($form_procedures ? 2 : 0); ?>">
694 <?php
echo xlt('Totals for ') . text($docname) ?
>
697 <?php
bucks($doctotal1) ?
>
699 <?php
if ($form_procedures) { ?
>
701 <?php
bucks($doctotal2) ?
>
706 <tr bgcolor
="#ffdddd">
707 <td
class="detail" colspan
="<?php echo ($form_proc_codefull ? 4 : 2) + ($form_procedures ? 2 : 0); ?>">
708 <?php
echo xlt('Grand Totals') ?
>
711 <?php
bucks($grandtotal1) ?
>
713 <?php
if ($form_procedures) { ?
>
715 <?php
bucks($grandtotal2) ?
>
719 <?php
$report_from_date = oeFormatShortDate($form_from_date) ;
720 $report_to_date = oeFormatShortDate($form_to_date) ;
722 <div align
='right'><span
class='title' ><?php
echo xlt('Report Date'). ' '; ?
><?php
echo text($report_from_date);?
> - <?php
echo text($report_to_date);?
></span
></div
>
732 <?php
echo xlt('Please input search criteria above, and click Submit to view results.'); ?
>