Fixed bug: https://sourceforge.net/p/openemr/bugs/416/
[openemr.git] / interface / reports / pat_ledger.php
blob68bff64ace52d64b604e5e4491281aaf49b5bf75
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
25 $sanitize_all_escapes=true;
26 $fake_register_globals=false;
27 require_once('../globals.php');
28 require_once($GLOBALS['srcdir'].'/patient.inc');
29 require_once($GLOBALS['srcdir'].'/acl.inc');
30 require_once($GLOBALS['srcdir'].'/formatting.inc.php');
31 require_once($GLOBALS['srcdir'].'/options.inc.php');
32 require_once($GLOBALS['srcdir'].'/formdata.inc.php');
33 require_once($GLOBALS['srcdir'].'/appointments.inc.php');
35 $enc_units = $total_units = 0;
36 $enc_chg = $total_chg = 0;
37 $enc_pmt = $total_pmt = 0;
38 $enc_adj = $total_adj = 0;
39 $enc_bal = $total_bal = 0;
40 $bgcolor = "#FFFFDD";
41 $orow = 0;
43 $pat_pid = $_GET['patient_id'];
44 $type_form = $_GET['form'];
46 if (! acl_check('acct', 'rep')) die(xlt("Unauthorized access."));
48 function GetAllUnapplied($pat='',$from_dt='',$to_dt='') {
49 $all = array();
50 if(!$pat) return($all);
51 $sql = "SELECT ar_session.*, ins.name, " .
52 "pat.lname, pat.fname, pat.mname, " .
53 "(SELECT SUM(ar_activity.pay_amount) FROM ar_activity WHERE " .
54 "ar_activity.session_id = ar_session.session_id) AS applied " .
55 "FROM ar_session " .
56 "LEFT JOIN insurance_companies AS ins on ar_session.payer_id = ins.id " .
57 "LEFT JOIN patient_data AS pat on ar_session.patient_id = pat.pid " .
58 "WHERE " .
59 "ar_session.created_time >= ? AND ar_session.created_time <= ? " .
60 "AND ar_session.patient_id=?";
61 $result = sqlStatement($sql, array($from_dt, $to_dt, $pat));
62 $iter = 0;
63 while($row = sqlFetchArray($result)) {
64 $all[$iter] = $row;
65 $iter++;
67 return($all);
70 function User_Id_Look($thisField) {
71 if(!$thisField) return '';
72 $ret = '';
73 $rlist= sqlStatement("SELECT lname, fname, mname FROM users WHERE id=?",array($thisField));
74 $rrow= sqlFetchArray($rlist);
75 if($rrow) {
76 $ret = $rrow{'lname'}.', '.$rrow{'fname'}.' '.$rrow{'mname'};
78 return $ret;
81 function List_Look($thisData, $thisList) {
82 if($thisList == 'occurrence') {
83 if(!$thisData || $thisData == '') return xl('Unknown or N/A');
85 if($thisData == '') return '';
86 $fres=sqlStatement("SELECT title FROM list_options WHERE list_id=? ".
87 "AND option_id=?", array($thisList, $thisData));
88 if($fres) {
89 $rret=sqlFetchArray($fres);
90 $dispValue= xl_list_label($rret{'title'});
91 if($thisList == 'occurrence' && $dispValue == '') {
92 $dispValue = xl('Unknown or N/A');
94 } else {
95 $dispValue= xl('Not Found');
97 return $dispValue;
100 function GetAllCredits($enc = '', $pat='') {
101 $all = array();
102 if(!$enc || !$pat) return($all);
103 $sql = "SELECT activity.*, session.*, ins.name FROM ar_activity AS ".
104 "activity LEFT JOIN ar_session AS session USING (session_id) ".
105 "LEFT JOIN insurance_companies AS ins ON session.payer_id = ".
106 "ins.id WHERE encounter=? AND pid=? ".
107 "ORDER BY sequence_no";
108 $result = sqlStatement($sql, array($enc, $pat));
109 $iter = 0;
110 while($row = sqlFetchArray($result)) {
111 $all[$iter] = $row;
112 $iter++;
114 return($all);
116 function PrintEncHeader($dt, $rsn, $dr) {
117 global $bgcolor, $orow;
118 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
119 echo "<tr bgcolor='#FFFFFF'>";
120 if(strlen($rsn) > 50) $rsn = substr($rsn,0,50).'...';
121 echo "<td colspan='4'><span class='bold'>".xlt('Encounter Dt / Rsn'). ": </span><span class='detail'>".text(substr($dt,0,10))." / ".text($rsn)."</span></td>";
122 echo "<td colspan='5'><span class='bold'>" . xlt('Provider'). ": </span><span class='detail'>".text(User_Id_Look($dr))."</span></td>";
123 echo "</tr>\n";
124 $orow++;
126 function PrintEncFooter() {
127 global $enc_units, $enc_chg, $enc_pmt, $enc_adj, $enc_bal;
128 echo "<tr bgcolor='#DDFFFF'>";
129 echo "<td colspan='3'>&nbsp;</td>";
130 echo "<td class='detail'>". xlt('Encounter Balance').":</td>";
131 echo "<td class='detail' style='text-align: right;'>".text($enc_units)."</td>";
132 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_chg))."</td>";
133 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_pmt))."</td>";
134 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_adj))."</td>";
135 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_bal))."</td>";
136 echo "</tr>\n";
138 function PrintCreditDetail($detail, $pat, $unassigned=false) {
139 global $enc_pmt, $total_pmt, $enc_adj, $total_adj, $enc_bal, $total_bal;
140 global $bgcolor, $orow, $enc_units, $enc_chg;
141 foreach($detail as $pmt) {
142 if($unassigned) {
143 if(($pmt['pay_total'] - $pmt['applied']) == 0) continue;
145 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
146 $print = "<tr bgcolor='" . attr($bgcolor) . "'>";
147 $print .= "<td class='detail'>&nbsp;</td>";
148 $method = List_Look($pmt['payment_method'],'payment_method');
149 $desc = $pmt['description'];
150 $ref = $pmt['reference'];
151 if($unassigned) {
152 $memo = List_Look($pmt['adjustment_code'],'payment_adjustment_code');
153 } else {
154 $memo = $pmt['memo'];
156 $description = $method;
157 if($ref) {
158 if($description) { $description .= ' - '; }
159 $description .= $ref;
161 if($desc) {
162 if($description) { $description .= ': '; }
163 $description .= $desc;
165 if($memo) {
166 if($description) { $description .= ' '; }
167 $description .= '['.$memo.']';
169 $print .= "<td class='detail' colspan='2'>".
170 text($description)."&nbsp;</td>";
171 $payer = ($pmt['name'] == '') ? xl('Patient') : $pmt['name'];
172 if($unassigned) {
173 $pmt_date = substr($pmt['post_to_date'],0,10);
174 } else {
175 $pmt_date = substr($pmt['post_time'],0,10);
177 $print .= "<td class='detail'>".
178 text($pmt_date)."&nbsp;/&nbsp;".text($payer)."</td>";
179 $type = List_Look($pmt['payment_type'],'payment_type');
180 $print .= "<td class='detail'>".text($type)."&nbsp;</td>";
181 if($unassigned) {
182 $pmt_amt = $pmt['pay_total'] - $pmt['applied'];
183 $uac_bal = oeFormatMoney($pmt_amt * -1);
184 $uac_appl = oeFormatMoney($pmt['applied']);
185 $uac_total = oeFormatMoney($pmt['pay_total']);
186 $pmt_amt = $pmt['pay_total'];
187 $total_pmt = $total_pmt - $uac_bal;
188 } else {
189 $uac_total = '';
190 $uac_bal = '';
191 $uac_appl = '';
192 $pmt_amt = $pmt['pay_amount'];
193 $adj_amt = $pmt['adj_amount'];
194 $enc_pmt = $enc_pmt + $pmt['pay_amount'];
195 $total_pmt = $total_pmt + $pmt['pay_amount'];
196 $enc_adj = $enc_adj + $pmt['adj_amount'];
197 $total_adj = $total_adj + $pmt['adj_amount'];
199 $print_pmt = '';
200 if($pmt_amt != 0) $print_pmt = oeFormatMoney($pmt_amt);
201 $print_adj = '';
202 if($adj_amt != 0) $print_adj = oeFormatMoney($adj_amt);
203 $print .= "<td class='detail' style='text-align: right;'>".text($uac_appl)."&nbsp;</td>";
204 $print .= "<td class='detail' style='text-align: right;'>".text($print_pmt)."&nbsp;</td>";
205 $print .= "<td class='detail' style='text-align: right;'>".text($print_adj)."&nbsp;</td>";
206 $print .= "<td class='detail' style='text-align: right;'>".text($uac_bal)."&nbsp;</td>";
207 $print .= "</tr>\n";
208 echo $print;
209 if($pmt['follow_up_note'] != '') {
210 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
211 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
212 $print .= "<td class='detail' colspan='2'>&nbsp;</td>";
213 $print .= "<td colspan='7'>". xlt('Follow Up Note') .": ";
214 $print .= text($pmt['follow_up_note']);
215 $print .= "</td></tr>\n";
216 echo $print;
218 if($unassigned) {
219 $total_bal = $total_bal + $uac_bal;
220 } else {
221 $enc_bal = $enc_bal - $pmt_amt - $adj_amt;
222 $total_bal = $total_bal - $pmt_amt - $adj_amt;
224 $orow++;
226 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
228 if(!isset($_REQUEST['form_from_date'])) { $_REQUEST['form_from_date'] = ''; }
229 if(!isset($_REQUEST['form_to_date'])) { $_REQUEST['form_to_date'] = ''; }
230 if(!isset($_REQUEST['form_facility'])) { $_REQUEST['form_facility'] = ''; }
231 if(!isset($_REQUEST['form_provider'])) { $_REQUEST['form_provider'] = ''; }
232 if($type_form=='0') {
233 if(!isset($_REQUEST['form_patient'])) { $_REQUEST['form_patient'] = ''; }
234 if(!isset($_REQUEST['form_pid'])) { $_REQUEST['form_pid'] = ''; }
236 else
238 if(!isset($_REQUEST['form_patient'])) { $_REQUEST['form_patient'] = $pat_pid; }
239 if(!isset($_REQUEST['form_pid'])) { $_REQUEST['form_pid'] = $pat_pid; }
241 if(!isset($_REQUEST['form_csvexport'])) { $_REQUEST['form_csvexport'] = ''; }
242 if(!isset($_REQUEST['form_refresh'])) { $_REQUEST['form_refresh'] = ''; }
243 if(!isset($_REQUEST['$form_dob'])) { $_REQUEST['$form_dob'] = ''; }
245 if (substr($GLOBALS['ledger_begin_date'],0,1) == 'Y') {
246 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
247 $last_year = mktime(0,0,0,date('m'),date('d'),date('Y')-$ledger_time);
249 elseif (substr($GLOBALS['ledger_begin_date'],0,1) == 'M') {
250 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
251 $last_year = mktime(0,0,0,date('m')-$ledger_time ,date('d'),date('Y'));
253 elseif (substr($GLOBALS['ledger_begin_date'],0,1) == 'D') {
254 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
255 $last_year = mktime(0,0,0,date('m') ,date('d')-$ledger_time,date('Y'));
258 $form_from_date = date('Y-m-d', $last_year);
259 if($_REQUEST['form_from_date']) {
260 $form_from_date = fixDate($_REQUEST['form_from_date'], $last_year);
262 $form_to_date = fixDate($_REQUEST['form_to_date'] , date('Y-m-d'));
263 $form_facility = $_REQUEST['form_facility'];
264 $form_provider = $_REQUEST['form_provider'];
265 $form_patient = $_REQUEST['form_patient'];
266 $form_pid = $_REQUEST['form_pid'];
267 $form_dob = $_REQUEST['form_dob'];
269 if ($_REQUEST['form_csvexport']) {
270 header("Pragma: public");
271 header("Expires: 0");
272 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
273 header("Content-Type: application/force-download");
274 header("Content-Disposition: attachment; filename=svc_financial_report_".attr($form_from_date)."--".attr($form_to_date).".csv");
275 header("Content-Description: File Transfer");
276 } else {
278 <html>
279 <head>
280 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
281 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
282 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.1.3.2.js"></script>
283 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js"></script>
284 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-ui.js"></script>
285 <script type="text/javascript">
286 var mypcc = '<?php echo $GLOBALS['phone_country_code']; ?>';
287 var pt_name;
288 var pt_id;
289 function checkSubmit() {
290 var pat = document.forms[0].elements['form_patient'].value;
291 if(!pat || pat == 0) {
292 alert('<?php echo xls('A Patient Must Be Selected to Generate This Report') ?>');
293 return false;
295 document.forms[0].elements['form_refresh'].value = true;
296 document.forms[0].elements['form_csvexport'].value = '';
297 document.forms[0].submit();
299 function setpatient(pid, lname, fname, dob) {
300 document.forms[0].elements['form_patient'].value = lname + ', ' + fname;
301 document.forms[0].elements['form_pid'].value = pid;
302 document.forms[0].elements['form_dob'].value = dob;
304 function sel_patient() {
305 dlgopen('../main/calendar/find_patient_popup.php?pflag=0', '_blank', 500, 400);
307 </script>
309 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
310 <?php html_header_show();?>
311 <style type="text/css">
313 /* specifically include & exclude from printing */
314 @media print {
315 #report_parameters {
316 visibility: hidden;
317 display: none;
319 #report_parameters_daterange {
320 visibility: visible;
321 display: inline;
323 #report_results {
324 margin-top: 30px;
326 #report_header {
327 visibility: visible;
328 display: inline;
330 #title {
331 visibility: hidden;
332 display: none;
335 /* specifically exclude some from the screen */
336 @media screen {
337 #report_parameters_daterange {
338 visibility: hidden;
339 display: none;
341 #report_header {
342 visibility: hidden;
343 display: none;
345 #title {
346 visibility: visible;
347 display: inline;
350 </style>
352 <title><?php echo xlt('Patient Ledger by Date') ?></title>
354 <script language="JavaScript">
355 $(document).ready(function() {
356 var win = top.printLogSetup ? top : opener.top;
357 win.printLogSetup(document.getElementById('printbutton'));
359 </script>
361 </head>
362 <body class="body_top">
363 <?php if($type_form == '0') { ?>
364 <span class='title' id='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Patient Ledger by Date'); ?></span>
365 <?php }else{ ?>
366 <span class='title' id='title'><?php echo xlt('Patient Ledger'); ?></span>
367 <?php } ?>
368 <form method='post' action='pat_ledger.php?form=<?php echo attr($type_form);?>&patient_id=<?php echo attr($form_pid);?>' id='theform'>
369 <div id="report_parameters">
370 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
371 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
372 <table>
373 <tr>
374 <?php if($type_form == '1') { ?>
375 <td width='35%'>
376 <?php }else{ ?>
377 <td width='70%'>
378 <?php } ?>
379 <div style='float:left'>
380 <table class='text'>
381 <tr>
382 <?php if($type_form == '0') { ?>
383 <td class='label'>
384 <?php echo xlt('Facility'); ?>:
385 </td>
386 <td>
387 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
388 </td>
389 <td><?php echo xlt('Provider'); ?>:</td>
390 <td><?php
391 $query = "SELECT id, lname, fname FROM users WHERE ".
392 "authorized=1 AND active!=0 ORDER BY lname, fname";
393 $ures = sqlStatement($query);
394 echo " <select name='form_provider'>\n";
395 echo " <option value=''>-- " . xlt('All') . " --\n";
396 while ($urow = sqlFetchArray($ures)) {
397 $provid = $urow['id'];
398 echo " <option value='" . attr($provid) ."'";
399 if ($provid == $_REQUEST['form_provider']) echo " selected";
400 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
402 echo " </select>\n";
403 ?></td>
404 </tr><tr>
405 <?php } ?>
406 <td colspan="2">
407 <?php echo xlt('From'); ?>:&nbsp;&nbsp;&nbsp;&nbsp;
408 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr($form_from_date) ?>' onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
409 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22' id='img_from_date' border='0' alt='[?]' style='cursor:pointer' title='<?php echo xla("Click here to choose a date"); ?>'>
410 </td>
411 <td class='label'>
412 <?php echo xlt('To'); ?>:
413 </td>
414 <td>
415 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>' onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
416 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22' id='img_to_date' border='0' alt='[?]' style='cursor:pointer' title='<?php echo xla("Click here to choose a date"); ?>'>
417 </td>
418 <?php if($type_form == '0') { ?>
419 <td><span class='label'><?php echo xlt('Patient'); ?>:&nbsp;&nbsp;</span></td>
420 <td>
421 <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'); ?>' />
422 <?php }else{ ?>
423 <input type='hidden' name='form_patient' value='<?php echo attr($form_patient); ?>' />
424 <?php } ?>
425 <input type='hidden' name='form_pid' value='<?php echo attr($form_pid); ?>' />
426 <input type='hidden' name='form_dob' value='<?php echo attr($form_dob); ?>' />
428 </td>
429 </tr>
430 </table>
431 </div>
432 </td>
433 <td align='left' valign='middle' height="100%">
434 <table style='border-left:1px solid; width:100%; height:100%' >
435 <tr>
436 <td>
437 <div style='margin-left:15px'>
438 <a href='#' class='css_button' onclick="checkSubmit();" >
439 <span><?php echo xlt('Submit'); ?></span></a>
441 <?php if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) { ?>
442 <div id="controls">
443 <a href='#' class='css_button' id='printbutton'>
444 <span><?php echo xlt('Print Ledger'); ?></span></a>
445 <?php if($type_form == '1') { ?>
446 <a href="../patient_file/summary/demographics.php" <?php if (!$GLOBALS['concurrent_layout']) echo "target='Main'"; ?> class="css_button" onclick="top.restoreSession()">
447 <span><?php echo xlt('Back To Patient');?></span></a>
448 <?php } ?>
449 </div>
450 <?php } ?>
451 </div>
452 </td>
453 </tr>
454 </table>
455 </td>
456 </tr>
457 </table>
458 </div> <!-- end of parameters -->
460 <?php
461 } // end not export
462 $from_date = $form_from_date . ' 00:00:00';
463 $to_date = $form_to_date . ' 23:59:59';
464 if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) {
465 $rows = array();
466 $sqlBindArray = array();
467 $query = "select b.code_type, b.code, b.code_text, b.pid, b.provider_id, ".
468 "b.billed, b.payer_id, b.units, b.fee, b.bill_date, b.id, ".
469 "ins.name, ".
470 "fe.encounter, fe.date, fe.reason, fe.provider_id ".
471 "FROM form_encounter AS fe ".
472 "LEFT JOIN billing AS b ON b.pid=fe.pid AND b.encounter=fe.encounter ".
473 "LEFT JOIN insurance_companies AS ins ON b.payer_id = ins.id ".
474 "LEFT OUTER JOIN code_types AS c ON c.ct_key = b.code_type ".
475 "WHERE fe.date >= ? AND fe.date <= ? AND fe.pid = ? ";
476 array_push($sqlBindArray,$from_date,$to_date,$form_pid);
477 if ($form_facility) {
478 $query .= "AND fe.facility_id = ? ";
479 array_push($sqlBindArray,$form_facility);
481 if ($form_provider) {
482 $query .= "AND b.provider_id = ? ";
483 array_push($sqlBindArray,$form_provider);
485 $query .= "AND c.ct_proc = '1' ";
486 $query .= "AND activity > 0 ORDER BY fe.date, fe.id ";
487 $res = sqlStatement($query,$sqlBindArray);
489 if ($_REQUEST['form_csvexport']) {
490 // CSV headers:
491 if (true) {
492 echo '"Code/Enc Dt",';
493 echo '"Description",';
494 echo '"Billed/Who",';
495 echo '"Type/Units",';
496 echo '"Chg/Pmt Amount",'."\n";
498 } else {
499 if(!$form_facility) $form_facility = '3';
500 $facility = sqlQuery("SELECT * FROM facility WHERE id=?", array($form_facility));
501 $patient = sqlQuery("SELECT * from patient_data WHERE pid=?", array($form_patient));
502 $pat_dob = $patient['DOB'];
503 $pat_name = $patient['fname']. ' ' . $patient['lname'];
505 <div id="report_header">
506 <table width="98%" border="0" cellspacing="0" cellpadding="0">
507 <tr>
508 <td class="title" ><?php echo text($facility{'name'}); ?></td>
509 </tr>
510 <tr>
511 <td class="title" ><?php echo text($facility{'street'}); ?></td>
512 </tr>
513 <tr>
514 <td class="title" ><?php echo text($facility{'city'}).", ".text($facility{'state'})." ".text($facility{'postal_code'}); ?></td>
515 </tr>
516 <tr>
517 <td class="title" ><?php echo xlt('Phone').': ' .text($facility{'phone'}); ?></td>
518 </tr>
519 <tr>
520 <td class="title" ><?php echo xlt('Tax Id').': ' .text($facility{'federal_ein'}); ?></td>
521 </tr>
522 <tr><td>&nbsp;</td></tr>
523 <tr>
524 <td class="title" ><?php echo xlt('Patient Ledger'); ?></td>
525 </tr>
526 <tr>
527 <?php
528 $title = xl('All Providers');
529 if($form_provider) { $title = xl('For Provider') . ': '.User_Id_Look($form_provider); }
531 <td class="title" ><?php echo text($title); ?></td>
532 </tr>
533 <tr>
534 <?php
535 $title = xl('For Dates') . ': '.$form_from_date.' - '.$form_to_date;
537 <td class="title" ><?php echo text($title); ?></td>
538 </tr>
539 </table>
540 <br/>
541 <table width="100%" border="0" cellspacing="0" cellpadding="0">
542 <tr>
543 <td class='bold' ><?php echo xlt('Date')?>:
544 <?php echo text(date('Y-m-d')); ?></td>
545 <td class='bold' ><?php echo xlt('Patient')?>:
546 <?php if($type_form == '1') { ?>
547 <?php echo text($pat_name); ?></td>
548 <?php }else{ ?>
549 <?php echo text($form_patient); ?></td>
550 <?php } ?>
551 <td class='bold' ><?php echo xlt('DOB')?>:
552 <?php if($type_form == '1') { ?>
553 <?php echo text($pat_dob);?></td>
554 <?php }else{ ?>
555 <?php echo text($form_dob); ?></td>
556 <?php } ?>
557 <td class='bold' > <?php echo xlt('ID')?>:
558 <?php echo text($form_pid);?></td>
559 </tr>
560 </table>
561 </div>
562 <div id="report_results">
563 <table >
564 <tr>
565 <td class='bold' ><?php echo xlt('Code'); ?></td>
566 <td colspan="2" class='bold' ><?php echo xlt('Description'); ?></td>
567 <td class='bold' ><?php echo xlt('Billed Date'); ?> / <?php echo xlt('Payor'); ?></td>
568 <td class='bold' ><?php echo xlt('Type'); ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
569 <?php echo xlt('Units'); ?></td>
570 <td class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Charge'); ?></td>
571 <td align='right' class='bold' >&nbsp;&nbsp;<?php echo xlt('Payment'); ?></td>
572 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Adjustment'); ?></td>
573 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('Balance'); ?></td>
574 </tr>
575 <tr>
576 <td>&nbsp;&nbsp;&nbsp;</td>
577 <td colspan="2" >&nbsp;&nbsp;&nbsp;</td>
578 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
579 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
580 &nbsp;&nbsp;&nbsp;</td>
581 <td class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Appl'); ?></td>
582 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Tot'); ?></td>
583 <td>&nbsp;</td>
584 <td>&nbsp;</td>
585 </tr>
586 <?php
588 $orow = 0;
589 $prev_encounter_id = -1;
590 $hdr_printed = false;
591 $prev_row = array();
592 while ($erow = sqlFetchArray($res)) {
593 $print = '';
594 $csv = '';
595 if($erow['encounter'] != $prev_encounter_id) {
596 if($prev_encounter_id != -1) {
597 $credits = GetAllCredits($prev_encounter_id, $form_pid);
598 if(count($credits) > 0) {
599 if(!$hdr_printed) {
600 PrintEncHeader($prev_row{'date'},
601 $prev_row{'reason'}, $prev_row{'provider_id'});
603 PrintCreditDetail($credits, $form_pid);
605 if($hdr_printed) PrintEncFooter();
606 $hdr_printed = false;
608 $enc_units = $enc_chg = $enc_pmt = $enc_adj = $enc_bal = 0;
610 if($erow{'id'}) {
611 // Now print an encounter heading line -
612 if(!$hdr_printed) {
613 PrintEncHeader($erow{'date'},
614 $erow{'reason'}, $erow{'provider_id'});
615 $hdr_printed = true;
618 $code_desc = $erow['code_text'];
619 if(strlen($code_desc) > 50) $code_desc = substr($code_desc,0,50).'...';
620 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
621 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
622 $print .= "<td class='detail'>".text($erow['code'])."</td>";
623 $print .= "<td class='detail' colspan='2'>".text($code_desc)."</td>";
624 $who = ($erow['name'] == '') ? xl('Self') : $erow['name'];
625 $bill = substr($erow['bill_date'],0,10);
626 if($bill == '') { $bill = 'unbilled'; }
627 $print .= "<td class='detail'>".text($bill)."&nbsp;/&nbsp;".text($who)."</td>";
628 $print .= "<td class='detail' style='text-align: right;'>". text($erow['units'])."</td>";
629 $print .= "<td class='detail' style='text-align: right;'>". text(oeFormatMoney($erow['fee']))."</td>";
630 $print .= "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
631 $print .= "</tr>\n";
633 $total_units += $erow['units'];
634 $total_chg += $erow['fee'];
635 $total_bal += $erow['fee'];
636 $enc_units += $erow['units'];
637 $enc_chg += $erow['fee'];
638 $enc_bal += $erow['fee'];
639 $orow++;
641 if ($_REQUEST['form_csvexport']) {
642 echo $csv;
643 } else {
644 echo $print;
647 $prev_encounter_id = $erow{'encounter'};
648 $prev_row = $erow;
650 if($prev_encounter_id != -1) {
651 $credits = GetAllCredits($prev_encounter_id, $form_pid);
652 if(count($credits) > 0) {
653 if(!$hdr_printed) {
654 PrintEncHeader($prev_row{'date'},
655 $prev_row{'reason'}, $prev_row{'provider_id'});
657 PrintCreditDetail($credits, $form_pid);
659 if($hdr_printed) PrintEncFooter();
661 // This is the end of the encounter/charge loop -
662 $uac = GetAllUnapplied($form_pid,$from_date,$to_date);
663 if(count($uac) > 0) {
664 if($orow) {
665 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
666 echo "<tr bgcolor='#FFFFFF'><td colspan='9'>&nbsp;</td></tr>\n";
668 PrintCreditDetail($uac, $form_pid, true);
670 if (!$_REQUEST['form_csvexport'] && $orow) {
671 echo "<tr bgcolor='#DDFFFF'>\n";
672 echo " <td colspan='2'>&nbsp;</td>";
673 echo " <td class='bold' colspan='2'>" . xlt("Grand Total") ."</td>\n";
674 echo " <td class='bold' style='text-align: right;'>". text($total_units) ."</td>\n";
675 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_chg)) ."</td>\n";
676 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_pmt)) ."</td>\n";
677 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_adj)) ."</td>\n";
678 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_bal)) . "</td>\n";
679 echo " </tr>\n";
681 </table>
682 <tr><td>&nbsp;</td></tr><br><br>
683 <?php if($GLOBALS['print_next_appointment_on_ledger'] == 1) {
684 $next_day = mktime(0,0,0,date('m'),date('d')+1,date('Y'));
685 # add one day to date so it will not get todays appointment
686 $current_date2 = date('Y-m-d', $next_day);
687 $events = fetchNextXAppts($current_date2,$form_pid);
688 $next_appoint_date = oeFormatShortDate($events[0]['pc_eventDate']);
689 $next_appoint_time = substr($events[0]['pc_startTime'],0,5);
690 if(strlen(umname) != 0 ) {
691 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['umname'] . ' ' . $events[0]['ulname'];
693 else
695 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['ulname'];
697 if(strlen($next_appoint_time) != 0) {
699 <tr>
700 <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>
701 </tr>
703 <?php
705 } // end ($GLOBALS['print_next_appointment_on_ledger'] == 1)
706 } // end (!$_REQUEST['form_csvexport'] && $orow)
707 echo "</div>\n";
709 if (! $_REQUEST['form_csvexport']) {
710 if ( $_REQUEST['form_refresh'] && $orow <= 0) {
711 echo "<span style='font-size:10pt;'>";
712 echo xlt('No matches found. Try search again.');
713 echo "</span>";
714 echo '<script>document.getElementById("report_results").style.display="none";</script>';
715 echo '<script>document.getElementById("controls").style.display="none";</script>';
718 if (!$_REQUEST['form_refresh'] && !$_REQUEST['form_csvexport']) { ?>
719 <div class='text'>
720 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
721 </div>
722 <?php } ?>
723 </form>
724 </body>
726 <!-- stuff for the popup calendar -->
728 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
729 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
730 <script type="text/javascript" src="<?php echo $GLOBALS['webroot']; ?>/library/dynarch_calendar.js"></script>
731 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
732 <script type="text/javascript" src="<?php echo $GLOBALS['webroot']; ?>/library/dynarch_calendar_setup.js"></script>
733 <script language="Javascript">
734 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
735 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
736 top.restoreSession();
737 </script>
738 </html>
739 <?php
740 } // End not csv export