Apply limit to names shown in document helper (#1679)
[openemr.git] / portal / report / pat_ledger.php
blob6038c5888912638fc2d0c5400323ce5fc6778892
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['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
370 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js?v=<?php echo $v_js_includes; ?>"></script>
371 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
373 <script type="text/javascript">
374 var mypcc = '<?php echo $GLOBALS['phone_country_code']; ?>';
375 var pt_name;
376 var pt_id;
377 function checkSubmit() {
378 var pat = document.forms[0].elements['form_patient'].value;
379 if(!pat || pat == 0) {
380 alert('<?php echo xls('A Patient Must Be Selected to Generate This Report') ?>');
381 return false;
383 document.forms[0].elements['form_refresh'].value = true;
384 document.forms[0].elements['form_csvexport'].value = '';
385 document.forms[0].submit();
387 function setpatient(pid, lname, fname, dob) {
388 document.forms[0].elements['form_patient'].value = lname + ', ' + fname;
389 document.forms[0].elements['form_pid'].value = pid;
390 document.forms[0].elements['form_dob'].value = dob;
392 function sel_patient() {
393 dlgopen('../main/calendar/find_patient_popup.php?pflag=0', '_blank', 500, 400);
395 </script>
397 <style type="text/css">
399 /* specifically include & exclude from printing */
400 @media print {
401 #report_parameters {
402 visibility: hidden;
403 display: none;
405 #report_parameters_daterange {
406 visibility: visible;
407 display: inline;
409 #report_results {
410 margin-top: 30px;
412 #report_header {
413 visibility: visible;
414 display: inline;
416 #title {
417 visibility: hidden;
418 display: none;
421 /* specifically exclude some from the screen */
422 @media screen {
423 #report_parameters_daterange {
424 visibility: hidden;
425 display: none;
427 #report_header {
428 visibility: hidden;
429 display: none;
431 #title {
432 visibility: visible;
433 display: inline;
436 </style>
438 <title><?php echo xlt('Patient Ledger by Date') ?></title>
440 <script>
441 $(document).ready(function() {
442 $('.datepicker').datetimepicker({
443 <?php $datetimepicker_timepicker = false; ?>
444 <?php $datetimepicker_formatInput = false; ?>
445 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
446 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
449 </script>
451 </head>
452 <body class="skin-blue">
453 <?php if ($type_form == '0') { ?>
454 <span class='title' id='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Patient Ledger by Date'); ?></span>
455 <?php } else { ?>
456 <span class='title' id='title'><?php echo xlt('Patient Ledger'); ?></span>
457 <?php } ?>
458 <form method='post' action='./pat_ledger.php?form=<?php echo attr($type_form);?>&patient_id=<?php echo attr($form_pid);?>' id='theform'>
459 <div id="report_parameters">
460 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
461 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
462 <table>
463 <tr>
464 <?php if ($type_form == '1') { ?>
465 <td width='35%'>
466 <?php } else { ?>
467 <td width='70%'>
468 <?php } ?>
469 <div style='float:left'>
470 <table class='text'>
471 <tr>
472 <?php if ($type_form == '0') { ?>
473 <td class='label_custom'>
474 <?php echo xlt('Facility'); ?>:
475 </td>
476 <td>
477 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
478 </td>
479 <td><?php echo xlt('Provider'); ?>:</td>
480 <td><?php
481 $query = "SELECT id, lname, fname FROM users WHERE ".
482 "authorized=1 AND active!=0 ORDER BY lname, fname";
483 $ures = sqlStatement($query);
484 echo " <select name='form_provider'>\n";
485 echo " <option value=''>-- " . xlt('All') . " --\n";
486 while ($urow = sqlFetchArray($ures)) {
487 $provid = $urow['id'];
488 echo " <option value='" . attr($provid) ."'";
489 if ($provid == $_REQUEST['form_provider']) {
490 echo " selected";
493 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
496 echo " </select>\n";
497 ?></td>
498 </tr><tr>
499 <?php } ?>
500 <td colspan="2">
501 <?php echo xlt('From'); ?>:&nbsp;&nbsp;&nbsp;&nbsp;
502 <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'>
503 </td>
504 <td class='label_custom'>
505 <?php echo xlt('To'); ?>:
506 </td>
507 <td>
508 <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'>
509 </td>
510 <?php if ($type_form == '0') { ?>
511 <td><span class='label_custom'><?php echo xlt('Patient'); ?>:&nbsp;&nbsp;</span></td>
512 <td>
513 <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'); ?>' />
514 <?php } else { ?>
515 <input type='hidden' name='form_patient' value='<?php echo attr($form_patient); ?>' />
516 <?php } ?>
517 <input type='hidden' name='form_pid' value='<?php echo attr($form_pid); ?>' />
518 <input type='hidden' name='form_dob' value='<?php echo attr($form_dob); ?>' />
520 </td>
521 </tr>
522 </table>
523 </div>
524 </td>
525 <td align='left' valign='middle' height="100%">
526 <table style='border-left:1px solid; width:100%; height:100%' >
527 <tr>
528 <td>
529 <div style='margin-left:15px'>
530 <a href='#' class='css_button' onclick="checkSubmit();" >
531 <span><?php echo xlt('Submit'); ?></span></a>
533 <?php if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) { ?>
534 <div id="controls"> <!-- print is patched out until I feel like convert to patient side -->
535 <!-- <a href='#' class='css_button' id='printbutton'>
536 <span><?php //echo xlt('Print Ledger'); ?></span></a> -->
537 <?php if ($type_form == '1') { ?>
538 <!-- <a href="../patient_file/summary/demographics.php" <?php // if (!$GLOBALS['concurrent_layout']) echo "target='Main'"; ?> class="css_button" onclick="top.restoreSession()">-->
539 <!-- <span><?php //echo xlt('Back To Patient');?></span></a> -->
540 <?php } ?>
541 </div>
542 <?php } ?>
543 </div>
544 </td>
545 </tr>
546 </table>
547 </td>
548 </tr>
549 </table>
550 </div> <!-- end of parameters -->
552 <?php
553 } // end not export
554 $from_date = $form_from_date . ' 00:00:00';
555 $to_date = $form_to_date . ' 23:59:59';
556 if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) {
557 $rows = array();
558 $sqlBindArray = array();
559 $query = "select b.code_type, b.code, b.code_text, b.pid, b.provider_id, ".
560 "b.billed, b.payer_id, b.units, b.fee, b.bill_date, b.id, ".
561 "ins.name, ".
562 "fe.encounter, fe.date, fe.reason, fe.provider_id ".
563 "FROM form_encounter AS fe ".
564 "LEFT JOIN billing AS b ON b.pid=fe.pid AND b.encounter=fe.encounter ".
565 "LEFT JOIN insurance_companies AS ins ON b.payer_id = ins.id ".
566 "LEFT OUTER JOIN code_types AS c ON c.ct_key = b.code_type ".
567 "WHERE fe.date >= ? AND fe.date <= ? AND fe.pid = ? ";
568 array_push($sqlBindArray, $from_date, $to_date, $form_pid);
569 if ($form_facility) {
570 $query .= "AND fe.facility_id = ? ";
571 array_push($sqlBindArray, $form_facility);
574 if ($form_provider) {
575 $query .= "AND b.provider_id = ? ";
576 array_push($sqlBindArray, $form_provider);
579 $query .= "AND c.ct_proc = '1' ";
580 $query .= "AND activity > 0 ORDER BY fe.date, fe.id ";
581 $res = sqlStatement($query, $sqlBindArray);
583 if ($_REQUEST['form_csvexport']) {
584 // CSV headers:
585 if (true) {
586 echo '"Code/Enc Dt",';
587 echo '"Description",';
588 echo '"Billed/Who",';
589 echo '"Type/Units",';
590 echo '"Chg/Pmt Amount",'."\n";
592 } else {
593 if (!$form_facility) {
594 $form_facility = '3';
597 $facility = sqlQuery("SELECT * FROM facility WHERE id=?", array($form_facility));
598 $patient = sqlQuery("SELECT * from patient_data WHERE pid=?", array($form_patient));
599 $pat_dob = $patient['DOB'];
600 $pat_name = $patient['fname']. ' ' . $patient['lname'];
602 <div id="report_header">
603 <table width="98%" border="0" cellspacing="0" cellpadding="0">
604 <tr>
605 <td class="title" ><?php echo text($facility{'name'}); ?></td>
606 </tr>
607 <tr>
608 <td class="title" ><?php echo text($facility{'street'}); ?></td>
609 </tr>
610 <tr>
611 <td class="title" ><?php echo text($facility{'city'}).", ".text($facility{'state'})." ".text($facility{'postal_code'}); ?></td>
612 </tr>
613 <tr>
614 <td class="title" ><?php echo xlt('Phone').': ' .text($facility{'phone'}); ?></td>
615 </tr>
616 <tr>
617 <td class="title" ><?php echo xlt('Tax Id').': ' .text($facility{'federal_ein'}); ?></td>
618 </tr>
619 <tr><td>&nbsp;</td></tr>
620 <tr>
621 <td class="title" ><?php echo xlt('Patient Ledger'); ?></td>
622 </tr>
623 <tr>
624 <?php
625 $title = xl('All Providers');
626 if ($form_provider) {
627 $title = xl('For Provider') . ': '.User_Id_Look($form_provider);
630 <td class="title" ><?php echo text($title); ?></td>
631 </tr>
632 <tr>
633 <?php
634 $title = xl('For Dates') . ': '.$form_from_date.' - '.$form_to_date;
636 <td class="title" ><?php echo text($title); ?></td>
637 </tr>
638 </table>
639 <br/>
640 <table width="100%" border="0" cellspacing="0" cellpadding="0">
641 <tr>
642 <td class='bold' ><?php echo xlt('Date')?>:
643 <?php echo text(date('Y-m-d')); ?></td>
644 <td class='bold' ><?php echo xlt('Patient')?>:
645 <?php if ($type_form == '1') { ?>
646 <?php echo text($pat_name); ?></td>
647 <?php } else { ?>
648 <?php echo text($form_patient); ?></td>
649 <?php } ?>
650 <td class='bold' ><?php echo xlt('DOB')?>:
651 <?php if ($type_form == '1') { ?>
652 <?php echo text($pat_dob);?></td>
653 <?php } else { ?>
654 <?php echo text($form_dob); ?></td>
655 <?php } ?>
656 <td class='bold' > <?php echo xlt('ID')?>:
657 <?php echo text($form_pid);?></td>
658 </tr>
659 </table>
660 </div>
661 <div id="report_results">
662 <table >
663 <tr>
664 <td class='bold' ><?php echo xlt('Code'); ?></td>
665 <td colspan="2" class='bold' ><?php echo xlt('Description'); ?></td>
666 <td class='bold' ><?php echo xlt('Billed Date'); ?> / <?php echo xlt('Payor'); ?></td>
667 <td class='bold' ><?php echo xlt('Type'); ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
668 <?php echo xlt('Units'); ?></td>
669 <td class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Charge'); ?></td>
670 <td align='right' class='bold' >&nbsp;&nbsp;<?php echo xlt('Payment'); ?></td>
671 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Adjustment'); ?></td>
672 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('Balance'); ?></td>
673 </tr>
674 <tr>
675 <td>&nbsp;&nbsp;&nbsp;</td>
676 <td colspan="2" >&nbsp;&nbsp;&nbsp;</td>
677 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
678 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
679 &nbsp;&nbsp;&nbsp;</td>
680 <td class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Appl'); ?></td>
681 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Tot'); ?></td>
682 <td>&nbsp;</td>
683 <td>&nbsp;</td>
684 </tr>
685 <?php
688 $orow = 0;
689 $prev_encounter_id = -1;
690 $hdr_printed = false;
691 $prev_row = array();
692 while ($erow = sqlFetchArray($res)) {
693 $print = '';
694 $csv = '';
695 if ($erow['encounter'] != $prev_encounter_id) {
696 if ($prev_encounter_id != -1) {
697 $credits = GetAllCredits($prev_encounter_id, $form_pid);
698 if (count($credits) > 0) {
699 if (!$hdr_printed) {
700 PrintEncHeader(
701 $prev_row{'date'},
702 $prev_row{'reason'},
703 $prev_row{'provider_id'}
707 PrintCreditDetail($credits, $form_pid);
710 if ($hdr_printed) {
711 PrintEncFooter();
714 $hdr_printed = false;
717 $enc_units = $enc_chg = $enc_pmt = $enc_adj = $enc_bal = 0;
720 if ($erow{'id'}) {
721 // Now print an encounter heading line -
722 if (!$hdr_printed) {
723 PrintEncHeader(
724 $erow{'date'},
725 $erow{'reason'},
726 $erow{'provider_id'}
728 $hdr_printed = true;
731 $code_desc = $erow['code_text'];
732 if (strlen($code_desc) > 50) {
733 $code_desc = substr($code_desc, 0, 50).'...';
736 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
737 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
738 $print .= "<td class='detail'>".text($erow['code'])."</td>";
739 $print .= "<td class='detail' colspan='2'>".text($code_desc)."</td>";
740 $who = ($erow['name'] == '') ? xl('Self') : $erow['name'];
741 $bill = substr($erow['bill_date'], 0, 10);
742 if ($bill == '') {
743 $bill = 'unbilled';
746 $print .= "<td class='detail'>".text($bill)."&nbsp;/&nbsp;".text($who)."</td>";
747 $print .= "<td class='detail' style='text-align: right;'>". text($erow['units'])."</td>";
748 $print .= "<td class='detail' style='text-align: right;'>". text(oeFormatMoney($erow['fee']))."</td>";
749 $print .= "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
750 $print .= "</tr>\n";
752 $total_units += $erow['units'];
753 $total_chg += $erow['fee'];
754 $total_bal += $erow['fee'];
755 $enc_units += $erow['units'];
756 $enc_chg += $erow['fee'];
757 $enc_bal += $erow['fee'];
758 $orow++;
760 if ($_REQUEST['form_csvexport']) {
761 echo $csv;
762 } else {
763 echo $print;
767 $prev_encounter_id = $erow{'encounter'};
768 $prev_row = $erow;
771 if ($prev_encounter_id != -1) {
772 $credits = GetAllCredits($prev_encounter_id, $form_pid);
773 if (count($credits) > 0) {
774 if (!$hdr_printed) {
775 PrintEncHeader(
776 $prev_row{'date'},
777 $prev_row{'reason'},
778 $prev_row{'provider_id'}
782 PrintCreditDetail($credits, $form_pid);
785 if ($hdr_printed) {
786 PrintEncFooter();
790 // This is the end of the encounter/charge loop -
791 $uac = GetAllUnapplied($form_pid, $from_date, $to_date);
792 if (count($uac) > 0) {
793 if ($orow) {
794 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
795 echo "<tr bgcolor='#FFFFFF'><td colspan='9'>&nbsp;</td></tr>\n";
798 PrintCreditDetail($uac, $form_pid, true);
801 if (!$_REQUEST['form_csvexport'] && $orow) {
802 echo "<tr bgcolor='#DDFFFF'>\n";
803 echo " <td colspan='2'>&nbsp;</td>";
804 echo " <td class='bold' colspan='2'>" . xlt("Grand Total") ."</td>\n";
805 echo " <td class='bold' style='text-align: right;'>". text($total_units) ."</td>\n";
806 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_chg)) ."</td>\n";
807 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_pmt)) ."</td>\n";
808 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_adj)) ."</td>\n";
809 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_bal)) . "</td>\n";
810 echo " </tr>\n";
812 </table>
813 <tr><td>&nbsp;</td></tr><br><br>
814 <?php if ($GLOBALS['print_next_appointment_on_ledger'] == 1) {
815 $next_day = mktime(0, 0, 0, date('m'), date('d')+1, date('Y'));
816 # add one day to date so it will not get todays appointment
817 $current_date2 = date('Y-m-d', $next_day);
818 $events = fetchNextXAppts($current_date2, $form_pid);
819 $next_appoint_date = oeFormatShortDate($events[0]['pc_eventDate']);
820 $next_appoint_time = substr($events[0]['pc_startTime'], 0, 5);
821 if (strlen(umname) != 0) {
822 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['umname'] . ' ' . $events[0]['ulname'];
823 } else {
824 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['ulname'];
827 if (strlen($next_appoint_time) != 0) {
829 <tr>
830 <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>
831 </tr>
833 <?php
835 } // end ($GLOBALS['print_next_appointment_on_ledger'] == 1)
836 } // end (!$_REQUEST['form_csvexport'] && $orow)
837 echo "</div>\n";
840 if (! $_REQUEST['form_csvexport']) {
841 if ($_REQUEST['form_refresh'] && $orow <= 0) {
842 echo "<span style='font-size:10pt;'>";
843 echo xlt('No matches found. Try search again.');
844 echo "</span>";
845 echo '<script>document.getElementById("report_results").style.display="none";</script>';
846 echo '<script>document.getElementById("controls").style.display="none";</script>';
849 if (!$_REQUEST['form_refresh'] && !$_REQUEST['form_csvexport']) { ?>
850 <div class='text'>
851 <?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
852 </div>
853 <?php
856 </form>
857 </body>
859 </html>
860 <?php
861 } // End not csv export