finished up the patch recording mechanism
[openemr.git] / library / gen_x12_837.inc.php
blob10f7a74a8f643fb3d86b463c6323d60e817adf0f
1 <?php
2 // Copyright (C) 2007-2011 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, $encounter_claim=false) {
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 "*" . $claim->x12gsisa05() .
30 "*" . $claim->x12gssenderid() .
31 "*" . $claim->x12gsisa07() .
32 "*" . $claim->x12gsreceiverid() .
33 "*030911" .
34 "*1630" .
35 "*U" .
36 "*00401" .
37 "*000000001" .
38 "*" . $claim->x12gsisa14() .
39 "*" . $claim->x12gsisa15() .
40 "*:" .
41 "~\n";
43 $out .= "GS" .
44 "*HC" .
45 "*" . $claim->x12gsgs02() .
46 "*" . trim($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 ($encounter_claim ? "*RP" : "*CH") .
68 "~\n";
70 ++$edicount;
71 $out .= "REF" .
72 "*87" .
73 "*" . $claim->x12gsversionstring() .
74 "~\n";
76 ++$edicount;
77 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
78 $billingFacilityName=substr($claim->billingFacilityName(),0,35);
79 $out .= "NM1" . // Loop 1000A Submitter
80 "*41" .
81 "*2" .
82 "*" . $billingFacilityName .
83 "*" .
84 "*" .
85 "*" .
86 "*" .
87 "*46";
88 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
89 $out .= "*" . $claim->clearingHouseETIN();
90 } else {
91 $out .= "*" . $claim->billingFacilityETIN();
93 $out .= "~\n";
95 ++$edicount;
96 $out .= "PER" .
97 "*IC" .
98 "*" . $claim->billingContactName() .
99 "*TE" .
100 "*" . $claim->billingContactPhone();
101 if ($claim->x12gsper06()) {
102 $out .= "*ED*" . $claim->x12gsper06();
104 $out .= "~\n";
106 ++$edicount;
107 $out .= "NM1" . // Loop 1000B Receiver
108 "*40" .
109 "*2" .
110 "*" . $claim->clearingHouseName() .
111 "*" .
112 "*" .
113 "*" .
114 "*" .
115 "*46" .
116 "*" . $claim->clearingHouseETIN() .
117 "~\n";
119 $HLcount = 1;
121 ++$edicount;
122 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
123 "*$HLcount" .
124 "*" .
125 "*20" .
126 "*1" .
127 "~\n";
129 $HLBillingPayToProvider = $HLcount++;
131 ++$edicount;
132 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
133 $billingFacilityName=substr($claim->billingFacilityName(),0,35);
134 $out .= "NM1" . // Loop 2010AA Billing Provider
135 "*85" .
136 "*2" .
137 "*" . $billingFacilityName .
138 "*" .
139 "*" .
140 "*" .
141 "*";
142 if ($claim->billingFacilityNPI()) {
143 $out .= "*XX*" . $claim->billingFacilityNPI();
144 } else {
145 $log .= "*** Billing facility has no NPI.\n";
146 $out .= "*24*" . $claim->billingFacilityETIN();
148 $out .= "~\n";
150 ++$edicount;
151 $out .= "N3" .
152 "*" . $claim->billingFacilityStreet() .
153 "~\n";
155 ++$edicount;
156 $out .= "N4" .
157 "*" . $claim->billingFacilityCity() .
158 "*" . $claim->billingFacilityState() .
159 "*" . $claim->billingFacilityZip() .
160 "~\n";
162 // Add a REF*EI*<ein> segment if NPI was specified in the NM1 above.
163 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
164 ++$edicount;
165 $out .= "REF" ;
166 if($claim->federalIdType()){
167 $out .= "*" . $claim->federalIdType();
169 else{
170 $out .= "*EI";//For dealing with the situation before adding selection for TaxId type In facility ie default to EIN.
172 $out .= "*" . $claim->billingFacilityETIN() .
173 "~\n";
176 if ($claim->providerNumberType() && $claim->providerNumber()) {
177 ++$edicount;
178 $out .= "REF" .
179 "*" . $claim->providerNumberType() .
180 "*" . $claim->providerNumber() .
181 "~\n";
183 else if ($claim->providerNumber()) {
184 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
187 ++$edicount;
188 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
189 $billingFacilityName=substr($claim->billingFacilityName(),0,35);
190 $out .= "NM1" . // Loop 2010AB Pay-To Provider
191 "*87" .
192 "*2" .
193 "*" . $billingFacilityName .
194 "*" .
195 "*" .
196 "*" .
197 "*";
198 if ($claim->billingFacilityNPI())
199 $out .= "*XX*" . $claim->billingFacilityNPI();
200 else
201 $out .= "*24*" . $claim->billingFacilityETIN();
202 $out .= "~\n";
204 ++$edicount;
205 $out .= "N3" .
206 "*" . $claim->billingFacilityStreet() .
207 "~\n";
209 ++$edicount;
210 $out .= "N4" .
211 "*" . $claim->billingFacilityCity() .
212 "*" . $claim->billingFacilityState() .
213 "*" . $claim->billingFacilityZip() .
214 "~\n";
216 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
217 ++$edicount;
218 $out .= "REF" .
219 "*EI" .
220 "*" . $claim->billingFacilityETIN() .
221 "~\n";
224 $PatientHL = 0;
226 ++$edicount;
227 $out .= "HL" . // Loop 2000B Subscriber HL Loop
228 "*$HLcount" .
229 "*$HLBillingPayToProvider" .
230 "*22" .
231 "*$PatientHL" .
232 "~\n";
234 $HLSubscriber = $HLcount++;
236 if (!$claim->payerSequence()) {
237 $log .= "*** Error: Insurance information is missing!\n";
239 ++$edicount;
240 $out .= "SBR" . // Subscriber Information
241 "*" . $claim->payerSequence() .
242 "*" . $claim->insuredRelationship() .
243 "*" . $claim->groupNumber() .
244 "*" . $claim->groupName() .
245 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
246 "*" .
247 "*" .
248 "*" .
249 "*" . $claim->claimType() . // Zirmed replaces this
250 "~\n";
252 ++$edicount;
253 $out .= "NM1" . // Loop 2010BA Subscriber
254 "*IL" .
255 "*1" .
256 "*" . $claim->insuredLastName() .
257 "*" . $claim->insuredFirstName() .
258 "*" . $claim->insuredMiddleName() .
259 "*" .
260 "*" .
261 "*MI" .
262 "*" . $claim->policyNumber() .
263 "~\n";
265 ++$edicount;
266 $out .= "N3" .
267 "*" . $claim->insuredStreet() .
268 "~\n";
270 ++$edicount;
271 $out .= "N4" .
272 "*" . $claim->insuredCity() .
273 "*" . $claim->insuredState() .
274 "*" . $claim->insuredZip() .
275 "~\n";
277 ++$edicount;
278 $out .= "DMG" .
279 "*D8" .
280 "*" . $claim->insuredDOB() .
281 "*" . $claim->insuredSex() .
282 "~\n";
284 ++$edicount;
285 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
286 $payerName=substr($claim->payerName(),0,35);
287 $out .= "NM1" . // Loop 2010BB Payer
288 "*PR" .
289 "*2" .
290 "*" . $payerName .
291 "*" .
292 "*" .
293 "*" .
294 "*" .
295 "*PI" .
296 // Zirmed ignores this if using payer name matching:
297 "*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
298 "~\n";
300 // if (!$claim->payerID()) {
301 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
302 // }
304 ++$edicount;
305 $out .= "N3" .
306 "*" . $claim->payerStreet() .
307 "~\n";
309 ++$edicount;
310 $out .= "N4" .
311 "*" . $claim->payerCity() .
312 "*" . $claim->payerState() .
313 "*" . $claim->payerZip() .
314 "~\n";
316 if (! $claim->isSelfOfInsured()) {
317 ++$edicount;
318 $out .= "HL" . // Loop 2000C Patient Information
319 "*$HLcount" .
320 "*$HLSubscriber" .
321 "*23" .
322 "*0" .
323 "~\n";
325 $HLcount++;
327 ++$edicount;
328 $out .= "PAT" .
329 "*" . $claim->insuredRelationship() .
330 "~\n";
332 ++$edicount;
333 $out .= "NM1" . // Loop 2010CA Patient
334 "*QC" .
335 "*1" .
336 "*" . $claim->patientLastName() .
337 "*" . $claim->patientFirstName() .
338 "*" . $claim->patientMiddleName() .
339 "~\n";
341 ++$edicount;
342 $out .= "N3" .
343 "*" . $claim->patientStreet() .
344 "~\n";
346 ++$edicount;
347 $out .= "N4" .
348 "*" . $claim->patientCity() .
349 "*" . $claim->patientState() .
350 "*" . $claim->patientZip() .
351 "~\n";
353 ++$edicount;
354 $out .= "DMG" .
355 "*D8" .
356 "*" . $claim->patientDOB() .
357 "*" . $claim->patientSex() .
358 "~\n";
359 } // end of patient different from insured
361 $proccount = $claim->procCount();
363 $clm_total_charges = 0;
364 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
365 $clm_total_charges += $claim->cptCharges($prockey);
368 if (!$clm_total_charges) {
369 $log .= "*** This claim has no charges!\n";
373 ++$edicount;
374 $out .= "CLM" . // Loop 2300 Claim
375 "*$pid-$encounter" .
376 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
377 "*" .
378 "*" .
379 "*" . sprintf('%02d', $claim->facilityPOS()) . "::" . $claim->frequencyTypeCode() . // Changed to correct single digit output
380 "*Y" .
381 "*A" .
382 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
383 "*Y" .
384 "*C" .
385 "~\n";
387 if ($claim->dateInitialTreatment()) {
388 ++$edicount;
389 $out .= "DTP" . // Date of Initial Treatment
390 "*454" .
391 "*D8" .
392 "*" . $claim->dateInitialTreatment() .
393 "~\n";
396 ++$edicount;
397 $out .= "DTP" . // Date of Onset
398 "*431" .
399 "*D8" .
400 "*" . $claim->onsetDate() .
401 "~\n";
403 if (strcmp($claim->facilityPOS(),'21') == 0) {
404 ++$edicount;
405 $out .= "DTP" . // Date of Hospitalization
406 "*435" .
407 "*D8" .
408 "*" . $claim->onsetDate() .
409 "~\n";
412 $patientpaid = $claim->patientPaidAmount();
413 if ($patientpaid != 0) {
414 ++$edicount;
415 $out .= "AMT" . // Patient paid amount. Page 220.
416 "*F5" .
417 "*" . $patientpaid .
418 "~\n";
421 if ($claim->priorAuth()) {
422 ++$edicount;
423 $out .= "REF" . // Prior Authorization Number
424 "*G1" .
425 "*" . $claim->priorAuth() .
426 "~\n";
429 if ($claim->cliaCode() and $claim->claimType() === 'MB') {
430 // Required by Medicare when in-house labs are done.
431 ++$edicount;
432 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
433 "*X4" .
434 "*" . $claim->cliaCode() .
435 "~\n";
438 // Note: This would be the place to implement the NTE segment for loop 2300.
439 if ($claim->additionalNotes()) {
440 // Claim note.
441 ++$edicount;
442 $out .= "NTE" . // comments box 19
443 "*" .
444 "*" . $claim->additionalNotes() .
445 "~\n";
448 // Diagnoses, up to 8 per HI segment.
449 $da = $claim->diagArray();
450 $diag_type_code = 'BK';
451 $tmp = 0;
452 foreach ($da as $diag) {
453 if ($tmp % 8 == 0) {
454 if ($tmp) $out .= "~\n";
455 ++$edicount;
456 $out .= "HI"; // Health Diagnosis Codes
458 $out .= "*$diag_type_code:" . $diag;
459 $diag_type_code = 'BF';
460 ++$tmp;
462 if ($tmp) $out .= "~\n";
464 if ($claim->referrerLastName()) {
465 // Medicare requires referring provider's name and UPIN.
466 ++$edicount;
467 $out .= "NM1" . // Loop 2310A Referring Provider
468 "*DN" .
469 "*1" .
470 "*" . $claim->referrerLastName() .
471 "*" . $claim->referrerFirstName() .
472 "*" . $claim->referrerMiddleName() .
473 "*" .
474 "*";
475 if ($claim->referrerNPI()) { $out .=
476 "*XX" .
477 "*" . $claim->referrerNPI();
478 } else { $out .=
479 "*34" .
480 "*" . $claim->referrerSSN();
482 $out .= "~\n";
484 if ($claim->referrerTaxonomy()) {
485 ++$edicount;
486 $out .= "PRV" .
487 "*RF" . // ReFerring provider
488 "*ZZ" .
489 "*" . $claim->referrerTaxonomy() .
490 "~\n";
493 if ($claim->referrerUPIN()) {
494 ++$edicount;
495 $out .= "REF" . // Referring Provider Secondary Identification
496 "*1G" .
497 "*" . $claim->referrerUPIN() .
498 "~\n";
502 ++$edicount;
503 $out .= "NM1" . // Loop 2310B Rendering Provider
504 "*82" .
505 "*1" .
506 "*" . $claim->providerLastName() .
507 "*" . $claim->providerFirstName() .
508 "*" . $claim->providerMiddleName() .
509 "*" .
510 "*";
511 if ($claim->providerNPI()) { $out .=
512 "*XX" .
513 "*" . $claim->providerNPI();
514 } else { $out .=
515 "*34" .
516 "*" . $claim->providerSSN();
517 $log .= "*** Rendering provider has no NPI.\n";
519 $out .= "~\n";
521 if ($claim->providerTaxonomy()) {
522 ++$edicount;
523 $out .= "PRV" .
524 "*PE" . // PErforming provider
525 "*ZZ" .
526 "*" . $claim->providerTaxonomy() .
527 "~\n";
530 // REF*1C is required here for the Medicare provider number if NPI was
531 // specified in NM109. Not sure if other payers require anything here.
532 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
534 if (trim($claim->x12gsreceiverid()) != '470819582') { // if NOT ECLAIMS EDI
535 if ($claim->providerNumber()) {
536 ++$edicount;
537 $out .= "REF" .
538 "*" . $claim->providerNumberType() .
539 "*" . $claim->providerNumber() .
540 "~\n";
544 // Loop 2310D is omitted in the case of home visits (POS=12).
545 if ($claim->facilityPOS() != 12) {
546 ++$edicount;
547 $out .= "NM1" . // Loop 2310D Service Location
548 "*77" .
549 "*2";
550 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
551 $facilityName=substr($claim->facilityName(),0,35);
552 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
553 "*" . $facilityName;
555 if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
556 "*" .
557 "*" .
558 "*" .
559 "*";
560 if ($claim->facilityNPI()) { $out .=
561 "*XX*" . $claim->facilityNPI();
562 } else { $out .=
563 "*24*" . $claim->facilityETIN();
564 $log .= "*** Service location has no NPI.\n";
567 $out .= "~\n";
568 if ($claim->facilityStreet()) {
569 ++$edicount;
570 $out .= "N3" .
571 "*" . $claim->facilityStreet() .
572 "~\n";
574 if ($claim->facilityState()) {
575 ++$edicount;
576 $out .= "N4" .
577 "*" . $claim->facilityCity() .
578 "*" . $claim->facilityState() .
579 "*" . $claim->facilityZip() .
580 "~\n";
584 // Loop 2310E, Supervising Provider
586 if ($claim->supervisorLastName()) {
587 ++$edicount;
588 $out .= "NM1" .
589 "*DQ" . // Supervising Physician
590 "*1" . // Person
591 "*" . $claim->supervisorLastName() .
592 "*" . $claim->supervisorFirstName() .
593 "*" . $claim->supervisorMiddleName() .
594 "*" . // NM106 not used
595 "*"; // Name Suffix
596 if ($claim->supervisorNPI()) { $out .=
597 "*XX" .
598 "*" . $claim->supervisorNPI();
599 } else { $out .=
600 "*34" .
601 "*" . $claim->supervisorSSN();
603 $out .= "~\n";
605 if ($claim->supervisorNumber()) {
606 ++$edicount;
607 $out .= "REF" .
608 "*" . $claim->supervisorNumberType() .
609 "*" . $claim->supervisorNumber() .
610 "~\n";
614 $prev_pt_resp = $clm_total_charges; // for computation below
616 // Loops 2320 and 2330*, other subscriber/payer information.
618 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
620 $tmp1 = $claim->claimType($ins);
621 $tmp2 = 'C1'; // Here a kludge. See page 321.
622 if ($tmp1 === 'CI') $tmp2 = 'C1';
623 if ($tmp1 === 'AM') $tmp2 = 'AP';
624 if ($tmp1 === 'HM') $tmp2 = 'HM';
625 if ($tmp1 === 'MB') $tmp2 = 'MB';
626 if ($tmp1 === 'MC') $tmp2 = 'MC';
627 if ($tmp1 === '09') $tmp2 = 'PP';
628 ++$edicount;
629 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
630 "*" . $claim->payerSequence($ins) .
631 "*" . $claim->insuredRelationship($ins) .
632 "*" . $claim->groupNumber($ins) .
633 "*" . $claim->groupName($ins) .
634 "*" . $tmp2 .
635 "*" .
636 "*" .
637 "*" .
638 "*" . $claim->claimType($ins) .
639 "~\n";
641 // Things that apply only to previous payers, not future payers.
643 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
645 // Generate claim-level adjustments.
646 $aarr = $claim->payerAdjustments($ins);
647 foreach ($aarr as $a) {
648 ++$edicount;
649 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
650 "*" . $a[1] .
651 "*" . $a[2] .
652 "*" . $a[3] .
653 "~\n";
656 $payerpaid = $claim->payerTotals($ins);
657 ++$edicount;
658 $out .= "AMT" . // Previous payer's paid amount. Page 332.
659 "*D" .
660 "*" . $payerpaid[1] .
661 "~\n";
663 // Patient responsibility amount as of this previous payer.
664 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
665 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
667 ++$edicount;
668 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
669 "*B6" .
670 "*" . sprintf('%.2f', $payerpaid[1] + $prev_pt_resp) .
671 "~\n";
673 ++$edicount;
674 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
675 "*F2" .
676 "*" . sprintf('%.2f', $prev_pt_resp) .
677 "~\n";
679 } // End of things that apply only to previous payers.
681 ++$edicount;
682 $out .= "DMG" . // Other subscriber demographic information. Page 342.
683 "*D8" .
684 "*" . $claim->insuredDOB($ins) .
685 "*" . $claim->insuredSex($ins) .
686 "~\n";
688 ++$edicount;
689 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
690 "*" .
691 "*" .
692 "*Y" .
693 "*B" .
694 "*" .
695 "*Y" .
696 "~\n";
698 ++$edicount;
699 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
700 "*IL" .
701 "*1" .
702 "*" . $claim->insuredLastName($ins) .
703 "*" . $claim->insuredFirstName($ins) .
704 "*" . $claim->insuredMiddleName($ins) .
705 "*" .
706 "*" .
707 "*MI" .
708 "*" . $claim->policyNumber($ins) .
709 "~\n";
711 ++$edicount;
712 $out .= "N3" .
713 "*" . $claim->insuredStreet($ins) .
714 "~\n";
716 ++$edicount;
717 $out .= "N4" .
718 "*" . $claim->insuredCity($ins) .
719 "*" . $claim->insuredState($ins) .
720 "*" . $claim->insuredZip($ins) .
721 "~\n";
723 ++$edicount;
724 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
725 $payerName=substr($claim->payerName($ins),0,35);
726 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
727 "*PR" .
728 "*2" .
729 "*" . $payerName .
730 "*" .
731 "*" .
732 "*" .
733 "*" .
734 "*PI" .
735 "*" . $claim->payerID($ins) .
736 "~\n";
738 // if (!$claim->payerID($ins)) {
739 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
740 // }
742 // Payer address (N3 and N4) are added below so that Gateway EDI can
743 // auto-generate secondary claims. These do NOT appear in my copy of
744 // the spec! -- Rod 2008-06-12
746 if (trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
747 ++$edicount;
748 $out .= "N3" .
749 "*" . $claim->payerStreet($ins) .
750 "~\n";
752 ++$edicount;
753 $out .= "N4" .
754 "*" . $claim->payerCity($ins) .
755 "*" . $claim->payerState($ins) .
756 "*" . $claim->payerZip($ins) .
757 "~\n";
758 } // end Gateway EDI
760 } // End loops 2320/2330*.
762 $loopcount = 0;
764 // Procedure loop starts here.
766 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
767 ++$loopcount;
769 ++$edicount;
770 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
771 "*$loopcount" .
772 "~\n";
774 ++$edicount;
775 $out .= "SV1" . // Professional Service. Page 400.
776 "*HC:" . $claim->cptKey($prockey) .
777 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
778 "*UN" .
779 "*" . $claim->cptUnits($prockey) .
780 "*" .
781 "*" .
782 "*";
783 $dia = $claim->diagIndexArray($prockey);
784 $i = 0;
785 foreach ($dia as $dindex) {
786 if ($i) $out .= ':';
787 $out .= $dindex;
788 if (++$i >= 4) break;
790 $out .= "~\n";
792 if (!$claim->cptCharges($prockey)) {
793 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
796 if (empty($dia)) {
797 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
800 ++$edicount;
801 $out .= "DTP" . // Date of Service. Page 435.
802 "*472" .
803 "*D8" .
804 "*" . $claim->serviceDate() .
805 "~\n";
807 // AMT*AAE segment for Approved Amount from previous payer.
808 // Medicare secondaries seem to require this.
810 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
811 if ($claim->payerSequence($ins) > $claim->payerSequence())
812 continue; // payer is future, not previous
813 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
814 ++$edicount;
815 $out .= "AMT" . // Approved amount per previous payer. Page 485.
816 "*AAE" .
817 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
818 "~\n";
819 break;
822 // Loop 2410, Drug Information. Medicaid insurers seem to want this
823 // with HCPCS codes.
825 $ndc = $claim->cptNDCID($prockey);
826 if ($ndc) {
827 ++$edicount;
828 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
829 "*" . // Per addendum, LIN01 is not used.
830 "*N4" .
831 "*" . $ndc .
832 "~\n";
834 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
835 $log .= "*** NDC code '$ndc' has invalid format!\n";
838 ++$edicount;
839 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
840 if (!$tmpunits) $tmpunits = 1;
841 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
842 "*" .
843 "*" .
844 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
845 "*" . $claim->cptNDCQuantity($prockey) .
846 "*" . $claim->cptNDCUOM($prockey) .
847 "~\n";
850 // Loop 2420A, Rendering Provider (service-specific).
851 // Used if the rendering provider for this service line is different
852 // from that in loop 2310B.
854 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
855 ++$edicount;
856 $out .= "NM1" . // Loop 2310B Rendering Provider
857 "*82" .
858 "*1" .
859 "*" . $claim->providerLastName($prockey) .
860 "*" . $claim->providerFirstName($prockey) .
861 "*" . $claim->providerMiddleName($prockey) .
862 "*" .
863 "*";
864 if ($claim->providerNPI($prockey)) { $out .=
865 "*XX" .
866 "*" . $claim->providerNPI($prockey);
867 } else { $out .=
868 "*34" .
869 "*" . $claim->providerSSN($prockey);
870 $log .= "*** Rendering provider has no NPI.\n";
872 $out .= "~\n";
874 if ($claim->providerTaxonomy($prockey)) {
875 ++$edicount;
876 $out .= "PRV" .
877 "*PE" . // PErforming provider
878 "*ZZ" .
879 "*" . $claim->providerTaxonomy($prockey) .
880 "~\n";
883 // REF*1C is required here for the Medicare provider number if NPI was
884 // specified in NM109. Not sure if other payers require anything here.
885 if ($claim->providerNumber($prockey)) {
886 ++$edicount;
887 $out .= "REF" .
888 "*" . $claim->providerNumberType($prockey) .
889 "*" . $claim->providerNumber($prockey) .
890 "~\n";
894 // Loop 2430, adjudication by previous payers.
896 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
897 if ($claim->payerSequence($ins) > $claim->payerSequence())
898 continue; // payer is future, not previous
900 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
901 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
903 if ($payerpaid[1] == 0 && !count($aarr)) {
904 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
905 "' has no payments or adjustments from previous payer!\n";
906 continue;
909 ++$edicount;
910 $out .= "SVD" . // Service line adjudication. Page 554.
911 "*" . $claim->payerID($ins) .
912 "*" . $payerpaid[1] .
913 "*HC:" . $claim->cptKey($prockey) .
914 "*" .
915 "*" . $claim->cptUnits($prockey) .
916 "~\n";
918 $tmpdate = $payerpaid[0];
919 foreach ($aarr as $a) {
920 ++$edicount;
921 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
922 "*" . $a[1] .
923 "*" . $a[2] .
924 "*" . $a[3] .
925 "~\n";
926 if (!$tmpdate) $tmpdate = $a[0];
927 if ( isset($a[4]) &&
928 $a[4] != null ) {
929 $out .= "CAS02" . // Previous payer's adjustment reason
930 "*" . $a[4] .
931 "~\n";
935 if ($tmpdate) {
936 ++$edicount;
937 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
938 "*573" .
939 "*D8" .
940 "*$tmpdate" .
941 "~\n";
943 } // end loop 2430
944 } // end this procedure
946 ++$edicount;
947 $out .= "SE" . // SE Trailer
948 "*$edicount" .
949 "*0021" .
950 "~\n";
952 $out .= "GE" . // GE Trailer
953 "*1" .
954 "*1" .
955 "~\n";
957 $out .= "IEA" . // IEA Trailer
958 "*1" .
959 "*000000001" .
960 "~\n";
962 $log .= "\n";
963 return $out;