added support for mariadb in the ubuntu-debian packages
[openemr.git] / interface / reports / receipts_by_method_report.php
blob680105bc74569194a4139540f56aba4defda41e6
1 <?php
2 // Copyright (C) 2006-2015 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;
207 </style>
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;
224 if (code) {
225 s = codetype + ':' + code;
226 } else {
227 s = '';
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);
237 </script>
239 <title><?xl('Receipts Summary','e')?></title>
240 </head>
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=''/>
252 <table>
253 <tr>
254 <td width='630px'>
255 <div style='float:left'>
257 <table class='text'>
258 <tr>
259 <td class='label'>
260 <?php xl('Report by','e'); ?>
261 </td>
262 <td>
263 <?php
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>&nbsp;\n"; ?>
271 </td>
273 <td>
274 <?php dropdown_facility(strip_escape_custom($form_facility), 'form_facility', false); ?>
275 </td>
277 <td>
278 <?php if (!$GLOBALS['simplified_demographics']) echo '&nbsp;' . xl('Procedure/Service') . ':'; ?>
279 </td>
280 <td>
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'"; ?> />
284 <br>
285 &nbsp;<input type='checkbox' name='form_details' value='1'<?php if ($_POST['form_details']) echo " checked"; ?> /><?xl('Details','e')?>
286 </td>
287 </tr>
288 <tr>
289 <td>&nbsp;</td>
290 <td>
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>
294 </select>
295 </td>
296 <td>
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'); ?>'>
302 </td>
303 <td class='label'>
304 <?php xl('To','e'); ?>:
305 </td>
306 <td>
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'); ?>'>
312 </td>
313 </tr>
314 </table>
316 </div>
318 </td>
319 <td align='left' valign='middle' height="100%">
320 <table style='border-left:1px solid; width:100%; height:100%' >
321 <tr>
322 <td>
323 <div style='margin-left:15px'>
324 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
325 <span>
326 <?php xl('Submit','e'); ?>
327 </span>
328 </a>
330 <?php if ($_POST['form_refresh']) { ?>
331 <a href='#' class='css_button' id='printbutton'>
332 <span>
333 <?php xl('Print','e'); ?>
334 </span>
335 </a>
336 <?php } ?>
337 </div>
338 </td>
339 </tr>
340 </table>
341 </td>
342 </tr>
343 </table>
345 </div> <!-- end of parameters -->
347 <?php
348 if ($_POST['form_refresh']) {
350 <div id="report_results">
352 <table>
354 <thead>
355 <th>
356 <?xl('Method','e')?>
357 </th>
358 <th>
359 <?xl('Date','e')?>
360 </th>
361 <th>
362 <?xl('Invoice','e')?>
363 </th>
364 <?php if ($showing_ppd) { ?>
365 <th>
366 <?xl('Patient','e')?>
367 </th>
368 <th>
369 <?xl('Policy','e')?>
370 </th>
371 <th>
372 <?xl('DOS','e')?>
373 </th>
374 <?php } ?>
375 <th>
376 <?xl('Procedure','e')?>
377 </th>
378 <th align="right">
379 <?xl('Adjustments','e')?>
380 </th>
381 <th align="right">
382 <?xl('Payments','e')?>
383 </th>
384 </thead>
385 <?php
388 if ($_POST['form_refresh']) {
389 $from_date = $form_from_date;
390 $to_date = $form_to_date;
392 $paymethod = "";
393 $paymethodleft = "";
394 $methodpaytotal = 0;
395 $grandpaytotal = 0;
396 $methodadjtotal = 0;
397 $grandadjtotal = 0;
400 // Get co-pays using the encounter date as the pay date. These will
401 // always be considered patient payments. Ignored if selecting by
402 // billing code.
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'";
438 } else {
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";
454 } else {
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'];
464 } else {
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'])) {
470 $rowmethod = '';
471 } else {
472 if (empty($row['name'])) $rowmethod = xl('Unnamed insurance company');
473 else $rowmethod = $row['name'];
476 else {
477 if (empty($row['session_id'])) {
478 $rowmethod = trim($row['memo']);
479 } else {
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');
501 $b = array();
502 foreach ($insarray as $a) {
503 if (empty($a[4])) $a[4] = xl('Patient');
504 if (empty($b)) {
505 $b = $a;
507 else {
508 $match = true;
509 foreach (array(4,3,0,1,2,7) as $i) if ($a[$i] != $b[$i]) $match = false;
510 if ($match) {
511 $b[5] += $a[5];
512 $b[6] += $a[6];
513 } else {
514 showLineItem($b[0], $b[1], $b[2], $b[3], $b[4], $b[5], $b[6], $b[7], $b[8]);
515 $b = $a;
519 if (!empty($b)) {
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 ?>
529 </td>
530 <td align="right">
531 <?php bucks($methodadjtotal) ?>
532 </td>
533 <td align="right">
534 <?php bucks($methodpaytotal) ?>
535 </td>
536 </tr>
537 <?php
540 // Payer summary: need to sort and then print it all.
541 else {
542 ksort($insarray);
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; ?>">
548 <?php echo $key; ?>
549 </td>
550 <td align="right">
551 <?php bucks($value[1]); ?>
552 </td>
553 <td align="right">
554 <?php bucks($value[0]); ?>
555 </td>
556 </tr>
557 <?php
558 } // end foreach
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') ?>
564 </td>
565 <td align="right">
566 <?php bucks($grandadjtotal) ?>
567 </td>
568 <td align="right">
569 <?php bucks($grandpaytotal) ?>
570 </td>
571 </tr>
573 <?php
574 } // end form refresh
577 </table>
578 </div>
579 <?php } else { ?>
580 <div class='text'>
581 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
582 </div>
583 <?php } ?>
585 </form>
586 </body>
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"});
597 </script>
599 </html>