made sl data authoritative and fixed some bugs and omissions
[openemr.git] / library / gen_x12_837.inc.php
blobd37044411361600eee13e8a88e9f5ad1a82f5767
1 <?php
2 // Copyright (C) 2007-2009 Rod Roark <rod@sunsetsystems.com>
3 //
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) {
13 $today = time();
14 $out = '';
15 $claim = new Claim($pid, $encounter);
16 $edicount = 0;
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";
24 $out .= "ISA" .
25 "*00" .
26 "* " .
27 "*00" .
28 "* " .
29 "*ZZ" .
30 "*" . $claim->x12gssenderid() .
31 "*ZZ" .
32 "*" . $claim->x12gsreceiverid() .
33 "*030911" .
34 "*1630" .
35 "*U" .
36 "*00401" .
37 "*000000001" .
38 "*0" .
39 "*P" .
40 "*:" .
41 "~\n";
43 $out .= "GS" .
44 "*HC" .
45 "*" . $claim->x12gssenderid() .
46 "*" . $claim->x12gsreceiverid() .
47 "*" . date('Ymd', $today) .
48 "*" . date('Hi', $today) .
49 "*1" .
50 "*X" .
51 "*" . $claim->x12gsversionstring() .
52 "~\n";
54 ++$edicount;
55 $out .= "ST" .
56 "*837" .
57 "*0021" .
58 "~\n";
60 ++$edicount;
61 $out .= "BHT" .
62 "*0019" .
63 "*00" .
64 "*0123" .
65 "*" . date('Ymd', $today) .
66 "*1023" .
67 "*CH" .
68 "~\n";
70 ++$edicount;
71 $out .= "REF" .
72 "*87" .
73 "*" . $claim->x12gsversionstring() .
74 "~\n";
76 ++$edicount;
77 $out .= "NM1" . // Loop 1000A Submitter
78 "*41" .
79 "*2" .
80 "*" . $claim->billingFacilityName() .
81 "*" .
82 "*" .
83 "*" .
84 "*" .
85 "*46" .
86 "*" . $claim->billingFacilityETIN() .
87 "~\n";
89 ++$edicount;
90 $out .= "PER" .
91 "*IC" .
92 "*" . $claim->billingContactName() .
93 "*TE" .
94 "*" . $claim->billingContactPhone() .
95 "~\n";
97 ++$edicount;
98 $out .= "NM1" . // Loop 1000B Receiver
99 "*40" .
100 "*2" .
101 "*" . $claim->clearingHouseName() .
102 "*" .
103 "*" .
104 "*" .
105 "*" .
106 "*46" .
107 "*" . $claim->clearingHouseETIN() .
108 "~\n";
110 $HLcount = 1;
112 ++$edicount;
113 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
114 "*$HLcount" .
115 "*" .
116 "*20" .
117 "*1" .
118 "~\n";
120 $HLBillingPayToProvider = $HLcount++;
122 ++$edicount;
123 $out .= "NM1" . // Loop 2010AA Billing Provider
124 "*85" .
125 "*2" .
126 "*" . $claim->billingFacilityName() .
127 "*" .
128 "*" .
129 "*" .
130 "*";
131 if ($claim->billingFacilityNPI()) {
132 $out .= "*XX*" . $claim->billingFacilityNPI();
133 } else {
134 $log .= "*** Billing facility has no NPI.\n";
135 $out .= "*24*" . $claim->billingFacilityETIN();
137 $out .= "~\n";
139 ++$edicount;
140 $out .= "N3" .
141 "*" . $claim->billingFacilityStreet() .
142 "~\n";
144 ++$edicount;
145 $out .= "N4" .
146 "*" . $claim->billingFacilityCity() .
147 "*" . $claim->billingFacilityState() .
148 "*" . $claim->billingFacilityZip() .
149 "~\n";
151 // Add a REF*EI*<ein> segment if NPI was specified in the NM1 above.
152 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
153 ++$edicount;
154 $out .= "REF" .
155 "*EI" .
156 "*" . $claim->billingFacilityETIN() .
157 "~\n";
160 if ($claim->providerNumberType() && $claim->providerNumber()) {
161 ++$edicount;
162 $out .= "REF" .
163 "*" . $claim->providerNumberType() .
164 "*" . $claim->providerNumber() .
165 "~\n";
167 else if ($claim->providerNumber()) {
168 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
171 ++$edicount;
172 $out .= "NM1" . // Loop 2010AB Pay-To Provider
173 "*87" .
174 "*2" .
175 "*" . $claim->billingFacilityName() .
176 "*" .
177 "*" .
178 "*" .
179 "*";
180 if ($claim->billingFacilityNPI())
181 $out .= "*XX*" . $claim->billingFacilityNPI();
182 else
183 $out .= "*24*" . $claim->billingFacilityETIN();
184 $out .= "~\n";
186 ++$edicount;
187 $out .= "N3" .
188 "*" . $claim->billingFacilityStreet() .
189 "~\n";
191 ++$edicount;
192 $out .= "N4" .
193 "*" . $claim->billingFacilityCity() .
194 "*" . $claim->billingFacilityState() .
195 "*" . $claim->billingFacilityZip() .
196 "~\n";
198 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
199 ++$edicount;
200 $out .= "REF" .
201 "*EI" .
202 "*" . $claim->billingFacilityETIN() .
203 "~\n";
206 $PatientHL = 0;
208 ++$edicount;
209 $out .= "HL" . // Loop 2000B Subscriber HL Loop
210 "*$HLcount" .
211 "*$HLBillingPayToProvider" .
212 "*22" .
213 "*$PatientHL" .
214 "~\n";
216 $HLSubscriber = $HLcount++;
218 if (!$claim->payerSequence()) {
219 $log .= "*** Error: Insurance information is missing!\n";
221 ++$edicount;
222 $out .= "SBR" . // Subscriber Information
223 "*" . $claim->payerSequence() .
224 "*" . $claim->insuredRelationship() .
225 "*" . $claim->groupNumber() .
226 "*" . $claim->groupName() .
227 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
228 "*" .
229 "*" .
230 "*" .
231 "*" . $claim->claimType() . // Zirmed replaces this
232 "~\n";
234 ++$edicount;
235 $out .= "NM1" . // Loop 2010BA Subscriber
236 "*IL" .
237 "*1" .
238 "*" . $claim->insuredLastName() .
239 "*" . $claim->insuredFirstName() .
240 "*" . $claim->insuredMiddleName() .
241 "*" .
242 "*" .
243 "*MI" .
244 "*" . $claim->policyNumber() .
245 "~\n";
247 ++$edicount;
248 $out .= "N3" .
249 "*" . $claim->insuredStreet() .
250 "~\n";
252 ++$edicount;
253 $out .= "N4" .
254 "*" . $claim->insuredCity() .
255 "*" . $claim->insuredState() .
256 "*" . $claim->insuredZip() .
257 "~\n";
259 ++$edicount;
260 $out .= "DMG" .
261 "*D8" .
262 "*" . $claim->insuredDOB() .
263 "*" . $claim->insuredSex() .
264 "~\n";
266 ++$edicount;
267 $out .= "NM1" . // Loop 2010BB Payer
268 "*PR" .
269 "*2" .
270 "*" . $claim->payerName() .
271 "*" .
272 "*" .
273 "*" .
274 "*" .
275 "*PI" .
276 "*" . $claim->payerID() . // Zirmed ignores this if using Payer Name Matching.
277 "~\n";
279 // if (!$claim->payerID()) {
280 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
281 // }
283 ++$edicount;
284 $out .= "N3" .
285 "*" . $claim->payerStreet() .
286 "~\n";
288 ++$edicount;
289 $out .= "N4" .
290 "*" . $claim->payerCity() .
291 "*" . $claim->payerState() .
292 "*" . $claim->payerZip() .
293 "~\n";
295 if (! $claim->isSelfOfInsured()) {
296 ++$edicount;
297 $out .= "HL" . // Loop 2000C Patient Information
298 "*$HLcount" .
299 "*$HLSubscriber" .
300 "*23" .
301 "*0" .
302 "~\n";
304 $HLcount++;
306 ++$edicount;
307 $out .= "PAT" .
308 "*" . $claim->insuredRelationship() .
309 "~\n";
311 ++$edicount;
312 $out .= "NM1" . // Loop 2010CA Patient
313 "*QC" .
314 "*1" .
315 "*" . $claim->patientLastName() .
316 "*" . $claim->patientFirstName() .
317 "*" . $claim->patientMiddleName() .
318 "~\n";
320 ++$edicount;
321 $out .= "N3" .
322 "*" . $claim->patientStreet() .
323 "~\n";
325 ++$edicount;
326 $out .= "N4" .
327 "*" . $claim->patientCity() .
328 "*" . $claim->patientState() .
329 "*" . $claim->patientZip() .
330 "~\n";
332 ++$edicount;
333 $out .= "DMG" .
334 "*D8" .
335 "*" . $claim->patientDOB() .
336 "*" . $claim->patientSex() .
337 "~\n";
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";
351 ++$edicount;
352 $out .= "CLM" . // Loop 2300 Claim
353 "*$pid-$encounter" .
354 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
355 "*" .
356 "*" .
357 "*" . $claim->facilityPOS() . "::" . $claim->frequencyTypeCode() .
358 "*Y" .
359 "*A" .
360 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
361 "*Y" .
362 "*C" .
363 "~\n";
365 ++$edicount;
366 $out .= "DTP" . // Date of Onset
367 "*431" .
368 "*D8" .
369 "*" . $claim->onsetDate() .
370 "~\n";
372 if (strcmp($claim->facilityPOS(),'21') == 0) {
373 ++$edicount;
374 $out .= "DTP" . // Date of Hospitalization
375 "*435" .
376 "*D8" .
377 "*" . $claim->onsetDate() .
378 "~\n";
381 $patientpaid = $claim->patientPaidAmount();
382 if ($patientpaid != 0) {
383 ++$edicount;
384 $out .= "AMT" . // Patient paid amount. Page 220.
385 "*F5" .
386 "*" . $patientpaid .
387 "~\n";
390 if ($claim->priorAuth()) {
391 ++$edicount;
392 $out .= "REF" . // Prior Authorization Number
393 "*G1" .
394 "*" . $claim->priorAuth() .
395 "~\n";
398 if ($claim->cliaCode()) {
399 // Required by Medicare when in-house labs are done.
400 ++$edicount;
401 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
402 "*X4" .
403 "*" . $claim->cliaCode() .
404 "~\n";
407 // Note: This would be the place to implement the NTE segment for loop 2300.
409 $da = $claim->diagArray();
410 ++$edicount;
411 $out .= "HI"; // Health Diagnosis Codes
412 $diag_type_code = 'BK';
413 foreach ($da as $diag) {
414 $out .= "*$diag_type_code:" . $diag;
415 $diag_type_code = 'BF';
417 $out .= "~\n";
419 if ($claim->referrerLastName()) {
420 // Medicare requires referring provider's name and UPIN.
421 ++$edicount;
422 $out .= "NM1" . // Loop 2310A Referring Provider
423 "*DN" .
424 "*1" .
425 "*" . $claim->referrerLastName() .
426 "*" . $claim->referrerFirstName() .
427 "*" . $claim->referrerMiddleName() .
428 "*" .
429 "*";
430 if ($claim->referrerNPI()) { $out .=
431 "*XX" .
432 "*" . $claim->referrerNPI();
433 } else { $out .=
434 "*34" .
435 "*" . $claim->referrerSSN();
437 $out .= "~\n";
439 if ($claim->referrerTaxonomy()) {
440 ++$edicount;
441 $out .= "PRV" .
442 "*RF" . // ReFerring provider
443 "*ZZ" .
444 "*" . $claim->referrerTaxonomy() .
445 "~\n";
448 if ($claim->referrerUPIN()) {
449 ++$edicount;
450 $out .= "REF" . // Referring Provider Secondary Identification
451 "*1G" .
452 "*" . $claim->referrerUPIN() .
453 "~\n";
457 ++$edicount;
458 $out .= "NM1" . // Loop 2310B Rendering Provider
459 "*82" .
460 "*1" .
461 "*" . $claim->providerLastName() .
462 "*" . $claim->providerFirstName() .
463 "*" . $claim->providerMiddleName() .
464 "*" .
465 "*";
466 if ($claim->providerNPI()) { $out .=
467 "*XX" .
468 "*" . $claim->providerNPI();
469 } else { $out .=
470 "*34" .
471 "*" . $claim->providerSSN();
472 $log .= "*** Rendering provider has no NPI.\n";
474 $out .= "~\n";
476 if ($claim->providerTaxonomy()) {
477 ++$edicount;
478 $out .= "PRV" .
479 "*PE" . // PErforming provider
480 "*ZZ" .
481 "*" . $claim->providerTaxonomy() .
482 "~\n";
485 // REF*1C is required here for the Medicare provider number if NPI was
486 // specified in NM109. Not sure if other payers require anything here.
487 if ($claim->providerNumber()) {
488 ++$edicount;
489 $out .= "REF" .
490 "*" . $claim->providerNumberType() .
491 "*" . $claim->providerNumber() .
492 "~\n";
495 // Loop 2310D is omitted in the case of home visits (POS=12).
496 if ($claim->facilityPOS() != 12) {
497 ++$edicount;
498 $out .= "NM1" . // Loop 2310D Service Location
499 "*77" .
500 "*2";
501 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
502 "*" . $claim->facilityName();
504 if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
505 "*" .
506 "*" .
507 "*" .
508 "*";
509 if ($claim->facilityNPI()) { $out .=
510 "*XX*" . $claim->facilityNPI();
511 } else { $out .=
512 "*24*" . $claim->facilityETIN();
513 $log .= "*** Service location has no NPI.\n";
516 $out .= "~\n";
517 if ($claim->facilityStreet()) {
518 ++$edicount;
519 $out .= "N3" .
520 "*" . $claim->facilityStreet() .
521 "~\n";
523 if ($claim->facilityState()) {
524 ++$edicount;
525 $out .= "N4" .
526 "*" . $claim->facilityCity() .
527 "*" . $claim->facilityState() .
528 "*" . $claim->facilityZip() .
529 "~\n";
533 // Loop 2310E, Supervising Provider
535 if ($claim->supervisorLastName()) {
536 ++$edicount;
537 $out .= "NM1" .
538 "*DQ" . // Supervising Physician
539 "*1" . // Person
540 "*" . $claim->supervisorLastName() .
541 "*" . $claim->supervisorFirstName() .
542 "*" . $claim->supervisorMiddleName() .
543 "*" . // NM106 not used
544 "*"; // Name Suffix
545 if ($claim->supervisorNPI()) { $out .=
546 "*XX" .
547 "*" . $claim->supervisorNPI();
548 } else { $out .=
549 "*34" .
550 "*" . $claim->supervisorSSN();
552 $out .= "~\n";
554 if ($claim->supervisorNumber()) {
555 ++$edicount;
556 $out .= "REF" .
557 "*" . $claim->supervisorNumberType() .
558 "*" . $claim->supervisorNumber() .
559 "~\n";
563 $prev_pt_resp = $clm_total_charges; // for computation below
565 // Loops 2320 and 2330*, other subscriber/payer information.
567 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
569 $tmp1 = $claim->claimType($ins);
570 $tmp2 = 'C1'; // Here a kludge. See page 321.
571 if ($tmp1 === 'CI') $tmp2 = 'C1';
572 if ($tmp1 === 'AM') $tmp2 = 'AP';
573 if ($tmp1 === 'HM') $tmp2 = 'HM';
574 if ($tmp1 === 'MB') $tmp2 = 'MB';
575 if ($tmp1 === 'MC') $tmp2 = 'MC';
576 if ($tmp1 === '09') $tmp2 = 'PP';
577 ++$edicount;
578 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
579 "*" . $claim->payerSequence($ins) .
580 "*" . $claim->insuredRelationship($ins) .
581 "*" . $claim->groupNumber($ins) .
582 "*" . $claim->groupName($ins) .
583 "*" . $tmp2 .
584 "*" .
585 "*" .
586 "*" .
587 "*" . $claim->claimType($ins) .
588 "~\n";
590 // Things that apply only to previous payers, not future payers.
592 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
594 // Generate claim-level adjustments.
595 $aarr = $claim->payerAdjustments($ins);
596 foreach ($aarr as $a) {
597 ++$edicount;
598 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
599 "*" . $a[1] .
600 "*" . $a[2] .
601 "*" . $a[3] .
602 "~\n";
605 $payerpaid = $claim->payerTotals($ins);
606 ++$edicount;
607 $out .= "AMT" . // Previous payer's paid amount. Page 332.
608 "*D" .
609 "*" . $payerpaid[1] .
610 "~\n";
612 // Patient responsibility amount as of this previous payer.
613 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
614 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
616 ++$edicount;
617 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
618 "*F2" .
619 "*" . sprintf('%.2f', $prev_pt_resp) .
620 "~\n";
622 } // End of things that apply only to previous payers.
624 ++$edicount;
625 $out .= "DMG" . // Other subscriber demographic information. Page 342.
626 "*D8" .
627 "*" . $claim->insuredDOB($ins) .
628 "*" . $claim->insuredSex($ins) .
629 "~\n";
631 ++$edicount;
632 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
633 "*" .
634 "*" .
635 "*Y" .
636 "*B" .
637 "*" .
638 "*Y" .
639 "~\n";
641 ++$edicount;
642 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
643 "*IL" .
644 "*1" .
645 "*" . $claim->insuredLastName($ins) .
646 "*" . $claim->insuredFirstName($ins) .
647 "*" . $claim->insuredMiddleName($ins) .
648 "*" .
649 "*" .
650 "*MI" .
651 "*" . $claim->policyNumber($ins) .
652 "~\n";
654 ++$edicount;
655 $out .= "N3" .
656 "*" . $claim->insuredStreet($ins) .
657 "~\n";
659 ++$edicount;
660 $out .= "N4" .
661 "*" . $claim->insuredCity($ins) .
662 "*" . $claim->insuredState($ins) .
663 "*" . $claim->insuredZip($ins) .
664 "~\n";
666 ++$edicount;
667 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
668 "*PR" .
669 "*2" .
670 "*" . $claim->payerName($ins) .
671 "*" .
672 "*" .
673 "*" .
674 "*" .
675 "*PI" .
676 "*" . $claim->payerID($ins) .
677 "~\n";
679 // if (!$claim->payerID($ins)) {
680 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
681 // }
683 // Payer address (N3 and N4) are added below so that Gateway EDI can
684 // auto-generate secondary claims. These do NOT appear in my copy of
685 // the spec! -- Rod 2008-06-12
687 if ($claim->x12gsreceiverid() == '431420764') { // if Gateway EDI
688 ++$edicount;
689 $out .= "N3" .
690 "*" . $claim->payerStreet($ins) .
691 "~\n";
693 ++$edicount;
694 $out .= "N4" .
695 "*" . $claim->payerCity($ins) .
696 "*" . $claim->payerState($ins) .
697 "*" . $claim->payerZip($ins) .
698 "~\n";
699 } // end Gateway EDI
701 } // End loops 2320/2330*.
703 $loopcount = 0;
705 // Procedure loop starts here.
707 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
708 ++$loopcount;
710 ++$edicount;
711 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
712 "*$loopcount" .
713 "~\n";
715 ++$edicount;
716 $out .= "SV1" . // Professional Service. Page 400.
717 "*HC:" . $claim->cptKey($prockey) .
718 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
719 "*UN" .
720 "*" . $claim->cptUnits($prockey) .
721 "*" .
722 "*" .
723 "*";
724 $dia = $claim->diagIndexArray($prockey);
725 $separator = '';
726 foreach ($dia as $dindex) {
727 $out .= $separator . $dindex;
728 $separator = ':';
730 $out .= "~\n";
732 if (!$claim->cptCharges($prockey)) {
733 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
736 if (empty($dia)) {
737 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
740 ++$edicount;
741 $out .= "DTP" . // Date of Service. Page 435.
742 "*472" .
743 "*D8" .
744 "*" . $claim->serviceDate() .
745 "~\n";
747 // Loop 2410, Drug Information. Medicaid insurers seem to want this
748 // with HCPCS codes.
750 $ndc = $claim->cptNDCID($prockey);
751 if ($ndc) {
752 ++$edicount;
753 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
754 "*" . // Per addendum, LIN01 is not used.
755 "*N4" .
756 "*" . $ndc .
757 "~\n";
759 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
760 $log .= "*** NDC code '$ndc' has invalid format!\n";
763 ++$edicount;
764 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
765 if (!$tmpunits) $tmpunits = 1;
766 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
767 "*" .
768 "*" .
769 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
770 "*" . $claim->cptNDCQuantity($prockey) .
771 "*" . $claim->cptNDCUOM($prockey) .
772 "~\n";
775 // Loop 2420A, Rendering Provider (service-specific).
776 // Used if the rendering provider for this service line is different
777 // from that in loop 2310B.
779 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
780 ++$edicount;
781 $out .= "NM1" . // Loop 2310B Rendering Provider
782 "*82" .
783 "*1" .
784 "*" . $claim->providerLastName($prockey) .
785 "*" . $claim->providerFirstName($prockey) .
786 "*" . $claim->providerMiddleName($prockey) .
787 "*" .
788 "*";
789 if ($claim->providerNPI($prockey)) { $out .=
790 "*XX" .
791 "*" . $claim->providerNPI($prockey);
792 } else { $out .=
793 "*34" .
794 "*" . $claim->providerSSN($prockey);
795 $log .= "*** Rendering provider has no NPI.\n";
797 $out .= "~\n";
799 if ($claim->providerTaxonomy($prockey)) {
800 ++$edicount;
801 $out .= "PRV" .
802 "*PE" . // PErforming provider
803 "*ZZ" .
804 "*" . $claim->providerTaxonomy($prockey) .
805 "~\n";
808 // REF*1C is required here for the Medicare provider number if NPI was
809 // specified in NM109. Not sure if other payers require anything here.
810 if ($claim->providerNumber($prockey)) {
811 ++$edicount;
812 $out .= "REF" .
813 "*" . $claim->providerNumberType($prockey) .
814 "*" . $claim->providerNumber($prockey) .
815 "~\n";
819 // Loop 2430, adjudication by previous payers.
821 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
822 if ($claim->payerSequence($ins) > $claim->payerSequence())
823 continue; // payer is future, not previous
825 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
826 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
828 if ($payerpaid[1] == 0 && !count($aarr)) {
829 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
830 "' has no payments or adjustments from previous payer!\n";
831 continue;
834 ++$edicount;
835 $out .= "SVD" . // Service line adjudication. Page 554.
836 "*" . $claim->payerID($ins) .
837 "*" . $payerpaid[1] .
838 "*HC:" . $claim->cptKey($prockey) .
839 "*" .
840 "*" . $claim->cptUnits($prockey) .
841 "~\n";
843 $tmpdate = $payerpaid[0];
844 foreach ($aarr as $a) {
845 ++$edicount;
846 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
847 "*" . $a[1] .
848 "*" . $a[2] .
849 "*" . $a[3] .
850 "~\n";
851 if (!$tmpdate) $tmpdate = $a[0];
854 if ($tmpdate) {
855 ++$edicount;
856 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
857 "*573" .
858 "*D8" .
859 "*$tmpdate" .
860 "~\n";
862 } // end loop 2430
863 } // end this procedure
865 ++$edicount;
866 $out .= "SE" . // SE Trailer
867 "*$edicount" .
868 "*0021" .
869 "~\n";
871 $out .= "GE" . // GE Trailer
872 "*1" .
873 "*1" .
874 "~\n";
876 $out .= "IEA" . // IEA Trailer
877 "*1" .
878 "*000000001" .
879 "~\n";
881 $log .= "\n";
882 return $out;