Enhance Photo and Patient ID card handling with a widget that supports thumnails...
[openemr.git] / library / gen_x12_837.inc.php
blobb8aad2527e93587219da82c30630647b2edb9416
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, $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 $out .= "NM1" . // Loop 1000A Submitter
78 "*41" .
79 "*2" .
80 "*" . $claim->billingFacilityName() .
81 "*" .
82 "*" .
83 "*" .
84 "*" .
85 "*46";
86 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
87 $out .= "*" . $claim->clearingHouseETIN();
88 } else {
89 $out .= "*" . $claim->billingFacilityETIN();
91 $out .= "~\n";
93 ++$edicount;
94 $out .= "PER" .
95 "*IC" .
96 "*" . $claim->billingContactName() .
97 "*TE" .
98 "*" . $claim->billingContactPhone();
99 if ($claim->x12gsper06()) {
100 $out .= "*ED*" . $claim->x12gsper06();
102 $out .= "~\n";
104 ++$edicount;
105 $out .= "NM1" . // Loop 1000B Receiver
106 "*40" .
107 "*2" .
108 "*" . $claim->clearingHouseName() .
109 "*" .
110 "*" .
111 "*" .
112 "*" .
113 "*46" .
114 "*" . $claim->clearingHouseETIN() .
115 "~\n";
117 $HLcount = 1;
119 ++$edicount;
120 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
121 "*$HLcount" .
122 "*" .
123 "*20" .
124 "*1" .
125 "~\n";
127 $HLBillingPayToProvider = $HLcount++;
129 ++$edicount;
130 $out .= "NM1" . // Loop 2010AA Billing Provider
131 "*85" .
132 "*2" .
133 "*" . $claim->billingFacilityName() .
134 "*" .
135 "*" .
136 "*" .
137 "*";
138 if ($claim->billingFacilityNPI()) {
139 $out .= "*XX*" . $claim->billingFacilityNPI();
140 } else {
141 $log .= "*** Billing facility has no NPI.\n";
142 $out .= "*24*" . $claim->billingFacilityETIN();
144 $out .= "~\n";
146 ++$edicount;
147 $out .= "N3" .
148 "*" . $claim->billingFacilityStreet() .
149 "~\n";
151 ++$edicount;
152 $out .= "N4" .
153 "*" . $claim->billingFacilityCity() .
154 "*" . $claim->billingFacilityState() .
155 "*" . $claim->billingFacilityZip() .
156 "~\n";
158 // Add a REF*EI*<ein> segment if NPI was specified in the NM1 above.
159 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
160 ++$edicount;
161 $out .= "REF" ;
162 if($claim->federalIdType()){
163 $out .= "*" . $claim->federalIdType();
165 else{
166 $out .= "*EI";//For dealing with the situation before adding selection for TaxId type In facility ie default to EIN.
168 $out .= "*" . $claim->billingFacilityETIN() .
169 "~\n";
172 if ($claim->providerNumberType() && $claim->providerNumber()) {
173 ++$edicount;
174 $out .= "REF" .
175 "*" . $claim->providerNumberType() .
176 "*" . $claim->providerNumber() .
177 "~\n";
179 else if ($claim->providerNumber()) {
180 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
183 ++$edicount;
184 $out .= "NM1" . // Loop 2010AB Pay-To Provider
185 "*87" .
186 "*2" .
187 "*" . $claim->billingFacilityName() .
188 "*" .
189 "*" .
190 "*" .
191 "*";
192 if ($claim->billingFacilityNPI())
193 $out .= "*XX*" . $claim->billingFacilityNPI();
194 else
195 $out .= "*24*" . $claim->billingFacilityETIN();
196 $out .= "~\n";
198 ++$edicount;
199 $out .= "N3" .
200 "*" . $claim->billingFacilityStreet() .
201 "~\n";
203 ++$edicount;
204 $out .= "N4" .
205 "*" . $claim->billingFacilityCity() .
206 "*" . $claim->billingFacilityState() .
207 "*" . $claim->billingFacilityZip() .
208 "~\n";
210 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
211 ++$edicount;
212 $out .= "REF" .
213 "*EI" .
214 "*" . $claim->billingFacilityETIN() .
215 "~\n";
218 $PatientHL = 0;
220 ++$edicount;
221 $out .= "HL" . // Loop 2000B Subscriber HL Loop
222 "*$HLcount" .
223 "*$HLBillingPayToProvider" .
224 "*22" .
225 "*$PatientHL" .
226 "~\n";
228 $HLSubscriber = $HLcount++;
230 if (!$claim->payerSequence()) {
231 $log .= "*** Error: Insurance information is missing!\n";
233 ++$edicount;
234 $out .= "SBR" . // Subscriber Information
235 "*" . $claim->payerSequence() .
236 "*" . $claim->insuredRelationship() .
237 "*" . $claim->groupNumber() .
238 "*" . $claim->groupName() .
239 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
240 "*" .
241 "*" .
242 "*" .
243 "*" . $claim->claimType() . // Zirmed replaces this
244 "~\n";
246 ++$edicount;
247 $out .= "NM1" . // Loop 2010BA Subscriber
248 "*IL" .
249 "*1" .
250 "*" . $claim->insuredLastName() .
251 "*" . $claim->insuredFirstName() .
252 "*" . $claim->insuredMiddleName() .
253 "*" .
254 "*" .
255 "*MI" .
256 "*" . $claim->policyNumber() .
257 "~\n";
259 ++$edicount;
260 $out .= "N3" .
261 "*" . $claim->insuredStreet() .
262 "~\n";
264 ++$edicount;
265 $out .= "N4" .
266 "*" . $claim->insuredCity() .
267 "*" . $claim->insuredState() .
268 "*" . $claim->insuredZip() .
269 "~\n";
271 ++$edicount;
272 $out .= "DMG" .
273 "*D8" .
274 "*" . $claim->insuredDOB() .
275 "*" . $claim->insuredSex() .
276 "~\n";
278 ++$edicount;
279 $out .= "NM1" . // Loop 2010BB Payer
280 "*PR" .
281 "*2" .
282 "*" . $claim->payerName() .
283 "*" .
284 "*" .
285 "*" .
286 "*" .
287 "*PI" .
288 // Zirmed ignores this if using payer name matching:
289 "*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
290 "~\n";
292 // if (!$claim->payerID()) {
293 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
294 // }
296 ++$edicount;
297 $out .= "N3" .
298 "*" . $claim->payerStreet() .
299 "~\n";
301 ++$edicount;
302 $out .= "N4" .
303 "*" . $claim->payerCity() .
304 "*" . $claim->payerState() .
305 "*" . $claim->payerZip() .
306 "~\n";
308 if (! $claim->isSelfOfInsured()) {
309 ++$edicount;
310 $out .= "HL" . // Loop 2000C Patient Information
311 "*$HLcount" .
312 "*$HLSubscriber" .
313 "*23" .
314 "*0" .
315 "~\n";
317 $HLcount++;
319 ++$edicount;
320 $out .= "PAT" .
321 "*" . $claim->insuredRelationship() .
322 "~\n";
324 ++$edicount;
325 $out .= "NM1" . // Loop 2010CA Patient
326 "*QC" .
327 "*1" .
328 "*" . $claim->patientLastName() .
329 "*" . $claim->patientFirstName() .
330 "*" . $claim->patientMiddleName() .
331 "~\n";
333 ++$edicount;
334 $out .= "N3" .
335 "*" . $claim->patientStreet() .
336 "~\n";
338 ++$edicount;
339 $out .= "N4" .
340 "*" . $claim->patientCity() .
341 "*" . $claim->patientState() .
342 "*" . $claim->patientZip() .
343 "~\n";
345 ++$edicount;
346 $out .= "DMG" .
347 "*D8" .
348 "*" . $claim->patientDOB() .
349 "*" . $claim->patientSex() .
350 "~\n";
351 } // end of patient different from insured
353 $proccount = $claim->procCount();
355 $clm_total_charges = 0;
356 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
357 $clm_total_charges += $claim->cptCharges($prockey);
360 if (!$clm_total_charges) {
361 $log .= "*** This claim has no charges!\n";
364 ++$edicount;
365 $out .= "CLM" . // Loop 2300 Claim
366 "*$pid-$encounter" .
367 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
368 "*" .
369 "*" .
370 "*" . $claim->facilityPOS() . "::" . $claim->frequencyTypeCode() .
371 "*Y" .
372 "*A" .
373 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
374 "*Y" .
375 "*C" .
376 "~\n";
378 ++$edicount;
379 $out .= "DTP" . // Date of Onset
380 "*431" .
381 "*D8" .
382 "*" . $claim->onsetDate() .
383 "~\n";
385 if (strcmp($claim->facilityPOS(),'21') == 0) {
386 ++$edicount;
387 $out .= "DTP" . // Date of Hospitalization
388 "*435" .
389 "*D8" .
390 "*" . $claim->onsetDate() .
391 "~\n";
394 $patientpaid = $claim->patientPaidAmount();
395 if ($patientpaid != 0) {
396 ++$edicount;
397 $out .= "AMT" . // Patient paid amount. Page 220.
398 "*F5" .
399 "*" . $patientpaid .
400 "~\n";
403 if ($claim->priorAuth()) {
404 ++$edicount;
405 $out .= "REF" . // Prior Authorization Number
406 "*G1" .
407 "*" . $claim->priorAuth() .
408 "~\n";
411 if ($claim->cliaCode() and $claim->claimType() === 'MB') {
412 // Required by Medicare when in-house labs are done.
413 ++$edicount;
414 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
415 "*X4" .
416 "*" . $claim->cliaCode() .
417 "~\n";
420 // Note: This would be the place to implement the NTE segment for loop 2300.
422 // Diagnoses, up to 8 per HI segment.
423 $da = $claim->diagArray();
424 $diag_type_code = 'BK';
425 $tmp = 0;
426 foreach ($da as $diag) {
427 if ($tmp % 8 == 0) {
428 if ($tmp) $out .= "~\n";
429 ++$edicount;
430 $out .= "HI"; // Health Diagnosis Codes
432 $out .= "*$diag_type_code:" . $diag;
433 $diag_type_code = 'BF';
434 ++$tmp;
436 if ($tmp) $out .= "~\n";
438 if ($claim->referrerLastName()) {
439 // Medicare requires referring provider's name and UPIN.
440 ++$edicount;
441 $out .= "NM1" . // Loop 2310A Referring Provider
442 "*DN" .
443 "*1" .
444 "*" . $claim->referrerLastName() .
445 "*" . $claim->referrerFirstName() .
446 "*" . $claim->referrerMiddleName() .
447 "*" .
448 "*";
449 if ($claim->referrerNPI()) { $out .=
450 "*XX" .
451 "*" . $claim->referrerNPI();
452 } else { $out .=
453 "*34" .
454 "*" . $claim->referrerSSN();
456 $out .= "~\n";
458 if ($claim->referrerTaxonomy()) {
459 ++$edicount;
460 $out .= "PRV" .
461 "*RF" . // ReFerring provider
462 "*ZZ" .
463 "*" . $claim->referrerTaxonomy() .
464 "~\n";
467 if ($claim->referrerUPIN()) {
468 ++$edicount;
469 $out .= "REF" . // Referring Provider Secondary Identification
470 "*1G" .
471 "*" . $claim->referrerUPIN() .
472 "~\n";
476 ++$edicount;
477 $out .= "NM1" . // Loop 2310B Rendering Provider
478 "*82" .
479 "*1" .
480 "*" . $claim->providerLastName() .
481 "*" . $claim->providerFirstName() .
482 "*" . $claim->providerMiddleName() .
483 "*" .
484 "*";
485 if ($claim->providerNPI()) { $out .=
486 "*XX" .
487 "*" . $claim->providerNPI();
488 } else { $out .=
489 "*34" .
490 "*" . $claim->providerSSN();
491 $log .= "*** Rendering provider has no NPI.\n";
493 $out .= "~\n";
495 if ($claim->providerTaxonomy()) {
496 ++$edicount;
497 $out .= "PRV" .
498 "*PE" . // PErforming provider
499 "*ZZ" .
500 "*" . $claim->providerTaxonomy() .
501 "~\n";
504 // REF*1C is required here for the Medicare provider number if NPI was
505 // specified in NM109. Not sure if other payers require anything here.
506 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
508 if (trim($claim->x12gsreceiverid()) != '470819582') { // if NOT ECLAIMS EDI
509 if ($claim->providerNumber()) {
510 ++$edicount;
511 $out .= "REF" .
512 "*" . $claim->providerNumberType() .
513 "*" . $claim->providerNumber() .
514 "~\n";
518 // Loop 2310D is omitted in the case of home visits (POS=12).
519 if ($claim->facilityPOS() != 12) {
520 ++$edicount;
521 $out .= "NM1" . // Loop 2310D Service Location
522 "*77" .
523 "*2";
524 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
525 "*" . $claim->facilityName();
527 if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
528 "*" .
529 "*" .
530 "*" .
531 "*";
532 if ($claim->facilityNPI()) { $out .=
533 "*XX*" . $claim->facilityNPI();
534 } else { $out .=
535 "*24*" . $claim->facilityETIN();
536 $log .= "*** Service location has no NPI.\n";
539 $out .= "~\n";
540 if ($claim->facilityStreet()) {
541 ++$edicount;
542 $out .= "N3" .
543 "*" . $claim->facilityStreet() .
544 "~\n";
546 if ($claim->facilityState()) {
547 ++$edicount;
548 $out .= "N4" .
549 "*" . $claim->facilityCity() .
550 "*" . $claim->facilityState() .
551 "*" . $claim->facilityZip() .
552 "~\n";
556 // Loop 2310E, Supervising Provider
558 if ($claim->supervisorLastName()) {
559 ++$edicount;
560 $out .= "NM1" .
561 "*DQ" . // Supervising Physician
562 "*1" . // Person
563 "*" . $claim->supervisorLastName() .
564 "*" . $claim->supervisorFirstName() .
565 "*" . $claim->supervisorMiddleName() .
566 "*" . // NM106 not used
567 "*"; // Name Suffix
568 if ($claim->supervisorNPI()) { $out .=
569 "*XX" .
570 "*" . $claim->supervisorNPI();
571 } else { $out .=
572 "*34" .
573 "*" . $claim->supervisorSSN();
575 $out .= "~\n";
577 if ($claim->supervisorNumber()) {
578 ++$edicount;
579 $out .= "REF" .
580 "*" . $claim->supervisorNumberType() .
581 "*" . $claim->supervisorNumber() .
582 "~\n";
586 $prev_pt_resp = $clm_total_charges; // for computation below
588 // Loops 2320 and 2330*, other subscriber/payer information.
590 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
592 $tmp1 = $claim->claimType($ins);
593 $tmp2 = 'C1'; // Here a kludge. See page 321.
594 if ($tmp1 === 'CI') $tmp2 = 'C1';
595 if ($tmp1 === 'AM') $tmp2 = 'AP';
596 if ($tmp1 === 'HM') $tmp2 = 'HM';
597 if ($tmp1 === 'MB') $tmp2 = 'MB';
598 if ($tmp1 === 'MC') $tmp2 = 'MC';
599 if ($tmp1 === '09') $tmp2 = 'PP';
600 ++$edicount;
601 $out .= "SBR" . // Loop 2320, Subscriber Information - page 318
602 "*" . $claim->payerSequence($ins) .
603 "*" . $claim->insuredRelationship($ins) .
604 "*" . $claim->groupNumber($ins) .
605 "*" . $claim->groupName($ins) .
606 "*" . $tmp2 .
607 "*" .
608 "*" .
609 "*" .
610 "*" . $claim->claimType($ins) .
611 "~\n";
613 // Things that apply only to previous payers, not future payers.
615 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
617 // Generate claim-level adjustments.
618 $aarr = $claim->payerAdjustments($ins);
619 foreach ($aarr as $a) {
620 ++$edicount;
621 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 323.
622 "*" . $a[1] .
623 "*" . $a[2] .
624 "*" . $a[3] .
625 "~\n";
628 $payerpaid = $claim->payerTotals($ins);
629 ++$edicount;
630 $out .= "AMT" . // Previous payer's paid amount. Page 332.
631 "*D" .
632 "*" . $payerpaid[1] .
633 "~\n";
635 // Patient responsibility amount as of this previous payer.
636 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
637 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
639 ++$edicount;
640 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
641 "*B6" .
642 "*" . sprintf('%.2f', $payerpaid[1] + $prev_pt_resp) .
643 "~\n";
645 ++$edicount;
646 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
647 "*F2" .
648 "*" . sprintf('%.2f', $prev_pt_resp) .
649 "~\n";
651 } // End of things that apply only to previous payers.
653 ++$edicount;
654 $out .= "DMG" . // Other subscriber demographic information. Page 342.
655 "*D8" .
656 "*" . $claim->insuredDOB($ins) .
657 "*" . $claim->insuredSex($ins) .
658 "~\n";
660 ++$edicount;
661 $out .= "OI" . // Other Insurance Coverage Information. Page 344.
662 "*" .
663 "*" .
664 "*Y" .
665 "*B" .
666 "*" .
667 "*Y" .
668 "~\n";
670 ++$edicount;
671 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 350.
672 "*IL" .
673 "*1" .
674 "*" . $claim->insuredLastName($ins) .
675 "*" . $claim->insuredFirstName($ins) .
676 "*" . $claim->insuredMiddleName($ins) .
677 "*" .
678 "*" .
679 "*MI" .
680 "*" . $claim->policyNumber($ins) .
681 "~\n";
683 ++$edicount;
684 $out .= "N3" .
685 "*" . $claim->insuredStreet($ins) .
686 "~\n";
688 ++$edicount;
689 $out .= "N4" .
690 "*" . $claim->insuredCity($ins) .
691 "*" . $claim->insuredState($ins) .
692 "*" . $claim->insuredZip($ins) .
693 "~\n";
695 ++$edicount;
696 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 359.
697 "*PR" .
698 "*2" .
699 "*" . $claim->payerName($ins) .
700 "*" .
701 "*" .
702 "*" .
703 "*" .
704 "*PI" .
705 "*" . $claim->payerID($ins) .
706 "~\n";
708 // if (!$claim->payerID($ins)) {
709 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
710 // }
712 // Payer address (N3 and N4) are added below so that Gateway EDI can
713 // auto-generate secondary claims. These do NOT appear in my copy of
714 // the spec! -- Rod 2008-06-12
716 if (trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
717 ++$edicount;
718 $out .= "N3" .
719 "*" . $claim->payerStreet($ins) .
720 "~\n";
722 ++$edicount;
723 $out .= "N4" .
724 "*" . $claim->payerCity($ins) .
725 "*" . $claim->payerState($ins) .
726 "*" . $claim->payerZip($ins) .
727 "~\n";
728 } // end Gateway EDI
730 } // End loops 2320/2330*.
732 $loopcount = 0;
734 // Procedure loop starts here.
736 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
737 ++$loopcount;
739 ++$edicount;
740 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
741 "*$loopcount" .
742 "~\n";
744 ++$edicount;
745 $out .= "SV1" . // Professional Service. Page 400.
746 "*HC:" . $claim->cptKey($prockey) .
747 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
748 "*UN" .
749 "*" . $claim->cptUnits($prockey) .
750 "*" .
751 "*" .
752 "*";
753 $dia = $claim->diagIndexArray($prockey);
754 $i = 0;
755 foreach ($dia as $dindex) {
756 if ($i) $out .= ':';
757 $out .= $dindex;
758 if (++$i >= 4) break;
760 $out .= "~\n";
762 if (!$claim->cptCharges($prockey)) {
763 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
766 if (empty($dia)) {
767 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
770 ++$edicount;
771 $out .= "DTP" . // Date of Service. Page 435.
772 "*472" .
773 "*D8" .
774 "*" . $claim->serviceDate() .
775 "~\n";
777 // AMT*AAE segment for Approved Amount from previous payer.
778 // Medicare secondaries seem to require this.
780 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
781 if ($claim->payerSequence($ins) > $claim->payerSequence())
782 continue; // payer is future, not previous
783 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
784 ++$edicount;
785 $out .= "AMT" . // Approved amount per previous payer. Page 485.
786 "*AAE" .
787 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
788 "~\n";
789 break;
792 // Loop 2410, Drug Information. Medicaid insurers seem to want this
793 // with HCPCS codes.
795 $ndc = $claim->cptNDCID($prockey);
796 if ($ndc) {
797 ++$edicount;
798 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
799 "*" . // Per addendum, LIN01 is not used.
800 "*N4" .
801 "*" . $ndc .
802 "~\n";
804 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp)) {
805 $log .= "*** NDC code '$ndc' has invalid format!\n";
808 ++$edicount;
809 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
810 if (!$tmpunits) $tmpunits = 1;
811 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
812 "*" .
813 "*" .
814 "*" . sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits) .
815 "*" . $claim->cptNDCQuantity($prockey) .
816 "*" . $claim->cptNDCUOM($prockey) .
817 "~\n";
820 // Loop 2420A, Rendering Provider (service-specific).
821 // Used if the rendering provider for this service line is different
822 // from that in loop 2310B.
824 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
825 ++$edicount;
826 $out .= "NM1" . // Loop 2310B Rendering Provider
827 "*82" .
828 "*1" .
829 "*" . $claim->providerLastName($prockey) .
830 "*" . $claim->providerFirstName($prockey) .
831 "*" . $claim->providerMiddleName($prockey) .
832 "*" .
833 "*";
834 if ($claim->providerNPI($prockey)) { $out .=
835 "*XX" .
836 "*" . $claim->providerNPI($prockey);
837 } else { $out .=
838 "*34" .
839 "*" . $claim->providerSSN($prockey);
840 $log .= "*** Rendering provider has no NPI.\n";
842 $out .= "~\n";
844 if ($claim->providerTaxonomy($prockey)) {
845 ++$edicount;
846 $out .= "PRV" .
847 "*PE" . // PErforming provider
848 "*ZZ" .
849 "*" . $claim->providerTaxonomy($prockey) .
850 "~\n";
853 // REF*1C is required here for the Medicare provider number if NPI was
854 // specified in NM109. Not sure if other payers require anything here.
855 if ($claim->providerNumber($prockey)) {
856 ++$edicount;
857 $out .= "REF" .
858 "*" . $claim->providerNumberType($prockey) .
859 "*" . $claim->providerNumber($prockey) .
860 "~\n";
864 // Loop 2430, adjudication by previous payers.
866 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
867 if ($claim->payerSequence($ins) > $claim->payerSequence())
868 continue; // payer is future, not previous
870 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
871 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
873 if ($payerpaid[1] == 0 && !count($aarr)) {
874 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
875 "' has no payments or adjustments from previous payer!\n";
876 continue;
879 ++$edicount;
880 $out .= "SVD" . // Service line adjudication. Page 554.
881 "*" . $claim->payerID($ins) .
882 "*" . $payerpaid[1] .
883 "*HC:" . $claim->cptKey($prockey) .
884 "*" .
885 "*" . $claim->cptUnits($prockey) .
886 "~\n";
888 $tmpdate = $payerpaid[0];
889 foreach ($aarr as $a) {
890 ++$edicount;
891 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
892 "*" . $a[1] .
893 "*" . $a[2] .
894 "*" . $a[3] .
895 "~\n";
896 if (!$tmpdate) $tmpdate = $a[0];
899 if ($tmpdate) {
900 ++$edicount;
901 $out .= "DTP" . // Previous payer's line adjustment date. Page 566.
902 "*573" .
903 "*D8" .
904 "*$tmpdate" .
905 "~\n";
907 } // end loop 2430
908 } // end this procedure
910 ++$edicount;
911 $out .= "SE" . // SE Trailer
912 "*$edicount" .
913 "*0021" .
914 "~\n";
916 $out .= "GE" . // GE Trailer
917 "*1" .
918 "*1" .
919 "~\n";
921 $out .= "IEA" . // IEA Trailer
922 "*1" .
923 "*000000001" .
924 "~\n";
926 $log .= "\n";
927 return $out;