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