Collection report bug fix and an internationalization issue fix
[openemr.git] / library / parse_era.inc.php
blobd40461ac7cd18aead264dda4022913ba02d59a18
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['svc'] = array();
216 // This is the poorly-named "Patient Account Number". For 837p
217 // it comes from CLM01 which we populated as pid-diagid-procid,
218 // where diagid and procid are id values from the billing table.
219 // For HCFA 1500 claims it comes from field 26 which we
220 // populated with our familiar pid-encounter billing key.
222 // The 835 spec calls this the "provider-assigned claim control
223 // number" and notes that it is specifically intended for
224 // identifying the claim in the provider's database.
225 $out['our_claim_id'] = trim($seg[1]);
227 $out['claim_status_code'] = trim($seg[2]);
228 $out['amount_charged'] = trim($seg[3]);
229 $out['amount_approved'] = trim($seg[4]);
230 $out['amount_patient'] = trim($seg[5]); // pt responsibility, copay + deductible
231 $out['payer_claim_id'] = trim($seg[7]); // payer's claim number
233 else if ($segid == 'CAS' && $out['loopid'] == '2100') {
234 // This is a claim-level adjustment and should be unusual.
235 // Handle it by creating a dummy zero-charge service item and
236 // then populating the adjustments into it. See also code in
237 // parse_era_2100() which will later plug in a payment reversal
238 // amount that offsets these adjustments.
239 $i = 0; // if present, the dummy service item will be first.
240 if (!$out['svc'][$i]) {
241 $out['svc'][$i] = array();
242 $out['svc'][$i]['code'] = 'Claim';
243 $out['svc'][$i]['mod'] = '';
244 $out['svc'][$i]['chg'] = '0';
245 $out['svc'][$i]['paid'] = '0';
246 $out['svc'][$i]['adj'] = array();
248 for ($k = 2; $k < 20; $k += 3) {
249 if (!$seg[$k]) break;
250 $j = count($out['svc'][$i]['adj']);
251 $out['svc'][$i]['adj'][$j] = array();
252 $out['svc'][$i]['adj'][$j]['group_code'] = $seg[1];
253 $out['svc'][$i]['adj'][$j]['reason_code'] = $seg[$k];
254 $out['svc'][$i]['adj'][$j]['amount'] = $seg[$k+1];
257 // QC = Patient
258 else if ($segid == 'NM1' && $seg[1] == 'QC' && $out['loopid'] == '2100') {
259 $out['patient_lname'] = trim($seg[3]);
260 $out['patient_fname'] = trim($seg[4]);
261 $out['patient_mname'] = trim($seg[5]);
262 $out['patient_member_id'] = trim($seg[9]);
264 // IL = Insured or Subscriber
265 else if ($segid == 'NM1' && $seg[1] == 'IL' && $out['loopid'] == '2100') {
266 $out['subscriber_lname'] = trim($seg[3]);
267 $out['subscriber_fname'] = trim($seg[4]);
268 $out['subscriber_mname'] = trim($seg[5]);
269 $out['subscriber_member_id'] = trim($seg[9]);
271 // 82 = Rendering Provider
272 else if ($segid == 'NM1' && $seg[1] == '82' && $out['loopid'] == '2100') {
273 $out['provider_lname'] = trim($seg[3]);
274 $out['provider_fname'] = trim($seg[4]);
275 $out['provider_mname'] = trim($seg[5]);
276 $out['provider_member_id'] = trim($seg[9]);
278 // 74 = Corrected Insured
279 // TT = Crossover Carrier (Transfer To another payer)
280 // PR = Corrected Payer
281 else if ($segid == 'NM1' && $out['loopid'] == '2100') {
282 // $out['warnings'] .= "NM1 segment at claim level ignored.\n";
284 else if ($segid == 'MOA' && $out['loopid'] == '2100') {
285 $out['warnings'] .= "MOA segment at claim level ignored.\n";
287 // REF segments may provide various identifying numbers, where REF02
288 // indicates the type of number.
289 else if ($segid == 'REF' && $seg[1] == '1W' && $out['loopid'] == '2100') {
290 $out['claim_comment'] = trim($seg[2]);
292 else if ($segid == 'REF' && $out['loopid'] == '2100') {
293 // ignore
295 else if ($segid == 'DTM' && $seg[1] == '050' && $out['loopid'] == '2100') {
296 $out['claim_date'] = trim($seg[2]); // yyyymmdd
298 // 036 = expiration date of coverage
299 // 050 = date claim received by payer
300 // 232 = claim statement period start
301 // 233 = claim statement period end
302 else if ($segid == 'DTM' && $out['loopid'] == '2100') {
303 // ignore?
305 else if ($segid == 'PER' && $out['loopid'] == '2100') {
307 $out['payer_insurance'] = trim($seg[2]);
308 $out['warnings'] .= 'Claim contact information: ' .
309 $seg[4] . "\n";
311 // For AMT01 see the Amount Qualifier Codes on pages 135-135 of the
312 // Implementation Guide. AMT is only good for comments and is not
313 // part of claim balancing.
314 else if ($segid == 'AMT' && $out['loopid'] == '2100') {
315 $out['warnings'] .= "AMT segment at claim level ignored.\n";
317 // For QTY01 see the Quantity Qualifier Codes on pages 137-138 of the
318 // Implementation Guide. QTY is only good for comments and is not
319 // part of claim balancing.
320 else if ($segid == 'QTY' && $out['loopid'] == '2100') {
321 $out['warnings'] .= "QTY segment at claim level ignored.\n";
324 // Loop 2110 is Service Payment Information.
326 else if ($segid == 'SVC') {
327 if (! $out['loopid']) return 'Unexpected SVC segment';
328 $out['loopid'] = '2110';
329 if ($seg[6]) {
330 // SVC06 if present is our original procedure code that they are changing.
331 // We will not put their crap in our invoice, but rather log a note and
332 // treat it as adjustments to our originally submitted coding.
333 $svc = explode($delimiter3, $seg[6]);
334 $tmp = explode($delimiter3, $seg[1]);
335 $out['warnings'] .= "Payer is restating our procedure " . $svc[1] .
336 " as " . $tmp[1] . ".\n";
337 } else {
338 $svc = explode($delimiter3, $seg[1]);
340 if ($svc[0] != 'HC') return 'SVC segment has unexpected qualifier';
341 // TBD: Other qualifiers are possible; see IG pages 140-141.
342 $i = count($out['svc']);
343 $out['svc'][$i] = array();
344 // It seems some payers append the modifier with no separator!
345 if (strlen($svc[1]) == 7 && empty($svc[2])) {
346 $out['svc'][$i]['code'] = substr($svc[1], 0, 5);
347 $out['svc'][$i]['mod'] = substr($svc[1], 5);
348 } else {
349 $out['svc'][$i]['code'] = $svc[1];
350 $out['svc'][$i]['mod'] = $svc[2] ? $svc[2] : '';
352 // TBD: There may be up to 4 procedure modifiers in $svc[2] thru [5].
353 $out['svc'][$i]['chg'] = $seg[2];
354 $out['svc'][$i]['paid'] = $seg[3];
355 $out['svc'][$i]['adj'] = array();
356 // Note: SVC05, if present, indicates the paid units of service.
357 // It defaults to 1.
359 // DTM01 identifies the type of service date:
360 // 472 = a single date of service
361 // 150 = service period start
362 // 151 = service period end
363 else if ($segid == 'DTM' && $out['loopid'] == '2110') {
364 $out['dos'] = trim($seg[2]); // yyyymmdd
366 else if ($segid == 'CAS' && $out['loopid'] == '2110') {
367 $i = count($out['svc']) - 1;
368 for ($k = 2; $k < 20; $k += 3) {
369 if (!$seg[$k]) break;
370 if ($seg[1] == 'CO' && $seg[$k+1] < 0) {
371 $out['warnings'] .= "Negative Contractual Obligation adjustment " .
372 "seems wrong. Inverting, but should be checked!\n";
373 $seg[$k+1] = 0 - $seg[$k+1];
375 $j = count($out['svc'][$i]['adj']);
376 $out['svc'][$i]['adj'][$j] = array();
377 $out['svc'][$i]['adj'][$j]['group_code'] = $seg[1];
378 $out['svc'][$i]['adj'][$j]['reason_code'] = $seg[$k];
379 $out['svc'][$i]['adj'][$j]['amount'] = $seg[$k+1];
380 // Note: $seg[$k+2] is "quantity". A value here indicates a change to
381 // the number of units of service. We're ignoring that for now.
384 else if ($segid == 'REF' && $out['loopid'] == '2110') {
385 // ignore
387 else if ($segid == 'AMT' && $seg[1] == 'B6' && $out['loopid'] == '2110') {
388 $i = count($out['svc']) - 1;
389 $out['svc'][$i]['allowed'] = $seg[2]; // report this amount as a note
391 else if ($segid == 'AMT' && $out['loopid'] == '2110') {
392 $out['warnings'] .= "$inline at service level ignored.\n";
394 else if ($segid == 'LQ' && $seg[1] == 'HE' && $out['loopid'] == '2110') {
395 $i = count($out['svc']) - 1;
396 $out['svc'][$i]['remark'] = $seg[2];
398 else if ($segid == 'QTY' && $out['loopid'] == '2110') {
399 $out['warnings'] .= "QTY segment at service level ignored.\n";
401 else if ($segid == 'PLB') {
402 // Provider-level adjustments are a General Ledger thing and should not
403 // alter the A/R for the claim, so we just report them as notes.
404 for ($k = 3; $k < 15; $k += 2) {
405 if (!$seg[$k]) break;
406 $out['warnings'] .= 'PROVIDER LEVEL ADJUSTMENT (not claim-specific): $' .
407 sprintf('%.2f', $seg[$k+1]) . " with reason code " . $seg[$k] . "\n";
408 // Note: For PLB adjustment reason codes see IG pages 165-170.
411 else if ($segid == 'SE') {
412 parse_era_2100($out, $cb);
413 $out['loopid'] = '';
414 if ($out['st_control_number'] != trim($seg[2])) {
415 return 'Ending transaction set control number mismatch';
417 if (($out['st_segment_count'] + 1) != trim($seg[1])) {
418 return 'Ending transaction set segment count mismatch';
421 else if ($segid == 'GE') {
422 if ($out['loopid']) return 'Unexpected GE segment';
423 if ($out['gs_control_number'] != trim($seg[2])) {
424 return 'Ending functional group control number mismatch';
427 else if ($segid == 'IEA') {
428 if ($out['loopid']) return 'Unexpected IEA segment';
429 if ($out['isa_control_number'] != trim($seg[2])) {
430 return 'Ending interchange control number mismatch';
433 else {
434 return "Unknown or unexpected segment ID $segid";
437 ++$out['st_segment_count'];
440 if ($segid != 'IEA') return 'Premature end of ERA file';
441 return '';
443 //for getting the check details and provider details
444 function parse_era_for_check($filename) {
445 $delimiter1 = '~';
446 $delimiter2 = '|';
447 $delimiter3 = '^';
449 $infh = fopen($filename, 'r');
450 if (! $infh) return "ERA input file open failed";
452 $out = array();
453 $out['loopid'] = '';
454 $out['st_segment_count'] = 0;
455 $buffer = '';
456 $segid = '';
457 $check_count=0;
458 while (true) {
460 if (strlen($buffer) < 2048 && ! feof($infh)) $buffer .= fread($infh, 2048);
461 $tpos = strpos($buffer, $delimiter1);
462 if ($tpos === false) break;
463 $inline = substr($buffer, 0, $tpos);
464 $buffer = substr($buffer, $tpos + 1);
466 // If this is the ISA segment then figure out what the delimiters are.
467 if ($segid === '' && substr($inline, 0, 3) === 'ISA') {
468 $delimiter2 = substr($inline, 3, 1);
469 $delimiter3 = substr($inline, -1);
472 $seg = explode($delimiter2, $inline);
473 $segid = $seg[0];
475 if ($segid == 'ISA') {
478 else if ($segid == 'BPR') {
479 ++$check_count;
480 //if ($out['loopid']) return 'Unexpected BPR segment';
481 $out['check_amount'.$check_count] = trim($seg[2]);
482 $out['check_date'.$check_count] = trim($seg[16]); // yyyymmdd
483 // TBD: BPR04 is a payment method code.
486 else if ($segid == 'N1' && $seg[1] == 'PE') {
487 //if ($out['loopid'] != '1000A') return 'Unexpected N1|PE segment';
488 $out['loopid'] = '1000B';
489 $out['payee_name'.$check_count] = trim($seg[2]);
490 $out['payee_tax_id'.$check_count] = trim($seg[4]);
493 else if ($segid == 'TRN') {
494 //if ($out['loopid']) return 'Unexpected TRN segment';
495 $out['check_number'.$check_count] = trim($seg[2]);
496 $out['payer_tax_id'.$check_count] = substr($seg[3], 1); // 9 digits
497 $out['payer_id'.$check_count] = trim($seg[4]);
498 // Note: TRN04 further qualifies the paying entity within the
499 // organization identified by TRN03.
505 ++$out['st_segment_count'];
507 $out['check_count']=$check_count;
508 era_callback_check($out);
510 if ($segid != 'IEA') return 'Premature end of ERA file';
511 return '';