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 $out .= "NM1" . // Loop 1000A Submitter
80 "*" . $claim->billingFacilityName() .
86 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
87 $out .= "*" . $claim->clearingHouseETIN();
89 $out .= "*" . $claim->billingFacilityETIN();
96 "*" . $claim->billingContactName() .
98 "*" . $claim->billingContactPhone();
99 if ($claim->x12gsper06()) {
100 $out .= "*ED*" . $claim->x12gsper06();
105 $out .= "NM1" . // Loop 1000B Receiver
108 "*" . $claim->clearingHouseName() .
114 "*" . $claim->clearingHouseETIN() .
120 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
127 $HLBillingPayToProvider = $HLcount++
;
130 $out .= "NM1" . // Loop 2010AA Billing Provider
133 "*" . $claim->billingFacilityName() .
138 if ($claim->billingFacilityNPI()) {
139 $out .= "*XX*" . $claim->billingFacilityNPI();
141 $log .= "*** Billing facility has no NPI.\n";
142 $out .= "*24*" . $claim->billingFacilityETIN();
148 "*" . $claim->billingFacilityStreet() .
153 "*" . $claim->billingFacilityCity() .
154 "*" . $claim->billingFacilityState() .
155 "*" . $claim->billingFacilityZip() .
158 // Add a REF*EI*<ein> segment if NPI was specified in the NM1 above.
159 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
162 if($claim->federalIdType()){
163 $out .= "*" . $claim->federalIdType();
166 $out .= "*EI";//For dealing with the situation before adding selection for TaxId type In facility ie default to EIN.
168 $out .= "*" . $claim->billingFacilityETIN() .
172 if ($claim->providerNumberType() && $claim->providerNumber()) {
175 "*" . $claim->providerNumberType() .
176 "*" . $claim->providerNumber() .
179 else if ($claim->providerNumber()) {
180 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
184 $out .= "NM1" . // Loop 2010AB Pay-To Provider
187 "*" . $claim->billingFacilityName() .
192 if ($claim->billingFacilityNPI())
193 $out .= "*XX*" . $claim->billingFacilityNPI();
195 $out .= "*24*" . $claim->billingFacilityETIN();
200 "*" . $claim->billingFacilityStreet() .
205 "*" . $claim->billingFacilityCity() .
206 "*" . $claim->billingFacilityState() .
207 "*" . $claim->billingFacilityZip() .
210 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
214 "*" . $claim->billingFacilityETIN() .
221 $out .= "HL" . // Loop 2000B Subscriber HL Loop
223 "*$HLBillingPayToProvider" .
228 $HLSubscriber = $HLcount++
;
230 if (!$claim->payerSequence()) {
231 $log .= "*** Error: Insurance information is missing!\n";
234 $out .= "SBR" . // Subscriber Information
235 "*" . $claim->payerSequence() .
236 "*" . $claim->insuredRelationship() .
237 "*" . $claim->groupNumber() .
238 "*" . $claim->groupName() .
239 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
243 "*" . $claim->claimType() . // Zirmed replaces this
247 $out .= "NM1" . // Loop 2010BA Subscriber
250 "*" . $claim->insuredLastName() .
251 "*" . $claim->insuredFirstName() .
252 "*" . $claim->insuredMiddleName() .
256 "*" . $claim->policyNumber() .
261 "*" . $claim->insuredStreet() .
266 "*" . $claim->insuredCity() .
267 "*" . $claim->insuredState() .
268 "*" . $claim->insuredZip() .
274 "*" . $claim->insuredDOB() .
275 "*" . $claim->insuredSex() .
279 $out .= "NM1" . // Loop 2010BB Payer
282 "*" . $claim->payerName() .
288 // Zirmed ignores this if using payer name matching:
289 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
292 // if (!$claim->payerID()) {
293 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
298 "*" . $claim->payerStreet() .
303 "*" . $claim->payerCity() .
304 "*" . $claim->payerState() .
305 "*" . $claim->payerZip() .
308 if (! $claim->isSelfOfInsured()) {
310 $out .= "HL" . // Loop 2000C Patient Information
321 "*" . $claim->insuredRelationship() .
325 $out .= "NM1" . // Loop 2010CA Patient
328 "*" . $claim->patientLastName() .
329 "*" . $claim->patientFirstName() .
330 "*" . $claim->patientMiddleName() .
335 "*" . $claim->patientStreet() .
340 "*" . $claim->patientCity() .
341 "*" . $claim->patientState() .
342 "*" . $claim->patientZip() .
348 "*" . $claim->patientDOB() .
349 "*" . $claim->patientSex() .
351 } // end of patient different from insured
353 $proccount = $claim->procCount();
355 $clm_total_charges = 0;
356 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
357 $clm_total_charges +
= $claim->cptCharges($prockey);
360 if (!$clm_total_charges) {
361 $log .= "*** This claim has no charges!\n";
365 $out .= "CLM" . // Loop 2300 Claim
367 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
370 "*" . $claim->facilityPOS() . "::" . $claim->frequencyTypeCode() .
373 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
379 $out .= "DTP" . // Date of Onset
382 "*" . $claim->onsetDate() .
385 if (strcmp($claim->facilityPOS(),'21') == 0) {
387 $out .= "DTP" . // Date of Hospitalization
390 "*" . $claim->onsetDate() .
394 $patientpaid = $claim->patientPaidAmount();
395 if ($patientpaid != 0) {
397 $out .= "AMT" . // Patient paid amount. Page 220.
403 if ($claim->priorAuth()) {
405 $out .= "REF" . // Prior Authorization Number
407 "*" . $claim->priorAuth() .
411 if ($claim->cliaCode() and $claim->claimType() === 'MB') {
412 // Required by Medicare when in-house labs are done.
414 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
416 "*" . $claim->cliaCode() .
420 // Note: This would be the place to implement the NTE segment for loop 2300.
422 // Diagnoses, up to 8 per HI segment.
423 $da = $claim->diagArray();
424 $diag_type_code = 'BK';
426 foreach ($da as $diag) {
428 if ($tmp) $out .= "~\n";
430 $out .= "HI"; // Health Diagnosis Codes
432 $out .= "*$diag_type_code:" . $diag;
433 $diag_type_code = 'BF';
436 if ($tmp) $out .= "~\n";
438 if ($claim->referrerLastName()) {
439 // Medicare requires referring provider's name and UPIN.
441 $out .= "NM1" . // Loop 2310A Referring Provider
444 "*" . $claim->referrerLastName() .
445 "*" . $claim->referrerFirstName() .
446 "*" . $claim->referrerMiddleName() .
449 if ($claim->referrerNPI()) { $out .=
451 "*" . $claim->referrerNPI();
454 "*" . $claim->referrerSSN();
458 if ($claim->referrerTaxonomy()) {
461 "*RF" . // ReFerring provider
463 "*" . $claim->referrerTaxonomy() .
467 if ($claim->referrerUPIN()) {
469 $out .= "REF" . // Referring Provider Secondary Identification
471 "*" . $claim->referrerUPIN() .
477 $out .= "NM1" . // Loop 2310B Rendering Provider
480 "*" . $claim->providerLastName() .
481 "*" . $claim->providerFirstName() .
482 "*" . $claim->providerMiddleName() .
485 if ($claim->providerNPI()) { $out .=
487 "*" . $claim->providerNPI();
490 "*" . $claim->providerSSN();
491 $log .= "*** Rendering provider has no NPI.\n";
495 if ($claim->providerTaxonomy()) {
498 "*PE" . // PErforming provider
500 "*" . $claim->providerTaxonomy() .
504 // REF*1C is required here for the Medicare provider number if NPI was
505 // specified in NM109. Not sure if other payers require anything here.
506 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
508 if (trim($claim->x12gsreceiverid()) != '470819582') { // if NOT ECLAIMS EDI
509 if ($claim->providerNumber()) {
512 "*" . $claim->providerNumberType() .
513 "*" . $claim->providerNumber() .
518 // Loop 2310D is omitted in the case of home visits (POS=12).
519 if ($claim->facilityPOS() != 12) {
521 $out .= "NM1" . // Loop 2310D Service Location
524 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
525 "*" . $claim->facilityName();
527 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
532 if ($claim->facilityNPI()) { $out .=
533 "*XX*" . $claim->facilityNPI();
535 "*24*" . $claim->facilityETIN();
536 $log .= "*** Service location has no NPI.\n";
540 if ($claim->facilityStreet()) {
543 "*" . $claim->facilityStreet() .
546 if ($claim->facilityState()) {
549 "*" . $claim->facilityCity() .
550 "*" . $claim->facilityState() .
551 "*" . $claim->facilityZip() .
556 // Loop 2310E, Supervising Provider
558 if ($claim->supervisorLastName()) {
561 "*DQ" . // Supervising Physician
563 "*" . $claim->supervisorLastName() .
564 "*" . $claim->supervisorFirstName() .
565 "*" . $claim->supervisorMiddleName() .
566 "*" . // NM106 not used
568 if ($claim->supervisorNPI()) { $out .=
570 "*" . $claim->supervisorNPI();
573 "*" . $claim->supervisorSSN();
577 if ($claim->supervisorNumber()) {
580 "*" . $claim->supervisorNumberType() .
581 "*" . $claim->supervisorNumber() .
586 $prev_pt_resp = $clm_total_charges; // for computation below
588 // Loops 2320 and 2330*, other subscriber/payer information.
590 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
592 $tmp1 = $claim->claimType($ins);
593 $tmp2 = 'C1'; // Here a kludge. See page 321.
594 if ($tmp1 === 'CI') $tmp2 = 'C1';
595 if ($tmp1 === 'AM') $tmp2 = 'AP';
596 if ($tmp1 === 'HM') $tmp2 = 'HM';
597 if ($tmp1 === 'MB') $tmp2 = 'MB';
598 if ($tmp1 === 'MC') $tmp2 = 'MC';
599 if ($tmp1 === '09') $tmp2 = 'PP';
601 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
602 "*" . $claim->payerSequence($ins) .
603 "*" . $claim->insuredRelationship($ins) .
604 "*" . $claim->groupNumber($ins) .
605 "*" . $claim->groupName($ins) .
610 "*" . $claim->claimType($ins) .
613 // Things that apply only to previous payers, not future payers.
615 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
617 // Generate claim-level adjustments.
618 $aarr = $claim->payerAdjustments($ins);
619 foreach ($aarr as $a) {
621 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
628 $payerpaid = $claim->payerTotals($ins);
630 $out .= "AMT" . // Previous payer's paid amount. Page 332.
632 "*" . $payerpaid[1] .
635 // Patient responsibility amount as of this previous payer.
636 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
637 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
640 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
642 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
646 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
648 "*" . sprintf('%.2f', $prev_pt_resp) .
651 } // End of things that apply only to previous payers.
654 $out .= "DMG" . // Other subscriber demographic information. Page 342.
656 "*" . $claim->insuredDOB($ins) .
657 "*" . $claim->insuredSex($ins) .
661 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
671 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
674 "*" . $claim->insuredLastName($ins) .
675 "*" . $claim->insuredFirstName($ins) .
676 "*" . $claim->insuredMiddleName($ins) .
680 "*" . $claim->policyNumber($ins) .
685 "*" . $claim->insuredStreet($ins) .
690 "*" . $claim->insuredCity($ins) .
691 "*" . $claim->insuredState($ins) .
692 "*" . $claim->insuredZip($ins) .
696 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
699 "*" . $claim->payerName($ins) .
705 "*" . $claim->payerID($ins) .
708 // if (!$claim->payerID($ins)) {
709 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
712 // Payer address (N3 and N4) are added below so that Gateway EDI can
713 // auto-generate secondary claims. These do NOT appear in my copy of
714 // the spec! -- Rod 2008-06-12
716 if (trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
719 "*" . $claim->payerStreet($ins) .
724 "*" . $claim->payerCity($ins) .
725 "*" . $claim->payerState($ins) .
726 "*" . $claim->payerZip($ins) .
730 } // End loops 2320/2330*.
734 // Procedure loop starts here.
736 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
740 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
745 $out .= "SV1" . // Professional Service. Page 400.
746 "*HC:" . $claim->cptKey($prockey) .
747 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
749 "*" . $claim->cptUnits($prockey) .
753 $dia = $claim->diagIndexArray($prockey);
755 foreach ($dia as $dindex) {
758 if (++
$i >= 4) break;
762 if (!$claim->cptCharges($prockey)) {
763 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
767 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
771 $out .= "DTP" . // Date of Service. Page 435.
774 "*" . $claim->serviceDate() .
777 // AMT*AAE segment for Approved Amount from previous payer.
778 // Medicare secondaries seem to require this.
780 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
781 if ($claim->payerSequence($ins) > $claim->payerSequence())
782 continue; // payer is future, not previous
783 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
785 $out .= "AMT" . // Approved amount per previous payer. Page 485.
787 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
792 // Loop 2410, Drug Information. Medicaid insurers seem to want this
795 $ndc = $claim->cptNDCID($prockey);
798 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
799 "*" . // Per addendum, LIN01 is not used.
804 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
805 $log .= "*** NDC code '$ndc' has invalid format!\n";
809 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
810 if (!$tmpunits) $tmpunits = 1;
811 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
814 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
815 "*" . $claim->cptNDCQuantity($prockey) .
816 "*" . $claim->cptNDCUOM($prockey) .
820 // Loop 2420A, Rendering Provider (service-specific).
821 // Used if the rendering provider for this service line is different
822 // from that in loop 2310B.
824 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
826 $out .= "NM1" . // Loop 2310B Rendering Provider
829 "*" . $claim->providerLastName($prockey) .
830 "*" . $claim->providerFirstName($prockey) .
831 "*" . $claim->providerMiddleName($prockey) .
834 if ($claim->providerNPI($prockey)) { $out .=
836 "*" . $claim->providerNPI($prockey);
839 "*" . $claim->providerSSN($prockey);
840 $log .= "*** Rendering provider has no NPI.\n";
844 if ($claim->providerTaxonomy($prockey)) {
847 "*PE" . // PErforming provider
849 "*" . $claim->providerTaxonomy($prockey) .
853 // REF*1C is required here for the Medicare provider number if NPI was
854 // specified in NM109. Not sure if other payers require anything here.
855 if ($claim->providerNumber($prockey)) {
858 "*" . $claim->providerNumberType($prockey) .
859 "*" . $claim->providerNumber($prockey) .
864 // Loop 2430, adjudication by previous payers.
866 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
867 if ($claim->payerSequence($ins) > $claim->payerSequence())
868 continue; // payer is future, not previous
870 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
871 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
873 if ($payerpaid[1] == 0 && !count($aarr)) {
874 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
875 "' has no payments or adjustments from previous payer!\n";
880 $out .= "SVD" . // Service line adjudication. Page 554.
881 "*" . $claim->payerID($ins) .
882 "*" . $payerpaid[1] .
883 "*HC:" . $claim->cptKey($prockey) .
885 "*" . $claim->cptUnits($prockey) .
888 $tmpdate = $payerpaid[0];
889 foreach ($aarr as $a) {
891 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
896 if (!$tmpdate) $tmpdate = $a[0];
901 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
908 } // end this procedure
911 $out .= "SE" . // SE Trailer
916 $out .= "GE" . // GE Trailer
921 $out .= "IEA" . // IEA Trailer