2 // Copyright (C) 2006 Rod Roark <rod@sunsetsystems.com>
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;
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";
63 function parse_era($filename, $cb) {
68 $infh = fopen($filename, 'r');
69 if (! $infh) return "ERA input file open failed";
73 $out['st_segment_count'] = 0;
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);
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);
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']) {
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
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') {
198 else if ($segid == 'TS3' && $out['loopid'] == '2000') {
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'] = '';
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];
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') {
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') {
310 else if ($segid == 'PER' && $out['loopid'] == '2100') {
312 $out['payer_insurance'] = trim($seg[2]);
313 $out['warnings'] .= 'Claim contact information: ' .
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';
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";
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);
354 $out['svc'][$i]['code'] = $svc[1];
355 $out['svc'][$i]['mod'] = $svc[2] ?
$svc[2] . ':' : '';
356 $out['svc'][$i]['mod'] .= $svc[3] ?
$svc[3] . ':' : '';
357 $out['svc'][$i]['mod'] .= $svc[4] ?
$svc[4] . ':' : '';
358 $out['svc'][$i]['mod'] .= $svc[5] ?
$svc[5] . ':' : '';
359 $out['svc'][$i]['mod'] = preg_replace('/:$/','',$out['svc'][$i]['mod']);
361 $out['svc'][$i]['chg'] = $seg[2];
362 $out['svc'][$i]['paid'] = $seg[3];
363 $out['svc'][$i]['adj'] = array();
364 // Note: SVC05, if present, indicates the paid units of service.
367 // DTM01 identifies the type of service date:
368 // 472 = a single date of service
369 // 150 = service period start
370 // 151 = service period end
371 else if ($segid == 'DTM' && $out['loopid'] == '2110') {
372 $out['dos'] = trim($seg[2]); // yyyymmdd
374 else if ($segid == 'CAS' && $out['loopid'] == '2110') {
375 $i = count($out['svc']) - 1;
376 for ($k = 2; $k < 20; $k +
= 3) {
377 if (!$seg[$k]) break;
378 if ($seg[1] == 'CO' && $seg[$k+
1] < 0) {
379 $out['warnings'] .= "Negative Contractual Obligation adjustment " .
380 "seems wrong. Inverting, but should be checked!\n";
381 $seg[$k+
1] = 0 - $seg[$k+
1];
383 $j = count($out['svc'][$i]['adj']);
384 $out['svc'][$i]['adj'][$j] = array();
385 $out['svc'][$i]['adj'][$j]['group_code'] = $seg[1];
386 $out['svc'][$i]['adj'][$j]['reason_code'] = $seg[$k];
387 $out['svc'][$i]['adj'][$j]['amount'] = $seg[$k+
1];
388 // Note: $seg[$k+2] is "quantity". A value here indicates a change to
389 // the number of units of service. We're ignoring that for now.
392 else if ($segid == 'REF' && $out['loopid'] == '2110') {
395 else if ($segid == 'AMT' && $seg[1] == 'B6' && $out['loopid'] == '2110') {
396 $i = count($out['svc']) - 1;
397 $out['svc'][$i]['allowed'] = $seg[2]; // report this amount as a note
399 else if ($segid == 'AMT' && $out['loopid'] == '2110') {
400 $out['warnings'] .= "$inline at service level ignored.\n";
402 else if ($segid == 'LQ' && $seg[1] == 'HE' && $out['loopid'] == '2110') {
403 $i = count($out['svc']) - 1;
404 $out['svc'][$i]['remark'] = $seg[2];
406 else if ($segid == 'QTY' && $out['loopid'] == '2110') {
407 $out['warnings'] .= "QTY segment at service level ignored.\n";
409 else if ($segid == 'PLB') {
410 // Provider-level adjustments are a General Ledger thing and should not
411 // alter the A/R for the claim, so we just report them as notes.
412 for ($k = 3; $k < 15; $k +
= 2) {
413 if (!$seg[$k]) break;
414 $out['warnings'] .= 'PROVIDER LEVEL ADJUSTMENT (not claim-specific): $' .
415 sprintf('%.2f', $seg[$k+
1]) . " with reason code " . $seg[$k] . "\n";
416 // Note: For PLB adjustment reason codes see IG pages 165-170.
419 else if ($segid == 'SE') {
420 parse_era_2100($out, $cb);
422 if ($out['st_control_number'] != trim($seg[2])) {
423 return 'Ending transaction set control number mismatch';
425 if (($out['st_segment_count'] +
1) != trim($seg[1])) {
426 return 'Ending transaction set segment count mismatch';
429 else if ($segid == 'GE') {
430 if ($out['loopid']) return 'Unexpected GE segment';
431 if ($out['gs_control_number'] != trim($seg[2])) {
432 return 'Ending functional group control number mismatch';
435 else if ($segid == 'IEA') {
436 if ($out['loopid']) return 'Unexpected IEA segment';
437 if ($out['isa_control_number'] != trim($seg[2])) {
438 return 'Ending interchange control number mismatch';
442 return "Unknown or unexpected segment ID $segid";
445 ++
$out['st_segment_count'];
448 if ($segid != 'IEA') return 'Premature end of ERA file';
451 //for getting the check details and provider details
452 function parse_era_for_check($filename) {
457 $infh = fopen($filename, 'r');
458 if (! $infh) return "ERA input file open failed";
462 $out['st_segment_count'] = 0;
468 if (strlen($buffer) < 2048 && ! feof($infh)) $buffer .= fread($infh, 2048);
469 $tpos = strpos($buffer, $delimiter1);
470 if ($tpos === false) break;
471 $inline = substr($buffer, 0, $tpos);
472 $buffer = substr($buffer, $tpos +
1);
474 // If this is the ISA segment then figure out what the delimiters are.
475 if ($segid === '' && substr($inline, 0, 3) === 'ISA') {
476 $delimiter2 = substr($inline, 3, 1);
477 $delimiter3 = substr($inline, -1);
480 $seg = explode($delimiter2, $inline);
483 if ($segid == 'ISA') {
486 else if ($segid == 'BPR') {
488 //if ($out['loopid']) return 'Unexpected BPR segment';
489 $out['check_amount'.$check_count] = trim($seg[2]);
490 $out['check_date'.$check_count] = trim($seg[16]); // yyyymmdd
491 // TBD: BPR04 is a payment method code.
494 else if ($segid == 'N1' && $seg[1] == 'PE') {
495 //if ($out['loopid'] != '1000A') return 'Unexpected N1|PE segment';
496 $out['loopid'] = '1000B';
497 $out['payee_name'.$check_count] = trim($seg[2]);
498 $out['payee_tax_id'.$check_count] = trim($seg[4]);
501 else if ($segid == 'TRN') {
502 //if ($out['loopid']) return 'Unexpected TRN segment';
503 $out['check_number'.$check_count] = trim($seg[2]);
504 $out['payer_tax_id'.$check_count] = substr($seg[3], 1); // 9 digits
505 $out['payer_id'.$check_count] = trim($seg[4]);
506 // Note: TRN04 further qualifies the paying entity within the
507 // organization identified by TRN03.
513 ++
$out['st_segment_count'];
515 $out['check_count']=$check_count;
516 era_callback_check($out);
518 if ($segid != 'IEA') return 'Premature end of ERA file';