2 // Copyright (C) 2006-2015 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 {
209 <script type
="text/javascript" src
="../../library/dialog.js"></script
>
210 <script type
="text/javascript" src
="../../library/js/jquery.1.3.2.js"></script
>
212 <script language
="JavaScript">
214 $
(document
).ready(function() {
215 var win
= top
.printLogSetup ? top
: opener
.top
;
216 win
.printLogSetup(document
.getElementById('printbutton'));
219 // This is for callback by the find-code popup.
220 // Erases the current entry
221 function set_related(codetype
, code
, selector
, codedesc
) {
222 var f
= document
.forms
[0];
223 var s
= f
.form_proc_codefull
.value
;
225 s
= codetype +
':' + code
;
229 f
.form_proc_codefull
.value
= s
;
232 // This invokes the find-code popup.
233 function sel_procedure() {
234 dlgopen('../patient_file/encounter/find_code_popup.php?codetype=<?php echo attr(collect_codetypes("procedure","csv")) ?>', '_blank', 500, 400);
239 <title
><?
xl('Receipts Summary','e')?
></title
>
242 <body
class="body_top">
244 <span
class='title'><?php
xl('Report','e'); ?
> - <?php
xl('Receipts Summary','e'); ?
></span
>
246 <form method
='post' action
='receipts_by_method_report.php' id
='theform'>
248 <div id
="report_parameters">
250 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
255 <div style
='float:left'>
260 <?php
xl('Report by','e'); ?
>
264 echo " <select name='form_report_by'>\n";
265 foreach (array(1 => 'Payer', 2 => 'Payment Method', 3 => 'Check Number') as $key => $value) {
266 echo " <option value='$key'";
267 if ($key == $form_report_by) echo ' selected';
268 echo ">" . xl($value) . "</option>\n";
270 echo " </select> \n"; ?
>
274 <?php
dropdown_facility(strip_escape_custom($form_facility), 'form_facility', false); ?
>
278 <?php
if (!$GLOBALS['simplified_demographics']) echo ' ' . xl('Procedure/Service') . ':'; ?
>
281 <input type
='text' name
='form_proc_codefull' size
='12' value
='<?php echo $form_proc_codefull; ?>' onclick
='sel_procedure()'
282 title
='<?php xl('Click to select optional procedure code
','e
'); ?>'
283 <?php
if ($GLOBALS['simplified_demographics']) echo "style='display:none'"; ?
> />
285  
;<input type
='checkbox' name
='form_details' value
='1'<?php
if ($_POST['form_details']) echo " checked"; ?
> /><?
xl('Details','e')?
>
291 <select name
='form_use_edate'>
292 <option value
='0'><?php
xl('Payment Date','e'); ?
></option
>
293 <option value
='1'<?php
if ($form_use_edate) echo ' selected' ?
>><?php
xl('Invoice Date','e'); ?
></option
>
297 <input type
='text' name
='form_from_date' id
="form_from_date" size
='10' value
='<?php echo $form_from_date ?>'
298 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
299 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
300 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
301 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
304 <?php
xl('To','e'); ?
>:
307 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_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_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
311 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
319 <td align
='left' valign
='middle' height
="100%">
320 <table style
='border-left:1px solid; width:100%; height:100%' >
323 <div style
='margin-left:15px'>
324 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
326 <?php
xl('Submit','e'); ?
>
330 <?php
if ($_POST['form_refresh']) { ?
>
331 <a href
='#' class='css_button' id
='printbutton'>
333 <?php
xl('Print','e'); ?
>
345 </div
> <!-- end of parameters
-->
348 if ($_POST['form_refresh']) {
350 <div id
="report_results">
362 <?
xl('Invoice','e')?
>
364 <?php
if ($showing_ppd) { ?
>
366 <?
xl('Patient','e')?
>
376 <?
xl('Procedure','e')?
>
379 <?
xl('Adjustments','e')?
>
382 <?
xl('Payments','e')?
>
388 if ($_POST['form_refresh']) {
389 $from_date = $form_from_date;
390 $to_date = $form_to_date;
400 // Get co-pays using the encounter date as the pay date. These will
401 // always be considered patient payments. Ignored if selecting by
404 if (!$form_proc_code ||
!$form_proc_codetype) {
405 $query = "SELECT b.fee, b.pid, b.encounter, b.code_type, " .
406 "fe.date, fe.facility_id, fe.invoice_refno " .
407 "FROM billing AS b " .
408 "JOIN form_encounter AS fe ON fe.pid = b.pid AND fe.encounter = b.encounter " .
409 "WHERE b.code_type = 'COPAY' AND b.activity = 1 AND b.fee != 0 AND " .
410 "fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
411 // If a facility was specified.
412 if ($form_facility) $query .= " AND fe.facility_id = '$form_facility'";
413 $query .= " ORDER BY fe.date, b.pid, b.encounter, fe.id";
415 $res = sqlStatement($query);
416 while ($row = sqlFetchArray($res)) {
417 $rowmethod = $form_report_by == 1 ?
'Patient' : 'Co-Pay';
418 thisLineItem($row['pid'], $row['encounter'], $row['code_text'],
419 substr($row['date'], 0, 10), $rowmethod, 0 - $row['fee'], 0, 0, $row['invoice_refno']);
421 } // end if not form_proc_code
423 // Get all other payments and adjustments and their dates, corresponding
424 // payers and check reference data, and the encounter dates separately.
426 $query = "SELECT a.pid, a.encounter, a.post_time, a.pay_amount, " .
427 "a.adj_amount, a.memo, a.session_id, a.code, a.payer_type, fe.id, fe.date, " .
428 "fe.invoice_refno, s.deposit_date, s.payer_id, s.reference, i.name " .
429 "FROM ar_activity AS a " .
430 "JOIN form_encounter AS fe ON fe.pid = a.pid AND fe.encounter = a.encounter " .
431 "JOIN forms AS f ON f.pid = a.pid AND f.encounter = a.encounter AND f.formdir = 'newpatient' " .
432 "LEFT JOIN ar_session AS s ON s.session_id = a.session_id " .
433 "LEFT JOIN insurance_companies AS i ON i.id = s.payer_id " .
434 "WHERE ( a.pay_amount != 0 OR a.adj_amount != 0 )";
436 if ($form_use_edate) {
437 $query .= " AND fe.date >= '$from_date 00:00:00' AND fe.date <= '$to_date 23:59:59'";
439 $query .= " AND ( ( s.deposit_date IS NOT NULL AND " .
440 "s.deposit_date >= '$from_date' AND s.deposit_date <= '$to_date' ) OR " .
441 "( s.deposit_date IS NULL AND a.post_time >= '$from_date 00:00:00' AND " .
442 "a.post_time <= '$to_date 23:59:59' ) )";
444 // If a procedure code was specified.
445 if ($form_proc_code && $form_proc_codetype) {
446 // 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.
447 $query .= " AND ( a.code_type = '$form_proc_codetype' OR a.code_type = '' ) AND a.code LIKE '$form_proc_code%'";
449 // If a facility was specified.
450 if ($form_facility) $query .= " AND fe.facility_id = '$form_facility'";
452 if ($form_use_edate) {
453 $query .= " ORDER BY s.reference, fe.date, a.pid, a.encounter, fe.id";
455 $query .= " ORDER BY s.reference, s.deposit_date, a.post_time, a.pid, a.encounter, fe.id";
458 $res = sqlStatement($query);
459 while ($row = sqlFetchArray($res)) {
460 if ($form_use_edate) {
461 $thedate = substr($row['date'], 0, 10);
462 } else if (!empty($row['deposit_date'])) {
463 $thedate = $row['deposit_date'];
465 $thedate = substr($row['post_time'], 0, 10);
467 // Compute reporting key: insurance company name or payment method.
468 if ($form_report_by == '1') {
469 if (empty($row['payer_id'])) {
472 if (empty($row['name'])) $rowmethod = xl('Unnamed insurance company');
473 else $rowmethod = $row['name'];
477 if (empty($row['session_id'])) {
478 $rowmethod = trim($row['memo']);
480 $rowmethod = trim($row['reference']);
482 if ($form_report_by != '3') {
483 // Extract only the first word as the payment method because any
484 // following text will be some petty detail like a check number.
485 $rowmethod = substr($rowmethod, 0, strcspn($rowmethod, ' /'));
489 thisLineItem($row['pid'], $row['encounter'], $row['code'], $thedate,
490 $rowmethod, $row['pay_amount'], $row['adj_amount'], $row['payer_type'],
491 $row['invoice_refno']);
494 // Not payer summary.
495 if ($form_report_by != '1' ||
$_POST['form_details']) {
497 if ($form_report_by == '1') { // by payer with details
498 // Sort and dump saved info, and consolidate items with all key
499 // fields being the same.
500 usort($insarray, 'payerCmp');
502 foreach ($insarray as $a) {
503 if (empty($a[4])) $a[4] = xl('Patient');
509 foreach (array(4,3,0,1,2,7) as $i) if ($a[$i] != $b[$i]) $match = false;
514 showLineItem($b[0], $b[1], $b[2], $b[3], $b[4], $b[5], $b[6], $b[7], $b[8]);
520 showLineItem($b[0], $b[1], $b[2], $b[3], $b[4], $b[5], $b[6], $b[7], $b[8]);
522 } // end by payer with details
524 // Print last method total.
526 <tr bgcolor
="#ddddff">
527 <td
class="detail" colspan
="<?php echo $showing_ppd ? 7 : 4; ?>">
528 <?php
echo xl('Total for ') . $paymethod ?
>
531 <?php
bucks($methodadjtotal) ?
>
534 <?php
bucks($methodpaytotal) ?
>
540 // Payer summary: need to sort and then print it all.
543 foreach ($insarray as $key => $value) {
544 if (empty($key)) $key = xl('Patient');
546 <tr bgcolor
="#ddddff">
547 <td
class="detail" colspan
="<?php echo $showing_ppd ? 7 : 4; ?>">
551 <?php
bucks($value[1]); ?
>
554 <?php
bucks($value[0]); ?
>
559 } // end payer summary
561 <tr bgcolor
="#ffdddd">
562 <td
class="detail" colspan
="<?php echo $showing_ppd ? 7 : 4; ?>">
563 <?php
xl('Grand Total','e') ?
>
566 <?php
bucks($grandadjtotal) ?
>
569 <?php
bucks($grandpaytotal) ?
>
574 } // end form refresh
581 <?php
echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?
>
588 <!-- stuff
for the popup calendar
-->
589 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
590 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
591 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
592 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
593 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
594 <script language
="Javascript">
595 Calendar
.setup({inputField
:"form_from_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
596 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});