Revert "1. add edit event fails when save edited single provider recurring events...
[openemr.git] / interface / reports / pat_ledger.php
blob666812d11c9c16a722afd51aca13b747f72f6e41
1 <?php
2 /**
3 * This is a report to create a patient ledger of charges with payments
4 * applied.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author WMT
9 * @author Terry Hill <terry@lillysystems.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @copyright Copyright (c) 2015 Rich Genandt <rgenandt@gmail.com>
12 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 require_once('../globals.php');
17 require_once($GLOBALS['srcdir'].'/patient.inc');
18 require_once($GLOBALS['srcdir'].'/acl.inc');
19 require_once($GLOBALS['srcdir'].'/options.inc.php');
20 require_once($GLOBALS['srcdir'].'/appointments.inc.php');
22 use OpenEMR\Core\Header;
23 use OpenEMR\Services\FacilityService;
25 $facilityService = new FacilityService();
27 $enc_units = $total_units = 0;
28 $enc_chg = $total_chg = 0;
29 $enc_pmt = $total_pmt = 0;
30 $enc_adj = $total_adj = 0;
31 $enc_bal = $total_bal = 0;
32 $bgcolor = "#FFFFDD";
33 $orow = 0;
35 $pat_pid = $_GET['patient_id'];
36 $type_form = $_GET['form'];
38 if (! acl_check('acct', 'rep')) {
39 die(xlt("Unauthorized access."));
42 function GetAllUnapplied($pat = '', $from_dt = '', $to_dt = '')
44 $all = array();
45 if (!$pat) {
46 return($all);
49 $sql = "SELECT ar_session.*, ins.name, " .
50 "pat.lname, pat.fname, pat.mname, " .
51 "(SELECT SUM(ar_activity.pay_amount) FROM ar_activity WHERE " .
52 "ar_activity.session_id = ar_session.session_id) AS applied " .
53 "FROM ar_session " .
54 "LEFT JOIN insurance_companies AS ins on ar_session.payer_id = ins.id " .
55 "LEFT JOIN patient_data AS pat on ar_session.patient_id = pat.pid " .
56 "WHERE " .
57 "ar_session.created_time >= ? AND ar_session.created_time <= ? " .
58 "AND ar_session.patient_id=?";
59 $result = sqlStatement($sql, array($from_dt, $to_dt, $pat));
60 $iter = 0;
61 while ($row = sqlFetchArray($result)) {
62 $all[$iter] = $row;
63 $iter++;
66 return($all);
69 function User_Id_Look($thisField)
71 if (!$thisField) {
72 return '';
75 $ret = '';
76 $rlist= sqlStatement("SELECT lname, fname, mname FROM users WHERE id=?", array($thisField));
77 $rrow= sqlFetchArray($rlist);
78 if ($rrow) {
79 $ret = $rrow{'lname'}.', '.$rrow{'fname'}.' '.$rrow{'mname'};
82 return $ret;
85 function List_Look($thisData, $thisList)
87 if ($thisList == 'occurrence') {
88 if (!$thisData || $thisData == '') {
89 return xl('Unknown or N/A');
93 if ($thisData == '') {
94 return '';
97 $fres=sqlStatement("SELECT title FROM list_options WHERE list_id = ? ".
98 "AND option_id = ? AND activity = 1", array($thisList, $thisData));
99 if ($fres) {
100 $rret=sqlFetchArray($fres);
101 $dispValue= xl_list_label($rret{'title'});
102 if ($thisList == 'occurrence' && $dispValue == '') {
103 $dispValue = xl('Unknown or N/A');
105 } else {
106 $dispValue= xl('Not Found');
109 return $dispValue;
112 function GetAllCredits($enc = '', $pat = '')
114 $all = array();
115 if (!$enc || !$pat) {
116 return($all);
119 $sql = "SELECT activity.*, session.*, ins.name FROM ar_activity AS ".
120 "activity LEFT JOIN ar_session AS session USING (session_id) ".
121 "LEFT JOIN insurance_companies AS ins ON session.payer_id = ".
122 "ins.id WHERE encounter=? AND pid=? ".
123 "ORDER BY sequence_no";
124 $result = sqlStatement($sql, array($enc, $pat));
125 $iter = 0;
126 while ($row = sqlFetchArray($result)) {
127 $all[$iter] = $row;
128 $iter++;
131 return($all);
133 function PrintEncHeader($dt, $rsn, $dr)
135 global $bgcolor, $orow;
136 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
137 echo "<tr bgcolor='#FFFFFF'>";
138 if (strlen($rsn) > 50) {
139 $rsn = substr($rsn, 0, 50).'...';
142 echo "<td colspan='4'><span class='bold'>".xlt('Encounter Dt / Rsn'). ": </span><span class='detail'>".text(substr($dt, 0, 10))." / ".text($rsn)."</span></td>";
143 echo "<td colspan='5'><span class='bold'>" . xlt('Provider'). ": </span><span class='detail'>".text(User_Id_Look($dr))."</span></td>";
144 echo "</tr>\n";
145 $orow++;
147 function PrintEncFooter()
149 global $enc_units, $enc_chg, $enc_pmt, $enc_adj, $enc_bal;
150 echo "<tr bgcolor='#DDFFFF'>";
151 echo "<td colspan='3'>&nbsp;</td>";
152 echo "<td class='detail'>". xlt('Encounter Balance').":</td>";
153 echo "<td class='detail' style='text-align: center;'>".text($enc_units)."</td>";
154 echo "<td class='detail' style='text-align: center;'>".text(oeFormatMoney($enc_chg))."</td>";
155 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_pmt))."</td>";
156 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_adj))."</td>";
157 echo "<td class='detail' style='text-align: right;'>".text(oeFormatMoney($enc_bal))."</td>";
158 echo "</tr>\n";
160 function PrintCreditDetail($detail, $pat, $unassigned = false)
162 global $enc_pmt, $total_pmt, $enc_adj, $total_adj, $enc_bal, $total_bal;
163 global $bgcolor, $orow, $enc_units, $enc_chg;
164 foreach ($detail as $pmt) {
165 $uap_flag = false;
166 if ($unassigned) {
167 if (($pmt['pay_total'] - $pmt['applied']) == 0) {
168 if (!$GLOBALS['show_payment_history']) {
169 continue;
171 $uap_flag = true;
175 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
176 $print = "<tr bgcolor='" . attr($bgcolor) . "'>";
177 $print .= "<td class='detail'>&nbsp;</td>";
178 $method = List_Look($pmt['payment_method'], 'payment_method');
179 $desc = $pmt['description'];
180 $ref = $pmt['reference'];
181 if ($unassigned) {
182 $memo = List_Look($pmt['adjustment_code'], 'payment_adjustment_code');
183 } else {
184 $memo = $pmt['memo'];
187 $description = $method;
188 if ($ref) {
189 if ($description) {
190 $description .= ' - ';
193 $description .= $ref;
196 if ($desc) {
197 if ($description) {
198 $description .= ': ';
201 $description .= $desc;
204 if ($memo) {
205 if ($description) {
206 $description .= ' ';
209 $description .= '['.$memo.']';
211 if ($uap_flag === true) {
212 if ($description) {
213 $description .= ' ';
215 $description .= '{Pay History}';
217 $print .= "<td class='detail' colspan='2'>".
218 text($description)."&nbsp;</td>";
219 $payer = ($pmt['name'] == '') ? xl('Patient') : $pmt['name'];
220 if ($unassigned) {
221 $pmt_date = substr($pmt['post_to_date'], 0, 10);
222 } else {
223 $pmt_date = substr($pmt['post_time'], 0, 10);
226 $print .= "<td class='detail'>".
227 text($pmt_date)."&nbsp;/&nbsp;".text($payer)."</td>";
228 $type = List_Look($pmt['payment_type'], 'payment_type');
229 $print .= "<td class='detail'>".text($type)."&nbsp;</td>";
230 if ($unassigned) {
231 $pmt_amt = $pmt['pay_total'] - $pmt['applied'];
232 $uac_bal = $pmt_amt * -1;
233 $uac_appl = $pmt['applied'];
234 $uac_total = $pmt['pay_total'];
235 $pmt_amt = $pmt['pay_total'];
236 $total_pmt = $total_pmt - $uac_bal;
237 } else {
238 $uac_total = '';
239 $uac_bal = '';
240 $uac_appl = '';
241 $pmt_amt = $pmt['pay_amount'];
242 $adj_amt = $pmt['adj_amount'];
243 $enc_pmt = $enc_pmt + $pmt['pay_amount'];
244 $total_pmt = $total_pmt + $pmt['pay_amount'];
245 $enc_adj = $enc_adj + $pmt['adj_amount'];
246 $total_adj = $total_adj + $pmt['adj_amount'];
249 $print_pmt = '';
250 if ($pmt_amt != 0) {
251 $print_pmt = oeFormatMoney($pmt_amt);
254 $print_adj = '';
255 if ($adj_amt != 0) {
256 $print_adj = oeFormatMoney($adj_amt);
259 $print_appl = $uac_appl ? oeFormatMoney($uac_appl) : "";
260 $print_bal = $uac_bal ? oeFormatMoney($uac_bal) : "";
262 $print .= "<td class='detail' style='text-align: center;'>".text($print_appl)."&nbsp;</td>";
263 $print .= "<td class='detail' style='text-align: right;'>".text($print_pmt)."&nbsp;</td>";
264 $print .= "<td class='detail' style='text-align: right;'>".text($print_adj)."&nbsp;</td>";
265 $print .= "<td class='detail' style='text-align: right;'>".text($print_bal)."&nbsp;</td>";
266 $print .= "</tr>\n";
267 echo $print;
268 if ($pmt['follow_up_note'] != '') {
269 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
270 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
271 $print .= "<td class='detail' colspan='2'>&nbsp;</td>";
272 $print .= "<td colspan='7'>". xlt('Follow Up Note') .": ";
273 $print .= text($pmt['follow_up_note']);
274 $print .= "</td></tr>\n";
275 echo $print;
278 if ($unassigned) {
279 $total_bal = $total_bal + $uac_bal;
280 } else {
281 $enc_bal = $enc_bal - $pmt_amt - $adj_amt;
282 $total_bal = $total_bal - $pmt_amt - $adj_amt;
285 $orow++;
288 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
291 if (!isset($_REQUEST['form_facility'])) {
292 $_REQUEST['form_facility'] = '';
295 if (!isset($_REQUEST['form_provider'])) {
296 $_REQUEST['form_provider'] = '';
299 if ($type_form=='0') {
300 if (!isset($_REQUEST['form_patient'])) {
301 $_REQUEST['form_patient'] = '';
304 if (!isset($_REQUEST['form_pid'])) {
305 $_REQUEST['form_pid'] = '';
307 } else {
308 if (!isset($_REQUEST['form_patient'])) {
309 $_REQUEST['form_patient'] = $pat_pid;
312 if (!isset($_REQUEST['form_pid'])) {
313 $_REQUEST['form_pid'] = $pat_pid;
317 if (!isset($_REQUEST['form_csvexport'])) {
318 $_REQUEST['form_csvexport'] = '';
321 if (!isset($_REQUEST['form_refresh'])) {
322 $_REQUEST['form_refresh'] = '';
325 if (!isset($_REQUEST['$form_dob'])) {
326 $_REQUEST['$form_dob'] = '';
329 if (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'Y') {
330 $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1);
331 $last_year = mktime(0, 0, 0, date('m'), date('d'), date('Y')-$ledger_time);
332 } elseif (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'M') {
333 $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1);
334 $last_year = mktime(0, 0, 0, date('m')-$ledger_time, date('d'), date('Y'));
335 } elseif (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'D') {
336 $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1);
337 $last_year = mktime(0, 0, 0, date('m'), date('d')-$ledger_time, date('Y'));
340 $form_from_date = date('Y-m-d', $last_year);
341 if ($_REQUEST['form_from_date']) {
342 $form_from_date = DateToYYYYMMDD($_POST['form_from_date']);
345 $form_to_date = (!empty($_POST['form_to_date'])) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
346 $form_facility = $_REQUEST['form_facility'];
347 $form_provider = $_REQUEST['form_provider'];
348 $form_patient = $_REQUEST['form_patient'];
349 $form_pid = $_REQUEST['form_pid'];
350 $form_dob = $_REQUEST['form_dob'];
352 if ($_REQUEST['form_csvexport']) {
353 header("Pragma: public");
354 header("Expires: 0");
355 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
356 header("Content-Type: application/force-download");
357 header("Content-Disposition: attachment; filename=svc_financial_report_".attr($form_from_date)."--".attr($form_to_date).".csv");
358 header("Content-Description: File Transfer");
359 } else {
361 <html>
362 <head>
364 <title><?php echo xlt('Patient Ledger by Date'); ?></title>
366 <?php Header::setupHeader('datetime-picker'); ?>
368 <script type="text/javascript">
369 var pt_name;
370 var pt_id;
371 function checkSubmit() {
372 var pat = document.forms[0].elements['form_patient'].value;
373 if(!pat || pat == 0) {
374 alert('<?php echo xls('A Patient Must Be Selected to Generate This Report') ?>');
375 return false;
377 document.forms[0].elements['form_refresh'].value = true;
378 document.forms[0].elements['form_csvexport'].value = '';
379 document.forms[0].submit();
381 function setpatient(pid, lname, fname, dob) {
382 document.forms[0].elements['form_patient'].value = lname + ', ' + fname;
383 document.forms[0].elements['form_pid'].value = pid;
384 document.forms[0].elements['form_dob'].value = dob;
386 function sel_patient() {
387 dlgopen('../main/calendar/find_patient_popup.php?pflag=0', '_blank', 500, 400);
389 </script>
391 <style type="text/css">
392 /* specifically include & exclude from printing */
393 @media print {
394 #report_parameters {
395 visibility: hidden;
396 display: none;
398 #report_parameters_daterange {
399 visibility: visible;
400 display: inline;
402 #report_results {
403 margin-top: 30px;
405 #report_header {
406 visibility: visible;
407 display: inline;
409 #title {
410 visibility: hidden;
411 display: none;
414 /* specifically exclude some from the screen */
415 @media screen {
416 #report_parameters_daterange {
417 visibility: hidden;
418 display: none;
420 #report_header {
421 visibility: hidden;
422 display: none;
424 #title {
425 visibility: visible;
426 display: inline;
429 </style>
431 <script language="JavaScript">
432 $(document).ready(function() {
433 var win = top.printLogSetup ? top : opener.top;
434 win.printLogSetup(document.getElementById('printbutton'));
436 $('.datepicker').datetimepicker({
437 <?php $datetimepicker_timepicker = false; ?>
438 <?php $datetimepicker_showseconds = false; ?>
439 <?php $datetimepicker_formatInput = true; ?>
440 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
441 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
444 </script>
445 </head>
446 <body class="body_top">
447 <?php if ($type_form == '0') { ?>
448 <span class='title' id='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Patient Ledger by Date'); ?></span>
449 <?php } else { ?>
450 <span class='title' id='title'><?php echo xlt('Patient Ledger'); ?></span>
451 <?php } ?>
452 <form method='post' action='pat_ledger.php?form=<?php echo attr($type_form);?>&patient_id=<?php echo attr($form_pid);?>' id='theform' onsubmit='return top.restoreSession()'>
453 <div id="report_parameters">
454 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
455 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
456 <table>
457 <tr>
458 <?php if ($type_form == '1') { ?>
459 <td width='35%'>
460 <?php } else { ?>
461 <td width='70%'>
462 <?php } ?>
463 <div style='float:left'>
464 <table class='text'>
465 <tr>
466 <?php if ($type_form == '0') { ?>
467 <td class='control-label'>
468 <?php echo xlt('Facility'); ?>:
469 </td>
470 <td>
471 <?php dropdown_facility($form_facility, 'form_facility', true); ?>
472 </td>
473 <td class='control-label'><?php echo xlt('Provider'); ?>:</td>
474 <td><?php
475 $query = "SELECT id, lname, fname FROM users WHERE ".
476 "authorized=1 AND active!=0 ORDER BY lname, fname";
477 $ures = sqlStatement($query);
478 echo " <select name='form_provider' class='form-control'>\n";
479 echo " <option value=''>-- " . xlt('All') . " --\n";
480 while ($urow = sqlFetchArray($ures)) {
481 $provid = $urow['id'];
482 echo " <option value='" . attr($provid) ."'";
483 if ($provid == $_REQUEST['form_provider']) {
484 echo " selected";
487 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
490 echo " </select>\n";
491 ?></td>
492 </tr><tr>
493 <?php } ?>
494 <td class='control-label'>
495 <?php echo xlt('From'); ?>:&nbsp;&nbsp;&nbsp;&nbsp;
496 </td>
497 <td>
498 <input type='text' class='datepicker form-control' name='form_from_date' id="form_from_date" size='10' value='<?php echo attr(oeFormatShortDate($form_from_date)); ?>'>
499 </td>
500 <td class='control-label' class='control-label'>
501 <?php echo xlt('To'); ?>:
502 </td>
503 <td>
504 <input type='text' class='datepicker form-control' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr(oeFormatShortDate($form_to_date)); ?>'>
505 </td>
506 <?php if ($type_form == '0') { ?>
507 <td><span class='control-label'><?php echo xlt('Patient'); ?>:&nbsp;&nbsp;</span></td>
508 <td>
509 <input type='text' size='20' name='form_patient' class='form-control' style='width:100%;cursor:pointer;cursor:hand' id='form_patient' value='<?php echo ($form_patient) ? attr($form_patient) : xla('Click To Select'); ?>' onclick='sel_patient()' title='<?php echo xla('Click to select patient'); ?>' />
510 <?php } else { ?>
511 <input type='hidden' name='form_patient' value='<?php echo attr($form_patient); ?>' />
512 <?php } ?>
513 <input type='hidden' name='form_pid' value='<?php echo attr($form_pid); ?>' />
514 <input type='hidden' name='form_dob' value='<?php echo attr($form_dob); ?>' />
516 </td>
517 </tr>
518 </table>
519 </div>
520 </td>
521 <td align='left' valign='middle' height="100%">
522 <table style='border-left:1px solid; width:100%; height:100%' >
523 <tr>
524 <td>
525 <div class="text-center">
526 <div class="btn-group" role="group">
527 <a href='#' class='btn btn-default btn-save' onclick="checkSubmit();" >
528 <?php echo xlt('Submit'); ?>
529 </a>
530 <?php if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) { ?>
531 <a href='#' class='btn btn-default btn-print' id='printbutton'>
532 <?php echo xlt('Print Ledger'); ?>
533 </a>
534 <?php if ($type_form == '1') { ?>
535 <a href="../patient_file/summary/demographics.php" class="btn btn-default btn-transmit" onclick="top.restoreSession()">
536 <?php echo xlt('Back To Patient');?>
537 </a>
538 <?php } ?>
539 <?php } ?>
540 </div>
541 </div>
542 </td>
543 </tr>
544 </table>
545 </td>
546 </tr>
547 </table>
548 </div> <!-- end of parameters -->
550 <?php
551 } // end not export
552 $from_date = $form_from_date . ' 00:00:00';
553 $to_date = $form_to_date . ' 23:59:59';
554 if ($_REQUEST['form_refresh'] || $_REQUEST['form_csvexport']) {
555 $rows = array();
556 $sqlBindArray = array();
557 $query = "select b.code_type, b.code, b.code_text, b.pid, b.provider_id, ".
558 "b.billed, b.payer_id, b.units, b.fee, b.bill_date, b.id, ".
559 "ins.name, ".
560 "fe.encounter, fe.date, fe.reason, fe.provider_id ".
561 "FROM form_encounter AS fe ".
562 "LEFT JOIN billing AS b ON b.pid=fe.pid AND b.encounter=fe.encounter ".
563 "LEFT JOIN insurance_companies AS ins ON b.payer_id = ins.id ".
564 "LEFT OUTER JOIN code_types AS c ON c.ct_key = b.code_type ".
565 "WHERE fe.date >= ? AND fe.date <= ? AND fe.pid = ? ";
566 array_push($sqlBindArray, $from_date, $to_date, $form_pid);
567 if ($form_facility) {
568 $query .= "AND fe.facility_id = ? ";
569 array_push($sqlBindArray, $form_facility);
572 if ($form_provider) {
573 $query .= "AND b.provider_id = ? ";
574 array_push($sqlBindArray, $form_provider);
577 $query .= "AND c.ct_proc = '1' ";
578 $query .= "AND activity > 0 ORDER BY fe.date, fe.id ";
579 $res = sqlStatement($query, $sqlBindArray);
581 if ($_REQUEST['form_csvexport']) {
582 // CSV headers:
583 if (true) {
584 echo '"Code/Enc Dt",';
585 echo '"Description",';
586 echo '"Billed/Who",';
587 echo '"Type/Units",';
588 echo '"Chg/Pmt Amount",'."\n";
590 } else {
591 if (!$form_facility) {
592 $form_facility = '3';
595 $facility = $facilityService->getById($form_facility);
596 $patient = sqlQuery("SELECT * from patient_data WHERE pid=?", array($form_patient));
597 $pat_dob = $patient['DOB'];
598 $pat_name = $patient['fname']. ' ' . $patient['lname'];
600 <div id="report_header">
601 <table width="98%" border="0" cellspacing="0" cellpadding="0">
602 <tr>
603 <td class="title" ><?php echo text($facility{'name'}); ?></td>
604 </tr>
605 <tr>
606 <td class="title" ><?php echo text($facility{'street'}); ?></td>
607 </tr>
608 <tr>
609 <td class="title" ><?php echo text($facility{'city'}).", ".text($facility{'state'})." ".text($facility{'postal_code'}); ?></td>
610 </tr>
611 <tr>
612 <td class="title" ><?php echo xlt('Phone').': ' .text($facility{'phone'}); ?></td>
613 </tr>
614 <tr>
615 <td class="title" ><?php echo xlt('Tax Id').': ' .text($facility{'federal_ein'}); ?></td>
616 </tr>
617 <tr><td>&nbsp;</td></tr>
618 <tr>
619 <td class="title" ><?php echo xlt('Patient Ledger'); ?></td>
620 </tr>
621 <tr>
622 <?php
623 $title = xl('All Providers');
624 if ($form_provider) {
625 $title = xl('For Provider') . ': '.User_Id_Look($form_provider);
628 <td class="title" ><?php echo text($title); ?></td>
629 </tr>
630 <tr>
631 <?php
632 $title = xl('For Dates') . ': ' . oeFormatShortDate($form_from_date) . ' - ' . oeFormatShortDate($form_to_date);
634 <td class="title" ><?php echo text($title); ?></td>
635 </tr>
636 </table>
637 <br/>
638 <table width="100%" border="0" cellspacing="0" cellpadding="0">
639 <tr>
640 <td class='bold' ><?php echo xlt('Date')?>:
641 <?php echo text(date('Y-m-d')); ?></td>
642 <td class='bold' ><?php echo xlt('Patient')?>:
643 <?php if ($type_form == '1') { ?>
644 <?php echo text($pat_name); ?></td>
645 <?php } else { ?>
646 <?php echo text($form_patient); ?></td>
647 <?php } ?>
648 <td class='bold' ><?php echo xlt('DOB')?>:
649 <?php if ($type_form == '1') { ?>
650 <?php echo text($pat_dob);?></td>
651 <?php } else { ?>
652 <?php echo text($form_dob); ?></td>
653 <?php } ?>
654 <td class='bold' > <?php echo xlt('ID')?>:
655 <?php echo text($form_pid);?></td>
656 </tr>
657 </table>
658 </div>
659 <div id="report_results">
660 <table >
661 <tr>
662 <td class='bold' ><?php echo xlt('Code'); ?></td>
663 <td colspan="2" class='bold' ><?php echo xlt('Description'); ?></td>
664 <td class='bold' ><?php echo xlt('Billed Date'); ?> / <?php echo xlt('Payor'); ?></td>
665 <td class='bold' ><?php echo xlt('Type'); ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
666 <?php echo xlt('Units'); ?></td>
667 <td class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Charge'); ?></td>
668 <td align='right' class='bold' >&nbsp;&nbsp;<?php echo xlt('Payment'); ?></td>
669 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('Adjustment'); ?></td>
670 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('Balance'); ?></td>
671 </tr>
672 <tr>
673 <td>&nbsp;&nbsp;&nbsp;</td>
674 <td colspan="2" >&nbsp;&nbsp;&nbsp;</td>
675 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
676 <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
677 &nbsp;&nbsp;&nbsp;</td>
678 <td class='bold' >&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Appl'); ?></td>
679 <td align='right' class='bold' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo xlt('UAC Tot'); ?></td>
680 <td>&nbsp;</td>
681 <td>&nbsp;</td>
682 </tr>
683 <?php
686 $orow = 0;
687 $prev_encounter_id = -1;
688 $hdr_printed = false;
689 $prev_row = array();
690 while ($erow = sqlFetchArray($res)) {
691 $print = '';
692 $csv = '';
693 if ($erow['encounter'] != $prev_encounter_id) {
694 if ($prev_encounter_id != -1) {
695 $credits = GetAllCredits($prev_encounter_id, $form_pid);
696 if (count($credits) > 0) {
697 if (!$hdr_printed) {
698 PrintEncHeader(
699 $prev_row{'date'},
700 $prev_row{'reason'},
701 $prev_row{'provider_id'}
705 PrintCreditDetail($credits, $form_pid);
708 if ($hdr_printed) {
709 PrintEncFooter();
712 $hdr_printed = false;
715 $enc_units = $enc_chg = $enc_pmt = $enc_adj = $enc_bal = 0;
718 if ($erow{'id'}) {
719 // Now print an encounter heading line -
720 if (!$hdr_printed) {
721 PrintEncHeader(
722 $erow{'date'},
723 $erow{'reason'},
724 $erow{'provider_id'}
726 $hdr_printed = true;
729 $code_desc = $erow['code_text'];
730 if (strlen($code_desc) > 50) {
731 $code_desc = substr($code_desc, 0, 50).'...';
734 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
735 $print = "<tr bgcolor='". attr($bgcolor) ."'>";
736 $print .= "<td class='detail'>".text($erow['code'])."</td>";
737 $print .= "<td class='detail' colspan='2'>".text($code_desc)."</td>";
738 $who = ($erow['name'] == '') ? xl('Self') : $erow['name'];
739 $bill = substr($erow['bill_date'], 0, 10);
740 if ($bill == '') {
741 $bill = 'unbilled';
744 $print .= "<td class='detail'>".text($bill)."&nbsp;/&nbsp;".text($who)."</td>";
745 $print .= "<td class='detail' style='text-align: center;'>". text($erow['units'])."</td>";
746 $print .= "<td class='detail' style='text-align: center;'>". text(oeFormatMoney($erow['fee']))."</td>";
747 $print .= "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
748 $print .= "</tr>\n";
750 $total_units += $erow['units'];
751 $total_chg += $erow['fee'];
752 $total_bal += $erow['fee'];
753 $enc_units += $erow['units'];
754 $enc_chg += $erow['fee'];
755 $enc_bal += $erow['fee'];
756 $orow++;
758 if ($_REQUEST['form_csvexport']) {
759 echo $csv;
760 } else {
761 echo $print;
765 $prev_encounter_id = $erow{'encounter'};
766 $prev_row = $erow;
769 if ($prev_encounter_id != -1) {
770 $credits = GetAllCredits($prev_encounter_id, $form_pid);
771 if (count($credits) > 0) {
772 if (!$hdr_printed) {
773 PrintEncHeader(
774 $prev_row{'date'},
775 $prev_row{'reason'},
776 $prev_row{'provider_id'}
780 PrintCreditDetail($credits, $form_pid);
783 if ($hdr_printed) {
784 PrintEncFooter();
788 // This is the end of the encounter/charge loop -
789 $uac = GetAllUnapplied($form_pid, $from_date, $to_date);
790 if (count($uac) > 0) {
791 if ($orow) {
792 $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD");
793 echo "<tr bgcolor='#FFFFFF'><td colspan='9'>&nbsp;</td></tr>\n";
796 PrintCreditDetail($uac, $form_pid, true);
799 if (!$_REQUEST['form_csvexport'] && $orow) {
800 echo "<tr bgcolor='#DDFFFF'>\n";
801 echo " <td colspan='2'>&nbsp;</td>";
802 echo " <td class='bold' colspan='2'>" . xlt("Grand Total") ."</td>\n";
803 echo " <td class='bold' style='text-align: center;'>". text($total_units) ."</td>\n";
804 echo " <td class='bold' style='text-align: center;'>". text(oeFormatMoney($total_chg)) ."</td>\n";
805 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_pmt)) ."</td>\n";
806 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_adj)) ."</td>\n";
807 echo " <td class='bold' style='text-align: right;'>". text(oeFormatMoney($total_bal)) . "</td>\n";
808 echo " </tr>\n";
810 </table>
811 <tr><td>&nbsp;</td></tr><br><br>
812 <?php if ($GLOBALS['print_next_appointment_on_ledger'] == 1) {
813 $next_day = mktime(0, 0, 0, date('m'), date('d')+1, date('Y'));
814 # add one day to date so it will not get todays appointment
815 $current_date2 = date('Y-m-d', $next_day);
816 $events = fetchNextXAppts($current_date2, $form_pid);
817 $next_appoint_date = oeFormatShortDate($events[0]['pc_eventDate']);
818 $next_appoint_time = substr($events[0]['pc_startTime'], 0, 5);
819 if (strlen($events[0]['umname']) != 0) {
820 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['umname'] . ' ' . $events[0]['ulname'];
821 } else {
822 $next_appoint_provider = $events[0]['ufname'] . ' ' . $events[0]['ulname'];
825 if (strlen($next_appoint_time) != 0) { ?>
826 <tr>
827 <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>
828 </tr>
829 <?php
831 } // end ($GLOBALS['print_next_appointment_on_ledger'] == 1)
832 } // end (!$_REQUEST['form_csvexport'] && $orow)
833 echo "</div>\n";
836 if (! $_REQUEST['form_csvexport']) {
837 if ($_REQUEST['form_refresh'] && $orow <= 0) {
838 echo "<span style='font-size:10pt;'>";
839 echo xlt('No matches found. Try search again.');
840 echo "</span>";
841 echo '<script>document.getElementById("report_results").style.display="none";</script>';
842 echo '<script>document.getElementById("controls").style.display="none";</script>';
845 if (!$_REQUEST['form_refresh'] && !$_REQUEST['form_csvexport']) { ?>
846 <div class='text'>
847 <?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
848 </div><?php
849 } ?>
850 </form>
851 </body>
853 </html>
854 <?php
855 } // End not csv export