added new datepicker to amendments gui
[openemr.git] / interface / reports / pat_ledger.php
blob774d57614308b105f81df4ba6abb9d28c0f2e7b4
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>
7 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
9 * LICENSE: This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
20 * @package OpenEMR
21 * @author WMT
22 * @author Terry Hill <terry@lillysystems.com>
23 * @author Brady Miller <brady.g.miller@gmail.com>
24 * @link http://www.open-emr.org
27 $sanitize_all_escapes=true;
28 $fake_register_globals=false;
29 require_once('../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'].'/appointments.inc.php');
36 $enc_units = $total_units = 0;
37 $enc_chg = $total_chg = 0;
38 $enc_pmt = $total_pmt = 0;
39 $enc_adj = $total_adj = 0;
40 $enc_bal = $total_bal = 0;
41 $bgcolor = "#FFFFDD";
42 $orow = 0;
44 $pat_pid = $_GET['patient_id'];
45 $type_form = $_GET['form'];
47 if (! acl_check('acct', 'rep')) die(xlt("Unauthorized access."));
49 function GetAllUnapplied($pat='',$from_dt='',$to_dt='') {
50 $all = array();
51 if(!$pat) return($all);
52 $sql = "SELECT ar_session.*, ins.name, " .
53 "pat.lname, pat.fname, pat.mname, " .
54 "(SELECT SUM(ar_activity.pay_amount) FROM ar_activity WHERE " .
55 "ar_activity.session_id = ar_session.session_id) AS applied " .
56 "FROM ar_session " .
57 "LEFT JOIN insurance_companies AS ins on ar_session.payer_id = ins.id " .
58 "LEFT JOIN patient_data AS pat on ar_session.patient_id = pat.pid " .
59 "WHERE " .
60 "ar_session.created_time >= ? AND ar_session.created_time <= ? " .
61 "AND ar_session.patient_id=?";
62 $result = sqlStatement($sql, array($from_dt, $to_dt, $pat));
63 $iter = 0;
64 while($row = sqlFetchArray($result)) {
65 $all[$iter] = $row;
66 $iter++;
68 return($all);
71 function User_Id_Look($thisField) {
72 if(!$thisField) return '';
73 $ret = '';
74 $rlist= sqlStatement("SELECT lname, fname, mname FROM users WHERE id=?",array($thisField));
75 $rrow= sqlFetchArray($rlist);
76 if($rrow) {
77 $ret = $rrow{'lname'}.', '.$rrow{'fname'}.' '.$rrow{'mname'};
79 return $ret;
82 function List_Look($thisData, $thisList) {
83 if($thisList == 'occurrence') {
84 if(!$thisData || $thisData == '') return xl('Unknown or N/A');
86 if($thisData == '') return '';
87 $fres=sqlStatement("SELECT title FROM list_options WHERE list_id = ? ".
88 "AND option_id = ? AND activity = 1", array($thisList, $thisData));
89 if($fres) {
90 $rret=sqlFetchArray($fres);
91 $dispValue= xl_list_label($rret{'title'});
92 if($thisList == 'occurrence' && $dispValue == '') {
93 $dispValue = xl('Unknown or N/A');
95 } else {
96 $dispValue= xl('Not Found');
98 return $dispValue;
101 function GetAllCredits($enc = '', $pat='') {
102 $all = array();
103 if(!$enc || !$pat) return($all);
104 $sql = "SELECT activity.*, session.*, ins.name FROM ar_activity AS ".
105 "activity LEFT JOIN ar_session AS session USING (session_id) ".
106 "LEFT JOIN insurance_companies AS ins ON session.payer_id = ".
107 "ins.id WHERE encounter=? AND pid=? ".
108 "ORDER BY sequence_no";
109 $result = sqlStatement($sql, array($enc, $pat));
110 $iter = 0;
111 while($row = sqlFetchArray($result)) {
112 $all[$iter] = $row;
113 $iter++;
115 return($all);
117 function PrintEncHeader($dt, $rsn, $dr) {
118 global $bgcolor, $orow;
119 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
120 echo "<tr bgcolor='#FFFFFF'>";
121 if(strlen($rsn) > 50) $rsn = substr($rsn,0,50).'...';
122 echo "<td colspan='4'><span class='bold'>".xlt('Encounter Dt / Rsn'). ": </span><span class='detail'>".text(substr($dt,0,10))." / ".text($rsn)."</span></td>";
123 echo "<td colspan='5'><span class='bold'>" . xlt('Provider'). ": </span><span class='detail'>".text(User_Id_Look($dr))."</span></td>";
124 echo "</tr>\n";
125 $orow++;
127 function PrintEncFooter() {
128 global $enc_units, $enc_chg, $enc_pmt, $enc_adj, $enc_bal;
129 echo "<tr bgcolor='#DDFFFF'>";
130 echo "<td colspan='3'>&nbsp;</td>";
131 echo "<td class='detail'>". xlt('Encounter Balance').":</td>";
132 echo "<td class='detail' style='text-align: right;'>".text($enc_units)."</td>";
133 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_chg))."</td>";
134 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_pmt))."</td>";
135 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_adj))."</td>";
136 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_bal))."</td>";
137 echo "</tr>\n";
139 function PrintCreditDetail($detail, $pat, $unassigned=false) {
140 global $enc_pmt, $total_pmt, $enc_adj, $total_adj, $enc_bal, $total_bal;
141 global $bgcolor, $orow, $enc_units, $enc_chg;
142 foreach($detail as $pmt) {
143 if($unassigned) {
144 if(($pmt['pay_total'] - $pmt['applied']) == 0) continue;
146 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
147 $print = "<tr bgcolor='" . attr($bgcolor) . "'>";
148 $print .= "<td class='detail'>&nbsp;</td>";
149 $method = List_Look($pmt['payment_method'],'payment_method');
150 $desc = $pmt['description'];
151 $ref = $pmt['reference'];
152 if($unassigned) {
153 $memo = List_Look($pmt['adjustment_code'],'payment_adjustment_code');
154 } else {
155 $memo = $pmt['memo'];
157 $description = $method;
158 if($ref) {
159 if($description) { $description .= ' - '; }
160 $description .= $ref;
162 if($desc) {
163 if($description) { $description .= ': '; }
164 $description .= $desc;
166 if($memo) {
167 if($description) { $description .= ' '; }
168 $description .= '['.$memo.']';
170 $print .= "<td class='detail' colspan='2'>".
171 text($description)."&nbsp;</td>";
172 $payer = ($pmt['name'] == '') ? xl('Patient') : $pmt['name'];
173 if($unassigned) {
174 $pmt_date = substr($pmt['post_to_date'],0,10);
175 } else {
176 $pmt_date = substr($pmt['post_time'],0,10);
178 $print .= "<td class='detail'>".
179 text($pmt_date)."&nbsp;/&nbsp;".text($payer)."</td>";
180 $type = List_Look($pmt['payment_type'],'payment_type');
181 $print .= "<td class='detail'>".text($type)."&nbsp;</td>";
182 if($unassigned) {
183 $pmt_amt = $pmt['pay_total'] - $pmt['applied'];
184 $uac_bal = oeFormatMoney($pmt_amt * -1);
185 $uac_appl = oeFormatMoney($pmt['applied']);
186 $uac_total = oeFormatMoney($pmt['pay_total']);
187 $pmt_amt = $pmt['pay_total'];
188 $total_pmt = $total_pmt - $uac_bal;
189 } else {
190 $uac_total = '';
191 $uac_bal = '';
192 $uac_appl = '';
193 $pmt_amt = $pmt['pay_amount'];
194 $adj_amt = $pmt['adj_amount'];
195 $enc_pmt = $enc_pmt + $pmt['pay_amount'];
196 $total_pmt = $total_pmt + $pmt['pay_amount'];
197 $enc_adj = $enc_adj + $pmt['adj_amount'];
198 $total_adj = $total_adj + $pmt['adj_amount'];
200 $print_pmt = '';
201 if($pmt_amt != 0) $print_pmt = oeFormatMoney($pmt_amt);
202 $print_adj = '';
203 if($adj_amt != 0) $print_adj = oeFormatMoney($adj_amt);
204 $print .= "<td class='detail' style='text-align: right;'>".text($uac_appl)."&nbsp;</td>";
205 $print .= "<td class='detail' style='text-align: right;'>".text($print_pmt)."&nbsp;</td>";
206 $print .= "<td class='detail' style='text-align: right;'>".text($print_adj)."&nbsp;</td>";
207 $print .= "<td class='detail' style='text-align: right;'>".text($uac_bal)."&nbsp;</td>";
208 $print .= "</tr>\n";
209 echo $print;
210 if($pmt['follow_up_note'] != '') {
211 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
212 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
213 $print .= "<td class='detail' colspan='2'>&nbsp;</td>";
214 $print .= "<td colspan='7'>". xlt('Follow Up Note') .": ";
215 $print .= text($pmt['follow_up_note']);
216 $print .= "</td></tr>\n";
217 echo $print;
219 if($unassigned) {
220 $total_bal = $total_bal + $uac_bal;
221 } else {
222 $enc_bal = $enc_bal - $pmt_amt - $adj_amt;
223 $total_bal = $total_bal - $pmt_amt - $adj_amt;
225 $orow++;
227 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
229 if(!isset($_REQUEST['form_from_date'])) { $_REQUEST['form_from_date'] = ''; }
230 if(!isset($_REQUEST['form_to_date'])) { $_REQUEST['form_to_date'] = ''; }
231 if(!isset($_REQUEST['form_facility'])) { $_REQUEST['form_facility'] = ''; }
232 if(!isset($_REQUEST['form_provider'])) { $_REQUEST['form_provider'] = ''; }
233 if($type_form=='0') {
234 if(!isset($_REQUEST['form_patient'])) { $_REQUEST['form_patient'] = ''; }
235 if(!isset($_REQUEST['form_pid'])) { $_REQUEST['form_pid'] = ''; }
237 else
239 if(!isset($_REQUEST['form_patient'])) { $_REQUEST['form_patient'] = $pat_pid; }
240 if(!isset($_REQUEST['form_pid'])) { $_REQUEST['form_pid'] = $pat_pid; }
242 if(!isset($_REQUEST['form_csvexport'])) { $_REQUEST['form_csvexport'] = ''; }
243 if(!isset($_REQUEST['form_refresh'])) { $_REQUEST['form_refresh'] = ''; }
244 if(!isset($_REQUEST['$form_dob'])) { $_REQUEST['$form_dob'] = ''; }
246 if (substr($GLOBALS['ledger_begin_date'],0,1) == 'Y') {
247 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
248 $last_year = mktime(0,0,0,date('m'),date('d'),date('Y')-$ledger_time);
250 elseif (substr($GLOBALS['ledger_begin_date'],0,1) == 'M') {
251 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
252 $last_year = mktime(0,0,0,date('m')-$ledger_time ,date('d'),date('Y'));
254 elseif (substr($GLOBALS['ledger_begin_date'],0,1) == 'D') {
255 $ledger_time = substr($GLOBALS['ledger_begin_date'],1,1);
256 $last_year = mktime(0,0,0,date('m') ,date('d')-$ledger_time,date('Y'));
259 $form_from_date = date('Y-m-d', $last_year);
260 if($_REQUEST['form_from_date']) {
261 $form_from_date = fixDate($_REQUEST['form_from_date'], $last_year);
263 $form_to_date = fixDate($_REQUEST['form_to_date'] , date('Y-m-d'));
264 $form_facility = $_REQUEST['form_facility'];
265 $form_provider = $_REQUEST['form_provider'];
266 $form_patient = $_REQUEST['form_patient'];
267 $form_pid = $_REQUEST['form_pid'];
268 $form_dob = $_REQUEST['form_dob'];
270 if ($_REQUEST['form_csvexport']) {
271 header("Pragma: public");
272 header("Expires: 0");
273 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
274 header("Content-Type: application/force-download");
275 header("Content-Disposition: attachment; filename=svc_financial_report_".attr($form_from_date)."--".attr($form_to_date).".csv");
276 header("Content-Description: File Transfer");
277 } else {
279 <html>
280 <head>
282 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
283 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
284 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js?v=<?php echo $v_js_includes; ?>"></script>
285 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
287 <script type="text/javascript">
288 var mypcc = '<?php echo $GLOBALS['phone_country_code']; ?>';
289 var pt_name;
290 var pt_id;
291 function checkSubmit() {
292 var pat = document.forms[0].elements['form_patient'].value;
293 if(!pat || pat == 0) {
294 alert('<?php echo xls('A Patient Must Be Selected to Generate This Report') ?>');
295 return false;
297 document.forms[0].elements['form_refresh'].value = true;
298 document.forms[0].elements['form_csvexport'].value = '';
299 document.forms[0].submit();
301 function setpatient(pid, lname, fname, dob) {
302 document.forms[0].elements['form_patient'].value = lname + ', ' + fname;
303 document.forms[0].elements['form_pid'].value = pid;
304 document.forms[0].elements['form_dob'].value = dob;
306 function sel_patient() {
307 dlgopen('../main/calendar/find_patient_popup.php?pflag=0', '_blank', 500, 400);
309 </script>
311 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
312 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
314 <?php html_header_show();?>
315 <style type="text/css">
317 /* specifically include & exclude from printing */
318 @media print {
319 #report_parameters {
320 visibility: hidden;
321 display: none;
323 #report_parameters_daterange {
324 visibility: visible;
325 display: inline;
327 #report_results {
328 margin-top: 30px;
330 #report_header {
331 visibility: visible;
332 display: inline;
334 #title {
335 visibility: hidden;
336 display: none;
339 /* specifically exclude some from the screen */
340 @media screen {
341 #report_parameters_daterange {
342 visibility: hidden;
343 display: none;
345 #report_header {
346 visibility: hidden;
347 display: none;
349 #title {
350 visibility: visible;
351 display: inline;
354 </style>
356 <title><?php echo xlt('Patient Ledger by Date') ?></title>
358 <script language="JavaScript">
359 $(document).ready(function() {
360 var win = top.printLogSetup ? top : opener.top;
361 win.printLogSetup(document.getElementById('printbutton'));
363 $('.datepicker').datetimepicker({
364 <?php $datetimepicker_timepicker = false; ?>
365 <?php $datetimepicker_formatInput = false; ?>
366 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
367 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
370 </script>
372 </head>
373 <body class="body_top">
374 <?php if($type_form == '0') { ?>
375 <span class='title' id='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Patient Ledger by Date'); ?></span>
376 <?php }else{ ?>
377 <span class='title' id='title'><?php echo xlt('Patient Ledger'); ?></span>
378 <?php } ?>
379 <form method='post' action='pat_ledger.php?form=<?php echo attr($type_form);?>&patient_id=<?php echo attr($form_pid);?>' id='theform'>
380 <div id="report_parameters">
381 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
382 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
383 <table>
384 <tr>
385 <?php if($type_form == '1') { ?>
386 <td width='35%'>
387 <?php }else{ ?>
388 <td width='70%'>
389 <?php } ?>
390 <div style='float:left'>
391 <table class='text'>
392 <tr>
393 <?php if($type_form == '0') { ?>
394 <td class='label'>
395 <?php echo xlt('Facility'); ?>:
396 </td>
397 <td>
398 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
399 </td>
400 <td><?php echo xlt('Provider'); ?>:</td>
401 <td><?php
402 $query = "SELECT id, lname, fname FROM users WHERE ".
403 "authorized=1 AND active!=0 ORDER BY lname, fname";
404 $ures = sqlStatement($query);
405 echo " <select name='form_provider'>\n";
406 echo " <option value=''>-- " . xlt('All') . " --\n";
407 while ($urow = sqlFetchArray($ures)) {
408 $provid = $urow['id'];
409 echo " <option value='" . attr($provid) ."'";
410 if ($provid == $_REQUEST['form_provider']) echo " selected";
411 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
413 echo " </select>\n";
414 ?></td>
415 </tr><tr>
416 <?php } ?>
417 <td colspan="2">
418 <?php echo xlt('From'); ?>:&nbsp;&nbsp;&nbsp;&nbsp;
419 <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'>
420 </td>
421 <td class='label'>
422 <?php echo xlt('To'); ?>:
423 </td>
424 <td>
425 <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'>
426 </td>
427 <?php if($type_form == '0') { ?>
428 <td><span class='label'><?php echo xlt('Patient'); ?>:&nbsp;&nbsp;</span></td>
429 <td>
430 <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'); ?>' />
431 <?php }else{ ?>
432 <input type='hidden' name='form_patient' value='<?php echo attr($form_patient); ?>' />
433 <?php } ?>
434 <input type='hidden' name='form_pid' value='<?php echo attr($form_pid); ?>' />
435 <input type='hidden' name='form_dob' value='<?php echo attr($form_dob); ?>' />
437 </td>
438 </tr>
439 </table>
440 </div>
441 </td>
442 <td align='left' valign='middle' height="100%">
443 <table style='border-left:1px solid; width:100%; height:100%' >
444 <tr>
445 <td>
446 <div style='margin-left:15px'>
447 <a href='#' class='css_button' onclick="checkSubmit();" >
448 <span><?php echo xlt('Submit'); ?></span></a>
450 <?php if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) { ?>
451 <div id="controls">
452 <a href='#' class='css_button' id='printbutton'>
453 <span><?php echo xlt('Print Ledger'); ?></span></a>
454 <?php if($type_form == '1') { ?>
455 <a href="../patient_file/summary/demographics.php" class="css_button" onclick="top.restoreSession()">
456 <span><?php echo xlt('Back To Patient');?></span></a>
457 <?php } ?>
458 </div>
459 <?php } ?>
460 </div>
461 </td>
462 </tr>
463 </table>
464 </td>
465 </tr>
466 </table>
467 </div> <!-- end of parameters -->
469 <?php
470 } // end not export
471 $from_date = $form_from_date . ' 00:00:00';
472 $to_date = $form_to_date . ' 23:59:59';
473 if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) {
474 $rows = array();
475 $sqlBindArray = array();
476 $query = "select b.code_type, b.code, b.code_text, b.pid, b.provider_id, ".
477 "b.billed, b.payer_id, b.units, b.fee, b.bill_date, b.id, ".
478 "ins.name, ".
479 "fe.encounter, fe.date, fe.reason, fe.provider_id ".
480 "FROM form_encounter AS fe ".
481 "LEFT JOIN billing AS b ON b.pid=fe.pid AND b.encounter=fe.encounter ".
482 "LEFT JOIN insurance_companies AS ins ON b.payer_id = ins.id ".
483 "LEFT OUTER JOIN code_types AS c ON c.ct_key = b.code_type ".
484 "WHERE fe.date >= ? AND fe.date <= ? AND fe.pid = ? ";
485 array_push($sqlBindArray,$from_date,$to_date,$form_pid);
486 if ($form_facility) {
487 $query .= "AND fe.facility_id = ? ";
488 array_push($sqlBindArray,$form_facility);
490 if ($form_provider) {
491 $query .= "AND b.provider_id = ? ";
492 array_push($sqlBindArray,$form_provider);
494 $query .= "AND c.ct_proc = '1' ";
495 $query .= "AND activity > 0 ORDER BY fe.date, fe.id ";
496 $res = sqlStatement($query,$sqlBindArray);
498 if ($_REQUEST['form_csvexport']) {
499 // CSV headers:
500 if (true) {
501 echo '"Code/Enc Dt",';
502 echo '"Description",';
503 echo '"Billed/Who",';
504 echo '"Type/Units",';
505 echo '"Chg/Pmt Amount",'."\n";
507 } else {
508 if(!$form_facility) $form_facility = '3';
509 $facility = sqlQuery("SELECT * FROM facility WHERE id=?", array($form_facility));
510 $patient = sqlQuery("SELECT * from patient_data WHERE pid=?", array($form_patient));
511 $pat_dob = $patient['DOB'];
512 $pat_name = $patient['fname']. ' ' . $patient['lname'];
514 <div id="report_header">
515 <table width="98%" border="0" cellspacing="0" cellpadding="0">
516 <tr>
517 <td class="title" ><?php echo text($facility{'name'}); ?></td>
518 </tr>
519 <tr>
520 <td class="title" ><?php echo text($facility{'street'}); ?></td>
521 </tr>
522 <tr>
523 <td class="title" ><?php echo text($facility{'city'}).", ".text($facility{'state'})." ".text($facility{'postal_code'}); ?></td>
524 </tr>
525 <tr>
526 <td class="title" ><?php echo xlt('Phone').': ' .text($facility{'phone'}); ?></td>
527 </tr>
528 <tr>
529 <td class="title" ><?php echo xlt('Tax Id').': ' .text($facility{'federal_ein'}); ?></td>
530 </tr>
531 <tr><td>&nbsp;</td></tr>
532 <tr>
533 <td class="title" ><?php echo xlt('Patient Ledger'); ?></td>
534 </tr>
535 <tr>
536 <?php
537 $title = xl('All Providers');
538 if($form_provider) { $title = xl('For Provider') . ': '.User_Id_Look($form_provider); }
540 <td class="title" ><?php echo text($title); ?></td>
541 </tr>
542 <tr>
543 <?php
544 $title = xl('For Dates') . ': '.$form_from_date.' - '.$form_to_date;
546 <td class="title" ><?php echo text($title); ?></td>
547 </tr>
548 </table>
549 <br/>
550 <table width="100%" border="0" cellspacing="0" cellpadding="0">
551 <tr>
552 <td class='bold' ><?php echo xlt('Date')?>:
553 <?php echo text(date('Y-m-d')); ?></td>
554 <td class='bold' ><?php echo xlt('Patient')?>:
555 <?php if($type_form == '1') { ?>
556 <?php echo text($pat_name); ?></td>
557 <?php }else{ ?>
558 <?php echo text($form_patient); ?></td>
559 <?php } ?>
560 <td class='bold' ><?php echo xlt('DOB')?>:
561 <?php if($type_form == '1') { ?>
562 <?php echo text($pat_dob);?></td>
563 <?php }else{ ?>
564 <?php echo text($form_dob); ?></td>
565 <?php } ?>
566 <td class='bold' > <?php echo xlt('ID')?>:
567 <?php echo text($form_pid);?></td>
568 </tr>
569 </table>
570 </div>
571 <div id="report_results">
572 <table >
573 <tr>
574 <td class='bold' ><?php echo xlt('Code'); ?></td>
575 <td colspan="2" class='bold' ><?php echo xlt('Description'); ?></td>
576 <td class='bold' ><?php echo xlt('Billed Date'); ?> / <?php echo xlt('Payor'); ?></td>
577 <td class='bold' ><?php echo xlt('Type'); ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
578 <?php echo xlt('Units'); ?></td>
579 <td class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Charge'); ?></td>
580 <td align='right' class='bold' >&nbsp;&nbsp;<?php echo xlt('Payment'); ?></td>
581 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Adjustment'); ?></td>
582 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('Balance'); ?></td>
583 </tr>
584 <tr>
585 <td>&nbsp;&nbsp;&nbsp;</td>
586 <td colspan="2" >&nbsp;&nbsp;&nbsp;</td>
587 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
588 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
589 &nbsp;&nbsp;&nbsp;</td>
590 <td class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Appl'); ?></td>
591 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Tot'); ?></td>
592 <td>&nbsp;</td>
593 <td>&nbsp;</td>
594 </tr>
595 <?php
597 $orow = 0;
598 $prev_encounter_id = -1;
599 $hdr_printed = false;
600 $prev_row = array();
601 while ($erow = sqlFetchArray($res)) {
602 $print = '';
603 $csv = '';
604 if($erow['encounter'] != $prev_encounter_id) {
605 if($prev_encounter_id != -1) {
606 $credits = GetAllCredits($prev_encounter_id, $form_pid);
607 if(count($credits) > 0) {
608 if(!$hdr_printed) {
609 PrintEncHeader($prev_row{'date'},
610 $prev_row{'reason'}, $prev_row{'provider_id'});
612 PrintCreditDetail($credits, $form_pid);
614 if($hdr_printed) PrintEncFooter();
615 $hdr_printed = false;
617 $enc_units = $enc_chg = $enc_pmt = $enc_adj = $enc_bal = 0;
619 if($erow{'id'}) {
620 // Now print an encounter heading line -
621 if(!$hdr_printed) {
622 PrintEncHeader($erow{'date'},
623 $erow{'reason'}, $erow{'provider_id'});
624 $hdr_printed = true;
627 $code_desc = $erow['code_text'];
628 if(strlen($code_desc) > 50) $code_desc = substr($code_desc,0,50).'...';
629 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
630 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
631 $print .= "<td class='detail'>".text($erow['code'])."</td>";
632 $print .= "<td class='detail' colspan='2'>".text($code_desc)."</td>";
633 $who = ($erow['name'] == '') ? xl('Self') : $erow['name'];
634 $bill = substr($erow['bill_date'],0,10);
635 if($bill == '') { $bill = 'unbilled'; }
636 $print .= "<td class='detail'>".text($bill)."&nbsp;/&nbsp;".text($who)."</td>";
637 $print .= "<td class='detail' style='text-align: right;'>". text($erow['units'])."</td>";
638 $print .= "<td class='detail' style='text-align: right;'>". text(oeFormatMoney($erow['fee']))."</td>";
639 $print .= "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
640 $print .= "</tr>\n";
642 $total_units += $erow['units'];
643 $total_chg += $erow['fee'];
644 $total_bal += $erow['fee'];
645 $enc_units += $erow['units'];
646 $enc_chg += $erow['fee'];
647 $enc_bal += $erow['fee'];
648 $orow++;
650 if ($_REQUEST['form_csvexport']) {
651 echo $csv;
652 } else {
653 echo $print;
656 $prev_encounter_id = $erow{'encounter'};
657 $prev_row = $erow;
659 if($prev_encounter_id != -1) {
660 $credits = GetAllCredits($prev_encounter_id, $form_pid);
661 if(count($credits) > 0) {
662 if(!$hdr_printed) {
663 PrintEncHeader($prev_row{'date'},
664 $prev_row{'reason'}, $prev_row{'provider_id'});
666 PrintCreditDetail($credits, $form_pid);
668 if($hdr_printed) PrintEncFooter();
670 // This is the end of the encounter/charge loop -
671 $uac = GetAllUnapplied($form_pid,$from_date,$to_date);
672 if(count($uac) > 0) {
673 if($orow) {
674 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
675 echo "<tr bgcolor='#FFFFFF'><td colspan='9'>&nbsp;</td></tr>\n";
677 PrintCreditDetail($uac, $form_pid, true);
679 if (!$_REQUEST['form_csvexport'] && $orow) {
680 echo "<tr bgcolor='#DDFFFF'>\n";
681 echo " <td colspan='2'>&nbsp;</td>";
682 echo " <td class='bold' colspan='2'>" . xlt("Grand Total") ."</td>\n";
683 echo " <td class='bold' style='text-align: right;'>". text($total_units) ."</td>\n";
684 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_chg)) ."</td>\n";
685 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_pmt)) ."</td>\n";
686 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_adj)) ."</td>\n";
687 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_bal)) . "</td>\n";
688 echo " </tr>\n";
690 </table>
691 <tr><td>&nbsp;</td></tr><br><br>
692 <?php if($GLOBALS['print_next_appointment_on_ledger'] == 1) {
693 $next_day = mktime(0,0,0,date('m'),date('d')+1,date('Y'));
694 # add one day to date so it will not get todays appointment
695 $current_date2 = date('Y-m-d', $next_day);
696 $events = fetchNextXAppts($current_date2,$form_pid);
697 $next_appoint_date = oeFormatShortDate($events[0]['pc_eventDate']);
698 $next_appoint_time = substr($events[0]['pc_startTime'],0,5);
699 if(strlen(umname) != 0 ) {
700 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['umname'] . ' ' . $events[0]['ulname'];
702 else
704 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['ulname'];
706 if(strlen($next_appoint_time) != 0) {
708 <tr>
709 <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>
710 </tr>
712 <?php
714 } // end ($GLOBALS['print_next_appointment_on_ledger'] == 1)
715 } // end (!$_REQUEST['form_csvexport'] && $orow)
716 echo "</div>\n";
718 if (! $_REQUEST['form_csvexport']) {
719 if ( $_REQUEST['form_refresh'] && $orow <= 0) {
720 echo "<span style='font-size:10pt;'>";
721 echo xlt('No matches found. Try search again.');
722 echo "</span>";
723 echo '<script>document.getElementById("report_results").style.display="none";</script>';
724 echo '<script>document.getElementById("controls").style.display="none";</script>';
727 if (!$_REQUEST['form_refresh'] && !$_REQUEST['form_csvexport']) { ?>
728 <div class='text'>
729 <?php echo xlt('Please input search criteria above, and click Submit to view results.' ); ?>
730 </div>
731 <?php } ?>
732 </form>
733 </body>
735 </html>
736 <?php
737 } // End not csv export