More misc billing (#890)
[openemr.git] / portal / report / pat_ledger.php
blob7c46d4fb726d9197eff702eadbaab534ffe0b934
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='') {
51 $all = array();
52 if(!$pat) return($all);
53 $sql = "SELECT ar_session.*, ins.name, " .
54 "pat.lname, pat.fname, pat.mname, " .
55 "(SELECT SUM(ar_activity.pay_amount) FROM ar_activity WHERE " .
56 "ar_activity.session_id = ar_session.session_id) AS applied " .
57 "FROM ar_session " .
58 "LEFT JOIN insurance_companies AS ins on ar_session.payer_id = ins.id " .
59 "LEFT JOIN patient_data AS pat on ar_session.patient_id = pat.pid " .
60 "WHERE " .
61 "ar_session.created_time >= ? AND ar_session.created_time <= ? " .
62 "AND ar_session.patient_id=?";
63 $result = sqlStatement($sql, array($from_dt, $to_dt, $pat));
64 $iter = 0;
65 while($row = sqlFetchArray($result)) {
66 $all[$iter] = $row;
67 $iter++;
69 return($all);
72 function User_Id_Look($thisField) {
73 if(!$thisField) return '';
74 $ret = '';
75 $rlist= sqlStatement("SELECT lname, fname, mname FROM users WHERE id=?",array($thisField));
76 $rrow= sqlFetchArray($rlist);
77 if($rrow) {
78 $ret = $rrow{'lname'}.', '.$rrow{'fname'}.' '.$rrow{'mname'};
80 return $ret;
83 function List_Look($thisData, $thisList) {
84 if($thisList == 'occurrence') {
85 if(!$thisData || $thisData == '') return xl('Unknown or N/A');
87 if($thisData == '') return '';
88 $fres=sqlStatement("SELECT title FROM list_options WHERE list_id=? ".
89 "AND option_id=?", array($thisList, $thisData));
90 if($fres) {
91 $rret=sqlFetchArray($fres);
92 $dispValue= xl_list_label($rret{'title'});
93 if($thisList == 'occurrence' && $dispValue == '') {
94 $dispValue = xl('Unknown or N/A');
96 } else {
97 $dispValue= xl('Not Found');
99 return $dispValue;
102 function GetAllCredits($enc = '', $pat='') {
103 $all = array();
104 if(!$enc || !$pat) return($all);
105 $sql = "SELECT activity.*, session.*, ins.name FROM ar_activity AS ".
106 "activity LEFT JOIN ar_session AS session USING (session_id) ".
107 "LEFT JOIN insurance_companies AS ins ON session.payer_id = ".
108 "ins.id WHERE encounter=? AND pid=? ".
109 "ORDER BY sequence_no";
110 $result = sqlStatement($sql, array($enc, $pat));
111 $iter = 0;
112 while($row = sqlFetchArray($result)) {
113 $all[$iter] = $row;
114 $iter++;
116 return($all);
118 function PrintEncHeader($dt, $rsn, $dr) {
119 global $bgcolor, $orow;
120 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
121 echo "<tr bgcolor='#FFFFFF'>";
122 if(strlen($rsn) > 50) $rsn = substr($rsn,0,50).'...';
123 echo "<td colspan='4'><span class='bold'>".xlt('Encounter Dt / Rsn'). ": </span><span class='detail'>".text(substr($dt,0,10))." / ".text($rsn)."</span></td>";
124 echo "<td colspan='5'><span class='bold'>" . xlt('Provider'). ": </span><span class='detail'>".text(User_Id_Look($dr))."</span></td>";
125 echo "</tr>\n";
126 $orow++;
128 function PrintEncFooter() {
129 global $enc_units, $enc_chg, $enc_pmt, $enc_adj, $enc_bal;
130 echo "<tr bgcolor='#DDFFFF'>";
131 echo "<td colspan='3'>&nbsp;</td>";
132 echo "<td class='detail'>". xlt('Encounter Balance').":</td>";
133 echo "<td class='detail' style='text-align: right;'>".text($enc_units)."</td>";
134 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_chg))."</td>";
135 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_pmt))."</td>";
136 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_adj))."</td>";
137 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_bal))."</td>";
138 echo "</tr>\n";
140 function PrintCreditDetail($detail, $pat, $unassigned=false) {
141 global $enc_pmt, $total_pmt, $enc_adj, $total_adj, $enc_bal, $total_bal;
142 global $bgcolor, $orow, $enc_units, $enc_chg;
143 foreach($detail as $pmt) {
144 if($unassigned) {
145 if(($pmt['pay_total'] - $pmt['applied']) == 0) continue;
147 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
148 $print = "<tr bgcolor='" . attr($bgcolor) . "'>";
149 $print .= "<td class='detail'>&nbsp;</td>";
150 $method = List_Look($pmt['payment_method'],'payment_method');
151 $desc = $pmt['description'];
152 $ref = $pmt['reference'];
153 if($unassigned) {
154 $memo = List_Look($pmt['adjustment_code'],'payment_adjustment_code');
155 } else {
156 $memo = $pmt['memo'];
158 $description = $method;
159 if($ref) {
160 if($description) { $description .= ' - '; }
161 $description .= $ref;
163 if($desc) {
164 if($description) { $description .= ': '; }
165 $description .= $desc;
167 if($memo) {
168 if($description) { $description .= ' '; }
169 $description .= '['.$memo.']';
171 $print .= "<td class='detail' colspan='2'>".
172 text($description)."&nbsp;</td>";
173 $payer = ($pmt['name'] == '') ? xl('Patient') : $pmt['name'];
174 if($unassigned) {
175 $pmt_date = substr($pmt['post_to_date'],0,10);
176 } else {
177 $pmt_date = substr($pmt['post_time'],0,10);
179 $print .= "<td class='detail'>".
180 text($pmt_date)."&nbsp;/&nbsp;".text($payer)."</td>";
181 $type = List_Look($pmt['payment_type'],'payment_type');
182 $print .= "<td class='detail'>".text($type)."&nbsp;</td>";
183 if($unassigned) {
184 $pmt_amt = $pmt['pay_total'] - $pmt['applied'];
185 $uac_bal = oeFormatMoney($pmt_amt * -1);
186 $uac_appl = oeFormatMoney($pmt['applied']);
187 $uac_total = oeFormatMoney($pmt['pay_total']);
188 $pmt_amt = $pmt['pay_total'];
189 $total_pmt = $total_pmt - $uac_bal;
190 } else {
191 $uac_total = '';
192 $uac_bal = '';
193 $uac_appl = '';
194 $pmt_amt = $pmt['pay_amount'];
195 $adj_amt = $pmt['adj_amount'];
196 $enc_pmt = $enc_pmt + $pmt['pay_amount'];
197 $total_pmt = $total_pmt + $pmt['pay_amount'];
198 $enc_adj = $enc_adj + $pmt['adj_amount'];
199 $total_adj = $total_adj + $pmt['adj_amount'];
201 $print_pmt = '';
202 if($pmt_amt != 0) $print_pmt = oeFormatMoney($pmt_amt);
203 $print_adj = '';
204 if($adj_amt != 0) $print_adj = oeFormatMoney($adj_amt);
205 $print .= "<td class='detail' style='text-align: right;'>".text($uac_appl)."&nbsp;</td>";
206 $print .= "<td class='detail' style='text-align: right;'>".text($print_pmt)."&nbsp;</td>";
207 $print .= "<td class='detail' style='text-align: right;'>".text($print_adj)."&nbsp;</td>";
208 $print .= "<td class='detail' style='text-align: right;'>".text($uac_bal)."&nbsp;</td>";
209 $print .= "</tr>\n";
210 echo $print;
211 if($pmt['follow_up_note'] != '') {
212 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
213 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
214 $print .= "<td class='detail' colspan='2'>&nbsp;</td>";
215 $print .= "<td colspan='7'>". xlt('Follow Up Note') .": ";
216 $print .= text($pmt['follow_up_note']);
217 $print .= "</td></tr>\n";
218 echo $print;
220 if($unassigned) {
221 $total_bal = $total_bal + $uac_bal;
222 } else {
223 $enc_bal = $enc_bal - $pmt_amt - $adj_amt;
224 $total_bal = $total_bal - $pmt_amt - $adj_amt;
226 $orow++;
228 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
230 if(!isset($_REQUEST['form_from_date'])) { $_REQUEST['form_from_date'] = ''; }
231 if(!isset($_REQUEST['form_to_date'])) { $_REQUEST['form_to_date'] = ''; }
232 if(!isset($_REQUEST['form_facility'])) { $_REQUEST['form_facility'] = ''; }
233 if(!isset($_REQUEST['form_provider'])) { $_REQUEST['form_provider'] = ''; }
234 if($type_form=='0') {
235 if(!isset($_REQUEST['form_patient'])) { $_REQUEST['form_patient'] = ''; }
236 if(!isset($_REQUEST['form_pid'])) { $_REQUEST['form_pid'] = ''; }
238 else
240 if(!isset($_REQUEST['form_patient'])) { $_REQUEST['form_patient'] = $pat_pid; }
241 if(!isset($_REQUEST['form_pid'])) { $_REQUEST['form_pid'] = $pat_pid; }
243 if(!isset($_REQUEST['form_csvexport'])) { $_REQUEST['form_csvexport'] = ''; }
244 if(!isset($_REQUEST['form_refresh'])) { $_REQUEST['form_refresh'] = ''; }
245 if(!isset($_REQUEST['$form_dob'])) { $_REQUEST['$form_dob'] = ''; }
247 if (substr($GLOBALS['ledger_begin_date'],0,1) == 'Y') {
248 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
249 $last_year = mktime(0,0,0,date('m'),date('d'),date('Y')-$ledger_time);
251 elseif (substr($GLOBALS['ledger_begin_date'],0,1) == 'M') {
252 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
253 $last_year = mktime(0,0,0,date('m')-$ledger_time ,date('d'),date('Y'));
255 elseif (substr($GLOBALS['ledger_begin_date'],0,1) == 'D') {
256 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
257 $last_year = mktime(0,0,0,date('m') ,date('d')-$ledger_time,date('Y'));
260 $form_from_date = date('Y-m-d', $last_year);
261 if($_REQUEST['form_from_date']) {
262 $form_from_date = fixDate($_REQUEST['form_from_date'], $last_year);
264 $form_to_date = fixDate($_REQUEST['form_to_date'] , date('Y-m-d'));
265 $form_facility = $_REQUEST['form_facility'];
266 $form_provider = $_REQUEST['form_provider'];
267 $form_patient = $_REQUEST['form_patient'];
268 $form_pid = $_REQUEST['form_pid'];
269 $form_dob = $_REQUEST['form_dob'];
271 if ($_REQUEST['form_csvexport']) {
272 header("Pragma: public");
273 header("Expires: 0");
274 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
275 header("Content-Type: application/force-download");
276 header("Content-Disposition: attachment; filename=svc_financial_report_".attr($form_from_date)."--".attr($form_to_date).".csv");
277 header("Content-Description: File Transfer");
278 } else {
280 <html>
281 <head>
282 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
283 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
285 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
286 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
287 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js?v=<?php echo $v_js_includes; ?>"></script>
288 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
290 <script type="text/javascript">
291 var mypcc = '<?php echo $GLOBALS['phone_country_code']; ?>';
292 var pt_name;
293 var pt_id;
294 function checkSubmit() {
295 var pat = document.forms[0].elements['form_patient'].value;
296 if(!pat || pat == 0) {
297 alert('<?php echo xls('A Patient Must Be Selected to Generate This Report') ?>');
298 return false;
300 document.forms[0].elements['form_refresh'].value = true;
301 document.forms[0].elements['form_csvexport'].value = '';
302 document.forms[0].submit();
304 function setpatient(pid, lname, fname, dob) {
305 document.forms[0].elements['form_patient'].value = lname + ', ' + fname;
306 document.forms[0].elements['form_pid'].value = pid;
307 document.forms[0].elements['form_dob'].value = dob;
309 function sel_patient() {
310 dlgopen('../main/calendar/find_patient_popup.php?pflag=0', '_blank', 500, 400);
312 </script>
314 <style type="text/css">
316 /* specifically include & exclude from printing */
317 @media print {
318 #report_parameters {
319 visibility: hidden;
320 display: none;
322 #report_parameters_daterange {
323 visibility: visible;
324 display: inline;
326 #report_results {
327 margin-top: 30px;
329 #report_header {
330 visibility: visible;
331 display: inline;
333 #title {
334 visibility: hidden;
335 display: none;
338 /* specifically exclude some from the screen */
339 @media screen {
340 #report_parameters_daterange {
341 visibility: hidden;
342 display: none;
344 #report_header {
345 visibility: hidden;
346 display: none;
348 #title {
349 visibility: visible;
350 display: inline;
353 </style>
355 <title><?php echo xlt('Patient Ledger by Date') ?></title>
357 <script>
358 $(document).ready(function() {
359 $('.datepicker').datetimepicker({
360 <?php $datetimepicker_timepicker = false; ?>
361 <?php $datetimepicker_formatInput = false; ?>
362 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
363 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
366 </script>
368 </head>
369 <body class="body_top">
370 <?php if($type_form == '0') { ?>
371 <span class='title' id='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Patient Ledger by Date'); ?></span>
372 <?php }else{ ?>
373 <span class='title' id='title'><?php echo xlt('Patient Ledger'); ?></span>
374 <?php } ?>
375 <form method='post' action='./pat_ledger.php?form=<?php echo attr($type_form);?>&patient_id=<?php echo attr($form_pid);?>' id='theform'>
376 <div id="report_parameters">
377 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
378 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
379 <table>
380 <tr>
381 <?php if($type_form == '1') { ?>
382 <td width='35%'>
383 <?php }else{ ?>
384 <td width='70%'>
385 <?php } ?>
386 <div style='float:left'>
387 <table class='text'>
388 <tr>
389 <?php if($type_form == '0') { ?>
390 <td class='label_custom'>
391 <?php echo xlt('Facility'); ?>:
392 </td>
393 <td>
394 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
395 </td>
396 <td><?php echo xlt('Provider'); ?>:</td>
397 <td><?php
398 $query = "SELECT id, lname, fname FROM users WHERE ".
399 "authorized=1 AND active!=0 ORDER BY lname, fname";
400 $ures = sqlStatement($query);
401 echo " <select name='form_provider'>\n";
402 echo " <option value=''>-- " . xlt('All') . " --\n";
403 while ($urow = sqlFetchArray($ures)) {
404 $provid = $urow['id'];
405 echo " <option value='" . attr($provid) ."'";
406 if ($provid == $_REQUEST['form_provider']) echo " selected";
407 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
409 echo " </select>\n";
410 ?></td>
411 </tr><tr>
412 <?php } ?>
413 <td colspan="2">
414 <?php echo xlt('From'); ?>:&nbsp;&nbsp;&nbsp;&nbsp;
415 <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'>
416 </td>
417 <td class='label_custom'>
418 <?php echo xlt('To'); ?>:
419 </td>
420 <td>
421 <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'>
422 </td>
423 <?php if($type_form == '0') { ?>
424 <td><span class='label_custom'><?php echo xlt('Patient'); ?>:&nbsp;&nbsp;</span></td>
425 <td>
426 <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'); ?>' />
427 <?php }else{ ?>
428 <input type='hidden' name='form_patient' value='<?php echo attr($form_patient); ?>' />
429 <?php } ?>
430 <input type='hidden' name='form_pid' value='<?php echo attr($form_pid); ?>' />
431 <input type='hidden' name='form_dob' value='<?php echo attr($form_dob); ?>' />
433 </td>
434 </tr>
435 </table>
436 </div>
437 </td>
438 <td align='left' valign='middle' height="100%">
439 <table style='border-left:1px solid; width:100%; height:100%' >
440 <tr>
441 <td>
442 <div style='margin-left:15px'>
443 <a href='#' class='css_button' onclick="checkSubmit();" >
444 <span><?php echo xlt('Submit'); ?></span></a>
446 <?php if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) { ?>
447 <div id="controls"> <!-- print is patched out until I feel like convert to patient side -->
448 <!-- <a href='#' class='css_button' id='printbutton'>
449 <span><?php //echo xlt('Print Ledger'); ?></span></a> -->
450 <?php if($type_form == '1') { ?>
451 <!-- <a href="../patient_file/summary/demographics.php" <?php // if (!$GLOBALS['concurrent_layout']) echo "target='Main'"; ?> class="css_button" onclick="top.restoreSession()">-->
452 <!-- <span><?php //echo xlt('Back To Patient');?></span></a> -->
453 <?php } ?>
454 </div>
455 <?php } ?>
456 </div>
457 </td>
458 </tr>
459 </table>
460 </td>
461 </tr>
462 </table>
463 </div> <!-- end of parameters -->
465 <?php
466 } // end not export
467 $from_date = $form_from_date . ' 00:00:00';
468 $to_date = $form_to_date . ' 23:59:59';
469 if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) {
470 $rows = array();
471 $sqlBindArray = array();
472 $query = "select b.code_type, b.code, b.code_text, b.pid, b.provider_id, ".
473 "b.billed, b.payer_id, b.units, b.fee, b.bill_date, b.id, ".
474 "ins.name, ".
475 "fe.encounter, fe.date, fe.reason, fe.provider_id ".
476 "FROM form_encounter AS fe ".
477 "LEFT JOIN billing AS b ON b.pid=fe.pid AND b.encounter=fe.encounter ".
478 "LEFT JOIN insurance_companies AS ins ON b.payer_id = ins.id ".
479 "LEFT OUTER JOIN code_types AS c ON c.ct_key = b.code_type ".
480 "WHERE fe.date >= ? AND fe.date <= ? AND fe.pid = ? ";
481 array_push($sqlBindArray,$from_date,$to_date,$form_pid);
482 if ($form_facility) {
483 $query .= "AND fe.facility_id = ? ";
484 array_push($sqlBindArray,$form_facility);
486 if ($form_provider) {
487 $query .= "AND b.provider_id = ? ";
488 array_push($sqlBindArray,$form_provider);
490 $query .= "AND c.ct_proc = '1' ";
491 $query .= "AND activity > 0 ORDER BY fe.date, fe.id ";
492 $res = sqlStatement($query,$sqlBindArray);
494 if ($_REQUEST['form_csvexport']) {
495 // CSV headers:
496 if (true) {
497 echo '"Code/Enc Dt",';
498 echo '"Description",';
499 echo '"Billed/Who",';
500 echo '"Type/Units",';
501 echo '"Chg/Pmt Amount",'."\n";
503 } else {
504 if(!$form_facility) $form_facility = '3';
505 $facility = sqlQuery("SELECT * FROM facility WHERE id=?", array($form_facility));
506 $patient = sqlQuery("SELECT * from patient_data WHERE pid=?", array($form_patient));
507 $pat_dob = $patient['DOB'];
508 $pat_name = $patient['fname']. ' ' . $patient['lname'];
510 <div id="report_header">
511 <table width="98%" border="0" cellspacing="0" cellpadding="0">
512 <tr>
513 <td class="title" ><?php echo text($facility{'name'}); ?></td>
514 </tr>
515 <tr>
516 <td class="title" ><?php echo text($facility{'street'}); ?></td>
517 </tr>
518 <tr>
519 <td class="title" ><?php echo text($facility{'city'}).", ".text($facility{'state'})." ".text($facility{'postal_code'}); ?></td>
520 </tr>
521 <tr>
522 <td class="title" ><?php echo xlt('Phone').': ' .text($facility{'phone'}); ?></td>
523 </tr>
524 <tr>
525 <td class="title" ><?php echo xlt('Tax Id').': ' .text($facility{'federal_ein'}); ?></td>
526 </tr>
527 <tr><td>&nbsp;</td></tr>
528 <tr>
529 <td class="title" ><?php echo xlt('Patient Ledger'); ?></td>
530 </tr>
531 <tr>
532 <?php
533 $title = xl('All Providers');
534 if($form_provider) { $title = xl('For Provider') . ': '.User_Id_Look($form_provider); }
536 <td class="title" ><?php echo text($title); ?></td>
537 </tr>
538 <tr>
539 <?php
540 $title = xl('For Dates') . ': '.$form_from_date.' - '.$form_to_date;
542 <td class="title" ><?php echo text($title); ?></td>
543 </tr>
544 </table>
545 <br/>
546 <table width="100%" border="0" cellspacing="0" cellpadding="0">
547 <tr>
548 <td class='bold' ><?php echo xlt('Date')?>:
549 <?php echo text(date('Y-m-d')); ?></td>
550 <td class='bold' ><?php echo xlt('Patient')?>:
551 <?php if($type_form == '1') { ?>
552 <?php echo text($pat_name); ?></td>
553 <?php }else{ ?>
554 <?php echo text($form_patient); ?></td>
555 <?php } ?>
556 <td class='bold' ><?php echo xlt('DOB')?>:
557 <?php if($type_form == '1') { ?>
558 <?php echo text($pat_dob);?></td>
559 <?php }else{ ?>
560 <?php echo text($form_dob); ?></td>
561 <?php } ?>
562 <td class='bold' > <?php echo xlt('ID')?>:
563 <?php echo text($form_pid);?></td>
564 </tr>
565 </table>
566 </div>
567 <div id="report_results">
568 <table >
569 <tr>
570 <td class='bold' ><?php echo xlt('Code'); ?></td>
571 <td colspan="2" class='bold' ><?php echo xlt('Description'); ?></td>
572 <td class='bold' ><?php echo xlt('Billed Date'); ?> / <?php echo xlt('Payor'); ?></td>
573 <td class='bold' ><?php echo xlt('Type'); ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
574 <?php echo xlt('Units'); ?></td>
575 <td class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Charge'); ?></td>
576 <td align='right' class='bold' >&nbsp;&nbsp;<?php echo xlt('Payment'); ?></td>
577 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Adjustment'); ?></td>
578 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('Balance'); ?></td>
579 </tr>
580 <tr>
581 <td>&nbsp;&nbsp;&nbsp;</td>
582 <td colspan="2" >&nbsp;&nbsp;&nbsp;</td>
583 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
584 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
585 &nbsp;&nbsp;&nbsp;</td>
586 <td class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Appl'); ?></td>
587 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Tot'); ?></td>
588 <td>&nbsp;</td>
589 <td>&nbsp;</td>
590 </tr>
591 <?php
593 $orow = 0;
594 $prev_encounter_id = -1;
595 $hdr_printed = false;
596 $prev_row = array();
597 while ($erow = sqlFetchArray($res)) {
598 $print = '';
599 $csv = '';
600 if($erow['encounter'] != $prev_encounter_id) {
601 if($prev_encounter_id != -1) {
602 $credits = GetAllCredits($prev_encounter_id, $form_pid);
603 if(count($credits) > 0) {
604 if(!$hdr_printed) {
605 PrintEncHeader($prev_row{'date'},
606 $prev_row{'reason'}, $prev_row{'provider_id'});
608 PrintCreditDetail($credits, $form_pid);
610 if($hdr_printed) PrintEncFooter();
611 $hdr_printed = false;
613 $enc_units = $enc_chg = $enc_pmt = $enc_adj = $enc_bal = 0;
615 if($erow{'id'}) {
616 // Now print an encounter heading line -
617 if(!$hdr_printed) {
618 PrintEncHeader($erow{'date'},
619 $erow{'reason'}, $erow{'provider_id'});
620 $hdr_printed = true;
623 $code_desc = $erow['code_text'];
624 if(strlen($code_desc) > 50) $code_desc = substr($code_desc,0,50).'...';
625 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
626 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
627 $print .= "<td class='detail'>".text($erow['code'])."</td>";
628 $print .= "<td class='detail' colspan='2'>".text($code_desc)."</td>";
629 $who = ($erow['name'] == '') ? xl('Self') : $erow['name'];
630 $bill = substr($erow['bill_date'],0,10);
631 if($bill == '') { $bill = 'unbilled'; }
632 $print .= "<td class='detail'>".text($bill)."&nbsp;/&nbsp;".text($who)."</td>";
633 $print .= "<td class='detail' style='text-align: right;'>". text($erow['units'])."</td>";
634 $print .= "<td class='detail' style='text-align: right;'>". text(oeFormatMoney($erow['fee']))."</td>";
635 $print .= "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
636 $print .= "</tr>\n";
638 $total_units += $erow['units'];
639 $total_chg += $erow['fee'];
640 $total_bal += $erow['fee'];
641 $enc_units += $erow['units'];
642 $enc_chg += $erow['fee'];
643 $enc_bal += $erow['fee'];
644 $orow++;
646 if ($_REQUEST['form_csvexport']) {
647 echo $csv;
648 } else {
649 echo $print;
652 $prev_encounter_id = $erow{'encounter'};
653 $prev_row = $erow;
655 if($prev_encounter_id != -1) {
656 $credits = GetAllCredits($prev_encounter_id, $form_pid);
657 if(count($credits) > 0) {
658 if(!$hdr_printed) {
659 PrintEncHeader($prev_row{'date'},
660 $prev_row{'reason'}, $prev_row{'provider_id'});
662 PrintCreditDetail($credits, $form_pid);
664 if($hdr_printed) PrintEncFooter();
666 // This is the end of the encounter/charge loop -
667 $uac = GetAllUnapplied($form_pid,$from_date,$to_date);
668 if(count($uac) > 0) {
669 if($orow) {
670 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
671 echo "<tr bgcolor='#FFFFFF'><td colspan='9'>&nbsp;</td></tr>\n";
673 PrintCreditDetail($uac, $form_pid, true);
675 if (!$_REQUEST['form_csvexport'] && $orow) {
676 echo "<tr bgcolor='#DDFFFF'>\n";
677 echo " <td colspan='2'>&nbsp;</td>";
678 echo " <td class='bold' colspan='2'>" . xlt("Grand Total") ."</td>\n";
679 echo " <td class='bold' style='text-align: right;'>". text($total_units) ."</td>\n";
680 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_chg)) ."</td>\n";
681 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_pmt)) ."</td>\n";
682 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_adj)) ."</td>\n";
683 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_bal)) . "</td>\n";
684 echo " </tr>\n";
686 </table>
687 <tr><td>&nbsp;</td></tr><br><br>
688 <?php if($GLOBALS['print_next_appointment_on_ledger'] == 1) {
689 $next_day = mktime(0,0,0,date('m'),date('d')+1,date('Y'));
690 # add one day to date so it will not get todays appointment
691 $current_date2 = date('Y-m-d', $next_day);
692 $events = fetchNextXAppts($current_date2,$form_pid);
693 $next_appoint_date = oeFormatShortDate($events[0]['pc_eventDate']);
694 $next_appoint_time = substr($events[0]['pc_startTime'],0,5);
695 if(strlen(umname) != 0 ) {
696 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['umname'] . ' ' . $events[0]['ulname'];
698 else
700 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['ulname'];
702 if(strlen($next_appoint_time) != 0) {
704 <tr>
705 <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>
706 </tr>
708 <?php
710 } // end ($GLOBALS['print_next_appointment_on_ledger'] == 1)
711 } // end (!$_REQUEST['form_csvexport'] && $orow)
712 echo "</div>\n";
714 if (! $_REQUEST['form_csvexport']) {
715 if ( $_REQUEST['form_refresh'] && $orow <= 0) {
716 echo "<span style='font-size:10pt;'>";
717 echo xlt('No matches found. Try search again.');
718 echo "</span>";
719 echo '<script>document.getElementById("report_results").style.display="none";</script>';
720 echo '<script>document.getElementById("controls").style.display="none";</script>';
723 if (!$_REQUEST['form_refresh'] && !$_REQUEST['form_csvexport']) { ?>
724 <div class='text'>
725 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
726 </div>
727 <?php } ?>
728 </form>
729 </body>
731 </html>
732 <?php
733 } // End not csv export