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() .
162 "*" . $claim->providerNumberType() .
163 "*" . $claim->providerNumber() .
167 $out .= "NM1" . // Loop 2010AB Pay-To Provider
170 "*" . $claim->billingFacilityName() .
175 if ($claim->billingFacilityNPI())
176 $out .= "*XX*" . $claim->billingFacilityNPI();
178 $out .= "*24*" . $claim->billingFacilityETIN();
183 "*" . $claim->billingFacilityStreet() .
188 "*" . $claim->billingFacilityCity() .
189 "*" . $claim->billingFacilityState() .
190 "*" . $claim->billingFacilityZip() .
193 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
197 "*" . $claim->billingFacilityETIN() .
204 $out .= "HL" . // Loop 2000B Subscriber HL Loop
206 "*$HLBillingPayToProvider" .
211 $HLSubscriber = $HLcount++
;
213 if (!$claim->payerSequence()) {
214 $log .= "*** Error: Insurance information is missing!\n";
217 $out .= "SBR" . // Subscriber Information
218 "*" . $claim->payerSequence() .
219 "*" . $claim->insuredRelationship() .
220 "*" . $claim->groupNumber() .
221 "*" . $claim->groupName() .
222 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
226 "*" . $claim->claimType() . // Zirmed replaces this
230 $out .= "NM1" . // Loop 2010BA Subscriber
233 "*" . $claim->insuredLastName() .
234 "*" . $claim->insuredFirstName() .
235 "*" . $claim->insuredMiddleName() .
239 "*" . $claim->policyNumber() .
244 "*" . $claim->insuredStreet() .
249 "*" . $claim->insuredCity() .
250 "*" . $claim->insuredState() .
251 "*" . $claim->insuredZip() .
257 "*" . $claim->insuredDOB() .
258 "*" . $claim->insuredSex() .
262 $out .= "NM1" . // Loop 2010BB Payer
265 "*" . $claim->payerName() .
271 "*" . $claim->payerID() . // Zirmed ignores this if using Payer Name Matching.
274 // if (!$claim->payerID()) {
275 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
280 "*" . $claim->payerStreet() .
285 "*" . $claim->payerCity() .
286 "*" . $claim->payerState() .
287 "*" . $claim->payerZip() .
290 if (! $claim->isSelfOfInsured()) {
292 $out .= "HL" . // Loop 2000C Patient Information
303 "*" . $claim->insuredRelationship() .
307 $out .= "NM1" . // Loop 2010CA Patient
310 "*" . $claim->patientLastName() .
311 "*" . $claim->patientFirstName() .
312 "*" . $claim->patientMiddleName() .
317 "*" . $claim->patientStreet() .
322 "*" . $claim->patientCity() .
323 "*" . $claim->patientState() .
324 "*" . $claim->patientZip() .
330 "*" . $claim->patientDOB() .
331 "*" . $claim->patientSex() .
333 } // end of patient different from insured
335 $proccount = $claim->procCount();
337 $clm_total_charges = 0;
338 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
339 $clm_total_charges +
= $claim->cptCharges($prockey);
342 if (!$clm_total_charges) {
343 $log .= "*** This claim has no charges!\n";
347 $out .= "CLM" . // Loop 2300 Claim
349 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
352 "*" . $claim->facilityPOS() . "::1" .
361 $out .= "DTP" . // Date of Onset
364 "*" . $claim->onsetDate() .
367 if (strcmp($claim->facilityPOS(),'21') == 0) {
369 $out .= "DTP" . // Date of Hospitalization
372 "*" . $claim->onsetDate() .
376 $patientpaid = $claim->patientPaidAmount();
377 if ($patientpaid != 0) {
379 $out .= "AMT" . // Patient paid amount. Page 220.
385 if ($claim->priorAuth()) {
387 $out .= "REF" . // Prior Authorization Number
389 "*" . $claim->priorAuth() .
393 if ($claim->cliaCode()) {
394 // Required by Medicare when in-house labs are done.
396 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
398 "*" . $claim->cliaCode() .
402 $da = $claim->diagArray();
404 $out .= "HI"; // Health Diagnosis Codes
405 $diag_type_code = 'BK';
406 foreach ($da as $diag) {
407 $out .= "*$diag_type_code:" . $diag;
408 $diag_type_code = 'BF';
412 if ($claim->referrerLastName()) {
413 // Medicare requires referring provider's name and UPIN.
415 $out .= "NM1" . // Loop 2310A Referring Provider
418 "*" . $claim->referrerLastName() .
419 "*" . $claim->referrerFirstName() .
420 "*" . $claim->referrerMiddleName() .
423 if ($claim->referrerNPI()) { $out .=
425 "*" . $claim->referrerNPI();
428 "*" . $claim->referrerSSN();
433 $out .= "REF" . // Referring Provider Secondary Identification
435 "*" . $claim->referrerUPIN() .
440 $out .= "NM1" . // Loop 2310B Rendering Provider
443 "*" . $claim->providerLastName() .
444 "*" . $claim->providerFirstName() .
445 "*" . $claim->providerMiddleName() .
448 if ($claim->providerNPI()) { $out .=
450 "*" . $claim->providerNPI();
453 "*" . $claim->providerSSN();
454 $log .= "*** Rendering provider has no NPI.\n";
459 $out .= "PRV" . // Rendering Provider Information
465 // REF*1C is required here for the Medicare provider number if NPI was
466 // specified in NM109. Not sure if other payers require anything here.
467 if ($claim->providerNumber()) {
470 "*" . $claim->providerNumberType() .
471 "*" . $claim->providerNumber() .
476 $out .= "NM1" . // Loop 2310D Service Location
479 "*" . $claim->facilityName() .
484 if ($claim->facilityNPI()) { $out .=
485 "*XX*" . $claim->facilityNPI();
487 "*24*" . $claim->facilityETIN();
488 $log .= "*** Service location has no NPI.\n";
494 "*" . $claim->facilityStreet() .
499 "*" . $claim->facilityCity() .
500 "*" . $claim->facilityState() .
501 "*" . $claim->facilityZip() .
504 $prev_pt_resp = $clm_total_charges; // for computation below
506 // Loops 2320 and 2330*, other subscriber/payer information.
508 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
511 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
512 "*" . $claim->payerSequence($ins) .
513 "*" . $claim->insuredRelationship($ins) .
514 "*" . $claim->groupNumber($ins) .
515 "*" . $claim->groupName($ins) .
520 "*" . $claim->claimType($ins) .
523 // Things that apply only to previous payers, not future payers.
525 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
527 // Generate claim-level adjustments.
528 $aarr = $claim->payerAdjustments($ins);
529 foreach ($aarr as $a) {
531 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
538 $payerpaid = $claim->payerTotals($ins);
540 $out .= "AMT" . // Previous payer's paid amount. Page 332.
542 "*" . $payerpaid[1] .
545 // Patient responsibility amount as of this previous payer.
546 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
547 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
550 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
552 "*" . sprintf('%.2f', $prev_pt_resp) .
555 } // End of things that apply only to previous payers.
558 $out .= "DMG" . // Other subscriber demographic information. Page 342.
560 "*" . $claim->insuredDOB($ins) .
561 "*" . $claim->insuredSex($ins) .
565 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
575 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
578 "*" . $claim->insuredLastName($ins) .
579 "*" . $claim->insuredFirstName($ins) .
580 "*" . $claim->insuredMiddleName($ins) .
584 "*" . $claim->policyNumber($ins) .
588 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
591 "*" . $claim->payerName($ins) .
597 "*" . $claim->payerID($ins) .
600 // if (!$claim->payerID($ins)) {
601 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
604 } // End loops 2320/2330*.
608 // Procedure loop starts here.
610 for ($prockey = 0; $prockey < $proccount; ++
$prockey) {
614 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
619 $out .= "SV1" . // Professional Service. Page 400.
620 "*HC:" . $claim->cptKey($prockey) .
621 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
623 "*" . $claim->cptUnits($prockey) .
627 $dia = $claim->diagIndexArray($prockey);
629 foreach ($dia as $dindex) {
630 $out .= $separator . $dindex;
635 if (!$claim->cptCharges($prockey)) {
636 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
640 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
644 $out .= "DTP" . // Date of Service. Page 435.
647 "*" . $claim->serviceDate() .
650 // Loop 2410, Drug Information. Medicaid insurers seem to want this
653 $ndc = $claim->cptNDCID($prockey);
656 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
662 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
663 $log .= "*** NDC code '$ndc' has invalid format!\n";
667 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
668 if (!$tmpunits) $tmpunits = 1;
669 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
672 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
673 "*" . $claim->cptNDCQuantity($prockey) .
674 "*" . $claim->cptNDCUOM($prockey) .
678 // Loop 2430, adjudication by previous payers.
680 for ($ins = 1; $ins < $claim->payerCount(); ++
$ins) {
681 if ($claim->payerSequence($ins) > $claim->payerSequence())
682 continue; // payer is future, not previous
684 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
685 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
687 if ($payerpaid[1] == 0 && !count($aarr)) {
688 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
689 "' has no payments or adjustments from previous payer!\n";
694 $out .= "SVD" . // Service line adjudication. Page 554.
695 "*" . $claim->payerID($ins) .
696 "*" . $payerpaid[1] .
697 "*HC:" . $claim->cptKey($prockey) .
699 "*" . $claim->cptUnits($prockey) .
702 $tmpdate = $payerpaid[0];
703 foreach ($aarr as $a) {
705 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
710 if (!$tmpdate) $tmpdate = $a[0];
715 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
722 } // end this procedure
725 $out .= "SE" . // SE Trailer
730 $out .= "GE" . // GE Trailer
735 $out .= "IEA" . // IEA Trailer