2 // Copyright (C) 2007 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.
153 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
157 "*" . $claim->billingFacilityETIN() .
163 "*" . $claim->providerNumberType() .
164 "*" . $claim->providerNumber() .
168 $out .= "NM1" . // Loop 2010AB Pay-To Provider
171 "*" . $claim->billingFacilityName() .
176 if ($claim->billingFacilityNPI())
177 $out .= "*XX*" . $claim->billingFacilityNPI();
179 $out .= "*24*" . $claim->billingFacilityETIN();
184 "*" . $claim->billingFacilityStreet() .
189 "*" . $claim->billingFacilityCity() .
190 "*" . $claim->billingFacilityState() .
191 "*" . $claim->billingFacilityZip() .
194 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
198 "*" . $claim->billingFacilityETIN() .
205 $out .= "HL" . // Loop 2000B Subscriber HL Loop
207 "*$HLBillingPayToProvider" .
212 $HLSubscriber = $HLcount++
;
215 $out .= "SBR" . // Subscriber Information
216 "*" . $claim->payerSequence() .
217 "*" . $claim->insuredRelationship() .
218 "*" . $claim->groupNumber() .
219 "*" . $claim->groupName() .
220 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
224 "*" . $claim->claimType() . // Zirmed replaces this
228 $out .= "NM1" . // Loop 2010BA Subscriber
231 "*" . $claim->insuredLastName() .
232 "*" . $claim->insuredFirstName() .
233 "*" . $claim->insuredMiddleName() .
237 "*" . $claim->policyNumber() .
242 "*" . $claim->insuredStreet() .
247 "*" . $claim->insuredCity() .
248 "*" . $claim->insuredState() .
249 "*" . $claim->insuredZip() .
255 "*" . $claim->insuredDOB() .
256 "*" . $claim->insuredSex() .
260 $out .= "NM1" . // Loop 2010BB Payer
263 "*" . $claim->payerName() .
269 "*" . $claim->payerID() . // Zirmed ignores this if using Payer Name Matching.
272 // if (!$claim->payerID()) {
273 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
278 "*" . $claim->payerStreet() .
283 "*" . $claim->payerCity() .
284 "*" . $claim->payerState() .
285 "*" . $claim->payerZip() .
288 if (! $claim->isSelfOfInsured()) {
290 $out .= "HL" . // Loop 2000C Patient Information
301 "*" . $claim->insuredRelationship() .
305 $out .= "NM1" . // Loop 2010CA Patient
308 "*" . $claim->patientLastName() .
309 "*" . $claim->patientFirstName() .
310 "*" . $claim->patientMiddleName() .
315 "*" . $claim->patientStreet() .
320 "*" . $claim->patientCity() .
321 "*" . $claim->patientState() .
322 "*" . $claim->patientZip() .
328 "*" . $claim->patientDOB() .
329 "*" . $claim->patientSex() .
331 } // end of patient different from insured
333 $proccount = $claim->procCount();
335 $clm_total_charges = 0;
336 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
337 $clm_total_charges +
= $claim->cptCharges($prockey);
340 if (!$clm_total_charges) {
341 $log .= "*** This claim has no charges!\n";
345 $out .= "CLM" . // Loop 2300 Claim
347 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
350 "*" . $claim->facilityPOS() . "::1" .
359 $out .= "DTP" . // Date of Onset
362 "*" . $claim->onsetDate() .
365 if (strcmp($claim->facilityPOS(),'21') == 0) {
367 $out .= "DTP" . // Date of Hospitalization
370 "*" . $claim->onsetDate() .
374 $patientpaid = $claim->patientPaidAmount();
375 if ($patientpaid != 0) {
377 $out .= "AMT" . // Patient paid amount. Page 220.
383 if ($claim->priorAuth()) {
385 $out .= "REF" . // Prior Authorization Number
387 "*" . $claim->priorAuth() .
391 if ($claim->cliaCode()) {
392 // Required by Medicare when in-house labs are done.
394 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
396 "*" . $claim->cliaCode() .
400 $da = $claim->diagArray();
402 $out .= "HI"; // Health Diagnosis Codes
403 $diag_type_code = 'BK';
404 foreach ($da as $diag) {
405 $out .= "*$diag_type_code:" . $diag;
406 $diag_type_code = 'BF';
410 if ($claim->referrerLastName()) {
411 // Medicare requires referring provider's name and UPIN.
413 $out .= "NM1" . // Loop 2310A Referring Provider
416 "*" . $claim->referrerLastName() .
417 "*" . $claim->referrerFirstName() .
418 "*" . $claim->referrerMiddleName() .
421 if ($claim->referrerNPI()) { $out .=
423 "*" . $claim->referrerNPI();
426 "*" . $claim->referrerSSN();
431 $out .= "REF" . // Referring Provider Secondary Identification
433 "*" . $claim->referrerUPIN() .
438 $out .= "NM1" . // Loop 2310B Rendering Provider
441 "*" . $claim->providerLastName() .
442 "*" . $claim->providerFirstName() .
443 "*" . $claim->providerMiddleName() .
446 if ($claim->providerNPI()) { $out .=
448 "*" . $claim->providerNPI();
451 "*" . $claim->providerSSN();
452 $log .= "*** Rendering provider has no NPI.\n";
457 $out .= "PRV" . // Rendering Provider Information
464 $out .= "NM1" . // Loop 2310B Service Location
467 "*" . $claim->facilityName() .
472 if ($claim->facilityNPI()) { $out .=
473 "*XX*" . $claim->facilityNPI();
475 "*24*" . $claim->facilityETIN();
476 $log .= "*** Service location has no NPI.\n";
482 "*" . $claim->facilityStreet() .
487 "*" . $claim->facilityCity() .
488 "*" . $claim->facilityState() .
489 "*" . $claim->facilityZip() .
492 $prev_pt_resp = $clm_total_charges; // for computation below
494 // Loops 2320 and 2330*, other subscriber/payer information.
496 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
499 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
500 "*" . $claim->payerSequence($ins) .
501 "*" . $claim->insuredRelationship($ins) .
502 "*" . $claim->groupNumber($ins) .
503 "*" . $claim->groupName($ins) .
508 "*" . $claim->claimType($ins) .
511 // Things that apply only to previous payers, not future payers.
513 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
515 // Generate claim-level adjustments.
516 $aarr = $claim->payerAdjustments($ins);
517 foreach ($aarr as $a) {
519 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
526 $payerpaid = $claim->payerTotals($ins);
528 $out .= "AMT" . // Previous payer's paid amount. Page 332.
530 "*" . $payerpaid[1] .
533 // Patient responsibility amount as of this previous payer.
534 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
535 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
538 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
540 "*" . sprintf('%.2f', $prev_pt_resp) .
543 } // End of things that apply only to previous payers.
546 $out .= "DMG" . // Other subscriber demographic information. Page 342.
548 "*" . $claim->insuredDOB($ins) .
549 "*" . $claim->insuredSex($ins) .
553 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
563 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
566 "*" . $claim->insuredLastName($ins) .
567 "*" . $claim->insuredFirstName($ins) .
568 "*" . $claim->insuredMiddleName($ins) .
572 "*" . $claim->policyNumber($ins) .
576 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
579 "*" . $claim->payerName($ins) .
585 "*" . $claim->payerID($ins) .
588 // if (!$claim->payerID($ins)) {
589 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
592 } // End loops 2320/2330*.
596 // Procedure loop starts here.
598 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
602 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
607 $out .= "SV1" . // Professional Service. Page 400.
608 "*HC:" . $claim->cptKey($prockey) .
609 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
611 "*" . $claim->cptUnits($prockey) .
614 "*" . $claim->diagIndex($prockey) .
617 if (!$claim->cptCharges($prockey)) {
618 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
621 if (!$claim->diagIndex($prockey)) {
622 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
626 $out .= "DTP" . // Date of Service. Page 435.
629 "*" . $claim->serviceDate() .
632 // Loop 2410, Drug Information. Medicaid insurers seem to want this
635 $ndc = $claim->cptNDCID($prockey);
638 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
644 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
645 $log .= "*** NDC code '$ndc' has invalid format!\n";
649 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
652 "*0" . // dummy price, required by HIPAA
653 "*" . $claim->cptNDCQuantity($prockey) .
654 "*" . $claim->cptNDCUOM($prockey) .
658 // Loop 2430, adjudication by previous payers.
660 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
661 if ($claim->payerSequence($ins) > $claim->payerSequence())
662 continue; // payer is future, not previous
664 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
665 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
667 if ($payerpaid[1] == 0 && !count($aarr)) {
668 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
669 "' has no payments or adjustments from previous payer!\n";
674 $out .= "SVD" . // Service line adjudication. Page 554.
675 "*" . $claim->payerID($ins) .
676 "*" . $payerpaid[1] .
677 "*HC:" . $claim->cptKey($prockey) .
679 "*" . $claim->cptUnits($prockey) .
682 $tmpdate = $payerpaid[0];
683 foreach ($aarr as $a) {
685 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
690 if (!$tmpdate) $tmpdate = $a[0];
695 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
702 } // end this procedure
705 $out .= "SE" . // SE Trailer
710 $out .= "GE" . // GE Trailer
715 $out .= "IEA" . // IEA Trailer