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() .
90 if ($claim->federalIdType() == "SY") { // check entity type for NM*102 1 == person, 2 == non-person entity
91 $tempName = $claim->billingFacilityName();
92 $partsName = explode(' ', $tempName);// Loop 1000A submitter entity == person
93 $num_parts = count($partsName);
96 $firstName = $partsName[0];
98 $lastName = $partsName[1];
102 $firstName = $partsName[0];
103 $middleName = $partsName[1];
104 $lastName = $partsName[2];
108 $firstName = $partsName[0];
109 $middleName = $partsName[1];
110 $lastName = $partsName[2];
111 $suffixName = $partsName[3];
114 $log .= "*** submitter name in 1000A loop has more than 4 parts, may not be desirable\n";
115 $firstName = $partsName[0];
116 $middleName = $partsName[1];
117 $lastName = $partsName[2];
118 $suffixName = $partsName[3];
120 $out .= "NM1" . // Loop 1000A Submitter
126 "*" . // Name Prefix not used
127 "*" . // Name Suffix not used
129 } else { //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
130 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ?
60 : 35);
131 if ($billingFacilityName == '') $log .= "*** billing facility name in 1000A loop is empty\n";
135 "*" . $billingFacilityName .
143 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
144 $out .= "*" . $claim->clearingHouseETIN();
146 $out .= "*" . $claim->billingFacilityETIN();
153 "*" . $claim->billingContactName() .
155 "*" . $claim->billingContactPhone();
156 if (!$CMS_5010 && $claim->x12gsper06()) {
157 $out .= "*ED*" . $claim->x12gsper06();
162 $out .= "NM1" . // Loop 1000B Receiver
165 "*" . $claim->clearingHouseName() .
171 "*" . $claim->clearingHouseETIN() .
177 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
181 "*1" . // 1 indicates there are child segments
184 $HLBillingPayToProvider = $HLcount++
;
186 // Situational PRV segment for provider taxonomy code for Medicaid.
187 if ($claim->claimType() == 'MC') {
189 $out .= "PRV*BI*ZZ" .
190 "*" . $claim->providerTaxonomy() .
194 // Situational CUR segment (foreign currency information) omitted here.
197 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
198 if ($claim->federalIdType() == "SY") { // check for entity type like in 1000A
199 $tempName = $claim->billingFacilityName();
200 $partsName = explode(' ', $tempName);// Loop 2010AA Billing Provider entity == person
201 $num_parts = count($partsName);
202 switch ($num_parts) {
204 $firstName = $partsName[0];
206 $lastName = $partsName[1];
210 $firstName = $partsName[0];
211 $middleName = $partsName[1];
212 $lastName = $partsName[2];
216 $firstName = $partsName[0];
217 $middleName = $partsName[1];
218 $lastName = $partsName[2];
219 $suffixName = $partsName[3];
222 $log .= "*** billing provider name in 2010AA loop has more than 4 parts, may not be desirable\n";
223 $firstName = $partsName[0];
224 $middleName = $partsName[1];
225 $lastName = $partsName[2];
226 $suffixName = $partsName[3];
234 "*" . // Name Prefix not used
238 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ?
60 : 35);
239 if ($billingFacilityName == '') $log .= "*** billing facility name in 2010A loop is empty\n";
240 $out .= "NM1" . // Loop 2010AA Billing Provider
243 "*" . $billingFacilityName .
249 if ($claim->billingFacilityNPI()) {
250 $out .= "*XX*" . $claim->billingFacilityNPI();
253 $log .= "*** Billing facility has no NPI.\n";
258 $out .= "*24*" . $claim->billingFacilityETIN();
265 "*" . $claim->billingFacilityStreet() .
270 "*" . $claim->billingFacilityCity() .
271 "*" . $claim->billingFacilityState() .
272 "*" . stripZipCode($claim->billingFacilityZip()) .
275 if ($CMS_5010 ||
($claim->billingFacilityNPI() && $claim->billingFacilityETIN())) {
278 if($claim->federalIdType()){
279 $out .= "*" . $claim->federalIdType();
282 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
284 $out .= "*" . $claim->billingFacilityETIN() .
288 if ($claim->providerNumberType() && $claim->providerNumber() &&
289 !($CMS_5010 && $claim->billingFacilityNPI()))
293 "*" . $claim->providerNumberType() .
294 "*" . $claim->providerNumber() .
297 else if ($claim->providerNumber() && !$claim->providerNumberType()) {
298 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
301 // Situational PER*1C segment omitted.
303 // Pay-To Address defaults to billing provider and is no longer required in 5010.
306 // Field length is limited to 35. See nucc dataset page 63 www.nucc.org
307 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ?
60 : 35);
308 $out .= "NM1" . // Loop 2010AB Pay-To Provider
311 "*" . $billingFacilityName .
316 if ($claim->billingFacilityNPI())
317 $out .= "*XX*" . $claim->billingFacilityNPI();
319 $out .= "*24*" . $claim->billingFacilityETIN();
324 "*" . $claim->billingFacilityStreet() .
329 "*" . $claim->billingFacilityCity() .
330 "*" . $claim->billingFacilityState() .
331 "*" . stripZipCode($claim->billingFacilityZip()) .
334 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
338 "*" . $claim->billingFacilityETIN() .
343 // Loop 2010AC Pay-To Plan Name omitted. Includes:
344 // NM1*PE, N3, N4, REF*2U, REF*EI
346 $PatientHL = $claim->isSelfOfInsured() ?
0 : 1;
347 $HLSubscriber = $HLcount++
;
350 $out .= "HL" . // Loop 2000B Subscriber HL Loop
352 "*$HLBillingPayToProvider" .
357 if (!$claim->payerSequence()) {
358 $log .= "*** Error: Insurance information is missing!\n";
362 $out .= "SBR" . // Subscriber Information
363 "*" . $claim->payerSequence() .
364 "*" . ($claim->isSelfOfInsured() ?
'18' : '') .
365 "*" . $claim->groupNumber() .
366 "*" . (($CMS_5010 && $claim->groupNumber()) ?
'' : $claim->groupName()) .
367 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
371 "*" . $claim->claimType() . // Zirmed replaces this
374 // Segment PAT omitted.
377 $out .= "NM1" . // Loop 2010BA Subscriber
379 "*1" . // 1 = person, 2 = non-person
380 "*" . $claim->insuredLastName() .
381 "*" . $claim->insuredFirstName() .
382 "*" . $claim->insuredMiddleName() .
386 // "MI" = Member Identification Number
387 // "II" = Standard Unique Health Identifier, "Required if the
388 // HIPAA Individual Patient Identifier is mandated use."
389 // Here we presume that is not true yet.
390 "*" . $claim->policyNumber() .
393 // For 5010, further subscriber info is sent only if they are the patient.
394 if (!$CMS_5010 ||
$claim->isSelfOfInsured()) {
397 "*" . $claim->insuredStreet() .
402 "*" . $claim->insuredCity() .
403 "*" . $claim->insuredState() .
404 "*" . stripZipCode($claim->insuredZip()) .
410 "*" . $claim->insuredDOB() .
411 "*" . $claim->insuredSex() .
415 // Segment REF*SY (Subscriber Secondary Identification) omitted.
416 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
417 // Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
420 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
421 $payerName = substr($claim->payerName(), 0, $CMS_5010 ?
60 : 35);
422 $out .= "NM1" . // Loop 2010BB Payer
430 // The 5010 spec says:
431 // "On or after the mandated implementation date for the HIPAA
432 // National Plan Identifier (National Plan ID), XV must be sent.
433 // Prior to the mandated implementation date and prior to any phase-
434 // in period identified by Federal regulation, PI must be sent."
435 // *************** Anybody know what that date is? ***************
436 // August 2011 - Publish interim final rule
437 // October 1, 2012-March 31, 2013 - Enumeration
438 // April 1, 2013-September 30, 2013 - Testing
439 // October 1, 2013 - Implementation
442 // Zirmed ignores this if using payer name matching:
443 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
446 // if (!$claim->payerID()) {
447 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
450 if (true) { // !$CMS_5010
451 // The 5010 spec says:
452 // "Required when the payer address is available and the submitter intends
453 // for the claim to be printed on paper at the next EDI location (for example, a
454 // clearinghouse). If not required by this implementation guide, do not send."
458 "*" . $claim->payerStreet() .
463 "*" . $claim->payerCity() .
464 "*" . $claim->payerState() .
465 "*" . stripZipCode($claim->payerZip()) .
469 // Segment REF (Payer Secondary Identification) omitted.
470 // Segment REF (Billing Provider Secondary Identification) omitted.
472 if (! $claim->isSelfOfInsured()) {
474 $out .= "HL" . // Loop 2000C Patient Information
485 "*" . $claim->insuredRelationship() .
489 $out .= "NM1" . // Loop 2010CA Patient
492 "*" . $claim->patientLastName() .
493 "*" . $claim->patientFirstName();
494 if ($claim->patientMiddleName() !== '') $out .= "*"
495 . $claim->patientMiddleName();
500 "*" . $claim->patientStreet() .
505 "*" . $claim->patientCity() .
506 "*" . $claim->patientState() .
507 "*" . stripZipCode($claim->patientZip()) .
513 "*" . $claim->patientDOB() .
514 "*" . $claim->patientSex() .
517 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
518 // Segment REF (Property and Casualty Patient Identifier) omitted.
519 // Segment PER (Property and Casualty Patient Contact Information) omitted.
521 } // end of patient different from insured
523 $proccount = $claim->procCount();
525 $clm_total_charges = 0;
526 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
527 $clm_total_charges +
= $claim->cptCharges($prockey);
530 if (!$clm_total_charges) {
531 $log .= "*** This claim has no charges!\n";
535 $out .= "CLM" . // Loop 2300 Claim
537 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
540 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" .
541 ($CMS_5010 ?
"B" : "") . ":" .
542 $claim->frequencyTypeCode() . // Changed to correct single digit output
545 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
547 ($CMS_5010 ?
"" : "*C") .
550 if ($claim->onsetDate() &&
551 ($claim->onsetDate()!== $claim->serviceDate()) &&
552 ($claim->onsetDateValid())
555 $out .= "DTP" . // Date of Onset
558 "*" . $claim->onsetDate() .
562 if ($claim->dateInitialTreatment() && ($claim->onsetDateValid())) {
564 $out .= "DTP" . // Date of Initial Treatment
567 "*" . $claim->dateInitialTreatment() .
571 // Segment DTP*304 (Last Seen Date) omitted.
572 // Segment DTP*453 (Acute Manifestation Date) omitted.
573 // Segment DTP*439 (Accident Date) omitted.
574 // Segment DTP*484 (Last Menstrual Period Date) omitted.
575 // Segment DTP*455 (Last X-Ray Date) omitted.
576 // Segment DTP*471 (Hearing and Vision Prescription Date) omitted.
577 // Segments DTP (Disability Dates) omitted.
578 // Segment DTP*297 (Last Worked Date) omitted.
579 // Segment DTP*296 (Authorized Return to Work Date) omitted.
581 if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->onsetDateValid() ) {
583 $out .= "DTP" . // Date of Hospitalization
586 "*" . $claim->onsetDate() .
590 // Segment DTP*096 (Discharge Date) omitted.
591 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
592 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
593 // Segment DTP*050 (Repricer Received Date) omitted.
594 // Segment PWK (Claim Supplemental Information) omitted.
595 // Segment CN1 (Contract Information) omitted.
597 $patientpaid = $claim->patientPaidAmount();
598 if ($patientpaid != 0) {
600 $out .= "AMT" . // Patient paid amount. Page 190/220.
606 // Segment REF*4N (Service Authorization Exception Code) omitted.
607 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
608 // Segment REF*EW (Mammography Certification Number) omitted.
609 // Segment REF*9F (Referral Number) omitted.
611 if ($claim->priorAuth()) {
613 $out .= "REF" . // Prior Authorization Number
615 "*" . $claim->priorAuth() .
619 // Segment REF*F8 Payer Claim Control Number for claim re-submission.icn_resubmission_number
621 #if($claim->billing_options['replacement_claim'] == '1'){
622 if(trim($claim->billing_options
['icn_resubmission_number']) > 3){
624 error_log("Method 1: ".$claim->billing_options
['icn_resubmission_number'], 0);
627 "*" . $claim->icnResubmissionNumber() .
632 if ($claim->cliaCode() && ($CMS_5010 ||
$claim->claimType() === 'MB')) {
633 // Required by Medicare when in-house labs are done.
635 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
637 "*" . $claim->cliaCode() .
641 // Segment REF*9A (Repriced Claim Number) omitted.
642 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
643 // Segment REF*LX (Investigational Device Exemption Number) omitted.
644 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
645 // Segment REF*EA (Medical Record Number) omitted.
646 // Segment REF*P4 (Demonstration Project Identifier) omitted.
647 // Segment REF*1J (Care Plan Oversight) omitted.
648 // Segment K3 (File Information) omitted.
650 if ($claim->additionalNotes()) {
653 $out .= "NTE" . // comments box 19
654 "*" . ($CMS_5010 ?
"ADD" : "") .
655 "*" . $claim->additionalNotes() .
659 // Segment CR1 (Ambulance Transport Information) omitted.
660 // Segment CR2 (Spinal Manipulation Service Information) omitted.
661 // Segment CRC (Ambulance Certification) omitted.
662 // Segment CRC (Patient Condition Information: Vision) omitted.
663 // Segment CRC (Homebound Indicator) omitted.
665 // Segment CRC (EPSDT Referral).
666 if($claim->epsdtFlag()) {
671 "*" . $claim->medicaidReferralCode() .
675 // Diagnoses, up to $max_per_seg per HI segment.
676 $max_per_seg = $CMS_5010 ?
12 : 8;
677 $da = $claim->diagArray();
678 if ($claim->diagtype
== "ICD9") {
679 $diag_type_code = 'BK';
683 $diag_type_code = 'ABK';
686 foreach ($da as $diag) {
687 if ($tmp %
$max_per_seg == 0) {
688 if ($tmp) $out .= "~\n";
690 $out .= "HI"; // Health Diagnosis Codes
692 $out .= "*$diag_type_code:" . $diag;
693 if ($claim->diagtype
== "ICD9") {
694 $diag_type_code = 'BF';
698 $diag_type_code = 'ABF';
702 if ($tmp) $out .= "~\n";
704 // Segment HI*BP (Anesthesia Related Procedure) omitted.
705 // Segment HI*BG (Condition Information) omitted.
706 // Segment HCP (Claim Pricing/Repricing Information) omitted.
708 if ($claim->referrerLastName()) {
709 // Medicare requires referring provider's name and UPIN.
711 $out .= "NM1" . // Loop 2310A Referring Provider
714 "*" . $claim->referrerLastName() .
715 "*" . $claim->referrerFirstName() .
716 "*" . $claim->referrerMiddleName() .
719 if ($CMS_5010 ||
$claim->referrerNPI()) { $out .=
721 "*" . $claim->referrerNPI();
723 "*34" . // not allowed for 5010
724 "*" . $claim->referrerSSN();
728 if (!$CMS_5010 && $claim->referrerTaxonomy()) {
731 "*RF" . // ReFerring provider
733 "*" . $claim->referrerTaxonomy() .
737 if (!CMS_5010
&& $claim->referrerUPIN()) {
739 $out .= "REF" . // Referring Provider Secondary Identification
741 "*" . $claim->referrerUPIN() .
747 /* Per the implementation guide lines, only include this information if it is different
748 * than the Loop 2010AA information
751 ($claim->providerNPIValid() &&
752 $claim->billingFacilityNPI() !== $claim->providerNPI() ))
755 $out .= "NM1" . // Loop 2310B Rendering Provider
758 "*" . $claim->providerLastName() .
759 "*" . $claim->providerFirstName() .
760 "*" . $claim->providerMiddleName() .
763 if ($CMS_5010 ||
$claim->providerNPI()) { $out .=
765 "*" . $claim->providerNPI();
767 "*34" . // not allowed for 5010
768 "*" . $claim->providerSSN();
769 $log .= "*** Rendering provider has no NPI.\n";
773 if ($claim->providerTaxonomy()) {
776 "*PE" . // Performing provider
777 "*" .($claim->claimType() != 'MC' ?
"PXC" : "ZZ") .
778 "*" . $claim->providerTaxonomy() .
785 // This loop can only get skipped if we are generating a 5010 claim
786 if(!($claim->providerNPIValid()))
788 /* If the loop was skipped because the provider NPI was invalid, generate
789 * a warning for the log.*/
790 $log.="*** Skipping 2310B because ".$claim->providerLastName() ."," . $claim->providerFirstName() . " has invalid NPI.\n";
792 /* Skipping this segment because the providerNPI and the billingFacilityNPI are identical
793 * is a normal condition, so no need to warn.
798 // 4010: REF*1C is required here for the Medicare provider number if NPI was
799 // specified in NM109. Not sure if other payers require anything here.
800 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
802 // 5010 spec says nothing here if NPI was specified.
804 if (($CMS_5010 && !$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B','1G','G2','LU')))
805 ||
(!$CMS_5010 && trim($claim->x12gsreceiverid()) != '470819582')) // if NOT ECLAIMS EDI
807 if ($claim->providerNumber()) {
810 "*" . $claim->providerNumberType() .
811 "*" . $claim->providerNumber() .
816 // Loop 2310D is omitted in the case of home visits (POS=12).
817 if ($claim->facilityPOS() != 12 && (!$CMS_5010 ||
818 ($claim->facilityNPI() != $claim->billingFacilityNPI() ||
819 ($claim->facilityNPI() == $claim->billingFacilityNPI() &&
820 $claim->facilityStreet() != $claim->billingFacilityStreet()))))
823 $out .= "NM1" . // Loop 2310D Service Location
826 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
827 $facilityName = substr($claim->facilityName(), 0, $CMS_5010 ?
60 : 35);
828 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
831 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
836 if ($CMS_5010 ||
$claim->facilityNPI()) { $out .=
837 "*XX*" . $claim->facilityNPI();
839 "*24*" . $claim->facilityETIN();
841 if (!$claim->facilityNPI()) {
842 $log .= "*** Service location has no NPI.\n";
846 if ($claim->facilityStreet()) {
849 "*" . $claim->facilityStreet() .
852 if ($claim->facilityState()) {
855 "*" . $claim->facilityCity() .
856 "*" . $claim->facilityState() .
857 "*" . stripZipCode($claim->facilityZip()) .
862 // Segment REF (Service Facility Location Secondary Identification) omitted.
863 // Segment PER (Service Facility Contact Information) omitted.
865 // Loop 2310E, Supervising Provider
867 if ($claim->supervisorLastName()) {
870 "*DQ" . // Supervising Physician
872 "*" . $claim->supervisorLastName() .
873 "*" . $claim->supervisorFirstName() .
874 "*" . $claim->supervisorMiddleName() .
875 "*" . // NM106 not used
877 if ($CMS_5010 ||
$claim->supervisorNPI()) { $out .=
879 "*" . $claim->supervisorNPI();
882 "*" . $claim->supervisorSSN();
884 if (!$claim->supervisorNPI()) {
885 $log .= "*** Supervising Provider has no NPI.\n";
889 if ($claim->supervisorNumber()) {
892 "*" . $claim->supervisorNumberType() .
893 "*" . $claim->supervisorNumber() .
898 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
899 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
901 $prev_pt_resp = $clm_total_charges; // for computation below
903 // Loops 2320 and 2330*, other subscriber/payer information.
904 // Remember that insurance index 0 is always for the payer being billed
905 // by this claim, and 1 and above are always for the "other" payers.
907 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
909 $tmp1 = $claim->claimType($ins);
910 $tmp2 = 'C1'; // Here a kludge. See page 321.
911 if ($tmp1 === 'CI') $tmp2 = 'C1';
912 if ($tmp1 === 'AM') $tmp2 = 'AP';
913 if ($tmp1 === 'HM') $tmp2 = 'HM';
914 if ($tmp1 === 'MB') $tmp2 = 'MB';
915 if ($tmp1 === 'MC') $tmp2 = 'MC';
916 if ($tmp1 === '09') $tmp2 = 'PP';
918 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
919 "*" . $claim->payerSequence($ins) .
920 "*" . $claim->insuredRelationship($ins) .
921 "*" . $claim->groupNumber($ins) .
922 "*" . (($CMS_5010 && $claim->groupNumber($ins)) ?
'' : $claim->groupName($ins)) .
923 "*" . ($CMS_5010 ?
$claim->insuredTypeCode($ins) : $tmp2) .
927 "*" . $claim->claimType($ins) .
930 // Things that apply only to previous payers, not future payers.
932 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
934 // Generate claim-level adjustments.
935 $aarr = $claim->payerAdjustments($ins);
936 foreach ($aarr as $a) {
938 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
945 $payerpaid = $claim->payerTotals($ins);
947 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
949 "*" . $payerpaid[1] .
952 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
953 // Segment AMT*EAF (Remaining Patient Liability) omitted.
956 // Patient responsibility amount as of this previous payer.
957 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
958 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
961 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
963 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
967 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
969 "*" . sprintf('%.2f', $prev_pt_resp) .
972 } // End of things that apply only to previous payers.
976 $out .= "DMG" . // Other subscriber demographic information. Page 342.
978 "*" . $claim->insuredDOB($ins) .
979 "*" . $claim->insuredSex($ins) .
984 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
987 "*" . ($claim->billingFacilityAssignment($ins) ?
'Y' : 'N') .
988 // For this next item, the 5010 example in the spec does not match its
989 // description. So this might be wrong.
990 "*" . ($CMS_5010 ?
'' : 'B') .
995 // Segment MOA (Medicare Outpatient Adjudication) omitted.
998 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
1001 "*" . $claim->insuredLastName($ins) .
1002 "*" . $claim->insuredFirstName($ins) .
1003 "*" . $claim->insuredMiddleName($ins) .
1007 "*" . $claim->policyNumber($ins) .
1012 "*" . $claim->insuredStreet($ins) .
1017 "*" . $claim->insuredCity($ins) .
1018 "*" . $claim->insuredState($ins) .
1019 "*" . stripZipCode($claim->insuredZip($ins)) .
1022 // Segment REF (Other Subscriber Secondary Identification) omitted.
1025 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
1026 $payerName = substr($claim->payerName($ins), 0, $CMS_5010 ?
60 : 35);
1027 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
1036 "*" . $claim->payerID($ins) .
1039 // if (!$claim->payerID($ins)) {
1040 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
1043 // Payer address (N3 and N4) are added below so that Gateway EDI can
1044 // auto-generate secondary claims. These do NOT appear in my copy of
1045 // the spec! -- Rod 2008-06-12
1047 if ($CMS_5010 ||
trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
1050 "*" . $claim->payerStreet($ins) .
1055 "*" . $claim->payerCity($ins) .
1056 "*" . $claim->payerState($ins) .
1057 "*" . stripZipCode($claim->payerZip($ins)) .
1059 } // end Gateway EDI
1061 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
1062 // Segment REF (Other Payer Secondary Identifier) omitted.
1063 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
1064 // Segment REF*9F (Other Payer Referral Number) omitted.
1065 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
1066 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
1067 // Segment NM1 (Other Payer Referring Provider) omitted.
1068 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
1069 // Segment NM1 (Other Payer Rendering Provider) omitted.
1070 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
1071 // Segment NM1 (Other Payer Service Facility Location) omitted.
1072 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
1073 // Segment NM1 (Other Payer Supervising Provider) omitted.
1074 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
1075 // Segment NM1 (Other Payer Billing Provider) omitted.
1076 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
1078 } // End loops 2320/2330*.
1082 // Procedure loop starts here.
1084 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
1088 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
1093 $out .= "SV1" . // Professional Service. Page 400.
1094 "*HC:" . $claim->cptKey($prockey) .
1095 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
1097 "*" . $claim->cptUnits($prockey) .
1101 $dia = $claim->diagIndexArray($prockey);
1103 foreach ($dia as $dindex) {
1104 if ($i) $out .= ':';
1106 if (++
$i >= 4) break;
1109 if($claim->epsdtFlag()) {
1121 if (!$claim->cptCharges($prockey)) {
1122 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
1126 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
1129 // Segment SV5 (Durable Medical Equipment Service) omitted.
1130 // Segment PWK (Line Supplemental Information) omitted.
1131 // Segment PWK (Durable Medical Equipment Certificate of Medical Necessity Indicator) omitted.
1132 // Segment CR1 (Ambulance Transport Information) omitted.
1133 // Segment CR3 (Durable Medical Equipment Certification) omitted.
1134 // Segment CRC (Ambulance Certification) omitted.
1135 // Segment CRC (Hospice Employee Indicator) omitted.
1136 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
1139 $out .= "DTP" . // Date of Service. Page 435.
1142 "*" . $claim->serviceDate() .
1145 $testnote = rtrim($claim->cptNotecodes($prockey));
1146 if (!empty($testnote)) {
1148 $out .= "NTE" . // Explain Unusual Circumstances.
1150 "*" . $claim->cptNotecodes($prockey) .
1154 // Segment DTP*471 (Prescription Date) omitted.
1155 // Segment DTP*607 (Revision/Recertification Date) omitted.
1156 // Segment DTP*463 (Begin Therapy Date) omitted.
1157 // Segment DTP*461 (Last Certification Date) omitted.
1158 // Segment DTP*304 (Last Seen Date) omitted.
1159 // Segment DTP (Test Date) omitted.
1160 // Segment DTP*011 (Shipped Date) omitted.
1161 // Segment DTP*455 (Last X-Ray Date) omitted.
1162 // Segment DTP*454 (Initial Treatment Date) omitted.
1163 // Segment QTY (Ambulance Patient Count) omitted.
1164 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1165 // Segment MEA (Test Result) omitted.
1166 // Segment CN1 (Contract Information) omitted.
1167 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1168 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1169 // Segment REF*G1 (Prior Authorization) omitted.
1170 // Segment REF*6R (Line Item Control Number) omitted.
1171 // (Really oughta have this for robust 835 posting!)
1172 // Segment REF*EW (Mammography Certification Number) omitted.
1173 // Segment REF*X4 (CLIA Number) omitted.
1174 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1175 // Segment REF*BT (Immunization Batch Number) omitted.
1176 // Segment REF*9F (Referral Number) omitted.
1177 // Segment AMT*T (Sales Tax Amount) omitted.
1178 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1179 // Segment K3 (File Information) omitted.
1180 // Segment NTE (Line Note) omitted.
1181 // Segment NTE (Third Party Organization Notes) omitted.
1182 // Segment PS1 (Purchased Service Information) omitted.
1183 // Segment HCP (Line Pricing/Repricing Information) omitted.
1186 // This segment was deleted for 5010.
1188 // AMT*AAE segment for Approved Amount from previous payer.
1189 // Medicare secondaries seem to require this.
1191 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
1192 if ($claim->payerSequence($ins) > $claim->payerSequence())
1193 continue; // payer is future, not previous
1194 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1196 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1198 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1204 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1205 // with HCPCS codes.
1207 $ndc = $claim->cptNDCID($prockey);
1210 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1211 "*" . // Per addendum, LIN01 is not used.
1216 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1217 $log .= "*** NDC code '$ndc' has invalid format!\n";
1221 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1222 if (!$tmpunits) $tmpunits = 1;
1223 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1226 "*" . ($CMS_5010 ?
'' : sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits)) .
1227 "*" . $claim->cptNDCQuantity($prockey) .
1228 "*" . $claim->cptNDCUOM($prockey) .
1229 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1233 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1235 // Loop 2420A, Rendering Provider (service-specific).
1236 // Used if the rendering provider for this service line is different
1237 // from that in loop 2310B.
1239 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1241 $out .= "NM1" . // Loop 2310B Rendering Provider
1244 "*" . $claim->providerLastName($prockey) .
1245 "*" . $claim->providerFirstName($prockey) .
1246 "*" . $claim->providerMiddleName($prockey) .
1249 if ($CMS_5010 ||
$claim->providerNPI($prockey)) { $out .=
1251 "*" . $claim->providerNPI($prockey);
1253 "*34" . // Not allowed for 5010
1254 "*" . $claim->providerSSN($prockey);
1256 if (!$claim->providerNPI($prockey)) {
1257 $log .= "*** Rendering provider has no NPI.\n";
1261 if ($claim->providerTaxonomy($prockey)) {
1264 "*PE" . // PErforming provider
1265 "*" . ($CMS_5010 ?
"PXC" : "ZZ") .
1266 "*" . $claim->providerTaxonomy($prockey) .
1270 // Segment PRV*PE (Rendering Provider Specialty Information) omitted.
1271 // Segment REF (Rendering Provider Secondary Identification) omitted.
1272 // Segment NM1 (Purchased Service Provider Name) omitted.
1273 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1274 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1275 // Segment REF (Service Facility Location Secondary Identification) omitted.
1276 // Segment NM1 (Supervising Provider Name) omitted.
1277 // Segment REF (Supervising Provider Secondary Identification) omitted.
1278 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1279 // Segment REF (Ordering Provider Secondary Identification) omitted.
1280 // Segment PER (Ordering Provider Contact Information) omitted.
1281 // Segment NM1 (Referring Provider Name) omitted.
1282 // Segment REF (Referring Provider Secondary Identification) omitted.
1283 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1284 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1286 // REF*1C is required here for the Medicare provider number if NPI was
1287 // specified in NM109. Not sure if other payers require anything here.
1288 if (!$CMS_5010 && $claim->providerNumber($prockey)) {
1291 "*" . $claim->providerNumberType($prockey) .
1292 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1293 "*" . $claim->providerNumber($prockey) .
1298 // Loop 2430, adjudication by previous payers.
1300 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
1301 if ($claim->payerSequence($ins) > $claim->payerSequence())
1302 continue; // payer is future, not previous
1304 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1305 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1307 if ($payerpaid[1] == 0 && !count($aarr)) {
1308 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1309 "' has no payments or adjustments from previous payer!\n";
1314 $out .= "SVD" . // Service line adjudication. Page 554.
1315 "*" . $claim->payerID($ins) .
1316 "*" . $payerpaid[1] .
1317 "*HC:" . $claim->cptKey($prockey) .
1319 "*" . $claim->cptUnits($prockey) .
1322 $tmpdate = $payerpaid[0];
1323 foreach ($aarr as $a) {
1325 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1330 if (!$tmpdate) $tmpdate = $a[0];
1333 /*************************************************************
1334 if ( isset($a[4]) &&
1336 $out .= "CAS02" . // Previous payer's adjustment reason
1340 *************************************************************/
1345 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1352 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1353 // Segment LQ (Form Identification Code) omitted.
1354 // Segment FRM (Supporting Documentation) omitted.
1357 } // end this procedure
1360 $out .= "SE" . // SE Trailer
1365 $out .= "GE" . // GE Trailer
1370 $out .= "IEA" . // IEA Trailer
1375 // Remove any trailing empty fields (delimiters) from each segment.
1376 $out = preg_replace('/\*+~/', '~', $out);