Merge pull request #1761 from sjpadgett/PP2-appointment
[openemr.git] / library / sl_eob.inc.php
blob605763974abec7608f842fbf784b5aa437cfc7f7
1 <?php
2 // Copyright (C) 2005-2009 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 include_once("patient.inc");
10 include_once("billing.inc");
11 include_once("invoice_summary.inc.php");
13 $chart_id_cash = 0;
14 $chart_id_ar = 0;
15 $chart_id_income = 0;
16 $services_id = 0;
19 // Try to figure out our invoice number (pid.encounter) from the
20 // claim ID and other stuff in the ERA. This should be straightforward
21 // except that some payers mangle the claim ID that we give them.
23 function slInvoiceNumber(&$out)
25 $invnumber = $out['our_claim_id'];
26 $atmp = preg_split('/[ -]/', $invnumber);
27 $acount = count($atmp);
29 $pid = 0;
30 $encounter = 0;
31 if ($acount == 2) {
32 $pid = $atmp[0];
33 $encounter = $atmp[1];
34 } else if ($acount == 3) {
35 $pid = $atmp[0];
36 $brow = sqlQuery("SELECT encounter FROM billing WHERE " .
37 "pid = '$pid' AND encounter = '" . $atmp[1] . "' AND activity = 1");
39 $encounter = $brow['encounter'];
40 } else if ($acount == 1) {
41 $pres = sqlStatement("SELECT pid FROM patient_data WHERE " .
42 "lname LIKE '" . addslashes($out['patient_lname']) . "' AND " .
43 "fname LIKE '" . addslashes($out['patient_fname']) . "' " .
44 "ORDER BY pid DESC");
45 while ($prow = sqlFetchArray($pres)) {
46 if (strpos($invnumber, $prow['pid']) === 0) {
47 $pid = $prow['pid'];
48 $encounter = substr($invnumber, strlen($pid));
49 break;
54 if ($pid && $encounter) {
55 $invnumber = "$pid.$encounter";
58 return array($pid, $encounter, $invnumber);
61 // This gets a posting session ID. If the payer ID is not 0 and a matching
62 // session already exists, then its ID is returned. Otherwise a new session
63 // is created.
65 function arGetSession($payer_id, $reference, $check_date, $deposit_date = '', $pay_total = 0)
67 if (empty($deposit_date)) {
68 $deposit_date = $check_date;
71 if ($payer_id) {
72 $row = sqlQuery("SELECT session_id FROM ar_session WHERE " .
73 "payer_id = '$payer_id' AND reference = '$reference' AND " .
74 "check_date = '$check_date' AND deposit_date = '$deposit_date' " .
75 "ORDER BY session_id DESC LIMIT 1");
76 if (!empty($row['session_id'])) {
77 return $row['session_id'];
81 return sqlInsert("INSERT INTO ar_session ( " .
82 "payer_id, user_id, reference, check_date, deposit_date, pay_total " .
83 ") VALUES ( " .
84 "'$payer_id', " .
85 "'" . $_SESSION['authUserID'] . "', " .
86 "'$reference', " .
87 "'$check_date', " .
88 "'$deposit_date', " .
89 "'$pay_total' " .
90 ")");
92 //writing the check details to Session Table on ERA proxcessing
93 function arPostSession($payer_id, $check_number, $check_date, $pay_total, $post_to_date, $deposit_date, $debug)
95 $query = "INSERT INTO ar_session( " .
96 "payer_id,user_id,closed,reference,check_date,pay_total,post_to_date,deposit_date,patient_id,payment_type,adjustment_code,payment_method " .
97 ") VALUES ( " .
98 "'$payer_id'," .
99 $_SESSION['authUserID']."," .
100 "0," .
101 "'ePay - $check_number'," .
102 "'$check_date', " .
103 "$pay_total, " .
104 "'$post_to_date','$deposit_date', " .
105 "0,'insurance','insurance_payment','electronic'" .
106 ")";
107 if ($debug) {
108 echo $query . "<br>\n";
109 } else {
110 $sessionId=sqlInsert($query);
111 return $sessionId;
115 // Post a payment, new style.
117 function arPostPayment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $memo, $debug, $time = '', $codetype = '')
119 $codeonly = $code;
120 $modifier = '';
121 $tmp = strpos($code, ':');
122 if ($tmp) {
123 $codeonly = substr($code, 0, $tmp);
124 $modifier = substr($code, $tmp+1);
127 if (empty($time)) {
128 $time = date('Y-m-d H:i:s');
131 sqlBeginTrans();
132 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
133 $query = "INSERT INTO ar_activity ( " .
134 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_time, post_user, " .
135 "session_id, memo, pay_amount " .
136 ") VALUES ( " .
137 "'$patient_id', " .
138 "'$encounter_id', " .
139 "'{$sequence_no['increment']}', " .
140 "'$codetype', " .
141 "'$codeonly', " .
142 "'$modifier', " .
143 "'$payer_type', " .
144 "'$time', " .
145 "'" . $_SESSION['authUserID'] . "', " .
146 "'$session_id', " .
147 "'$memo', " .
148 "'$amount' " .
149 ")";
150 sqlStatement($query);
151 sqlCommitTrans();
152 return;
155 // Post a charge. This is called only from sl_eob_process.php where
156 // automated remittance processing can create a new service item.
157 // Here we add it as an unauthorized item to the billing table.
159 function arPostCharge($patient_id, $encounter_id, $session_id, $amount, $units, $thisdate, $code, $description, $debug, $codetype = '')
161 /*****************************************************************
162 // Select an existing billing item as a template.
163 $row= sqlQuery("SELECT * FROM billing WHERE " .
164 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
165 "code_type = 'CPT4' AND activity = 1 " .
166 "ORDER BY id DESC LIMIT 1");
167 $this_authorized = 0;
168 $this_provider = 0;
169 if (!empty($row)) {
170 $this_authorized = $row['authorized'];
171 $this_provider = $row['provider_id'];
173 *****************************************************************/
175 if (empty($codetype)) {
176 // default to CPT4 if empty, which is consistent with previous functionality.
177 $codetype="CPT4";
180 $codeonly = $code;
181 $modifier = '';
182 $tmp = strpos($code, ':');
183 if ($tmp) {
184 $codeonly = substr($code, 0, $tmp);
185 $modifier = substr($code, $tmp+1);
188 addBilling(
189 $encounter_id,
190 $codetype,
191 $codeonly,
192 $description,
193 $patient_id,
196 $modifier,
197 $units,
198 $amount,
204 // Post an adjustment, new style.
206 function arPostAdjustment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $reason, $debug, $time = '', $codetype = '')
208 $codeonly = $code;
209 $modifier = '';
210 $tmp = strpos($code, ':');
211 if ($tmp) {
212 $codeonly = substr($code, 0, $tmp);
213 $modifier = substr($code, $tmp+1);
216 if (empty($time)) {
217 $time = date('Y-m-d H:i:s');
220 sqlBeginTrans();
221 $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array($patient_id, $encounter_id));
222 $query = "INSERT INTO ar_activity ( " .
223 "pid, encounter, sequence_no, code_type, code, modifier, payer_type, post_user, post_time, " .
224 "session_id, memo, adj_amount " .
225 ") VALUES ( " .
226 "'$patient_id', " .
227 "'$encounter_id', " .
228 "'{$sequence_no['increment']}', " .
229 "'$codetype', " .
230 "'$codeonly', " .
231 "'$modifier', " .
232 "'$payer_type', " .
233 "'" . $_SESSION['authUserID'] . "', " .
234 "'$time', " .
235 "'$session_id', " .
236 "'$reason', " .
237 "'$amount' " .
238 ")";
239 sqlStatement($query);
240 sqlCommitTrans();
241 return;
244 function arGetPayerID($patient_id, $date_of_service, $payer_type)
246 if ($payer_type < 1 || $payer_type > 3) {
247 return 0;
250 $tmp = array(1 => 'primary', 2 => 'secondary', 3 => 'tertiary');
251 $value = $tmp[$payer_type];
252 $query = "SELECT provider FROM insurance_data WHERE " .
253 "pid = ? AND type = ? AND date <= ? " .
254 "ORDER BY date DESC LIMIT 1";
255 $nprow = sqlQuery($query, array($patient_id,$value,$date_of_service));
256 if (empty($nprow)) {
257 return 0;
260 return $nprow['provider'];
263 // Make this invoice re-billable, new style.
265 function arSetupSecondary($patient_id, $encounter_id, $debug, $crossover = 0)
267 if ($crossover==1) {
268 //if claim forwarded setting a new status
269 $status=6;
270 } else {
271 $status=1;
274 // Determine the next insurance level to be billed.
275 $ferow = sqlQuery("SELECT date, last_level_billed " .
276 "FROM form_encounter WHERE " .
277 "pid = '$patient_id' AND encounter = '$encounter_id'");
278 $date_of_service = substr($ferow['date'], 0, 10);
279 $new_payer_type = 0 + $ferow['last_level_billed'];
280 if ($new_payer_type < 3 && !empty($ferow['last_level_billed']) || $new_payer_type == 0) {
281 ++$new_payer_type;
284 $new_payer_id = arGetPayerID($patient_id, $date_of_service, $new_payer_type);
286 if ($new_payer_id) {
287 // Queue up the claim.
288 if (!$debug) {
289 updateClaim(true, $patient_id, $encounter_id, $new_payer_id, $new_payer_type, $status, 5, '', 'hcfa', '', $crossover);
291 } else {
292 // Just reopen the claim.
293 if (!$debug) {
294 updateClaim(true, $patient_id, $encounter_id, -1, -1, $status, 0, '', '', '', $crossover);
298 return xl("Encounter ") . $encounter . xl(" is ready for re-billing.");