4 * This class has various billing functions for posting charges and payments.
7 * @author Rod Roark <rod@sunsetsystems.com>
8 * @author Stephen Waite <stephen.waite@cmsvt.com>
9 * @copyright Copyright (c) 2005-2009 Rod Roark <rod@sunsetsystems.com>
10 * @copyright Copyright (c) 2018 Stephen Waite <stephen.waite@cmsvt.com>
11 * @link https://www.open-emr.org
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 namespace OpenEMR\Billing
;
17 require_once(dirname(__FILE__
) . "/../../library/patient.inc.php");
19 use OpenEMR\Billing\BillingUtilities
;
23 // Try to figure out our invoice number (pid.encounter) from the
24 // claim ID and other stuff in the ERA. This should be straightforward
25 // except that some payers mangle the claim ID that we give them.
27 public static function slInvoiceNumber(&$out)
29 $invnumber = $out['our_claim_id'];
30 $atmp = preg_split('/[ -]/', $invnumber);
31 $acount = count($atmp);
37 $encounter = $atmp[1];
38 } elseif ($acount == 3) {
40 $brow = sqlQuery("SELECT encounter FROM billing WHERE " .
41 "pid = '$pid' AND encounter = ? AND activity = 1", array($atmp[1]));
43 $encounter = $brow['encounter'];
44 } elseif ($acount == 1) {
45 $pres = sqlStatement("SELECT pid FROM patient_data WHERE " .
48 "ORDER BY pid DESC", array($out['patient_lname'], $out['patient_fname']));
49 while ($prow = sqlFetchArray($pres)) {
50 if (strpos($invnumber, $prow['pid']) === 0) {
52 $encounter = substr($invnumber, strlen($pid));
58 if ($pid && $encounter) {
59 $invnumber = "$pid.$encounter";
62 return array($pid, $encounter, $invnumber);
65 // This gets a posting session ID. If the payer ID is not 0 and a matching
66 // session already exists, then its ID is returned. Otherwise a new session
69 public static function arGetSession($payer_id, $reference, $check_date, $deposit_date = '', $pay_total = 0)
71 if (empty($deposit_date)) {
72 $deposit_date = $check_date;
76 $row = sqlQuery("SELECT session_id FROM ar_session WHERE " .
77 "payer_id = ? AND reference = ? AND " .
78 "check_date = ? AND deposit_date = ? " .
79 "ORDER BY session_id DESC LIMIT 1", array($payer_id, $reference, $check_date, $deposit_date));
80 if (!empty($row['session_id'])) {
81 return $row['session_id'];
85 return sqlInsert("INSERT INTO ar_session ( " .
86 "payer_id, user_id, reference, check_date, deposit_date, pay_total " .
87 ") VALUES ( ?, ?, ?, ?, ?, ? )", array($payer_id, $_SESSION['authUserID'], $reference, $check_date, $deposit_date, $pay_total));
90 //writing the check details to Session Table on ERA proxcessing
91 public static function arPostSession($payer_id, $check_number, $check_date, $pay_total, $post_to_date, $deposit_date, $debug)
93 $query = "INSERT INTO ar_session( " .
94 "payer_id,user_id,closed,reference,check_date,pay_total,post_to_date,deposit_date,patient_id,payment_type,adjustment_code,payment_method " .
95 ") VALUES (?, ?, 0, ?, ?, ?, ? ,?, 0, 'insurance', 'insurance_payment', 'electronic')";
97 echo text($query) . "<br />\n";
99 $sessionId = sqlInsert($query, array($payer_id, $_SESSION['authUserID'], 'ePay - ' . $check_number, $check_date, $pay_total, $post_to_date, $deposit_date));
104 // Post a payment, new style.
106 public static function arPostPayment(
121 $tmp = strpos($code, ':');
123 $codeonly = substr($code, 0, $tmp);
124 $modifier = substr($code, $tmp +
1);
128 $time = date('Y-m-d H:i:s');
132 $sequence_no = sqlQuery(
133 "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?",
134 array($patient_id, $encounter_id)
136 $query = "INSERT INTO ar_activity ( " .
137 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_time, post_date, post_user, " .
138 "session_id, memo, pay_amount " .
139 ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
145 $sequence_no['increment'],
152 $_SESSION['authUserID'],
162 // Post a charge. This is called only from sl_eob_process.php where
163 // automated remittance processing can create a new service item.
164 // Here we add it as an unauthorized item to the billing table.
166 public static function arPostCharge($patient_id, $encounter_id, $session_id, $amount, $units, $thisdate, $code, $description, $debug, $codetype = '')
168 /*****************************************************************
169 * // Select an existing billing item as a template.
170 * $row= sqlQuery("SELECT * FROM billing WHERE " .
171 * "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
172 * "code_type = 'CPT4' AND activity = 1 " .
173 * "ORDER BY id DESC LIMIT 1");
174 * $this_authorized = 0;
175 * $this_provider = 0;
176 * if (!empty($row)) {
177 * $this_authorized = $row['authorized'];
178 * $this_provider = $row['provider_id'];
180 *****************************************************************/
182 if (empty($codetype)) {
183 // default to CPT4 if empty, which is consistent with previous functionality.
189 $tmp = strpos($code, ':');
191 $codeonly = substr($code, 0, $tmp);
192 $modifier = substr($code, $tmp +
1);
195 BillingUtilities
::addBilling(
211 // Post an adjustment, new style.
213 public static function arPostAdjustment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $reason, $debug, $time = '', $codetype = '')
217 $tmp = strpos($code, ':');
219 $codeonly = substr($code, 0, $tmp);
220 $modifier = substr($code, $tmp +
1);
224 $time = date('Y-m-d H:i:s');
228 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
229 $query = "INSERT INTO ar_activity ( " .
230 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_user, post_time, " .
231 "session_id, memo, adj_amount " .
232 ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
233 sqlStatement($query, array($patient_id, $encounter_id, $sequence_no['increment'], $codetype, $codeonly, $modifier, $payer_type, $_SESSION['authUserID'], $time, $session_id, $reason, $amount));
238 public static function arGetPayerID($patient_id, $date_of_service, $payer_type)
240 if ($payer_type < 1 ||
$payer_type > 3) {
244 $tmp = array(1 => 'primary', 2 => 'secondary', 3 => 'tertiary');
245 $value = $tmp[$payer_type];
246 $query = "SELECT provider FROM insurance_data WHERE " .
247 "pid = ? AND type = ? AND (date <= ? OR date IS NULL) " .
248 "ORDER BY date DESC LIMIT 1";
249 $nprow = sqlQuery($query, array($patient_id, $value, $date_of_service));
254 return $nprow['provider'];
257 // Make this invoice re-billable, new style.
259 public static function arSetupSecondary($patient_id, $encounter_id, $debug, $crossover = 0)
261 if ($crossover == 1) {
262 //if claim forwarded setting a new status
268 // Determine the next insurance level to be billed.
269 $ferow = sqlQuery("SELECT date, last_level_billed " .
270 "FROM form_encounter WHERE " .
271 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
272 $date_of_service = substr($ferow['date'], 0, 10);
273 $new_payer_type = 0 +
$ferow['last_level_billed'];
274 if ($new_payer_type < 3 && !empty($ferow['last_level_billed']) ||
$new_payer_type == 0) {
278 $new_payer_id = self
::arGetPayerID($patient_id, $date_of_service, $new_payer_type);
281 // Queue up the claim.
283 BillingUtilities
::updateClaim(true, $patient_id, $encounter_id, $new_payer_id, $new_payer_type, $status, 5, '', 'hcfa', '', $crossover);
286 // Just reopen the claim.
288 BillingUtilities
::updateClaim(true, $patient_id, $encounter_id, -1, -1, $status, 0, '', '', '', $crossover);
292 return xl("Encounter ") . $encounter_id . xl(" is ready for re-billing.");