Fix for NFQ 0059 added check for procedure code in addition to standard code in lab...
[openemr.git] / library / parse_era.inc.php
blob6a53a864bf65883688533f7dae2031522749fd9b
1 <?php
2 // Copyright (C) 2006 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 function parse_era_2100(&$out, $cb) {
10 if ($out['loopid'] == '2110' || $out['loopid'] == '2100') {
12 // Production date is posted with adjustments, so make sure it exists.
13 if (!$out['production_date']) $out['production_date'] = $out['check_date'];
15 // Force the sum of service payments to equal the claim payment
16 // amount, and the sum of service adjustments to equal the CLP's
17 // (charged amount - paid amount - patient responsibility amount).
18 // This may result from claim-level adjustments, and in this case the
19 // first SVC item that we stored was a 'Claim' type. It also may result
20 // from poorly reported payment reversals, in which case we may need to
21 // create the 'Claim' service type here.
23 $paytotal = $out['amount_approved'];
24 $adjtotal = $out['amount_charged'] - $out['amount_approved'] - $out['amount_patient'];
25 foreach ($out['svc'] as $svc) {
26 $paytotal -= $svc['paid'];
27 foreach ($svc['adj'] as $adj) {
28 if ($adj['group_code'] != 'PR') $adjtotal -= $adj['amount'];
31 $paytotal = round($paytotal, 2);
32 $adjtotal = round($adjtotal, 2);
33 if ($paytotal != 0 || $adjtotal != 0) {
34 if ($out['svc'][0]['code'] != 'Claim') {
35 array_unshift($out['svc'], array());
36 $out['svc'][0]['code'] = 'Claim';
37 $out['svc'][0]['mod'] = '';
38 $out['svc'][0]['chg'] = '0';
39 $out['svc'][0]['paid'] = '0';
40 $out['svc'][0]['adj'] = array();
41 $out['warnings'] .= "Procedure 'Claim' is inserted artificially to " .
42 "force claim balancing.\n";
44 $out['svc'][0]['paid'] += $paytotal;
45 if ($adjtotal) {
46 $j = count($out['svc'][0]['adj']);
47 $out['svc'][0]['adj'][$j] = array();
48 $out['svc'][0]['adj'][$j]['group_code'] = 'CR'; // presuming a correction or reversal
49 $out['svc'][0]['adj'][$j]['reason_code'] = 'Balancing';
50 $out['svc'][0]['adj'][$j]['amount'] = $adjtotal;
52 // if ($out['svc'][0]['code'] != 'Claim') {
53 // $out['warnings'] .= "First service item payment amount " .
54 // "adjusted by $paytotal due to payment imbalance. " .
55 // "This should not happen!\n";
56 // }
59 $cb($out);
63 function parse_era($filename, $cb) {
64 $delimiter1 = '~';
65 $delimiter2 = '|';
66 $delimiter3 = '^';
68 $infh = fopen($filename, 'r');
69 if (! $infh) return "ERA input file open failed";
71 $out = array();
72 $out['loopid'] = '';
73 $out['st_segment_count'] = 0;
74 $buffer = '';
75 $segid = '';
77 while (true) {
78 if (strlen($buffer) < 2048 && ! feof($infh)) $buffer .= fread($infh, 2048);
79 $tpos = strpos($buffer, $delimiter1);
80 if ($tpos === false) break;
81 $inline = substr($buffer, 0, $tpos);
82 $buffer = substr($buffer, $tpos + 1);
84 // If this is the ISA segment then figure out what the delimiters are.
85 if ($segid === '' && substr($inline, 0, 3) === 'ISA') {
86 $delimiter2 = substr($inline, 3, 1);
87 $delimiter3 = substr($inline, -1);
90 $seg = explode($delimiter2, $inline);
91 $segid = $seg[0];
93 if ($segid == 'ISA') {
94 if ($out['loopid']) return 'Unexpected ISA segment';
95 $out['isa_sender_id'] = trim($seg[6]);
96 $out['isa_receiver_id'] = trim($seg[8]);
97 $out['isa_control_number'] = trim($seg[13]);
98 // TBD: clear some stuff if we allow multiple transmission files.
100 else if ($segid == 'GS') {
101 if ($out['loopid']) return 'Unexpected GS segment';
102 $out['gs_date'] = trim($seg[4]);
103 $out['gs_time'] = trim($seg[5]);
104 $out['gs_control_number'] = trim($seg[6]);
106 else if ($segid == 'ST') {
107 parse_era_2100($out, $cb);
108 $out['loopid'] = '';
109 $out['st_control_number'] = trim($seg[2]);
110 $out['st_segment_count'] = 0;
112 else if ($segid == 'BPR') {
113 if ($out['loopid']) return 'Unexpected BPR segment';
114 $out['check_amount'] = trim($seg[2]);
115 $out['check_date'] = trim($seg[16]); // yyyymmdd
116 // TBD: BPR04 is a payment method code.
118 else if ($segid == 'TRN') {
119 if ($out['loopid']) return 'Unexpected TRN segment';
120 $out['check_number'] = trim($seg[2]);
121 $out['payer_tax_id'] = substr($seg[3], 1); // 9 digits
122 $out['payer_id'] = trim($seg[4]);
123 // Note: TRN04 further qualifies the paying entity within the
124 // organization identified by TRN03.
126 else if ($segid == 'REF' && $seg[1] == 'EV') {
127 if ($out['loopid']) return 'Unexpected REF|EV segment';
129 else if ($segid == 'CUR' && ! $out['loopid']) {
130 if ($seg[3] && $seg[3] != 1.0) {
131 return("We cannot handle foreign currencies!");
134 else if ($segid == 'REF' && ! $out['loopid']) {
135 // ignore
137 else if ($segid == 'DTM' && $seg[1] == '405') {
138 if ($out['loopid']) return 'Unexpected DTM|405 segment';
139 $out['production_date'] = trim($seg[2]); // yyyymmdd
142 // Loop 1000A is Payer Information.
144 else if ($segid == 'N1' && $seg[1] == 'PR') {
145 if ($out['loopid']) return 'Unexpected N1|PR segment';
146 $out['loopid'] = '1000A';
147 $out['payer_name'] = trim($seg[2]);
149 else if ($segid == 'N3' && $out['loopid'] == '1000A') {
150 $out['payer_street'] = trim($seg[1]);
151 // TBD: N302 may exist as an additional address line.
153 else if ($segid == 'N4' && $out['loopid'] == '1000A') {
154 $out['payer_city'] = trim($seg[1]);
155 $out['payer_state'] = trim($seg[2]);
156 $out['payer_zip'] = trim($seg[3]);
158 else if ($segid == 'REF' && $out['loopid'] == '1000A') {
159 // Other types of REFs may be given to identify the payer, but we
160 // ignore them.
162 else if ($segid == 'PER' && $out['loopid'] == '1000A') {
163 // TBD: Report payer contact information as a note.
166 // Loop 1000B is Payee Identification.
168 else if ($segid == 'N1' && $seg[1] == 'PE') {
169 if ($out['loopid'] != '1000A') return 'Unexpected N1|PE segment';
170 $out['loopid'] = '1000B';
171 $out['payee_name'] = trim($seg[2]);
172 $out['payee_tax_id'] = trim($seg[4]);
174 else if ($segid == 'N3' && $out['loopid'] == '1000B') {
175 $out['payee_street'] = trim($seg[1]);
177 else if ($segid == 'N4' && $out['loopid'] == '1000B') {
178 $out['payee_city'] = trim($seg[1]);
179 $out['payee_state'] = trim($seg[2]);
180 $out['payee_zip'] = trim($seg[3]);
182 else if ($segid == 'REF' && $out['loopid'] == '1000B') {
183 // Used to report additional ID numbers. Ignored.
186 // Loop 2000 provides for logical grouping of claim payment information.
187 // LX is required if any CLPs are present, but so far we do not care
188 // about loop 2000 content.
190 else if ($segid == 'LX') {
191 if (! $out['loopid']) return 'Unexpected LX segment';
192 parse_era_2100($out, $cb);
193 $out['loopid'] = '2000';
195 else if ($segid == 'TS2' && $out['loopid'] == '2000') {
196 // ignore
198 else if ($segid == 'TS3' && $out['loopid'] == '2000') {
199 // ignore
202 // Loop 2100 is Claim Payment Information. The good stuff begins here.
204 else if ($segid == 'CLP') {
205 if (! $out['loopid']) return 'Unexpected CLP segment';
206 parse_era_2100($out, $cb);
207 $out['loopid'] = '2100';
208 $out['warnings'] = '';
209 // Clear some stuff to start the new claim:
210 $out['subscriber_lname'] = '';
211 $out['subscriber_fname'] = '';
212 $out['subscriber_mname'] = '';
213 $out['subscriber_member_id'] = '';
214 $out['crossover']=0;
215 $out['svc'] = array();
217 // This is the poorly-named "Patient Account Number". For 837p
218 // it comes from CLM01 which we populated as pid-diagid-procid,
219 // where diagid and procid are id values from the billing table.
220 // For HCFA 1500 claims it comes from field 26 which we
221 // populated with our familiar pid-encounter billing key.
223 // The 835 spec calls this the "provider-assigned claim control
224 // number" and notes that it is specifically intended for
225 // identifying the claim in the provider's database.
226 $out['our_claim_id'] = trim($seg[1]);
228 $out['claim_status_code'] = trim($seg[2]);
229 $out['amount_charged'] = trim($seg[3]);
230 $out['amount_approved'] = trim($seg[4]);
231 $out['amount_patient'] = trim($seg[5]); // pt responsibility, copay + deductible
232 $out['payer_claim_id'] = trim($seg[7]); // payer's claim number
234 else if ($segid == 'CAS' && $out['loopid'] == '2100') {
235 // This is a claim-level adjustment and should be unusual.
236 // Handle it by creating a dummy zero-charge service item and
237 // then populating the adjustments into it. See also code in
238 // parse_era_2100() which will later plug in a payment reversal
239 // amount that offsets these adjustments.
240 $i = 0; // if present, the dummy service item will be first.
241 if (!$out['svc'][$i]) {
242 $out['svc'][$i] = array();
243 $out['svc'][$i]['code'] = 'Claim';
244 $out['svc'][$i]['mod'] = '';
245 $out['svc'][$i]['chg'] = '0';
246 $out['svc'][$i]['paid'] = '0';
247 $out['svc'][$i]['adj'] = array();
249 for ($k = 2; $k < 20; $k += 3) {
250 if (!$seg[$k]) break;
251 $j = count($out['svc'][$i]['adj']);
252 $out['svc'][$i]['adj'][$j] = array();
253 $out['svc'][$i]['adj'][$j]['group_code'] = $seg[1];
254 $out['svc'][$i]['adj'][$j]['reason_code'] = $seg[$k];
255 $out['svc'][$i]['adj'][$j]['amount'] = $seg[$k+1];
258 // QC = Patient
259 else if ($segid == 'NM1' && $seg[1] == 'QC' && $out['loopid'] == '2100') {
260 $out['patient_lname'] = trim($seg[3]);
261 $out['patient_fname'] = trim($seg[4]);
262 $out['patient_mname'] = trim($seg[5]);
263 $out['patient_member_id'] = trim($seg[9]);
265 // IL = Insured or Subscriber
266 else if ($segid == 'NM1' && $seg[1] == 'IL' && $out['loopid'] == '2100') {
267 $out['subscriber_lname'] = trim($seg[3]);
268 $out['subscriber_fname'] = trim($seg[4]);
269 $out['subscriber_mname'] = trim($seg[5]);
270 $out['subscriber_member_id'] = trim($seg[9]);
272 // 82 = Rendering Provider
273 else if ($segid == 'NM1' && $seg[1] == '82' && $out['loopid'] == '2100') {
274 $out['provider_lname'] = trim($seg[3]);
275 $out['provider_fname'] = trim($seg[4]);
276 $out['provider_mname'] = trim($seg[5]);
277 $out['provider_member_id'] = trim($seg[9]);
279 else if ($segid == 'NM1' && $seg[1] == 'TT' && $out['loopid'] == '2100') {
280 $out['crossover'] = 1;//Claim automatic forward case.
283 // 74 = Corrected Insured
284 // TT = Crossover Carrier (Transfer To another payer)
285 // PR = Corrected Payer
286 else if ($segid == 'NM1' && $out['loopid'] == '2100') {
287 // $out['warnings'] .= "NM1 segment at claim level ignored.\n";
289 else if ($segid == 'MOA' && $out['loopid'] == '2100') {
290 $out['warnings'] .= "MOA segment at claim level ignored.\n";
292 // REF segments may provide various identifying numbers, where REF02
293 // indicates the type of number.
294 else if ($segid == 'REF' && $seg[1] == '1W' && $out['loopid'] == '2100') {
295 $out['claim_comment'] = trim($seg[2]);
297 else if ($segid == 'REF' && $out['loopid'] == '2100') {
298 // ignore
300 else if ($segid == 'DTM' && $seg[1] == '050' && $out['loopid'] == '2100') {
301 $out['claim_date'] = trim($seg[2]); // yyyymmdd
303 // 036 = expiration date of coverage
304 // 050 = date claim received by payer
305 // 232 = claim statement period start
306 // 233 = claim statement period end
307 else if ($segid == 'DTM' && $out['loopid'] == '2100') {
308 // ignore?
310 else if ($segid == 'PER' && $out['loopid'] == '2100') {
312 $out['payer_insurance'] = trim($seg[2]);
313 $out['warnings'] .= 'Claim contact information: ' .
314 $seg[4] . "\n";
316 // For AMT01 see the Amount Qualifier Codes on pages 135-135 of the
317 // Implementation Guide. AMT is only good for comments and is not
318 // part of claim balancing.
319 else if ($segid == 'AMT' && $out['loopid'] == '2100') {
320 $out['warnings'] .= "AMT segment at claim level ignored.\n";
322 // For QTY01 see the Quantity Qualifier Codes on pages 137-138 of the
323 // Implementation Guide. QTY is only good for comments and is not
324 // part of claim balancing.
325 else if ($segid == 'QTY' && $out['loopid'] == '2100') {
326 $out['warnings'] .= "QTY segment at claim level ignored.\n";
329 // Loop 2110 is Service Payment Information.
331 else if ($segid == 'SVC') {
332 if (! $out['loopid']) return 'Unexpected SVC segment';
333 $out['loopid'] = '2110';
334 if ($seg[6]) {
335 // SVC06 if present is our original procedure code that they are changing.
336 // We will not put their crap in our invoice, but rather log a note and
337 // treat it as adjustments to our originally submitted coding.
338 $svc = explode($delimiter3, $seg[6]);
339 $tmp = explode($delimiter3, $seg[1]);
340 $out['warnings'] .= "Payer is restating our procedure " . $svc[1] .
341 " as " . $tmp[1] . ".\n";
342 } else {
343 $svc = explode($delimiter3, $seg[1]);
345 if ($svc[0] != 'HC') return 'SVC segment has unexpected qualifier';
346 // TBD: Other qualifiers are possible; see IG pages 140-141.
347 $i = count($out['svc']);
348 $out['svc'][$i] = array();
349 // It seems some payers append the modifier with no separator!
350 if (strlen($svc[1]) == 7 && empty($svc[2])) {
351 $out['svc'][$i]['code'] = substr($svc[1], 0, 5);
352 $out['svc'][$i]['mod'] = substr($svc[1], 5);
353 } else {
354 $out['svc'][$i]['code'] = $svc[1];
355 $out['svc'][$i]['mod'] = $svc[2] ? $svc[2] : '';
357 // TBD: There may be up to 4 procedure modifiers in $svc[2] thru [5].
358 $out['svc'][$i]['chg'] = $seg[2];
359 $out['svc'][$i]['paid'] = $seg[3];
360 $out['svc'][$i]['adj'] = array();
361 // Note: SVC05, if present, indicates the paid units of service.
362 // It defaults to 1.
364 // DTM01 identifies the type of service date:
365 // 472 = a single date of service
366 // 150 = service period start
367 // 151 = service period end
368 else if ($segid == 'DTM' && $out['loopid'] == '2110') {
369 $out['dos'] = trim($seg[2]); // yyyymmdd
371 else if ($segid == 'CAS' && $out['loopid'] == '2110') {
372 $i = count($out['svc']) - 1;
373 for ($k = 2; $k < 20; $k += 3) {
374 if (!$seg[$k]) break;
375 if ($seg[1] == 'CO' && $seg[$k+1] < 0) {
376 $out['warnings'] .= "Negative Contractual Obligation adjustment " .
377 "seems wrong. Inverting, but should be checked!\n";
378 $seg[$k+1] = 0 - $seg[$k+1];
380 $j = count($out['svc'][$i]['adj']);
381 $out['svc'][$i]['adj'][$j] = array();
382 $out['svc'][$i]['adj'][$j]['group_code'] = $seg[1];
383 $out['svc'][$i]['adj'][$j]['reason_code'] = $seg[$k];
384 $out['svc'][$i]['adj'][$j]['amount'] = $seg[$k+1];
385 // Note: $seg[$k+2] is "quantity". A value here indicates a change to
386 // the number of units of service. We're ignoring that for now.
389 else if ($segid == 'REF' && $out['loopid'] == '2110') {
390 // ignore
392 else if ($segid == 'AMT' && $seg[1] == 'B6' && $out['loopid'] == '2110') {
393 $i = count($out['svc']) - 1;
394 $out['svc'][$i]['allowed'] = $seg[2]; // report this amount as a note
396 else if ($segid == 'AMT' && $out['loopid'] == '2110') {
397 $out['warnings'] .= "$inline at service level ignored.\n";
399 else if ($segid == 'LQ' && $seg[1] == 'HE' && $out['loopid'] == '2110') {
400 $i = count($out['svc']) - 1;
401 $out['svc'][$i]['remark'] = $seg[2];
403 else if ($segid == 'QTY' && $out['loopid'] == '2110') {
404 $out['warnings'] .= "QTY segment at service level ignored.\n";
406 else if ($segid == 'PLB') {
407 // Provider-level adjustments are a General Ledger thing and should not
408 // alter the A/R for the claim, so we just report them as notes.
409 for ($k = 3; $k < 15; $k += 2) {
410 if (!$seg[$k]) break;
411 $out['warnings'] .= 'PROVIDER LEVEL ADJUSTMENT (not claim-specific): $' .
412 sprintf('%.2f', $seg[$k+1]) . " with reason code " . $seg[$k] . "\n";
413 // Note: For PLB adjustment reason codes see IG pages 165-170.
416 else if ($segid == 'SE') {
417 parse_era_2100($out, $cb);
418 $out['loopid'] = '';
419 if ($out['st_control_number'] != trim($seg[2])) {
420 return 'Ending transaction set control number mismatch';
422 if (($out['st_segment_count'] + 1) != trim($seg[1])) {
423 return 'Ending transaction set segment count mismatch';
426 else if ($segid == 'GE') {
427 if ($out['loopid']) return 'Unexpected GE segment';
428 if ($out['gs_control_number'] != trim($seg[2])) {
429 return 'Ending functional group control number mismatch';
432 else if ($segid == 'IEA') {
433 if ($out['loopid']) return 'Unexpected IEA segment';
434 if ($out['isa_control_number'] != trim($seg[2])) {
435 return 'Ending interchange control number mismatch';
438 else {
439 return "Unknown or unexpected segment ID $segid";
442 ++$out['st_segment_count'];
445 if ($segid != 'IEA') return 'Premature end of ERA file';
446 return '';
448 //for getting the check details and provider details
449 function parse_era_for_check($filename) {
450 $delimiter1 = '~';
451 $delimiter2 = '|';
452 $delimiter3 = '^';
454 $infh = fopen($filename, 'r');
455 if (! $infh) return "ERA input file open failed";
457 $out = array();
458 $out['loopid'] = '';
459 $out['st_segment_count'] = 0;
460 $buffer = '';
461 $segid = '';
462 $check_count=0;
463 while (true) {
465 if (strlen($buffer) < 2048 && ! feof($infh)) $buffer .= fread($infh, 2048);
466 $tpos = strpos($buffer, $delimiter1);
467 if ($tpos === false) break;
468 $inline = substr($buffer, 0, $tpos);
469 $buffer = substr($buffer, $tpos + 1);
471 // If this is the ISA segment then figure out what the delimiters are.
472 if ($segid === '' && substr($inline, 0, 3) === 'ISA') {
473 $delimiter2 = substr($inline, 3, 1);
474 $delimiter3 = substr($inline, -1);
477 $seg = explode($delimiter2, $inline);
478 $segid = $seg[0];
480 if ($segid == 'ISA') {
483 else if ($segid == 'BPR') {
484 ++$check_count;
485 //if ($out['loopid']) return 'Unexpected BPR segment';
486 $out['check_amount'.$check_count] = trim($seg[2]);
487 $out['check_date'.$check_count] = trim($seg[16]); // yyyymmdd
488 // TBD: BPR04 is a payment method code.
491 else if ($segid == 'N1' && $seg[1] == 'PE') {
492 //if ($out['loopid'] != '1000A') return 'Unexpected N1|PE segment';
493 $out['loopid'] = '1000B';
494 $out['payee_name'.$check_count] = trim($seg[2]);
495 $out['payee_tax_id'.$check_count] = trim($seg[4]);
498 else if ($segid == 'TRN') {
499 //if ($out['loopid']) return 'Unexpected TRN segment';
500 $out['check_number'.$check_count] = trim($seg[2]);
501 $out['payer_tax_id'.$check_count] = substr($seg[3], 1); // 9 digits
502 $out['payer_id'.$check_count] = trim($seg[4]);
503 // Note: TRN04 further qualifies the paying entity within the
504 // organization identified by TRN03.
510 ++$out['st_segment_count'];
512 $out['check_count']=$check_count;
513 era_callback_check($out);
515 if ($segid != 'IEA') return 'Premature end of ERA file';
516 return '';