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 "*" . $claim->billingFacilityETIN() .
92 "*" . $claim->billingContactName() .
94 "*" . $claim->billingContactPhone();
95 if ($claim->x12gsper06()) {
96 $out .= "*ED*" . $claim->x12gsper06();
101 $out .= "NM1" . // Loop 1000B Receiver
104 "*" . $claim->clearingHouseName() .
110 "*" . $claim->clearingHouseETIN() .
116 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
123 $HLBillingPayToProvider = $HLcount++
;
126 $out .= "NM1" . // Loop 2010AA Billing Provider
129 "*" . $claim->billingFacilityName() .
134 if ($claim->billingFacilityNPI()) {
135 $out .= "*XX*" . $claim->billingFacilityNPI();
137 $log .= "*** Billing facility has no NPI.\n";
138 $out .= "*24*" . $claim->billingFacilityETIN();
144 "*" . $claim->billingFacilityStreet() .
149 "*" . $claim->billingFacilityCity() .
150 "*" . $claim->billingFacilityState() .
151 "*" . $claim->billingFacilityZip() .
154 // Add a REF*EI*<ein> segment if NPI was specified in the NM1 above.
155 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
159 "*" . $claim->billingFacilityETIN() .
163 if ($claim->providerNumberType() && $claim->providerNumber()) {
166 "*" . $claim->providerNumberType() .
167 "*" . $claim->providerNumber() .
170 else if ($claim->providerNumber()) {
171 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
175 $out .= "NM1" . // Loop 2010AB Pay-To Provider
178 "*" . $claim->billingFacilityName() .
183 if ($claim->billingFacilityNPI())
184 $out .= "*XX*" . $claim->billingFacilityNPI();
186 $out .= "*24*" . $claim->billingFacilityETIN();
191 "*" . $claim->billingFacilityStreet() .
196 "*" . $claim->billingFacilityCity() .
197 "*" . $claim->billingFacilityState() .
198 "*" . $claim->billingFacilityZip() .
201 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
205 "*" . $claim->billingFacilityETIN() .
212 $out .= "HL" . // Loop 2000B Subscriber HL Loop
214 "*$HLBillingPayToProvider" .
219 $HLSubscriber = $HLcount++
;
221 if (!$claim->payerSequence()) {
222 $log .= "*** Error: Insurance information is missing!\n";
225 $out .= "SBR" . // Subscriber Information
226 "*" . $claim->payerSequence() .
227 "*" . $claim->insuredRelationship() .
228 "*" . $claim->groupNumber() .
229 "*" . $claim->groupName() .
230 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
234 "*" . $claim->claimType() . // Zirmed replaces this
238 $out .= "NM1" . // Loop 2010BA Subscriber
241 "*" . $claim->insuredLastName() .
242 "*" . $claim->insuredFirstName() .
243 "*" . $claim->insuredMiddleName() .
247 "*" . $claim->policyNumber() .
252 "*" . $claim->insuredStreet() .
257 "*" . $claim->insuredCity() .
258 "*" . $claim->insuredState() .
259 "*" . $claim->insuredZip() .
265 "*" . $claim->insuredDOB() .
266 "*" . $claim->insuredSex() .
270 $out .= "NM1" . // Loop 2010BB Payer
273 "*" . $claim->payerName() .
279 // Zirmed ignores this if using payer name matching:
280 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
283 // if (!$claim->payerID()) {
284 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
289 "*" . $claim->payerStreet() .
294 "*" . $claim->payerCity() .
295 "*" . $claim->payerState() .
296 "*" . $claim->payerZip() .
299 if (! $claim->isSelfOfInsured()) {
301 $out .= "HL" . // Loop 2000C Patient Information
312 "*" . $claim->insuredRelationship() .
316 $out .= "NM1" . // Loop 2010CA Patient
319 "*" . $claim->patientLastName() .
320 "*" . $claim->patientFirstName() .
321 "*" . $claim->patientMiddleName() .
326 "*" . $claim->patientStreet() .
331 "*" . $claim->patientCity() .
332 "*" . $claim->patientState() .
333 "*" . $claim->patientZip() .
339 "*" . $claim->patientDOB() .
340 "*" . $claim->patientSex() .
342 } // end of patient different from insured
344 $proccount = $claim->procCount();
346 $clm_total_charges = 0;
347 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
348 $clm_total_charges +
= $claim->cptCharges($prockey);
351 if (!$clm_total_charges) {
352 $log .= "*** This claim has no charges!\n";
356 $out .= "CLM" . // Loop 2300 Claim
358 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
361 "*" . $claim->facilityPOS() . "::" . $claim->frequencyTypeCode() .
364 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
370 $out .= "DTP" . // Date of Onset
373 "*" . $claim->onsetDate() .
376 if (strcmp($claim->facilityPOS(),'21') == 0) {
378 $out .= "DTP" . // Date of Hospitalization
381 "*" . $claim->onsetDate() .
385 $patientpaid = $claim->patientPaidAmount();
386 if ($patientpaid != 0) {
388 $out .= "AMT" . // Patient paid amount. Page 220.
394 if ($claim->priorAuth()) {
396 $out .= "REF" . // Prior Authorization Number
398 "*" . $claim->priorAuth() .
402 if ($claim->cliaCode()) {
403 // Required by Medicare when in-house labs are done.
405 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
407 "*" . $claim->cliaCode() .
411 // Note: This would be the place to implement the NTE segment for loop 2300.
413 // Diagnoses, up to 8 per HI segment.
414 $da = $claim->diagArray();
415 $diag_type_code = 'BK';
417 foreach ($da as $diag) {
419 if ($tmp) $out .= "~\n";
421 $out .= "HI"; // Health Diagnosis Codes
423 $out .= "*$diag_type_code:" . $diag;
424 $diag_type_code = 'BF';
427 if ($tmp) $out .= "~\n";
429 if ($claim->referrerLastName()) {
430 // Medicare requires referring provider's name and UPIN.
432 $out .= "NM1" . // Loop 2310A Referring Provider
435 "*" . $claim->referrerLastName() .
436 "*" . $claim->referrerFirstName() .
437 "*" . $claim->referrerMiddleName() .
440 if ($claim->referrerNPI()) { $out .=
442 "*" . $claim->referrerNPI();
445 "*" . $claim->referrerSSN();
449 if ($claim->referrerTaxonomy()) {
452 "*RF" . // ReFerring provider
454 "*" . $claim->referrerTaxonomy() .
458 if ($claim->referrerUPIN()) {
460 $out .= "REF" . // Referring Provider Secondary Identification
462 "*" . $claim->referrerUPIN() .
468 $out .= "NM1" . // Loop 2310B Rendering Provider
471 "*" . $claim->providerLastName() .
472 "*" . $claim->providerFirstName() .
473 "*" . $claim->providerMiddleName() .
476 if ($claim->providerNPI()) { $out .=
478 "*" . $claim->providerNPI();
481 "*" . $claim->providerSSN();
482 $log .= "*** Rendering provider has no NPI.\n";
486 if ($claim->providerTaxonomy()) {
489 "*PE" . // PErforming provider
491 "*" . $claim->providerTaxonomy() .
495 // REF*1C is required here for the Medicare provider number if NPI was
496 // specified in NM109. Not sure if other payers require anything here.
497 if ($claim->providerNumber()) {
500 "*" . $claim->providerNumberType() .
501 "*" . $claim->providerNumber() .
505 // Loop 2310D is omitted in the case of home visits (POS=12).
506 if ($claim->facilityPOS() != 12) {
508 $out .= "NM1" . // Loop 2310D Service Location
511 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
512 "*" . $claim->facilityName();
514 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
519 if ($claim->facilityNPI()) { $out .=
520 "*XX*" . $claim->facilityNPI();
522 "*24*" . $claim->facilityETIN();
523 $log .= "*** Service location has no NPI.\n";
527 if ($claim->facilityStreet()) {
530 "*" . $claim->facilityStreet() .
533 if ($claim->facilityState()) {
536 "*" . $claim->facilityCity() .
537 "*" . $claim->facilityState() .
538 "*" . $claim->facilityZip() .
543 // Loop 2310E, Supervising Provider
545 if ($claim->supervisorLastName()) {
548 "*DQ" . // Supervising Physician
550 "*" . $claim->supervisorLastName() .
551 "*" . $claim->supervisorFirstName() .
552 "*" . $claim->supervisorMiddleName() .
553 "*" . // NM106 not used
555 if ($claim->supervisorNPI()) { $out .=
557 "*" . $claim->supervisorNPI();
560 "*" . $claim->supervisorSSN();
564 if ($claim->supervisorNumber()) {
567 "*" . $claim->supervisorNumberType() .
568 "*" . $claim->supervisorNumber() .
573 $prev_pt_resp = $clm_total_charges; // for computation below
575 // Loops 2320 and 2330*, other subscriber/payer information.
577 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
579 $tmp1 = $claim->claimType($ins);
580 $tmp2 = 'C1'; // Here a kludge. See page 321.
581 if ($tmp1 === 'CI') $tmp2 = 'C1';
582 if ($tmp1 === 'AM') $tmp2 = 'AP';
583 if ($tmp1 === 'HM') $tmp2 = 'HM';
584 if ($tmp1 === 'MB') $tmp2 = 'MB';
585 if ($tmp1 === 'MC') $tmp2 = 'MC';
586 if ($tmp1 === '09') $tmp2 = 'PP';
588 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
589 "*" . $claim->payerSequence($ins) .
590 "*" . $claim->insuredRelationship($ins) .
591 "*" . $claim->groupNumber($ins) .
592 "*" . $claim->groupName($ins) .
597 "*" . $claim->claimType($ins) .
600 // Things that apply only to previous payers, not future payers.
602 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
604 // Generate claim-level adjustments.
605 $aarr = $claim->payerAdjustments($ins);
606 foreach ($aarr as $a) {
608 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
615 $payerpaid = $claim->payerTotals($ins);
617 $out .= "AMT" . // Previous payer's paid amount. Page 332.
619 "*" . $payerpaid[1] .
622 // Patient responsibility amount as of this previous payer.
623 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
624 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
627 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
629 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
633 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
635 "*" . sprintf('%.2f', $prev_pt_resp) .
638 } // End of things that apply only to previous payers.
641 $out .= "DMG" . // Other subscriber demographic information. Page 342.
643 "*" . $claim->insuredDOB($ins) .
644 "*" . $claim->insuredSex($ins) .
648 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
658 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
661 "*" . $claim->insuredLastName($ins) .
662 "*" . $claim->insuredFirstName($ins) .
663 "*" . $claim->insuredMiddleName($ins) .
667 "*" . $claim->policyNumber($ins) .
672 "*" . $claim->insuredStreet($ins) .
677 "*" . $claim->insuredCity($ins) .
678 "*" . $claim->insuredState($ins) .
679 "*" . $claim->insuredZip($ins) .
683 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
686 "*" . $claim->payerName($ins) .
692 "*" . $claim->payerID($ins) .
695 // if (!$claim->payerID($ins)) {
696 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
699 // Payer address (N3 and N4) are added below so that Gateway EDI can
700 // auto-generate secondary claims. These do NOT appear in my copy of
701 // the spec! -- Rod 2008-06-12
703 if (trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
706 "*" . $claim->payerStreet($ins) .
711 "*" . $claim->payerCity($ins) .
712 "*" . $claim->payerState($ins) .
713 "*" . $claim->payerZip($ins) .
717 } // End loops 2320/2330*.
721 // Procedure loop starts here.
723 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
727 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
732 $out .= "SV1" . // Professional Service. Page 400.
733 "*HC:" . $claim->cptKey($prockey) .
734 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
736 "*" . $claim->cptUnits($prockey) .
740 $dia = $claim->diagIndexArray($prockey);
742 foreach ($dia as $dindex) {
745 if (++
$i >= 4) break;
749 if (!$claim->cptCharges($prockey)) {
750 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
754 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
758 $out .= "DTP" . // Date of Service. Page 435.
761 "*" . $claim->serviceDate() .
764 // AMT*AAE segment for Approved Amount from previous payer.
765 // Medicare secondaries seem to require this.
767 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
768 if ($claim->payerSequence($ins) > $claim->payerSequence())
769 continue; // payer is future, not previous
770 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
772 $out .= "AMT" . // Approved amount per previous payer. Page 485.
774 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
779 // Loop 2410, Drug Information. Medicaid insurers seem to want this
782 $ndc = $claim->cptNDCID($prockey);
785 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
786 "*" . // Per addendum, LIN01 is not used.
791 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
792 $log .= "*** NDC code '$ndc' has invalid format!\n";
796 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
797 if (!$tmpunits) $tmpunits = 1;
798 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
801 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
802 "*" . $claim->cptNDCQuantity($prockey) .
803 "*" . $claim->cptNDCUOM($prockey) .
807 // Loop 2420A, Rendering Provider (service-specific).
808 // Used if the rendering provider for this service line is different
809 // from that in loop 2310B.
811 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
813 $out .= "NM1" . // Loop 2310B Rendering Provider
816 "*" . $claim->providerLastName($prockey) .
817 "*" . $claim->providerFirstName($prockey) .
818 "*" . $claim->providerMiddleName($prockey) .
821 if ($claim->providerNPI($prockey)) { $out .=
823 "*" . $claim->providerNPI($prockey);
826 "*" . $claim->providerSSN($prockey);
827 $log .= "*** Rendering provider has no NPI.\n";
831 if ($claim->providerTaxonomy($prockey)) {
834 "*PE" . // PErforming provider
836 "*" . $claim->providerTaxonomy($prockey) .
840 // REF*1C is required here for the Medicare provider number if NPI was
841 // specified in NM109. Not sure if other payers require anything here.
842 if ($claim->providerNumber($prockey)) {
845 "*" . $claim->providerNumberType($prockey) .
846 "*" . $claim->providerNumber($prockey) .
851 // Loop 2430, adjudication by previous payers.
853 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
854 if ($claim->payerSequence($ins) > $claim->payerSequence())
855 continue; // payer is future, not previous
857 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
858 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
860 if ($payerpaid[1] == 0 && !count($aarr)) {
861 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
862 "' has no payments or adjustments from previous payer!\n";
867 $out .= "SVD" . // Service line adjudication. Page 554.
868 "*" . $claim->payerID($ins) .
869 "*" . $payerpaid[1] .
870 "*HC:" . $claim->cptKey($prockey) .
872 "*" . $claim->cptUnits($prockey) .
875 $tmpdate = $payerpaid[0];
876 foreach ($aarr as $a) {
878 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
883 if (!$tmpdate) $tmpdate = $a[0];
888 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
895 } // end this procedure
898 $out .= "SE" . // SE Trailer
903 $out .= "GE" . // GE Trailer
908 $out .= "IEA" . // IEA Trailer