2 // Copyright (C) 2007-2011 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 require_once("Claim.class.php");
10 function stripZipCode($zip)
12 return preg_replace('/[-\s]*/','',$zip);
14 function gen_x12_837($pid, $encounter, &$log, $encounter_claim=false) {
18 $claim = new Claim($pid, $encounter);
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";
32 "*" . $claim->x12gsisa01() .
33 "*" . $claim->x12gsisa02() .
34 "*" . $claim->x12gsisa03() .
35 "*" . $claim->x12gsisa04() .
36 "*" . $claim->x12gsisa05() .
37 "*" . $claim->x12gssenderid() .
38 "*" . $claim->x12gsisa07() .
39 "*" . $claim->x12gsreceiverid() .
42 "*" . ($CMS_5010 ?
"^" : "U" ) .
43 "*" . ($CMS_5010 ?
"00501" : "00401") .
45 "*" . $claim->x12gsisa14() .
46 "*" . $claim->x12gsisa15() .
52 "*" . $claim->x12gsgs02() .
53 "*" . trim($claim->x12gs03()) .
54 "*" . date('Ymd', $today) .
55 "*" . date('Hi', $today) .
58 "*" . $claim->x12gsversionstring() .
65 // Spec says the following is optional, so should be able to leave it out.
66 ($CMS_5010 ?
("*" . $claim->x12gsversionstring()) : "") .
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
80 // This segment was deleted for 5010.
84 "*" . $claim->x12gsversionstring() .
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
94 "*" . $billingFacilityName .
100 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
101 $out .= "*" . $claim->clearingHouseETIN();
103 $out .= "*" . $claim->billingFacilityETIN();
110 "*" . $claim->billingContactName() .
112 "*" . $claim->billingContactPhone();
113 if (!$CMS_5010 && $claim->x12gsper06()) {
114 $out .= "*ED*" . $claim->x12gsper06();
119 $out .= "NM1" . // Loop 1000B Receiver
122 "*" . $claim->clearingHouseName() .
128 "*" . $claim->clearingHouseETIN() .
134 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
138 "*1" . // 1 indicates there are child segments
141 $HLBillingPayToProvider = $HLcount++
;
143 // Situational PRV segment (for provider taxonomy code) omitted here.
144 // Situational CUR segment (foreign currency information) omitted here.
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
152 "*" . $billingFacilityName .
157 if ($claim->billingFacilityNPI()) {
158 $out .= "*XX*" . $claim->billingFacilityNPI();
161 $log .= "*** Billing facility has no NPI.\n";
166 $out .= "*24*" . $claim->billingFacilityETIN();
173 "*" . $claim->billingFacilityStreet() .
178 "*" . $claim->billingFacilityCity() .
179 "*" . $claim->billingFacilityState() .
180 "*" . stripZipCode($claim->billingFacilityZip()) .
183 if ($CMS_5010 ||
($claim->billingFacilityNPI() && $claim->billingFacilityETIN())) {
186 if($claim->federalIdType()){
187 $out .= "*" . $claim->federalIdType();
190 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
192 $out .= "*" . $claim->billingFacilityETIN() .
196 if ($claim->providerNumberType() && $claim->providerNumber() &&
197 !($CMS_5010 && $claim->billingFacilityNPI()))
201 "*" . $claim->providerNumberType() .
202 "*" . $claim->providerNumber() .
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.
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
219 "*" . $billingFacilityName .
224 if ($claim->billingFacilityNPI())
225 $out .= "*XX*" . $claim->billingFacilityNPI();
227 $out .= "*24*" . $claim->billingFacilityETIN();
232 "*" . $claim->billingFacilityStreet() .
237 "*" . $claim->billingFacilityCity() .
238 "*" . $claim->billingFacilityState() .
239 "*" . stripZipCode($claim->billingFacilityZip()) .
242 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
246 "*" . $claim->billingFacilityETIN() .
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++
;
258 $out .= "HL" . // Loop 2000B Subscriber HL Loop
260 "*$HLBillingPayToProvider" .
265 if (!$claim->payerSequence()) {
266 $log .= "*** Error: Insurance information is missing!\n";
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
279 "*" . $claim->claimType() . // Zirmed replaces this
282 // Segment PAT omitted.
285 $out .= "NM1" . // Loop 2010BA Subscriber
287 "*1" . // 1 = person, 2 = non-person
288 "*" . $claim->insuredLastName() .
289 "*" . $claim->insuredFirstName() .
290 "*" . $claim->insuredMiddleName() .
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() .
301 // For 5010, further subscriber info is sent only if they are the patient.
302 if (!$CMS_5010 ||
$claim->isSelfOfInsured()) {
305 "*" . $claim->insuredStreet() .
310 "*" . $claim->insuredCity() .
311 "*" . $claim->insuredState() .
312 "*" . stripZipCode($claim->insuredZip()) .
318 "*" . $claim->insuredDOB() .
319 "*" . $claim->insuredSex() .
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.
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
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 // August 2011 - Publish interim final rule
345 // October 1, 2012-March 31, 2013 - Enumeration
346 // April 1, 2013-September 30, 2013 - Testing
347 // October 1, 2013 - Implementation
350 // Zirmed ignores this if using payer name matching:
351 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
354 // if (!$claim->payerID()) {
355 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
358 if (true) { // !$CMS_5010
359 // The 5010 spec says:
360 // "Required when the payer address is available and the submitter intends
361 // for the claim to be printed on paper at the next EDI location (for example, a
362 // clearinghouse). If not required by this implementation guide, do not send."
366 "*" . $claim->payerStreet() .
371 "*" . $claim->payerCity() .
372 "*" . $claim->payerState() .
373 "*" . stripZipCode($claim->payerZip()) .
377 // Segment REF (Payer Secondary Identification) omitted.
378 // Segment REF (Billing Provider Secondary Identification) omitted.
380 if (! $claim->isSelfOfInsured()) {
382 $out .= "HL" . // Loop 2000C Patient Information
393 "*" . $claim->insuredRelationship() .
397 $out .= "NM1" . // Loop 2010CA Patient
400 "*" . $claim->patientLastName() .
401 "*" . $claim->patientFirstName();
402 if ($claim->patientMiddleName() !== '') $out .= "*"
403 . $claim->patientMiddleName();
408 "*" . $claim->patientStreet() .
413 "*" . $claim->patientCity() .
414 "*" . $claim->patientState() .
415 "*" . stripZipCode($claim->patientZip()) .
421 "*" . $claim->patientDOB() .
422 "*" . $claim->patientSex() .
425 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
426 // Segment REF (Property and Casualty Patient Identifier) omitted.
427 // Segment PER (Property and Casualty Patient Contact Information) omitted.
429 } // end of patient different from insured
431 $proccount = $claim->procCount();
433 $clm_total_charges = 0;
434 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
435 $clm_total_charges +
= $claim->cptCharges($prockey);
438 if (!$clm_total_charges) {
439 $log .= "*** This claim has no charges!\n";
443 $out .= "CLM" . // Loop 2300 Claim
445 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
448 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" .
449 ($CMS_5010 ?
"B" : "") . ":" .
450 $claim->frequencyTypeCode() . // Changed to correct single digit output
453 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
455 ($CMS_5010 ?
"" : "*C") .
458 if ($claim->onsetDate() &&
459 ($claim->onsetDate()!== $claim->serviceDate()) &&
460 ($claim->onsetDateValid())
463 $out .= "DTP" . // Date of Onset
466 "*" . $claim->onsetDate() .
470 if ($claim->dateInitialTreatment() && ($claim->onsetDateValid())) {
472 $out .= "DTP" . // Date of Initial Treatment
475 "*" . $claim->dateInitialTreatment() .
479 // Segment DTP*304 (Last Seen Date) omitted.
480 // Segment DTP*453 (Acute Manifestation Date) omitted.
481 // Segment DTP*439 (Accident Date) omitted.
482 // Segment DTP*484 (Last Menstrual Period Date) omitted.
483 // Segment DTP*455 (Last X-Ray Date) omitted.
484 // Segment DTP*471 (Hearing and Vision Prescription Date) omitted.
485 // Segments DTP (Disability Dates) omitted.
486 // Segment DTP*297 (Last Worked Date) omitted.
487 // Segment DTP*296 (Authorized Return to Work Date) omitted.
489 if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->onsetDateValid() ) {
491 $out .= "DTP" . // Date of Hospitalization
494 "*" . $claim->onsetDate() .
498 // Segment DTP*096 (Discharge Date) omitted.
499 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
500 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
501 // Segment DTP*050 (Repricer Received Date) omitted.
502 // Segment PWK (Claim Supplemental Information) omitted.
503 // Segment CN1 (Contract Information) omitted.
505 $patientpaid = $claim->patientPaidAmount();
506 if ($patientpaid != 0) {
508 $out .= "AMT" . // Patient paid amount. Page 190/220.
514 // Segment REF*4N (Service Authorization Exception Code) omitted.
515 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
516 // Segment REF*EW (Mammography Certification Number) omitted.
517 // Segment REF*9F (Referral Number) omitted.
519 if ($claim->priorAuth()) {
521 $out .= "REF" . // Prior Authorization Number
523 "*" . $claim->priorAuth() .
527 // Segment REF*F8 (Payer Claim Control Number) omitted.
529 if ($claim->cliaCode() && ($CMS_5010 ||
$claim->claimType() === 'MB')) {
530 // Required by Medicare when in-house labs are done.
532 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
534 "*" . $claim->cliaCode() .
538 // Segment REF*9A (Repriced Claim Number) omitted.
539 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
540 // Segment REF*LX (Investigational Device Exemption Number) omitted.
541 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
542 // Segment REF*EA (Medical Record Number) omitted.
543 // Segment REF*P4 (Demonstration Project Identifier) omitted.
544 // Segment REF*1J (Care Plan Oversight) omitted.
545 // Segment K3 (File Information) omitted.
547 if ($claim->additionalNotes()) {
550 $out .= "NTE" . // comments box 19
551 "*" . ($CMS_5010 ?
"ADD" : "") .
552 "*" . $claim->additionalNotes() .
556 // Segment CR1 (Ambulance Transport Information) omitted.
557 // Segment CR2 (Spinal Manipulation Service Information) omitted.
558 // Segment CRC (Ambulance Certification) omitted.
559 // Segment CRC (Patient Condition Information: Vision) omitted.
560 // Segment CRC (Homebound Indicator) omitted.
561 // Segment CRC (EPSDT Referral) omitted.
563 // Diagnoses, up to $max_per_seg per HI segment.
564 $max_per_seg = $CMS_5010 ?
12 : 8;
565 $da = $claim->diagArray();
566 $diag_type_code = 'BK';
568 foreach ($da as $diag) {
569 if ($tmp %
$max_per_seg == 0) {
570 if ($tmp) $out .= "~\n";
572 $out .= "HI"; // Health Diagnosis Codes
574 $out .= "*$diag_type_code:" . $diag;
575 $diag_type_code = 'BF';
578 if ($tmp) $out .= "~\n";
580 // Segment HI*BP (Anesthesia Related Procedure) omitted.
581 // Segment HI*BG (Condition Information) omitted.
582 // Segment HCP (Claim Pricing/Repricing Information) omitted.
584 if ($claim->referrerLastName()) {
585 // Medicare requires referring provider's name and UPIN.
587 $out .= "NM1" . // Loop 2310A Referring Provider
590 "*" . $claim->referrerLastName() .
591 "*" . $claim->referrerFirstName() .
592 "*" . $claim->referrerMiddleName() .
595 if ($CMS_5010 ||
$claim->referrerNPI()) { $out .=
597 "*" . $claim->referrerNPI();
599 "*34" . // not allowed for 5010
600 "*" . $claim->referrerSSN();
604 if (!$CMS_5010 && $claim->referrerTaxonomy()) {
607 "*RF" . // ReFerring provider
609 "*" . $claim->referrerTaxonomy() .
613 if (!CMS_5010
&& $claim->referrerUPIN()) {
615 $out .= "REF" . // Referring Provider Secondary Identification
617 "*" . $claim->referrerUPIN() .
623 /* Per the implementation guide lines, only include this information if it is different
624 * than the Loop 2010AA information
627 ($claim->providerNPIValid() &&
628 $claim->billingFacilityNPI() !== $claim->providerNPI() ))
631 $out .= "NM1" . // Loop 2310B Rendering Provider
634 "*" . $claim->providerLastName() .
635 "*" . $claim->providerFirstName() .
636 "*" . $claim->providerMiddleName() .
639 if ($CMS_5010 ||
$claim->providerNPI()) { $out .=
641 "*" . $claim->providerNPI();
643 "*34" . // not allowed for 5010
644 "*" . $claim->providerSSN();
645 $log .= "*** Rendering provider has no NPI.\n";
649 if ($claim->providerTaxonomy()) {
652 "*PE" . // PErforming provider
653 "*" . ($CMS_5010 ?
"PXC" : "ZZ") .
654 "*" . $claim->providerTaxonomy() .
661 // This loop can only get skipped if we are generating a 5010 claim
662 if(!($claim->providerNPIValid()))
664 /* If the loop was skipped because the provider NPI was invalid, generate
665 * a warning for the log.*/
666 $log.="*** Skipping 2310B because ".$claim->providerLastName() ."," . $claim->providerFirstName() . " has invalid NPI.\n";
668 /* Skipping this segment because the providerNPI and the billingFacilityNPI are identical
669 * is a normal condition, so no need to warn.
674 // 4010: REF*1C is required here for the Medicare provider number if NPI was
675 // specified in NM109. Not sure if other payers require anything here.
676 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
678 // 5010 spec says nothing here if NPI was specified.
680 if (($CMS_5010 && !$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B','1G','G2','LU')))
681 ||
(!$CMS_5010 && trim($claim->x12gsreceiverid()) != '470819582')) // if NOT ECLAIMS EDI
683 if ($claim->providerNumber()) {
686 "*" . $claim->providerNumberType() .
687 "*" . $claim->providerNumber() .
692 // Loop 2310D is omitted in the case of home visits (POS=12).
693 if ($claim->facilityPOS() != 12 &&
694 (!$CMS_5010 ||
$claim->facilityNPI() != $claim->billingFacilityNPI()))
697 $out .= "NM1" . // Loop 2310D Service Location
700 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
701 $facilityName = substr($claim->facilityName(), 0, $CMS_5010 ?
60 : 35);
702 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
705 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
710 if ($CMS_5010 ||
$claim->facilityNPI()) { $out .=
711 "*XX*" . $claim->facilityNPI();
713 "*24*" . $claim->facilityETIN();
715 if (!$claim->facilityNPI()) {
716 $log .= "*** Service location has no NPI.\n";
720 if ($claim->facilityStreet()) {
723 "*" . $claim->facilityStreet() .
726 if ($claim->facilityState()) {
729 "*" . $claim->facilityCity() .
730 "*" . $claim->facilityState() .
731 "*" . stripZipCode($claim->facilityZip()) .
736 // Segment REF (Service Facility Location Secondary Identification) omitted.
737 // Segment PER (Service Facility Contact Information) omitted.
739 // Loop 2310E, Supervising Provider
741 if ($claim->supervisorLastName()) {
744 "*DQ" . // Supervising Physician
746 "*" . $claim->supervisorLastName() .
747 "*" . $claim->supervisorFirstName() .
748 "*" . $claim->supervisorMiddleName() .
749 "*" . // NM106 not used
751 if ($CMS_5010 ||
$claim->supervisorNPI()) { $out .=
753 "*" . $claim->supervisorNPI();
756 "*" . $claim->supervisorSSN();
758 if (!$claim->supervisorNPI()) {
759 $log .= "*** Supervising Provider has no NPI.\n";
763 if ($claim->supervisorNumber()) {
766 "*" . $claim->supervisorNumberType() .
767 "*" . $claim->supervisorNumber() .
772 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
773 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
775 $prev_pt_resp = $clm_total_charges; // for computation below
777 // Loops 2320 and 2330*, other subscriber/payer information.
778 // Remember that insurance index 0 is always for the payer being billed
779 // by this claim, and 1 and above are always for the "other" payers.
781 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
783 $tmp1 = $claim->claimType($ins);
784 $tmp2 = 'C1'; // Here a kludge. See page 321.
785 if ($tmp1 === 'CI') $tmp2 = 'C1';
786 if ($tmp1 === 'AM') $tmp2 = 'AP';
787 if ($tmp1 === 'HM') $tmp2 = 'HM';
788 if ($tmp1 === 'MB') $tmp2 = 'MB';
789 if ($tmp1 === 'MC') $tmp2 = 'MC';
790 if ($tmp1 === '09') $tmp2 = 'PP';
792 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
793 "*" . $claim->payerSequence($ins) .
794 "*" . $claim->insuredRelationship($ins) .
795 "*" . $claim->groupNumber($ins) .
796 "*" . (($CMS_5010 && $claim->groupNumber($ins)) ?
'' : $claim->groupName($ins)) .
797 "*" . ($CMS_5010 ?
$claim->insuredTypeCode($ins) : $tmp2) .
801 "*" . $claim->claimType($ins) .
804 // Things that apply only to previous payers, not future payers.
806 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
808 // Generate claim-level adjustments.
809 $aarr = $claim->payerAdjustments($ins);
810 foreach ($aarr as $a) {
812 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
819 $payerpaid = $claim->payerTotals($ins);
821 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
823 "*" . $payerpaid[1] .
826 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
827 // Segment AMT*EAF (Remaining Patient Liability) omitted.
830 // Patient responsibility amount as of this previous payer.
831 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
832 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
835 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
837 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
841 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
843 "*" . sprintf('%.2f', $prev_pt_resp) .
846 } // End of things that apply only to previous payers.
850 $out .= "DMG" . // Other subscriber demographic information. Page 342.
852 "*" . $claim->insuredDOB($ins) .
853 "*" . $claim->insuredSex($ins) .
858 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
861 "*" . ($claim->billingFacilityAssignment($ins) ?
'Y' : 'N') .
862 // For this next item, the 5010 example in the spec does not match its
863 // description. So this might be wrong.
864 "*" . ($CMS_5010 ?
'' : 'B') .
869 // Segment MOA (Medicare Outpatient Adjudication) omitted.
872 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
875 "*" . $claim->insuredLastName($ins) .
876 "*" . $claim->insuredFirstName($ins) .
877 "*" . $claim->insuredMiddleName($ins) .
881 "*" . $claim->policyNumber($ins) .
886 "*" . $claim->insuredStreet($ins) .
891 "*" . $claim->insuredCity($ins) .
892 "*" . $claim->insuredState($ins) .
893 "*" . stripZipCode($claim->insuredZip($ins)) .
896 // Segment REF (Other Subscriber Secondary Identification) omitted.
899 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
900 $payerName = substr($claim->payerName($ins), 0, $CMS_5010 ?
60 : 35);
901 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
910 "*" . $claim->payerID($ins) .
913 // if (!$claim->payerID($ins)) {
914 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
917 // Payer address (N3 and N4) are added below so that Gateway EDI can
918 // auto-generate secondary claims. These do NOT appear in my copy of
919 // the spec! -- Rod 2008-06-12
921 if ($CMS_5010 ||
trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
924 "*" . $claim->payerStreet($ins) .
929 "*" . $claim->payerCity($ins) .
930 "*" . $claim->payerState($ins) .
931 "*" . stripZipCode($claim->payerZip($ins)) .
935 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
936 // Segment REF (Other Payer Secondary Identifier) omitted.
937 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
938 // Segment REF*9F (Other Payer Referral Number) omitted.
939 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
940 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
941 // Segment NM1 (Other Payer Referring Provider) omitted.
942 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
943 // Segment NM1 (Other Payer Rendering Provider) omitted.
944 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
945 // Segment NM1 (Other Payer Service Facility Location) omitted.
946 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
947 // Segment NM1 (Other Payer Supervising Provider) omitted.
948 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
949 // Segment NM1 (Other Payer Billing Provider) omitted.
950 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
952 } // End loops 2320/2330*.
956 // Procedure loop starts here.
958 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
962 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
967 $out .= "SV1" . // Professional Service. Page 400.
968 "*HC:" . $claim->cptKey($prockey) .
969 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
971 "*" . $claim->cptUnits($prockey) .
975 $dia = $claim->diagIndexArray($prockey);
977 foreach ($dia as $dindex) {
980 if (++
$i >= 4) break;
984 if (!$claim->cptCharges($prockey)) {
985 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
989 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
992 // Segment SV5 (Durable Medical Equipment Service) omitted.
993 // Segment PWK (Line Supplemental Information) omitted.
994 // Segment PWK (Durable Medical Equipment Certificate of Medical Necessity Indicator) omitted.
995 // Segment CR1 (Ambulance Transport Information) omitted.
996 // Segment CR3 (Durable Medical Equipment Certification) omitted.
997 // Segment CRC (Ambulance Certification) omitted.
998 // Segment CRC (Hospice Employee Indicator) omitted.
999 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
1002 $out .= "DTP" . // Date of Service. Page 435.
1005 "*" . $claim->serviceDate() .
1008 $testnote = rtrim($claim->cptNotecodes($prockey));
1009 if (!empty($testnote)) {
1011 $out .= "NTE" . // Explain Unusual Circumstances.
1013 "*" . $claim->cptNotecodes($prockey) .
1017 // Segment DTP*471 (Prescription Date) omitted.
1018 // Segment DTP*607 (Revision/Recertification Date) omitted.
1019 // Segment DTP*463 (Begin Therapy Date) omitted.
1020 // Segment DTP*461 (Last Certification Date) omitted.
1021 // Segment DTP*304 (Last Seen Date) omitted.
1022 // Segment DTP (Test Date) omitted.
1023 // Segment DTP*011 (Shipped Date) omitted.
1024 // Segment DTP*455 (Last X-Ray Date) omitted.
1025 // Segment DTP*454 (Initial Treatment Date) omitted.
1026 // Segment QTY (Ambulance Patient Count) omitted.
1027 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1028 // Segment MEA (Test Result) omitted.
1029 // Segment CN1 (Contract Information) omitted.
1030 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1031 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1032 // Segment REF*G1 (Prior Authorization) omitted.
1033 // Segment REF*6R (Line Item Control Number) omitted.
1034 // (Really oughta have this for robust 835 posting!)
1035 // Segment REF*EW (Mammography Certification Number) omitted.
1036 // Segment REF*X4 (CLIA Number) omitted.
1037 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1038 // Segment REF*BT (Immunization Batch Number) omitted.
1039 // Segment REF*9F (Referral Number) omitted.
1040 // Segment AMT*T (Sales Tax Amount) omitted.
1041 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1042 // Segment K3 (File Information) omitted.
1043 // Segment NTE (Line Note) omitted.
1044 // Segment NTE (Third Party Organization Notes) omitted.
1045 // Segment PS1 (Purchased Service Information) omitted.
1046 // Segment HCP (Line Pricing/Repricing Information) omitted.
1049 // This segment was deleted for 5010.
1051 // AMT*AAE segment for Approved Amount from previous payer.
1052 // Medicare secondaries seem to require this.
1054 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
1055 if ($claim->payerSequence($ins) > $claim->payerSequence())
1056 continue; // payer is future, not previous
1057 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1059 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1061 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1067 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1068 // with HCPCS codes.
1070 $ndc = $claim->cptNDCID($prockey);
1073 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1074 "*" . // Per addendum, LIN01 is not used.
1079 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1080 $log .= "*** NDC code '$ndc' has invalid format!\n";
1084 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1085 if (!$tmpunits) $tmpunits = 1;
1086 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1089 "*" . ($CMS_5010 ?
'' : sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits)) .
1090 "*" . $claim->cptNDCQuantity($prockey) .
1091 "*" . $claim->cptNDCUOM($prockey) .
1092 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1096 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1098 // Loop 2420A, Rendering Provider (service-specific).
1099 // Used if the rendering provider for this service line is different
1100 // from that in loop 2310B.
1102 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1104 $out .= "NM1" . // Loop 2310B Rendering Provider
1107 "*" . $claim->providerLastName($prockey) .
1108 "*" . $claim->providerFirstName($prockey) .
1109 "*" . $claim->providerMiddleName($prockey) .
1112 if ($CMS_5010 ||
$claim->providerNPI($prockey)) { $out .=
1114 "*" . $claim->providerNPI($prockey);
1116 "*34" . // Not allowed for 5010
1117 "*" . $claim->providerSSN($prockey);
1119 if (!$claim->providerNPI($prockey)) {
1120 $log .= "*** Rendering provider has no NPI.\n";
1124 if ($claim->providerTaxonomy($prockey)) {
1127 "*PE" . // PErforming provider
1128 "*" . ($CMS_5010 ?
"PXC" : "ZZ") .
1129 "*" . $claim->providerTaxonomy($prockey) .
1133 // Segment PRV*PE (Rendering Provider Specialty Information) omitted.
1134 // Segment REF (Rendering Provider Secondary Identification) omitted.
1135 // Segment NM1 (Purchased Service Provider Name) omitted.
1136 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1137 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1138 // Segment REF (Service Facility Location Secondary Identification) omitted.
1139 // Segment NM1 (Supervising Provider Name) omitted.
1140 // Segment REF (Supervising Provider Secondary Identification) omitted.
1141 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1142 // Segment REF (Ordering Provider Secondary Identification) omitted.
1143 // Segment PER (Ordering Provider Contact Information) omitted.
1144 // Segment NM1 (Referring Provider Name) omitted.
1145 // Segment REF (Referring Provider Secondary Identification) omitted.
1146 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1147 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1149 // REF*1C is required here for the Medicare provider number if NPI was
1150 // specified in NM109. Not sure if other payers require anything here.
1151 if (!$CMS_5010 && $claim->providerNumber($prockey)) {
1154 "*" . $claim->providerNumberType($prockey) .
1155 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1156 "*" . $claim->providerNumber($prockey) .
1161 // Loop 2430, adjudication by previous payers.
1163 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
1164 if ($claim->payerSequence($ins) > $claim->payerSequence())
1165 continue; // payer is future, not previous
1167 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1168 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1170 if ($payerpaid[1] == 0 && !count($aarr)) {
1171 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1172 "' has no payments or adjustments from previous payer!\n";
1177 $out .= "SVD" . // Service line adjudication. Page 554.
1178 "*" . $claim->payerID($ins) .
1179 "*" . $payerpaid[1] .
1180 "*HC:" . $claim->cptKey($prockey) .
1182 "*" . $claim->cptUnits($prockey) .
1185 $tmpdate = $payerpaid[0];
1186 foreach ($aarr as $a) {
1188 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1193 if (!$tmpdate) $tmpdate = $a[0];
1196 /*************************************************************
1197 if ( isset($a[4]) &&
1199 $out .= "CAS02" . // Previous payer's adjustment reason
1203 *************************************************************/
1208 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1215 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1216 // Segment LQ (Form Identification Code) omitted.
1217 // Segment FRM (Supporting Documentation) omitted.
1220 } // end this procedure
1223 $out .= "SE" . // SE Trailer
1228 $out .= "GE" . // GE Trailer
1233 $out .= "IEA" . // IEA Trailer
1238 // Remove any trailing empty fields (delimiters) from each segment.
1239 $out = preg_replace('/\*+~/', '~', $out);