MU2 for items e2 and f1.
[openemr.git] / interface / reports / receipts_by_method_report.php
blob3032bf959c1f8be627b8457ac0c8b057b3971f75
1 <?php
2 // Copyright (C) 2006-2016 Rod Roark <rod@sunsetsystems.com>
3 //
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.
27 $showing_ppd = true;
29 $insarray = array();
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);
43 return;
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) {
73 if ($paymethod) {
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 ?>
80 </td>
81 <td align="right">
82 <?php bucks($methodadjtotal) ?>
83 </td>
84 <td align="right">
85 <?php bucks($methodpaytotal) ?>
86 </td>
87 </tr>
88 <?php
90 $methodpaytotal = 0;
91 $methodadjtotal = 0;
92 $paymethod = $rowmethod;
93 $paymethodleft = $paymethod;
96 if ($_POST['form_details']) {
99 <tr>
100 <td class="detail">
101 <?php echo $paymethodleft; $paymethodleft = "&nbsp;" ?>
102 </td>
103 <td>
104 <?php echo oeFormatShortDate($transdate) ?>
105 </td>
106 <td class="detail">
107 <?php echo $invnumber ?>
108 </td>
110 <?php
111 if ($showing_ppd) {
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'];
120 echo " </td>\n";
122 echo " <td class='dehead'>\n";
123 if ($payer_type) {
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'];
129 echo " </td>\n";
131 echo " <td class='dehead'>\n";
132 echo " " . oeFormatShortDate($dos) . "\n";
133 echo " </td>\n";
137 <td>
138 <?php echo $memo ?>
139 </td>
140 <td align="right">
141 <?php bucks($rowadjamount) ?>
142 </td>
143 <td align="right">
144 <?php bucks($rowpayamount) ?>
145 </td>
146 </tr>
147 <?php
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;
162 return 0;
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];
180 <html>
181 <head>
183 <?php if (function_exists('html_header_show')) html_header_show(); ?>
184 <style type="text/css">
185 /* specifically include & exclude from printing */
186 @media print {
187 #report_parameters {
188 visibility: hidden;
189 display: none;
191 #report_parameters_daterange {
192 visibility: visible;
193 display: inline;
195 #report_results {
196 margin-top: 30px;
200 /* specifically exclude some from the screen */
201 @media screen {
202 #report_parameters_daterange {
203 visibility: hidden;
204 display: none;
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;
215 </style>
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;
234 if (code) {
235 s = codetype + ':' + code;
236 } else {
237 s = '';
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);
247 </script>
249 <title><?xl('Receipts Summary','e')?></title>
250 </head>
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=''/>
262 <table>
263 <tr>
264 <td width='630px'>
265 <div style='float:left'>
267 <table class='text'>
268 <tr>
269 <td class='label'>
270 <?php xl('Report by','e'); ?>
271 </td>
272 <td>
273 <?php
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>&nbsp;\n"; ?>
281 </td>
283 <td>
284 <?php dropdown_facility(strip_escape_custom($form_facility), 'form_facility', false); ?>
285 </td>
287 <td>
288 <?php if (!$GLOBALS['simplified_demographics']) echo '&nbsp;' . xl('Procedure/Service') . ':'; ?>
289 </td>
290 <td>
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'"; ?> />
294 <br>
295 &nbsp;<input type='checkbox' name='form_details' value='1'<?php if ($_POST['form_details']) echo " checked"; ?> /><?xl('Details','e')?>
296 </td>
297 </tr>
298 <tr>
299 <td>&nbsp;</td>
300 <td>
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>
304 </select>
305 </td>
306 <td>
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'); ?>'>
312 </td>
313 <td class='label'>
314 <?php xl('To','e'); ?>:
315 </td>
316 <td>
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'); ?>'>
322 </td>
323 </tr>
324 </table>
326 </div>
328 </td>
329 <td align='left' valign='middle' height="100%">
330 <table style='border-left:1px solid; width:100%; height:100%' >
331 <tr>
332 <td>
333 <div style='margin-left:15px'>
334 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
335 <span>
336 <?php xl('Submit','e'); ?>
337 </span>
338 </a>
340 <?php if ($_POST['form_refresh']) { ?>
341 <a href='#' class='css_button' id='printbutton'>
342 <span>
343 <?php xl('Print','e'); ?>
344 </span>
345 </a>
346 <?php } ?>
347 </div>
348 </td>
349 </tr>
350 </table>
351 </td>
352 </tr>
353 </table>
355 </div> <!-- end of parameters -->
357 <?php
358 if ($_POST['form_refresh']) {
360 <div id="report_results">
362 <table width='98%' id='mymaintable' class='mymaintable'>
364 <thead>
365 <tr bgcolor="#dddddd">
366 <th>
367 <?php xl('Method','e') ?>
368 </th>
369 <th>
370 <?php xl('Date','e') ?>
371 </th>
372 <th>
373 <?php xl('Invoice','e') ?>
374 </th>
375 <?php if ($showing_ppd) { ?>
376 <th>
377 <?xl('Patient','e')?>
378 </th>
379 <th>
380 <?xl('Policy','e')?>
381 </th>
382 <th>
383 <?xl('DOS','e')?>
384 </th>
385 <?php } ?>
386 <th>
387 <?xl('Procedure','e')?>
388 </th>
389 <th align="right">
390 <?xl('Adjustments','e')?>
391 </th>
392 <th align="right">
393 <?xl('Payments','e')?>
394 </th>
395 </tr>
396 </thead>
397 <tbody>
398 <?php
400 if ($_POST['form_refresh']) {
401 $from_date = $form_from_date;
402 $to_date = $form_to_date;
404 $paymethod = "";
405 $paymethodleft = "";
406 $methodpaytotal = 0;
407 $grandpaytotal = 0;
408 $methodadjtotal = 0;
409 $grandadjtotal = 0;
412 // Get co-pays using the encounter date as the pay date. These will
413 // always be considered patient payments. Ignored if selecting by
414 // billing code.
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'";
450 } else {
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";
466 } else {
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'];
476 } else {
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'])) {
482 $rowmethod = '';
483 } else {
484 if (empty($row['name'])) $rowmethod = xl('Unnamed insurance company');
485 else $rowmethod = $row['name'];
488 else {
489 if (empty($row['session_id'])) {
490 $rowmethod = trim($row['memo']);
491 } else {
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');
513 $b = array();
514 foreach ($insarray as $a) {
515 if (empty($a[4])) $a[4] = xl('Patient');
516 if (empty($b)) {
517 $b = $a;
519 else {
520 $match = true;
521 foreach (array(4,3,0,1,2,7) as $i) if ($a[$i] != $b[$i]) $match = false;
522 if ($match) {
523 $b[5] += $a[5];
524 $b[6] += $a[6];
525 } else {
526 showLineItem($b[0], $b[1], $b[2], $b[3], $b[4], $b[5], $b[6], $b[7], $b[8]);
527 $b = $a;
531 if (!empty($b)) {
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 ?>
541 </td>
542 <td align="right">
543 <?php bucks($methodadjtotal) ?>
544 </td>
545 <td align="right">
546 <?php bucks($methodpaytotal) ?>
547 </td>
548 </tr>
549 <?php
552 // Payer summary: need to sort and then print it all.
553 else {
554 ksort($insarray);
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; ?>">
560 <?php echo $key; ?>
561 </td>
562 <td align="right">
563 <?php bucks($value[1]); ?>
564 </td>
565 <td align="right">
566 <?php bucks($value[0]); ?>
567 </td>
568 </tr>
569 <?php
570 } // end foreach
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') ?>
576 </td>
577 <td align="right">
578 <?php bucks($grandadjtotal) ?>
579 </td>
580 <td align="right">
581 <?php bucks($grandpaytotal) ?>
582 </td>
583 </tr>
585 <?php
586 } // end form refresh
589 </tbody>
590 </table>
591 </div>
592 <?php } else { ?>
593 <div class='text'>
594 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
595 </div>
596 <?php } ?>
598 </form>
599 </body>
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"});
610 </script>
612 </html>