2 // Copyright (C) 2007-2008 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) {
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";
30 "*" . $claim->x12gssenderid() .
32 "*" . $claim->x12gsreceiverid() .
45 "*" . $claim->x12gssenderid() .
46 "*" . $claim->x12gsreceiverid() .
47 "*" . date('Ymd', $today) .
48 "*" . date('Hi', $today) .
51 "*" . $claim->x12gsversionstring() .
65 "*" . date('Ymd', $today) .
73 "*" . $claim->x12gsversionstring() .
77 $out .= "NM1" . // Loop 1000A Submitter
80 "*" . $claim->billingFacilityName() .
86 "*" . $claim->billingFacilityETIN() .
92 "*" . $claim->billingContactName() .
94 "*" . $claim->billingContactPhone() .
98 $out .= "NM1" . // Loop 1000B Receiver
101 "*" . $claim->clearingHouseName() .
107 "*" . $claim->clearingHouseETIN() .
113 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
120 $HLBillingPayToProvider = $HLcount++
;
123 $out .= "NM1" . // Loop 2010AA Billing Provider
126 "*" . $claim->billingFacilityName() .
131 if ($claim->billingFacilityNPI()) {
132 $out .= "*XX*" . $claim->billingFacilityNPI();
134 $log .= "*** Billing facility has no NPI.\n";
135 $out .= "*24*" . $claim->billingFacilityETIN();
141 "*" . $claim->billingFacilityStreet() .
146 "*" . $claim->billingFacilityCity() .
147 "*" . $claim->billingFacilityState() .
148 "*" . $claim->billingFacilityZip() .
151 // Add a REF*EI*<ein> segment if NPI was specified in the NM1 above.
152 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
156 "*" . $claim->billingFacilityETIN() .
160 if ($claim->providerNumberType() && $claim->providerNumber()) {
163 "*" . $claim->providerNumberType() .
164 "*" . $claim->providerNumber() .
167 else if ($claim->providerNumber()) {
168 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
172 $out .= "NM1" . // Loop 2010AB Pay-To Provider
175 "*" . $claim->billingFacilityName() .
180 if ($claim->billingFacilityNPI())
181 $out .= "*XX*" . $claim->billingFacilityNPI();
183 $out .= "*24*" . $claim->billingFacilityETIN();
188 "*" . $claim->billingFacilityStreet() .
193 "*" . $claim->billingFacilityCity() .
194 "*" . $claim->billingFacilityState() .
195 "*" . $claim->billingFacilityZip() .
198 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
202 "*" . $claim->billingFacilityETIN() .
209 $out .= "HL" . // Loop 2000B Subscriber HL Loop
211 "*$HLBillingPayToProvider" .
216 $HLSubscriber = $HLcount++
;
218 if (!$claim->payerSequence()) {
219 $log .= "*** Error: Insurance information is missing!\n";
222 $out .= "SBR" . // Subscriber Information
223 "*" . $claim->payerSequence() .
224 "*" . $claim->insuredRelationship() .
225 "*" . $claim->groupNumber() .
226 "*" . $claim->groupName() .
227 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
231 "*" . $claim->claimType() . // Zirmed replaces this
235 $out .= "NM1" . // Loop 2010BA Subscriber
238 "*" . $claim->insuredLastName() .
239 "*" . $claim->insuredFirstName() .
240 "*" . $claim->insuredMiddleName() .
244 "*" . $claim->policyNumber() .
249 "*" . $claim->insuredStreet() .
254 "*" . $claim->insuredCity() .
255 "*" . $claim->insuredState() .
256 "*" . $claim->insuredZip() .
262 "*" . $claim->insuredDOB() .
263 "*" . $claim->insuredSex() .
267 $out .= "NM1" . // Loop 2010BB Payer
270 "*" . $claim->payerName() .
276 "*" . $claim->payerID() . // Zirmed ignores this if using Payer Name Matching.
279 // if (!$claim->payerID()) {
280 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
285 "*" . $claim->payerStreet() .
290 "*" . $claim->payerCity() .
291 "*" . $claim->payerState() .
292 "*" . $claim->payerZip() .
295 if (! $claim->isSelfOfInsured()) {
297 $out .= "HL" . // Loop 2000C Patient Information
308 "*" . $claim->insuredRelationship() .
312 $out .= "NM1" . // Loop 2010CA Patient
315 "*" . $claim->patientLastName() .
316 "*" . $claim->patientFirstName() .
317 "*" . $claim->patientMiddleName() .
322 "*" . $claim->patientStreet() .
327 "*" . $claim->patientCity() .
328 "*" . $claim->patientState() .
329 "*" . $claim->patientZip() .
335 "*" . $claim->patientDOB() .
336 "*" . $claim->patientSex() .
338 } // end of patient different from insured
340 $proccount = $claim->procCount();
342 $clm_total_charges = 0;
343 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
344 $clm_total_charges +
= $claim->cptCharges($prockey);
347 if (!$clm_total_charges) {
348 $log .= "*** This claim has no charges!\n";
352 $out .= "CLM" . // Loop 2300 Claim
354 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
357 "*" . $claim->facilityPOS() . "::1" .
366 $out .= "DTP" . // Date of Onset
369 "*" . $claim->onsetDate() .
372 if (strcmp($claim->facilityPOS(),'21') == 0) {
374 $out .= "DTP" . // Date of Hospitalization
377 "*" . $claim->onsetDate() .
381 $patientpaid = $claim->patientPaidAmount();
382 if ($patientpaid != 0) {
384 $out .= "AMT" . // Patient paid amount. Page 220.
390 if ($claim->priorAuth()) {
392 $out .= "REF" . // Prior Authorization Number
394 "*" . $claim->priorAuth() .
398 if ($claim->cliaCode()) {
399 // Required by Medicare when in-house labs are done.
401 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
403 "*" . $claim->cliaCode() .
407 $da = $claim->diagArray();
409 $out .= "HI"; // Health Diagnosis Codes
410 $diag_type_code = 'BK';
411 foreach ($da as $diag) {
412 $out .= "*$diag_type_code:" . $diag;
413 $diag_type_code = 'BF';
417 if ($claim->referrerLastName()) {
418 // Medicare requires referring provider's name and UPIN.
420 $out .= "NM1" . // Loop 2310A Referring Provider
423 "*" . $claim->referrerLastName() .
424 "*" . $claim->referrerFirstName() .
425 "*" . $claim->referrerMiddleName() .
428 if ($claim->referrerNPI()) { $out .=
430 "*" . $claim->referrerNPI();
433 "*" . $claim->referrerSSN();
437 if ($claim->referrerTaxonomy()) {
440 "*RF" . // ReFerring provider
441 "*" . $claim->referrerTaxonomy() .
445 if ($claim->referrerUPIN()) {
447 $out .= "REF" . // Referring Provider Secondary Identification
449 "*" . $claim->referrerUPIN() .
455 $out .= "NM1" . // Loop 2310B Rendering Provider
458 "*" . $claim->providerLastName() .
459 "*" . $claim->providerFirstName() .
460 "*" . $claim->providerMiddleName() .
463 if ($claim->providerNPI()) { $out .=
465 "*" . $claim->providerNPI();
468 "*" . $claim->providerSSN();
469 $log .= "*** Rendering provider has no NPI.\n";
473 /*******************************************************************
475 $out .= "PRV" . // Rendering Provider Information
480 *******************************************************************/
481 if ($claim->providerTaxonomy()) {
484 "*PE" . // PErforming provider
485 "*" . $claim->providerTaxonomy() .
488 /******************************************************************/
490 // REF*1C is required here for the Medicare provider number if NPI was
491 // specified in NM109. Not sure if other payers require anything here.
492 if ($claim->providerNumber()) {
495 "*" . $claim->providerNumberType() .
496 "*" . $claim->providerNumber() .
501 $out .= "NM1" . // Loop 2310D Service Location
504 "*" . $claim->facilityName() .
509 if ($claim->facilityNPI()) { $out .=
510 "*XX*" . $claim->facilityNPI();
512 "*24*" . $claim->facilityETIN();
513 $log .= "*** Service location has no NPI.\n";
519 "*" . $claim->facilityStreet() .
524 "*" . $claim->facilityCity() .
525 "*" . $claim->facilityState() .
526 "*" . $claim->facilityZip() .
529 $prev_pt_resp = $clm_total_charges; // for computation below
531 // Loops 2320 and 2330*, other subscriber/payer information.
533 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
535 $tmp1 = $claim->claimType($ins);
536 $tmp2 = 'C1'; // Here a kludge. See page 321.
537 if ($tmp1 === 'CI') $tmp2 = 'C1';
538 if ($tmp1 === 'AM') $tmp2 = 'AP';
539 if ($tmp1 === 'HM') $tmp2 = 'HM';
540 if ($tmp1 === 'MB') $tmp2 = 'MB';
541 if ($tmp1 === 'MC') $tmp2 = 'MC';
542 if ($tmp1 === '09') $tmp2 = 'PP';
544 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
545 "*" . $claim->payerSequence($ins) .
546 "*" . $claim->insuredRelationship($ins) .
547 "*" . $claim->groupNumber($ins) .
548 "*" . $claim->groupName($ins) .
553 "*" . $claim->claimType($ins) .
556 // Things that apply only to previous payers, not future payers.
558 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
560 // Generate claim-level adjustments.
561 $aarr = $claim->payerAdjustments($ins);
562 foreach ($aarr as $a) {
564 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
571 $payerpaid = $claim->payerTotals($ins);
573 $out .= "AMT" . // Previous payer's paid amount. Page 332.
575 "*" . $payerpaid[1] .
578 // Patient responsibility amount as of this previous payer.
579 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
580 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
583 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
585 "*" . sprintf('%.2f', $prev_pt_resp) .
588 } // End of things that apply only to previous payers.
591 $out .= "DMG" . // Other subscriber demographic information. Page 342.
593 "*" . $claim->insuredDOB($ins) .
594 "*" . $claim->insuredSex($ins) .
598 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
608 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
611 "*" . $claim->insuredLastName($ins) .
612 "*" . $claim->insuredFirstName($ins) .
613 "*" . $claim->insuredMiddleName($ins) .
617 "*" . $claim->policyNumber($ins) .
622 "*" . $claim->insuredStreet($ins) .
627 "*" . $claim->insuredCity($ins) .
628 "*" . $claim->insuredState($ins) .
629 "*" . $claim->insuredZip($ins) .
633 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
636 "*" . $claim->payerName($ins) .
642 "*" . $claim->payerID($ins) .
645 // if (!$claim->payerID($ins)) {
646 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
649 // Payer address (N3 and N4) are added below so that Gateway EDI can
650 // auto-generate secondary claims. These do NOT appear in my copy of
651 // the spec! -- Rod 2008-06-12
655 "*" . $claim->payerStreet($ins) .
660 "*" . $claim->payerCity($ins) .
661 "*" . $claim->payerState($ins) .
662 "*" . $claim->payerZip($ins) .
665 } // End loops 2320/2330*.
669 // Procedure loop starts here.
671 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
675 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
680 $out .= "SV1" . // Professional Service. Page 400.
681 "*HC:" . $claim->cptKey($prockey) .
682 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
684 "*" . $claim->cptUnits($prockey) .
688 $dia = $claim->diagIndexArray($prockey);
690 foreach ($dia as $dindex) {
691 $out .= $separator . $dindex;
696 if (!$claim->cptCharges($prockey)) {
697 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
701 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
705 $out .= "DTP" . // Date of Service. Page 435.
708 "*" . $claim->serviceDate() .
711 // Loop 2410, Drug Information. Medicaid insurers seem to want this
714 $ndc = $claim->cptNDCID($prockey);
717 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
723 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
724 $log .= "*** NDC code '$ndc' has invalid format!\n";
728 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
729 if (!$tmpunits) $tmpunits = 1;
730 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
733 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
734 "*" . $claim->cptNDCQuantity($prockey) .
735 "*" . $claim->cptNDCUOM($prockey) .
739 // Loop 2430, adjudication by previous payers.
741 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
742 if ($claim->payerSequence($ins) > $claim->payerSequence())
743 continue; // payer is future, not previous
745 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
746 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
748 if ($payerpaid[1] == 0 && !count($aarr)) {
749 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
750 "' has no payments or adjustments from previous payer!\n";
755 $out .= "SVD" . // Service line adjudication. Page 554.
756 "*" . $claim->payerID($ins) .
757 "*" . $payerpaid[1] .
758 "*HC:" . $claim->cptKey($prockey) .
760 "*" . $claim->cptUnits($prockey) .
763 $tmpdate = $payerpaid[0];
764 foreach ($aarr as $a) {
766 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
771 if (!$tmpdate) $tmpdate = $a[0];
776 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
783 } // end this procedure
786 $out .= "SE" . // SE Trailer
791 $out .= "GE" . // GE Trailer
796 $out .= "IEA" . // IEA Trailer