2 // Copyright (C) 2006-2016 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This is a report of receipts by payer or payment method.
11 // The payer option means an insurance company name or "Patient".
13 // The payment method option is most useful for sites using
14 // pos_checkout.php (e.g. weight loss clinics) because this plugs
15 // a payment method like Cash, Check, VISA, etc. into the "source"
16 // column of the SQL-Ledger acc_trans table or ar_session table.
18 require_once("../globals.php");
19 require_once("$srcdir/patient.inc");
20 require_once("$srcdir/acl.inc");
21 require_once("$srcdir/formatting.inc.php");
22 require_once "$srcdir/options.inc.php";
23 require_once "$srcdir/formdata.inc.php";
24 require_once("../../custom/code_types.inc.php");
26 // This controls whether we show pt name, policy number and DOS.
31 function bucks($amount) {
32 if ($amount) echo oeFormatMoney($amount);
35 function thisLineItem($patient_id, $encounter_id, $memo, $transdate,
36 $rowmethod, $rowpayamount, $rowadjamount, $payer_type=0, $irnumber='')
38 global $form_report_by, $insarray, $grandpaytotal, $grandadjtotal;
40 if ($form_report_by != '1') { // reporting by method or check number
41 showLineItem($patient_id, $encounter_id, $memo, $transdate,
42 $rowmethod, $rowpayamount, $rowadjamount, $payer_type, $irnumber);
46 // Reporting by payer.
48 if ($_POST['form_details']) { // details are wanted
49 // Save everything for later sorting.
50 $insarray[] = array($patient_id, $encounter_id, $memo, $transdate,
51 $rowmethod, $rowpayamount, $rowadjamount, $payer_type, $irnumber);
53 else { // details not wanted
54 if (empty($insarray[$rowmethod])) $insarray[$rowmethod] = array(0, 0);
55 $insarray[$rowmethod][0] +
= $rowpayamount;
56 $insarray[$rowmethod][1] +
= $rowadjamount;
57 $grandpaytotal +
= $rowpayamount;
58 $grandadjtotal +
= $rowadjamount;
62 function showLineItem($patient_id, $encounter_id, $memo, $transdate,
63 $rowmethod, $rowpayamount, $rowadjamount, $payer_type=0, $irnumber='')
65 global $paymethod, $paymethodleft, $methodpaytotal, $methodadjtotal,
66 $grandpaytotal, $grandadjtotal, $showing_ppd;
68 if (! $rowmethod) $rowmethod = 'Unknown';
70 $invnumber = $irnumber ?
$irnumber : "$patient_id.$encounter_id";
72 if ($paymethod != $rowmethod) {
74 // Print method total.
77 <tr bgcolor
="#ddddff">
78 <td
class="detail" colspan
="<?php echo $showing_ppd ? 7 : 4; ?>">
79 <?php
echo xl('Total for ') . $paymethod ?
>
82 <?php
bucks($methodadjtotal) ?
>
85 <?php
bucks($methodpaytotal) ?
>
92 $paymethod = $rowmethod;
93 $paymethodleft = $paymethod;
96 if ($_POST['form_details']) {
101 <?php
echo $paymethodleft; $paymethodleft = " " ?
>
104 <?php
echo oeFormatShortDate($transdate) ?
>
107 <?php
echo $invnumber ?
>
112 $pferow = sqlQuery("SELECT p.fname, p.mname, p.lname, fe.date " .
113 "FROM patient_data AS p, form_encounter AS fe WHERE " .
114 "p.pid = '$patient_id' AND fe.pid = p.pid AND " .
115 "fe.encounter = '$encounter_id' LIMIT 1");
116 $dos = substr($pferow['date'], 0, 10);
118 echo " <td class='dehead'>\n";
119 echo " " . $pferow['lname'] . ", " . $pferow['fname'] . " " . $pferow['mname'];
122 echo " <td class='dehead'>\n";
124 $ptarr = array(1 => 'primary', 2 => 'secondary', 3 => 'tertiary');
125 $insrow = getInsuranceDataByDate($patient_id, $dos,
126 $ptarr[$payer_type], "policy_number");
127 echo " " . $insrow['policy_number'];
131 echo " <td class='dehead'>\n";
132 echo " " . oeFormatShortDate($dos) . "\n";
141 <?php
bucks($rowadjamount) ?
>
144 <?php
bucks($rowpayamount) ?
>
149 $methodpaytotal +
= $rowpayamount;
150 $grandpaytotal +
= $rowpayamount;
151 $methodadjtotal +
= $rowadjamount;
152 $grandadjtotal +
= $rowadjamount;
155 // This is called by usort() when reporting by payer with details.
156 // Sorts by payer/date/patient/encounter/memo.
157 function payerCmp($a, $b) {
158 foreach (array(4,3,0,1,2,7) as $i) {
159 if ($a[$i] < $b[$i]) return -1;
160 if ($a[$i] > $b[$i]) return 1;
165 if (! acl_check('acct', 'rep')) die(xl("Unauthorized access."));
168 $form_from_date = fixDate($_POST['form_from_date'], date('Y-m-d'));
169 $form_to_date = fixDate($_POST['form_to_date'] , date('Y-m-d'));
170 $form_use_edate = $_POST['form_use_edate'];
171 $form_facility = $_POST['form_facility'];
172 $form_report_by = $_POST['form_report_by'];
173 $form_proc_codefull = trim($_POST['form_proc_codefull']);
174 // Parse the code type and the code from <code_type>:<code>
175 $tmp_code_array = explode(':',$form_proc_codefull);
176 $form_proc_codetype = $tmp_code_array[0];
177 $form_proc_code = $tmp_code_array[1];
183 <?php
if (function_exists('html_header_show')) html_header_show(); ?
>
184 <style type
="text/css">
185 /* specifically include & exclude from printing */
191 #report_parameters_daterange {
200 /* specifically exclude some from the screen */
202 #report_parameters_daterange {
208 table
.mymaintable
, table
.mymaintable td
{
209 border
: 1px solid
#aaaaaa;
210 border
-collapse
: collapse
;
212 table
.mymaintable td
{
213 padding
: 1pt
4pt
1pt
4pt
;
217 <script type
="text/javascript" src
="../../library/dialog.js?v=<?php echo $v_js_includes; ?>"></script
>
218 <script type
="text/javascript" src
="../../library/js/jquery-1.9.1.min.js"></script
>
219 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
221 <script language
="JavaScript">
223 $
(document
).ready(function() {
224 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
225 var win
= top
.printLogSetup ? top
: opener
.top
;
226 win
.printLogSetup(document
.getElementById('printbutton'));
229 // This is for callback by the find-code popup.
230 // Erases the current entry
231 function set_related(codetype
, code
, selector
, codedesc
) {
232 var f
= document
.forms
[0];
233 var s
= f
.form_proc_codefull
.value
;
235 s
= codetype +
':' + code
;
239 f
.form_proc_codefull
.value
= s
;
242 // This invokes the find-code popup.
243 function sel_procedure() {
244 dlgopen('../patient_file/encounter/find_code_popup.php?codetype=<?php echo attr(collect_codetypes("procedure","csv")) ?>', '_blank', 500, 400);
249 <title
><?
xl('Receipts Summary','e')?
></title
>
252 <body
class="body_top">
254 <span
class='title'><?php
xl('Report','e'); ?
> - <?php
xl('Receipts Summary','e'); ?
></span
>
256 <form method
='post' action
='receipts_by_method_report.php' id
='theform'>
258 <div id
="report_parameters">
260 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
265 <div style
='float:left'>
270 <?php
xl('Report by','e'); ?
>
274 echo " <select name='form_report_by'>\n";
275 foreach (array(1 => 'Payer', 2 => 'Payment Method', 3 => 'Check Number') as $key => $value) {
276 echo " <option value='$key'";
277 if ($key == $form_report_by) echo ' selected';
278 echo ">" . xl($value) . "</option>\n";
280 echo " </select> \n"; ?
>
284 <?php
dropdown_facility(strip_escape_custom($form_facility), 'form_facility', false); ?
>
288 <?php
if (!$GLOBALS['simplified_demographics']) echo ' ' . xl('Procedure/Service') . ':'; ?
>
291 <input type
='text' name
='form_proc_codefull' size
='12' value
='<?php echo $form_proc_codefull; ?>' onclick
='sel_procedure()'
292 title
='<?php xl('Click to select optional procedure code
','e
'); ?>'
293 <?php
if ($GLOBALS['simplified_demographics']) echo "style='display:none'"; ?
> />
295  
;<input type
='checkbox' name
='form_details' value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
> /><?
xl('Details','e')?
>
301 <select name
='form_use_edate'>
302 <option value
='0'><?php
xl('Payment Date','e'); ?
></option
>
303 <option value
='1'<?php
if ($form_use_edate) echo ' selected' ?
>><?php
xl('Invoice Date','e'); ?
></option
>
307 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo $form_from_date ?>'
308 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
309 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
310 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
311 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
314 <?php
xl('To','e'); ?
>:
317 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_date ?>'
318 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
319 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
320 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
321 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
329 <td align
='left' valign
='middle' height
="100%">
330 <table style
='border-left:1px solid; width:100%; height:100%' >
333 <div style
='margin-left:15px'>
334 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
336 <?php
xl('Submit','e'); ?
>
340 <?php
if ($_POST['form_refresh']) { ?
>
341 <a href
='#' class='css_button' id
='printbutton'>
343 <?php
xl('Print','e'); ?
>
355 </div
> <!-- end of parameters
-->
358 if ($_POST['form_refresh']) {
360 <div id
="report_results">
362 <table width
='98%' id
='mymaintable' class='mymaintable'>
365 <tr bgcolor
="#dddddd">
367 <?php
xl('Method','e') ?
>
370 <?php
xl('Date','e') ?
>
373 <?php
xl('Invoice','e') ?
>
375 <?php
if ($showing_ppd) { ?
>
377 <?
xl('Patient','e')?
>
387 <?
xl('Procedure','e')?
>
390 <?
xl('Adjustments','e')?
>
393 <?
xl('Payments','e')?
>
400 if ($_POST['form_refresh']) {
401 $from_date = $form_from_date;
402 $to_date = $form_to_date;
412 // Get co-pays using the encounter date as the pay date. These will
413 // always be considered patient payments. Ignored if selecting by
416 if (!$form_proc_code ||
!$form_proc_codetype) {
417 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, " .
418 "fe.date, fe.facility_id, fe.invoice_refno " .
419 "FROM billing AS b " .
420 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
421 "WHERE b.code_type = 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
422 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
423 // If a facility was specified.
424 if ($form_facility) $query .= " AND fe.facility_id = '$form_facility'";
425 $query .= " ORDER BY fe.date, b.pid, b.encounter, fe.id";
427 $res = sqlStatement($query);
428 while ($row = sqlFetchArray($res)) {
429 $rowmethod = $form_report_by == 1 ?
'Patient' : 'Co-Pay';
430 thisLineItem($row['pid'], $row['encounter'], $row['code_text'],
431 substr($row['date'], 0, 10), $rowmethod, 0 - $row['fee'], 0, 0, $row['invoice_refno']);
433 } // end if not form_proc_code
435 // Get all other payments and adjustments and their dates, corresponding
436 // payers and check reference data, and the encounter dates separately.
438 $query = "SELECT a.pid, a.encounter, a.post_time, a.pay_amount, " .
439 "a.adj_amount, a.memo, a.session_id, a.code, a.payer_type, fe.id, fe.date, " .
440 "fe.invoice_refno, s.deposit_date, s.payer_id, s.reference, i.name " .
441 "FROM ar_activity AS a " .
442 "JOIN form_encounter AS fe ON fe.pid = a.pid AND fe.encounter = a.encounter " .
443 "JOIN forms AS f ON f.pid = a.pid AND f.encounter = a.encounter AND f.formdir = 'newpatient' " .
444 "LEFT JOIN ar_session AS s ON s.session_id = a.session_id " .
445 "LEFT JOIN insurance_companies AS i ON i.id = s.payer_id " .
446 "WHERE ( a.pay_amount != 0 OR a.adj_amount != 0 )";
448 if ($form_use_edate) {
449 $query .= " AND fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
451 $query .= " AND ( ( s.deposit_date IS NOT NULL AND " .
452 "s.deposit_date >= '$from_date' AND s.deposit_date <= '$to_date' ) OR " .
453 "( s.deposit_date IS NULL AND a.post_time >= '$from_date 00:00:00' AND " .
454 "a.post_time <= '$to_date 23:59:59' ) )";
456 // If a procedure code was specified.
457 if ($form_proc_code && $form_proc_codetype) {
458 // if a code_type is entered into the ar_activity table, then use it. If it is not entered in, then do not use it.
459 $query .= " AND ( a.code_type = '$form_proc_codetype' OR a.code_type = '' ) AND a.code LIKE '$form_proc_code%'";
461 // If a facility was specified.
462 if ($form_facility) $query .= " AND fe.facility_id = '$form_facility'";
464 if ($form_use_edate) {
465 $query .= " ORDER BY s.reference, fe.date, a.pid, a.encounter, fe.id";
467 $query .= " ORDER BY s.reference, s.deposit_date, a.post_time, a.pid, a.encounter, fe.id";
470 $res = sqlStatement($query);
471 while ($row = sqlFetchArray($res)) {
472 if ($form_use_edate) {
473 $thedate = substr($row['date'], 0, 10);
474 } else if (!empty($row['deposit_date'])) {
475 $thedate = $row['deposit_date'];
477 $thedate = substr($row['post_time'], 0, 10);
479 // Compute reporting key: insurance company name or payment method.
480 if ($form_report_by == '1') {
481 if (empty($row['payer_id'])) {
484 if (empty($row['name'])) $rowmethod = xl('Unnamed insurance company');
485 else $rowmethod = $row['name'];
489 if (empty($row['session_id'])) {
490 $rowmethod = trim($row['memo']);
492 $rowmethod = trim($row['reference']);
494 if ($form_report_by != '3') {
495 // Extract only the first word as the payment method because any
496 // following text will be some petty detail like a check number.
497 $rowmethod = substr($rowmethod, 0, strcspn($rowmethod, ' /'));
501 thisLineItem($row['pid'], $row['encounter'], $row['code'], $thedate,
502 $rowmethod, $row['pay_amount'], $row['adj_amount'], $row['payer_type'],
503 $row['invoice_refno']);
506 // Not payer summary.
507 if ($form_report_by != '1' ||
$_POST['form_details']) {
509 if ($form_report_by == '1') { // by payer with details
510 // Sort and dump saved info, and consolidate items with all key
511 // fields being the same.
512 usort($insarray, 'payerCmp');
514 foreach ($insarray as $a) {
515 if (empty($a[4])) $a[4] = xl('Patient');
521 foreach (array(4,3,0,1,2,7) as $i) if ($a[$i] != $b[$i]) $match = false;
526 showLineItem($b[0], $b[1], $b[2], $b[3], $b[4], $b[5], $b[6], $b[7], $b[8]);
532 showLineItem($b[0], $b[1], $b[2], $b[3], $b[4], $b[5], $b[6], $b[7], $b[8]);
534 } // end by payer with details
536 // Print last method total.
538 <tr bgcolor
="#ddddff">
539 <td
class="detail" colspan
="<?php echo $showing_ppd ? 7 : 4; ?>">
540 <?php
echo xl('Total for ') . $paymethod ?
>
543 <?php
bucks($methodadjtotal) ?
>
546 <?php
bucks($methodpaytotal) ?
>
552 // Payer summary: need to sort and then print it all.
555 foreach ($insarray as $key => $value) {
556 if (empty($key)) $key = xl('Patient');
558 <tr bgcolor
="#ddddff">
559 <td
class="detail" colspan
="<?php echo $showing_ppd ? 7 : 4; ?>">
563 <?php
bucks($value[1]); ?
>
566 <?php
bucks($value[0]); ?
>
571 } // end payer summary
573 <tr bgcolor
="#ffdddd">
574 <td
class="detail" colspan
="<?php echo $showing_ppd ? 7 : 4; ?>">
575 <?php
xl('Grand Total','e') ?
>
578 <?php
bucks($grandadjtotal) ?
>
581 <?php
bucks($grandpaytotal) ?
>
586 } // end form refresh
594 <?php
echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?
>
601 <!-- stuff
for the popup calendar
-->
602 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
603 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
604 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
605 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
606 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
607 <script language
="Javascript">
608 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
609 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});