3 * @link http://www.open-emr.org
4 * @author Rod Roark <rod@sunsetsystems.com>
5 * @copyright Copyright (c) 2009 Rod Roark <rod@sunsetsystems.com>
6 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
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)
19 $claim = new Claim($pid, $encounter);
22 // This is true for the 5010 standard, false for 4010.
23 // x12gsversionstring() should be "005010X222A1" or "004010X098A1".
24 $CMS_5010 = strpos($claim->x12gsversionstring(), '5010') !== false;
26 $log .= "Generating claim $pid-$encounter for " .
27 $claim->patientFirstName() . ' ' .
28 $claim->patientMiddleName() . ' ' .
29 $claim->patientLastName() . ' on ' .
30 date('Y-m-d H:i', $today) . ".\n";
33 "*" . $claim->x12gsisa01() .
34 "*" . $claim->x12gsisa02() .
35 "*" . $claim->x12gsisa03() .
36 "*" . $claim->x12gsisa04() .
37 "*" . $claim->x12gsisa05() .
38 "*" . $claim->x12gssenderid() .
39 "*" . $claim->x12gsisa07() .
40 "*" . $claim->x12gsreceiverid() .
43 "*" . ($CMS_5010 ?
"^" : "U" ) .
44 "*" . ($CMS_5010 ?
"00501" : "00401") .
46 "*" . $claim->x12gsisa14() .
47 "*" . $claim->x12gsisa15() .
53 "*" . $claim->x12gsgs02() .
54 "*" . trim($claim->x12gs03()) .
55 "*" . date('Ymd', $today) .
56 "*" . date('Hi', $today) .
59 "*" . $claim->x12gsversionstring() .
66 // Spec says the following is optional, so should be able to leave it out.
67 ($CMS_5010 ?
("*" . $claim->x12gsversionstring()) : "") .
72 "*0019" . // 0019 is required here
73 "*00" . // 00 = original transmission
74 "*0123" . // reference identification
75 "*" . date('Ymd', $today) . // transaction creation date
76 "*" . date('Hi', $today) . // transaction creation time
77 ($encounter_claim ?
"*RP" : "*CH") . // RP = reporting, CH = chargeable
81 // This segment was deleted for 5010.
85 "*" . $claim->x12gsversionstring() .
91 if ($claim->federalIdType() == "SY") { // check entity type for NM*102 1 == person, 2 == non-person entity
92 $tempName = $claim->billingFacilityName();
93 $partsName = explode(' ', $tempName);// Loop 1000A submitter entity == person
94 $num_parts = count($partsName);
97 $firstName = $partsName[0];
99 $lastName = $partsName[1];
103 $firstName = $partsName[0];
104 $middleName = $partsName[1];
105 $lastName = $partsName[2];
109 $firstName = $partsName[0];
110 $middleName = $partsName[1];
111 $lastName = $partsName[2];
112 $suffixName = $partsName[3];
115 $log .= "*** submitter name in 1000A loop has more than 4 parts, may not be desirable\n";
116 $firstName = $partsName[0];
117 $middleName = $partsName[1];
118 $lastName = $partsName[2];
119 $suffixName = $partsName[3];
121 $out .= "NM1" . // Loop 1000A Submitter
127 "*" . // Name Prefix not used
128 "*" . // Name Suffix not used
130 } else { //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
131 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ?
60 : 35);
132 if ($billingFacilityName == '') $log .= "*** billing facility name in 1000A loop is empty\n";
136 "*" . $billingFacilityName .
144 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
145 $out .= "*" . $claim->clearingHouseETIN();
147 $out .= "*" . $claim->billingFacilityETIN();
154 "*" . $claim->billingContactName() .
156 "*" . $claim->billingContactPhone();
157 if (!$CMS_5010 && $claim->x12gsper06()) {
158 $out .= "*ED*" . $claim->x12gsper06();
163 $out .= "NM1" . // Loop 1000B Receiver
166 "*" . $claim->clearingHouseName() .
172 "*" . $claim->clearingHouseETIN() .
178 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
182 "*1" . // 1 indicates there are child segments
185 $HLBillingPayToProvider = $HLcount++
;
187 // Situational PRV segment for provider taxonomy code for Medicaid.
188 if ($claim->claimType() == 'MC') {
190 $out .= "PRV*BI*ZZ" .
191 "*" . $claim->providerTaxonomy() .
195 // Situational CUR segment (foreign currency information) omitted here.
198 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
199 if ($claim->federalIdType() == "SY") { // check for entity type like in 1000A
200 $tempName = $claim->billingFacilityName();
201 $partsName = explode(' ', $tempName);// Loop 2010AA Billing Provider entity == person
202 $num_parts = count($partsName);
203 switch ($num_parts) {
205 $firstName = $partsName[0];
207 $lastName = $partsName[1];
211 $firstName = $partsName[0];
212 $middleName = $partsName[1];
213 $lastName = $partsName[2];
217 $firstName = $partsName[0];
218 $middleName = $partsName[1];
219 $lastName = $partsName[2];
220 $suffixName = $partsName[3];
223 $log .= "*** billing provider name in 2010AA loop has more than 4 parts, may not be desirable\n";
224 $firstName = $partsName[0];
225 $middleName = $partsName[1];
226 $lastName = $partsName[2];
227 $suffixName = $partsName[3];
235 "*" . // Name Prefix not used
239 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ?
60 : 35);
240 if ($billingFacilityName == '') $log .= "*** billing facility name in 2010A loop is empty\n";
241 $out .= "NM1" . // Loop 2010AA Billing Provider
244 "*" . $billingFacilityName .
250 if ($claim->billingFacilityNPI()) {
251 $out .= "*XX*" . $claim->billingFacilityNPI();
254 $log .= "*** Billing facility has no NPI.\n";
259 $out .= "*24*" . $claim->billingFacilityETIN();
266 "*" . $claim->billingFacilityStreet() .
271 "*" . $claim->billingFacilityCity() .
272 "*" . $claim->billingFacilityState() .
273 "*" . stripZipCode($claim->billingFacilityZip()) .
276 if ($CMS_5010 ||
($claim->billingFacilityNPI() && $claim->billingFacilityETIN())) {
279 if($claim->federalIdType()){
280 $out .= "*" . $claim->federalIdType();
283 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
285 $out .= "*" . $claim->billingFacilityETIN() .
289 if ($claim->providerNumberType() && $claim->providerNumber() &&
290 !($CMS_5010 && $claim->billingFacilityNPI()))
294 "*" . $claim->providerNumberType() .
295 "*" . $claim->providerNumber() .
298 else if ($claim->providerNumber() && !$claim->providerNumberType()) {
299 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
302 // Situational PER*1C segment omitted.
304 // Pay-To Address defaults to billing provider and is no longer required in 5010.
307 // Field length is limited to 35. See nucc dataset page 63 www.nucc.org
308 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ?
60 : 35);
309 $out .= "NM1" . // Loop 2010AB Pay-To Provider
312 "*" . $billingFacilityName .
317 if ($claim->billingFacilityNPI())
318 $out .= "*XX*" . $claim->billingFacilityNPI();
320 $out .= "*24*" . $claim->billingFacilityETIN();
325 "*" . $claim->billingFacilityStreet() .
330 "*" . $claim->billingFacilityCity() .
331 "*" . $claim->billingFacilityState() .
332 "*" . stripZipCode($claim->billingFacilityZip()) .
335 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
339 "*" . $claim->billingFacilityETIN() .
344 // Loop 2010AC Pay-To Plan Name omitted. Includes:
345 // NM1*PE, N3, N4, REF*2U, REF*EI
347 $PatientHL = $claim->isSelfOfInsured() ?
0 : 1;
348 $HLSubscriber = $HLcount++
;
351 $out .= "HL" . // Loop 2000B Subscriber HL Loop
353 "*$HLBillingPayToProvider" .
358 if (!$claim->payerSequence()) {
359 $log .= "*** Error: Insurance information is missing!\n";
363 $out .= "SBR" . // Subscriber Information
364 "*" . $claim->payerSequence() .
365 "*" . ($claim->isSelfOfInsured() ?
'18' : '') .
366 "*" . $claim->groupNumber() .
367 "*" . (($CMS_5010 && $claim->groupNumber()) ?
'' : $claim->groupName()) .
368 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
372 "*" . $claim->claimType() . // Zirmed replaces this
375 // Segment PAT omitted.
378 $out .= "NM1" . // Loop 2010BA Subscriber
380 "*1" . // 1 = person, 2 = non-person
381 "*" . $claim->insuredLastName() .
382 "*" . $claim->insuredFirstName() .
383 "*" . $claim->insuredMiddleName() .
387 // "MI" = Member Identification Number
388 // "II" = Standard Unique Health Identifier, "Required if the
389 // HIPAA Individual Patient Identifier is mandated use."
390 // Here we presume that is not true yet.
391 "*" . $claim->policyNumber() .
394 // For 5010, further subscriber info is sent only if they are the patient.
395 if (!$CMS_5010 ||
$claim->isSelfOfInsured()) {
398 "*" . $claim->insuredStreet() .
403 "*" . $claim->insuredCity() .
404 "*" . $claim->insuredState() .
405 "*" . stripZipCode($claim->insuredZip()) .
411 "*" . $claim->insuredDOB() .
412 "*" . $claim->insuredSex() .
416 // Segment REF*SY (Subscriber Secondary Identification) omitted.
417 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
418 // Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
421 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
422 $payerName = substr($claim->payerName(), 0, $CMS_5010 ?
60 : 35);
423 $out .= "NM1" . // Loop 2010BB Payer
431 // The 5010 spec says:
432 // "On or after the mandated implementation date for the HIPAA
433 // National Plan Identifier (National Plan ID), XV must be sent.
434 // Prior to the mandated implementation date and prior to any phase-
435 // in period identified by Federal regulation, PI must be sent."
436 // *************** Anybody know what that date is? ***************
437 // August 2011 - Publish interim final rule
438 // October 1, 2012-March 31, 2013 - Enumeration
439 // April 1, 2013-September 30, 2013 - Testing
440 // October 1, 2013 - Implementation
443 // Zirmed ignores this if using payer name matching:
444 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
447 // if (!$claim->payerID()) {
448 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
451 if (true) { // !$CMS_5010
452 // The 5010 spec says:
453 // "Required when the payer address is available and the submitter intends
454 // for the claim to be printed on paper at the next EDI location (for example, a
455 // clearinghouse). If not required by this implementation guide, do not send."
459 "*" . $claim->payerStreet() .
464 "*" . $claim->payerCity() .
465 "*" . $claim->payerState() .
466 "*" . stripZipCode($claim->payerZip()) .
470 // Segment REF (Payer Secondary Identification) omitted.
471 // Segment REF (Billing Provider Secondary Identification) omitted.
473 if (! $claim->isSelfOfInsured()) {
475 $out .= "HL" . // Loop 2000C Patient Information
486 "*" . $claim->insuredRelationship() .
490 $out .= "NM1" . // Loop 2010CA Patient
493 "*" . $claim->patientLastName() .
494 "*" . $claim->patientFirstName();
495 if ($claim->patientMiddleName() !== '') $out .= "*"
496 . $claim->patientMiddleName();
501 "*" . $claim->patientStreet() .
506 "*" . $claim->patientCity() .
507 "*" . $claim->patientState() .
508 "*" . stripZipCode($claim->patientZip()) .
514 "*" . $claim->patientDOB() .
515 "*" . $claim->patientSex() .
518 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
519 // Segment REF (Property and Casualty Patient Identifier) omitted.
520 // Segment PER (Property and Casualty Patient Contact Information) omitted.
522 } // end of patient different from insured
524 $proccount = $claim->procCount();
526 $clm_total_charges = 0;
527 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
528 $clm_total_charges +
= $claim->cptCharges($prockey);
531 if (!$clm_total_charges) {
532 $log .= "*** This claim has no charges!\n";
536 $out .= "CLM" . // Loop 2300 Claim
538 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
541 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" .
542 ($CMS_5010 ?
"B" : "") . ":" .
543 $claim->frequencyTypeCode() . // Changed to correct single digit output
546 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
548 ($CMS_5010 ?
"" : "*C") .
551 if ($claim->onsetDate() &&
552 ($claim->onsetDate()!== $claim->serviceDate()) &&
553 ($claim->onsetDateValid())
556 $out .= "DTP" . // Date of Onset
559 "*" . $claim->onsetDate() .
563 // above is for historical use of encounter onset date, now in misc_billing_options
564 // Segment DTP*431 (Onset of Current Symptoms or Illness)
565 // Segment DTP*484 (Last Menstrual Period Date)
567 if ($claim->miscOnsetDate() &&
568 ($claim->box14Qualifier()) &&
569 ($claim->miscOnsetDateValid())) {
571 $out .= "DTP" . // Date Last Seen
572 "*" . $claim->box14Qualifier() .
574 "*" . $claim->miscOnsetDate() .
578 // Segment DTP*454 (Initial Treatment Date)
579 // Segment DTP*304 (Last Seen Date)
580 // Segment DTP*453 (Acute Manifestation Date)
581 // Segment DTP*439 (Accident Date)
582 // Segment DTP*455 (Last X-Ray Date)
583 // Segment DTP*471 (Hearing and Vision Prescription Date)
584 // Segment DTP*314 (Disability) omitted.
585 // Segment DTP*360 (Initial Disability Period Start) omitted.
586 // Segment DTP*361 (Initial Disability Period End) omitted.
587 // Segment DTP*297 (Last Worked Date)
588 // Segment DTP*296 (Authorized Return to Work Date)
590 if ($claim->dateInitialTreatment() &&
591 ($claim->box15Qualifier()) &&
592 ($claim->dateInitialTreatmentValid())){
594 $out .= "DTP" . // Date Last Seen
595 "*" . $claim->box15Qualifier() .
597 "*" . $claim->dateInitialTreatment() .
601 if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->onsetDateValid() ) {
603 $out .= "DTP" . // Date of Hospitalization
606 "*" . $claim->onsetDate() .
610 // above is for historical use of encounter onset date, now in misc_billing_options
611 if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->hospitalizedFromDateValid() ) {
613 $out .= "DTP" . // Date of Admission
616 "*" . $claim->hospitalizedFrom() .
620 // Segment DTP*096 (Discharge Date)
621 if (strcmp($claim->facilityPOS(),'21') == 0 && $claim->hospitalizedToDateValid() ) {
623 $out .= "DTP" . // Date of Discharge
626 "*" . $claim->hospitalizedTo() .
630 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
631 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
632 // Segment DTP*050 (Repricer Received Date) omitted.
633 // Segment PWK (Claim Supplemental Information) omitted.
634 // Segment CN1 (Contract Information) omitted.
636 $patientpaid = $claim->patientPaidAmount();
637 if ($patientpaid != 0) {
639 $out .= "AMT" . // Patient paid amount. Page 190/220.
645 // Segment REF*4N (Service Authorization Exception Code) omitted.
646 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
647 // Segment REF*EW (Mammography Certification Number) omitted.
648 // Segment REF*9F (Referral Number) omitted.
650 if ($claim->priorAuth()) {
652 $out .= "REF" . // Prior Authorization Number
654 "*" . $claim->priorAuth() .
658 // Segment REF*F8 Payer Claim Control Number for claim re-submission.icn_resubmission_number
660 #if($claim->billing_options['replacement_claim'] == '1'){
661 if(trim($claim->billing_options
['icn_resubmission_number']) > 3){
663 error_log("Method 1: ".$claim->billing_options
['icn_resubmission_number'], 0);
666 "*" . $claim->icnResubmissionNumber() .
671 if ($claim->cliaCode() && ($CMS_5010 ||
$claim->claimType() === 'MB')) {
672 // Required by Medicare when in-house labs are done.
674 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
676 "*" . $claim->cliaCode() .
680 // Segment REF*9A (Repriced Claim Number) omitted.
681 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
682 // Segment REF*LX (Investigational Device Exemption Number) omitted.
683 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
684 // Segment REF*EA (Medical Record Number) omitted.
685 // Segment REF*P4 (Demonstration Project Identifier) omitted.
686 // Segment REF*1J (Care Plan Oversight) omitted.
687 // Segment K3 (File Information) omitted.
689 if ($claim->additionalNotes()) {
692 $out .= "NTE" . // comments box 19
693 "*" . ($CMS_5010 ?
"ADD" : "") .
694 "*" . $claim->additionalNotes() .
698 // Segment CR1 (Ambulance Transport Information) omitted.
699 // Segment CR2 (Spinal Manipulation Service Information) omitted.
700 // Segment CRC (Ambulance Certification) omitted.
701 // Segment CRC (Patient Condition Information: Vision) omitted.
702 // Segment CRC (Homebound Indicator) omitted.
704 // Segment CRC (EPSDT Referral).
705 if($claim->epsdtFlag()) {
710 "*" . $claim->medicaidReferralCode() .
714 // Diagnoses, up to $max_per_seg per HI segment.
715 $max_per_seg = $CMS_5010 ?
12 : 8;
716 $da = $claim->diagArray();
717 if ($claim->diagtype
== "ICD9") {
718 $diag_type_code = 'BK';
722 $diag_type_code = 'ABK';
725 foreach ($da as $diag) {
726 if ($tmp %
$max_per_seg == 0) {
727 if ($tmp) $out .= "~\n";
729 $out .= "HI"; // Health Diagnosis Codes
731 $out .= "*$diag_type_code:" . $diag;
732 if ($claim->diagtype
== "ICD9") {
733 $diag_type_code = 'BF';
737 $diag_type_code = 'ABF';
741 if ($tmp) $out .= "~\n";
743 // Segment HI*BP (Anesthesia Related Procedure) omitted.
744 // Segment HI*BG (Condition Information) omitted.
745 // Segment HCP (Claim Pricing/Repricing Information) omitted.
747 if ($claim->referrerLastName()) {
748 // Medicare requires referring provider's name and UPIN.
750 $out .= "NM1" . // Loop 2310A Referring Provider
753 "*" . $claim->referrerLastName() .
754 "*" . $claim->referrerFirstName() .
755 "*" . $claim->referrerMiddleName() .
758 if ($CMS_5010 ||
$claim->referrerNPI()) { $out .=
760 "*" . $claim->referrerNPI();
762 "*34" . // not allowed for 5010
763 "*" . $claim->referrerSSN();
767 if (!$CMS_5010 && $claim->referrerTaxonomy()) {
770 "*RF" . // ReFerring provider
772 "*" . $claim->referrerTaxonomy() .
776 if (!CMS_5010
&& $claim->referrerUPIN()) {
778 $out .= "REF" . // Referring Provider Secondary Identification
780 "*" . $claim->referrerUPIN() .
786 /* Per the implementation guide lines, only include this information if it is different
787 * than the Loop 2010AA information
790 ($claim->providerNPIValid() &&
791 $claim->billingFacilityNPI() !== $claim->providerNPI() ))
794 $out .= "NM1" . // Loop 2310B Rendering Provider
797 "*" . $claim->providerLastName() .
798 "*" . $claim->providerFirstName() .
799 "*" . $claim->providerMiddleName() .
802 if ($CMS_5010 ||
$claim->providerNPI()) { $out .=
804 "*" . $claim->providerNPI();
806 "*34" . // not allowed for 5010
807 "*" . $claim->providerSSN();
808 $log .= "*** Rendering provider has no NPI.\n";
812 if ($claim->providerTaxonomy()) {
815 "*PE" . // Performing provider
816 "*" .($claim->claimType() != 'MC' ?
"PXC" : "ZZ") .
817 "*" . $claim->providerTaxonomy() .
824 // This loop can only get skipped if we are generating a 5010 claim
825 if(!($claim->providerNPIValid()))
827 /* If the loop was skipped because the provider NPI was invalid, generate
828 * a warning for the log.*/
829 $log.="*** Skipping 2310B because ".$claim->providerLastName() ."," . $claim->providerFirstName() . " has invalid NPI.\n";
831 /* Skipping this segment because the providerNPI and the billingFacilityNPI are identical
832 * is a normal condition, so no need to warn.
837 // 4010: REF*1C is required here for the Medicare provider number if NPI was
838 // specified in NM109. Not sure if other payers require anything here.
839 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
841 // 5010 spec says nothing here if NPI was specified.
843 if (($CMS_5010 && !$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B','1G','G2','LU')))
844 ||
(!$CMS_5010 && trim($claim->x12gsreceiverid()) != '470819582')) // if NOT ECLAIMS EDI
846 if ($claim->providerNumber()) {
849 "*" . $claim->providerNumberType() .
850 "*" . $claim->providerNumber() .
855 // Loop 2310D is omitted in the case of home visits (POS=12).
856 if ($claim->facilityPOS() != 12 && (!$CMS_5010 ||
857 ($claim->facilityNPI() != $claim->billingFacilityNPI() ||
858 ($claim->facilityNPI() == $claim->billingFacilityNPI() &&
859 $claim->facilityStreet() != $claim->billingFacilityStreet()))))
862 $out .= "NM1" . // Loop 2310D Service Location
865 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
866 $facilityName = substr($claim->facilityName(), 0, $CMS_5010 ?
60 : 35);
867 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
870 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
875 if ($CMS_5010 ||
$claim->facilityNPI()) { $out .=
876 "*XX*" . $claim->facilityNPI();
878 "*24*" . $claim->facilityETIN();
880 if (!$claim->facilityNPI()) {
881 $log .= "*** Service location has no NPI.\n";
885 if ($claim->facilityStreet()) {
888 "*" . $claim->facilityStreet() .
891 if ($claim->facilityState()) {
894 "*" . $claim->facilityCity() .
895 "*" . $claim->facilityState() .
896 "*" . stripZipCode($claim->facilityZip()) .
901 // Segment REF (Service Facility Location Secondary Identification) omitted.
902 // Segment PER (Service Facility Contact Information) omitted.
904 // Loop 2310E, Supervising Provider
906 if ($claim->supervisorLastName()) {
909 "*DQ" . // Supervising Physician
911 "*" . $claim->supervisorLastName() .
912 "*" . $claim->supervisorFirstName() .
913 "*" . $claim->supervisorMiddleName() .
914 "*" . // NM106 not used
916 if ($CMS_5010 ||
$claim->supervisorNPI()) { $out .=
918 "*" . $claim->supervisorNPI();
921 "*" . $claim->supervisorSSN();
923 if (!$claim->supervisorNPI()) {
924 $log .= "*** Supervising Provider has no NPI.\n";
928 if ($claim->supervisorNumber()) {
931 "*" . $claim->supervisorNumberType() .
932 "*" . $claim->supervisorNumber() .
937 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
938 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
940 $prev_pt_resp = $clm_total_charges; // for computation below
942 // Loops 2320 and 2330*, other subscriber/payer information.
943 // Remember that insurance index 0 is always for the payer being billed
944 // by this claim, and 1 and above are always for the "other" payers.
946 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
948 $tmp1 = $claim->claimType($ins);
949 $tmp2 = 'C1'; // Here a kludge. See page 321.
950 if ($tmp1 === 'CI') $tmp2 = 'C1';
951 if ($tmp1 === 'AM') $tmp2 = 'AP';
952 if ($tmp1 === 'HM') $tmp2 = 'HM';
953 if ($tmp1 === 'MB') $tmp2 = 'MB';
954 if ($tmp1 === 'MC') $tmp2 = 'MC';
955 if ($tmp1 === '09') $tmp2 = 'PP';
957 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
958 "*" . $claim->payerSequence($ins) .
959 "*" . $claim->insuredRelationship($ins) .
960 "*" . $claim->groupNumber($ins) .
961 "*" . (($CMS_5010 && $claim->groupNumber($ins)) ?
'' : $claim->groupName($ins)) .
962 "*" . ($CMS_5010 ?
$claim->insuredTypeCode($ins) : $tmp2) .
966 "*" . $claim->claimType($ins) .
969 // Things that apply only to previous payers, not future payers.
971 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
973 // Generate claim-level adjustments.
974 $aarr = $claim->payerAdjustments($ins);
975 foreach ($aarr as $a) {
977 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
984 $payerpaid = $claim->payerTotals($ins);
986 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
988 "*" . $payerpaid[1] .
991 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
992 // Segment AMT*EAF (Remaining Patient Liability) omitted.
995 // Patient responsibility amount as of this previous payer.
996 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
997 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
1000 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
1002 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
1006 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
1008 "*" . sprintf('%.2f', $prev_pt_resp) .
1011 } // End of things that apply only to previous payers.
1015 $out .= "DMG" . // Other subscriber demographic information. Page 342.
1017 "*" . $claim->insuredDOB($ins) .
1018 "*" . $claim->insuredSex($ins) .
1023 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
1026 "*" . ($claim->billingFacilityAssignment($ins) ?
'Y' : 'N') .
1027 // For this next item, the 5010 example in the spec does not match its
1028 // description. So this might be wrong.
1029 "*" . ($CMS_5010 ?
'' : 'B') .
1034 // Segment MOA (Medicare Outpatient Adjudication) omitted.
1037 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
1040 "*" . $claim->insuredLastName($ins) .
1041 "*" . $claim->insuredFirstName($ins) .
1042 "*" . $claim->insuredMiddleName($ins) .
1046 "*" . $claim->policyNumber($ins) .
1051 "*" . $claim->insuredStreet($ins) .
1056 "*" . $claim->insuredCity($ins) .
1057 "*" . $claim->insuredState($ins) .
1058 "*" . stripZipCode($claim->insuredZip($ins)) .
1061 // Segment REF (Other Subscriber Secondary Identification) omitted.
1064 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
1065 $payerName = substr($claim->payerName($ins), 0, $CMS_5010 ?
60 : 35);
1066 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
1075 "*" . $claim->payerID($ins) .
1078 // if (!$claim->payerID($ins)) {
1079 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
1082 // Payer address (N3 and N4) are added below so that Gateway EDI can
1083 // auto-generate secondary claims. These do NOT appear in my copy of
1084 // the spec! -- Rod 2008-06-12
1086 if ($CMS_5010 ||
trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
1089 "*" . $claim->payerStreet($ins) .
1094 "*" . $claim->payerCity($ins) .
1095 "*" . $claim->payerState($ins) .
1096 "*" . stripZipCode($claim->payerZip($ins)) .
1098 } // end Gateway EDI
1100 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
1101 // Segment REF (Other Payer Secondary Identifier) omitted.
1102 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
1103 // Segment REF*9F (Other Payer Referral Number) omitted.
1104 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
1105 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
1106 // Segment NM1 (Other Payer Referring Provider) omitted.
1107 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
1108 // Segment NM1 (Other Payer Rendering Provider) omitted.
1109 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
1110 // Segment NM1 (Other Payer Service Facility Location) omitted.
1111 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
1112 // Segment NM1 (Other Payer Supervising Provider) omitted.
1113 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
1114 // Segment NM1 (Other Payer Billing Provider) omitted.
1115 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
1117 } // End loops 2320/2330*.
1121 // Procedure loop starts here.
1123 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
1127 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
1132 $out .= "SV1" . // Professional Service. Page 400.
1133 "*HC:" . $claim->cptKey($prockey) .
1134 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
1136 "*" . $claim->cptUnits($prockey) .
1140 $dia = $claim->diagIndexArray($prockey);
1142 foreach ($dia as $dindex) {
1143 if ($i) $out .= ':';
1145 if (++
$i >= 4) break;
1148 if($claim->epsdtFlag()) {
1160 if (!$claim->cptCharges($prockey)) {
1161 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
1165 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
1168 // Segment SV5 (Durable Medical Equipment Service) omitted.
1169 // Segment PWK (Line Supplemental Information) omitted.
1170 // Segment PWK (Durable Medical Equipment Certificate of Medical Necessity Indicator) omitted.
1171 // Segment CR1 (Ambulance Transport Information) omitted.
1172 // Segment CR3 (Durable Medical Equipment Certification) omitted.
1173 // Segment CRC (Ambulance Certification) omitted.
1174 // Segment CRC (Hospice Employee Indicator) omitted.
1175 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
1178 $out .= "DTP" . // Date of Service. Page 435.
1181 "*" . $claim->serviceDate() .
1184 $testnote = rtrim($claim->cptNotecodes($prockey));
1185 if (!empty($testnote)) {
1187 $out .= "NTE" . // Explain Unusual Circumstances.
1189 "*" . $claim->cptNotecodes($prockey) .
1193 // Segment DTP*471 (Prescription Date) omitted.
1194 // Segment DTP*607 (Revision/Recertification Date) omitted.
1195 // Segment DTP*463 (Begin Therapy Date) omitted.
1196 // Segment DTP*461 (Last Certification Date) omitted.
1197 // Segment DTP*304 (Last Seen Date) omitted.
1198 // Segment DTP (Test Date) omitted.
1199 // Segment DTP*011 (Shipped Date) omitted.
1200 // Segment DTP*455 (Last X-Ray Date) omitted.
1201 // Segment DTP*454 (Initial Treatment Date) omitted.
1202 // Segment QTY (Ambulance Patient Count) omitted.
1203 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
1204 // Segment MEA (Test Result) omitted.
1205 // Segment CN1 (Contract Information) omitted.
1206 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
1207 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
1208 // Segment REF*G1 (Prior Authorization) omitted.
1209 // Segment REF*6R (Line Item Control Number) omitted.
1210 // (Really oughta have this for robust 835 posting!)
1211 // Segment REF*EW (Mammography Certification Number) omitted.
1212 // Segment REF*X4 (CLIA Number) omitted.
1213 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1214 // Segment REF*BT (Immunization Batch Number) omitted.
1215 // Segment REF*9F (Referral Number) omitted.
1216 // Segment AMT*T (Sales Tax Amount) omitted.
1217 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1218 // Segment K3 (File Information) omitted.
1219 // Segment NTE (Line Note) omitted.
1220 // Segment NTE (Third Party Organization Notes) omitted.
1221 // Segment PS1 (Purchased Service Information) omitted.
1222 // Segment HCP (Line Pricing/Repricing Information) omitted.
1225 // This segment was deleted for 5010.
1227 // AMT*AAE segment for Approved Amount from previous payer.
1228 // Medicare secondaries seem to require this.
1230 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
1231 if ($claim->payerSequence($ins) > $claim->payerSequence())
1232 continue; // payer is future, not previous
1233 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1235 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1237 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1243 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1244 // with HCPCS codes.
1246 $ndc = $claim->cptNDCID($prockey);
1249 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1250 "*" . // Per addendum, LIN01 is not used.
1255 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1256 $log .= "*** NDC code '$ndc' has invalid format!\n";
1260 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1261 if (!$tmpunits) $tmpunits = 1;
1262 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1265 "*" . ($CMS_5010 ?
'' : sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits)) .
1266 "*" . $claim->cptNDCQuantity($prockey) .
1267 "*" . $claim->cptNDCUOM($prockey) .
1268 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1272 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1274 // Loop 2420A, Rendering Provider (service-specific).
1275 // Used if the rendering provider for this service line is different
1276 // from that in loop 2310B.
1278 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1280 $out .= "NM1" . // Loop 2310B Rendering Provider
1283 "*" . $claim->providerLastName($prockey) .
1284 "*" . $claim->providerFirstName($prockey) .
1285 "*" . $claim->providerMiddleName($prockey) .
1288 if ($CMS_5010 ||
$claim->providerNPI($prockey)) { $out .=
1290 "*" . $claim->providerNPI($prockey);
1292 "*34" . // Not allowed for 5010
1293 "*" . $claim->providerSSN($prockey);
1295 if (!$claim->providerNPI($prockey)) {
1296 $log .= "*** Rendering provider has no NPI.\n";
1300 if ($claim->providerTaxonomy($prockey)) {
1303 "*PE" . // PErforming provider
1304 "*" . ($CMS_5010 ?
"PXC" : "ZZ") .
1305 "*" . $claim->providerTaxonomy($prockey) .
1309 // Segment PRV*PE (Rendering Provider Specialty Information) omitted.
1310 // Segment REF (Rendering Provider Secondary Identification) omitted.
1311 // Segment NM1 (Purchased Service Provider Name) omitted.
1312 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1313 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1314 // Segment REF (Service Facility Location Secondary Identification) omitted.
1315 // Segment NM1 (Supervising Provider Name) omitted.
1316 // Segment REF (Supervising Provider Secondary Identification) omitted.
1317 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1318 // Segment REF (Ordering Provider Secondary Identification) omitted.
1319 // Segment PER (Ordering Provider Contact Information) omitted.
1320 // Segment NM1 (Referring Provider Name) omitted.
1321 // Segment REF (Referring Provider Secondary Identification) omitted.
1322 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1323 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1325 // REF*1C is required here for the Medicare provider number if NPI was
1326 // specified in NM109. Not sure if other payers require anything here.
1327 if (!$CMS_5010 && $claim->providerNumber($prockey)) {
1330 "*" . $claim->providerNumberType($prockey) .
1331 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1332 "*" . $claim->providerNumber($prockey) .
1337 // Loop 2430, adjudication by previous payers.
1339 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
1340 if ($claim->payerSequence($ins) > $claim->payerSequence())
1341 continue; // payer is future, not previous
1343 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1344 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1346 if ($payerpaid[1] == 0 && !count($aarr)) {
1347 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1348 "' has no payments or adjustments from previous payer!\n";
1353 $out .= "SVD" . // Service line adjudication. Page 554.
1354 "*" . $claim->payerID($ins) .
1355 "*" . $payerpaid[1] .
1356 "*HC:" . $claim->cptKey($prockey) .
1358 "*" . $claim->cptUnits($prockey) .
1361 $tmpdate = $payerpaid[0];
1362 foreach ($aarr as $a) {
1364 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1369 if (!$tmpdate) $tmpdate = $a[0];
1372 /*************************************************************
1373 if ( isset($a[4]) &&
1375 $out .= "CAS02" . // Previous payer's adjustment reason
1379 *************************************************************/
1384 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1391 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1392 // Segment LQ (Form Identification Code) omitted.
1393 // Segment FRM (Supporting Documentation) omitted.
1396 } // end this procedure
1399 $out .= "SE" . // SE Trailer
1404 $out .= "GE" . // GE Trailer
1409 $out .= "IEA" . // IEA Trailer
1414 // Remove any trailing empty fields (delimiters) from each segment.
1415 $out = preg_replace('/\*+~/', '~', $out);