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()) {
158 if($claim->federalIdType()){
159 $out .= "*" . $claim->federalIdType();
162 $out .= "*EI";//For dealing with the situation before adding selection for TaxId type In facility ie default to EIN.
164 $out .= "*" . $claim->billingFacilityETIN() .
168 if ($claim->providerNumberType() && $claim->providerNumber()) {
171 "*" . $claim->providerNumberType() .
172 "*" . $claim->providerNumber() .
175 else if ($claim->providerNumber()) {
176 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
180 $out .= "NM1" . // Loop 2010AB Pay-To Provider
183 "*" . $claim->billingFacilityName() .
188 if ($claim->billingFacilityNPI())
189 $out .= "*XX*" . $claim->billingFacilityNPI();
191 $out .= "*24*" . $claim->billingFacilityETIN();
196 "*" . $claim->billingFacilityStreet() .
201 "*" . $claim->billingFacilityCity() .
202 "*" . $claim->billingFacilityState() .
203 "*" . $claim->billingFacilityZip() .
206 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
210 "*" . $claim->billingFacilityETIN() .
217 $out .= "HL" . // Loop 2000B Subscriber HL Loop
219 "*$HLBillingPayToProvider" .
224 $HLSubscriber = $HLcount++
;
226 if (!$claim->payerSequence()) {
227 $log .= "*** Error: Insurance information is missing!\n";
230 $out .= "SBR" . // Subscriber Information
231 "*" . $claim->payerSequence() .
232 "*" . $claim->insuredRelationship() .
233 "*" . $claim->groupNumber() .
234 "*" . $claim->groupName() .
235 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
239 "*" . $claim->claimType() . // Zirmed replaces this
243 $out .= "NM1" . // Loop 2010BA Subscriber
246 "*" . $claim->insuredLastName() .
247 "*" . $claim->insuredFirstName() .
248 "*" . $claim->insuredMiddleName() .
252 "*" . $claim->policyNumber() .
257 "*" . $claim->insuredStreet() .
262 "*" . $claim->insuredCity() .
263 "*" . $claim->insuredState() .
264 "*" . $claim->insuredZip() .
270 "*" . $claim->insuredDOB() .
271 "*" . $claim->insuredSex() .
275 $out .= "NM1" . // Loop 2010BB Payer
278 "*" . $claim->payerName() .
284 // Zirmed ignores this if using payer name matching:
285 "*" . ($encounter_claim ?
$claim->payerAltID() : $claim->payerID()) .
288 // if (!$claim->payerID()) {
289 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
294 "*" . $claim->payerStreet() .
299 "*" . $claim->payerCity() .
300 "*" . $claim->payerState() .
301 "*" . $claim->payerZip() .
304 if (! $claim->isSelfOfInsured()) {
306 $out .= "HL" . // Loop 2000C Patient Information
317 "*" . $claim->insuredRelationship() .
321 $out .= "NM1" . // Loop 2010CA Patient
324 "*" . $claim->patientLastName() .
325 "*" . $claim->patientFirstName() .
326 "*" . $claim->patientMiddleName() .
331 "*" . $claim->patientStreet() .
336 "*" . $claim->patientCity() .
337 "*" . $claim->patientState() .
338 "*" . $claim->patientZip() .
344 "*" . $claim->patientDOB() .
345 "*" . $claim->patientSex() .
347 } // end of patient different from insured
349 $proccount = $claim->procCount();
351 $clm_total_charges = 0;
352 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
353 $clm_total_charges +
= $claim->cptCharges($prockey);
356 if (!$clm_total_charges) {
357 $log .= "*** This claim has no charges!\n";
361 $out .= "CLM" . // Loop 2300 Claim
363 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
366 "*" . $claim->facilityPOS() . "::" . $claim->frequencyTypeCode() .
369 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
375 $out .= "DTP" . // Date of Onset
378 "*" . $claim->onsetDate() .
381 if (strcmp($claim->facilityPOS(),'21') == 0) {
383 $out .= "DTP" . // Date of Hospitalization
386 "*" . $claim->onsetDate() .
390 $patientpaid = $claim->patientPaidAmount();
391 if ($patientpaid != 0) {
393 $out .= "AMT" . // Patient paid amount. Page 220.
399 if ($claim->priorAuth()) {
401 $out .= "REF" . // Prior Authorization Number
403 "*" . $claim->priorAuth() .
407 if ($claim->cliaCode()) {
408 // Required by Medicare when in-house labs are done.
410 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
412 "*" . $claim->cliaCode() .
416 // Note: This would be the place to implement the NTE segment for loop 2300.
418 // Diagnoses, up to 8 per HI segment.
419 $da = $claim->diagArray();
420 $diag_type_code = 'BK';
422 foreach ($da as $diag) {
424 if ($tmp) $out .= "~\n";
426 $out .= "HI"; // Health Diagnosis Codes
428 $out .= "*$diag_type_code:" . $diag;
429 $diag_type_code = 'BF';
432 if ($tmp) $out .= "~\n";
434 if ($claim->referrerLastName()) {
435 // Medicare requires referring provider's name and UPIN.
437 $out .= "NM1" . // Loop 2310A Referring Provider
440 "*" . $claim->referrerLastName() .
441 "*" . $claim->referrerFirstName() .
442 "*" . $claim->referrerMiddleName() .
445 if ($claim->referrerNPI()) { $out .=
447 "*" . $claim->referrerNPI();
450 "*" . $claim->referrerSSN();
454 if ($claim->referrerTaxonomy()) {
457 "*RF" . // ReFerring provider
459 "*" . $claim->referrerTaxonomy() .
463 if ($claim->referrerUPIN()) {
465 $out .= "REF" . // Referring Provider Secondary Identification
467 "*" . $claim->referrerUPIN() .
473 $out .= "NM1" . // Loop 2310B Rendering Provider
476 "*" . $claim->providerLastName() .
477 "*" . $claim->providerFirstName() .
478 "*" . $claim->providerMiddleName() .
481 if ($claim->providerNPI()) { $out .=
483 "*" . $claim->providerNPI();
486 "*" . $claim->providerSSN();
487 $log .= "*** Rendering provider has no NPI.\n";
491 if ($claim->providerTaxonomy()) {
494 "*PE" . // PErforming provider
496 "*" . $claim->providerTaxonomy() .
500 // REF*1C is required here for the Medicare provider number if NPI was
501 // specified in NM109. Not sure if other payers require anything here.
502 if ($claim->providerNumber()) {
505 "*" . $claim->providerNumberType() .
506 "*" . $claim->providerNumber() .
510 // Loop 2310D is omitted in the case of home visits (POS=12).
511 if ($claim->facilityPOS() != 12) {
513 $out .= "NM1" . // Loop 2310D Service Location
516 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
517 "*" . $claim->facilityName();
519 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
524 if ($claim->facilityNPI()) { $out .=
525 "*XX*" . $claim->facilityNPI();
527 "*24*" . $claim->facilityETIN();
528 $log .= "*** Service location has no NPI.\n";
532 if ($claim->facilityStreet()) {
535 "*" . $claim->facilityStreet() .
538 if ($claim->facilityState()) {
541 "*" . $claim->facilityCity() .
542 "*" . $claim->facilityState() .
543 "*" . $claim->facilityZip() .
548 // Loop 2310E, Supervising Provider
550 if ($claim->supervisorLastName()) {
553 "*DQ" . // Supervising Physician
555 "*" . $claim->supervisorLastName() .
556 "*" . $claim->supervisorFirstName() .
557 "*" . $claim->supervisorMiddleName() .
558 "*" . // NM106 not used
560 if ($claim->supervisorNPI()) { $out .=
562 "*" . $claim->supervisorNPI();
565 "*" . $claim->supervisorSSN();
569 if ($claim->supervisorNumber()) {
572 "*" . $claim->supervisorNumberType() .
573 "*" . $claim->supervisorNumber() .
578 $prev_pt_resp = $clm_total_charges; // for computation below
580 // Loops 2320 and 2330*, other subscriber/payer information.
582 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
584 $tmp1 = $claim->claimType($ins);
585 $tmp2 = 'C1'; // Here a kludge. See page 321.
586 if ($tmp1 === 'CI') $tmp2 = 'C1';
587 if ($tmp1 === 'AM') $tmp2 = 'AP';
588 if ($tmp1 === 'HM') $tmp2 = 'HM';
589 if ($tmp1 === 'MB') $tmp2 = 'MB';
590 if ($tmp1 === 'MC') $tmp2 = 'MC';
591 if ($tmp1 === '09') $tmp2 = 'PP';
593 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
594 "*" . $claim->payerSequence($ins) .
595 "*" . $claim->insuredRelationship($ins) .
596 "*" . $claim->groupNumber($ins) .
597 "*" . $claim->groupName($ins) .
602 "*" . $claim->claimType($ins) .
605 // Things that apply only to previous payers, not future payers.
607 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
609 // Generate claim-level adjustments.
610 $aarr = $claim->payerAdjustments($ins);
611 foreach ($aarr as $a) {
613 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
620 $payerpaid = $claim->payerTotals($ins);
622 $out .= "AMT" . // Previous payer's paid amount. Page 332.
624 "*" . $payerpaid[1] .
627 // Patient responsibility amount as of this previous payer.
628 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
629 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
632 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
634 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
638 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
640 "*" . sprintf('%.2f', $prev_pt_resp) .
643 } // End of things that apply only to previous payers.
646 $out .= "DMG" . // Other subscriber demographic information. Page 342.
648 "*" . $claim->insuredDOB($ins) .
649 "*" . $claim->insuredSex($ins) .
653 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
663 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
666 "*" . $claim->insuredLastName($ins) .
667 "*" . $claim->insuredFirstName($ins) .
668 "*" . $claim->insuredMiddleName($ins) .
672 "*" . $claim->policyNumber($ins) .
677 "*" . $claim->insuredStreet($ins) .
682 "*" . $claim->insuredCity($ins) .
683 "*" . $claim->insuredState($ins) .
684 "*" . $claim->insuredZip($ins) .
688 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
691 "*" . $claim->payerName($ins) .
697 "*" . $claim->payerID($ins) .
700 // if (!$claim->payerID($ins)) {
701 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
704 // Payer address (N3 and N4) are added below so that Gateway EDI can
705 // auto-generate secondary claims. These do NOT appear in my copy of
706 // the spec! -- Rod 2008-06-12
708 if (trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
711 "*" . $claim->payerStreet($ins) .
716 "*" . $claim->payerCity($ins) .
717 "*" . $claim->payerState($ins) .
718 "*" . $claim->payerZip($ins) .
722 } // End loops 2320/2330*.
726 // Procedure loop starts here.
728 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
732 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
737 $out .= "SV1" . // Professional Service. Page 400.
738 "*HC:" . $claim->cptKey($prockey) .
739 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
741 "*" . $claim->cptUnits($prockey) .
745 $dia = $claim->diagIndexArray($prockey);
747 foreach ($dia as $dindex) {
750 if (++
$i >= 4) break;
754 if (!$claim->cptCharges($prockey)) {
755 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
759 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
763 $out .= "DTP" . // Date of Service. Page 435.
766 "*" . $claim->serviceDate() .
769 // AMT*AAE segment for Approved Amount from previous payer.
770 // Medicare secondaries seem to require this.
772 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
773 if ($claim->payerSequence($ins) > $claim->payerSequence())
774 continue; // payer is future, not previous
775 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
777 $out .= "AMT" . // Approved amount per previous payer. Page 485.
779 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
784 // Loop 2410, Drug Information. Medicaid insurers seem to want this
787 $ndc = $claim->cptNDCID($prockey);
790 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
791 "*" . // Per addendum, LIN01 is not used.
796 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
797 $log .= "*** NDC code '$ndc' has invalid format!\n";
801 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
802 if (!$tmpunits) $tmpunits = 1;
803 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
806 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
807 "*" . $claim->cptNDCQuantity($prockey) .
808 "*" . $claim->cptNDCUOM($prockey) .
812 // Loop 2420A, Rendering Provider (service-specific).
813 // Used if the rendering provider for this service line is different
814 // from that in loop 2310B.
816 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
818 $out .= "NM1" . // Loop 2310B Rendering Provider
821 "*" . $claim->providerLastName($prockey) .
822 "*" . $claim->providerFirstName($prockey) .
823 "*" . $claim->providerMiddleName($prockey) .
826 if ($claim->providerNPI($prockey)) { $out .=
828 "*" . $claim->providerNPI($prockey);
831 "*" . $claim->providerSSN($prockey);
832 $log .= "*** Rendering provider has no NPI.\n";
836 if ($claim->providerTaxonomy($prockey)) {
839 "*PE" . // PErforming provider
841 "*" . $claim->providerTaxonomy($prockey) .
845 // REF*1C is required here for the Medicare provider number if NPI was
846 // specified in NM109. Not sure if other payers require anything here.
847 if ($claim->providerNumber($prockey)) {
850 "*" . $claim->providerNumberType($prockey) .
851 "*" . $claim->providerNumber($prockey) .
856 // Loop 2430, adjudication by previous payers.
858 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
859 if ($claim->payerSequence($ins) > $claim->payerSequence())
860 continue; // payer is future, not previous
862 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
863 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
865 if ($payerpaid[1] == 0 && !count($aarr)) {
866 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
867 "' has no payments or adjustments from previous payer!\n";
872 $out .= "SVD" . // Service line adjudication. Page 554.
873 "*" . $claim->payerID($ins) .
874 "*" . $payerpaid[1] .
875 "*HC:" . $claim->cptKey($prockey) .
877 "*" . $claim->cptUnits($prockey) .
880 $tmpdate = $payerpaid[0];
881 foreach ($aarr as $a) {
883 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
888 if (!$tmpdate) $tmpdate = $a[0];
893 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
900 } // end this procedure
903 $out .= "SE" . // SE Trailer
908 $out .= "GE" . // GE Trailer
913 $out .= "IEA" . // IEA Trailer