corrected
[openemr.git] / library / gen_x12_837.inc.php
blob7ebc1e1b0ec29daebb56c11659ae40142d134c1d
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");
10 function stripZipCode($zip)
12 return str_replace('-','',$zip);
14 function gen_x12_837($pid, $encounter, &$log, $encounter_claim=false) {
16 $today = time();
17 $out = '';
18 $claim = new Claim($pid, $encounter);
19 $edicount = 0;
21 // This is true for the 5010 standard, false for 4010.
22 // x12gsversionstring() should be "005010X222A1" or "004010X098A1".
23 $CMS_5010 = strpos($claim->x12gsversionstring(), '5010') !== false;
25 $log .= "Generating claim $pid-$encounter for " .
26 $claim->patientFirstName() . ' ' .
27 $claim->patientMiddleName() . ' ' .
28 $claim->patientLastName() . ' on ' .
29 date('Y-m-d H:i', $today) . ".\n";
31 $out .= "ISA" .
32 "*00" .
33 "* " .
34 "*00" .
35 "* " .
36 "*" . $claim->x12gsisa05() .
37 "*" . $claim->x12gssenderid() .
38 "*" . $claim->x12gsisa07() .
39 "*" . $claim->x12gsreceiverid() .
40 "*030911" .
41 "*1630" .
42 "*" . ($CMS_5010 ? "^" : "U" ) .
43 "*" . ($CMS_5010 ? "00501" : "00401") .
44 "*000000001" .
45 "*" . $claim->x12gsisa14() .
46 "*" . $claim->x12gsisa15() .
47 "*:" .
48 "~\n";
50 $out .= "GS" .
51 "*HC" .
52 "*" . $claim->x12gsgs02() .
53 "*" . trim($claim->x12gs03()) .
54 "*" . date('Ymd', $today) .
55 "*" . date('Hi', $today) .
56 "*1" .
57 "*X" .
58 "*" . $claim->x12gsversionstring() .
59 "~\n";
61 ++$edicount;
62 $out .= "ST" .
63 "*837" .
64 "*0021" .
65 // Spec says the following is optional, so should be able to leave it out.
66 ($CMS_5010 ? ("*" . $claim->x12gsversionstring()) : "") .
67 "~\n";
69 ++$edicount;
70 $out .= "BHT" .
71 "*0019" . // 0019 is required here
72 "*00" . // 00 = original transmission
73 "*0123" . // reference identification
74 "*" . date('Ymd', $today) . // transaction creation date
75 "*" . date('Hi', $today) . // transaction creation time
76 ($encounter_claim ? "*RP" : "*CH") . // RP = reporting, CH = chargeable
77 "~\n";
79 if (!$CMS_5010) {
80 // This segment was deleted for 5010.
81 ++$edicount;
82 $out .= "REF" .
83 "*87" .
84 "*" . $claim->x12gsversionstring() .
85 "~\n";
88 ++$edicount;
89 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
90 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
91 $out .= "NM1" . // Loop 1000A Submitter
92 "*41" .
93 "*2" .
94 "*" . $billingFacilityName .
95 "*" .
96 "*" .
97 "*" .
98 "*" .
99 "*46";
100 if (trim($claim->x12gsreceiverid()) == '470819582') { // if ECLAIMS EDI
101 $out .= "*" . $claim->clearingHouseETIN();
102 } else {
103 $out .= "*" . $claim->billingFacilityETIN();
105 $out .= "~\n";
107 ++$edicount;
108 $out .= "PER" .
109 "*IC" .
110 "*" . $claim->billingContactName() .
111 "*TE" .
112 "*" . $claim->billingContactPhone();
113 if (!$CMS_5010 && $claim->x12gsper06()) {
114 $out .= "*ED*" . $claim->x12gsper06();
116 $out .= "~\n";
118 ++$edicount;
119 $out .= "NM1" . // Loop 1000B Receiver
120 "*40" .
121 "*2" .
122 "*" . $claim->clearingHouseName() .
123 "*" .
124 "*" .
125 "*" .
126 "*" .
127 "*46" .
128 "*" . $claim->clearingHouseETIN() .
129 "~\n";
131 $HLcount = 1;
133 ++$edicount;
134 $out .= "HL" . // Loop 2000A Billing/Pay-To Provider HL Loop
135 "*$HLcount" .
136 "*" .
137 "*20" .
138 "*1" . // 1 indicates there are child segments
139 "~\n";
141 $HLBillingPayToProvider = $HLcount++;
143 // Situational PRV segment (for provider taxonomy code) omitted here.
144 // Situational CUR segment (foreign currency information) omitted here.
146 ++$edicount;
147 //Field length is limited to 35. See nucc dataset page 63 www.nucc.org
148 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
149 $out .= "NM1" . // Loop 2010AA Billing Provider
150 "*85" .
151 "*2" .
152 "*" . $billingFacilityName .
153 "*" .
154 "*" .
155 "*" .
156 "*";
157 if ($claim->billingFacilityNPI()) {
158 $out .= "*XX*" . $claim->billingFacilityNPI();
160 else {
161 $log .= "*** Billing facility has no NPI.\n";
162 if ($CMS_5010) {
163 $out .= "*XX*";
165 else {
166 $out .= "*24*" . $claim->billingFacilityETIN();
169 $out .= "~\n";
171 ++$edicount;
172 $out .= "N3" .
173 "*" . $claim->billingFacilityStreet() .
174 "~\n";
176 ++$edicount;
177 $out .= "N4" .
178 "*" . $claim->billingFacilityCity() .
179 "*" . $claim->billingFacilityState() .
180 "*" . stripZipCode($claim->billingFacilityZip()) .
181 "~\n";
183 if ($CMS_5010 || ($claim->billingFacilityNPI() && $claim->billingFacilityETIN())) {
184 ++$edicount;
185 $out .= "REF" ;
186 if($claim->federalIdType()){
187 $out .= "*" . $claim->federalIdType();
189 else{
190 $out .= "*EI"; // For dealing with the situation before adding TaxId type In facility.
192 $out .= "*" . $claim->billingFacilityETIN() .
193 "~\n";
196 if ($claim->providerNumberType() && $claim->providerNumber() &&
197 !($CMS_5010 && $claim->billingFacilityNPI()))
199 ++$edicount;
200 $out .= "REF" .
201 "*" . $claim->providerNumberType() .
202 "*" . $claim->providerNumber() .
203 "~\n";
205 else if ($claim->providerNumber() && !$claim->providerNumberType()) {
206 $log .= "*** Payer-specific provider insurance number is present but has no type assigned.\n";
209 // Situational PER*1C segment omitted.
211 // Pay-To Address defaults to billing provider and is no longer required in 5010.
212 if (!$CMS_5010) {
213 ++$edicount;
214 // Field length is limited to 35. See nucc dataset page 63 www.nucc.org
215 $billingFacilityName = substr($claim->billingFacilityName(), 0, $CMS_5010 ? 60 : 35);
216 $out .= "NM1" . // Loop 2010AB Pay-To Provider
217 "*87" .
218 "*2" .
219 "*" . $billingFacilityName .
220 "*" .
221 "*" .
222 "*" .
223 "*";
224 if ($claim->billingFacilityNPI())
225 $out .= "*XX*" . $claim->billingFacilityNPI();
226 else
227 $out .= "*24*" . $claim->billingFacilityETIN();
228 $out .= "~\n";
230 ++$edicount;
231 $out .= "N3" .
232 "*" . $claim->billingFacilityStreet() .
233 "~\n";
235 ++$edicount;
236 $out .= "N4" .
237 "*" . $claim->billingFacilityCity() .
238 "*" . $claim->billingFacilityState() .
239 "*" . stripZipCode($claim->billingFacilityZip()) .
240 "~\n";
242 if ($claim->billingFacilityNPI() && $claim->billingFacilityETIN()) {
243 ++$edicount;
244 $out .= "REF" .
245 "*EI" .
246 "*" . $claim->billingFacilityETIN() .
247 "~\n";
251 // Loop 2010AC Pay-To Plan Name omitted. Includes:
252 // NM1*PE, N3, N4, REF*2U, REF*EI
254 $PatientHL = $claim->isSelfOfInsured() ? 0 : 1;
255 $HLSubscriber = $HLcount++;
257 ++$edicount;
258 $out .= "HL" . // Loop 2000B Subscriber HL Loop
259 "*$HLSubscriber" .
260 "*$HLBillingPayToProvider" .
261 "*22" .
262 "*$PatientHL" .
263 "~\n";
265 if (!$claim->payerSequence()) {
266 $log .= "*** Error: Insurance information is missing!\n";
269 ++$edicount;
270 $out .= "SBR" . // Subscriber Information
271 "*" . $claim->payerSequence() .
272 "*" . ($claim->isSelfOfInsured() ? '18' : '') .
273 "*" . $claim->groupNumber() .
274 "*" . (($CMS_5010 && $claim->groupNumber()) ? '' : $claim->groupName()) .
275 "*" . $claim->insuredTypeCode() . // applies for secondary medicare
276 "*" .
277 "*" .
278 "*" .
279 "*" . $claim->claimType() . // Zirmed replaces this
280 "~\n";
282 // Segment PAT omitted.
284 ++$edicount;
285 $out .= "NM1" . // Loop 2010BA Subscriber
286 "*IL" .
287 "*1" . // 1 = person, 2 = non-person
288 "*" . $claim->insuredLastName() .
289 "*" . $claim->insuredFirstName() .
290 "*" . $claim->insuredMiddleName() .
291 "*" .
292 "*" . // Name Suffix
293 "*MI" .
294 // "MI" = Member Identification Number
295 // "II" = Standard Unique Health Identifier, "Required if the
296 // HIPAA Individual Patient Identifier is mandated use."
297 // Here we presume that is not true yet.
298 "*" . $claim->policyNumber() .
299 "~\n";
301 // For 5010, further subscriber info is sent only if they are the patient.
302 if (!$CMS_5010 || $claim->isSelfOfInsured()) {
303 ++$edicount;
304 $out .= "N3" .
305 "*" . $claim->insuredStreet() .
306 "~\n";
308 ++$edicount;
309 $out .= "N4" .
310 "*" . $claim->insuredCity() .
311 "*" . $claim->insuredState() .
312 "*" . stripZipCode($claim->insuredZip()) .
313 "~\n";
315 ++$edicount;
316 $out .= "DMG" .
317 "*D8" .
318 "*" . $claim->insuredDOB() .
319 "*" . $claim->insuredSex() .
320 "~\n";
323 // Segment REF*SY (Subscriber Secondary Identification) omitted.
324 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
325 // Segment PER*IC (Property and Casualty Subscriber Contact Information) omitted.
327 ++$edicount;
328 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
329 $payerName = substr($claim->payerName(), 0, $CMS_5010 ? 60 : 35);
330 $out .= "NM1" . // Loop 2010BB Payer
331 "*PR" .
332 "*2" .
333 "*" . $payerName .
334 "*" .
335 "*" .
336 "*" .
337 "*" .
338 // The 5010 spec says:
339 // "On or after the mandated implementation date for the HIPAA
340 // National Plan Identifier (National Plan ID), XV must be sent.
341 // Prior to the mandated implementation date and prior to any phase-
342 // in period identified by Federal regulation, PI must be sent."
343 // *************** Anybody know what that date is? ***************
344 "*PI" .
345 // Zirmed ignores this if using payer name matching:
346 "*" . ($encounter_claim ? $claim->payerAltID() : $claim->payerID()) .
347 "~\n";
349 // if (!$claim->payerID()) {
350 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName() . "'.\n";
351 // }
353 if (true) { // !$CMS_5010
354 // The 5010 spec says:
355 // "Required when the payer address is available and the submitter intends
356 // for the claim to be printed on paper at the next EDI location (for example, a
357 // clearinghouse). If not required by this implementation guide, do not send."
359 ++$edicount;
360 $out .= "N3" .
361 "*" . $claim->payerStreet() .
362 "~\n";
364 ++$edicount;
365 $out .= "N4" .
366 "*" . $claim->payerCity() .
367 "*" . $claim->payerState() .
368 "*" . stripZipCode($claim->payerZip()) .
369 "~\n";
372 // Segment REF (Payer Secondary Identification) omitted.
373 // Segment REF (Billing Provider Secondary Identification) omitted.
375 if (! $claim->isSelfOfInsured()) {
376 ++$edicount;
377 $out .= "HL" . // Loop 2000C Patient Information
378 "*$HLcount" .
379 "*$HLSubscriber" .
380 "*23" .
381 "*0" .
382 "~\n";
384 $HLcount++;
386 ++$edicount;
387 $out .= "PAT" .
388 "*" . $claim->insuredRelationship() .
389 "~\n";
391 ++$edicount;
392 $out .= "NM1" . // Loop 2010CA Patient
393 "*QC" .
394 "*1" .
395 "*" . $claim->patientLastName() .
396 "*" . $claim->patientFirstName() .
397 "*" . $claim->patientMiddleName() .
398 "~\n";
400 ++$edicount;
401 $out .= "N3" .
402 "*" . $claim->patientStreet() .
403 "~\n";
405 ++$edicount;
406 $out .= "N4" .
407 "*" . $claim->patientCity() .
408 "*" . $claim->patientState() .
409 "*" . stripZipCode($claim->patientZip()) .
410 "~\n";
412 ++$edicount;
413 $out .= "DMG" .
414 "*D8" .
415 "*" . $claim->patientDOB() .
416 "*" . $claim->patientSex() .
417 "~\n";
419 // Segment REF*Y4 (Property and Casualty Claim Number) omitted.
420 // Segment REF (Property and Casualty Patient Identifier) omitted.
421 // Segment PER (Property and Casualty Patient Contact Information) omitted.
423 } // end of patient different from insured
425 $proccount = $claim->procCount();
427 $clm_total_charges = 0;
428 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
429 $clm_total_charges += $claim->cptCharges($prockey);
432 if (!$clm_total_charges) {
433 $log .= "*** This claim has no charges!\n";
436 ++$edicount;
437 $out .= "CLM" . // Loop 2300 Claim
438 "*$pid-$encounter" .
439 "*" . sprintf("%.2f",$clm_total_charges) . // Zirmed computes and replaces this
440 "*" .
441 "*" .
442 "*" . sprintf('%02d', $claim->facilityPOS()) . ":" .
443 ($CMS_5010 ? "B" : "") . ":" .
444 $claim->frequencyTypeCode() . // Changed to correct single digit output
445 "*Y" .
446 "*A" .
447 "*" . ($claim->billingFacilityAssignment() ? 'Y' : 'N') .
448 "*Y" .
449 ($CMS_5010 ? "" : "*C") .
450 "~\n";
452 if ($claim->onsetDate() && ($claim->onsetDate()!==$claim->serviceDate())) {
453 ++$edicount;
454 $out .= "DTP" . // Date of Onset
455 "*431" .
456 "*D8" .
457 "*" . $claim->onsetDate() .
458 "~\n";
461 if ($claim->dateInitialTreatment()) {
462 ++$edicount;
463 $out .= "DTP" . // Date of Initial Treatment
464 "*454" .
465 "*D8" .
466 "*" . $claim->dateInitialTreatment() .
467 "~\n";
470 // Segment DTP*304 (Last Seen Date) omitted.
471 // Segment DTP*453 (Acute Manifestation Date) omitted.
472 // Segment DTP*439 (Accident Date) omitted.
473 // Segment DTP*484 (Last Menstrual Period Date) omitted.
474 // Segment DTP*455 (Last X-Ray Date) omitted.
475 // Segment DTP*471 (Hearing and Vision Prescription Date) omitted.
476 // Segments DTP (Disability Dates) omitted.
477 // Segment DTP*297 (Last Worked Date) omitted.
478 // Segment DTP*296 (Authorized Return to Work Date) omitted.
480 if (strcmp($claim->facilityPOS(),'21') == 0) {
481 ++$edicount;
482 $out .= "DTP" . // Date of Hospitalization
483 "*435" .
484 "*D8" .
485 "*" . $claim->onsetDate() .
486 "~\n";
489 // Segment DTP*096 (Discharge Date) omitted.
490 // Segments DTP (Assumed and Relinquished Care Dates) omitted.
491 // Segment DTP*444 (Property and Casualty Date of First Contact) omitted.
492 // Segment DTP*050 (Repricer Received Date) omitted.
493 // Segment PWK (Claim Supplemental Information) omitted.
494 // Segment CN1 (Contract Information) omitted.
496 $patientpaid = $claim->patientPaidAmount();
497 if ($patientpaid != 0) {
498 ++$edicount;
499 $out .= "AMT" . // Patient paid amount. Page 190/220.
500 "*F5" .
501 "*" . $patientpaid .
502 "~\n";
505 // Segment REF*4N (Service Authorization Exception Code) omitted.
506 // Segment REF*F5 (Mandatory Medicare Crossover Indicator) omitted.
507 // Segment REF*EW (Mammography Certification Number) omitted.
508 // Segment REF*9F (Referral Number) omitted.
510 if ($claim->priorAuth()) {
511 ++$edicount;
512 $out .= "REF" . // Prior Authorization Number
513 "*G1" .
514 "*" . $claim->priorAuth() .
515 "~\n";
518 // Segment REF*F8 (Payer Claim Control Number) omitted.
520 if ($claim->cliaCode() && ($CMS_5010 || $claim->claimType() === 'MB')) {
521 // Required by Medicare when in-house labs are done.
522 ++$edicount;
523 $out .= "REF" . // Clinical Laboratory Improvement Amendment Number
524 "*X4" .
525 "*" . $claim->cliaCode() .
526 "~\n";
529 // Segment REF*9A (Repriced Claim Number) omitted.
530 // Segment REF*9C (Adjusted Repriced Claim Number) omitted.
531 // Segment REF*LX (Investigational Device Exemption Number) omitted.
532 // Segment REF*D9 (Claim Identifier for Transmission Intermediaries) omitted.
533 // Segment REF*EA (Medical Record Number) omitted.
534 // Segment REF*P4 (Demonstration Project Identifier) omitted.
535 // Segment REF*1J (Care Plan Oversight) omitted.
536 // Segment K3 (File Information) omitted.
538 if ($claim->additionalNotes()) {
539 // Claim note.
540 ++$edicount;
541 $out .= "NTE" . // comments box 19
542 "*" . ($CMS_5010 ? "ADD" : "") .
543 "*" . $claim->additionalNotes() .
544 "~\n";
547 // Segment CR1 (Ambulance Transport Information) omitted.
548 // Segment CR2 (Spinal Manipulation Service Information) omitted.
549 // Segment CRC (Ambulance Certification) omitted.
550 // Segment CRC (Patient Condition Information: Vision) omitted.
551 // Segment CRC (Homebound Indicator) omitted.
552 // Segment CRC (EPSDT Referral) omitted.
554 // Diagnoses, up to $max_per_seg per HI segment.
555 $max_per_seg = $CMS_5010 ? 12 : 8;
556 $da = $claim->diagArray();
557 $diag_type_code = 'BK';
558 $tmp = 0;
559 foreach ($da as $diag) {
560 if ($tmp % $max_per_seg == 0) {
561 if ($tmp) $out .= "~\n";
562 ++$edicount;
563 $out .= "HI"; // Health Diagnosis Codes
565 $out .= "*$diag_type_code:" . $diag;
566 $diag_type_code = 'BF';
567 ++$tmp;
569 if ($tmp) $out .= "~\n";
571 // Segment HI*BP (Anesthesia Related Procedure) omitted.
572 // Segment HI*BG (Condition Information) omitted.
573 // Segment HCP (Claim Pricing/Repricing Information) omitted.
575 if ($claim->referrerLastName()) {
576 // Medicare requires referring provider's name and UPIN.
577 ++$edicount;
578 $out .= "NM1" . // Loop 2310A Referring Provider
579 "*DN" .
580 "*1" .
581 "*" . $claim->referrerLastName() .
582 "*" . $claim->referrerFirstName() .
583 "*" . $claim->referrerMiddleName() .
584 "*" .
585 "*";
586 if ($CMS_5010 || $claim->referrerNPI()) { $out .=
587 "*XX" .
588 "*" . $claim->referrerNPI();
589 } else { $out .=
590 "*34" . // not allowed for 5010
591 "*" . $claim->referrerSSN();
593 $out .= "~\n";
595 if (!$CMS_5010 && $claim->referrerTaxonomy()) {
596 ++$edicount;
597 $out .= "PRV" .
598 "*RF" . // ReFerring provider
599 "*ZZ" .
600 "*" . $claim->referrerTaxonomy() .
601 "~\n";
604 if (!CMS_5010 && $claim->referrerUPIN()) {
605 ++$edicount;
606 $out .= "REF" . // Referring Provider Secondary Identification
607 "*1G" .
608 "*" . $claim->referrerUPIN() .
609 "~\n";
613 ++$edicount;
614 $out .= "NM1" . // Loop 2310B Rendering Provider
615 "*82" .
616 "*1" .
617 "*" . $claim->providerLastName() .
618 "*" . $claim->providerFirstName() .
619 "*" . $claim->providerMiddleName() .
620 "*" .
621 "*";
622 if ($CMS_5010 || $claim->providerNPI()) { $out .=
623 "*XX" .
624 "*" . $claim->providerNPI();
625 } else { $out .=
626 "*34" . // not allowed for 5010
627 "*" . $claim->providerSSN();
628 $log .= "*** Rendering provider has no NPI.\n";
630 $out .= "~\n";
632 if ($claim->providerTaxonomy()) {
633 ++$edicount;
634 $out .= "PRV" .
635 "*PE" . // PErforming provider
636 "*" . ($CMS_5010 ? "PXC" : "ZZ") .
637 "*" . $claim->providerTaxonomy() .
638 "~\n";
641 // 4010: REF*1C is required here for the Medicare provider number if NPI was
642 // specified in NM109. Not sure if other payers require anything here.
643 // --- apparently ECLAIMS, INC wants the data in 2010 but NOT in 2310B - tony@mi-squared.com
645 // 5010 spec says nothing here if NPI was specified.
647 if (($CMS_5010 && !$claim->providerNPI() && in_array($claim->providerNumberType(), array('0B','1G','G2','LU')))
648 || (!$CMS_5010 && trim($claim->x12gsreceiverid()) != '470819582')) // if NOT ECLAIMS EDI
650 if ($claim->providerNumber()) {
651 ++$edicount;
652 $out .= "REF" .
653 "*" . $claim->providerNumberType() .
654 "*" . $claim->providerNumber() .
655 "~\n";
659 // Loop 2310D is omitted in the case of home visits (POS=12).
660 if ($claim->facilityPOS() != 12 &&
661 (!$CMS_5010 || $claim->facilityNPI() != $claim->billingFacilityNPI()))
663 ++$edicount;
664 $out .= "NM1" . // Loop 2310D Service Location
665 "*77" .
666 "*2";
667 //Field length is limited to 35. See nucc dataset page 77 www.nucc.org
668 $facilityName = substr($claim->facilityName(), 0, $CMS_5010 ? 60 : 35);
669 if ($claim->facilityName() || $claim->facilityNPI() || $claim->facilityETIN()) { $out .=
670 "*" . $facilityName;
672 if ($claim->facilityNPI() || $claim->facilityETIN()) { $out .=
673 "*" .
674 "*" .
675 "*" .
676 "*";
677 if ($CMS_5010 || $claim->facilityNPI()) { $out .=
678 "*XX*" . $claim->facilityNPI();
679 } else { $out .=
680 "*24*" . $claim->facilityETIN();
682 if (!$claim->facilityNPI()) {
683 $log .= "*** Service location has no NPI.\n";
686 $out .= "~\n";
687 if ($claim->facilityStreet()) {
688 ++$edicount;
689 $out .= "N3" .
690 "*" . $claim->facilityStreet() .
691 "~\n";
693 if ($claim->facilityState()) {
694 ++$edicount;
695 $out .= "N4" .
696 "*" . $claim->facilityCity() .
697 "*" . $claim->facilityState() .
698 "*" . stripZipCode($claim->facilityZip()) .
699 "~\n";
703 // Segment REF (Service Facility Location Secondary Identification) omitted.
704 // Segment PER (Service Facility Contact Information) omitted.
706 // Loop 2310E, Supervising Provider
708 if ($claim->supervisorLastName()) {
709 ++$edicount;
710 $out .= "NM1" .
711 "*DQ" . // Supervising Physician
712 "*1" . // Person
713 "*" . $claim->supervisorLastName() .
714 "*" . $claim->supervisorFirstName() .
715 "*" . $claim->supervisorMiddleName() .
716 "*" . // NM106 not used
717 "*"; // Name Suffix
718 if ($CMS_5010 || $claim->supervisorNPI()) { $out .=
719 "*XX" .
720 "*" . $claim->supervisorNPI();
721 } else { $out .=
722 "*34" .
723 "*" . $claim->supervisorSSN();
725 if (!$claim->supervisorNPI()) {
726 $log .= "*** Supervising Provider has no NPI.\n";
728 $out .= "~\n";
730 if ($claim->supervisorNumber()) {
731 ++$edicount;
732 $out .= "REF" .
733 "*" . $claim->supervisorNumberType() .
734 "*" . $claim->supervisorNumber() .
735 "~\n";
739 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
740 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
742 $prev_pt_resp = $clm_total_charges; // for computation below
744 // Loops 2320 and 2330*, other subscriber/payer information.
745 // Remember that insurance index 0 is always for the payer being billed
746 // by this claim, and 1 and above are always for the "other" payers.
748 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
750 $tmp1 = $claim->claimType($ins);
751 $tmp2 = 'C1'; // Here a kludge. See page 321.
752 if ($tmp1 === 'CI') $tmp2 = 'C1';
753 if ($tmp1 === 'AM') $tmp2 = 'AP';
754 if ($tmp1 === 'HM') $tmp2 = 'HM';
755 if ($tmp1 === 'MB') $tmp2 = 'MB';
756 if ($tmp1 === 'MC') $tmp2 = 'MC';
757 if ($tmp1 === '09') $tmp2 = 'PP';
758 ++$edicount;
759 $out .= "SBR" . // Loop 2320, Subscriber Information - page 297/318
760 "*" . $claim->payerSequence($ins) .
761 "*" . $claim->insuredRelationship($ins) .
762 "*" . $claim->groupNumber($ins) .
763 "*" . (($CMS_5010 && $claim->groupNumber($ins)) ? '' : $claim->groupName($ins)) .
764 "*" . ($CMS_5010 ? $claim->insuredTypeCode($ins) : $tmp2) .
765 "*" .
766 "*" .
767 "*" .
768 "*" . $claim->claimType($ins) .
769 "~\n";
771 // Things that apply only to previous payers, not future payers.
773 if ($claim->payerSequence($ins) < $claim->payerSequence()) {
775 // Generate claim-level adjustments.
776 $aarr = $claim->payerAdjustments($ins);
777 foreach ($aarr as $a) {
778 ++$edicount;
779 $out .= "CAS" . // Previous payer's claim-level adjustments. Page 301/323.
780 "*" . $a[1] .
781 "*" . $a[2] .
782 "*" . $a[3] .
783 "~\n";
786 $payerpaid = $claim->payerTotals($ins);
787 ++$edicount;
788 $out .= "AMT" . // Previous payer's paid amount. Page 307/332.
789 "*D" .
790 "*" . $payerpaid[1] .
791 "~\n";
793 // Segment AMT*A8 (COB Total Non-Covered Amount) omitted.
794 // Segment AMT*EAF (Remaining Patient Liability) omitted.
796 if (!$CMS_5010) {
797 // Patient responsibility amount as of this previous payer.
798 $prev_pt_resp -= $payerpaid[1]; // reduce by payments
799 $prev_pt_resp -= $payerpaid[2]; // reduce by adjustments
801 ++$edicount;
802 $out .= "AMT" . // Allowed amount per previous payer. Page 334.
803 "*B6" .
804 "*" . sprintf('%.2f', $payerpaid[1] + $prev_pt_resp) .
805 "~\n";
807 ++$edicount;
808 $out .= "AMT" . // Patient responsibility amount per previous payer. Page 335.
809 "*F2" .
810 "*" . sprintf('%.2f', $prev_pt_resp) .
811 "~\n";
813 } // End of things that apply only to previous payers.
815 if (!$CMS_5010) {
816 ++$edicount;
817 $out .= "DMG" . // Other subscriber demographic information. Page 342.
818 "*D8" .
819 "*" . $claim->insuredDOB($ins) .
820 "*" . $claim->insuredSex($ins) .
821 "~\n";
824 ++$edicount;
825 $out .= "OI" . // Other Insurance Coverage Information. Page 310/344.
826 "*" .
827 "*" .
828 "*" . ($claim->billingFacilityAssignment($ins) ? 'Y' : 'N') .
829 // For this next item, the 5010 example in the spec does not match its
830 // description. So this might be wrong.
831 "*" . ($CMS_5010 ? '' : 'B') .
832 "*" .
833 "*Y" .
834 "~\n";
836 // Segment MOA (Medicare Outpatient Adjudication) omitted.
838 ++$edicount;
839 $out .= "NM1" . // Loop 2330A Subscriber info for other insco. Page 315/350.
840 "*IL" .
841 "*1" .
842 "*" . $claim->insuredLastName($ins) .
843 "*" . $claim->insuredFirstName($ins) .
844 "*" . $claim->insuredMiddleName($ins) .
845 "*" .
846 "*" .
847 "*MI" .
848 "*" . $claim->policyNumber($ins) .
849 "~\n";
851 ++$edicount;
852 $out .= "N3" .
853 "*" . $claim->insuredStreet($ins) .
854 "~\n";
856 ++$edicount;
857 $out .= "N4" .
858 "*" . $claim->insuredCity($ins) .
859 "*" . $claim->insuredState($ins) .
860 "*" . stripZipCode($claim->insuredZip($ins)) .
861 "~\n";
863 // Segment REF (Other Subscriber Secondary Identification) omitted.
865 ++$edicount;
866 //Field length is limited to 35. See nucc dataset page 81 www.nucc.org
867 $payerName = substr($claim->payerName($ins), 0, $CMS_5010 ? 60 : 35);
868 $out .= "NM1" . // Loop 2330B Payer info for other insco. Page 322/359.
869 "*PR" .
870 "*2" .
871 "*" . $payerName .
872 "*" .
873 "*" .
874 "*" .
875 "*" .
876 "*PI" .
877 "*" . $claim->payerID($ins) .
878 "~\n";
880 // if (!$claim->payerID($ins)) {
881 // $log .= "*** CMS ID is missing for payer '" . $claim->payerName($ins) . "'.\n";
882 // }
884 // Payer address (N3 and N4) are added below so that Gateway EDI can
885 // auto-generate secondary claims. These do NOT appear in my copy of
886 // the spec! -- Rod 2008-06-12
888 if ($CMS_5010 || trim($claim->x12gsreceiverid()) == '431420764') { // if Gateway EDI
889 ++$edicount;
890 $out .= "N3" .
891 "*" . $claim->payerStreet($ins) .
892 "~\n";
894 ++$edicount;
895 $out .= "N4" .
896 "*" . $claim->payerCity($ins) .
897 "*" . $claim->payerState($ins) .
898 "*" . stripZipCode($claim->payerZip($ins)) .
899 "~\n";
900 } // end Gateway EDI
902 // Segment DTP*573 (Claim Check or Remittance Date) omitted.
903 // Segment REF (Other Payer Secondary Identifier) omitted.
904 // Segment REF*G1 (Other Payer Prior Authorization Number) omitted.
905 // Segment REF*9F (Other Payer Referral Number) omitted.
906 // Segment REF*T4 (Other Payer Claim Adjustment Indicator) omitted.
907 // Segment REF*F8 (Other Payer Claim Control Number) omitted.
908 // Segment NM1 (Other Payer Referring Provider) omitted.
909 // Segment REF (Other Payer Referring Provider Secondary Identification) omitted.
910 // Segment NM1 (Other Payer Rendering Provider) omitted.
911 // Segment REF (Other Payer Rendering Provider Secondary Identification) omitted.
912 // Segment NM1 (Other Payer Service Facility Location) omitted.
913 // Segment REF (Other Payer Service Facility Location Secondary Identification) omitted.
914 // Segment NM1 (Other Payer Supervising Provider) omitted.
915 // Segment REF (Other Payer Supervising Provider Secondary Identification) omitted.
916 // Segment NM1 (Other Payer Billing Provider) omitted.
917 // Segment REF (Other Payer Billing Provider Secondary Identification) omitted.
919 } // End loops 2320/2330*.
921 $loopcount = 0;
923 // Procedure loop starts here.
925 for ($prockey = 0; $prockey < $proccount; ++$prockey) {
926 ++$loopcount;
928 ++$edicount;
929 $out .= "LX" . // Loop 2400 LX Service Line. Page 398.
930 "*$loopcount" .
931 "~\n";
933 ++$edicount;
934 $out .= "SV1" . // Professional Service. Page 400.
935 "*HC:" . $claim->cptKey($prockey) .
936 "*" . sprintf('%.2f', $claim->cptCharges($prockey)) .
937 "*UN" .
938 "*" . $claim->cptUnits($prockey) .
939 "*" .
940 "*" .
941 "*";
942 $dia = $claim->diagIndexArray($prockey);
943 $i = 0;
944 foreach ($dia as $dindex) {
945 if ($i) $out .= ':';
946 $out .= $dindex;
947 if (++$i >= 4) break;
949 $out .= "~\n";
951 if (!$claim->cptCharges($prockey)) {
952 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' has no charges!\n";
955 if (empty($dia)) {
956 $log .= "*** Procedure '" . $claim->cptKey($prockey) . "' is not justified!\n";
959 // Segment SV5 (Durable Medical Equipment Service) omitted.
960 // Segment PWK (Line Supplemental Information) omitted.
961 // Segment PWK (Durable Medical Equipment Certificate of Medical Necessity Indicator) omitted.
962 // Segment CR1 (Ambulance Transport Information) omitted.
963 // Segment CR3 (Durable Medical Equipment Certification) omitted.
964 // Segment CRC (Ambulance Certification) omitted.
965 // Segment CRC (Hospice Employee Indicator) omitted.
966 // Segment CRC (Condition Indicator / Durable Medical Equipment) omitted.
968 ++$edicount;
969 $out .= "DTP" . // Date of Service. Page 435.
970 "*472" .
971 "*D8" .
972 "*" . $claim->serviceDate() .
973 "~\n";
975 $testnote = rtrim($claim->cptNotecodes($prockey));
976 if (!empty($testnote)) {
977 ++$edicount;
978 $out .= "NTE" . // Explain Unusual Circumstances.
979 "*ADD" .
980 "*" . $claim->cptNotecodes($prockey) .
981 "~\n";
984 // Segment DTP*471 (Prescription Date) omitted.
985 // Segment DTP*607 (Revision/Recertification Date) omitted.
986 // Segment DTP*463 (Begin Therapy Date) omitted.
987 // Segment DTP*461 (Last Certification Date) omitted.
988 // Segment DTP*304 (Last Seen Date) omitted.
989 // Segment DTP (Test Date) omitted.
990 // Segment DTP*011 (Shipped Date) omitted.
991 // Segment DTP*455 (Last X-Ray Date) omitted.
992 // Segment DTP*454 (Initial Treatment Date) omitted.
993 // Segment QTY (Ambulance Patient Count) omitted.
994 // Segment QTY (Obstetric Anesthesia Additional Units) omitted.
995 // Segment MEA (Test Result) omitted.
996 // Segment CN1 (Contract Information) omitted.
997 // Segment REF*9B (Repriced Line Item Reference Number) omitted.
998 // Segment REF*9D (Adjusted Repriced Line Item Reference Number) omitted.
999 // Segment REF*G1 (Prior Authorization) omitted.
1000 // Segment REF*6R (Line Item Control Number) omitted.
1001 // (Really oughta have this for robust 835 posting!)
1002 // Segment REF*EW (Mammography Certification Number) omitted.
1003 // Segment REF*X4 (CLIA Number) omitted.
1004 // Segment REF*F4 (Referring CLIA Facility Identification) omitted.
1005 // Segment REF*BT (Immunization Batch Number) omitted.
1006 // Segment REF*9F (Referral Number) omitted.
1007 // Segment AMT*T (Sales Tax Amount) omitted.
1008 // Segment AMT*F4 (Postage Claimed Amount) omitted.
1009 // Segment K3 (File Information) omitted.
1010 // Segment NTE (Line Note) omitted.
1011 // Segment NTE (Third Party Organization Notes) omitted.
1012 // Segment PS1 (Purchased Service Information) omitted.
1013 // Segment HCP (Line Pricing/Repricing Information) omitted.
1015 if (!$CMS_5010) {
1016 // This segment was deleted for 5010.
1018 // AMT*AAE segment for Approved Amount from previous payer.
1019 // Medicare secondaries seem to require this.
1021 for ($ins = $claim->payerCount() - 1; $ins > 0; --$ins) {
1022 if ($claim->payerSequence($ins) > $claim->payerSequence())
1023 continue; // payer is future, not previous
1024 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1025 ++$edicount;
1026 $out .= "AMT" . // Approved amount per previous payer. Page 485.
1027 "*AAE" .
1028 "*" . sprintf('%.2f', $claim->cptCharges($prockey) - $payerpaid[2]) .
1029 "~\n";
1030 break;
1034 // Loop 2410, Drug Information. Medicaid insurers seem to want this
1035 // with HCPCS codes.
1037 $ndc = $claim->cptNDCID($prockey);
1038 if ($ndc) {
1039 ++$edicount;
1040 $out .= "LIN" . // Drug Identification. Page 500+ (Addendum pg 71).
1041 "*" . // Per addendum, LIN01 is not used.
1042 "*N4" .
1043 "*" . $ndc .
1044 "~\n";
1046 if (!preg_match('/^\d\d\d\d\d-\d\d\d\d-\d\d$/', $ndc, $tmp) && !preg_match('/^\d{11}$/', $ndc)) {
1047 $log .= "*** NDC code '$ndc' has invalid format!\n";
1050 ++$edicount;
1051 $tmpunits = $claim->cptNDCQuantity($prockey) * $claim->cptUnits($prockey);
1052 if (!$tmpunits) $tmpunits = 1;
1053 $out .= "CTP" . // Drug Pricing. Page 500+ (Addendum pg 74).
1054 "*" .
1055 "*" .
1056 "*" . ($CMS_5010 ? '' : sprintf('%.2f', $claim->cptCharges($prockey) / $tmpunits)) .
1057 "*" . $claim->cptNDCQuantity($prockey) .
1058 "*" . $claim->cptNDCUOM($prockey) .
1059 // Note: 5010 documents "ME" (Milligrams) as an additional unit of measure.
1060 "~\n";
1063 // Segment REF (Prescription or Compound Drug Association Number) omitted.
1065 // Loop 2420A, Rendering Provider (service-specific).
1066 // Used if the rendering provider for this service line is different
1067 // from that in loop 2310B.
1069 if ($claim->providerNPI() != $claim->providerNPI($prockey)) {
1070 ++$edicount;
1071 $out .= "NM1" . // Loop 2310B Rendering Provider
1072 "*82" .
1073 "*1" .
1074 "*" . $claim->providerLastName($prockey) .
1075 "*" . $claim->providerFirstName($prockey) .
1076 "*" . $claim->providerMiddleName($prockey) .
1077 "*" .
1078 "*";
1079 if ($CMS_5010 || $claim->providerNPI($prockey)) { $out .=
1080 "*XX" .
1081 "*" . $claim->providerNPI($prockey);
1082 } else { $out .=
1083 "*34" . // Not allowed for 5010
1084 "*" . $claim->providerSSN($prockey);
1086 if (!$claim->providerNPI($prockey)) {
1087 $log .= "*** Rendering provider has no NPI.\n";
1089 $out .= "~\n";
1091 if ($claim->providerTaxonomy($prockey)) {
1092 ++$edicount;
1093 $out .= "PRV" .
1094 "*PE" . // PErforming provider
1095 "*" . ($CMS_5010 ? "PXC" : "ZZ") .
1096 "*" . $claim->providerTaxonomy($prockey) .
1097 "~\n";
1100 // Segment PRV*PE (Rendering Provider Specialty Information) omitted.
1101 // Segment REF (Rendering Provider Secondary Identification) omitted.
1102 // Segment NM1 (Purchased Service Provider Name) omitted.
1103 // Segment REF (Purchased Service Provider Secondary Identification) omitted.
1104 // Segment NM1,N3,N4 (Service Facility Location) omitted.
1105 // Segment REF (Service Facility Location Secondary Identification) omitted.
1106 // Segment NM1 (Supervising Provider Name) omitted.
1107 // Segment REF (Supervising Provider Secondary Identification) omitted.
1108 // Segment NM1,N3,N4 (Ordering Provider) omitted.
1109 // Segment REF (Ordering Provider Secondary Identification) omitted.
1110 // Segment PER (Ordering Provider Contact Information) omitted.
1111 // Segment NM1 (Referring Provider Name) omitted.
1112 // Segment REF (Referring Provider Secondary Identification) omitted.
1113 // Segments NM1*PW, N3, N4 (Ambulance Pick-Up Location) omitted.
1114 // Segments NM1*45, N3, N4 (Ambulance Drop-Off Location) omitted.
1116 // REF*1C is required here for the Medicare provider number if NPI was
1117 // specified in NM109. Not sure if other payers require anything here.
1118 if (!$CMS_5010 && $claim->providerNumber($prockey)) {
1119 ++$edicount;
1120 $out .= "REF" .
1121 "*" . $claim->providerNumberType($prockey) .
1122 // Note: 5010 documents that type 1D (Medicaid) is changed to G2.
1123 "*" . $claim->providerNumber($prockey) .
1124 "~\n";
1128 // Loop 2430, adjudication by previous payers.
1130 for ($ins = 1; $ins < $claim->payerCount(); ++$ins) {
1131 if ($claim->payerSequence($ins) > $claim->payerSequence())
1132 continue; // payer is future, not previous
1134 $payerpaid = $claim->payerTotals($ins, $claim->cptKey($prockey));
1135 $aarr = $claim->payerAdjustments($ins, $claim->cptKey($prockey));
1137 if ($payerpaid[1] == 0 && !count($aarr)) {
1138 $log .= "*** Procedure '" . $claim->cptKey($prockey) .
1139 "' has no payments or adjustments from previous payer!\n";
1140 continue;
1143 ++$edicount;
1144 $out .= "SVD" . // Service line adjudication. Page 554.
1145 "*" . $claim->payerID($ins) .
1146 "*" . $payerpaid[1] .
1147 "*HC:" . $claim->cptKey($prockey) .
1148 "*" .
1149 "*" . $claim->cptUnits($prockey) .
1150 "~\n";
1152 $tmpdate = $payerpaid[0];
1153 foreach ($aarr as $a) {
1154 ++$edicount;
1155 $out .= "CAS" . // Previous payer's line level adjustments. Page 558.
1156 "*" . $a[1] .
1157 "*" . $a[2] .
1158 "*" . $a[3] .
1159 "~\n";
1160 if (!$tmpdate) $tmpdate = $a[0];
1162 // WTH is this??
1163 /*************************************************************
1164 if ( isset($a[4]) &&
1165 $a[4] != null ) {
1166 $out .= "CAS02" . // Previous payer's adjustment reason
1167 "*" . $a[4] .
1168 "~\n";
1170 *************************************************************/
1173 if ($tmpdate) {
1174 ++$edicount;
1175 $out .= "DTP" . // Previous payer's line adjustment date. Page 493/566.
1176 "*573" .
1177 "*D8" .
1178 "*$tmpdate" .
1179 "~\n";
1182 // Segment AMT*EAF (Remaining Patient Liability) omitted.
1183 // Segment LQ (Form Identification Code) omitted.
1184 // Segment FRM (Supporting Documentation) omitted.
1186 } // end loop 2430
1187 } // end this procedure
1189 ++$edicount;
1190 $out .= "SE" . // SE Trailer
1191 "*$edicount" .
1192 "*0021" .
1193 "~\n";
1195 $out .= "GE" . // GE Trailer
1196 "*1" .
1197 "*1" .
1198 "~\n";
1200 $out .= "IEA" . // IEA Trailer
1201 "*1" .
1202 "*000000001" .
1203 "~\n";
1205 // Remove any trailing empty fields (delimiters) from each segment.
1206 $out = preg_replace('/\*+~/', '~', $out);
1208 $log .= "\n";
1209 return $out;