Highway to PSR2
[openemr.git] / portal / report / pat_ledger.php
blob78157727b8ee8ee4350f9e4791171da2cbaeeac6
1 <?php
2 /**
3 * This is a report to create a patient ledger of charges with payments
4 * applied.
6 * Copyright (C) 2015 Rich Genandt <rgenandt@gmail.com>
8 * LICENSE: This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
19 * @package OpenEMR
20 * @author WMT
21 * @author Terry Hill <terry@lillysystems.com>
22 * @link http://www.open-emr.org
24 require_once("./../verify_session.php");
25 $ignoreAuth = true;
26 global $ignoreAuth;
29 require_once('../../interface/globals.php');
30 require_once($GLOBALS['srcdir'].'/patient.inc');
31 require_once($GLOBALS['srcdir'].'/acl.inc');
32 require_once($GLOBALS['srcdir'].'/formatting.inc.php');
33 require_once($GLOBALS['srcdir'].'/options.inc.php');
34 require_once($GLOBALS['srcdir'].'/formdata.inc.php');
35 require_once($GLOBALS['srcdir'].'/appointments.inc.php');
37 $enc_units = $total_units = 0;
38 $enc_chg = $total_chg = 0;
39 $enc_pmt = $total_pmt = 0;
40 $enc_adj = $total_adj = 0;
41 $enc_bal = $total_bal = 0;
42 $bgcolor = "#FFFFDD";
43 $orow = 0;
45 $pat_pid = $_GET['patient_id'];
46 $type_form = $_GET['form'];
48 //if (! acl_check('acct', 'rep')) die(xlt("Unauthorized access."));
50 function GetAllUnapplied($pat = '', $from_dt = '', $to_dt = '')
52 $all = array();
53 if (!$pat) {
54 return($all);
57 $sql = "SELECT ar_session.*, ins.name, " .
58 "pat.lname, pat.fname, pat.mname, " .
59 "(SELECT SUM(ar_activity.pay_amount) FROM ar_activity WHERE " .
60 "ar_activity.session_id = ar_session.session_id) AS applied " .
61 "FROM ar_session " .
62 "LEFT JOIN insurance_companies AS ins on ar_session.payer_id = ins.id " .
63 "LEFT JOIN patient_data AS pat on ar_session.patient_id = pat.pid " .
64 "WHERE " .
65 "ar_session.created_time >= ? AND ar_session.created_time <= ? " .
66 "AND ar_session.patient_id=?";
67 $result = sqlStatement($sql, array($from_dt, $to_dt, $pat));
68 $iter = 0;
69 while ($row = sqlFetchArray($result)) {
70 $all[$iter] = $row;
71 $iter++;
74 return($all);
77 function User_Id_Look($thisField)
79 if (!$thisField) {
80 return '';
83 $ret = '';
84 $rlist= sqlStatement("SELECT lname, fname, mname FROM users WHERE id=?", array($thisField));
85 $rrow= sqlFetchArray($rlist);
86 if ($rrow) {
87 $ret = $rrow{'lname'}.', '.$rrow{'fname'}.' '.$rrow{'mname'};
90 return $ret;
93 function List_Look($thisData, $thisList)
95 if ($thisList == 'occurrence') {
96 if (!$thisData || $thisData == '') {
97 return xl('Unknown or N/A');
101 if ($thisData == '') {
102 return '';
105 $fres=sqlStatement("SELECT title FROM list_options WHERE list_id=? ".
106 "AND option_id=?", array($thisList, $thisData));
107 if ($fres) {
108 $rret=sqlFetchArray($fres);
109 $dispValue= xl_list_label($rret{'title'});
110 if ($thisList == 'occurrence' && $dispValue == '') {
111 $dispValue = xl('Unknown or N/A');
113 } else {
114 $dispValue= xl('Not Found');
117 return $dispValue;
120 function GetAllCredits($enc = '', $pat = '')
122 $all = array();
123 if (!$enc || !$pat) {
124 return($all);
127 $sql = "SELECT activity.*, session.*, ins.name FROM ar_activity AS ".
128 "activity LEFT JOIN ar_session AS session USING (session_id) ".
129 "LEFT JOIN insurance_companies AS ins ON session.payer_id = ".
130 "ins.id WHERE encounter=? AND pid=? ".
131 "ORDER BY sequence_no";
132 $result = sqlStatement($sql, array($enc, $pat));
133 $iter = 0;
134 while ($row = sqlFetchArray($result)) {
135 $all[$iter] = $row;
136 $iter++;
139 return($all);
141 function PrintEncHeader($dt, $rsn, $dr)
143 global $bgcolor, $orow;
144 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
145 echo "<tr bgcolor='#FFFFFF'>";
146 if (strlen($rsn) > 50) {
147 $rsn = substr($rsn, 0, 50).'...';
150 echo "<td colspan='4'><span class='bold'>".xlt('Encounter Dt / Rsn'). ": </span><span class='detail'>".text(substr($dt, 0, 10))." / ".text($rsn)."</span></td>";
151 echo "<td colspan='5'><span class='bold'>" . xlt('Provider'). ": </span><span class='detail'>".text(User_Id_Look($dr))."</span></td>";
152 echo "</tr>\n";
153 $orow++;
155 function PrintEncFooter()
157 global $enc_units, $enc_chg, $enc_pmt, $enc_adj, $enc_bal;
158 echo "<tr bgcolor='#DDFFFF'>";
159 echo "<td colspan='3'>&nbsp;</td>";
160 echo "<td class='detail'>". xlt('Encounter Balance').":</td>";
161 echo "<td class='detail' style='text-align: right;'>".text($enc_units)."</td>";
162 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_chg))."</td>";
163 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_pmt))."</td>";
164 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_adj))."</td>";
165 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_bal))."</td>";
166 echo "</tr>\n";
168 function PrintCreditDetail($detail, $pat, $unassigned = false)
170 global $enc_pmt, $total_pmt, $enc_adj, $total_adj, $enc_bal, $total_bal;
171 global $bgcolor, $orow, $enc_units, $enc_chg;
172 foreach ($detail as $pmt) {
173 if ($unassigned) {
174 if (($pmt['pay_total'] - $pmt['applied']) == 0) {
175 continue;
179 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
180 $print = "<tr bgcolor='" . attr($bgcolor) . "'>";
181 $print .= "<td class='detail'>&nbsp;</td>";
182 $method = List_Look($pmt['payment_method'], 'payment_method');
183 $desc = $pmt['description'];
184 $ref = $pmt['reference'];
185 if ($unassigned) {
186 $memo = List_Look($pmt['adjustment_code'], 'payment_adjustment_code');
187 } else {
188 $memo = $pmt['memo'];
191 $description = $method;
192 if ($ref) {
193 if ($description) {
194 $description .= ' - ';
197 $description .= $ref;
200 if ($desc) {
201 if ($description) {
202 $description .= ': ';
205 $description .= $desc;
208 if ($memo) {
209 if ($description) {
210 $description .= ' ';
213 $description .= '['.$memo.']';
216 $print .= "<td class='detail' colspan='2'>".
217 text($description)."&nbsp;</td>";
218 $payer = ($pmt['name'] == '') ? xl('Patient') : $pmt['name'];
219 if ($unassigned) {
220 $pmt_date = substr($pmt['post_to_date'], 0, 10);
221 } else {
222 $pmt_date = substr($pmt['post_time'], 0, 10);
225 $print .= "<td class='detail'>".
226 text($pmt_date)."&nbsp;/&nbsp;".text($payer)."</td>";
227 $type = List_Look($pmt['payment_type'], 'payment_type');
228 $print .= "<td class='detail'>".text($type)."&nbsp;</td>";
229 if ($unassigned) {
230 $pmt_amt = $pmt['pay_total'] - $pmt['applied'];
231 $uac_bal = oeFormatMoney($pmt_amt * -1);
232 $uac_appl = oeFormatMoney($pmt['applied']);
233 $uac_total = oeFormatMoney($pmt['pay_total']);
234 $pmt_amt = $pmt['pay_total'];
235 $total_pmt = $total_pmt - $uac_bal;
236 } else {
237 $uac_total = '';
238 $uac_bal = '';
239 $uac_appl = '';
240 $pmt_amt = $pmt['pay_amount'];
241 $adj_amt = $pmt['adj_amount'];
242 $enc_pmt = $enc_pmt + $pmt['pay_amount'];
243 $total_pmt = $total_pmt + $pmt['pay_amount'];
244 $enc_adj = $enc_adj + $pmt['adj_amount'];
245 $total_adj = $total_adj + $pmt['adj_amount'];
248 $print_pmt = '';
249 if ($pmt_amt != 0) {
250 $print_pmt = oeFormatMoney($pmt_amt);
253 $print_adj = '';
254 if ($adj_amt != 0) {
255 $print_adj = oeFormatMoney($adj_amt);
258 $print .= "<td class='detail' style='text-align: right;'>".text($uac_appl)."&nbsp;</td>";
259 $print .= "<td class='detail' style='text-align: right;'>".text($print_pmt)."&nbsp;</td>";
260 $print .= "<td class='detail' style='text-align: right;'>".text($print_adj)."&nbsp;</td>";
261 $print .= "<td class='detail' style='text-align: right;'>".text($uac_bal)."&nbsp;</td>";
262 $print .= "</tr>\n";
263 echo $print;
264 if ($pmt['follow_up_note'] != '') {
265 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
266 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
267 $print .= "<td class='detail' colspan='2'>&nbsp;</td>";
268 $print .= "<td colspan='7'>". xlt('Follow Up Note') .": ";
269 $print .= text($pmt['follow_up_note']);
270 $print .= "</td></tr>\n";
271 echo $print;
274 if ($unassigned) {
275 $total_bal = $total_bal + $uac_bal;
276 } else {
277 $enc_bal = $enc_bal - $pmt_amt - $adj_amt;
278 $total_bal = $total_bal - $pmt_amt - $adj_amt;
281 $orow++;
284 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
286 if (!isset($_REQUEST['form_from_date'])) {
287 $_REQUEST['form_from_date'] = '';
290 if (!isset($_REQUEST['form_to_date'])) {
291 $_REQUEST['form_to_date'] = '';
294 if (!isset($_REQUEST['form_facility'])) {
295 $_REQUEST['form_facility'] = '';
298 if (!isset($_REQUEST['form_provider'])) {
299 $_REQUEST['form_provider'] = '';
302 if ($type_form=='0') {
303 if (!isset($_REQUEST['form_patient'])) {
304 $_REQUEST['form_patient'] = '';
307 if (!isset($_REQUEST['form_pid'])) {
308 $_REQUEST['form_pid'] = '';
310 } else {
311 if (!isset($_REQUEST['form_patient'])) {
312 $_REQUEST['form_patient'] = $pat_pid;
315 if (!isset($_REQUEST['form_pid'])) {
316 $_REQUEST['form_pid'] = $pat_pid;
320 if (!isset($_REQUEST['form_csvexport'])) {
321 $_REQUEST['form_csvexport'] = '';
324 if (!isset($_REQUEST['form_refresh'])) {
325 $_REQUEST['form_refresh'] = '';
328 if (!isset($_REQUEST['$form_dob'])) {
329 $_REQUEST['$form_dob'] = '';
332 if (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'Y') {
333 $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1);
334 $last_year = mktime(0, 0, 0, date('m'), date('d'), date('Y')-$ledger_time);
335 } elseif (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'M') {
336 $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1);
337 $last_year = mktime(0, 0, 0, date('m')-$ledger_time, date('d'), date('Y'));
338 } elseif (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'D') {
339 $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1);
340 $last_year = mktime(0, 0, 0, date('m'), date('d')-$ledger_time, date('Y'));
343 $form_from_date = date('Y-m-d', $last_year);
344 if ($_REQUEST['form_from_date']) {
345 $form_from_date = fixDate($_REQUEST['form_from_date'], $last_year);
348 $form_to_date = fixDate($_REQUEST['form_to_date'], date('Y-m-d'));
349 $form_facility = $_REQUEST['form_facility'];
350 $form_provider = $_REQUEST['form_provider'];
351 $form_patient = $_REQUEST['form_patient'];
352 $form_pid = $_REQUEST['form_pid'];
353 $form_dob = $_REQUEST['form_dob'];
355 if ($_REQUEST['form_csvexport']) {
356 header("Pragma: public");
357 header("Expires: 0");
358 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
359 header("Content-Type: application/force-download");
360 header("Content-Disposition: attachment; filename=svc_financial_report_".attr($form_from_date)."--".attr($form_to_date).".csv");
361 header("Content-Description: File Transfer");
362 } else {
364 <html>
365 <head>
366 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
367 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
369 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
370 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
371 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js?v=<?php echo $v_js_includes; ?>"></script>
372 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
374 <script type="text/javascript">
375 var mypcc = '<?php echo $GLOBALS['phone_country_code']; ?>';
376 var pt_name;
377 var pt_id;
378 function checkSubmit() {
379 var pat = document.forms[0].elements['form_patient'].value;
380 if(!pat || pat == 0) {
381 alert('<?php echo xls('A Patient Must Be Selected to Generate This Report') ?>');
382 return false;
384 document.forms[0].elements['form_refresh'].value = true;
385 document.forms[0].elements['form_csvexport'].value = '';
386 document.forms[0].submit();
388 function setpatient(pid, lname, fname, dob) {
389 document.forms[0].elements['form_patient'].value = lname + ', ' + fname;
390 document.forms[0].elements['form_pid'].value = pid;
391 document.forms[0].elements['form_dob'].value = dob;
393 function sel_patient() {
394 dlgopen('../main/calendar/find_patient_popup.php?pflag=0', '_blank', 500, 400);
396 </script>
398 <style type="text/css">
400 /* specifically include & exclude from printing */
401 @media print {
402 #report_parameters {
403 visibility: hidden;
404 display: none;
406 #report_parameters_daterange {
407 visibility: visible;
408 display: inline;
410 #report_results {
411 margin-top: 30px;
413 #report_header {
414 visibility: visible;
415 display: inline;
417 #title {
418 visibility: hidden;
419 display: none;
422 /* specifically exclude some from the screen */
423 @media screen {
424 #report_parameters_daterange {
425 visibility: hidden;
426 display: none;
428 #report_header {
429 visibility: hidden;
430 display: none;
432 #title {
433 visibility: visible;
434 display: inline;
437 </style>
439 <title><?php echo xlt('Patient Ledger by Date') ?></title>
441 <script>
442 $(document).ready(function() {
443 $('.datepicker').datetimepicker({
444 <?php $datetimepicker_timepicker = false; ?>
445 <?php $datetimepicker_formatInput = false; ?>
446 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
447 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
450 </script>
452 </head>
453 <body class="body_top">
454 <?php if ($type_form == '0') { ?>
455 <span class='title' id='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Patient Ledger by Date'); ?></span>
456 <?php } else { ?>
457 <span class='title' id='title'><?php echo xlt('Patient Ledger'); ?></span>
458 <?php } ?>
459 <form method='post' action='./pat_ledger.php?form=<?php echo attr($type_form);?>&patient_id=<?php echo attr($form_pid);?>' id='theform'>
460 <div id="report_parameters">
461 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
462 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
463 <table>
464 <tr>
465 <?php if ($type_form == '1') { ?>
466 <td width='35%'>
467 <?php } else { ?>
468 <td width='70%'>
469 <?php } ?>
470 <div style='float:left'>
471 <table class='text'>
472 <tr>
473 <?php if ($type_form == '0') { ?>
474 <td class='label_custom'>
475 <?php echo xlt('Facility'); ?>:
476 </td>
477 <td>
478 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
479 </td>
480 <td><?php echo xlt('Provider'); ?>:</td>
481 <td><?php
482 $query = "SELECT id, lname, fname FROM users WHERE ".
483 "authorized=1 AND active!=0 ORDER BY lname, fname";
484 $ures = sqlStatement($query);
485 echo " <select name='form_provider'>\n";
486 echo " <option value=''>-- " . xlt('All') . " --\n";
487 while ($urow = sqlFetchArray($ures)) {
488 $provid = $urow['id'];
489 echo " <option value='" . attr($provid) ."'";
490 if ($provid == $_REQUEST['form_provider']) {
491 echo " selected";
494 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
497 echo " </select>\n";
498 ?></td>
499 </tr><tr>
500 <?php } ?>
501 <td colspan="2">
502 <?php echo xlt('From'); ?>:&nbsp;&nbsp;&nbsp;&nbsp;
503 <input type='text' class='datepicker' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>' title='yyyy-mm-dd'>
504 </td>
505 <td class='label_custom'>
506 <?php echo xlt('To'); ?>:
507 </td>
508 <td>
509 <input type='text' class='datepicker' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>' title='yyyy-mm-dd'>
510 </td>
511 <?php if ($type_form == '0') { ?>
512 <td><span class='label_custom'><?php echo xlt('Patient'); ?>:&nbsp;&nbsp;</span></td>
513 <td>
514 <input type='text' size='20' name='form_patient' style='width:100%;cursor:pointer;cursor:hand' id='form_patient' value='<?php echo attr($form_patient) ? attr($form_patient) : xla('Click To Select'); ?>' onclick='sel_patient()' title='<?php echo xla('Click to select patient'); ?>' />
515 <?php } else { ?>
516 <input type='hidden' name='form_patient' value='<?php echo attr($form_patient); ?>' />
517 <?php } ?>
518 <input type='hidden' name='form_pid' value='<?php echo attr($form_pid); ?>' />
519 <input type='hidden' name='form_dob' value='<?php echo attr($form_dob); ?>' />
521 </td>
522 </tr>
523 </table>
524 </div>
525 </td>
526 <td align='left' valign='middle' height="100%">
527 <table style='border-left:1px solid; width:100%; height:100%' >
528 <tr>
529 <td>
530 <div style='margin-left:15px'>
531 <a href='#' class='css_button' onclick="checkSubmit();" >
532 <span><?php echo xlt('Submit'); ?></span></a>
534 <?php if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) { ?>
535 <div id="controls"> <!-- print is patched out until I feel like convert to patient side -->
536 <!-- <a href='#' class='css_button' id='printbutton'>
537 <span><?php //echo xlt('Print Ledger'); ?></span></a> -->
538 <?php if ($type_form == '1') { ?>
539 <!-- <a href="../patient_file/summary/demographics.php" <?php // if (!$GLOBALS['concurrent_layout']) echo "target='Main'"; ?> class="css_button" onclick="top.restoreSession()">-->
540 <!-- <span><?php //echo xlt('Back To Patient');?></span></a> -->
541 <?php } ?>
542 </div>
543 <?php } ?>
544 </div>
545 </td>
546 </tr>
547 </table>
548 </td>
549 </tr>
550 </table>
551 </div> <!-- end of parameters -->
553 <?php
554 } // end not export
555 $from_date = $form_from_date . ' 00:00:00';
556 $to_date = $form_to_date . ' 23:59:59';
557 if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) {
558 $rows = array();
559 $sqlBindArray = array();
560 $query = "select b.code_type, b.code, b.code_text, b.pid, b.provider_id, ".
561 "b.billed, b.payer_id, b.units, b.fee, b.bill_date, b.id, ".
562 "ins.name, ".
563 "fe.encounter, fe.date, fe.reason, fe.provider_id ".
564 "FROM form_encounter AS fe ".
565 "LEFT JOIN billing AS b ON b.pid=fe.pid AND b.encounter=fe.encounter ".
566 "LEFT JOIN insurance_companies AS ins ON b.payer_id = ins.id ".
567 "LEFT OUTER JOIN code_types AS c ON c.ct_key = b.code_type ".
568 "WHERE fe.date >= ? AND fe.date <= ? AND fe.pid = ? ";
569 array_push($sqlBindArray, $from_date, $to_date, $form_pid);
570 if ($form_facility) {
571 $query .= "AND fe.facility_id = ? ";
572 array_push($sqlBindArray, $form_facility);
575 if ($form_provider) {
576 $query .= "AND b.provider_id = ? ";
577 array_push($sqlBindArray, $form_provider);
580 $query .= "AND c.ct_proc = '1' ";
581 $query .= "AND activity > 0 ORDER BY fe.date, fe.id ";
582 $res = sqlStatement($query, $sqlBindArray);
584 if ($_REQUEST['form_csvexport']) {
585 // CSV headers:
586 if (true) {
587 echo '"Code/Enc Dt",';
588 echo '"Description",';
589 echo '"Billed/Who",';
590 echo '"Type/Units",';
591 echo '"Chg/Pmt Amount",'."\n";
593 } else {
594 if (!$form_facility) {
595 $form_facility = '3';
598 $facility = sqlQuery("SELECT * FROM facility WHERE id=?", array($form_facility));
599 $patient = sqlQuery("SELECT * from patient_data WHERE pid=?", array($form_patient));
600 $pat_dob = $patient['DOB'];
601 $pat_name = $patient['fname']. ' ' . $patient['lname'];
603 <div id="report_header">
604 <table width="98%" border="0" cellspacing="0" cellpadding="0">
605 <tr>
606 <td class="title" ><?php echo text($facility{'name'}); ?></td>
607 </tr>
608 <tr>
609 <td class="title" ><?php echo text($facility{'street'}); ?></td>
610 </tr>
611 <tr>
612 <td class="title" ><?php echo text($facility{'city'}).", ".text($facility{'state'})." ".text($facility{'postal_code'}); ?></td>
613 </tr>
614 <tr>
615 <td class="title" ><?php echo xlt('Phone').': ' .text($facility{'phone'}); ?></td>
616 </tr>
617 <tr>
618 <td class="title" ><?php echo xlt('Tax Id').': ' .text($facility{'federal_ein'}); ?></td>
619 </tr>
620 <tr><td>&nbsp;</td></tr>
621 <tr>
622 <td class="title" ><?php echo xlt('Patient Ledger'); ?></td>
623 </tr>
624 <tr>
625 <?php
626 $title = xl('All Providers');
627 if ($form_provider) {
628 $title = xl('For Provider') . ': '.User_Id_Look($form_provider);
631 <td class="title" ><?php echo text($title); ?></td>
632 </tr>
633 <tr>
634 <?php
635 $title = xl('For Dates') . ': '.$form_from_date.' - '.$form_to_date;
637 <td class="title" ><?php echo text($title); ?></td>
638 </tr>
639 </table>
640 <br/>
641 <table width="100%" border="0" cellspacing="0" cellpadding="0">
642 <tr>
643 <td class='bold' ><?php echo xlt('Date')?>:
644 <?php echo text(date('Y-m-d')); ?></td>
645 <td class='bold' ><?php echo xlt('Patient')?>:
646 <?php if ($type_form == '1') { ?>
647 <?php echo text($pat_name); ?></td>
648 <?php } else { ?>
649 <?php echo text($form_patient); ?></td>
650 <?php } ?>
651 <td class='bold' ><?php echo xlt('DOB')?>:
652 <?php if ($type_form == '1') { ?>
653 <?php echo text($pat_dob);?></td>
654 <?php } else { ?>
655 <?php echo text($form_dob); ?></td>
656 <?php } ?>
657 <td class='bold' > <?php echo xlt('ID')?>:
658 <?php echo text($form_pid);?></td>
659 </tr>
660 </table>
661 </div>
662 <div id="report_results">
663 <table >
664 <tr>
665 <td class='bold' ><?php echo xlt('Code'); ?></td>
666 <td colspan="2" class='bold' ><?php echo xlt('Description'); ?></td>
667 <td class='bold' ><?php echo xlt('Billed Date'); ?> / <?php echo xlt('Payor'); ?></td>
668 <td class='bold' ><?php echo xlt('Type'); ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
669 <?php echo xlt('Units'); ?></td>
670 <td class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Charge'); ?></td>
671 <td align='right' class='bold' >&nbsp;&nbsp;<?php echo xlt('Payment'); ?></td>
672 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Adjustment'); ?></td>
673 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('Balance'); ?></td>
674 </tr>
675 <tr>
676 <td>&nbsp;&nbsp;&nbsp;</td>
677 <td colspan="2" >&nbsp;&nbsp;&nbsp;</td>
678 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
679 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
680 &nbsp;&nbsp;&nbsp;</td>
681 <td class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Appl'); ?></td>
682 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Tot'); ?></td>
683 <td>&nbsp;</td>
684 <td>&nbsp;</td>
685 </tr>
686 <?php
689 $orow = 0;
690 $prev_encounter_id = -1;
691 $hdr_printed = false;
692 $prev_row = array();
693 while ($erow = sqlFetchArray($res)) {
694 $print = '';
695 $csv = '';
696 if ($erow['encounter'] != $prev_encounter_id) {
697 if ($prev_encounter_id != -1) {
698 $credits = GetAllCredits($prev_encounter_id, $form_pid);
699 if (count($credits) > 0) {
700 if (!$hdr_printed) {
701 PrintEncHeader(
702 $prev_row{'date'},
703 $prev_row{'reason'},
704 $prev_row{'provider_id'}
708 PrintCreditDetail($credits, $form_pid);
711 if ($hdr_printed) {
712 PrintEncFooter();
715 $hdr_printed = false;
718 $enc_units = $enc_chg = $enc_pmt = $enc_adj = $enc_bal = 0;
721 if ($erow{'id'}) {
722 // Now print an encounter heading line -
723 if (!$hdr_printed) {
724 PrintEncHeader(
725 $erow{'date'},
726 $erow{'reason'},
727 $erow{'provider_id'}
729 $hdr_printed = true;
732 $code_desc = $erow['code_text'];
733 if (strlen($code_desc) > 50) {
734 $code_desc = substr($code_desc, 0, 50).'...';
737 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
738 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
739 $print .= "<td class='detail'>".text($erow['code'])."</td>";
740 $print .= "<td class='detail' colspan='2'>".text($code_desc)."</td>";
741 $who = ($erow['name'] == '') ? xl('Self') : $erow['name'];
742 $bill = substr($erow['bill_date'], 0, 10);
743 if ($bill == '') {
744 $bill = 'unbilled';
747 $print .= "<td class='detail'>".text($bill)."&nbsp;/&nbsp;".text($who)."</td>";
748 $print .= "<td class='detail' style='text-align: right;'>". text($erow['units'])."</td>";
749 $print .= "<td class='detail' style='text-align: right;'>". text(oeFormatMoney($erow['fee']))."</td>";
750 $print .= "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
751 $print .= "</tr>\n";
753 $total_units += $erow['units'];
754 $total_chg += $erow['fee'];
755 $total_bal += $erow['fee'];
756 $enc_units += $erow['units'];
757 $enc_chg += $erow['fee'];
758 $enc_bal += $erow['fee'];
759 $orow++;
761 if ($_REQUEST['form_csvexport']) {
762 echo $csv;
763 } else {
764 echo $print;
768 $prev_encounter_id = $erow{'encounter'};
769 $prev_row = $erow;
772 if ($prev_encounter_id != -1) {
773 $credits = GetAllCredits($prev_encounter_id, $form_pid);
774 if (count($credits) > 0) {
775 if (!$hdr_printed) {
776 PrintEncHeader(
777 $prev_row{'date'},
778 $prev_row{'reason'},
779 $prev_row{'provider_id'}
783 PrintCreditDetail($credits, $form_pid);
786 if ($hdr_printed) {
787 PrintEncFooter();
791 // This is the end of the encounter/charge loop -
792 $uac = GetAllUnapplied($form_pid, $from_date, $to_date);
793 if (count($uac) > 0) {
794 if ($orow) {
795 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
796 echo "<tr bgcolor='#FFFFFF'><td colspan='9'>&nbsp;</td></tr>\n";
799 PrintCreditDetail($uac, $form_pid, true);
802 if (!$_REQUEST['form_csvexport'] && $orow) {
803 echo "<tr bgcolor='#DDFFFF'>\n";
804 echo " <td colspan='2'>&nbsp;</td>";
805 echo " <td class='bold' colspan='2'>" . xlt("Grand Total") ."</td>\n";
806 echo " <td class='bold' style='text-align: right;'>". text($total_units) ."</td>\n";
807 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_chg)) ."</td>\n";
808 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_pmt)) ."</td>\n";
809 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_adj)) ."</td>\n";
810 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_bal)) . "</td>\n";
811 echo " </tr>\n";
813 </table>
814 <tr><td>&nbsp;</td></tr><br><br>
815 <?php if ($GLOBALS['print_next_appointment_on_ledger'] == 1) {
816 $next_day = mktime(0, 0, 0, date('m'), date('d')+1, date('Y'));
817 # add one day to date so it will not get todays appointment
818 $current_date2 = date('Y-m-d', $next_day);
819 $events = fetchNextXAppts($current_date2, $form_pid);
820 $next_appoint_date = oeFormatShortDate($events[0]['pc_eventDate']);
821 $next_appoint_time = substr($events[0]['pc_startTime'], 0, 5);
822 if (strlen(umname) != 0) {
823 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['umname'] . ' ' . $events[0]['ulname'];
824 } else {
825 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['ulname'];
828 if (strlen($next_appoint_time) != 0) {
830 <tr>
831 <td class="title" ><?php echo xlt('Next Appointment Date') . ': ' . text($next_appoint_date) . ' ' . xlt('Time') . ' ' . text($next_appoint_time) . ' ' . xlt('Provider') . ' ' . text($next_appoint_provider); ?></td>
832 </tr>
834 <?php
836 } // end ($GLOBALS['print_next_appointment_on_ledger'] == 1)
837 } // end (!$_REQUEST['form_csvexport'] && $orow)
838 echo "</div>\n";
841 if (! $_REQUEST['form_csvexport']) {
842 if ($_REQUEST['form_refresh'] && $orow <= 0) {
843 echo "<span style='font-size:10pt;'>";
844 echo xlt('No matches found. Try search again.');
845 echo "</span>";
846 echo '<script>document.getElementById("report_results").style.display="none";</script>';
847 echo '<script>document.getElementById("controls").style.display="none";</script>';
850 if (!$_REQUEST['form_refresh'] && !$_REQUEST['form_csvexport']) { ?>
851 <div class='text'>
852 <?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
853 </div>
854 <?php
857 </form>
858 </body>
860 </html>
861 <?php
862 } // End not csv export