forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsl_eob.inc.php
260 lines (239 loc) · 8.54 KB
/
sl_eob.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
// Copyright (C) 2005-2009 Rod Roark <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
include_once("patient.inc");
include_once("billing.inc");
include_once("invoice_summary.inc.php");
$chart_id_cash = 0;
$chart_id_ar = 0;
$chart_id_income = 0;
$services_id = 0;
// Try to figure out our invoice number (pid.encounter) from the
// claim ID and other stuff in the ERA. This should be straightforward
// except that some payers mangle the claim ID that we give them.
//
function slInvoiceNumber(&$out) {
$invnumber = $out['our_claim_id'];
$atmp = preg_split('/[ -]/', $invnumber);
$acount = count($atmp);
$pid = 0;
$encounter = 0;
if ($acount == 2) {
$pid = $atmp[0];
$encounter = $atmp[1];
}
else if ($acount == 3) {
$pid = $atmp[0];
$brow = sqlQuery("SELECT encounter FROM billing WHERE " .
"pid = '$pid' AND encounter = '" . $atmp[1] . "' AND activity = 1");
$encounter = $brow['encounter'];
}
else if ($acount == 1) {
$pres = sqlStatement("SELECT pid FROM patient_data WHERE " .
"lname LIKE '" . addslashes($out['patient_lname']) . "' AND " .
"fname LIKE '" . addslashes($out['patient_fname']) . "' " .
"ORDER BY pid DESC");
while ($prow = sqlFetchArray($pres)) {
if (strpos($invnumber, $prow['pid']) === 0) {
$pid = $prow['pid'];
$encounter = substr($invnumber, strlen($pid));
break;
}
}
}
if ($pid && $encounter) $invnumber = "$pid.$encounter";
return array($pid, $encounter, $invnumber);
}
// This gets a posting session ID. If the payer ID is not 0 and a matching
// session already exists, then its ID is returned. Otherwise a new session
// is created.
//
function arGetSession($payer_id, $reference, $check_date, $deposit_date='', $pay_total=0) {
if (empty($deposit_date)) $deposit_date = $check_date;
if ($payer_id) {
$row = sqlQuery("SELECT session_id FROM ar_session WHERE " .
"payer_id = '$payer_id' AND reference = '$reference' AND " .
"check_date = '$check_date' AND deposit_date = '$deposit_date' " .
"ORDER BY session_id DESC LIMIT 1");
if (!empty($row['session_id'])) return $row['session_id'];
}
return sqlInsert("INSERT INTO ar_session ( " .
"payer_id, user_id, reference, check_date, deposit_date, pay_total " .
") VALUES ( " .
"'$payer_id', " .
"'" . $_SESSION['authUserID'] . "', " .
"'$reference', " .
"'$check_date', " .
"'$deposit_date', " .
"'$pay_total' " .
")");
}
//writing the check details to Session Table on ERA proxcessing
function arPostSession($payer_id,$check_number,$check_date,$pay_total,$post_to_date,$deposit_date,$debug) {
$query = "INSERT INTO ar_session( " .
"payer_id,user_id,closed,reference,check_date,pay_total,post_to_date,deposit_date,patient_id,payment_type,adjustment_code,payment_method " .
") VALUES ( " .
"'$payer_id'," .
$_SESSION['authUserID']."," .
"0," .
"'ePay - $check_number'," .
"'$check_date', " .
"$pay_total, " .
"'$post_to_date','$deposit_date', " .
"0,'insurance','insurance_payment','electronic'" .
")";
if ($debug) {
echo $query . "<br>\n";
} else {
$sessionId=sqlInsert($query);
return $sessionId;
}
}
// Post a payment, new style.
//
function arPostPayment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $memo, $debug, $time='', $codetype='') {
$codeonly = $code;
$modifier = '';
$tmp = strpos($code, ':');
if ($tmp) {
$codeonly = substr($code, 0, $tmp);
$modifier = substr($code, $tmp+1);
}
if (empty($time)) $time = date('Y-m-d H:i:s');
$query = "INSERT INTO ar_activity ( " .
"pid, encounter, code_type, code, modifier, payer_type, post_time, post_user, " .
"session_id, memo, pay_amount " .
") VALUES ( " .
"'$patient_id', " .
"'$encounter_id', " .
"'$codetype', " .
"'$codeonly', " .
"'$modifier', " .
"'$payer_type', " .
"'$time', " .
"'" . $_SESSION['authUserID'] . "', " .
"'$session_id', " .
"'$memo', " .
"'$amount' " .
")";
sqlStatement($query);
return;
}
// Post a charge. This is called only from sl_eob_process.php where
// automated remittance processing can create a new service item.
// Here we add it as an unauthorized item to the billing table.
//
function arPostCharge($patient_id, $encounter_id, $session_id, $amount, $units, $thisdate, $code, $description, $debug, $codetype='') {
/*****************************************************************
// Select an existing billing item as a template.
$row= sqlQuery("SELECT * FROM billing WHERE " .
"pid = '$patient_id' AND encounter = '$encounter_id' AND " .
"code_type = 'CPT4' AND activity = 1 " .
"ORDER BY id DESC LIMIT 1");
$this_authorized = 0;
$this_provider = 0;
if (!empty($row)) {
$this_authorized = $row['authorized'];
$this_provider = $row['provider_id'];
}
*****************************************************************/
if (empty($codetype)) {
// default to CPT4 if empty, which is consistent with previous functionality.
$codetype="CPT4";
}
$codeonly = $code;
$modifier = '';
$tmp = strpos($code, ':');
if ($tmp) {
$codeonly = substr($code, 0, $tmp);
$modifier = substr($code, $tmp+1);
}
addBilling($encounter_id,
$codetype,
$codeonly,
$description,
$patient_id,
0,
0,
$modifier,
$units,
$amount,
'',
'');
}
// Post an adjustment, new style.
//
function arPostAdjustment($patient_id, $encounter_id, $session_id, $amount, $code, $payer_type, $reason, $debug, $time='', $codetype='') {
$codeonly = $code;
$modifier = '';
$tmp = strpos($code, ':');
if ($tmp) {
$codeonly = substr($code, 0, $tmp);
$modifier = substr($code, $tmp+1);
}
if (empty($time)) $time = date('Y-m-d H:i:s');
$query = "INSERT INTO ar_activity ( " .
"pid, encounter, code_type, code, modifier, payer_type, post_user, post_time, " .
"session_id, memo, adj_amount " .
") VALUES ( " .
"'$patient_id', " .
"'$encounter_id', " .
"'$codetype', " .
"'$codeonly', " .
"'$modifier', " .
"'$payer_type', " .
"'" . $_SESSION['authUserID'] . "', " .
"'$time', " .
"'$session_id', " .
"'$reason', " .
"'$amount' " .
")";
sqlStatement($query);
return;
}
function arGetPayerID($patient_id, $date_of_service, $payer_type) {
if ($payer_type < 1 || $payer_type > 3) return 0;
$tmp = array(1 => 'primary', 2 => 'secondary', 3 => 'tertiary');
$value = $tmp[$payer_type];
$query = "SELECT provider FROM insurance_data WHERE " .
"pid = ? AND type = ? AND date <= ? " .
"ORDER BY date DESC LIMIT 1";
$nprow = sqlQuery($query, array($patient_id,$value,$date_of_service) );
if (empty($nprow)) return 0;
return $nprow['provider'];
}
// Make this invoice re-billable, new style.
//
function arSetupSecondary($patient_id, $encounter_id, $debug,$crossover=0) {
if ($crossover==1) {
//if claim forwarded setting a new status
$status=6;
} else {
$status=1;
}
// Determine the next insurance level to be billed.
$ferow = sqlQuery("SELECT date, last_level_billed " .
"FROM form_encounter WHERE " .
"pid = '$patient_id' AND encounter = '$encounter_id'");
$date_of_service = substr($ferow['date'], 0, 10);
$new_payer_type = 0 + $ferow['last_level_billed'];
if ($new_payer_type < 3 && !empty($ferow['last_level_billed']) || $new_payer_type == 0)
++$new_payer_type;
$new_payer_id = arGetPayerID($patient_id, $date_of_service, $new_payer_type);
if ($new_payer_id) {
// Queue up the claim.
if (!$debug)
updateClaim(true, $patient_id, $encounter_id, $new_payer_id, $new_payer_type,$status, 5, '', 'hcfa','',$crossover);
}
else {
// Just reopen the claim.
if (!$debug)
updateClaim(true, $patient_id, $encounter_id, -1, -1, $status, 0, '','','',$crossover);
}
return xl("Encounter ") . $encounter . xl(" is ready for re-billing.");
}
?>