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");
11 function gen_x12_837($pid, $encounter, &$log, $encounter_claim=false) {
15 $claim = new Claim($pid, $encounter);
18 $log .= "Generating claim $pid-$encounter for " .
19 $claim->patientFirstName() . ' ' .
20 $claim->patientMiddleName() . ' ' .
21 $claim->patientLastName() . ' on ' .
22 date('Y-m-d H:i', $today) . ".\n";
29 "*" . $claim->x12gsisa05() .
30 "*" . $claim->x12gssenderid() .
31 "*" . $claim->x12gsisa07() .
32 "*" . $claim->x12gsreceiverid() .
38 "*" . $claim->x12gsisa14() .
39 "*" . $claim->x12gsisa15() .
45 "*" . $claim->x12gsgs02() .
46 "*" . trim($claim->x12gsreceiverid()) .
47 "*" . date('Ymd', $today) .
48 "*" . date('Hi', $today) .
51 "*" . $claim->x12gsversionstring() .
65 "*" . date('Ymd', $today) .
67 ($encounter_claim ?
"*RP" : "*CH") .
73 "*" . $claim->x12gsversionstring() .
77 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
78 $billingFacilityName=substr($claim->billingFacilityName(),0,35);
79 $out .= "NM1" . // Loop 1000A Submitter
82 "*" . $billingFacilityName .
88 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
89 $out .= "*" . $claim->clearingHouseETIN();
91 $out .= "*" . $claim->billingFacilityETIN();
98 "*" . $claim->billingContactName() .
100 "*" . $claim->billingContactPhone();
101 if ($claim->x12gsper06()) {
102 $out .= "*ED*" . $claim->x12gsper06();
107 $out .= "NM1" . // Loop 1000B Receiver
110 "*" . $claim->clearingHouseName() .
116 "*" . $claim->clearingHouseETIN() .
122 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
129 $HLBillingPayToProvider = $HLcount++
;
132 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
133 $billingFacilityName=substr($claim->billingFacilityName(),0,35);
134 $out .= "NM1" . // Loop 2010AA Billing Provider
137 "*" . $billingFacilityName .
142 if ($claim->billingFacilityNPI()) {
143 $out .= "*XX*" . $claim->billingFacilityNPI();
145 $log .= "*** Billing facility has no NPI.\n";
146 $out .= "*24*" . $claim->billingFacilityETIN();
152 "*" . $claim->billingFacilityStreet() .
157 "*" . $claim->billingFacilityCity() .
158 "*" . $claim->billingFacilityState() .
159 "*" . $claim->billingFacilityZip() .
162 // Add a REF*EI*<ein> segment if NPI was specified in the NM1 above.
163 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
166 if($claim->federalIdType()){
167 $out .= "*" . $claim->federalIdType();
170 $out .= "*EI";//For dealing with the situation before adding selection for TaxId type In facility ie default to EIN.
172 $out .= "*" . $claim->billingFacilityETIN() .
176 if ($claim->providerNumberType() && $claim->providerNumber()) {
179 "*" . $claim->providerNumberType() .
180 "*" . $claim->providerNumber() .
183 else if ($claim->providerNumber()) {
184 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
188 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
189 $billingFacilityName=substr($claim->billingFacilityName(),0,35);
190 $out .= "NM1" . // Loop 2010AB Pay-To Provider
193 "*" . $billingFacilityName .
198 if ($claim->billingFacilityNPI())
199 $out .= "*XX*" . $claim->billingFacilityNPI();
201 $out .= "*24*" . $claim->billingFacilityETIN();
206 "*" . $claim->billingFacilityStreet() .
211 "*" . $claim->billingFacilityCity() .
212 "*" . $claim->billingFacilityState() .
213 "*" . $claim->billingFacilityZip() .
216 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
220 "*" . $claim->billingFacilityETIN() .
223 if($claim->isSelfOfInsured()){
231 $out .= "HL" . // Loop 2000B Subscriber HL Loop
233 "*$HLBillingPayToProvider" .
238 $HLSubscriber = $HLcount++
;
240 if (!$claim->payerSequence()) {
241 $log .= "*** Error: Insurance information is missing!\n";
245 if($claim->isSelfOfInsured()){
248 $out .= "SBR" . // Subscriber Information
249 "*" . $claim->payerSequence() .
251 "*" . $claim->groupNumber() .
252 "*" . $claim->groupName() .
253 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
257 "*" . $claim->claimType() . // Zirmed replaces this
261 $out .= "NM1" . // Loop 2010BA Subscriber
264 "*" . $claim->insuredLastName() .
265 "*" . $claim->insuredFirstName() .
266 "*" . $claim->insuredMiddleName() .
270 "*" . $claim->policyNumber() .
275 "*" . $claim->insuredStreet() .
280 "*" . $claim->insuredCity() .
281 "*" . $claim->insuredState() .
282 "*" . $claim->insuredZip() .
288 "*" . $claim->insuredDOB() .
289 "*" . $claim->insuredSex() .
293 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
294 $payerName=substr($claim->payerName(),0,35);
295 $out .= "NM1" . // Loop 2010BB Payer
304 // Zirmed ignores this if using payer name matching:
305 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
308 // if (!$claim->payerID()) {
309 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
314 "*" . $claim->payerStreet() .
319 "*" . $claim->payerCity() .
320 "*" . $claim->payerState() .
321 "*" . $claim->payerZip() .
324 if (! $claim->isSelfOfInsured()) {
326 $out .= "HL" . // Loop 2000C Patient Information
337 "*" . $claim->insuredRelationship() .
341 $out .= "NM1" . // Loop 2010CA Patient
344 "*" . $claim->patientLastName() .
345 "*" . $claim->patientFirstName() .
346 "*" . $claim->patientMiddleName() .
351 "*" . $claim->patientStreet() .
356 "*" . $claim->patientCity() .
357 "*" . $claim->patientState() .
358 "*" . $claim->patientZip() .
364 "*" . $claim->patientDOB() .
365 "*" . $claim->patientSex() .
367 } // end of patient different from insured
369 $proccount = $claim->procCount();
371 $clm_total_charges = 0;
372 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
373 $clm_total_charges +
= $claim->cptCharges($prockey);
376 if (!$clm_total_charges) {
377 $log .= "*** This claim has no charges!\n";
382 $out .= "CLM" . // Loop 2300 Claim
384 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
387 "*" . sprintf('%02d', $claim->facilityPOS()) . "::" . $claim->frequencyTypeCode() . // Changed to correct single digit output
390 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
395 if ($claim->dateInitialTreatment()) {
397 $out .= "DTP" . // Date of Initial Treatment
400 "*" . $claim->dateInitialTreatment() .
405 $out .= "DTP" . // Date of Onset
408 "*" . $claim->onsetDate() .
411 if (strcmp($claim->facilityPOS(),'21') == 0) {
413 $out .= "DTP" . // Date of Hospitalization
416 "*" . $claim->onsetDate() .
420 $patientpaid = $claim->patientPaidAmount();
421 if ($patientpaid != 0) {
423 $out .= "AMT" . // Patient paid amount. Page 220.
429 if ($claim->priorAuth()) {
431 $out .= "REF" . // Prior Authorization Number
433 "*" . $claim->priorAuth() .
437 if ($claim->cliaCode() and $claim->claimType() === 'MB') {
438 // Required by Medicare when in-house labs are done.
440 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
442 "*" . $claim->cliaCode() .
446 // Note: This would be the place to implement the NTE segment for loop 2300.
447 if ($claim->additionalNotes()) {
450 $out .= "NTE" . // comments box 19
452 "*" . $claim->additionalNotes() .
456 // Diagnoses, up to 8 per HI segment.
457 $da = $claim->diagArray();
458 $diag_type_code = 'BK';
460 foreach ($da as $diag) {
462 if ($tmp) $out .= "~\n";
464 $out .= "HI"; // Health Diagnosis Codes
466 $out .= "*$diag_type_code:" . $diag;
467 $diag_type_code = 'BF';
470 if ($tmp) $out .= "~\n";
472 if ($claim->referrerLastName()) {
473 // Medicare requires referring provider's name and UPIN.
475 $out .= "NM1" . // Loop 2310A Referring Provider
478 "*" . $claim->referrerLastName() .
479 "*" . $claim->referrerFirstName() .
480 "*" . $claim->referrerMiddleName() .
483 if ($claim->referrerNPI()) { $out .=
485 "*" . $claim->referrerNPI();
488 "*" . $claim->referrerSSN();
492 if ($claim->referrerTaxonomy()) {
495 "*RF" . // ReFerring provider
497 "*" . $claim->referrerTaxonomy() .
501 if ($claim->referrerUPIN()) {
503 $out .= "REF" . // Referring Provider Secondary Identification
505 "*" . $claim->referrerUPIN() .
511 $out .= "NM1" . // Loop 2310B Rendering Provider
514 "*" . $claim->providerLastName() .
515 "*" . $claim->providerFirstName() .
516 "*" . $claim->providerMiddleName() .
519 if ($claim->providerNPI()) { $out .=
521 "*" . $claim->providerNPI();
524 "*" . $claim->providerSSN();
525 $log .= "*** Rendering provider has no NPI.\n";
529 if ($claim->providerTaxonomy()) {
532 "*PE" . // PErforming provider
534 "*" . $claim->providerTaxonomy() .
538 // REF*1C is required here for the Medicare provider number if NPI was
539 // specified in NM109. Not sure if other payers require anything here.
540 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
542 if (trim($claim->x12gsreceiverid()) != '470819582') { // if NOT ECLAIMS EDI
543 if ($claim->providerNumber()) {
546 "*" . $claim->providerNumberType() .
547 "*" . $claim->providerNumber() .
552 // Loop 2310D is omitted in the case of home visits (POS=12).
553 if ($claim->facilityPOS() != 12) {
555 $out .= "NM1" . // Loop 2310D Service Location
558 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
559 $facilityName=substr($claim->facilityName(),0,35);
560 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
563 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
568 if ($claim->facilityNPI()) { $out .=
569 "*XX*" . $claim->facilityNPI();
571 "*24*" . $claim->facilityETIN();
572 $log .= "*** Service location has no NPI.\n";
576 if ($claim->facilityStreet()) {
579 "*" . $claim->facilityStreet() .
582 if ($claim->facilityState()) {
585 "*" . $claim->facilityCity() .
586 "*" . $claim->facilityState() .
587 "*" . $claim->facilityZip() .
592 // Loop 2310E, Supervising Provider
594 if ($claim->supervisorLastName()) {
597 "*DQ" . // Supervising Physician
599 "*" . $claim->supervisorLastName() .
600 "*" . $claim->supervisorFirstName() .
601 "*" . $claim->supervisorMiddleName() .
602 "*" . // NM106 not used
604 if ($claim->supervisorNPI()) { $out .=
606 "*" . $claim->supervisorNPI();
609 "*" . $claim->supervisorSSN();
613 if ($claim->supervisorNumber()) {
616 "*" . $claim->supervisorNumberType() .
617 "*" . $claim->supervisorNumber() .
622 $prev_pt_resp = $clm_total_charges; // for computation below
624 // Loops 2320 and 2330*, other subscriber/payer information.
626 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
628 $tmp1 = $claim->claimType($ins);
629 $tmp2 = 'C1'; // Here a kludge. See page 321.
630 if ($tmp1 === 'CI') $tmp2 = 'C1';
631 if ($tmp1 === 'AM') $tmp2 = 'AP';
632 if ($tmp1 === 'HM') $tmp2 = 'HM';
633 if ($tmp1 === 'MB') $tmp2 = 'MB';
634 if ($tmp1 === 'MC') $tmp2 = 'MC';
635 if ($tmp1 === '09') $tmp2 = 'PP';
637 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
638 "*" . $claim->payerSequence($ins) .
639 "*" . $claim->insuredRelationship($ins) .
640 "*" . $claim->groupNumber($ins) .
641 "*" . $claim->groupName($ins) .
646 "*" . $claim->claimType($ins) .
649 // Things that apply only to previous payers, not future payers.
651 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
653 // Generate claim-level adjustments.
654 $aarr = $claim->payerAdjustments($ins);
655 foreach ($aarr as $a) {
657 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
664 $payerpaid = $claim->payerTotals($ins);
666 $out .= "AMT" . // Previous payer's paid amount. Page 332.
668 "*" . $payerpaid[1] .
671 // Patient responsibility amount as of this previous payer.
672 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
673 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
676 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
678 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
682 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
684 "*" . sprintf('%.2f', $prev_pt_resp) .
687 } // End of things that apply only to previous payers.
690 $out .= "DMG" . // Other subscriber demographic information. Page 342.
692 "*" . $claim->insuredDOB($ins) .
693 "*" . $claim->insuredSex($ins) .
697 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
707 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
710 "*" . $claim->insuredLastName($ins) .
711 "*" . $claim->insuredFirstName($ins) .
712 "*" . $claim->insuredMiddleName($ins) .
716 "*" . $claim->policyNumber($ins) .
721 "*" . $claim->insuredStreet($ins) .
726 "*" . $claim->insuredCity($ins) .
727 "*" . $claim->insuredState($ins) .
728 "*" . $claim->insuredZip($ins) .
732 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
733 $payerName=substr($claim->payerName($ins),0,35);
734 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
743 "*" . $claim->payerID($ins) .
746 // if (!$claim->payerID($ins)) {
747 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
750 // Payer address (N3 and N4) are added below so that Gateway EDI can
751 // auto-generate secondary claims. These do NOT appear in my copy of
752 // the spec! -- Rod 2008-06-12
754 if (trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
757 "*" . $claim->payerStreet($ins) .
762 "*" . $claim->payerCity($ins) .
763 "*" . $claim->payerState($ins) .
764 "*" . $claim->payerZip($ins) .
768 } // End loops 2320/2330*.
772 // Procedure loop starts here.
774 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
778 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
783 $out .= "SV1" . // Professional Service. Page 400.
784 "*HC:" . $claim->cptKey($prockey) .
785 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
787 "*" . $claim->cptUnits($prockey) .
791 $dia = $claim->diagIndexArray($prockey);
793 foreach ($dia as $dindex) {
796 if (++
$i >= 4) break;
800 if (!$claim->cptCharges($prockey)) {
801 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
805 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
809 $out .= "DTP" . // Date of Service. Page 435.
812 "*" . $claim->serviceDate() .
815 // AMT*AAE segment for Approved Amount from previous payer.
816 // Medicare secondaries seem to require this.
818 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
819 if ($claim->payerSequence($ins) > $claim->payerSequence())
820 continue; // payer is future, not previous
821 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
823 $out .= "AMT" . // Approved amount per previous payer. Page 485.
825 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
830 // Loop 2410, Drug Information. Medicaid insurers seem to want this
833 $ndc = $claim->cptNDCID($prockey);
836 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
837 "*" . // Per addendum, LIN01 is not used.
842 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
843 $log .= "*** NDC code '$ndc' has invalid format!\n";
847 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
848 if (!$tmpunits) $tmpunits = 1;
849 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
852 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
853 "*" . $claim->cptNDCQuantity($prockey) .
854 "*" . $claim->cptNDCUOM($prockey) .
858 // Loop 2420A, Rendering Provider (service-specific).
859 // Used if the rendering provider for this service line is different
860 // from that in loop 2310B.
862 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
864 $out .= "NM1" . // Loop 2310B Rendering Provider
867 "*" . $claim->providerLastName($prockey) .
868 "*" . $claim->providerFirstName($prockey) .
869 "*" . $claim->providerMiddleName($prockey) .
872 if ($claim->providerNPI($prockey)) { $out .=
874 "*" . $claim->providerNPI($prockey);
877 "*" . $claim->providerSSN($prockey);
878 $log .= "*** Rendering provider has no NPI.\n";
882 if ($claim->providerTaxonomy($prockey)) {
885 "*PE" . // PErforming provider
887 "*" . $claim->providerTaxonomy($prockey) .
891 // REF*1C is required here for the Medicare provider number if NPI was
892 // specified in NM109. Not sure if other payers require anything here.
893 if ($claim->providerNumber($prockey)) {
896 "*" . $claim->providerNumberType($prockey) .
897 "*" . $claim->providerNumber($prockey) .
902 // Loop 2430, adjudication by previous payers.
904 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
905 if ($claim->payerSequence($ins) > $claim->payerSequence())
906 continue; // payer is future, not previous
908 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
909 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
911 if ($payerpaid[1] == 0 && !count($aarr)) {
912 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
913 "' has no payments or adjustments from previous payer!\n";
918 $out .= "SVD" . // Service line adjudication. Page 554.
919 "*" . $claim->payerID($ins) .
920 "*" . $payerpaid[1] .
921 "*HC:" . $claim->cptKey($prockey) .
923 "*" . $claim->cptUnits($prockey) .
926 $tmpdate = $payerpaid[0];
927 foreach ($aarr as $a) {
929 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
934 if (!$tmpdate) $tmpdate = $a[0];
937 $out .= "CAS02" . // Previous payer's adjustment reason
945 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
952 } // end this procedure
955 $out .= "SE" . // SE Trailer
960 $out .= "GE" . // GE Trailer
965 $out .= "IEA" . // IEA Trailer