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 str_replace('-','',$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";
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? ***************
345 // Zirmed ignores this if using payer name matching:
346 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
349 // if (!$claim->payerID()) {
350 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
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."
361 "*" . $claim->payerStreet() .
366 "*" . $claim->payerCity() .
367 "*" . $claim->payerState() .
368 "*" . stripZipCode($claim->payerZip()) .
372 // Segment REF (Payer Secondary Identification) omitted.
373 // Segment REF (Billing Provider Secondary Identification) omitted.
375 if (! $claim->isSelfOfInsured()) {
377 $out .= "HL" . // Loop 2000C Patient Information
388 "*" . $claim->insuredRelationship() .
392 $out .= "NM1" . // Loop 2010CA Patient
395 "*" . $claim->patientLastName() .
396 "*" . $claim->patientFirstName() .
397 "*" . $claim->patientMiddleName() .
402 "*" . $claim->patientStreet() .
407 "*" . $claim->patientCity() .
408 "*" . $claim->patientState() .
409 "*" . stripZipCode($claim->patientZip()) .
415 "*" . $claim->patientDOB() .
416 "*" . $claim->patientSex() .
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";
437 $out .= "CLM" . // Loop 2300 Claim
439 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
442 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" .
443 ($CMS_5010 ?
"B" : "") . ":" .
444 $claim->frequencyTypeCode() . // Changed to correct single digit output
447 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
449 ($CMS_5010 ?
"" : "*C") .
452 if ($claim->onsetDate() &&
453 ($claim->onsetDate()!== $claim->serviceDate()) &&
454 ($claim->onsetDateValid())
457 $out .= "DTP" . // Date of Onset
460 "*" . $claim->onsetDate() .
464 if ($claim->dateInitialTreatment() && ($claim->onsetDateValid())) {
466 $out .= "DTP" . // Date of Initial Treatment
469 "*" . $claim->dateInitialTreatment() .
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() ) {
485 $out .= "DTP" . // Date of Hospitalization
488 "*" . $claim->onsetDate() .
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) {
502 $out .= "AMT" . // Patient paid amount. Page 190/220.
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()) {
515 $out .= "REF" . // Prior Authorization Number
517 "*" . $claim->priorAuth() .
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.
526 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
528 "*" . $claim->cliaCode() .
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()) {
544 $out .= "NTE" . // comments box 19
545 "*" . ($CMS_5010 ?
"ADD" : "") .
546 "*" . $claim->additionalNotes() .
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';
562 foreach ($da as $diag) {
563 if ($tmp %
$max_per_seg == 0) {
564 if ($tmp) $out .= "~\n";
566 $out .= "HI"; // Health Diagnosis Codes
568 $out .= "*$diag_type_code:" . $diag;
569 $diag_type_code = 'BF';
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.
581 $out .= "NM1" . // Loop 2310A Referring Provider
584 "*" . $claim->referrerLastName() .
585 "*" . $claim->referrerFirstName() .
586 "*" . $claim->referrerMiddleName() .
589 if ($CMS_5010 ||
$claim->referrerNPI()) { $out .=
591 "*" . $claim->referrerNPI();
593 "*34" . // not allowed for 5010
594 "*" . $claim->referrerSSN();
598 if (!$CMS_5010 && $claim->referrerTaxonomy()) {
601 "*RF" . // ReFerring provider
603 "*" . $claim->referrerTaxonomy() .
607 if (!CMS_5010
&& $claim->referrerUPIN()) {
609 $out .= "REF" . // Referring Provider Secondary Identification
611 "*" . $claim->referrerUPIN() .
617 /* Per the implementation guide lines, only include this information if it is different
618 * than the Loop 2010AA information
621 ($claim->providerNPIValid() &&
622 $claim->billingFacilityNPI() !== $claim->providerNPI() ))
625 $out .= "NM1" . // Loop 2310B Rendering Provider
628 "*" . $claim->providerLastName() .
629 "*" . $claim->providerFirstName() .
630 "*" . $claim->providerMiddleName() .
633 if ($CMS_5010 ||
$claim->providerNPI()) { $out .=
635 "*" . $claim->providerNPI();
637 "*34" . // not allowed for 5010
638 "*" . $claim->providerSSN();
639 $log .= "*** Rendering provider has no NPI.\n";
643 if ($claim->providerTaxonomy()) {
646 "*PE" . // PErforming provider
647 "*" . ($CMS_5010 ?
"PXC" : "ZZ") .
648 "*" . $claim->providerTaxonomy() .
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()) {
680 "*" . $claim->providerNumberType() .
681 "*" . $claim->providerNumber() .
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()))
691 $out .= "NM1" . // Loop 2310D Service Location
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 .=
699 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
704 if ($CMS_5010 ||
$claim->facilityNPI()) { $out .=
705 "*XX*" . $claim->facilityNPI();
707 "*24*" . $claim->facilityETIN();
709 if (!$claim->facilityNPI()) {
710 $log .= "*** Service location has no NPI.\n";
714 if ($claim->facilityStreet()) {
717 "*" . $claim->facilityStreet() .
720 if ($claim->facilityState()) {
723 "*" . $claim->facilityCity() .
724 "*" . $claim->facilityState() .
725 "*" . stripZipCode($claim->facilityZip()) .
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()) {
738 "*DQ" . // Supervising Physician
740 "*" . $claim->supervisorLastName() .
741 "*" . $claim->supervisorFirstName() .
742 "*" . $claim->supervisorMiddleName() .
743 "*" . // NM106 not used
745 if ($CMS_5010 ||
$claim->supervisorNPI()) { $out .=
747 "*" . $claim->supervisorNPI();
750 "*" . $claim->supervisorSSN();
752 if (!$claim->supervisorNPI()) {
753 $log .= "*** Supervising Provider has no NPI.\n";
757 if ($claim->supervisorNumber()) {
760 "*" . $claim->supervisorNumberType() .
761 "*" . $claim->supervisorNumber() .
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';
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) .
795 "*" . $claim->claimType($ins) .
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) {
806 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
813 $payerpaid = $claim->payerTotals($ins);
815 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
817 "*" . $payerpaid[1] .
820 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
821 // Segment AMT*EAF (Remaining Patient Liability) omitted.
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
829 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
831 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
835 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
837 "*" . sprintf('%.2f', $prev_pt_resp) .
840 } // End of things that apply only to previous payers.
844 $out .= "DMG" . // Other subscriber demographic information. Page 342.
846 "*" . $claim->insuredDOB($ins) .
847 "*" . $claim->insuredSex($ins) .
852 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
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') .
863 // Segment MOA (Medicare Outpatient Adjudication) omitted.
866 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
869 "*" . $claim->insuredLastName($ins) .
870 "*" . $claim->insuredFirstName($ins) .
871 "*" . $claim->insuredMiddleName($ins) .
875 "*" . $claim->policyNumber($ins) .
880 "*" . $claim->insuredStreet($ins) .
885 "*" . $claim->insuredCity($ins) .
886 "*" . $claim->insuredState($ins) .
887 "*" . stripZipCode($claim->insuredZip($ins)) .
890 // Segment REF (Other Subscriber Secondary Identification) omitted.
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.
904 "*" . $claim->payerID($ins) .
907 // if (!$claim->payerID($ins)) {
908 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
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
918 "*" . $claim->payerStreet($ins) .
923 "*" . $claim->payerCity($ins) .
924 "*" . $claim->payerState($ins) .
925 "*" . stripZipCode($claim->payerZip($ins)) .
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*.
950 // Procedure loop starts here.
952 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
956 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
961 $out .= "SV1" . // Professional Service. Page 400.
962 "*HC:" . $claim->cptKey($prockey) .
963 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
965 "*" . $claim->cptUnits($prockey) .
969 $dia = $claim->diagIndexArray($prockey);
971 foreach ($dia as $dindex) {
974 if (++
$i >= 4) break;
978 if (!$claim->cptCharges($prockey)) {
979 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
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.
996 $out .= "DTP" . // Date of Service. Page 435.
999 "*" . $claim->serviceDate() .
1002 $testnote = rtrim($claim->cptNotecodes($prockey));
1003 if (!empty($testnote)) {
1005 $out .= "NTE" . // Explain Unusual Circumstances.
1007 "*" . $claim->cptNotecodes($prockey) .
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.
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));
1053 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1055 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1061 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1062 // with HCPCS codes.
1064 $ndc = $claim->cptNDCID($prockey);
1067 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1068 "*" . // Per addendum, LIN01 is not used.
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";
1078 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1079 if (!$tmpunits) $tmpunits = 1;
1080 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
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.
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)) {
1098 $out .= "NM1" . // Loop 2310B Rendering Provider
1101 "*" . $claim->providerLastName($prockey) .
1102 "*" . $claim->providerFirstName($prockey) .
1103 "*" . $claim->providerMiddleName($prockey) .
1106 if ($CMS_5010 ||
$claim->providerNPI($prockey)) { $out .=
1108 "*" . $claim->providerNPI($prockey);
1110 "*34" . // Not allowed for 5010
1111 "*" . $claim->providerSSN($prockey);
1113 if (!$claim->providerNPI($prockey)) {
1114 $log .= "*** Rendering provider has no NPI.\n";
1118 if ($claim->providerTaxonomy($prockey)) {
1121 "*PE" . // PErforming provider
1122 "*" . ($CMS_5010 ?
"PXC" : "ZZ") .
1123 "*" . $claim->providerTaxonomy($prockey) .
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)) {
1148 "*" . $claim->providerNumberType($prockey) .
1149 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1150 "*" . $claim->providerNumber($prockey) .
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";
1171 $out .= "SVD" . // Service line adjudication. Page 554.
1172 "*" . $claim->payerID($ins) .
1173 "*" . $payerpaid[1] .
1174 "*HC:" . $claim->cptKey($prockey) .
1176 "*" . $claim->cptUnits($prockey) .
1179 $tmpdate = $payerpaid[0];
1180 foreach ($aarr as $a) {
1182 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1187 if (!$tmpdate) $tmpdate = $a[0];
1190 /*************************************************************
1191 if ( isset($a[4]) &&
1193 $out .= "CAS02" . // Previous payer's adjustment reason
1197 *************************************************************/
1202 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1209 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1210 // Segment LQ (Form Identification Code) omitted.
1211 // Segment FRM (Supporting Documentation) omitted.
1214 } // end this procedure
1217 $out .= "SE" . // SE Trailer
1222 $out .= "GE" . // GE Trailer
1227 $out .= "IEA" . // IEA Trailer
1232 // Remove any trailing empty fields (delimiters) from each segment.
1233 $out = preg_replace('/\*+~/', '~', $out);