* @author Brady Miller * @copyright Copyright (c) 2015 Rich Genandt * @copyright Copyright (c) 2019 Brady Miller * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3 */ require_once("./../verify_session.php"); $ignoreAuth = true; global $ignoreAuth; require_once('../../interface/globals.php'); require_once($GLOBALS['srcdir'].'/patient.inc'); require_once($GLOBALS['srcdir'].'/acl.inc'); require_once($GLOBALS['srcdir'].'/options.inc.php'); require_once($GLOBALS['srcdir'].'/appointments.inc.php'); use OpenEMR\Core\Header; $enc_units = $total_units = 0; $enc_chg = $total_chg = 0; $enc_pmt = $total_pmt = 0; $enc_adj = $total_adj = 0; $enc_bal = $total_bal = 0; $bgcolor = "#FFFFDD"; $orow = 0; //if (! acl_check('acct', 'rep')) die(xlt("Unauthorized access.")); function GetAllUnapplied($pat = '', $from_dt = '', $to_dt = '') { $all = array(); if (!$pat) { return($all); } $sql = "SELECT ar_session.*, ins.name, " . "pat.lname, pat.fname, pat.mname, " . "(SELECT SUM(ar_activity.pay_amount) FROM ar_activity WHERE " . "ar_activity.session_id = ar_session.session_id) AS applied " . "FROM ar_session " . "LEFT JOIN insurance_companies AS ins on ar_session.payer_id = ins.id " . "LEFT JOIN patient_data AS pat on ar_session.patient_id = pat.pid " . "WHERE " . "ar_session.created_time >= ? AND ar_session.created_time <= ? " . "AND ar_session.patient_id=?"; $result = sqlStatement($sql, array($from_dt, $to_dt, $pat)); $iter = 0; while ($row = sqlFetchArray($result)) { $all[$iter] = $row; $iter++; } return($all); } function User_Id_Look($thisField) { if (!$thisField) { return ''; } $ret = ''; $rlist= sqlStatement("SELECT lname, fname, mname FROM users WHERE id=?", array($thisField)); $rrow= sqlFetchArray($rlist); if ($rrow) { $ret = $rrow{'lname'}.', '.$rrow{'fname'}.' '.$rrow{'mname'}; } return $ret; } function List_Look($thisData, $thisList) { if ($thisList == 'occurrence') { if (!$thisData || $thisData == '') { return xl('Unknown or N/A'); } } if ($thisData == '') { return ''; } $fres=sqlStatement("SELECT title FROM list_options WHERE list_id=? ". "AND option_id=?", array($thisList, $thisData)); if ($fres) { $rret=sqlFetchArray($fres); $dispValue= xl_list_label($rret{'title'}); if ($thisList == 'occurrence' && $dispValue == '') { $dispValue = xl('Unknown or N/A'); } } else { $dispValue= xl('Not Found'); } return $dispValue; } function GetAllCredits($enc = '', $pat = '') { $all = array(); if (!$enc || !$pat) { return($all); } $sql = "SELECT activity.*, session.*, ins.name FROM ar_activity AS ". "activity LEFT JOIN ar_session AS session USING (session_id) ". "LEFT JOIN insurance_companies AS ins ON session.payer_id = ". "ins.id WHERE encounter=? AND pid=? ". "ORDER BY sequence_no"; $result = sqlStatement($sql, array($enc, $pat)); $iter = 0; while ($row = sqlFetchArray($result)) { $all[$iter] = $row; $iter++; } return($all); } function PrintEncHeader($dt, $rsn, $dr) { global $bgcolor, $orow; $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD"); echo ""; if (strlen($rsn) > 50) { $rsn = substr($rsn, 0, 50).'...'; } echo "".xlt('Encounter Dt / Rsn'). ": ".text(substr($dt, 0, 10))." / ".text($rsn).""; echo "" . xlt('Provider'). ": ".text(User_Id_Look($dr)).""; echo "\n"; $orow++; } function PrintEncFooter() { global $enc_units, $enc_chg, $enc_pmt, $enc_adj, $enc_bal; echo ""; echo " "; echo "". xlt('Encounter Balance').":"; echo "".text($enc_units).""; echo "".text(oeFormatMoney($enc_chg)).""; echo "".text(oeFormatMoney($enc_pmt)).""; echo "".text(oeFormatMoney($enc_adj)).""; echo "".text(oeFormatMoney($enc_bal)).""; echo "\n"; } function PrintCreditDetail($detail, $pat, $unassigned = false) { global $enc_pmt, $total_pmt, $enc_adj, $total_adj, $enc_bal, $total_bal; global $bgcolor, $orow, $enc_units, $enc_chg; foreach ($detail as $pmt) { if ($unassigned) { if (($pmt['pay_total'] - $pmt['applied']) == 0) { continue; } } $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD"); $print = ""; $print .= " "; $method = List_Look($pmt['payment_method'], 'payment_method'); $desc = $pmt['description']; $ref = $pmt['reference']; if ($unassigned) { $memo = List_Look($pmt['adjustment_code'], 'payment_adjustment_code'); } else { $memo = $pmt['memo']; } $description = $method; if ($ref) { if ($description) { $description .= ' - '; } $description .= $ref; } if ($desc) { if ($description) { $description .= ': '; } $description .= $desc; } if ($memo) { if ($description) { $description .= ' '; } $description .= '['.$memo.']'; } $print .= "". text($description)." "; $payer = ($pmt['name'] == '') ? xl('Patient') : $pmt['name']; if ($unassigned) { $pmt_date = substr($pmt['post_to_date'], 0, 10); } else { $pmt_date = substr($pmt['post_time'], 0, 10); } $print .= "". text($pmt_date)." / ".text($payer).""; $type = List_Look($pmt['payment_type'], 'payment_type'); $print .= "".text($type)." "; if ($unassigned) { $pmt_amt = $pmt['pay_total'] - $pmt['applied']; $uac_bal = oeFormatMoney($pmt_amt * -1); $uac_appl = oeFormatMoney($pmt['applied']); $uac_total = oeFormatMoney($pmt['pay_total']); $pmt_amt = $pmt['pay_total']; $total_pmt = $total_pmt - $uac_bal; } else { $uac_total = ''; $uac_bal = ''; $uac_appl = ''; $pmt_amt = $pmt['pay_amount']; $adj_amt = $pmt['adj_amount']; $enc_pmt = $enc_pmt + $pmt['pay_amount']; $total_pmt = $total_pmt + $pmt['pay_amount']; $enc_adj = $enc_adj + $pmt['adj_amount']; $total_adj = $total_adj + $pmt['adj_amount']; } $print_pmt = ''; if ($pmt_amt != 0) { $print_pmt = oeFormatMoney($pmt_amt); } $print_adj = ''; if ($adj_amt != 0) { $print_adj = oeFormatMoney($adj_amt); } $print .= "".text($uac_appl)." "; $print .= "".text($print_pmt)." "; $print .= "".text($print_adj)." "; $print .= "".text($uac_bal)." "; $print .= "\n"; echo $print; if ($pmt['follow_up_note'] != '') { $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD"); $print = ""; $print .= " "; $print .= "". xlt('Follow Up Note') .": "; $print .= text($pmt['follow_up_note']); $print .= "\n"; echo $print; } if ($unassigned) { $total_bal = $total_bal + $uac_bal; } else { $enc_bal = $enc_bal - $pmt_amt - $adj_amt; $total_bal = $total_bal - $pmt_amt - $adj_amt; } $orow++; } $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD"); } if (!isset($_REQUEST['form_from_date'])) { $_REQUEST['form_from_date'] = ''; } if (!isset($_REQUEST['form_to_date'])) { $_REQUEST['form_to_date'] = ''; } if (!isset($_REQUEST['form_refresh'])) { $_REQUEST['form_refresh'] = ''; } if (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'Y') { $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1); $last_year = mktime(0, 0, 0, date('m'), date('d'), date('Y')-$ledger_time); } elseif (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'M') { $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1); $last_year = mktime(0, 0, 0, date('m')-$ledger_time, date('d'), date('Y')); } elseif (substr($GLOBALS['ledger_begin_date'], 0, 1) == 'D') { $ledger_time = substr($GLOBALS['ledger_begin_date'], 1, 1); $last_year = mktime(0, 0, 0, date('m'), date('d')-$ledger_time, date('Y')); } $form_from_date = date('Y-m-d', $last_year); if ($_REQUEST['form_from_date']) { $form_from_date = fixDate($_REQUEST['form_from_date'], $last_year); } $form_to_date = fixDate($_REQUEST['form_to_date'], date('Y-m-d')); ?> <?php echo xlt('Patient Ledger by Date') ?>

