ERA Posting additional bug fix.
[openemr.git] / library / gen_x12_837.inc.php
blob19dd07cc64dfdfd4b064725cc37acb774d56d51c
1 <?php
2 // Copyright (C) 2007-2011 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 require_once("Claim.class.php");
10 function stripZipCode($zip)
12 return str_replace('-','',$zip);
14 function gen_x12_837($pid, $encounter, &$log, $encounter_claim=false) {
16 $today = time();
17 $out = '';
18 $claim = new Claim($pid, $encounter);
19 $edicount = 0;
21 // This is true for the 5010 standard, false for 4010.
22 // x12gsversionstring() should be "005010X222A1" or "004010X098A1".
23 $CMS_5010 = strpos($claim->x12gsversionstring(), '5010') !== false;
25 $log .= "Generating claim $pid-$encounter for " .
26 $claim->patientFirstName() . ' ' .
27 $claim->patientMiddleName() . ' ' .
28 $claim->patientLastName() . ' on ' .
29 date('Y-m-d H:i', $today) . ".\n";
31 $out .= "ISA" .
32 "*00" .
33 "* " .
34 "*00" .
35 "* " .
36 "*" . $claim->x12gsisa05() .
37 "*" . $claim->x12gssenderid() .
38 "*" . $claim->x12gsisa07() .
39 "*" . $claim->x12gsreceiverid() .
40 "*030911" .
41 "*1630" .
42 "*" . ($CMS_5010 ? "^" : "U" ) .
43 "*" . ($CMS_5010 ? "00501" : "00401") .
44 "*000000001" .
45 "*" . $claim->x12gsisa14() .
46 "*" . $claim->x12gsisa15() .
47 "*:" .
48 "~\n";
50 $out .= "GS" .
51 "*HC" .
52 "*" . $claim->x12gsgs02() .
53 "*" . trim($claim->x12gs03()) .
54 "*" . date('Ymd', $today) .
55 "*" . date('Hi', $today) .
56 "*1" .
57 "*X" .
58 "*" . $claim->x12gsversionstring() .
59 "~\n";
61 ++$edicount;
62 $out .= "ST" .
63 "*837" .
64 "*0021" .
65 // Spec says the following is optional, so should be able to leave it out.
66 ($CMS_5010 ? ("*" . $claim->x12gsversionstring()) : "") .
67 "~\n";
69 ++$edicount;
70 $out .= "BHT" .
71 "*0019" . // 0019 is required here
72 "*00" . // 00 = original transmission
73 "*0123" . // reference identification
74 "*" . date('Ymd', $today) . // transaction creation date
75 "*" . date('Hi', $today) . // transaction creation time
76 ($encounter_claim ? "*RP" : "*CH") . // RP = reporting, CH = chargeable
77 "~\n";
79 if (!$CMS_5010) {
80 // This segment was deleted for 5010.
81 ++$edicount;
82 $out .= "REF" .
83 "*87" .
84 "*" . $claim->x12gsversionstring() .
85 "~\n";
88 ++$edicount;
89 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
90 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
91 $out .= "NM1" . // Loop 1000A Submitter
92 "*41" .
93 "*2" .
94 "*" . $billingFacilityName .
95 "*" .
96 "*" .
97 "*" .
98 "*" .
99 "*46";
100 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
101 $out .= "*" . $claim->clearingHouseETIN();
102 } else {
103 $out .= "*" . $claim->billingFacilityETIN();
105 $out .= "~\n";
107 ++$edicount;
108 $out .= "PER" .
109 "*IC" .
110 "*" . $claim->billingContactName() .
111 "*TE" .
112 "*" . $claim->billingContactPhone();
113 if (!$CMS_5010 && $claim->x12gsper06()) {
114 $out .= "*ED*" . $claim->x12gsper06();
116 $out .= "~\n";
118 ++$edicount;
119 $out .= "NM1" . // Loop 1000B Receiver
120 "*40" .
121 "*2" .
122 "*" . $claim->clearingHouseName() .
123 "*" .
124 "*" .
125 "*" .
126 "*" .
127 "*46" .
128 "*" . $claim->clearingHouseETIN() .
129 "~\n";
131 $HLcount = 1;
133 ++$edicount;
134 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
135 "*$HLcount" .
136 "*" .
137 "*20" .
138 "*1" . // 1 indicates there are child segments
139 "~\n";
141 $HLBillingPayToProvider = $HLcount++;
143 // Situational PRV segment (for provider taxonomy code) omitted here.
144 // Situational CUR segment (foreign currency information) omitted here.
146 ++$edicount;
147 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
148 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
149 $out .= "NM1" . // Loop 2010AA Billing Provider
150 "*85" .
151 "*2" .
152 "*" . $billingFacilityName .
153 "*" .
154 "*" .
155 "*" .
156 "*";
157 if ($claim->billingFacilityNPI()) {
158 $out .= "*XX*" . $claim->billingFacilityNPI();
160 else {
161 $log .= "*** Billing facility has no NPI.\n";
162 if ($CMS_5010) {
163 $out .= "*XX*";
165 else {
166 $out .= "*24*" . $claim->billingFacilityETIN();
169 $out .= "~\n";
171 ++$edicount;
172 $out .= "N3" .
173 "*" . $claim->billingFacilityStreet() .
174 "~\n";
176 ++$edicount;
177 $out .= "N4" .
178 "*" . $claim->billingFacilityCity() .
179 "*" . $claim->billingFacilityState() .
180 "*" . stripZipCode($claim->billingFacilityZip()) .
181 "~\n";
183 if ($CMS_5010 || ($claim->billingFacilityNPI() && $claim->billingFacilityETIN())) {
184 ++$edicount;
185 $out .= "REF" ;
186 if($claim->federalIdType()){
187 $out .= "*" . $claim->federalIdType();
189 else{
190 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
192 $out .= "*" . $claim->billingFacilityETIN() .
193 "~\n";
196 if ($claim->providerNumberType() && $claim->providerNumber() &&
197 !($CMS_5010 && $claim->billingFacilityNPI()))
199 ++$edicount;
200 $out .= "REF" .
201 "*" . $claim->providerNumberType() .
202 "*" . $claim->providerNumber() .
203 "~\n";
205 else if ($claim->providerNumber() && !$claim->providerNumberType()) {
206 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
209 // Situational PER*1C segment omitted.
211 // Pay-To Address defaults to billing provider and is no longer required in 5010.
212 if (!$CMS_5010) {
213 ++$edicount;
214 // Field length is limited to 35. See nucc dataset page 63 www.nucc.org
215 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
216 $out .= "NM1" . // Loop 2010AB Pay-To Provider
217 "*87" .
218 "*2" .
219 "*" . $billingFacilityName .
220 "*" .
221 "*" .
222 "*" .
223 "*";
224 if ($claim->billingFacilityNPI())
225 $out .= "*XX*" . $claim->billingFacilityNPI();
226 else
227 $out .= "*24*" . $claim->billingFacilityETIN();
228 $out .= "~\n";
230 ++$edicount;
231 $out .= "N3" .
232 "*" . $claim->billingFacilityStreet() .
233 "~\n";
235 ++$edicount;
236 $out .= "N4" .
237 "*" . $claim->billingFacilityCity() .
238 "*" . $claim->billingFacilityState() .
239 "*" . stripZipCode($claim->billingFacilityZip()) .
240 "~\n";
242 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
243 ++$edicount;
244 $out .= "REF" .
245 "*EI" .
246 "*" . $claim->billingFacilityETIN() .
247 "~\n";
251 // Loop 2010AC Pay-To Plan Name omitted. Includes:
252 // NM1*PE, N3, N4, REF*2U, REF*EI
254 $PatientHL = $claim->isSelfOfInsured() ? 0 : 1;
255 $HLSubscriber = $HLcount++;
257 ++$edicount;
258 $out .= "HL" . // Loop 2000B Subscriber HL Loop
259 "*$HLSubscriber" .
260 "*$HLBillingPayToProvider" .
261 "*22" .
262 "*$PatientHL" .
263 "~\n";
265 if (!$claim->payerSequence()) {
266 $log .= "*** Error: Insurance information is missing!\n";
269 ++$edicount;
270 $out .= "SBR" . // Subscriber Information
271 "*" . $claim->payerSequence() .
272 "*" . ($claim->isSelfOfInsured() ? '18' : '') .
273 "*" . $claim->groupNumber() .
274 "*" . (($CMS_5010 && $claim->groupNumber()) ? '' : $claim->groupName()) .
275 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
276 "*" .
277 "*" .
278 "*" .
279 "*" . $claim->claimType() . // Zirmed replaces this
280 "~\n";
282 // Segment PAT omitted.
284 ++$edicount;
285 $out .= "NM1" . // Loop 2010BA Subscriber
286 "*IL" .
287 "*1" . // 1 = person, 2 = non-person
288 "*" . $claim->insuredLastName() .
289 "*" . $claim->insuredFirstName() .
290 "*" . $claim->insuredMiddleName() .
291 "*" .
292 "*" . // Name Suffix
293 "*MI" .
294 // "MI" = Member Identification Number
295 // "II" = Standard Unique Health Identifier, "Required if the
296 // HIPAA Individual Patient Identifier is mandated use."
297 // Here we presume that is not true yet.
298 "*" . $claim->policyNumber() .
299 "~\n";
301 // For 5010, further subscriber info is sent only if they are the patient.
302 if (!$CMS_5010 || $claim->isSelfOfInsured()) {
303 ++$edicount;
304 $out .= "N3" .
305 "*" . $claim->insuredStreet() .
306 "~\n";
308 ++$edicount;
309 $out .= "N4" .
310 "*" . $claim->insuredCity() .
311 "*" . $claim->insuredState() .
312 "*" . stripZipCode($claim->insuredZip()) .
313 "~\n";
315 ++$edicount;
316 $out .= "DMG" .
317 "*D8" .
318 "*" . $claim->insuredDOB() .
319 "*" . $claim->insuredSex() .
320 "~\n";
323 // Segment REF*SY (Subscriber Secondary Identification) omitted.
324 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
325 // Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
327 ++$edicount;
328 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
329 $payerName = substr($claim->payerName(), 0, $CMS_5010 ? 60 : 35);
330 $out .= "NM1" . // Loop 2010BB Payer
331 "*PR" .
332 "*2" .
333 "*" . $payerName .
334 "*" .
335 "*" .
336 "*" .
337 "*" .
338 // The 5010 spec says:
339 // "On or after the mandated implementation date for the HIPAA
340 // National Plan Identifier (National Plan ID), XV must be sent.
341 // Prior to the mandated implementation date and prior to any phase-
342 // in period identified by Federal regulation, PI must be sent."
343 // *************** Anybody know what that date is? ***************
344 "*PI" .
345 // Zirmed ignores this if using payer name matching:
346 "*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
347 "~\n";
349 // if (!$claim->payerID()) {
350 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
351 // }
353 if (true) { // !$CMS_5010
354 // The 5010 spec says:
355 // "Required when the payer address is available and the submitter intends
356 // for the claim to be printed on paper at the next EDI location (for example, a
357 // clearinghouse). If not required by this implementation guide, do not send."
359 ++$edicount;
360 $out .= "N3" .
361 "*" . $claim->payerStreet() .
362 "~\n";
364 ++$edicount;
365 $out .= "N4" .
366 "*" . $claim->payerCity() .
367 "*" . $claim->payerState() .
368 "*" . stripZipCode($claim->payerZip()) .
369 "~\n";
372 // Segment REF (Payer Secondary Identification) omitted.
373 // Segment REF (Billing Provider Secondary Identification) omitted.
375 if (! $claim->isSelfOfInsured()) {
376 ++$edicount;
377 $out .= "HL" . // Loop 2000C Patient Information
378 "*$HLcount" .
379 "*$HLSubscriber" .
380 "*23" .
381 "*0" .
382 "~\n";
384 $HLcount++;
386 ++$edicount;
387 $out .= "PAT" .
388 "*" . $claim->insuredRelationship() .
389 "~\n";
391 ++$edicount;
392 $out .= "NM1" . // Loop 2010CA Patient
393 "*QC" .
394 "*1" .
395 "*" . $claim->patientLastName() .
396 "*" . $claim->patientFirstName() .
397 "*" . $claim->patientMiddleName() .
398 "~\n";
400 ++$edicount;
401 $out .= "N3" .
402 "*" . $claim->patientStreet() .
403 "~\n";
405 ++$edicount;
406 $out .= "N4" .
407 "*" . $claim->patientCity() .
408 "*" . $claim->patientState() .
409 "*" . stripZipCode($claim->patientZip()) .
410 "~\n";
412 ++$edicount;
413 $out .= "DMG" .
414 "*D8" .
415 "*" . $claim->patientDOB() .
416 "*" . $claim->patientSex() .
417 "~\n";
419 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
420 // Segment REF (Property and Casualty Patient Identifier) omitted.
421 // Segment PER (Property and Casualty Patient Contact Information) omitted.
423 } // end of patient different from insured
425 $proccount = $claim->procCount();
427 $clm_total_charges = 0;
428 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
429 $clm_total_charges += $claim->cptCharges($prockey);
432 if (!$clm_total_charges) {
433 $log .= "*** This claim has no charges!\n";
436 ++$edicount;
437 $out .= "CLM" . // Loop 2300 Claim
438 "*$pid-$encounter" .
439 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
440 "*" .
441 "*" .
442 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" .
443 ($CMS_5010 ? "B" : "") . ":" .
444 $claim->frequencyTypeCode() . // Changed to correct single digit output
445 "*Y" .
446 "*A" .
447 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
448 "*Y" .
449 ($CMS_5010 ? "" : "*C") .
450 "~\n";
452 if ($claim->onsetDate() &&
453 ($claim->onsetDate()!== $claim->serviceDate()) &&
454 ($claim->onsetDateValid())
456 ++$edicount;
457 $out .= "DTP" . // Date of Onset
458 "*431" .
459 "*D8" .
460 "*" . $claim->onsetDate() .
461 "~\n";
464 if ($claim->dateInitialTreatment() && ($claim->onsetDateValid())) {
465 ++$edicount;
466 $out .= "DTP" . // Date of Initial Treatment
467 "*454" .
468 "*D8" .
469 "*" . $claim->dateInitialTreatment() .
470 "~\n";
473 // Segment DTP*304 (Last Seen Date) omitted.
474 // Segment DTP*453 (Acute Manifestation Date) omitted.
475 // Segment DTP*439 (Accident Date) omitted.
476 // Segment DTP*484 (Last Menstrual Period Date) omitted.
477 // Segment DTP*455 (Last X-Ray Date) omitted.
478 // Segment DTP*471 (Hearing and Vision Prescription Date) omitted.
479 // Segments DTP (Disability Dates) omitted.
480 // Segment DTP*297 (Last Worked Date) omitted.
481 // Segment DTP*296 (Authorized Return to Work Date) omitted.
483 if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->onsetDateValid() ) {
484 ++$edicount;
485 $out .= "DTP" . // Date of Hospitalization
486 "*435" .
487 "*D8" .
488 "*" . $claim->onsetDate() .
489 "~\n";
492 // Segment DTP*096 (Discharge Date) omitted.
493 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
494 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
495 // Segment DTP*050 (Repricer Received Date) omitted.
496 // Segment PWK (Claim Supplemental Information) omitted.
497 // Segment CN1 (Contract Information) omitted.
499 $patientpaid = $claim->patientPaidAmount();
500 if ($patientpaid != 0) {
501 ++$edicount;
502 $out .= "AMT" . // Patient paid amount. Page 190/220.
503 "*F5" .
504 "*" . $patientpaid .
505 "~\n";
508 // Segment REF*4N (Service Authorization Exception Code) omitted.
509 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
510 // Segment REF*EW (Mammography Certification Number) omitted.
511 // Segment REF*9F (Referral Number) omitted.
513 if ($claim->priorAuth()) {
514 ++$edicount;
515 $out .= "REF" . // Prior Authorization Number
516 "*G1" .
517 "*" . $claim->priorAuth() .
518 "~\n";
521 // Segment REF*F8 (Payer Claim Control Number) omitted.
523 if ($claim->cliaCode() && ($CMS_5010 || $claim->claimType() === 'MB')) {
524 // Required by Medicare when in-house labs are done.
525 ++$edicount;
526 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
527 "*X4" .
528 "*" . $claim->cliaCode() .
529 "~\n";
532 // Segment REF*9A (Repriced Claim Number) omitted.
533 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
534 // Segment REF*LX (Investigational Device Exemption Number) omitted.
535 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
536 // Segment REF*EA (Medical Record Number) omitted.
537 // Segment REF*P4 (Demonstration Project Identifier) omitted.
538 // Segment REF*1J (Care Plan Oversight) omitted.
539 // Segment K3 (File Information) omitted.
541 if ($claim->additionalNotes()) {
542 // Claim note.
543 ++$edicount;
544 $out .= "NTE" . // comments box 19
545 "*" . ($CMS_5010 ? "ADD" : "") .
546 "*" . $claim->additionalNotes() .
547 "~\n";
550 // Segment CR1 (Ambulance Transport Information) omitted.
551 // Segment CR2 (Spinal Manipulation Service Information) omitted.
552 // Segment CRC (Ambulance Certification) omitted.
553 // Segment CRC (Patient Condition Information: Vision) omitted.
554 // Segment CRC (Homebound Indicator) omitted.
555 // Segment CRC (EPSDT Referral) omitted.
557 // Diagnoses, up to $max_per_seg per HI segment.
558 $max_per_seg = $CMS_5010 ? 12 : 8;
559 $da = $claim->diagArray();
560 $diag_type_code = 'BK';
561 $tmp = 0;
562 foreach ($da as $diag) {
563 if ($tmp % $max_per_seg == 0) {
564 if ($tmp) $out .= "~\n";
565 ++$edicount;
566 $out .= "HI"; // Health Diagnosis Codes
568 $out .= "*$diag_type_code:" . $diag;
569 $diag_type_code = 'BF';
570 ++$tmp;
572 if ($tmp) $out .= "~\n";
574 // Segment HI*BP (Anesthesia Related Procedure) omitted.
575 // Segment HI*BG (Condition Information) omitted.
576 // Segment HCP (Claim Pricing/Repricing Information) omitted.
578 if ($claim->referrerLastName()) {
579 // Medicare requires referring provider's name and UPIN.
580 ++$edicount;
581 $out .= "NM1" . // Loop 2310A Referring Provider
582 "*DN" .
583 "*1" .
584 "*" . $claim->referrerLastName() .
585 "*" . $claim->referrerFirstName() .
586 "*" . $claim->referrerMiddleName() .
587 "*" .
588 "*";
589 if ($CMS_5010 || $claim->referrerNPI()) { $out .=
590 "*XX" .
591 "*" . $claim->referrerNPI();
592 } else { $out .=
593 "*34" . // not allowed for 5010
594 "*" . $claim->referrerSSN();
596 $out .= "~\n";
598 if (!$CMS_5010 && $claim->referrerTaxonomy()) {
599 ++$edicount;
600 $out .= "PRV" .
601 "*RF" . // ReFerring provider
602 "*ZZ" .
603 "*" . $claim->referrerTaxonomy() .
604 "~\n";
607 if (!CMS_5010 && $claim->referrerUPIN()) {
608 ++$edicount;
609 $out .= "REF" . // Referring Provider Secondary Identification
610 "*1G" .
611 "*" . $claim->referrerUPIN() .
612 "~\n";
617 /* Per the implementation guide lines, only include this information if it is different
618 * than the Loop 2010AA information
620 if(!$CMS_5010 ||
621 ($claim->providerNPIValid() &&
622 $claim->billingFacilityNPI() !== $claim->providerNPI() ))
624 ++$edicount;
625 $out .= "NM1" . // Loop 2310B Rendering Provider
626 "*82" .
627 "*1" .
628 "*" . $claim->providerLastName() .
629 "*" . $claim->providerFirstName() .
630 "*" . $claim->providerMiddleName() .
631 "*" .
632 "*";
633 if ($CMS_5010 || $claim->providerNPI()) { $out .=
634 "*XX" .
635 "*" . $claim->providerNPI();
636 } else { $out .=
637 "*34" . // not allowed for 5010
638 "*" . $claim->providerSSN();
639 $log .= "*** Rendering provider has no NPI.\n";
641 $out .= "~\n";
643 if ($claim->providerTaxonomy()) {
644 ++$edicount;
645 $out .= "PRV" .
646 "*PE" . // PErforming provider
647 "*" . ($CMS_5010 ? "PXC" : "ZZ") .
648 "*" . $claim->providerTaxonomy() .
649 "~\n";
651 // End of Loop 2310B
653 else
655 // This loop can only get skipped if we are generating a 5010 claim
656 if(!($claim->providerNPIValid()))
658 /* If the loop was skipped because the provider NPI was invalid, generate
659 * a warning for the log.*/
660 $log.="*** Skipping 2310B because ".$claim->providerLastName() ."," . $claim->providerFirstName() . " has invalid NPI.\n";
662 /* Skipping this segment because the providerNPI and the billingFacilityNPI are identical
663 * is a normal condition, so no need to warn.
668 // 4010: REF*1C is required here for the Medicare provider number if NPI was
669 // specified in NM109. Not sure if other payers require anything here.
670 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
672 // 5010 spec says nothing here if NPI was specified.
674 if (($CMS_5010 && !$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B','1G','G2','LU')))
675 || (!$CMS_5010 && trim($claim->x12gsreceiverid()) != '470819582')) // if NOT ECLAIMS EDI
677 if ($claim->providerNumber()) {
678 ++$edicount;
679 $out .= "REF" .
680 "*" . $claim->providerNumberType() .
681 "*" . $claim->providerNumber() .
682 "~\n";
686 // Loop 2310D is omitted in the case of home visits (POS=12).
687 if ($claim->facilityPOS() != 12 &&
688 (!$CMS_5010 || $claim->facilityNPI() != $claim->billingFacilityNPI()))
690 ++$edicount;
691 $out .= "NM1" . // Loop 2310D Service Location
692 "*77" .
693 "*2";
694 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
695 $facilityName = substr($claim->facilityName(), 0, $CMS_5010 ? 60 : 35);
696 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
697 "*" . $facilityName;
699 if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
700 "*" .
701 "*" .
702 "*" .
703 "*";
704 if ($CMS_5010 || $claim->facilityNPI()) { $out .=
705 "*XX*" . $claim->facilityNPI();
706 } else { $out .=
707 "*24*" . $claim->facilityETIN();
709 if (!$claim->facilityNPI()) {
710 $log .= "*** Service location has no NPI.\n";
713 $out .= "~\n";
714 if ($claim->facilityStreet()) {
715 ++$edicount;
716 $out .= "N3" .
717 "*" . $claim->facilityStreet() .
718 "~\n";
720 if ($claim->facilityState()) {
721 ++$edicount;
722 $out .= "N4" .
723 "*" . $claim->facilityCity() .
724 "*" . $claim->facilityState() .
725 "*" . stripZipCode($claim->facilityZip()) .
726 "~\n";
730 // Segment REF (Service Facility Location Secondary Identification) omitted.
731 // Segment PER (Service Facility Contact Information) omitted.
733 // Loop 2310E, Supervising Provider
735 if ($claim->supervisorLastName()) {
736 ++$edicount;
737 $out .= "NM1" .
738 "*DQ" . // Supervising Physician
739 "*1" . // Person
740 "*" . $claim->supervisorLastName() .
741 "*" . $claim->supervisorFirstName() .
742 "*" . $claim->supervisorMiddleName() .
743 "*" . // NM106 not used
744 "*"; // Name Suffix
745 if ($CMS_5010 || $claim->supervisorNPI()) { $out .=
746 "*XX" .
747 "*" . $claim->supervisorNPI();
748 } else { $out .=
749 "*34" .
750 "*" . $claim->supervisorSSN();
752 if (!$claim->supervisorNPI()) {
753 $log .= "*** Supervising Provider has no NPI.\n";
755 $out .= "~\n";
757 if ($claim->supervisorNumber()) {
758 ++$edicount;
759 $out .= "REF" .
760 "*" . $claim->supervisorNumberType() .
761 "*" . $claim->supervisorNumber() .
762 "~\n";
766 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
767 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
769 $prev_pt_resp = $clm_total_charges; // for computation below
771 // Loops 2320 and 2330*, other subscriber/payer information.
772 // Remember that insurance index 0 is always for the payer being billed
773 // by this claim, and 1 and above are always for the "other" payers.
775 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
777 $tmp1 = $claim->claimType($ins);
778 $tmp2 = 'C1'; // Here a kludge. See page 321.
779 if ($tmp1 === 'CI') $tmp2 = 'C1';
780 if ($tmp1 === 'AM') $tmp2 = 'AP';
781 if ($tmp1 === 'HM') $tmp2 = 'HM';
782 if ($tmp1 === 'MB') $tmp2 = 'MB';
783 if ($tmp1 === 'MC') $tmp2 = 'MC';
784 if ($tmp1 === '09') $tmp2 = 'PP';
785 ++$edicount;
786 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
787 "*" . $claim->payerSequence($ins) .
788 "*" . $claim->insuredRelationship($ins) .
789 "*" . $claim->groupNumber($ins) .
790 "*" . (($CMS_5010 && $claim->groupNumber($ins)) ? '' : $claim->groupName($ins)) .
791 "*" . ($CMS_5010 ? $claim->insuredTypeCode($ins) : $tmp2) .
792 "*" .
793 "*" .
794 "*" .
795 "*" . $claim->claimType($ins) .
796 "~\n";
798 // Things that apply only to previous payers, not future payers.
800 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
802 // Generate claim-level adjustments.
803 $aarr = $claim->payerAdjustments($ins);
804 foreach ($aarr as $a) {
805 ++$edicount;
806 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
807 "*" . $a[1] .
808 "*" . $a[2] .
809 "*" . $a[3] .
810 "~\n";
813 $payerpaid = $claim->payerTotals($ins);
814 ++$edicount;
815 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
816 "*D" .
817 "*" . $payerpaid[1] .
818 "~\n";
820 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
821 // Segment AMT*EAF (Remaining Patient Liability) omitted.
823 if (!$CMS_5010) {
824 // Patient responsibility amount as of this previous payer.
825 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
826 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
828 ++$edicount;
829 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
830 "*B6" .
831 "*" . sprintf('%.2f', $payerpaid[1] + $prev_pt_resp) .
832 "~\n";
834 ++$edicount;
835 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
836 "*F2" .
837 "*" . sprintf('%.2f', $prev_pt_resp) .
838 "~\n";
840 } // End of things that apply only to previous payers.
842 if (!$CMS_5010) {
843 ++$edicount;
844 $out .= "DMG" . // Other subscriber demographic information. Page 342.
845 "*D8" .
846 "*" . $claim->insuredDOB($ins) .
847 "*" . $claim->insuredSex($ins) .
848 "~\n";
851 ++$edicount;
852 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
853 "*" .
854 "*" .
855 "*" . ($claim->billingFacilityAssignment($ins) ? 'Y' : 'N') .
856 // For this next item, the 5010 example in the spec does not match its
857 // description. So this might be wrong.
858 "*" . ($CMS_5010 ? '' : 'B') .
859 "*" .
860 "*Y" .
861 "~\n";
863 // Segment MOA (Medicare Outpatient Adjudication) omitted.
865 ++$edicount;
866 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
867 "*IL" .
868 "*1" .
869 "*" . $claim->insuredLastName($ins) .
870 "*" . $claim->insuredFirstName($ins) .
871 "*" . $claim->insuredMiddleName($ins) .
872 "*" .
873 "*" .
874 "*MI" .
875 "*" . $claim->policyNumber($ins) .
876 "~\n";
878 ++$edicount;
879 $out .= "N3" .
880 "*" . $claim->insuredStreet($ins) .
881 "~\n";
883 ++$edicount;
884 $out .= "N4" .
885 "*" . $claim->insuredCity($ins) .
886 "*" . $claim->insuredState($ins) .
887 "*" . stripZipCode($claim->insuredZip($ins)) .
888 "~\n";
890 // Segment REF (Other Subscriber Secondary Identification) omitted.
892 ++$edicount;
893 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
894 $payerName = substr($claim->payerName($ins), 0, $CMS_5010 ? 60 : 35);
895 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
896 "*PR" .
897 "*2" .
898 "*" . $payerName .
899 "*" .
900 "*" .
901 "*" .
902 "*" .
903 "*PI" .
904 "*" . $claim->payerID($ins) .
905 "~\n";
907 // if (!$claim->payerID($ins)) {
908 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
909 // }
911 // Payer address (N3 and N4) are added below so that Gateway EDI can
912 // auto-generate secondary claims. These do NOT appear in my copy of
913 // the spec! -- Rod 2008-06-12
915 if ($CMS_5010 || trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
916 ++$edicount;
917 $out .= "N3" .
918 "*" . $claim->payerStreet($ins) .
919 "~\n";
921 ++$edicount;
922 $out .= "N4" .
923 "*" . $claim->payerCity($ins) .
924 "*" . $claim->payerState($ins) .
925 "*" . stripZipCode($claim->payerZip($ins)) .
926 "~\n";
927 } // end Gateway EDI
929 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
930 // Segment REF (Other Payer Secondary Identifier) omitted.
931 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
932 // Segment REF*9F (Other Payer Referral Number) omitted.
933 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
934 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
935 // Segment NM1 (Other Payer Referring Provider) omitted.
936 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
937 // Segment NM1 (Other Payer Rendering Provider) omitted.
938 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
939 // Segment NM1 (Other Payer Service Facility Location) omitted.
940 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
941 // Segment NM1 (Other Payer Supervising Provider) omitted.
942 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
943 // Segment NM1 (Other Payer Billing Provider) omitted.
944 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
946 } // End loops 2320/2330*.
948 $loopcount = 0;
950 // Procedure loop starts here.
952 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
953 ++$loopcount;
955 ++$edicount;
956 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
957 "*$loopcount" .
958 "~\n";
960 ++$edicount;
961 $out .= "SV1" . // Professional Service. Page 400.
962 "*HC:" . $claim->cptKey($prockey) .
963 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
964 "*UN" .
965 "*" . $claim->cptUnits($prockey) .
966 "*" .
967 "*" .
968 "*";
969 $dia = $claim->diagIndexArray($prockey);
970 $i = 0;
971 foreach ($dia as $dindex) {
972 if ($i) $out .= ':';
973 $out .= $dindex;
974 if (++$i >= 4) break;
976 $out .= "~\n";
978 if (!$claim->cptCharges($prockey)) {
979 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
982 if (empty($dia)) {
983 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
986 // Segment SV5 (Durable Medical Equipment Service) omitted.
987 // Segment PWK (Line Supplemental Information) omitted.
988 // Segment PWK (Durable Medical Equipment Certificate of Medical Necessity Indicator) omitted.
989 // Segment CR1 (Ambulance Transport Information) omitted.
990 // Segment CR3 (Durable Medical Equipment Certification) omitted.
991 // Segment CRC (Ambulance Certification) omitted.
992 // Segment CRC (Hospice Employee Indicator) omitted.
993 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
995 ++$edicount;
996 $out .= "DTP" . // Date of Service. Page 435.
997 "*472" .
998 "*D8" .
999 "*" . $claim->serviceDate() .
1000 "~\n";
1002 $testnote = rtrim($claim->cptNotecodes($prockey));
1003 if (!empty($testnote)) {
1004 ++$edicount;
1005 $out .= "NTE" . // Explain Unusual Circumstances.
1006 "*ADD" .
1007 "*" . $claim->cptNotecodes($prockey) .
1008 "~\n";
1011 // Segment DTP*471 (Prescription Date) omitted.
1012 // Segment DTP*607 (Revision/Recertification Date) omitted.
1013 // Segment DTP*463 (Begin Therapy Date) omitted.
1014 // Segment DTP*461 (Last Certification Date) omitted.
1015 // Segment DTP*304 (Last Seen Date) omitted.
1016 // Segment DTP (Test Date) omitted.
1017 // Segment DTP*011 (Shipped Date) omitted.
1018 // Segment DTP*455 (Last X-Ray Date) omitted.
1019 // Segment DTP*454 (Initial Treatment Date) omitted.
1020 // Segment QTY (Ambulance Patient Count) omitted.
1021 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1022 // Segment MEA (Test Result) omitted.
1023 // Segment CN1 (Contract Information) omitted.
1024 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1025 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1026 // Segment REF*G1 (Prior Authorization) omitted.
1027 // Segment REF*6R (Line Item Control Number) omitted.
1028 // (Really oughta have this for robust 835 posting!)
1029 // Segment REF*EW (Mammography Certification Number) omitted.
1030 // Segment REF*X4 (CLIA Number) omitted.
1031 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1032 // Segment REF*BT (Immunization Batch Number) omitted.
1033 // Segment REF*9F (Referral Number) omitted.
1034 // Segment AMT*T (Sales Tax Amount) omitted.
1035 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1036 // Segment K3 (File Information) omitted.
1037 // Segment NTE (Line Note) omitted.
1038 // Segment NTE (Third Party Organization Notes) omitted.
1039 // Segment PS1 (Purchased Service Information) omitted.
1040 // Segment HCP (Line Pricing/Repricing Information) omitted.
1042 if (!$CMS_5010) {
1043 // This segment was deleted for 5010.
1045 // AMT*AAE segment for Approved Amount from previous payer.
1046 // Medicare secondaries seem to require this.
1048 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
1049 if ($claim->payerSequence($ins) > $claim->payerSequence())
1050 continue; // payer is future, not previous
1051 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1052 ++$edicount;
1053 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1054 "*AAE" .
1055 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1056 "~\n";
1057 break;
1061 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1062 // with HCPCS codes.
1064 $ndc = $claim->cptNDCID($prockey);
1065 if ($ndc) {
1066 ++$edicount;
1067 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1068 "*" . // Per addendum, LIN01 is not used.
1069 "*N4" .
1070 "*" . $ndc .
1071 "~\n";
1073 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1074 $log .= "*** NDC code '$ndc' has invalid format!\n";
1077 ++$edicount;
1078 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1079 if (!$tmpunits) $tmpunits = 1;
1080 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1081 "*" .
1082 "*" .
1083 "*" . ($CMS_5010 ? '' : sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits)) .
1084 "*" . $claim->cptNDCQuantity($prockey) .
1085 "*" . $claim->cptNDCUOM($prockey) .
1086 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1087 "~\n";
1090 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1092 // Loop 2420A, Rendering Provider (service-specific).
1093 // Used if the rendering provider for this service line is different
1094 // from that in loop 2310B.
1096 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1097 ++$edicount;
1098 $out .= "NM1" . // Loop 2310B Rendering Provider
1099 "*82" .
1100 "*1" .
1101 "*" . $claim->providerLastName($prockey) .
1102 "*" . $claim->providerFirstName($prockey) .
1103 "*" . $claim->providerMiddleName($prockey) .
1104 "*" .
1105 "*";
1106 if ($CMS_5010 || $claim->providerNPI($prockey)) { $out .=
1107 "*XX" .
1108 "*" . $claim->providerNPI($prockey);
1109 } else { $out .=
1110 "*34" . // Not allowed for 5010
1111 "*" . $claim->providerSSN($prockey);
1113 if (!$claim->providerNPI($prockey)) {
1114 $log .= "*** Rendering provider has no NPI.\n";
1116 $out .= "~\n";
1118 if ($claim->providerTaxonomy($prockey)) {
1119 ++$edicount;
1120 $out .= "PRV" .
1121 "*PE" . // PErforming provider
1122 "*" . ($CMS_5010 ? "PXC" : "ZZ") .
1123 "*" . $claim->providerTaxonomy($prockey) .
1124 "~\n";
1127 // Segment PRV*PE (Rendering Provider Specialty Information) omitted.
1128 // Segment REF (Rendering Provider Secondary Identification) omitted.
1129 // Segment NM1 (Purchased Service Provider Name) omitted.
1130 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1131 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1132 // Segment REF (Service Facility Location Secondary Identification) omitted.
1133 // Segment NM1 (Supervising Provider Name) omitted.
1134 // Segment REF (Supervising Provider Secondary Identification) omitted.
1135 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1136 // Segment REF (Ordering Provider Secondary Identification) omitted.
1137 // Segment PER (Ordering Provider Contact Information) omitted.
1138 // Segment NM1 (Referring Provider Name) omitted.
1139 // Segment REF (Referring Provider Secondary Identification) omitted.
1140 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1141 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1143 // REF*1C is required here for the Medicare provider number if NPI was
1144 // specified in NM109. Not sure if other payers require anything here.
1145 if (!$CMS_5010 && $claim->providerNumber($prockey)) {
1146 ++$edicount;
1147 $out .= "REF" .
1148 "*" . $claim->providerNumberType($prockey) .
1149 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1150 "*" . $claim->providerNumber($prockey) .
1151 "~\n";
1155 // Loop 2430, adjudication by previous payers.
1157 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
1158 if ($claim->payerSequence($ins) > $claim->payerSequence())
1159 continue; // payer is future, not previous
1161 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1162 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1164 if ($payerpaid[1] == 0 && !count($aarr)) {
1165 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1166 "' has no payments or adjustments from previous payer!\n";
1167 continue;
1170 ++$edicount;
1171 $out .= "SVD" . // Service line adjudication. Page 554.
1172 "*" . $claim->payerID($ins) .
1173 "*" . $payerpaid[1] .
1174 "*HC:" . $claim->cptKey($prockey) .
1175 "*" .
1176 "*" . $claim->cptUnits($prockey) .
1177 "~\n";
1179 $tmpdate = $payerpaid[0];
1180 foreach ($aarr as $a) {
1181 ++$edicount;
1182 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1183 "*" . $a[1] .
1184 "*" . $a[2] .
1185 "*" . $a[3] .
1186 "~\n";
1187 if (!$tmpdate) $tmpdate = $a[0];
1189 // WTH is this??
1190 /*************************************************************
1191 if ( isset($a[4]) &&
1192 $a[4] != null ) {
1193 $out .= "CAS02" . // Previous payer's adjustment reason
1194 "*" . $a[4] .
1195 "~\n";
1197 *************************************************************/
1200 if ($tmpdate) {
1201 ++$edicount;
1202 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1203 "*573" .
1204 "*D8" .
1205 "*$tmpdate" .
1206 "~\n";
1209 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1210 // Segment LQ (Form Identification Code) omitted.
1211 // Segment FRM (Supporting Documentation) omitted.
1213 } // end loop 2430
1214 } // end this procedure
1216 ++$edicount;
1217 $out .= "SE" . // SE Trailer
1218 "*$edicount" .
1219 "*0021" .
1220 "~\n";
1222 $out .= "GE" . // GE Trailer
1223 "*1" .
1224 "*1" .
1225 "~\n";
1227 $out .= "IEA" . // IEA Trailer
1228 "*1" .
1229 "*000000001" .
1230 "~\n";
1232 // Remove any trailing empty fields (delimiters) from each segment.
1233 $out = preg_replace('/\*+~/', '~', $out);
1235 $log .= "\n";
1236 return $out;