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->referrerUPIN()) {
439 $out .= "REF" . // Referring Provider Secondary Identification
441 "*" . $claim->referrerUPIN() .
447 $out .= "NM1" . // Loop 2310B Rendering Provider
450 "*" . $claim->providerLastName() .
451 "*" . $claim->providerFirstName() .
452 "*" . $claim->providerMiddleName() .
455 if ($claim->providerNPI()) { $out .=
457 "*" . $claim->providerNPI();
460 "*" . $claim->providerSSN();
461 $log .= "*** Rendering provider has no NPI.\n";
466 $out .= "PRV" . // Rendering Provider Information
472 // REF*1C is required here for the Medicare provider number if NPI was
473 // specified in NM109. Not sure if other payers require anything here.
474 if ($claim->providerNumber()) {
477 "*" . $claim->providerNumberType() .
478 "*" . $claim->providerNumber() .
483 $out .= "NM1" . // Loop 2310D Service Location
486 "*" . $claim->facilityName() .
491 if ($claim->facilityNPI()) { $out .=
492 "*XX*" . $claim->facilityNPI();
494 "*24*" . $claim->facilityETIN();
495 $log .= "*** Service location has no NPI.\n";
501 "*" . $claim->facilityStreet() .
506 "*" . $claim->facilityCity() .
507 "*" . $claim->facilityState() .
508 "*" . $claim->facilityZip() .
511 $prev_pt_resp = $clm_total_charges; // for computation below
513 // Loops 2320 and 2330*, other subscriber/payer information.
515 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
517 $tmp1 = $claim->claimType($ins);
518 $tmp2 = 'C1'; // Here a kludge. See page 321.
519 if ($tmp1 === 'CI') $tmp2 = 'C1';
520 if ($tmp1 === 'AM') $tmp2 = 'AP';
521 if ($tmp1 === 'HM') $tmp2 = 'HM';
522 if ($tmp1 === 'MB') $tmp2 = 'MB';
523 if ($tmp1 === 'MC') $tmp2 = 'MC';
524 if ($tmp1 === '09') $tmp2 = 'PP';
526 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
527 "*" . $claim->payerSequence($ins) .
528 "*" . $claim->insuredRelationship($ins) .
529 "*" . $claim->groupNumber($ins) .
530 "*" . $claim->groupName($ins) .
535 "*" . $claim->claimType($ins) .
538 // Things that apply only to previous payers, not future payers.
540 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
542 // Generate claim-level adjustments.
543 $aarr = $claim->payerAdjustments($ins);
544 foreach ($aarr as $a) {
546 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
553 $payerpaid = $claim->payerTotals($ins);
555 $out .= "AMT" . // Previous payer's paid amount. Page 332.
557 "*" . $payerpaid[1] .
560 // Patient responsibility amount as of this previous payer.
561 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
562 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
565 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
567 "*" . sprintf('%.2f', $prev_pt_resp) .
570 } // End of things that apply only to previous payers.
573 $out .= "DMG" . // Other subscriber demographic information. Page 342.
575 "*" . $claim->insuredDOB($ins) .
576 "*" . $claim->insuredSex($ins) .
580 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
590 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
593 "*" . $claim->insuredLastName($ins) .
594 "*" . $claim->insuredFirstName($ins) .
595 "*" . $claim->insuredMiddleName($ins) .
599 "*" . $claim->policyNumber($ins) .
603 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
606 "*" . $claim->payerName($ins) .
612 "*" . $claim->payerID($ins) .
615 // if (!$claim->payerID($ins)) {
616 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
619 } // End loops 2320/2330*.
623 // Procedure loop starts here.
625 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
629 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
634 $out .= "SV1" . // Professional Service. Page 400.
635 "*HC:" . $claim->cptKey($prockey) .
636 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
638 "*" . $claim->cptUnits($prockey) .
642 $dia = $claim->diagIndexArray($prockey);
644 foreach ($dia as $dindex) {
645 $out .= $separator . $dindex;
650 if (!$claim->cptCharges($prockey)) {
651 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
655 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
659 $out .= "DTP" . // Date of Service. Page 435.
662 "*" . $claim->serviceDate() .
665 // Loop 2410, Drug Information. Medicaid insurers seem to want this
668 $ndc = $claim->cptNDCID($prockey);
671 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
677 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
678 $log .= "*** NDC code '$ndc' has invalid format!\n";
682 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
683 if (!$tmpunits) $tmpunits = 1;
684 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
687 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
688 "*" . $claim->cptNDCQuantity($prockey) .
689 "*" . $claim->cptNDCUOM($prockey) .
693 // Loop 2430, adjudication by previous payers.
695 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
696 if ($claim->payerSequence($ins) > $claim->payerSequence())
697 continue; // payer is future, not previous
699 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
700 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
702 if ($payerpaid[1] == 0 && !count($aarr)) {
703 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
704 "' has no payments or adjustments from previous payer!\n";
709 $out .= "SVD" . // Service line adjudication. Page 554.
710 "*" . $claim->payerID($ins) .
711 "*" . $payerpaid[1] .
712 "*HC:" . $claim->cptKey($prockey) .
714 "*" . $claim->cptUnits($prockey) .
717 $tmpdate = $payerpaid[0];
718 foreach ($aarr as $a) {
720 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
725 if (!$tmpdate) $tmpdate = $a[0];
730 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
737 } // end this procedure
740 $out .= "SE" . // SE Trailer
745 $out .= "GE" . // GE Trailer
750 $out .= "IEA" . // IEA Trailer