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";
366 $out .= "CLM" . // Loop 2300 Claim
368 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
371 "*" . sprintf('%02d', $claim->facilityPOS()) . "::" . $claim->frequencyTypeCode() . // Changed to correct single digit output
374 "*" . ($claim->billingFacilityAssignment() ?
'Y' : 'N') .
380 $out .= "DTP" . // Date of Onset
383 "*" . $claim->onsetDate() .
386 if (strcmp($claim->facilityPOS(),'21') == 0) {
388 $out .= "DTP" . // Date of Hospitalization
391 "*" . $claim->onsetDate() .
395 $patientpaid = $claim->patientPaidAmount();
396 if ($patientpaid != 0) {
398 $out .= "AMT" . // Patient paid amount. Page 220.
404 if ($claim->priorAuth()) {
406 $out .= "REF" . // Prior Authorization Number
408 "*" . $claim->priorAuth() .
412 if ($claim->cliaCode() and $claim->claimType() === 'MB') {
413 // Required by Medicare when in-house labs are done.
415 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
417 "*" . $claim->cliaCode() .
421 // Note: This would be the place to implement the NTE segment for loop 2300.
423 // Diagnoses, up to 8 per HI segment.
424 $da = $claim->diagArray();
425 $diag_type_code = 'BK';
427 foreach ($da as $diag) {
429 if ($tmp) $out .= "~\n";
431 $out .= "HI"; // Health Diagnosis Codes
433 $out .= "*$diag_type_code:" . $diag;
434 $diag_type_code = 'BF';
437 if ($tmp) $out .= "~\n";
439 if ($claim->referrerLastName()) {
440 // Medicare requires referring provider's name and UPIN.
442 $out .= "NM1" . // Loop 2310A Referring Provider
445 "*" . $claim->referrerLastName() .
446 "*" . $claim->referrerFirstName() .
447 "*" . $claim->referrerMiddleName() .
450 if ($claim->referrerNPI()) { $out .=
452 "*" . $claim->referrerNPI();
455 "*" . $claim->referrerSSN();
459 if ($claim->referrerTaxonomy()) {
462 "*RF" . // ReFerring provider
464 "*" . $claim->referrerTaxonomy() .
468 if ($claim->referrerUPIN()) {
470 $out .= "REF" . // Referring Provider Secondary Identification
472 "*" . $claim->referrerUPIN() .
478 $out .= "NM1" . // Loop 2310B Rendering Provider
481 "*" . $claim->providerLastName() .
482 "*" . $claim->providerFirstName() .
483 "*" . $claim->providerMiddleName() .
486 if ($claim->providerNPI()) { $out .=
488 "*" . $claim->providerNPI();
491 "*" . $claim->providerSSN();
492 $log .= "*** Rendering provider has no NPI.\n";
496 if ($claim->providerTaxonomy()) {
499 "*PE" . // PErforming provider
501 "*" . $claim->providerTaxonomy() .
505 // REF*1C is required here for the Medicare provider number if NPI was
506 // specified in NM109. Not sure if other payers require anything here.
507 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
509 if (trim($claim->x12gsreceiverid()) != '470819582') { // if NOT ECLAIMS EDI
510 if ($claim->providerNumber()) {
513 "*" . $claim->providerNumberType() .
514 "*" . $claim->providerNumber() .
519 // Loop 2310D is omitted in the case of home visits (POS=12).
520 if ($claim->facilityPOS() != 12) {
522 $out .= "NM1" . // Loop 2310D Service Location
525 if ($claim->facilityName() ||
$claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
526 "*" . $claim->facilityName();
528 if ($claim->facilityNPI() ||
$claim->facilityETIN()) { $out .=
533 if ($claim->facilityNPI()) { $out .=
534 "*XX*" . $claim->facilityNPI();
536 "*24*" . $claim->facilityETIN();
537 $log .= "*** Service location has no NPI.\n";
541 if ($claim->facilityStreet()) {
544 "*" . $claim->facilityStreet() .
547 if ($claim->facilityState()) {
550 "*" . $claim->facilityCity() .
551 "*" . $claim->facilityState() .
552 "*" . $claim->facilityZip() .
557 // Loop 2310E, Supervising Provider
559 if ($claim->supervisorLastName()) {
562 "*DQ" . // Supervising Physician
564 "*" . $claim->supervisorLastName() .
565 "*" . $claim->supervisorFirstName() .
566 "*" . $claim->supervisorMiddleName() .
567 "*" . // NM106 not used
569 if ($claim->supervisorNPI()) { $out .=
571 "*" . $claim->supervisorNPI();
574 "*" . $claim->supervisorSSN();
578 if ($claim->supervisorNumber()) {
581 "*" . $claim->supervisorNumberType() .
582 "*" . $claim->supervisorNumber() .
587 $prev_pt_resp = $clm_total_charges; // for computation below
589 // Loops 2320 and 2330*, other subscriber/payer information.
591 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
593 $tmp1 = $claim->claimType($ins);
594 $tmp2 = 'C1'; // Here a kludge. See page 321.
595 if ($tmp1 === 'CI') $tmp2 = 'C1';
596 if ($tmp1 === 'AM') $tmp2 = 'AP';
597 if ($tmp1 === 'HM') $tmp2 = 'HM';
598 if ($tmp1 === 'MB') $tmp2 = 'MB';
599 if ($tmp1 === 'MC') $tmp2 = 'MC';
600 if ($tmp1 === '09') $tmp2 = 'PP';
602 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
603 "*" . $claim->payerSequence($ins) .
604 "*" . $claim->insuredRelationship($ins) .
605 "*" . $claim->groupNumber($ins) .
606 "*" . $claim->groupName($ins) .
611 "*" . $claim->claimType($ins) .
614 // Things that apply only to previous payers, not future payers.
616 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
618 // Generate claim-level adjustments.
619 $aarr = $claim->payerAdjustments($ins);
620 foreach ($aarr as $a) {
622 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
629 $payerpaid = $claim->payerTotals($ins);
631 $out .= "AMT" . // Previous payer's paid amount. Page 332.
633 "*" . $payerpaid[1] .
636 // Patient responsibility amount as of this previous payer.
637 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
638 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
641 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
643 "*" . sprintf('%.2f', $payerpaid[1] +
$prev_pt_resp) .
647 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
649 "*" . sprintf('%.2f', $prev_pt_resp) .
652 } // End of things that apply only to previous payers.
655 $out .= "DMG" . // Other subscriber demographic information. Page 342.
657 "*" . $claim->insuredDOB($ins) .
658 "*" . $claim->insuredSex($ins) .
662 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
672 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
675 "*" . $claim->insuredLastName($ins) .
676 "*" . $claim->insuredFirstName($ins) .
677 "*" . $claim->insuredMiddleName($ins) .
681 "*" . $claim->policyNumber($ins) .
686 "*" . $claim->insuredStreet($ins) .
691 "*" . $claim->insuredCity($ins) .
692 "*" . $claim->insuredState($ins) .
693 "*" . $claim->insuredZip($ins) .
697 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
700 "*" . $claim->payerName($ins) .
706 "*" . $claim->payerID($ins) .
709 // if (!$claim->payerID($ins)) {
710 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
713 // Payer address (N3 and N4) are added below so that Gateway EDI can
714 // auto-generate secondary claims. These do NOT appear in my copy of
715 // the spec! -- Rod 2008-06-12
717 if (trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
720 "*" . $claim->payerStreet($ins) .
725 "*" . $claim->payerCity($ins) .
726 "*" . $claim->payerState($ins) .
727 "*" . $claim->payerZip($ins) .
731 } // End loops 2320/2330*.
735 // Procedure loop starts here.
737 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
741 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
746 $out .= "SV1" . // Professional Service. Page 400.
747 "*HC:" . $claim->cptKey($prockey) .
748 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
750 "*" . $claim->cptUnits($prockey) .
754 $dia = $claim->diagIndexArray($prockey);
756 foreach ($dia as $dindex) {
759 if (++
$i >= 4) break;
763 if (!$claim->cptCharges($prockey)) {
764 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
768 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
772 $out .= "DTP" . // Date of Service. Page 435.
775 "*" . $claim->serviceDate() .
778 // AMT*AAE segment for Approved Amount from previous payer.
779 // Medicare secondaries seem to require this.
781 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
782 if ($claim->payerSequence($ins) > $claim->payerSequence())
783 continue; // payer is future, not previous
784 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
786 $out .= "AMT" . // Approved amount per previous payer. Page 485.
788 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
793 // Loop 2410, Drug Information. Medicaid insurers seem to want this
796 $ndc = $claim->cptNDCID($prockey);
799 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
800 "*" . // Per addendum, LIN01 is not used.
805 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
806 $log .= "*** NDC code '$ndc' has invalid format!\n";
810 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
811 if (!$tmpunits) $tmpunits = 1;
812 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
815 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
816 "*" . $claim->cptNDCQuantity($prockey) .
817 "*" . $claim->cptNDCUOM($prockey) .
821 // Loop 2420A, Rendering Provider (service-specific).
822 // Used if the rendering provider for this service line is different
823 // from that in loop 2310B.
825 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
827 $out .= "NM1" . // Loop 2310B Rendering Provider
830 "*" . $claim->providerLastName($prockey) .
831 "*" . $claim->providerFirstName($prockey) .
832 "*" . $claim->providerMiddleName($prockey) .
835 if ($claim->providerNPI($prockey)) { $out .=
837 "*" . $claim->providerNPI($prockey);
840 "*" . $claim->providerSSN($prockey);
841 $log .= "*** Rendering provider has no NPI.\n";
845 if ($claim->providerTaxonomy($prockey)) {
848 "*PE" . // PErforming provider
850 "*" . $claim->providerTaxonomy($prockey) .
854 // REF*1C is required here for the Medicare provider number if NPI was
855 // specified in NM109. Not sure if other payers require anything here.
856 if ($claim->providerNumber($prockey)) {
859 "*" . $claim->providerNumberType($prockey) .
860 "*" . $claim->providerNumber($prockey) .
865 // Loop 2430, adjudication by previous payers.
867 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
868 if ($claim->payerSequence($ins) > $claim->payerSequence())
869 continue; // payer is future, not previous
871 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
872 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
874 if ($payerpaid[1] == 0 && !count($aarr)) {
875 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
876 "' has no payments or adjustments from previous payer!\n";
881 $out .= "SVD" . // Service line adjudication. Page 554.
882 "*" . $claim->payerID($ins) .
883 "*" . $payerpaid[1] .
884 "*HC:" . $claim->cptKey($prockey) .
886 "*" . $claim->cptUnits($prockey) .
889 $tmpdate = $payerpaid[0];
890 foreach ($aarr as $a) {
892 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
897 if (!$tmpdate) $tmpdate = $a[0];
902 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
909 } // end this procedure
912 $out .= "SE" . // SE Trailer
917 $out .= "GE" . // GE Trailer
922 $out .= "IEA" . // IEA Trailer