:     :
= ? AND fe.date <= ? AND fe.pid = ? "; array_push($sqlBindArray, $from_date, $to_date, $pid); $query .= "AND c.ct_proc = '1' "; $query .= "AND activity > 0 ORDER BY fe.date, fe.id "; $res = sqlStatement($query, $sqlBindArray); $patient = sqlQuery("SELECT * from patient_data WHERE pid=?", array($pid)); $pat_dob = $patient['DOB']; $pat_name = $patient['fname']. ' ' . $patient['lname']; ?>

: : : :
0) { if (!$hdr_printed) { PrintEncHeader( $prev_row{'date'}, $prev_row{'reason'}, $prev_row{'provider_id'} ); } PrintCreditDetail($credits, $pid); } if ($hdr_printed) { PrintEncFooter(); } $hdr_printed = false; } $enc_units = $enc_chg = $enc_pmt = $enc_adj = $enc_bal = 0; } if ($erow{'id'}) { // Now print an encounter heading line - if (!$hdr_printed) { PrintEncHeader( $erow{'date'}, $erow{'reason'}, $erow{'provider_id'} ); $hdr_printed = true; } $code_desc = $erow['code_text']; if (strlen($code_desc) > 50) { $code_desc = substr($code_desc, 0, 50).'...'; } $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD"); $print = ""; $print .= ""; $print .= ""; $who = ($erow['name'] == '') ? xl('Self') : $erow['name']; $bill = substr($erow['bill_date'], 0, 10); if ($bill == '') { $bill = 'unbilled'; } $print .= ""; $print .= ""; $print .= ""; $print .= ""; $print .= "\n"; $total_units += $erow['units']; $total_chg += $erow['fee']; $total_bal += $erow['fee']; $enc_units += $erow['units']; $enc_chg += $erow['fee']; $enc_bal += $erow['fee']; $orow++; echo $print; } $prev_encounter_id = $erow{'encounter'}; $prev_row = $erow; } if ($prev_encounter_id != -1) { $credits = GetAllCredits($prev_encounter_id, $pid); if (count($credits) > 0) { if (!$hdr_printed) { PrintEncHeader( $prev_row{'date'}, $prev_row{'reason'}, $prev_row{'provider_id'} ); } PrintCreditDetail($credits, $pid); } if ($hdr_printed) { PrintEncFooter(); } } // This is the end of the encounter/charge loop - $uac = GetAllUnapplied($pid, $from_date, $to_date); if (count($uac) > 0) { if ($orow) { $bgcolor = (($bgcolor == "#FFFFDD") ? "#FFDDDD" : "#FFFFDD"); echo "\n"; } PrintCreditDetail($uac, $pid, true); } if ($orow) { echo "\n"; echo " "; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; ?>
/                           
                                         
".text($erow['code'])."".text($code_desc)."".text($bill)." / ".text($who)."". text($erow['units'])."". text(oeFormatMoney($erow['fee']))."   
 
 " . xlt("Grand Total") ."". text($total_units) ."". text(oeFormatMoney($total_chg)) ."". text(oeFormatMoney($total_pmt)) ."". text(oeFormatMoney($total_adj)) ."". text(oeFormatMoney($total_bal)) . "
 

\n"; } if ($_REQUEST['form_refresh'] && $orow <= 0) { echo ""; echo xlt('No matches found. Try search again.'); echo ""; echo ''; echo ''; } if (!$_REQUEST['form_refresh']) { ?>