2 // Copyright (C) 2007-2009 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() .
227 $out .= "HL" . // Loop 2000B Subscriber HL Loop
229 "*$HLBillingPayToProvider" .
234 $HLSubscriber = $HLcount++
;
236 if (!$claim->payerSequence()) {
237 $log .= "*** Error: Insurance information is missing!\n";
240 $out .= "SBR" . // Subscriber Information
241 "*" . $claim->payerSequence() .
242 "*" . $claim->insuredRelationship() .
243 "*" . $claim->groupNumber() .
244 "*" . $claim->groupName() .
245 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
249 "*" . $claim->claimType() . // Zirmed replaces this
253 $out .= "NM1" . // Loop 2010BA Subscriber
256 "*" . $claim->insuredLastName() .
257 "*" . $claim->insuredFirstName() .
258 "*" . $claim->insuredMiddleName() .
262 "*" . $claim->policyNumber() .
267 "*" . $claim->insuredStreet() .
272 "*" . $claim->insuredCity() .
273 "*" . $claim->insuredState() .
274 "*" . $claim->insuredZip() .
280 "*" . $claim->insuredDOB() .
281 "*" . $claim->insuredSex() .
285 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
286 $payerName=substr($claim->payerName(),0,35);
287 $out .= "NM1" . // Loop 2010BB Payer
296 // Zirmed ignores this if using payer name matching:
297 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
300 // if (!$claim->payerID()) {
301 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
306 "*" . $claim->payerStreet() .
311 "*" . $claim->payerCity() .
312 "*" . $claim->payerState() .
313 "*" . $claim->payerZip() .
316 if (! $claim->isSelfOfInsured()) {
318 $out .= "HL" . // Loop 2000C Patient Information
329 "*" . $claim->insuredRelationship() .
333 $out .= "NM1" . // Loop 2010CA Patient
336 "*" . $claim->patientLastName() .
337 "*" . $claim->patientFirstName() .
338 "*" . $claim->patientMiddleName() .
343 "*" . $claim->patientStreet() .
348 "*" . $claim->patientCity() .
349 "*" . $claim->patientState() .
350 "*" . $claim->patientZip() .
356 "*" . $claim->patientDOB() .
357 "*" . $claim->patientSex() .
359 } // end of patient different from insured
361 $proccount = $claim->procCount();
363 $clm_total_charges = 0;
364 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
365 $clm_total_charges +
= $claim->cptCharges($prockey);
368 if (!$clm_total_charges) {
369 $log .= "*** This claim has no charges!\n";
374 $out .= "CLM" . // Loop 2300 Claim
376 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
379 "*" . sprintf('%02d', $claim->facilityPOS()) . "::" . $claim->frequencyTypeCode() . // Changed to correct single digit output
382 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
388 $out .= "DTP" . // Date of Onset
391 "*" . $claim->onsetDate() .
394 if (strcmp($claim->facilityPOS(),'21') == 0) {
396 $out .= "DTP" . // Date of Hospitalization
399 "*" . $claim->onsetDate() .
403 $patientpaid = $claim->patientPaidAmount();
404 if ($patientpaid != 0) {
406 $out .= "AMT" . // Patient paid amount. Page 220.
412 if ($claim->priorAuth()) {
414 $out .= "REF" . // Prior Authorization Number
416 "*" . $claim->priorAuth() .
420 if ($claim->cliaCode() and $claim->claimType() === 'MB') {
421 // Required by Medicare when in-house labs are done.
423 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
425 "*" . $claim->cliaCode() .
429 // Note: This would be the place to implement the NTE segment for loop 2300.
430 if ($claim->additionalNotes()) {
433 $out .= "NTE" . // comments box 19
435 "*" . $claim->additionalNotes() .
439 // Diagnoses, up to 8 per HI segment.
440 $da = $claim->diagArray();
441 $diag_type_code = 'BK';
443 foreach ($da as $diag) {
445 if ($tmp) $out .= "~\n";
447 $out .= "HI"; // Health Diagnosis Codes
449 $out .= "*$diag_type_code:" . $diag;
450 $diag_type_code = 'BF';
453 if ($tmp) $out .= "~\n";
455 if ($claim->referrerLastName()) {
456 // Medicare requires referring provider's name and UPIN.
458 $out .= "NM1" . // Loop 2310A Referring Provider
461 "*" . $claim->referrerLastName() .
462 "*" . $claim->referrerFirstName() .
463 "*" . $claim->referrerMiddleName() .
466 if ($claim->referrerNPI()) { $out .=
468 "*" . $claim->referrerNPI();
471 "*" . $claim->referrerSSN();
475 if ($claim->referrerTaxonomy()) {
478 "*RF" . // ReFerring provider
480 "*" . $claim->referrerTaxonomy() .
484 if ($claim->referrerUPIN()) {
486 $out .= "REF" . // Referring Provider Secondary Identification
488 "*" . $claim->referrerUPIN() .
494 $out .= "NM1" . // Loop 2310B Rendering Provider
497 "*" . $claim->providerLastName() .
498 "*" . $claim->providerFirstName() .
499 "*" . $claim->providerMiddleName() .
502 if ($claim->providerNPI()) { $out .=
504 "*" . $claim->providerNPI();
507 "*" . $claim->providerSSN();
508 $log .= "*** Rendering provider has no NPI.\n";
512 if ($claim->providerTaxonomy()) {
515 "*PE" . // PErforming provider
517 "*" . $claim->providerTaxonomy() .
521 // REF*1C is required here for the Medicare provider number if NPI was
522 // specified in NM109. Not sure if other payers require anything here.
523 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
525 if (trim($claim->x12gsreceiverid()) != '470819582') { // if NOT ECLAIMS EDI
526 if ($claim->providerNumber()) {
529 "*" . $claim->providerNumberType() .
530 "*" . $claim->providerNumber() .
535 // Loop 2310D is omitted in the case of home visits (POS=12).
536 if ($claim->facilityPOS() != 12) {
538 $out .= "NM1" . // Loop 2310D Service Location
541 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
542 $facilityName=substr($claim->facilityName(),0,35);
543 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
546 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
551 if ($claim->facilityNPI()) { $out .=
552 "*XX*" . $claim->facilityNPI();
554 "*24*" . $claim->facilityETIN();
555 $log .= "*** Service location has no NPI.\n";
559 if ($claim->facilityStreet()) {
562 "*" . $claim->facilityStreet() .
565 if ($claim->facilityState()) {
568 "*" . $claim->facilityCity() .
569 "*" . $claim->facilityState() .
570 "*" . $claim->facilityZip() .
575 // Loop 2310E, Supervising Provider
577 if ($claim->supervisorLastName()) {
580 "*DQ" . // Supervising Physician
582 "*" . $claim->supervisorLastName() .
583 "*" . $claim->supervisorFirstName() .
584 "*" . $claim->supervisorMiddleName() .
585 "*" . // NM106 not used
587 if ($claim->supervisorNPI()) { $out .=
589 "*" . $claim->supervisorNPI();
592 "*" . $claim->supervisorSSN();
596 if ($claim->supervisorNumber()) {
599 "*" . $claim->supervisorNumberType() .
600 "*" . $claim->supervisorNumber() .
605 $prev_pt_resp = $clm_total_charges; // for computation below
607 // Loops 2320 and 2330*, other subscriber/payer information.
609 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
611 $tmp1 = $claim->claimType($ins);
612 $tmp2 = 'C1'; // Here a kludge. See page 321.
613 if ($tmp1 === 'CI') $tmp2 = 'C1';
614 if ($tmp1 === 'AM') $tmp2 = 'AP';
615 if ($tmp1 === 'HM') $tmp2 = 'HM';
616 if ($tmp1 === 'MB') $tmp2 = 'MB';
617 if ($tmp1 === 'MC') $tmp2 = 'MC';
618 if ($tmp1 === '09') $tmp2 = 'PP';
620 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
621 "*" . $claim->payerSequence($ins) .
622 "*" . $claim->insuredRelationship($ins) .
623 "*" . $claim->groupNumber($ins) .
624 "*" . $claim->groupName($ins) .
629 "*" . $claim->claimType($ins) .
632 // Things that apply only to previous payers, not future payers.
634 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
636 // Generate claim-level adjustments.
637 $aarr = $claim->payerAdjustments($ins);
638 foreach ($aarr as $a) {
640 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
647 $payerpaid = $claim->payerTotals($ins);
649 $out .= "AMT" . // Previous payer's paid amount. Page 332.
651 "*" . $payerpaid[1] .
654 // Patient responsibility amount as of this previous payer.
655 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
656 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
659 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
661 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
665 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
667 "*" . sprintf('%.2f', $prev_pt_resp) .
670 } // End of things that apply only to previous payers.
673 $out .= "DMG" . // Other subscriber demographic information. Page 342.
675 "*" . $claim->insuredDOB($ins) .
676 "*" . $claim->insuredSex($ins) .
680 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
690 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
693 "*" . $claim->insuredLastName($ins) .
694 "*" . $claim->insuredFirstName($ins) .
695 "*" . $claim->insuredMiddleName($ins) .
699 "*" . $claim->policyNumber($ins) .
704 "*" . $claim->insuredStreet($ins) .
709 "*" . $claim->insuredCity($ins) .
710 "*" . $claim->insuredState($ins) .
711 "*" . $claim->insuredZip($ins) .
715 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
716 $payerName=substr($claim->payerName($ins),0,35);
717 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
726 "*" . $claim->payerID($ins) .
729 // if (!$claim->payerID($ins)) {
730 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
733 // Payer address (N3 and N4) are added below so that Gateway EDI can
734 // auto-generate secondary claims. These do NOT appear in my copy of
735 // the spec! -- Rod 2008-06-12
737 if (trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
740 "*" . $claim->payerStreet($ins) .
745 "*" . $claim->payerCity($ins) .
746 "*" . $claim->payerState($ins) .
747 "*" . $claim->payerZip($ins) .
751 } // End loops 2320/2330*.
755 // Procedure loop starts here.
757 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
761 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
766 $out .= "SV1" . // Professional Service. Page 400.
767 "*HC:" . $claim->cptKey($prockey) .
768 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
770 "*" . $claim->cptUnits($prockey) .
774 $dia = $claim->diagIndexArray($prockey);
776 foreach ($dia as $dindex) {
779 if (++
$i >= 4) break;
783 if (!$claim->cptCharges($prockey)) {
784 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
788 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
792 $out .= "DTP" . // Date of Service. Page 435.
795 "*" . $claim->serviceDate() .
798 // AMT*AAE segment for Approved Amount from previous payer.
799 // Medicare secondaries seem to require this.
801 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
802 if ($claim->payerSequence($ins) > $claim->payerSequence())
803 continue; // payer is future, not previous
804 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
806 $out .= "AMT" . // Approved amount per previous payer. Page 485.
808 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
813 // Loop 2410, Drug Information. Medicaid insurers seem to want this
816 $ndc = $claim->cptNDCID($prockey);
819 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
820 "*" . // Per addendum, LIN01 is not used.
825 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
826 $log .= "*** NDC code '$ndc' has invalid format!\n";
830 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
831 if (!$tmpunits) $tmpunits = 1;
832 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
835 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
836 "*" . $claim->cptNDCQuantity($prockey) .
837 "*" . $claim->cptNDCUOM($prockey) .
841 // Loop 2420A, Rendering Provider (service-specific).
842 // Used if the rendering provider for this service line is different
843 // from that in loop 2310B.
845 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
847 $out .= "NM1" . // Loop 2310B Rendering Provider
850 "*" . $claim->providerLastName($prockey) .
851 "*" . $claim->providerFirstName($prockey) .
852 "*" . $claim->providerMiddleName($prockey) .
855 if ($claim->providerNPI($prockey)) { $out .=
857 "*" . $claim->providerNPI($prockey);
860 "*" . $claim->providerSSN($prockey);
861 $log .= "*** Rendering provider has no NPI.\n";
865 if ($claim->providerTaxonomy($prockey)) {
868 "*PE" . // PErforming provider
870 "*" . $claim->providerTaxonomy($prockey) .
874 // REF*1C is required here for the Medicare provider number if NPI was
875 // specified in NM109. Not sure if other payers require anything here.
876 if ($claim->providerNumber($prockey)) {
879 "*" . $claim->providerNumberType($prockey) .
880 "*" . $claim->providerNumber($prockey) .
885 // Loop 2430, adjudication by previous payers.
887 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
888 if ($claim->payerSequence($ins) > $claim->payerSequence())
889 continue; // payer is future, not previous
891 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
892 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
894 if ($payerpaid[1] == 0 && !count($aarr)) {
895 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
896 "' has no payments or adjustments from previous payer!\n";
901 $out .= "SVD" . // Service line adjudication. Page 554.
902 "*" . $claim->payerID($ins) .
903 "*" . $payerpaid[1] .
904 "*HC:" . $claim->cptKey($prockey) .
906 "*" . $claim->cptUnits($prockey) .
909 $tmpdate = $payerpaid[0];
910 foreach ($aarr as $a) {
912 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
917 if (!$tmpdate) $tmpdate = $a[0];
922 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
929 } // end this procedure
932 $out .= "SE" . // SE Trailer
937 $out .= "GE" . // GE Trailer
942 $out .= "IEA" . // IEA Trailer